1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef QV4COMPILERSCANFUNCTIONS_P_H
40#define QV4COMPILERSCANFUNCTIONS_P_H
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <private/qtqmlcompilerglobal_p.h>
54#include <private/qqmljsastvisitor_p.h>
55#include <private/qqmljsast_p.h>
56#include <private/qqmljsengine_p.h>
57#include <private/qv4compilercontext_p.h>
58#include <private/qv4util_p.h>
59#include <QtCore/QStringList>
60#include <QStack>
61#include <QScopedValueRollback>
62
63QT_BEGIN_NAMESPACE
64
65namespace QV4 {
66
67namespace Moth {
68struct Instruction;
69}
70
71namespace CompiledData {
72struct CompilationUnit;
73}
74
75namespace Compiler {
76
77class Codegen;
78
79class ScanFunctions: protected QQmlJS::AST::Visitor
80{
81 typedef QScopedValueRollback<bool> TemporaryBoolAssignment;
82public:
83 ScanFunctions(Codegen *cg, const QString &sourceCode, ContextType defaultProgramType);
84 void operator()(QQmlJS::AST::Node *node);
85
86 // see comment at its call site in generateJSCodeForFunctionsAndBindings
87 // for why this function is necessary
88 void handleTopLevelFunctionFormals(QQmlJS::AST::FunctionExpression *node) {
89 if (node && node->formals)
90 node->formals->accept(visitor: this);
91 }
92
93 void enterGlobalEnvironment(ContextType compilationMode);
94 void enterEnvironment(QQmlJS::AST::Node *node, ContextType compilationMode,
95 const QString &name);
96 void leaveEnvironment();
97
98 void enterQmlFunction(QQmlJS::AST::FunctionExpression *ast)
99 { enterFunction(ast, enterName: false); }
100
101protected:
102 using Visitor::visit;
103 using Visitor::endVisit;
104
105 void checkDirectivePrologue(QQmlJS::AST::StatementList *ast);
106
107 void checkName(const QStringRef &name, const QQmlJS::SourceLocation &loc);
108
109 bool visit(QQmlJS::AST::Program *ast) override;
110 void endVisit(QQmlJS::AST::Program *) override;
111
112 bool visit(QQmlJS::AST::ESModule *ast) override;
113 void endVisit(QQmlJS::AST::ESModule *) override;
114
115 bool visit(QQmlJS::AST::ExportDeclaration *declaration) override;
116 bool visit(QQmlJS::AST::ImportDeclaration *declaration) override;
117
118 bool visit(QQmlJS::AST::CallExpression *ast) override;
119 bool visit(QQmlJS::AST::PatternElement *ast) override;
120 bool visit(QQmlJS::AST::IdentifierExpression *ast) override;
121 bool visit(QQmlJS::AST::ExpressionStatement *ast) override;
122 bool visit(QQmlJS::AST::FunctionExpression *ast) override;
123 bool visit(QQmlJS::AST::TemplateLiteral *ast) override;
124 bool visit(QQmlJS::AST::SuperLiteral *) override;
125 bool visit(QQmlJS::AST::FieldMemberExpression *) override;
126 bool visit(QQmlJS::AST::ArrayPattern *) override;
127
128 bool enterFunction(QQmlJS::AST::FunctionExpression *ast, bool enterName);
129
130 void endVisit(QQmlJS::AST::FunctionExpression *) override;
131
132 bool visit(QQmlJS::AST::ObjectPattern *ast) override;
133
134 bool visit(QQmlJS::AST::PatternProperty *ast) override;
135 void endVisit(QQmlJS::AST::PatternProperty *) override;
136
137 bool visit(QQmlJS::AST::FunctionDeclaration *ast) override;
138 void endVisit(QQmlJS::AST::FunctionDeclaration *) override;
139
140 bool visit(QQmlJS::AST::ClassExpression *ast) override;
141 void endVisit(QQmlJS::AST::ClassExpression *) override;
142
143 bool visit(QQmlJS::AST::ClassDeclaration *ast) override;
144 void endVisit(QQmlJS::AST::ClassDeclaration *) override;
145
146 bool visit(QQmlJS::AST::DoWhileStatement *ast) override;
147 bool visit(QQmlJS::AST::ForStatement *ast) override;
148 void endVisit(QQmlJS::AST::ForStatement *) override;
149 bool visit(QQmlJS::AST::ForEachStatement *ast) override;
150 void endVisit(QQmlJS::AST::ForEachStatement *) override;
151
152 bool visit(QQmlJS::AST::ThisExpression *ast) override;
153
154 bool visit(QQmlJS::AST::Block *ast) override;
155 void endVisit(QQmlJS::AST::Block *ast) override;
156
157 bool visit(QQmlJS::AST::CaseBlock *ast) override;
158 void endVisit(QQmlJS::AST::CaseBlock *ast) override;
159
160 bool visit(QQmlJS::AST::Catch *ast) override;
161 void endVisit(QQmlJS::AST::Catch *ast) override;
162
163 bool visit(QQmlJS::AST::WithStatement *ast) override;
164 void endVisit(QQmlJS::AST::WithStatement *ast) override;
165
166 void throwRecursionDepthError() override;
167
168protected:
169 bool enterFunction(QQmlJS::AST::Node *ast, const QString &name,
170 QQmlJS::AST::FormalParameterList *formals,
171 QQmlJS::AST::StatementList *body, bool enterName);
172
173 void calcEscapingVariables();
174// fields:
175 Codegen *_cg;
176 const QString _sourceCode;
177 Context *_context;
178 QStack<Context *> _contextStack;
179
180 bool _allowFuncDecls;
181 ContextType defaultProgramType;
182
183private:
184 static constexpr QQmlJS::AST::Node *astNodeForGlobalEnvironment = nullptr;
185};
186
187}
188
189}
190
191QT_END_NAMESPACE
192
193#endif // QV4CODEGEN_P_H
194

source code of qtdeclarative/src/qml/compiler/qv4compilerscanfunctions_p.h