Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#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
30
31namespace QV4 {
32
33namespace Moth {
34class BytecodeGenerator;
35}
36
37namespace Compiler {
38
39class Codegen;
40struct ControlFlow;
41
42enum class ContextType {
43 Global,
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,
53};
54
55struct Context;
56
57struct Class {
68
71 QVector<Method> staticMethods;
72 QVector<Method> methods;
73};
74
76 QVector<uint> strings;
77 QVector<uint> rawStrings;
79 return strings == other.strings && rawStrings == other.rawStrings;
80 }
81};
82
94
102
103struct Module {
106 {}
110
111 Context *newContext(QQmlJS::AST::Node *node, Context *parent, ContextType compilationMode);
112
113 QHash<QQmlJS::AST::Node *, Context *> contextMap;
114 QList<Context *> functions;
115 QList<Context *> blocks;
116 QVector<Class> classes;
117 QVector<TemplateObject> templateObjects;
122 uint unitFlags = 0; // flags merged into CompiledData::Unit::flags
123 bool debugMode = false;
124 QVector<ExportEntry> localExportEntries;
125 QVector<ExportEntry> indirectExportEntries;
126 QVector<ExportEntry> starExportEntries;
127 QVector<ImportEntry> importEntries;
129};
130
131
132struct Context {
135 int line = 0;
136 int column = 0;
139 int blockIndex = -1;
140
148
150 {
156 QVector<Entry> entries;
157 };
158
159 struct Member {
161 int index = -1;
163 mutable bool canEscape = false;
164 bool isInjected = false;
167
168 bool isLexicallyScoped() const { return this->scope != QQmlJS::AST::VariableScope::Var; }
169 bool requiresTDZCheck(const QQmlJS::SourceLocation &accessLocation, bool accessAcrossContextBoundaries) const;
170 };
171 typedef QMap<QString, Member> MemberMap;
172
174 QSet<QString> usedVariables;
180 QVector<ImportEntry> importEntries;
181 QVector<ExportEntry> exportEntries;
183 QVector<Context *> nestedContexts;
184
187 QVector<CompiledData::CodeOffsetToLineAndStatement> lineAndStatementNumberMapping;
188 std::unique_ptr<SourceLocationTable> sourceLocationTable;
189 std::vector<unsigned> labelInfo;
190
191 int nRegisters = 0;
196 bool hasDirectEval = false;
197 bool allVarsEscape = false;
198 bool hasNestedFunctions = false;
199 bool isStrict = false;
200 bool isArrowFunction = false;
201 bool isGenerator = false;
202 bool usesThis = false;
205 bool returnsClosure = false;
206 mutable bool argumentsCanEscape = false;
208 bool isWithBlock = false;
209 bool isCatchBlock = false;
212
218
220
222
223 template <typename T>
224 class SmallSet: public QVarLengthArray<T, 8>
225 {
226 public:
227 void insert(int value)
228 {
229 for (auto it : *this) {
230 if (it == value)
231 return;
232 }
233 this->append(value);
234 }
235 };
236
237 // Map from meta property index (existence implies dependency) to notify signal index
239 {
242
245
246 quint32 key() const { return _key; }
247 quint32 value() const { return _value; }
248 };
249
250 class PropertyDependencyMap: public QVarLengthArray<KeyValuePair, 8>
251 {
252 public:
254 {
255 for (auto it = begin(), eit = end(); it != eit; ++it) {
256 if (it->_key == key) {
257 it->_value = value;
258 return;
259 }
260 }
262 }
263 };
264
266 : parent(parent)
268 {
269 if (parent && parent->isStrict)
270 isStrict = true;
271 }
272
273 bool hasArgument(const QString &name) const
274 {
275 return arguments.contains(name);
276 }
277
278 int findArgument(const QString &name, bool *isInjected) const
279 {
280 // search backwards to handle duplicate argument names correctly
281 for (int i = arguments.size() - 1; i >= 0; --i) {
282 const auto &arg = arguments.at(i);
283 if (arg.id == name) {
284 *isInjected = arg.isInjected();
285 return i;
286 }
287 }
288 return -1;
289 }
290
292 {
293 MemberMap::const_iterator it = members.find(name);
294 if (it == members.end())
295 return Member();
296 Q_ASSERT(it->index != -1 || !parent);
297 return (*it);
298 }
299
300 bool memberInfo(const QString &name, const Member **m) const
301 {
302 Q_ASSERT(m);
303 MemberMap::const_iterator it = members.find(name);
304 if (it == members.end()) {
305 *m = nullptr;
306 return false;
307 }
308 *m = &(*it);
309 return true;
310 }
311
317
321
322 bool addLocalVar(
324 QQmlJS::AST::FunctionExpression *function = nullptr,
325 const QQmlJS::SourceLocation &declarationLocation = QQmlJS::SourceLocation(),
326 bool isInjected = false);
327
347 ResolvedName resolveName(const QString &name, const QQmlJS::SourceLocation &accessLocation);
348 void emitBlockHeader(Compiler::Codegen *codegen);
349 void emitBlockFooter(Compiler::Codegen *codegen);
350
351 void setupFunctionIndices(Moth::BytecodeGenerator *bytecodeGenerator);
352
353 bool canHaveTailCalls() const
354 {
355 if (!isStrict)
356 return false;
358 return !isGenerator;
360 return parent->canHaveTailCalls();
361 return false;
362 }
363
364 bool isCaseBlock() const
365 {
366 return contextType == ContextType::Block && name == u"%CaseBlock";
367 }
368};
369
370
371} } // namespace QV4::Compiler
372
374
375#endif // QV4CODEGEN_P_H
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatetime.h:283
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
iterator find(const Key &key)
Definition qmap.h:641
iterator end()
Definition qmap.h:602
iterator insert(const T &value)
Definition qset.h:155
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
const GLfloat * m
GLuint64 key
GLuint index
[2]
GLsizei const GLchar ** strings
[1]
GLenum type
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int void * arg
unsigned int quint32
Definition qtypes.h:50
unsigned int uint
Definition qtypes.h:34
QSharedPointer< T > other(t)
[5]
bool contains(const QString &name) const
QVector< Method > staticMethods
QVector< Method > methods
KeyValuePair(quint32 key, quint32 value)
bool requiresTDZCheck(const QQmlJS::SourceLocation &accessLocation, bool accessAcrossContextBoundaries) const
QQmlJS::SourceLocation declarationLocation
QQmlJS::AST::VariableScope scope
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
QVector< Context * > nestedContexts
std::unique_ptr< SourceLocationTable > sourceLocationTable
QVector< ImportEntry > importEntries
UsesArgumentsObject usesArgumentsObject
QVector< ExportEntry > exportEntries
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
QVector< CompiledData::CodeOffsetToLineAndStatement > lineAndStatementNumberMapping
void addUsedVariable(const QString &name)
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
QVector< ImportEntry > importEntries
Context * newContext(QQmlJS::AST::Node *node, Context *parent, ContextType compilationMode)
QVector< ExportEntry > localExportEntries
QHash< QQmlJS::AST::Node *, Context * > contextMap
QList< Context * > functions
QVector< TemplateObject > templateObjects
QVector< ExportEntry > starExportEntries
QVector< ExportEntry > indirectExportEntries
bool operator==(const TemplateObject &other)
Definition moc.h:23