aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/parser
diff options
context:
space:
mode:
authorFawzi Mohamed <fawzi.mohamed@qt.io>2020-02-06 14:15:09 +0100
committerFawzi Mohamed <fawzi.mohamed@qt.io>2020-02-12 10:17:18 +0100
commit87f5cc385b670a63c2f2ba0ef90cba95ab215200 (patch)
treea20ba1d0c3644a422d196895eeeff0bd0d927830 /src/qml/parser
parent8e822e981d91e688799c8670f11dfdf6aaf9e0d1 (diff)
Add annotations to AST
Annotations are added to UiObjectMember. This makes it easy to find the annotations of any UiObjectMember through the annotations attribute. The clean AST approach would add an UiAnnotatedObjectMember that contains both the annotation list and an UiObjectMember, but that makes finding the annotation more difficult. The annotations are not visited by default, if one wants to dump them before the current object the simplest way is to use the preVisit and use .uiObjectMemberCast(). Depending on how we use annotation we could change the current approach. Task-number: QTBUG-81714 Change-Id: I543a2cfe5157bcc86de6de9faebde9aea22974eb Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/parser')
-rw-r--r--src/qml/parser/qqmljs.g17
-rw-r--r--src/qml/parser/qqmljsast.cpp9
-rw-r--r--src/qml/parser/qqmljsast_p.h44
-rw-r--r--src/qml/parser/qqmljsastfwd_p.h1
-rw-r--r--src/qml/parser/qqmljsastvisitor_p.h2
5 files changed, 68 insertions, 5 deletions
diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g
index 879de92e5f..a5062aafc7 100644
--- a/src/qml/parser/qqmljs.g
+++ b/src/qml/parser/qqmljs.g
@@ -326,6 +326,7 @@ public:
AST::UiQualifiedId *UiQualifiedId;
AST::UiEnumMemberList *UiEnumMemberList;
AST::UiVersionSpecifier *UiVersionSpecifier;
+ AST::UiAnnotationList *UiAnnotationList;
};
public:
@@ -958,18 +959,24 @@ UiAnnotationObjectDefinition: UiSimpleQualifiedId UiObjectInitializer;
./
UiAnnotation: T_AT UiAnnotationObjectDefinition;
+/.
+case $rule_number: {
+ sym(1).Node = sym(2).Node;
+} break;
+./
+
UiAnnotationList: UiAnnotation;
/.
case $rule_number: {
- sym(1).Node = new (pool) AST::UiArrayMemberList(sym(1).UiObjectMember);
+ sym(1).Node = new (pool) AST::UiAnnotationList(sym(1).UiObjectDefinition);
} break;
./
UiAnnotationList: UiAnnotationList UiAnnotation;
/.
case $rule_number: {
- AST::UiArrayMemberList *node = new (pool) AST::UiArrayMemberList(sym(1).UiArrayMemberList, sym(3).UiObjectMember);
+ AST::UiAnnotationList *node = new (pool) AST::UiAnnotationList(sym(1).UiAnnotationList, sym(2).UiObjectDefinition);
sym(1).Node = node;
} break;
./
@@ -977,7 +984,9 @@ UiAnnotationList: UiAnnotationList UiAnnotation;
UiAnnotatedObject: UiAnnotationList UiObjectDefinition;
/.
case $rule_number: {
- sym(1).Node = sym(2).Node;
+ AST::UiObjectMember *node = sym(2).UiObjectMember;
+ node->annotations = sym(1).UiAnnotationList->finish();
+ sym(1).Node = node;
} break;
./
@@ -1045,6 +1054,8 @@ UiObjectDefinition: UiQualifiedId UiObjectInitializer;
UiAnnotatedObjectMember: UiAnnotationList UiObjectMember;
/.
case $rule_number: {
+ AST::UiObjectMember *node = sym(2).UiObjectMember;
+ node->annotations = sym(1).UiAnnotationList->finish();
sym(1).Node = sym(2).Node;
} break;
./
diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp
index 416916c547..19b03d3cd1 100644
--- a/src/qml/parser/qqmljsast.cpp
+++ b/src/qml/parser/qqmljsast.cpp
@@ -1562,6 +1562,15 @@ void UiRequired::accept0(Visitor *visitor)
visitor->endVisit(this);
}
+void UiAnnotationList::accept0(Visitor *visitor)
+{
+ if (visitor->visit(this)) {
+ accept(annotation, visitor);
+ }
+
+ visitor->endVisit(this);
+}
+
} } // namespace QQmlJS::AST
QT_END_NAMESPACE
diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h
index f3369676e9..a5ef3ca67b 100644
--- a/src/qml/parser/qqmljsast_p.h
+++ b/src/qml/parser/qqmljsast_p.h
@@ -259,7 +259,8 @@ public:
Kind_UiEnumDeclaration,
Kind_UiEnumMemberList,
Kind_UiVersionSpecifier,
- Kind_UiRequired
+ Kind_UiRequired,
+ Kind_UiAnnotationList
};
inline Node() {}
@@ -3042,6 +3043,9 @@ public:
SourceLocation lastSourceLocation() const override = 0;
UiObjectMember *uiObjectMemberCast() override;
+
+// attributes
+ UiAnnotationList *annotations = nullptr;
};
class QML_PARSER_EXPORT UiObjectMemberList: public Node
@@ -3624,8 +3628,44 @@ public:
UiEnumMemberList *members;
};
-} } // namespace AST
+class QML_PARSER_EXPORT UiAnnotationList: public Node
+{
+public:
+ QQMLJS_DECLARE_AST_NODE(UiAnnotationList)
+
+ UiAnnotationList(UiObjectDefinition *annotation)
+ : next(this), annotation(annotation)
+ { kind = K; }
+
+ UiAnnotationList(UiAnnotationList *previous, UiObjectDefinition *annotation)
+ : annotation(annotation)
+ {
+ kind = K;
+ next = previous->next;
+ previous->next = this;
+ }
+
+ void accept0(Visitor *visitor) override;
+
+ SourceLocation firstSourceLocation() const override
+ { return annotation->firstSourceLocation(); }
+
+ SourceLocation lastSourceLocation() const override
+ { return lastListElement(this)->annotation->lastSourceLocation(); }
+
+ UiAnnotationList *finish()
+ {
+ UiAnnotationList *head = next;
+ next = nullptr;
+ return head;
+ }
+// attributes
+ UiAnnotationList *next;
+ UiObjectDefinition *annotation;
+};
+
+} } // namespace AST
QT_END_NAMESPACE
diff --git a/src/qml/parser/qqmljsastfwd_p.h b/src/qml/parser/qqmljsastfwd_p.h
index fe260e2bb5..595319e3c2 100644
--- a/src/qml/parser/qqmljsastfwd_p.h
+++ b/src/qml/parser/qqmljsastfwd_p.h
@@ -184,6 +184,7 @@ class UiEnumDeclaration;
class UiEnumMemberList;
class UiVersionSpecifier;
class UiRequired;
+class UiAnnotationList;
} // namespace AST
} // namespace QQmlJS
diff --git a/src/qml/parser/qqmljsastvisitor_p.h b/src/qml/parser/qqmljsastvisitor_p.h
index fcc48da1d3..566c6336b0 100644
--- a/src/qml/parser/qqmljsastvisitor_p.h
+++ b/src/qml/parser/qqmljsastvisitor_p.h
@@ -113,6 +113,7 @@ public:
virtual bool visit(UiEnumMemberList *) { return true; }
virtual bool visit(UiVersionSpecifier *) { return true; }
virtual bool visit(UiInlineComponent *) { return true; }
+ virtual bool visit(UiAnnotationList *) { return true; }
virtual void endVisit(UiProgram *) {}
virtual void endVisit(UiImport *) {}
@@ -133,6 +134,7 @@ public:
virtual void endVisit(UiEnumMemberList *) { }
virtual void endVisit(UiVersionSpecifier *) {}
virtual void endVisit(UiInlineComponent *) {}
+ virtual void endVisit(UiAnnotationList *) {}
// QQmlJS
virtual bool visit(ThisExpression *) { return true; }