aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/language/itemreaderastvisitor.cpp
blob: 4cddd0dc07c2c2ecf50d3e4e9bb861b73604feb1 (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "itemreaderastvisitor.h"
#include "asttools.h"
#include "builtindeclarations.h"
#include "identifiersearch.h"
#include "itemreader.h"
#include <jsextensions/jsextensions.h>
#include <parser/qmljsast_p.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/qbsassert.h>
#include <tools/qttools.h>
#include <logging/translator.h>

#include <QDirIterator>
#include <QFileInfo>
#include <QStringList>

using namespace QbsQmlJS;

namespace qbs {
namespace Internal {

ItemReaderASTVisitor::ItemReaderASTVisitor(ItemReader *reader, ItemReaderResult *result)
    : m_reader(reader)
    , m_readerResult(result)
    , m_languageVersion(readImportVersion(reader->builtins()->languageVersion()))
    , m_file(FileContext::create())
    , m_item(0)
    , m_sourceValue(0)
{
}

ItemReaderASTVisitor::~ItemReaderASTVisitor()
{
}

void ItemReaderASTVisitor::setFilePath(const QString &filePath)
{
    m_file->setFilePath(filePath);
}

void ItemReaderASTVisitor::setSourceCode(const QString &sourceCode)
{
    m_file->setContent(sourceCode);
}

bool ItemReaderASTVisitor::visit(AST::UiProgram *ast)
{
    Q_UNUSED(ast);
    m_sourceValue.clear();
    m_file->m_searchPaths = m_reader->searchPaths();

    if (Q_UNLIKELY(!ast->members->member))
        throw ErrorInfo(Tr::tr("No root item found in %1.").arg(m_file->filePath()));

    return true;
}

bool ItemReaderASTVisitor::addPrototype(const QString &fileName, const QString &filePath,
                                        const QString &as, bool needsCheck)
{
    if (needsCheck && fileName.size() <= 4)
        return false;

    const QString componentName = fileName.left(fileName.size() - 4);
    // ### validate componentName

    if (needsCheck && !componentName.at(0).isUpper())
        return false;

    QStringList prototypeName;
    if (!as.isEmpty())
        prototypeName.append(as);
    prototypeName.append(componentName);
    m_typeNameToFile.insert(prototypeName, filePath);
    return true;
}

void ItemReaderASTVisitor::collectPrototypes(const QString &path, const QString &as)
{
    QStringList fileNames; // Yes, file *names*.
    if (m_reader->findDirectoryEntries(path, &fileNames)) {
        foreach (const QString &fileName, fileNames)
            addPrototype(fileName, path + QLatin1Char('/') + fileName, as, false);
        return;
    }

    QDirIterator dirIter(path, QStringList(QLatin1String("*.qbs")));
    while (dirIter.hasNext()) {
        const QString filePath = dirIter.next();
        const QString fileName = dirIter.fileName();
        if (addPrototype(fileName, filePath, as, true))
            fileNames << fileName;
    }
    m_reader->cacheDirectoryEntries(path, fileNames);
}

bool ItemReaderASTVisitor::visit(AST::UiImportList *uiImportList)
{
    foreach (const QString &searchPath, m_reader->searchPaths())
        collectPrototypes(searchPath + QLatin1String("/imports"), QString());

    const QString path = FileInfo::path(m_file->filePath());

    // files in the same directory are available as prototypes
    collectPrototypes(path, QString());

    QSet<QString> importAsNames;
    QHash<QString, JsImport> jsImports;

    for (const AST::UiImportList *it = uiImportList; it; it = it->next) {
        const AST::UiImport *const import = it->import;

        QStringList importUri;
        bool isBase = false;
        if (import->importUri) {
            importUri = toStringList(import->importUri);
            isBase = (importUri.size() == 1 && importUri.first() == QLatin1String("qbs"))
                    || (importUri.size() == 2 && importUri.first() == QLatin1String("qbs")
                        && importUri.last() == QLatin1String("base"));
            if (isBase)
                checkImportVersion(import->versionToken);
            else if (import->versionToken.length)
                m_reader->logger().printWarning(ErrorInfo(Tr::tr("Superfluous version specification."),
                                                    toCodeLocation(import->versionToken)));
        }

        QString as;
        if (isBase) {
            if (Q_UNLIKELY(!import->importId.isNull())) {
                throw ErrorInfo(Tr::tr("Import of qbs.base must have no 'as <Name>'"),
                            toCodeLocation(import->importIdToken));
            }
        } else {
            if (importUri.count() == 2 && importUri.first() == QLatin1String("qbs")) {
                const QString extensionName = importUri.last();
                if (JsExtensions::hasExtension(extensionName)) {
                    if (Q_UNLIKELY(!import->importId.isNull())) {
                        throw ErrorInfo(Tr::tr("Import of built-in extension '%1' "
                                               "must not have 'as' specifier.").arg(extensionName));
                    }
                    if (Q_UNLIKELY(m_file->m_jsExtensions.contains(extensionName))) {
                        m_reader->logger().printWarning(Tr::tr("Built-in extension '%1' already "
                                                               "imported.").arg(extensionName));
                    } else {
                        m_file->m_jsExtensions << extensionName;
                    }
                    continue;
                }
            }

            if (import->importId.isNull()) {
                if (!import->fileName.isNull()) {
                    throw ErrorInfo(Tr::tr("File imports require 'as <Name>'"),
                                    toCodeLocation(import->importToken));
                }
                if (importUri.isEmpty()) {
                    throw ErrorInfo(Tr::tr("Invalid import URI."),
                                    toCodeLocation(import->importToken));
                }
                as = importUri.last();
            } else {
                as = import->importId.toString();
            }

            if (Q_UNLIKELY(importAsNames.contains(as))) {
                throw ErrorInfo(Tr::tr("Cannot import into the same name more than once."),
                            toCodeLocation(import->importIdToken));
            }
            if (Q_UNLIKELY(JsExtensions::hasExtension(as))) {
                throw ErrorInfo(Tr::tr("Cannot reuse the name of built-in extension '%1'.")
                                .arg(as));
            }
            importAsNames.insert(as);
        }

        if (!import->fileName.isNull()) {
            QString filePath = FileInfo::resolvePath(path, import->fileName.toString());

            QFileInfo fi(filePath);
            if (Q_UNLIKELY(!fi.exists()))
                throw ErrorInfo(Tr::tr("Cannot find imported file %0.").arg(filePath),
                            CodeLocation(m_file->filePath(), import->fileNameToken.startLine,
                                         import->fileNameToken.startColumn));
            filePath = fi.canonicalFilePath();
            if (fi.isDir()) {
                collectPrototypes(filePath, as);
            } else {
                if (filePath.endsWith(QLatin1String(".js"), Qt::CaseInsensitive)) {
                    JsImport &jsImport = jsImports[as];
                    jsImport.scopeName = as;
                    jsImport.filePaths.append(filePath);
                    jsImport.location = toCodeLocation(import->firstSourceLocation());
                } else if (filePath.endsWith(QLatin1String(".qbs"), Qt::CaseInsensitive)) {
                    m_typeNameToFile.insert(QStringList(as), filePath);
                } else {
                    throw ErrorInfo(Tr::tr("Can only import .qbs and .js files"),
                                CodeLocation(m_file->filePath(), import->fileNameToken.startLine,
                                             import->fileNameToken.startColumn));
                }
            }
        } else if (!importUri.isEmpty()) {
            const QString importPath = isBase
                    ? QLatin1String("qbs/base") : importUri.join(QDir::separator());
            bool found = m_typeNameToFile.contains(importUri);
            if (!found) {
                foreach (const QString &searchPath, m_reader->searchPaths()) {
                    const QFileInfo fi(FileInfo::resolvePath(
                                           FileInfo::resolvePath(searchPath,
                                                                 QLatin1String("imports")),
                                           importPath));
                    if (fi.isDir()) {
                        // ### versioning, qbsdir file, etc.
                        const QString &resultPath = fi.absoluteFilePath();
                        collectPrototypes(resultPath, as);

                        QDirIterator dirIter(resultPath, QStringList(QLatin1String("*.js")));
                        while (dirIter.hasNext()) {
                            dirIter.next();
                            JsImport &jsImport = jsImports[as];
                            if (jsImport.scopeName.isNull()) {
                                jsImport.scopeName = as;
                                jsImport.location = toCodeLocation(import->firstSourceLocation());
                            }
                            jsImport.filePaths.append(dirIter.filePath());
                        }
                        found = true;
                        break;
                    }
                }
            }
            if (Q_UNLIKELY(!found)) {
                throw ErrorInfo(Tr::tr("import %1 not found")
                                .arg(importUri.join(QLatin1String("."))),
                                toCodeLocation(import->fileNameToken));
            }
        }
    }

    for (QHash<QString, JsImport>::const_iterator it = jsImports.constBegin();
         it != jsImports.constEnd(); ++it)
    {
        m_file->m_jsImports += it.value();
    }

    return false;
}

bool ItemReaderASTVisitor::visit(AST::UiObjectDefinition *ast)
{
    const QString typeName = ast->qualifiedTypeNameId->name.toString();

    Item *item = Item::create(m_reader->m_pool);
    item->m_file = m_file;
    item->m_parent = m_item;
    item->m_typeName = typeName;
    item->m_location = ::qbs::Internal::toCodeLocation(m_file->filePath(),
                                                       ast->qualifiedTypeNameId->identifierToken);

    if (m_item) {
        // Add this item to the children of the parent item.
        m_item->m_children += item;
    } else {
        // This is the root item.
        m_item = item;
        m_readerResult->rootItem = item;
    }

    if (ast->initializer) {
        qSwap(m_item, item);
        ast->initializer->accept(this);
        qSwap(m_item, item);
    }

    m_reader->m_builtins->setupItemForBuiltinType(item);

    if (item->typeName() != QLatin1String("Properties")
            && item->typeName() != QLatin1String("SubProject")) {
        setupAlternatives(item);
    }

    // resolve inheritance
    const QStringList fullTypeName = toStringList(ast->qualifiedTypeNameId);
    const QString baseTypeFileName = m_typeNameToFile.value(fullTypeName);
    if (!baseTypeFileName.isEmpty()) {
        const ItemReaderResult baseFile = m_reader->internalReadFile(baseTypeFileName);

        inheritItem(item, baseFile.rootItem);
        if (baseFile.rootItem->m_file->m_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.
            ensureIdScope(item->m_file);
            baseFile.rootItem->m_file->m_idScope->setPrototype(item->m_file->m_idScope);
        }
    }

    return false;
}

void ItemReaderASTVisitor::checkDuplicateBinding(Item *item, const QStringList &bindingName,
                                  const AST::SourceLocation &sourceLocation)
{
    if (Q_UNLIKELY(item->properties().contains(bindingName.last()))) {
        QString msg = Tr::tr("Duplicate binding for '%1'");
        throw ErrorInfo(msg.arg(bindingName.join(QLatin1String("."))),
                    qbs::Internal::toCodeLocation(m_file->filePath(), 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 (ast->typeModifier.compare(QLatin1String("list")))
        p.setFlags(p.flags() | PropertyDeclaration::ListProperty);
    else if (Q_UNLIKELY(!ast->typeModifier.isEmpty()))
        throw ErrorInfo(Tr::tr("public member with type modifier '%1' not supported").arg(
                        ast->typeModifier.toString()));

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

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

    m_item->m_properties.insert(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.first() == QLatin1String("id")) {
        AST::ExpressionStatement *expStmt =
                AST::cast<AST::ExpressionStatement *>(ast->statement);
        if (Q_UNLIKELY(!expStmt))
            throw ErrorInfo(Tr::tr("id: must be followed by identifier"));
        AST::IdentifierExpression *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();
        ensureIdScope(m_file);
        m_file->m_idScope->m_properties[m_item->m_id] = ItemValue::create(m_item);
        return false;
    }

    JSSourceValuePtr value = JSSourceValue::create();
    value->setFile(m_file);
    m_sourceValue.swap(value);
    visitStatement(ast->statement);
    m_sourceValue.swap(value);

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

bool ItemReaderASTVisitor::visit(AST::FunctionDeclaration *ast)
{
    FunctionDeclaration f;
    if (Q_UNLIKELY(ast->name.isNull()))
        throw ErrorInfo(Tr::tr("function decl without name"));
    f.setName(ast->name.toString());

    // remove the name
    QString funcNoName = textOf(m_file->content(), ast);
    funcNoName.replace(QRegExp(QLatin1String("^(\\s*function\\s*)\\w*")), QLatin1String("(\\1"));
    funcNoName.append(QLatin1Char(')'));
    f.setSourceCode(funcNoName);

    f.setLocation(toCodeLocation(ast->firstSourceLocation()));
    m_item->m_functions += f;
    return false;
}

Version ItemReaderASTVisitor::readImportVersion(const QString &str, const CodeLocation &location)
{
    QStringList lst = str.split(QLatin1Char('.'));
    if (Q_UNLIKELY(lst.count() < 1 || lst.count() > 2))
        throw ErrorInfo(Tr::tr("Wrong number of components in import version."), location);
    Version v;
    int *parts[] = {&v.majorVersionRef(), &v.minorVersionRef(), 0};
    for (int i = 0; i < lst.count(); ++i) {
        bool ok;
        *parts[i] = lst.at(i).toInt(&ok);
        if (Q_UNLIKELY(!ok))
            throw ErrorInfo(Tr::tr("Cannot parse import version."), location);
    }
    return v;
}

bool ItemReaderASTVisitor::visitStatement(AST::Statement *statement)
{
    QBS_CHECK(statement);
    QBS_CHECK(m_sourceValue);

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

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

    bool usesBase, usesOuter;
    IdentifierSearch idsearch;
    idsearch.add(QLatin1String("base"), &usesBase);
    idsearch.add(QLatin1String("outer"), &usesOuter);
    idsearch.start(statement);
    if (usesBase)
        m_sourceValue->m_flags |= JSSourceValue::SourceUsesBase;
    if (usesOuter)
        m_sourceValue->m_flags |= JSSourceValue::SourceUsesOuter;
    return false;
}

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

Item *ItemReaderASTVisitor::targetItemForBinding(Item *item,
                                                   const QStringList &bindingName,
                                                   const JSSourceValueConstPtr &value)
{
    Item *targetItem = item;
    const int c = bindingName.count() - 1;
    for (int i = 0; i < c; ++i) {
        ValuePtr v = targetItem->m_properties.value(bindingName.at(i));
        if (!v) {
            Item *newItem = Item::create(m_reader->m_pool);
            v = ItemValue::create(newItem);
            targetItem->m_properties.insert(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());
        }
        ItemValuePtr jsv = v.staticCast<ItemValue>();
        targetItem = jsv->item();
    }
    return targetItem;
}

void ItemReaderASTVisitor::checkImportVersion(const AST::SourceLocation &versionToken) const
{
    if (!versionToken.length)
        return;
    const QString importVersionString = m_file->content().mid(versionToken.offset, versionToken.length);
    const Version importVersion = readImportVersion(importVersionString,
                                                    toCodeLocation(versionToken));
    if (Q_UNLIKELY(importVersion != m_languageVersion))
        throw ErrorInfo(Tr::tr("Incompatible qbs version %1. This is qbs %2.").arg(
                        importVersionString, m_reader->builtins()->languageVersion()),
                    toCodeLocation(versionToken));
}

void ItemReaderASTVisitor::inheritItem(Item *dst, const Item *src)
{
    if (!src->typeName().isEmpty())
        dst->setTypeName(src->typeName());

    int insertPos = 0;
    for (int i = 0; i < src->m_children.count(); ++i) {
        Item *child = src->m_children.at(i);
        dst->m_children.insert(insertPos++, child);
        child->m_parent = dst;
    }

    for (QMap<QString, ValuePtr>::const_iterator it = src->m_properties.constBegin();
         it != src->m_properties.constEnd(); ++it)
    {
        ValuePtr &v = dst->m_properties[it.key()];
        if (v) {
            if (v->type() == it.value()->type()) {
                if (v->type() == Value::JSSourceValueType) {
                    JSSourceValuePtr sv = v.staticCast<JSSourceValue>();
                    while (sv->baseValue())
                        sv = sv->baseValue();
                    const JSSourceValuePtr baseValue = it.value().staticCast<JSSourceValue>();
                    sv->setBaseValue(baseValue);
                    for (QList<JSSourceValue::Alternative>::iterator it
                            = sv->m_alternatives.begin(); it != sv->m_alternatives.end(); ++it) {
                        JSSourceValue::Alternative &alternative = *it;
                        alternative.value->setBaseValue(baseValue);
                    }
                } else if (v->type() == Value::ItemValueType) {
                    QBS_CHECK(v.staticCast<ItemValue>()->item());
                    QBS_CHECK(it.value().staticCast<const ItemValue>()->item());
                    inheritItem(v.staticCast<ItemValue>()->item(),
                                it.value().staticCast<const ItemValue>()->item());
                } else {
                    QBS_CHECK(!"unexpected value type");
                }
            }
        } else {
            v = it.value();
        }
    }

    for (Item::PropertyDeclarationMap::const_iterator it
            = src->m_propertyDeclarations.constBegin();
            it != src->m_propertyDeclarations.constEnd(); ++it) {
        dst->m_propertyDeclarations[it.key()] = it.value();
    }
}

void ItemReaderASTVisitor::ensureIdScope(const FileContextPtr &file)
{
    if (!file->m_idScope) {
        file->m_idScope = Item::create(m_reader->m_pool);
        file->m_idScope->m_typeName = QLatin1String("IdScope");
    }
}

void ItemReaderASTVisitor::setupAlternatives(Item *item)
{
    QList<Item *>::iterator it = item->m_children.begin();
    while (it != item->m_children.end()) {
        Item *child = *it;
        if (child->typeName() == QLatin1String("Properties")) {
            handlePropertiesBlock(item, child);
            it = item->m_children.erase(it);
        } else {
            ++it;
        }
    }
}

class PropertiesBlockConverter
{
public:
    PropertiesBlockConverter(const QString &condition, Item *propertiesBlockContainer,
                             const Item *propertiesBlock)
        : m_propertiesBlockContainer(propertiesBlockContainer)
        , m_propertiesBlock(propertiesBlock)
    {
        m_alternative.condition = condition;
    }

    void operator()()
    {
        apply(m_propertiesBlockContainer, m_propertiesBlock);
    }

private:
    JSSourceValue::Alternative m_alternative;
    Item *m_propertiesBlockContainer;
    const Item *m_propertiesBlock;

    void apply(Item *a, const Item *b)
    {
        for (QMap<QString, ValuePtr>::const_iterator it = b->properties().constBegin();
                it != b->properties().constEnd(); ++it) {
            if (b == m_propertiesBlock && it.key() == QLatin1String("condition"))
                continue;
            if (it.value()->type() == Value::ItemValueType) {
                apply(a->itemProperty(it.key(), true)->item(),
                      it.value().staticCast<ItemValue>()->item());
            } else if (it.value()->type() == Value::JSSourceValueType) {
                ValuePtr aval = a->property(it.key());
                if (Q_UNLIKELY(aval && aval->type() != Value::JSSourceValueType))
                    throw ErrorInfo(Tr::tr("Incompatible value type in unconditional value at %1.").arg(
                                    aval->location().toString()));
                apply(it.key(), a, aval.staticCast<JSSourceValue>(),
                      it.value().staticCast<JSSourceValue>());
            } else {
                QBS_CHECK(!"Unexpected value type in conditional value.");
            }
        }
    }

    void apply(const QString &propertyName, Item *item, JSSourceValuePtr value,
               const JSSourceValuePtr &conditionalValue)
    {
        QBS_ASSERT(!value || value->file() == conditionalValue->file(), return);
        if (!value) {
            value = JSSourceValue::create();
            value->setFile(conditionalValue->file());
            item->setProperty(propertyName, value);
            static const QString undefinedKeyword = QLatin1String("undefined");
            value->setSourceCode(QStringRef(&undefinedKeyword));
        }
        m_alternative.value = conditionalValue;
        value->addAlternative(m_alternative);
    }
};

void ItemReaderASTVisitor::handlePropertiesBlock(Item *item, const Item *block)
{
    ValuePtr value = block->property(QLatin1String("condition"));
    if (Q_UNLIKELY(!value))
        throw ErrorInfo(Tr::tr("Properties.condition must be provided."),
                    block->location());
    if (Q_UNLIKELY(value->type() != Value::JSSourceValueType))
        throw ErrorInfo(Tr::tr("Properties.condition must be a value binding."),
                    block->location());
    JSSourceValuePtr srcval = value.staticCast<JSSourceValue>();
    const QString condition = srcval->sourceCodeForEvaluation();
    PropertiesBlockConverter convertBlock(condition, item, block);
    convertBlock();
}

} // namespace Internal
} // namespace qbs