aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/language/itemreaderastvisitor.cpp
blob: 901772d162b7527d49360d53ca86ffa55a8fdb4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "itemreaderastvisitor.h"

#include "astimportshandler.h"
#include "astpropertiesitemhandler.h"
#include "asttools.h"
#include "builtindeclarations.h"
#include "filecontext.h"
#include "identifiersearch.h"
#include "item.h"
#include "itemreadervisitorstate.h"
#include "value.h"

#include <api/languageinfo.h>
#include <jsextensions/jsextensions.h>
#include <parser/qmljsast_p.h>
#include <tools/codelocation.h>
#include <tools/error.h>
#include <tools/qbsassert.h>
#include <tools/qttools.h>
#include <tools/stringconstants.h>
#include <logging/translator.h>

#include <algorithm>

using namespace QbsQmlJS;

namespace qbs {
namespace Internal {

ItemReaderASTVisitor::ItemReaderASTVisitor(ItemReaderVisitorState &visitorState,
        FileContextPtr file, ItemPool *itemPool, Logger &logger)
    : m_visitorState(visitorState)
    , m_file(std::move(file))
    , m_itemPool(itemPool)
    , m_logger(logger)
{
}

bool ItemReaderASTVisitor::visit(AST::UiProgram *uiProgram)
{
    ASTImportsHandler importsHandler(m_visitorState, m_logger, m_file);
    importsHandler.handleImports(uiProgram->imports);
    m_typeNameToFile = importsHandler.typeNameFileMap();
    return true;
}

static ItemValuePtr findItemProperty(const Item *container, const Item *item)
{
    ItemValuePtr itemValue;
    const auto &srcprops = container->properties();
    auto it = std::find_if(srcprops.begin(), srcprops.end(), [item] (const ValuePtr &v) {
        return v->type() == Value::ItemValueType
                && std::static_pointer_cast<ItemValue>(v)->item() == item;
    });
    if (it != srcprops.end())
        itemValue = std::static_pointer_cast<ItemValue>(it.value());
    return itemValue;
}

bool ItemReaderASTVisitor::visit(AST::UiObjectDefinition *ast)
{
    const QString typeName = ast->qualifiedTypeNameId->name.toString();
    const CodeLocation itemLocation = toCodeLocation(ast->qualifiedTypeNameId->identifierToken);
    const Item *baseItem = nullptr;
    Item *mostDerivingItem = nullptr;

    Item *item = Item::create(m_itemPool, ItemType::Unknown);
    item->setFile(m_file);
    item->setLocation(itemLocation);

    // Inheritance resolving, part 1: Find out our actual type name (needed for setting
    // up children and alternatives).
    const QStringList fullTypeName = toStringList(ast->qualifiedTypeNameId);
    const QString baseTypeFileName = m_typeNameToFile.value(fullTypeName);
    ItemType itemType;
    if (!baseTypeFileName.isEmpty()) {
        const bool isMostDerivingItem = (m_visitorState.mostDerivingItem() == nullptr);
        if (isMostDerivingItem)
            m_visitorState.setMostDerivingItem(item);
        mostDerivingItem = m_visitorState.mostDerivingItem();
        baseItem = m_visitorState.readFile(baseTypeFileName, m_file->searchPaths(), m_itemPool);
        if (isMostDerivingItem)
            m_visitorState.setMostDerivingItem(nullptr);
        QBS_CHECK(baseItem->type() <= ItemType::LastActualItem);
        itemType = baseItem->type();
    } else {
        if (fullTypeName.size() > 1) {
            throw ErrorInfo(Tr::tr("Invalid item '%1'. Did you mean to set a module property?")
                            .arg(fullTypeName.join(QLatin1Char('.'))), itemLocation);
        }
        itemType = BuiltinDeclarations::instance().typeForName(typeName, itemLocation);
        checkDeprecationStatus(itemType, typeName, itemLocation);
        if (itemType == ItemType::Properties && m_item && m_item->type() == ItemType::SubProject)
            itemType = ItemType::PropertiesInSubProject;
    }

    item->m_type = itemType;

    if (m_item)
        Item::addChild(m_item, item); // Add this item to the children of the parent item.
    else
        m_item = item; // This is the root item.

    if (ast->initializer) {
        Item *mdi = m_visitorState.mostDerivingItem();
        m_visitorState.setMostDerivingItem(nullptr);
        qSwap(m_item, item);
        const ItemType oldInstanceItemType = m_instanceItemType;
        if (itemType == ItemType::Parameters || itemType == ItemType::Depends)
            m_instanceItemType = ItemType::ModuleParameters;
        ast->initializer->accept(this);
        m_instanceItemType = oldInstanceItemType;
        qSwap(m_item, item);
        m_visitorState.setMostDerivingItem(mdi);
    }

    ASTPropertiesItemHandler(item).handlePropertiesItems();

    // Inheritance resolving, part 2 (depends on alternatives having been set up).
    if (baseItem) {
        inheritItem(item, baseItem);
        if (baseItem->file()->idScope()) {
            // Make ids from the derived file visible in the base file.
            // ### Do we want to turn off this feature? It's QMLish but kind of strange.
            item->file()->ensureIdScope(m_itemPool);
            baseItem->file()->idScope()->setPrototype(item->file()->idScope());

            // Replace the base item with the most deriving item.
            ItemValuePtr baseItemIdValue = findItemProperty(baseItem->file()->idScope(), baseItem);
            if (baseItemIdValue)
                baseItemIdValue->setItem(mostDerivingItem);
        }
    } else {
        // Only the item at the top of the inheritance chain is a built-in item.
        // We cannot do this in "part 1", because then the visitor would complain about duplicate
        // bindings.
        item->setupForBuiltinType(m_logger);
    }

    return false;
}

void ItemReaderASTVisitor::checkDuplicateBinding(Item *item, const QStringList &bindingName,
                                                 const AST::SourceLocation &sourceLocation)
{
    if (Q_UNLIKELY(item->hasOwnProperty(bindingName.last()))) {
        QString msg = Tr::tr("Duplicate binding for '%1'");
        throw ErrorInfo(msg.arg(bindingName.join(QLatin1Char('.'))),
                    toCodeLocation(sourceLocation));
    }
}

bool ItemReaderASTVisitor::visit(AST::UiPublicMember *ast)
{
    PropertyDeclaration p;
    if (Q_UNLIKELY(ast->name.isEmpty()))
        throw ErrorInfo(Tr::tr("public member without name"));
    if (Q_UNLIKELY(ast->memberType.isEmpty()))
        throw ErrorInfo(Tr::tr("public member without type"));
    if (Q_UNLIKELY(ast->type == AST::UiPublicMember::Signal))
        throw ErrorInfo(Tr::tr("public member with signal type not supported"));
    p.setName(ast->name.toString());
    p.setType(PropertyDeclaration::propertyTypeFromString(ast->memberType.toString()));
    if (p.type() == PropertyDeclaration::UnknownType) {
        throw ErrorInfo(Tr::tr("Unknown type '%1' in property declaration.")
                        .arg(ast->memberType.toString()), toCodeLocation(ast->typeToken));
    }
    if (Q_UNLIKELY(!ast->typeModifier.isEmpty())) {
        throw ErrorInfo(Tr::tr("public member with type modifier '%1' not supported").arg(
                        ast->typeModifier.toString()));
    }
    if (ast->isReadonlyMember)
        p.setFlags(PropertyDeclaration::ReadOnlyFlag);

    m_item->m_propertyDeclarations.insert(p.name(), p);

    const JSSourceValuePtr value = JSSourceValue::create();
    value->setFile(m_file);
    if (ast->statement) {
        handleBindingRhs(ast->statement, value);
        const QStringList bindingName(p.name());
        checkDuplicateBinding(m_item, bindingName, ast->colonToken);
    }

    m_item->setProperty(p.name(), value);
    return false;
}

bool ItemReaderASTVisitor::visit(AST::UiScriptBinding *ast)
{
    QBS_CHECK(ast->qualifiedId);
    QBS_CHECK(!ast->qualifiedId->name.isEmpty());

    const QStringList bindingName = toStringList(ast->qualifiedId);

    if (bindingName.length() == 1 && bindingName.front() == QStringLiteral("id")) {
        const auto * const expStmt = AST::cast<AST::ExpressionStatement *>(ast->statement);
        if (Q_UNLIKELY(!expStmt))
            throw ErrorInfo(Tr::tr("id: must be followed by identifier"));
        const auto * const idExp = AST::cast<AST::IdentifierExpression *>(expStmt->expression);
        if (Q_UNLIKELY(!idExp || idExp->name.isEmpty()))
            throw ErrorInfo(Tr::tr("id: must be followed by identifier"));
        m_item->m_id = idExp->name.toString();
        m_file->ensureIdScope(m_itemPool);
        ItemValueConstPtr existingId = m_file->idScope()->itemProperty(m_item->id());
        if (existingId) {
            ErrorInfo e(Tr::tr("The id '%1' is not unique.").arg(m_item->id()));
            e.append(Tr::tr("First occurrence is here."), existingId->item()->location());
            e.append(Tr::tr("Next occurrence is here."), m_item->location());
            throw e;
        }
        m_file->idScope()->setProperty(m_item->id(), ItemValue::create(m_item));
        return false;
    }

    const JSSourceValuePtr value = JSSourceValue::create();
    handleBindingRhs(ast->statement, value);

    Item * const targetItem = targetItemForBinding(bindingName, value);
    checkDuplicateBinding(targetItem, bindingName, ast->qualifiedId->identifierToken);
    targetItem->setProperty(bindingName.last(), value);
    return false;
}

bool ItemReaderASTVisitor::handleBindingRhs(AST::Statement *statement,
                                            const JSSourceValuePtr &value)
{
    QBS_CHECK(statement);
    QBS_CHECK(value);

    if (AST::cast<AST::Block *>(statement))
        value->m_flags |= JSSourceValue::HasFunctionForm;

    value->setFile(m_file);
    value->setSourceCode(textRefOf(m_file->content(), statement));
    value->setLocation(statement->firstSourceLocation().startLine,
                       statement->firstSourceLocation().startColumn);

    bool usesBase, usesOuter, usesOriginal;
    IdentifierSearch idsearch;
    idsearch.add(StringConstants::baseVar(), &usesBase);
    idsearch.add(StringConstants::outerVar(), &usesOuter);
    idsearch.add(StringConstants::originalVar(), &usesOriginal);
    idsearch.start(statement);
    if (usesBase)
        value->m_flags |= JSSourceValue::SourceUsesBase;
    if (usesOuter)
        value->m_flags |= JSSourceValue::SourceUsesOuter;
    if (usesOriginal)
        value->m_flags |= JSSourceValue::SourceUsesOriginal;
    return false;
}

CodeLocation ItemReaderASTVisitor::toCodeLocation(const AST::SourceLocation &location) const
{
    return CodeLocation(m_file->filePath(), location.startLine, location.startColumn);
}

Item *ItemReaderASTVisitor::targetItemForBinding(const QStringList &bindingName,
                                                 const JSSourceValueConstPtr &value)
{
    Item *targetItem = m_item;
    const int c = bindingName.size() - 1;
    for (int i = 0; i < c; ++i) {
        ValuePtr v = targetItem->ownProperty(bindingName.at(i));
        if (!v) {
            const ItemType itemType = i < c - 1 ? ItemType::ModulePrefix : m_instanceItemType;
            Item *newItem = Item::create(m_itemPool, itemType);
            newItem->setLocation(value->location());
            v = ItemValue::create(newItem);
            targetItem->setProperty(bindingName.at(i), v);
        }
        if (Q_UNLIKELY(v->type() != Value::ItemValueType)) {
            QString msg = Tr::tr("Binding to non-item property.");
            throw ErrorInfo(msg, value->location());
        }
        targetItem = std::static_pointer_cast<ItemValue>(v)->item();
    }
    return targetItem;
}

void ItemReaderASTVisitor::inheritItem(Item *dst, const Item *src)
{
    int insertPos = 0;
    for (Item *child : qAsConst(src->m_children)) {
        dst->m_children.insert(insertPos++, child);
        child->m_parent = dst;
    }

    for (const PropertyDeclaration &pd : src->propertyDeclarations()) {
        if (pd.flags().testFlag(PropertyDeclaration::ReadOnlyFlag)
                && dst->hasOwnProperty(pd.name())) {
            throw ErrorInfo(Tr::tr("Cannot set read-only property '%1'.").arg(pd.name()),
                            dst->property(pd.name())->location());
        }
        dst->setPropertyDeclaration(pd.name(), pd);
    }

    for (auto it = src->properties().constBegin(); it != src->properties().constEnd(); ++it) {
        ValuePtr &v = dst->m_properties[it.key()];
        if (!v) {
            v = it.value();
            continue;
        }
        if (v->type() == Value::ItemValueType && it.value()->type() != Value::ItemValueType)
            throw ErrorInfo(Tr::tr("Binding to non-item property."), v->location());
        if (v->type() != it.value()->type())
            continue;
        switch (v->type()) {
        case Value::JSSourceValueType: {
            JSSourceValuePtr sv = std::static_pointer_cast<JSSourceValue>(v);
            QBS_CHECK(!sv->baseValue());
            const JSSourceValuePtr baseValue = std::static_pointer_cast<JSSourceValue>(it.value());
            sv->setBaseValue(baseValue);
            for (const JSSourceValue::Alternative &alt : sv->m_alternatives)
                alt.value->setBaseValue(baseValue);
            break;
        }
        case Value::ItemValueType:
            inheritItem(std::static_pointer_cast<ItemValue>(v)->item(),
                        std::static_pointer_cast<const ItemValue>(it.value())->item());
            break;
        default:
            QBS_CHECK(!"unexpected value type");
        }
    }
}

void ItemReaderASTVisitor::checkDeprecationStatus(ItemType itemType, const QString &itemName,
                                                  const CodeLocation &itemLocation)
{
    const ItemDeclaration itemDecl = BuiltinDeclarations::instance().declarationsForType(itemType);
    const DeprecationInfo &di = itemDecl.deprecationInfo();
    if (!di.isValid())
        return;
    if (di.removalVersion() <= LanguageInfo::qbsVersion()) {
        QString message = Tr::tr("The item '%1' cannot be used anymore. "
                "It was removed in qbs %2.")
                .arg(itemName, di.removalVersion().toString());
        ErrorInfo error(message, itemLocation);
        if (!di.additionalUserInfo().isEmpty())
            error.append(di.additionalUserInfo());
        throw error;
    }
    QString warning = Tr::tr("The item '%1' is deprecated and will be removed in "
                             "qbs %2.").arg(itemName, di.removalVersion().toString());
    ErrorInfo error(warning, itemLocation);
    if (!di.additionalUserInfo().isEmpty())
        error.append(di.additionalUserInfo());
    m_logger.printWarning(error);
}

void ItemReaderASTVisitor::doCheckItemTypes(const Item *item)
{
    const ItemDeclaration decl = BuiltinDeclarations::instance().declarationsForType(item->type());
    for (const Item * const child : item->children()) {
        if (!decl.isChildTypeAllowed(child->type())) {
            throw ErrorInfo(Tr::tr("Items of type '%1' cannot contain items of type '%2'.")
                            .arg(item->typeName(), child->typeName()), child->location());
        }
        doCheckItemTypes(child);
    }
}

} // namespace Internal
} // namespace qbs