aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/parser/qqmljsastvisitor_p.h
blob: 540d21d725da5c28fc137edf9994407000611556 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQMLJSASTVISITOR_P_H
#define QQMLJSASTVISITOR_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qqmljsastfwd_p.h"
#include "qqmljsglobal_p.h"

QT_BEGIN_NAMESPACE

// as done in https://en.wikipedia.org/wiki/X_Macro

#define QQmlJSASTUiClassListToVisit \
    X(UiProgram)                    \
    X(UiHeaderItemList)             \
    X(UiPragmaValueList)            \
    X(UiPragma)                     \
    X(UiImport)                     \
    X(UiPublicMember)               \
    X(UiSourceElement)              \
    X(UiObjectDefinition)           \
    X(UiObjectInitializer)          \
    X(UiObjectBinding)              \
    X(UiScriptBinding)              \
    X(UiArrayBinding)               \
    X(UiParameterList)              \
    X(UiObjectMemberList)           \
    X(UiArrayMemberList)            \
    X(UiQualifiedId)                \
    X(UiEnumDeclaration)            \
    X(UiEnumMemberList)             \
    X(UiVersionSpecifier)           \
    X(UiInlineComponent)            \
    X(UiAnnotation)                 \
    X(UiAnnotationList)             \
    X(UiRequired)

#define QQmlJSASTQQmlJSClassListToVisit \
    X(TypeExpression)                   \
    X(ThisExpression)                   \
    X(IdentifierExpression)             \
    X(NullExpression)                   \
    X(TrueLiteral)                      \
    X(FalseLiteral)                     \
    X(SuperLiteral)                     \
    X(StringLiteral)                    \
    X(TemplateLiteral)                  \
    X(NumericLiteral)                   \
    X(RegExpLiteral)                    \
    X(ArrayPattern)                     \
    X(ObjectPattern)                    \
    X(PatternElementList)               \
    X(PatternPropertyList)              \
    X(PatternElement)                   \
    X(PatternProperty)                  \
    X(Elision)                          \
    X(NestedExpression)                 \
    X(IdentifierPropertyName)           \
    X(StringLiteralPropertyName)        \
    X(NumericLiteralPropertyName)       \
    X(ComputedPropertyName)             \
    X(ArrayMemberExpression)            \
    X(FieldMemberExpression)            \
    X(TaggedTemplate)                   \
    X(NewMemberExpression)              \
    X(NewExpression)                    \
    X(CallExpression)                   \
    X(ArgumentList)                     \
    X(PostIncrementExpression)          \
    X(PostDecrementExpression)          \
    X(DeleteExpression)                 \
    X(VoidExpression)                   \
    X(TypeOfExpression)                 \
    X(PreIncrementExpression)           \
    X(PreDecrementExpression)           \
    X(UnaryPlusExpression)              \
    X(UnaryMinusExpression)             \
    X(TildeExpression)                  \
    X(NotExpression)                    \
    X(BinaryExpression)                 \
    X(ConditionalExpression)            \
    X(Expression)                       \
    X(Block)                            \
    X(StatementList)                    \
    X(VariableStatement)                \
    X(VariableDeclarationList)          \
    X(EmptyStatement)                   \
    X(ExpressionStatement)              \
    X(IfStatement)                      \
    X(DoWhileStatement)                 \
    X(WhileStatement)                   \
    X(ForStatement)                     \
    X(ForEachStatement)                 \
    X(ContinueStatement)                \
    X(BreakStatement)                   \
    X(ReturnStatement)                  \
    X(YieldExpression)                  \
    X(WithStatement)                    \
    X(SwitchStatement)                  \
    X(CaseBlock)                        \
    X(CaseClauses)                      \
    X(CaseClause)                       \
    X(DefaultClause)                    \
    X(LabelledStatement)                \
    X(ThrowStatement)                   \
    X(TryStatement)                     \
    X(Catch)                            \
    X(Finally)                          \
    X(FunctionDeclaration)              \
    X(FunctionExpression)               \
    X(FormalParameterList)              \
    X(ClassExpression)                  \
    X(ClassDeclaration)                 \
    X(ClassElementList)                 \
    X(Program)                          \
    X(NameSpaceImport)                  \
    X(ImportSpecifier)                  \
    X(ImportsList)                      \
    X(NamedImports)                     \
    X(FromClause)                       \
    X(ImportClause)                     \
    X(ImportDeclaration)                \
    X(ExportSpecifier)                  \
    X(ExportsList)                      \
    X(ExportClause)                     \
    X(ExportDeclaration)                \
    X(ESModule)                         \
    X(DebuggerStatement)                \
    X(Type)                             \
    X(TypeAnnotation)

#define QQmlJSASTClassListToVisit QQmlJSASTUiClassListToVisit QQmlJSASTQQmlJSClassListToVisit

namespace QQmlJS { namespace AST {

class QML_PARSER_EXPORT BaseVisitor
{
public:
    class RecursionDepthCheck
    {
        Q_DISABLE_COPY(RecursionDepthCheck)
    public:
        RecursionDepthCheck(RecursionDepthCheck &&) = delete;
        RecursionDepthCheck &operator=(RecursionDepthCheck &&) = delete;

        RecursionDepthCheck(BaseVisitor *visitor) : m_visitor(visitor)
        {
            ++(m_visitor->m_recursionDepth);
        }

        ~RecursionDepthCheck()
        {
            --(m_visitor->m_recursionDepth);
        }

        bool operator()() const {
            return m_visitor->m_recursionDepth < s_recursionLimit;
        }

    private:
        static const quint16 s_recursionLimit = 4096;
        BaseVisitor *m_visitor;
    };

    BaseVisitor(quint16 parentRecursionDepth = 0);
    virtual ~BaseVisitor();

    virtual bool preVisit(Node *) = 0;
    virtual void postVisit(Node *) = 0;

#define X(name)                     \
    virtual bool visit(name *) = 0; \
    virtual void endVisit(name *) = 0;
    QQmlJSASTClassListToVisit
#undef X

    virtual void throwRecursionDepthError() = 0;

    quint16 recursionDepth() const { return m_recursionDepth; }

protected:
    quint16 m_recursionDepth = 0;
    friend class RecursionDepthCheck;
};

class QML_PARSER_EXPORT Visitor: public BaseVisitor
{
public:
    Visitor(quint16 parentRecursionDepth = 0);

    bool preVisit(Node *) override { return true; }
    void postVisit(Node *) override {}

#define X(name)                                  \
    bool visit(name *) override { return true; } \
    void endVisit(name *) override { }
    QQmlJSASTClassListToVisit
#undef X
};

class QML_PARSER_EXPORT JSVisitor : public BaseVisitor
{
public:
    JSVisitor() = default;

    bool preVisit(Node *) override { return true; }
    void postVisit(Node *) override { }

#define X(name)                                  \
    bool visit(name *) override { return true; } \
    void endVisit(name *) override { }
    QQmlJSASTQQmlJSClassListToVisit
#undef X

#define X(name)                 \
    bool visit(name *) override \
    {                           \
        Q_ASSERT(false);        \
        return false;           \
    }                           \
    void endVisit(name *) override { }
            QQmlJSASTUiClassListToVisit
#undef X
}; // namespace AST
} } // namespace AST

QT_END_NAMESPACE

#endif // QQMLJSASTVISITOR_P_H