Parser: Simplify argument "lists" for type annotations

There can in fact only be one type argument, and we don't need a
finish() method. In fact the finish() method didn't return the type
argument at all.

Task-number: QTBUG-107171
Change-Id: Ifb7d85ca42a38d37da71b6453b458c7ec10cd64d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2022-10-17 09:18:06 +02:00
parent 0f08e3e673
commit e89a06753c
9 changed files with 65 additions and 89 deletions

View File

@ -212,7 +212,7 @@ public:
AST::ExportClause *ExportClause;
AST::ExportDeclaration *ExportDeclaration;
AST::TypeAnnotation *TypeAnnotation;
AST::TypeArgumentList *TypeArgumentList;
AST::TypeArgument *TypeArgument;
AST::Type *Type;
AST::UiProgram *UiProgram;
@ -1575,28 +1575,16 @@ BindingIdentifier: IdentifierReference;
-- Types
--------------------------------------------------------------------------------------------------------
TypeArguments: Type;
Type: UiQualifiedId T_LT SimpleType T_GT;
/.
case $rule_number: {
sym(1).TypeArgumentList = new (pool) AST::TypeArgumentList(sym(1).Type);
sym(1).Type = new (pool) AST::Type(sym(1).UiQualifiedId, sym(3).Type);
} break;
./
TypeArguments: TypeArguments T_COMMA Type;
/.
case $rule_number: {
sym(1).TypeArgumentList = new (pool) AST::TypeArgumentList(sym(1).TypeArgumentList, sym(3).Type);
} break;
./
Type: SimpleType;
Type: UiQualifiedId T_LT TypeArguments T_GT;
/.
case $rule_number: {
sym(1).Type = new (pool) AST::Type(sym(1).UiQualifiedId, sym(3).TypeArgumentList->finish());
} break;
./
Type: T_RESERVED_WORD;
SimpleType: T_RESERVED_WORD;
/.
case $rule_number: {
AST::UiQualifiedId *id = new (pool) AST::UiQualifiedId(stringRef(1));
@ -1605,17 +1593,17 @@ Type: T_RESERVED_WORD;
} break;
./
Type: UiQualifiedId;
SimpleType: UiQualifiedId;
/.
case $rule_number: {
sym(1).Type = new (pool) AST::Type(sym(1).UiQualifiedId);
} break;
./
Type: T_VAR;
SimpleType: T_VAR;
/. case $rule_number: Q_FALLTHROUGH(); ./
Type: T_VOID;
SimpleType: T_VOID;
/.
case $rule_number: {
AST::UiQualifiedId *id = new (pool) AST::UiQualifiedId(stringRef(1));

View File

@ -1311,17 +1311,7 @@ void Type::accept0(BaseVisitor *visitor)
{
if (visitor->visit(this)) {
accept(typeId, visitor);
accept(typeArguments, visitor);
}
visitor->endVisit(this);
}
void TypeArgumentList::accept0(BaseVisitor *visitor)
{
if (visitor->visit(this)) {
for (TypeArgumentList *it = this; it; it = it->next)
accept(it->typeId, visitor);
accept(typeArgument, visitor);
}
visitor->endVisit(this);
@ -1563,17 +1553,11 @@ QString Type::toString() const
void Type::toString(QString *out) const
{
for (QQmlJS::AST::UiQualifiedId *it = typeId; it; it = it->next) {
out->append(it->name);
typeId->toString(out);
if (it->next)
out->append(QLatin1Char('.'));
}
if (typeArguments) {
if (typeArgument) {
out->append(QLatin1Char('<'));
if (auto subType = static_cast<TypeArgumentList*>(typeArguments)->typeId)
subType->toString(out);
typeArgument->toString(out);
out->append(QLatin1Char('>'));
};
}

View File

@ -211,7 +211,7 @@ public:
Kind_PatternProperty,
Kind_PatternPropertyList,
Kind_Type,
Kind_TypeArgumentList,
Kind_TypeArgument,
Kind_TypeAnnotation,
Kind_UiArrayBinding,
@ -329,6 +329,22 @@ public:
SourceLocation lastSourceLocation() const override
{ return lastListElement(this)->identifierToken; }
QString toString() const
{
QString result;
toString(&result);
return result;
}
void toString(QString *out) const
{
for (const UiQualifiedId *it = this; it; it = it->next) {
out->append(it->name);
if (it->next)
out->append(QLatin1Char('.'));
}
}
// attributes
UiQualifiedId *next;
QStringView name;
@ -340,9 +356,9 @@ class QML_PARSER_EXPORT Type: public Node
public:
QQMLJS_DECLARE_AST_NODE(Type)
Type(UiQualifiedId *typeId, Node *typeArguments = nullptr)
Type(UiQualifiedId *typeId, Type *typeArgument = nullptr)
: typeId(typeId)
, typeArguments(typeArguments)
, typeArgument(typeArgument ? typeArgument->typeId : nullptr)
{ kind = K; }
void accept0(BaseVisitor *visitor) override;
@ -351,53 +367,14 @@ public:
{ return typeId->firstSourceLocation(); }
SourceLocation lastSourceLocation() const override
{ return typeArguments ? typeArguments->lastSourceLocation() : typeId->lastSourceLocation(); }
{ return typeArgument ? typeArgument->lastSourceLocation() : typeId->lastSourceLocation(); }
QString toString() const;
void toString(QString *out) const;
// attributes
UiQualifiedId *typeId;
Node *typeArguments; // TypeArgumentList
};
class QML_PARSER_EXPORT TypeArgumentList: public Node
{
public:
QQMLJS_DECLARE_AST_NODE(TypeArgumentList)
TypeArgumentList(Type *typeId)
: typeId(typeId)
, next(this)
{ kind = K; }
TypeArgumentList(TypeArgumentList *previous, Type *typeId)
: typeId(typeId)
{
kind = K;
next = previous->next;
previous->next = this;
}
void accept0(BaseVisitor *visitor) override;
SourceLocation firstSourceLocation() const override
{ return typeId->firstSourceLocation(); }
SourceLocation lastSourceLocation() const override
{ return lastListElement(this)->typeId->lastSourceLocation(); }
inline TypeArgumentList *finish()
{
TypeArgumentList *front = next;
next = nullptr;
return front;
}
// attributes
Type *typeId;
TypeArgumentList *next;
UiQualifiedId *typeArgument;
};
class QML_PARSER_EXPORT TypeAnnotation: public Node

View File

@ -122,7 +122,7 @@ class NestedExpression;
class ClassExpression;
class ClassDeclaration;
class ClassElementList;
class TypeArgumentList;
class TypeArgument;
class Type;
class TypeAnnotation;

View File

@ -378,8 +378,8 @@ public:
virtual bool visit(Type *) = 0;
virtual void endVisit(Type *) = 0;
virtual bool visit(TypeArgumentList *) = 0;
virtual void endVisit(TypeArgumentList *) = 0;
virtual bool visit(TypeArgument *) = 0;
virtual void endVisit(TypeArgument *) = 0;
virtual bool visit(TypeAnnotation *) = 0;
virtual void endVisit(TypeAnnotation *) = 0;
@ -722,8 +722,8 @@ public:
bool visit(Type *) override { return true; }
void endVisit(Type *) override {}
bool visit(TypeArgumentList *) override { return true; }
void endVisit(TypeArgumentList *) override {}
bool visit(TypeArgument *) override { return true; }
void endVisit(TypeArgument *) override {}
bool visit(TypeAnnotation *) override { return true; }
void endVisit(TypeAnnotation *) override {}

View File

@ -71,6 +71,11 @@ static QString typeToString(AST::Type *t)
{
Q_ASSERT(t);
QString res = toString(t->typeId);
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (UiQualifiedId *arg = t->typeArgument)
res += u'<' + toString(arg) + u'>';
#else
if (!t->typeArguments)
return res;
res += u"<";
@ -84,6 +89,8 @@ static QString typeToString(AST::Type *t)
res += typeToString(tt->typeId);
}
res += u">";
#endif
return res;
}

View File

@ -990,11 +990,19 @@ public:
}
void endVisit(AST::Type *) override { stop(u"Type"); }
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
bool visit(AST::TypeArgument *) override {
start(u"TypeArgument");
return true;
}
void endVisit(AST::TypeArgument *) override { stop(u"TypeArgument"); }
#else
bool visit(AST::TypeArgumentList *) override {
start(u"TypeArgumentList");
return true;
}
void endVisit(AST::TypeArgumentList *) override { stop(u"TypeArgumentList"); }
#endif
bool visit(AST::TypeAnnotation *el) override {
start(QLatin1String("TypeAnnotation colonToken=%1")

View File

@ -419,7 +419,11 @@ const QSet<int> AstRangesVisitor::kindsToSkip()
AST::Node::Kind_ClassElementList,
AST::Node::Kind_PatternElementList,
AST::Node::Kind_PatternPropertyList,
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
AST::Node::Kind_TypeArgument,
#else
AST::Node::Kind_TypeArgumentList,
#endif
})
.unite(VisitAll::uiKinds());
return res;

View File

@ -992,7 +992,11 @@ protected:
bool visit(ESModule *) override { return true; }
bool visit(DebuggerStatement *) override { return true; }
bool visit(Type *) override { return true; }
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
bool visit(TypeArgument *) override { return true; }
#else
bool visit(TypeArgumentList *) override { return true; }
#endif
bool visit(TypeAnnotation *) override { return true; }
// overridden to use BasicVisitor (and ensure warnings about new added AST)
@ -1109,7 +1113,11 @@ protected:
void endVisit(ESModule *) override { }
void endVisit(DebuggerStatement *) override { }
void endVisit(Type *) override { }
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
void endVisit(TypeArgument *) override { }
#else
void endVisit(TypeArgumentList *) override { }
#endif
void endVisit(TypeAnnotation *) override { }
void throwRecursionDepthError() override