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 // see comment at its call site in generateJSCodeForFunctionsAndBindings
53 // for why this function is necessary
54 void handleTopLevelFunctionFormals(QQmlJS::AST::FunctionExpression *node) {
55 if (node && node->formals)
56 node->formals->accept(this);
57 }
58
59 void enterGlobalEnvironment(ContextType compilationMode);
60 void enterEnvironment(QQmlJS::AST::Node *node, ContextType compilationMode,
61 const QString &name);
62 void leaveEnvironment();
63
64 void enterQmlFunction(QQmlJS::AST::FunctionExpression *ast)
65 { enterFunction(ast, FunctionNameContext::None); }
66
67protected:
68 // Function declarations add their name to the outer scope, but not the
69 // inner scope. Function expressions add their name to the inner scope,
70 // unless the name is actually picked from the outer scope rather than
71 // given after the function token. QML functions don't add their name
72 // anywhere because the name is already recorded in the QML element.
73 // This enum is used to control the behavior of enterFunction().
76 };
77
78 using Visitor::visit;
79 using Visitor::endVisit;
80
81 void checkDirectivePrologue(QQmlJS::AST::StatementList *ast);
82
83 void checkName(QStringView name, const QQmlJS::SourceLocation &loc);
84
85 bool visit(QQmlJS::AST::Program *ast) override;
86 void endVisit(QQmlJS::AST::Program *) override;
87
88 bool visit(QQmlJS::AST::ESModule *ast) override;
89 void endVisit(QQmlJS::AST::ESModule *) override;
90
91 bool visit(QQmlJS::AST::ExportDeclaration *declaration) override;
92 bool visit(QQmlJS::AST::ImportDeclaration *declaration) override;
93
94 bool visit(QQmlJS::AST::CallExpression *ast) override;
95 bool visit(QQmlJS::AST::PatternElement *ast) override;
96 bool visit(QQmlJS::AST::IdentifierExpression *ast) override;
97 bool visit(QQmlJS::AST::ExpressionStatement *ast) override;
98 bool visit(QQmlJS::AST::FunctionExpression *ast) override;
99 bool visit(QQmlJS::AST::TemplateLiteral *ast) override;
100 bool visit(QQmlJS::AST::SuperLiteral *) override;
101 bool visit(QQmlJS::AST::FieldMemberExpression *) override;
102 bool visit(QQmlJS::AST::ArrayPattern *) override;
103
104 bool enterFunction(QQmlJS::AST::FunctionExpression *ast,
105 FunctionNameContext nameContext);
106
107 void endVisit(QQmlJS::AST::FunctionExpression *) override;
108
109 bool visit(QQmlJS::AST::ObjectPattern *ast) override;
110
111 bool visit(QQmlJS::AST::PatternProperty *ast) override;
112 void endVisit(QQmlJS::AST::PatternProperty *) override;
113
114 bool visit(QQmlJS::AST::FunctionDeclaration *ast) override;
115 void endVisit(QQmlJS::AST::FunctionDeclaration *) override;
116
117 bool visit(QQmlJS::AST::ClassExpression *ast) override;
118 void endVisit(QQmlJS::AST::ClassExpression *) override;
119
120 bool visit(QQmlJS::AST::ClassDeclaration *ast) override;
121 void endVisit(QQmlJS::AST::ClassDeclaration *) override;
122
123 bool visit(QQmlJS::AST::DoWhileStatement *ast) override;
124 bool visit(QQmlJS::AST::ForStatement *ast) override;
125 void endVisit(QQmlJS::AST::ForStatement *) override;
126 bool visit(QQmlJS::AST::ForEachStatement *ast) override;
127 void endVisit(QQmlJS::AST::ForEachStatement *) override;
128
129 bool visit(QQmlJS::AST::ThisExpression *ast) override;
130
131 bool visit(QQmlJS::AST::Block *ast) override;
132 void endVisit(QQmlJS::AST::Block *ast) override;
133
134 bool visit(QQmlJS::AST::CaseBlock *ast) override;
135 void endVisit(QQmlJS::AST::CaseBlock *ast) override;
136
137 bool visit(QQmlJS::AST::Catch *ast) override;
138 void endVisit(QQmlJS::AST::Catch *ast) override;
139
140 bool visit(QQmlJS::AST::WithStatement *ast) override;
141 void endVisit(QQmlJS::AST::WithStatement *ast) override;
142
144
145protected:
146 bool enterFunction(QQmlJS::AST::Node *ast, const QString &name,
147 QQmlJS::AST::FormalParameterList *formals,
148 QQmlJS::AST::StatementList *body, FunctionNameContext nameContext);
149
151// fields:
152 Codegen *_cg;
156
159
160private:
161 static constexpr QQmlJS::AST::Node *astNodeForGlobalEnvironment = nullptr;
162};
163
164}
165
166}
167
168QT_END_NAMESPACE
169
170#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 operator()(QQmlJS::AST::Node *node)
Definition qjsvalue.h:24
void forEachContext(Module *m, Action &&action)
static CompiledData::Location location(const QQmlJS::SourceLocation &astLocation)