1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLJSASTVISITOR_P_H
5#define QQMLJSASTVISITOR_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmljsastfwd_p.h"
19#include "qqmljsglobal_p.h"
20
21QT_BEGIN_NAMESPACE
22
23// as done in https://en.wikipedia.org/wiki/X_Macro
24
25#define QQmlJSASTUiClassListToVisit \
26 X(UiProgram) \
27 X(UiHeaderItemList) \
28 X(UiPragmaValueList) \
29 X(UiPragma) \
30 X(UiImport) \
31 X(UiPublicMember) \
32 X(UiSourceElement) \
33 X(UiObjectDefinition) \
34 X(UiObjectInitializer) \
35 X(UiObjectBinding) \
36 X(UiScriptBinding) \
37 X(UiArrayBinding) \
38 X(UiParameterList) \
39 X(UiObjectMemberList) \
40 X(UiArrayMemberList) \
41 X(UiQualifiedId) \
42 X(UiEnumDeclaration) \
43 X(UiEnumMemberList) \
44 X(UiVersionSpecifier) \
45 X(UiInlineComponent) \
46 X(UiAnnotation) \
47 X(UiAnnotationList) \
48 X(UiRequired)
49
50#define QQmlJSASTQQmlJSClassListToVisit \
51 X(TypeExpression) \
52 X(ThisExpression) \
53 X(IdentifierExpression) \
54 X(NullExpression) \
55 X(TrueLiteral) \
56 X(FalseLiteral) \
57 X(SuperLiteral) \
58 X(StringLiteral) \
59 X(TemplateLiteral) \
60 X(NumericLiteral) \
61 X(RegExpLiteral) \
62 X(ArrayPattern) \
63 X(ObjectPattern) \
64 X(PatternElementList) \
65 X(PatternPropertyList) \
66 X(PatternElement) \
67 X(PatternProperty) \
68 X(Elision) \
69 X(NestedExpression) \
70 X(IdentifierPropertyName) \
71 X(StringLiteralPropertyName) \
72 X(NumericLiteralPropertyName) \
73 X(ComputedPropertyName) \
74 X(ArrayMemberExpression) \
75 X(FieldMemberExpression) \
76 X(TaggedTemplate) \
77 X(NewMemberExpression) \
78 X(NewExpression) \
79 X(CallExpression) \
80 X(ArgumentList) \
81 X(PostIncrementExpression) \
82 X(PostDecrementExpression) \
83 X(DeleteExpression) \
84 X(VoidExpression) \
85 X(TypeOfExpression) \
86 X(PreIncrementExpression) \
87 X(PreDecrementExpression) \
88 X(UnaryPlusExpression) \
89 X(UnaryMinusExpression) \
90 X(TildeExpression) \
91 X(NotExpression) \
92 X(BinaryExpression) \
93 X(ConditionalExpression) \
94 X(Expression) \
95 X(Block) \
96 X(StatementList) \
97 X(VariableStatement) \
98 X(VariableDeclarationList) \
99 X(EmptyStatement) \
100 X(ExpressionStatement) \
101 X(IfStatement) \
102 X(DoWhileStatement) \
103 X(WhileStatement) \
104 X(ForStatement) \
105 X(ForEachStatement) \
106 X(ContinueStatement) \
107 X(BreakStatement) \
108 X(ReturnStatement) \
109 X(YieldExpression) \
110 X(WithStatement) \
111 X(SwitchStatement) \
112 X(CaseBlock) \
113 X(CaseClauses) \
114 X(CaseClause) \
115 X(DefaultClause) \
116 X(LabelledStatement) \
117 X(ThrowStatement) \
118 X(TryStatement) \
119 X(Catch) \
120 X(Finally) \
121 X(FunctionDeclaration) \
122 X(FunctionExpression) \
123 X(FormalParameterList) \
124 X(ClassExpression) \
125 X(ClassDeclaration) \
126 X(ClassElementList) \
127 X(Program) \
128 X(NameSpaceImport) \
129 X(ImportSpecifier) \
130 X(ImportsList) \
131 X(NamedImports) \
132 X(FromClause) \
133 X(ImportClause) \
134 X(ImportDeclaration) \
135 X(ExportSpecifier) \
136 X(ExportsList) \
137 X(ExportClause) \
138 X(ExportDeclaration) \
139 X(ESModule) \
140 X(DebuggerStatement) \
141 X(Type) \
142 X(TypeAnnotation)
143
144#define QQmlJSASTClassListToVisit QQmlJSASTUiClassListToVisit QQmlJSASTQQmlJSClassListToVisit
145
146namespace QQmlJS { namespace AST {
147
148class QML_PARSER_EXPORT BaseVisitor
149{
150public:
151 class RecursionDepthCheck
152 {
153 Q_DISABLE_COPY(RecursionDepthCheck)
154 public:
155 RecursionDepthCheck(RecursionDepthCheck &&) = delete;
156 RecursionDepthCheck &operator=(RecursionDepthCheck &&) = delete;
157
158 RecursionDepthCheck(BaseVisitor *visitor) : m_visitor(visitor)
159 {
160 ++(m_visitor->m_recursionDepth);
161 }
162
163 ~RecursionDepthCheck()
164 {
165 --(m_visitor->m_recursionDepth);
166 }
167
168 bool operator()() const {
169 return m_visitor->m_recursionDepth < s_recursionLimit;
170 }
171
172 private:
173 static const quint16 s_recursionLimit = 4096;
174 BaseVisitor *m_visitor;
175 };
176
177 BaseVisitor(quint16 parentRecursionDepth = 0);
178 virtual ~BaseVisitor();
179
180 virtual bool preVisit(Node *) = 0;
181 virtual void postVisit(Node *) = 0;
182
183#define X(name) \
184 virtual bool visit(name *) = 0; \
185 virtual void endVisit(name *) = 0;
186 QQmlJSASTClassListToVisit
187#undef X
188
189 virtual void throwRecursionDepthError() = 0;
190
191 quint16 recursionDepth() const { return m_recursionDepth; }
192
193protected:
194 quint16 m_recursionDepth = 0;
195 friend class RecursionDepthCheck;
196};
197
198class QML_PARSER_EXPORT Visitor: public BaseVisitor
199{
200public:
201 Visitor(quint16 parentRecursionDepth = 0);
202
203 bool preVisit(Node *) override { return true; }
204 void postVisit(Node *) override {}
205
206#define X(name) \
207 bool visit(name *) override { return true; } \
208 void endVisit(name *) override { }
209 QQmlJSASTClassListToVisit
210#undef X
211};
212
213} } // namespace AST
214
215QT_END_NAMESPACE
216
217#endif // QQMLJSASTVISITOR_P_H
218

source code of qtdeclarative/src/qml/parser/qqmljsastvisitor_p.h