1 | // Copyright (C) 2017 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 | #ifndef QV4COMPILERCONTEXT_P_H |
4 | #define QV4COMPILERCONTEXT_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <private/qqmljsast_p.h> |
18 | #include <private/qv4compileddata_p.h> |
19 | #include <QtCore/QStringList> |
20 | #include <QtCore/QDateTime> |
21 | #include <QtCore/QStack> |
22 | #include <QtCore/QHash> |
23 | #include <QtCore/QMap> |
24 | #include <QtCore/QSet> |
25 | #include <QtCore/QVarLengthArray> |
26 | |
27 | #include <memory> |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | namespace QV4 { |
32 | |
33 | namespace Moth { |
34 | class BytecodeGenerator; |
35 | } |
36 | |
37 | namespace Compiler { |
38 | |
39 | class Codegen; |
40 | struct ControlFlow; |
41 | |
42 | enum class ContextType { |
43 | Global, |
44 | Function, |
45 | Eval, |
46 | Binding, // This is almost the same as Eval, except: |
47 | // * function declarations are moved to the return address when encountered |
48 | // * return statements are allowed everywhere (like in FunctionCode) |
49 | // * variable declarations are treated as true locals (like in FunctionCode) |
50 | Block, |
51 | ESModule, |
52 | ScriptImportedByQML, |
53 | }; |
54 | |
55 | struct Context; |
56 | |
57 | struct Class { |
58 | struct Method { |
59 | enum Type { |
60 | Regular, |
61 | Getter, |
62 | Setter |
63 | }; |
64 | uint nameIndex; |
65 | Type type; |
66 | uint functionIndex; |
67 | }; |
68 | |
69 | uint nameIndex; |
70 | uint constructorIndex = UINT_MAX; |
71 | QVector<Method> staticMethods; |
72 | QVector<Method> methods; |
73 | }; |
74 | |
75 | struct TemplateObject { |
76 | QVector<uint> strings; |
77 | QVector<uint> rawStrings; |
78 | bool operator==(const TemplateObject &other) { |
79 | return strings == other.strings && rawStrings == other.rawStrings; |
80 | } |
81 | }; |
82 | |
83 | struct ExportEntry |
84 | { |
85 | QString exportName; |
86 | QString moduleRequest; |
87 | QString importName; |
88 | QString localName; |
89 | CompiledData::Location location; |
90 | |
91 | static bool lessThan(const ExportEntry &lhs, const ExportEntry &rhs) |
92 | { return lhs.exportName < rhs.exportName; } |
93 | }; |
94 | |
95 | struct ImportEntry |
96 | { |
97 | QString moduleRequest; |
98 | QString importName; |
99 | QString localName; |
100 | CompiledData::Location location; |
101 | }; |
102 | |
103 | struct Module { |
104 | Module(const QString &fileName, const QString &finalUrl, bool debugMode) |
105 | : fileName(fileName) |
106 | , finalUrl(finalUrl) |
107 | , debugMode(debugMode) |
108 | {} |
109 | ~Module() { |
110 | qDeleteAll(c: contextMap); |
111 | } |
112 | |
113 | Context *newContext(QQmlJS::AST::Node *node, Context *parent, ContextType compilationMode); |
114 | |
115 | QHash<QQmlJS::AST::Node *, Context *> contextMap; |
116 | QList<Context *> functions; |
117 | QList<Context *> blocks; |
118 | QVector<Class> classes; |
119 | QVector<TemplateObject> templateObjects; |
120 | Context *rootContext; |
121 | QString fileName; |
122 | QString finalUrl; |
123 | QDateTime sourceTimeStamp; |
124 | uint unitFlags = 0; // flags merged into CompiledData::Unit::flags |
125 | bool debugMode = false; |
126 | QVector<ExportEntry> localExportEntries; |
127 | QVector<ExportEntry> indirectExportEntries; |
128 | QVector<ExportEntry> starExportEntries; |
129 | QVector<ImportEntry> importEntries; |
130 | QStringList moduleRequests; |
131 | }; |
132 | |
133 | |
134 | struct Context { |
135 | Context *parent; |
136 | QString name; |
137 | int line = 0; |
138 | int column = 0; |
139 | int registerCountInFunction = 0; |
140 | int functionIndex = -1; |
141 | int blockIndex = -1; |
142 | |
143 | enum MemberType { |
144 | UndefinedMember, |
145 | ThisFunctionName, |
146 | VariableDefinition, |
147 | VariableDeclaration, |
148 | FunctionDefinition |
149 | }; |
150 | |
151 | struct SourceLocationTable |
152 | { |
153 | struct Entry |
154 | { |
155 | quint32 offset; |
156 | QQmlJS::SourceLocation location; |
157 | }; |
158 | QVector<Entry> entries; |
159 | }; |
160 | |
161 | struct Member { |
162 | MemberType type = UndefinedMember; |
163 | int index = -1; |
164 | QQmlJS::AST::VariableScope scope = QQmlJS::AST::VariableScope::Var; |
165 | mutable bool canEscape = false; |
166 | bool isInjected = false; |
167 | QQmlJS::AST::FunctionExpression *function = nullptr; |
168 | QQmlJS::SourceLocation declarationLocation; |
169 | |
170 | bool isLexicallyScoped() const { return this->scope != QQmlJS::AST::VariableScope::Var; } |
171 | bool requiresTDZCheck(const QQmlJS::SourceLocation &accessLocation, bool accessAcrossContextBoundaries) const; |
172 | }; |
173 | typedef QMap<QString, Member> MemberMap; |
174 | |
175 | MemberMap members; |
176 | QSet<QString> usedVariables; |
177 | QQmlJS::AST::FormalParameterList *formals = nullptr; |
178 | QQmlJS::AST::BoundNames arguments; |
179 | QQmlJS::AST::Type *returnType = nullptr; |
180 | QStringList locals; |
181 | QStringList moduleRequests; |
182 | QVector<ImportEntry> importEntries; |
183 | QVector<ExportEntry> exportEntries; |
184 | QString localNameForDefaultExport; |
185 | QVector<Context *> nestedContexts; |
186 | |
187 | ControlFlow *controlFlow = nullptr; |
188 | QByteArray code; |
189 | QVector<CompiledData::CodeOffsetToLineAndStatement> lineAndStatementNumberMapping; |
190 | std::unique_ptr<SourceLocationTable> sourceLocationTable; |
191 | std::vector<unsigned> labelInfo; |
192 | |
193 | int nRegisters = 0; |
194 | int registerOffset = -1; |
195 | int sizeOfLocalTemporalDeadZone = 0; |
196 | int firstTemporalDeadZoneRegister = 0; |
197 | int sizeOfRegisterTemporalDeadZone = 0; |
198 | bool hasDirectEval = false; |
199 | bool allVarsEscape = false; |
200 | bool hasNestedFunctions = false; |
201 | bool isStrict = false; |
202 | bool isArrowFunction = false; |
203 | bool isGenerator = false; |
204 | bool usesThis = false; |
205 | bool innerFunctionAccessesThis = false; |
206 | bool innerFunctionAccessesNewTarget = false; |
207 | bool returnsClosure = false; |
208 | mutable bool argumentsCanEscape = false; |
209 | bool requiresExecutionContext = false; |
210 | bool isWithBlock = false; |
211 | bool isCatchBlock = false; |
212 | QString caughtVariable; |
213 | QQmlJS::SourceLocation lastBlockInitializerLocation; |
214 | |
215 | enum UsesArgumentsObject { |
216 | ArgumentsObjectUnknown, |
217 | ArgumentsObjectNotUsed, |
218 | ArgumentsObjectUsed |
219 | }; |
220 | |
221 | UsesArgumentsObject usesArgumentsObject = ArgumentsObjectUnknown; |
222 | |
223 | ContextType contextType; |
224 | |
225 | template <typename T> |
226 | class SmallSet: public QVarLengthArray<T, 8> |
227 | { |
228 | public: |
229 | void insert(int value) |
230 | { |
231 | for (auto it : *this) { |
232 | if (it == value) |
233 | return; |
234 | } |
235 | this->append(value); |
236 | } |
237 | }; |
238 | |
239 | // Map from meta property index (existence implies dependency) to notify signal index |
240 | struct KeyValuePair |
241 | { |
242 | quint32 _key = 0; |
243 | quint32 _value = 0; |
244 | |
245 | KeyValuePair() {} |
246 | KeyValuePair(quint32 key, quint32 value): _key(key), _value(value) {} |
247 | |
248 | quint32 key() const { return _key; } |
249 | quint32 value() const { return _value; } |
250 | }; |
251 | |
252 | class PropertyDependencyMap: public QVarLengthArray<KeyValuePair, 8> |
253 | { |
254 | public: |
255 | void insert(quint32 key, quint32 value) |
256 | { |
257 | for (auto it = begin(), eit = end(); it != eit; ++it) { |
258 | if (it->_key == key) { |
259 | it->_value = value; |
260 | return; |
261 | } |
262 | } |
263 | append(t: KeyValuePair(key, value)); |
264 | } |
265 | }; |
266 | |
267 | Context(Context *parent, ContextType type) |
268 | : parent(parent) |
269 | , contextType(type) |
270 | { |
271 | if (parent && parent->isStrict) |
272 | isStrict = true; |
273 | } |
274 | |
275 | bool hasArgument(const QString &name) const |
276 | { |
277 | return arguments.contains(name); |
278 | } |
279 | |
280 | int findArgument(const QString &name, bool *isInjected) const |
281 | { |
282 | // search backwards to handle duplicate argument names correctly |
283 | for (int i = arguments.size() - 1; i >= 0; --i) { |
284 | const auto &arg = arguments.at(i); |
285 | if (arg.id == name) { |
286 | *isInjected = arg.isInjected(); |
287 | return i; |
288 | } |
289 | } |
290 | return -1; |
291 | } |
292 | |
293 | Member findMember(const QString &name) const |
294 | { |
295 | MemberMap::const_iterator it = members.find(key: name); |
296 | if (it == members.end()) |
297 | return Member(); |
298 | Q_ASSERT(it->index != -1 || !parent); |
299 | return (*it); |
300 | } |
301 | |
302 | bool memberInfo(const QString &name, const Member **m) const |
303 | { |
304 | Q_ASSERT(m); |
305 | MemberMap::const_iterator it = members.find(key: name); |
306 | if (it == members.end()) { |
307 | *m = nullptr; |
308 | return false; |
309 | } |
310 | *m = &(*it); |
311 | return true; |
312 | } |
313 | |
314 | bool requiresImplicitReturnValue() const { |
315 | return contextType == ContextType::Binding || |
316 | contextType == ContextType::Eval || |
317 | contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML; |
318 | } |
319 | |
320 | void addUsedVariable(const QString &name) { |
321 | usedVariables.insert(value: name); |
322 | } |
323 | |
324 | bool addLocalVar( |
325 | const QString &name, MemberType contextType, QQmlJS::AST::VariableScope scope, |
326 | QQmlJS::AST::FunctionExpression *function = nullptr, |
327 | const QQmlJS::SourceLocation &declarationLocation = QQmlJS::SourceLocation(), |
328 | bool isInjected = false); |
329 | |
330 | struct ResolvedName { |
331 | enum Type { |
332 | Unresolved, |
333 | QmlGlobal, |
334 | Global, |
335 | Local, |
336 | Stack, |
337 | Import |
338 | }; |
339 | Type type = Unresolved; |
340 | bool isArgOrEval = false; |
341 | bool isConst = false; |
342 | bool requiresTDZCheck = false; |
343 | bool isInjected = false; |
344 | int scope = -1; |
345 | int index = -1; |
346 | QQmlJS::SourceLocation declarationLocation; |
347 | bool isValid() const { return type != Unresolved; } |
348 | }; |
349 | ResolvedName resolveName(const QString &name, const QQmlJS::SourceLocation &accessLocation); |
350 | void (Compiler::Codegen *codegen); |
351 | void (Compiler::Codegen *codegen); |
352 | |
353 | void setupFunctionIndices(Moth::BytecodeGenerator *bytecodeGenerator); |
354 | |
355 | bool canHaveTailCalls() const |
356 | { |
357 | if (!isStrict) |
358 | return false; |
359 | if (contextType == ContextType::Function) |
360 | return !isGenerator; |
361 | if (contextType == ContextType::Block && parent) |
362 | return parent->canHaveTailCalls(); |
363 | return false; |
364 | } |
365 | |
366 | bool isCaseBlock() const |
367 | { |
368 | return contextType == ContextType::Block && name == u"%CaseBlock" ; |
369 | } |
370 | }; |
371 | |
372 | |
373 | } } // namespace QV4::Compiler |
374 | |
375 | QT_END_NAMESPACE |
376 | |
377 | #endif // QV4CODEGEN_P_H |
378 | |