diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2019-01-22 14:20:18 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2019-07-09 16:52:17 +0200 |
commit | 1b866db65eb45ff347b51a2bc3b08d2a0bc166d6 (patch) | |
tree | a3c5c8fdd71a1ff06fb5872974e366398e357f19 /src/qml | |
parent | a06736cab722c2a4283cdcbda8d003d8e91d7f54 (diff) |
Fix internal boundNames() API to expose optional types
This allows extracting the type information for variable declarations.
Change-Id: I1241df3b27ae292b83392d5caaa1587caafa46a3
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/compiler/qv4compilerscanfunctions.cpp | 26 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast.cpp | 14 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast_p.h | 32 |
3 files changed, 47 insertions, 25 deletions
diff --git a/src/qml/compiler/qv4compilerscanfunctions.cpp b/src/qml/compiler/qv4compilerscanfunctions.cpp index 11da6605c3..da22a2d826 100644 --- a/src/qml/compiler/qv4compilerscanfunctions.cpp +++ b/src/qml/compiler/qv4compilerscanfunctions.cpp @@ -202,16 +202,16 @@ bool ScanFunctions::visit(ExportDeclaration *declaration) _context->exportEntries << entry; } } else if (auto *vstmt = AST::cast<AST::VariableStatement*>(declaration->variableStatementOrDeclaration)) { - QStringList boundNames; + BoundNames boundNames; for (VariableDeclarationList *it = vstmt->declarations; it; it = it->next) { if (!it->declaration) continue; it->declaration->boundNames(&boundNames); } - for (const QString &name: boundNames) { + for (const auto &name: boundNames) { Compiler::ExportEntry entry; - entry.localName = name; - entry.exportName = name; + entry.localName = name.id; + entry.exportName = name.id; entry.location = location(vstmt->firstSourceLocation()); _context->exportEntries << entry; } @@ -326,26 +326,26 @@ bool ScanFunctions::visit(PatternElement *ast) if (!ast->isVariableDeclaration()) return true; - QStringList names; + BoundNames names; ast->boundNames(&names); QQmlJS::AST::SourceLocation lastInitializerLocation = ast->lastSourceLocation(); if (_context->lastBlockInitializerLocation.isValid()) lastInitializerLocation = _context->lastBlockInitializerLocation; - for (const QString &name : qAsConst(names)) { - if (_context->isStrict && (name == QLatin1String("eval") || name == QLatin1String("arguments"))) + for (const auto &name : qAsConst(names)) { + if (_context->isStrict && (name.id == QLatin1String("eval") || name.id == QLatin1String("arguments"))) _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Variable name may not be eval or arguments in strict mode")); - checkName(QStringRef(&name), ast->identifierToken); - if (name == QLatin1String("arguments")) + checkName(QStringRef(&name.id), ast->identifierToken); + if (name.id == QLatin1String("arguments")) _context->usesArgumentsObject = Context::ArgumentsObjectNotUsed; if (ast->scope == VariableScope::Const && !ast->initializer && !ast->isForDeclaration && !ast->destructuringPattern()) { _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Missing initializer in const declaration")); return false; } - if (!_context->addLocalVar(name, ast->initializer ? Context::VariableDefinition : Context::VariableDeclaration, ast->scope, + if (!_context->addLocalVar(name.id, ast->initializer ? Context::VariableDefinition : Context::VariableDeclaration, ast->scope, /*function*/nullptr, lastInitializerLocation)) { - _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Identifier %1 has already been declared").arg(name)); + _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Identifier %1 has already been declared").arg(name.id)); return false; } } @@ -698,9 +698,9 @@ bool ScanFunctions::enterFunction(Node *ast, const QString &name, FormalParamete _context->arguments = formals ? formals->formals() : QStringList(); - const QStringList boundNames = formals ? formals->boundNames() : QStringList(); + const BoundNames boundNames = formals ? formals->boundNames() : BoundNames(); for (int i = 0; i < boundNames.size(); ++i) { - const QString &arg = boundNames.at(i); + const QString &arg = boundNames.at(i).id; if (_context->isStrict || !isSimpleParameterList) { bool duplicate = (boundNames.indexOf(arg, i + 1) != -1); if (duplicate) { diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp index de5db73d3f..f68381ce44 100644 --- a/src/qml/parser/qqmljsast.cpp +++ b/src/qml/parser/qqmljsast.cpp @@ -1003,9 +1003,9 @@ QStringList FormalParameterList::formals() const return formals; } -QStringList FormalParameterList::boundNames() const +BoundNames FormalParameterList::boundNames() const { - QStringList names; + BoundNames names; for (const FormalParameterList *it = this; it; it = it->next) { if (it->element) it->element->boundNames(&names); @@ -1377,7 +1377,7 @@ void PatternElement::accept0(Visitor *visitor) visitor->endVisit(this); } -void PatternElement::boundNames(QStringList *names) +void PatternElement::boundNames(BoundNames *names) { if (bindingTarget) { if (PatternElementList *e = elementList()) @@ -1385,7 +1385,7 @@ void PatternElement::boundNames(QStringList *names) else if (PatternPropertyList *p = propertyList()) p->boundNames(names); } else { - names->append(bindingIdentifier.toString()); + names->append({bindingIdentifier.toString(), typeAnnotation}); } } @@ -1401,7 +1401,7 @@ void PatternElementList::accept0(Visitor *visitor) visitor->endVisit(this); } -void PatternElementList::boundNames(QStringList *names) +void PatternElementList::boundNames(BoundNames *names) { for (PatternElementList *it = this; it; it = it->next) { if (it->element) @@ -1421,7 +1421,7 @@ void PatternProperty::accept0(Visitor *visitor) visitor->endVisit(this); } -void PatternProperty::boundNames(QStringList *names) +void PatternProperty::boundNames(BoundNames *names) { PatternElement::boundNames(names); } @@ -1437,7 +1437,7 @@ void PatternPropertyList::accept0(Visitor *visitor) visitor->endVisit(this); } -void PatternPropertyList::boundNames(QStringList *names) +void PatternPropertyList::boundNames(BoundNames *names) { for (PatternPropertyList *it = this; it; it = it->next) it->property->boundNames(names); diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h index 21d143edf1..cdee4843b0 100644 --- a/src/qml/parser/qqmljsast_p.h +++ b/src/qml/parser/qqmljsast_p.h @@ -830,6 +830,28 @@ public: SourceLocation propertyNameToken; }; +struct QML_PARSER_EXPORT BoundName +{ + QString id; + TypeAnnotation *typeAnnotation = nullptr; + BoundName(const QString &id, TypeAnnotation *typeAnnotation) + : id(id), typeAnnotation(typeAnnotation) + {} + BoundName() = default; +}; + +struct BoundNames : public QVector<BoundName> +{ + int indexOf(const QString &name, int from = 0) const + { + auto found = std::find_if(constBegin() + from, constEnd(), + [name](const BoundName &it) { return it.id == name; }); + if (found == constEnd()) + return -1; + return found - constBegin(); + } +}; + class QML_PARSER_EXPORT PatternElement : public Node { public: @@ -886,7 +908,7 @@ public: bool isVariableDeclaration() const { return scope != VariableScope::NoScope; } bool isLexicallyScoped() const { return scope == VariableScope::Let || scope == VariableScope::Const; } - virtual void boundNames(QStringList *names); + virtual void boundNames(BoundNames *names); // attributes SourceLocation identifierToken; @@ -924,7 +946,7 @@ public: void accept0(Visitor *visitor) override; - void boundNames(QStringList *names); + void boundNames(BoundNames *names); SourceLocation firstSourceLocation() const override { return elision ? elision->firstSourceLocation() : element->firstSourceLocation(); } @@ -964,7 +986,7 @@ public: return loc.isValid() ? loc : name->lastSourceLocation(); } - void boundNames(QStringList *names) override; + void boundNames(BoundNames *names) override; bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage) override; // attributes @@ -992,7 +1014,7 @@ public: void accept0(Visitor *visitor) override; - void boundNames(QStringList *names); + void boundNames(BoundNames *names); inline PatternPropertyList *finish () { @@ -2390,7 +2412,7 @@ public: QStringList formals() const; - QStringList boundNames() const; + BoundNames boundNames() const; void accept0(Visitor *visitor) override; |