Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4compilerscanfunctions_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QV4COMPILERSCANFUNCTIONS_P_H
6#define QV4COMPILERSCANFUNCTIONS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qtqmlcompilerglobal_p.h>
20#include <private/qqmljsastvisitor_p.h>
21#include <private/qqmljsast_p.h>
22#include <private/qqmljsengine_p.h>
23#include <private/qv4compilercontext_p.h>
24#include <private/qv4util_p.h>
25#include <QtCore/QStringList>
26#include <QStack>
27#include <QScopedValueRollback>
28
29QT_BEGIN_NAMESPACE
30
31namespace QV4 {
32
33namespace Moth {
34struct Instruction;
35}
36
37namespace CompiledData {
38struct CompilationUnit;
39}
40
41namespace Compiler {
42
43class Codegen;
44
45class ScanFunctions: protected QQmlJS::AST::Visitor
46{
47 typedef QScopedValueRollback<bool> TemporaryBoolAssignment;
48public:
49 ScanFunctions(Codegen *cg, const QString &sourceCode, ContextType defaultProgramType);
50 void operator()(QQmlJS::AST::Node *node);
51
52 // Scanning repeatedly into one module: call calcEscapingVariablesForNewContexts() after.
53 void scanWithoutEscapingVariables(QQmlJS::AST::Node *node)
54 {
55 if (node)
56 node->accept(this);
57 }
59
60 // see comment at its call site in generateJSCodeForFunctionsAndBindings
61 // for why this function is necessary
62 void handleTopLevelFunctionFormals(QQmlJS::AST::FunctionExpression *node) {
63 if (node && node->formals)
64 node->formals->accept(this);
65 }
66
67 void enterGlobalEnvironment(ContextType compilationMode);
68 void enterEnvironment(QQmlJS::AST::Node *node, ContextType compilationMode,
69 const QString &name);
70 void leaveEnvironment();
71
72 void enterQmlFunction(QQmlJS::AST::FunctionExpression *ast)
73 { enterFunction(ast, FunctionNameContext::None); }
74
75protected:
76 // Function declarations add their name to the outer scope, but not the
77 // inner scope. Function expressions add their name to the inner scope,
78 // unless the name is actually picked from the outer scope rather than
79 // given after the function token. QML functions don't add their name
80 // anywhere because the name is already recorded in the QML element.
81 // This enum is used to control the behavior of enterFunction().
84 };
85
86 using Visitor::visit;
87 using Visitor::endVisit;
88
89 void checkDirectivePrologue(QQmlJS::AST::StatementList *ast);
90
91 void checkName(QStringView name, const QQmlJS::SourceLocation &loc);
92
93 bool visit(QQmlJS::AST::Program *ast) override;
94 void endVisit(QQmlJS::AST::Program *) override;
95
96 bool visit(QQmlJS::AST::ESModule *ast) override;
97 void endVisit(QQmlJS::AST::ESModule *) override;
98
99 bool visit(QQmlJS::AST::ExportDeclaration *declaration) override;
100 bool visit(QQmlJS::AST::ImportDeclaration *declaration) override;
101
102 bool visit(QQmlJS::AST::CallExpression *ast) override;
103 bool visit(QQmlJS::AST::PatternElement *ast) override;
104 bool visit(QQmlJS::AST::IdentifierExpression *ast) override;
105 bool visit(QQmlJS::AST::ExpressionStatement *ast) override;
106 bool visit(QQmlJS::AST::FunctionExpression *ast) override;
107 bool visit(QQmlJS::AST::SuperLiteral *) override;
108 bool visit(QQmlJS::AST::FieldMemberExpression *) override;
109 bool visit(QQmlJS::AST::ArrayPattern *) override;
110
111 bool enterFunction(QQmlJS::AST::FunctionExpression *ast,
112 FunctionNameContext nameContext);
113
114 void endVisit(QQmlJS::AST::FunctionExpression *) override;
115
116 bool visit(QQmlJS::AST::ObjectPattern *ast) override;
117
118 bool visit(QQmlJS::AST::PatternProperty *ast) override;
119 void endVisit(QQmlJS::AST::PatternProperty *) override;
120
121 bool visit(QQmlJS::AST::FunctionDeclaration *ast) override;
122 void endVisit(QQmlJS::AST::FunctionDeclaration *) override;
123
124 bool visit(QQmlJS::AST::ClassExpression *ast) override;
125 void endVisit(QQmlJS::AST::ClassExpression *) override;
126
127 bool visit(QQmlJS::AST::ClassDeclaration *ast) override;
128 void endVisit(QQmlJS::AST::ClassDeclaration *) override;
129
130 bool visit(QQmlJS::AST::DoWhileStatement *ast) override;
131 bool visit(QQmlJS::AST::ForStatement *ast) override;
132 void endVisit(QQmlJS::AST::ForStatement *) override;
133 bool visit(QQmlJS::AST::ForEachStatement *ast) override;
134 void endVisit(QQmlJS::AST::ForEachStatement *) override;
135
136 bool visit(QQmlJS::AST::ThisExpression *ast) override;
137
138 bool visit(QQmlJS::AST::Block *ast) override;
139 void endVisit(QQmlJS::AST::Block *ast) override;
140
141 bool visit(QQmlJS::AST::CaseBlock *ast) override;
142 void endVisit(QQmlJS::AST::CaseBlock *ast) override;
143
144 bool visit(QQmlJS::AST::Catch *ast) override;
145 void endVisit(QQmlJS::AST::Catch *ast) override;
146
147 bool visit(QQmlJS::AST::WithStatement *ast) override;
148 void endVisit(QQmlJS::AST::WithStatement *ast) override;
149
151
152protected:
153 bool enterFunction(QQmlJS::AST::Node *ast, const QString &name,
154 QQmlJS::AST::FormalParameterList *formals,
155 QQmlJS::AST::StatementList *body, FunctionNameContext nameContext);
156
157// fields:
158 Codegen *_cg;
162
165
166private:
167 static constexpr QQmlJS::AST::Node *astNodeForGlobalEnvironment = nullptr;
168};
169
170}
171
172}
173
174QT_END_NAMESPACE
175
176#endif // QV4CODEGEN_P_H
bool visit(QQmlJS::AST::Program *ast) override
bool enterFunction(QQmlJS::AST::FunctionExpression *ast, FunctionNameContext nameContext)
bool enterFunction(QQmlJS::AST::Node *ast, const QString &name, QQmlJS::AST::FormalParameterList *formals, QQmlJS::AST::StatementList *body, FunctionNameContext nameContext)
ScanFunctions(Codegen *cg, const QString &sourceCode, ContextType defaultProgramType)
void checkName(QStringView name, const QQmlJS::SourceLocation &loc)
void enterQmlFunction(QQmlJS::AST::FunctionExpression *ast)
void checkDirectivePrologue(QQmlJS::AST::StatementList *ast)
void handleTopLevelFunctionFormals(QQmlJS::AST::FunctionExpression *node)
void endVisit(QQmlJS::AST::Program *) override
void enterEnvironment(QQmlJS::AST::Node *node, ContextType compilationMode, const QString &name)
void enterGlobalEnvironment(ContextType compilationMode)
void scanWithoutEscapingVariables(QQmlJS::AST::Node *node)
void operator()(QQmlJS::AST::Node *node)
Definition qjsvalue.h:24
static void calcEscapingVariablesImpl(Module *m, ForEach &&forEach)
void forEachContext(Module *m, Action &&action)
static CompiledData::Location location(const QQmlJS::SourceLocation &astLocation)
bool visit(QQmlJS::AST::IdentifierExpression *ast) override