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
qv4compilercontext_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QV4COMPILERCONTEXT_P_H
6#define QV4COMPILERCONTEXT_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/qqmljsast_p.h>
20#include <private/qv4compileddata_p.h>
21#include <QtCore/QStringList>
22#include <QtCore/QDateTime>
23#include <QtCore/QStack>
24#include <QtCore/QHash>
25#include <QtCore/QMap>
26#include <QtCore/QSet>
27#include <QtCore/QVarLengthArray>
28
29#include <memory>
30
31QT_BEGIN_NAMESPACE
32
33namespace QV4 {
34
35namespace Moth {
37}
38
39namespace Compiler {
40
41class Codegen;
42struct ControlFlow;
43
44enum class ContextType {
48 Binding, // This is almost the same as Eval, except:
49 // * function declarations are moved to the return address when encountered
50 // * return statements are allowed everywhere (like in FunctionCode)
51 // * variable declarations are treated as true locals (like in FunctionCode)
55};
56
57struct Context;
58
76
80 bool operator==(const TemplateObject &other) {
81 return strings == other.strings && rawStrings == other.rawStrings;
82 }
83};
84
86{
92
93 static bool lessThan(const ExportEntry &lhs, const ExportEntry &rhs)
94 { return lhs.exportName < rhs.exportName; }
95};
96
104
105struct Module {
106 Module(const QString &fileName, const QString &finalUrl,
107 Moth::DebugInstructionPass debugInstructionPass)
111 {}
113 qDeleteAll(contextMap);
114 }
115
116 Context *newContext(QQmlJS::AST::Node *node, Context *parent, ContextType compilationMode);
117
127 uint unitFlags = 0; // flags merged into CompiledData::Unit::flags
129 bool isDebugging() const { return debugInstructionPass != nullptr; }
135};
136
137
138struct Context {
141 int line = 0;
142 int column = 0;
145 int blockIndex = -1;
146
154
164
165 struct Member {
167 int index = -1;
169 mutable bool canEscape = false;
170 bool isInjected = false;
174
175 bool isLexicallyScoped() const { return this->scope != QQmlJS::AST::VariableScope::Var; }
176 bool requiresTDZCheck(const QQmlJS::SourceLocation &accessLocation, bool accessAcrossContextBoundaries) const;
177 };
179
184 QQmlJS::AST::Type *returnType = nullptr;
191
196 std::vector<unsigned> labelInfo;
197
198 int nRegisters = 0;
203 bool hasDirectEval = false;
204 bool allVarsEscape = false;
205 bool hasNestedFunctions = false;
206 bool isStrict = false;
207 bool isArrowFunction = false;
208 bool isGenerator = false;
209 bool usesThis = false;
212 bool returnsClosure = false;
213 mutable bool argumentsCanEscape = false;
215 bool isWithBlock = false;
217 bool isCatchBlock = false;
220
226
228
230
231 template <typename T>
232 class SmallSet: public QVarLengthArray<T, 8>
233 {
234 public:
235 void insert(int value)
236 {
237 for (auto it : *this) {
238 if (it == value)
239 return;
240 }
241 this->append(value);
242 }
243 };
244
245 // Map from meta property index (existence implies dependency) to notify signal index
247 {
250
252 KeyValuePair(quint32 key, quint32 value): _key(key), _value(value) {}
253
254 quint32 key() const { return _key; }
255 quint32 value() const { return _value; }
256 };
257
259 {
260 public:
261 void insert(quint32 key, quint32 value)
262 {
263 for (auto it = begin(), eit = end(); it != eit; ++it) {
264 if (it->_key == key) {
265 it->_value = value;
266 return;
267 }
268 }
269 append(KeyValuePair(key, value));
270 }
271 };
272
274 : parent(parent)
275 , contextType(type)
276 {
277 if (parent && parent->isStrict)
278 isStrict = true;
279 }
280
281 bool hasArgument(const QString &name) const
282 {
283 return arguments.contains(name);
284 }
285
286 int findArgument(const QString &name, bool *isInjected) const
287 {
288 // search backwards to handle duplicate argument names correctly
289 for (int i = arguments.size() - 1; i >= 0; --i) {
290 const auto &arg = arguments.at(i);
291 if (arg.id == name) {
292 *isInjected = arg.isInjected();
293 return i;
294 }
295 }
296 return -1;
297 }
298
299 Member findMember(const QString &name) const
300 {
301 MemberMap::const_iterator it = members.find(name);
302 if (it == members.end())
303 return Member();
304 Q_ASSERT(it->index != -1 || !parent);
305 return (*it);
306 }
307
308 bool memberInfo(const QString &name, const Member **m) const
309 {
310 Q_ASSERT(m);
311 MemberMap::const_iterator it = members.find(name);
312 if (it == members.end()) {
313 *m = nullptr;
314 return false;
315 }
316 *m = &(*it);
317 return true;
318 }
319
325
326 void addUsedVariable(const QString &name) {
327 usedVariables.insert(name);
328 }
329
330 bool addLocalVar(
331 const QString &name, MemberType contextType, QQmlJS::AST::VariableScope scope,
332 QQmlJS::AST::FunctionExpression *function = nullptr,
333 const QQmlJS::SourceLocation &declarationLocation = QQmlJS::SourceLocation(),
334 bool isInjected = false);
335
336 void promoteFormalParameterForTDZ(const QString &name, const QQmlJS::SourceLocation &loc);
337
358 ResolvedName resolveName(const QString &name, const QQmlJS::SourceLocation &accessLocation);
359 void emitBlockHeader(Compiler::Codegen *codegen);
360 void emitBlockFooter(Compiler::Codegen *codegen);
361
362 void setupFunctionIndices(Moth::BytecodeGenerator *bytecodeGenerator);
363
364 bool canHaveTailCalls() const
365 {
366 if (!isStrict)
367 return false;
369 return !isGenerator;
372 return false;
373 }
374
375 bool isCaseBlock() const
376 {
377 return contextType == ContextType::Block && name == u"%CaseBlock";
378 }
379};
380
381
382} } // namespace QV4::Compiler
383
384QT_END_NAMESPACE
385
386#endif // QV4CODEGEN_P_H
Definition qjsvalue.h:24
QList< Method > staticMethods
KeyValuePair(quint32 key, quint32 value)
QQmlJS::AST::FunctionExpression * function
bool requiresTDZCheck(const QQmlJS::SourceLocation &accessLocation, bool accessAcrossContextBoundaries) const
QQmlJS::SourceLocation declarationLocation
QQmlJS::AST::VariableScope scope
QList< Context * > nestedContexts
bool memberInfo(const QString &name, const Member **m) const
void emitBlockFooter(Compiler::Codegen *codegen)
QQmlJS::AST::FormalParameterList * formals
bool hasArgument(const QString &name) const
void emitBlockHeader(Compiler::Codegen *codegen)
QQmlJS::AST::BoundNames arguments
std::unique_ptr< SourceLocationTable > sourceLocationTable
UsesArgumentsObject usesArgumentsObject
QList< CompiledData::CodeOffsetToLineAndStatement > lineAndStatementNumberMapping
QList< ImportEntry > importEntries
QMap< QString, Member > MemberMap
QQmlJS::AST::Type * returnType
Context(Context *parent, ContextType type)
ResolvedName resolveName(const QString &name, const QQmlJS::SourceLocation &accessLocation)
void setupFunctionIndices(Moth::BytecodeGenerator *bytecodeGenerator)
QQmlJS::SourceLocation lastBlockInitializerLocation
void addUsedVariable(const QString &name)
void promoteFormalParameterForTDZ(const QString &name, const QQmlJS::SourceLocation &loc)
QList< ExportEntry > exportEntries
std::vector< unsigned > labelInfo
Member findMember(const QString &name) const
bool addLocalVar(const QString &name, MemberType contextType, QQmlJS::AST::VariableScope scope, QQmlJS::AST::FunctionExpression *function=nullptr, const QQmlJS::SourceLocation &declarationLocation=QQmlJS::SourceLocation(), bool isInjected=false)
int findArgument(const QString &name, bool *isInjected) const
CompiledData::Location location
static bool lessThan(const ExportEntry &lhs, const ExportEntry &rhs)
CompiledData::Location location
Context * newContext(QQmlJS::AST::Node *node, Context *parent, ContextType compilationMode)
QHash< QQmlJS::AST::Node *, Context * > contextMap
QList< ExportEntry > indirectExportEntries
QList< Context * > functions
Module(const QString &fileName, const QString &finalUrl, Moth::DebugInstructionPass debugInstructionPass)
QList< ExportEntry > starExportEntries
QList< TemplateObject > templateObjects
Moth::DebugInstructionPass debugInstructionPass
QList< ImportEntry > importEntries
QList< ExportEntry > localExportEntries
bool operator==(const TemplateObject &other)