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
qqmljscodegenerator_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef QQMLJSCODEGENERATOR_P_H
5#define QQMLJSCODEGENERATOR_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16
17#include <private/qqmljscompiler_p.h>
18#include <private/qqmljstypepropagator_p.h>
19#include <private/qqmljstyperesolver_p.h>
20
21#include <private/qv4bytecodehandler_p.h>
22#include <private/qv4codegen_p.h>
23
24#include <QtCore/qstring.h>
25
26#include <memory>
27
29
30class Q_QMLCOMPILER_EXPORT QQmlJSCodeGenerator : public QQmlJSCompilePass
31{
32public:
33 QQmlJSCodeGenerator(const QV4::Compiler::Context *compilerContext,
34 const QV4::Compiler::JSUnitGenerator *unitGenerator,
35 const QQmlJSTypeResolver *typeResolver, QQmlJSLogger *logger,
36 BasicBlocks basicBlocks, InstructionAnnotations annotations);
38
40 bool basicBlocksValidationFailed);
41
42protected:
48
49 // This is an RAII helper we can use to automatically convert the result of "inflexible"
50 // operations to the desired type. For example GetLookup can only retrieve the type of
51 // the property we're looking up. If we want to store a different type, we need to convert.
52 struct Q_QMLCOMPILER_EXPORT AccumulatorConverter
53 {
57
58 private:
59 const QQmlJSRegisterContent accumulatorOut;
60 const QString accumulatorVariableIn;
61 const QString accumulatorVariableOut;
63 };
64
65 virtual QString metaObject(const QQmlJSScope::ConstPtr &objectType);
66 virtual QString metaType(const QQmlJSScope::ConstPtr &type);
67
68 void generate_Ret() override;
69 void generate_Debug() override;
70 void generate_LoadConst(int index) override;
71 void generate_LoadZero() override;
72 void generate_LoadTrue() override;
73 void generate_LoadFalse() override;
74 void generate_LoadNull() override;
75 void generate_LoadUndefined() override;
76 void generate_LoadInt(int value) override;
77 void generate_MoveConst(int constIndex, int destTemp) override;
78 void generate_LoadReg(int reg) override;
79 void generate_StoreReg(int reg) override;
80 void generate_MoveReg(int srcReg, int destReg) override;
81 void generate_LoadImport(int index) override;
82 void generate_LoadLocal(int index) override;
83 void generate_StoreLocal(int index) override;
84 void generate_LoadScopedLocal(int scope, int index) override;
85 void generate_StoreScopedLocal(int scope, int index) override;
86 void generate_LoadRuntimeString(int stringId) override;
87 void generate_MoveRegExp(int regExpId, int destReg) override;
88 void generate_LoadClosure(int value) override;
89 void generate_LoadName(int nameIndex) override;
90 void generate_LoadGlobalLookup(int index) override;
91 void generate_LoadQmlContextPropertyLookup(int index) override;
92 void generate_StoreNameSloppy(int nameIndex) override;
93 void generate_StoreNameStrict(int name) override;
94 void generate_LoadElement(int base) override;
95 void generate_StoreElement(int base, int index) override;
96 void generate_LoadProperty(int nameIndex) override;
97 void generate_LoadOptionalProperty(int name, int offset) override;
98 void generate_GetLookup(int index) override;
99 void generate_GetOptionalLookup(int index, int offset) override;
100 void generate_StoreProperty(int name, int baseReg) override;
101 void generate_SetLookup(int index, int base) override;
102 void generate_LoadSuperProperty(int property) override;
103 void generate_StoreSuperProperty(int property) override;
104 void generate_Yield() override;
105 void generate_YieldStar() override;
106 void generate_Resume(int) override;
107
108 void generate_CallValue(int name, int argc, int argv) override;
109 void generate_CallWithReceiver(int name, int thisObject, int argc, int argv) override;
110 void generate_CallProperty(int name, int base, int argc, int argv) override;
111 void generate_CallPropertyLookup(int lookupIndex, int base, int argc, int argv) override;
112 void generate_CallName(int name, int argc, int argv) override;
113 void generate_CallPossiblyDirectEval(int argc, int argv) override;
114 void generate_CallGlobalLookup(int index, int argc, int argv) override;
115 void generate_CallQmlContextPropertyLookup(int index, int argc, int argv) override;
116 void generate_CallWithSpread(int func, int thisObject, int argc, int argv) override;
117 void generate_TailCall(int func, int thisObject, int argc, int argv) override;
118 void generate_Construct(int func, int argc, int argv) override;
119 void generate_ConstructWithSpread(int func, int argc, int argv) override;
120 void generate_SetUnwindHandler(int offset) override;
121 void generate_UnwindDispatch() override;
122 void generate_UnwindToLabel(int level, int offset) override;
123 void generate_DeadTemporalZoneCheck(int name) override;
124 void generate_ThrowException() override;
125 void generate_GetException() override;
126 void generate_SetException() override;
127 void generate_CreateCallContext() override;
128 void generate_PushCatchContext(int index, int name) override;
129 void generate_PushWithContext() override;
130 void generate_PushBlockContext(int index) override;
131 void generate_CloneBlockContext() override;
132 void generate_PushScriptContext(int index) override;
133 void generate_PopScriptContext() override;
134 void generate_PopContext() override;
135 void generate_GetIterator(int iterator) override;
136 void generate_IteratorNext(int value, int offset) override;
137 void generate_IteratorNextForYieldStar(int iterator, int object, int offset) override;
138 void generate_IteratorClose() override;
139 void generate_DestructureRestElement() override;
140 void generate_DeleteProperty(int base, int index) override;
141 void generate_DeleteName(int name) override;
142 void generate_TypeofName(int name) override;
143 void generate_TypeofValue() override;
144 void generate_DeclareVar(int varName, int isDeletable) override;
145 void generate_DefineArray(int argc, int args) override;
146 void generate_DefineObjectLiteral(int internalClassId, int argc, int args) override;
147 void generate_CreateClass(int classIndex, int heritage, int computedNames) override;
148 void generate_CreateMappedArgumentsObject() override;
149 void generate_CreateUnmappedArgumentsObject() override;
150 void generate_CreateRestParameter(int argIndex) override;
151 void generate_ConvertThisToObject() override;
152 void generate_LoadSuperConstructor() override;
153 void generate_ToObject() override;
154 void generate_Jump(int offset) override;
155 void generate_JumpTrue(int offset) override;
156 void generate_JumpFalse(int offset) override;
157 void generate_JumpNoException(int offset) override;
158 void generate_JumpNotUndefined(int offset) override;
159 void generate_CheckException() override;
160 void generate_CmpEqNull() override;
161 void generate_CmpNeNull() override;
162 void generate_CmpEqInt(int lhs) override;
163 void generate_CmpNeInt(int lhs) override;
164 void generate_CmpEq(int lhs) override;
165 void generate_CmpNe(int lhs) override;
166 void generate_CmpGt(int lhs) override;
167 void generate_CmpGe(int lhs) override;
168 void generate_CmpLt(int lhs) override;
169 void generate_CmpLe(int lhs) override;
170 void generate_CmpStrictEqual(int lhs) override;
171 void generate_CmpStrictNotEqual(int lhs) override;
172 void generate_CmpIn(int lhs) override;
173 void generate_CmpInstanceOf(int lhs) override;
174 void generate_As(int lhs) override;
175 void generate_UNot() override;
176 void generate_UPlus() override;
177 void generate_UMinus() override;
178 void generate_UCompl() override;
179 void generate_Increment() override;
180 void generate_Decrement() override;
181 void generate_Add(int lhs) override;
182 void generate_BitAnd(int lhs) override;
183 void generate_BitOr(int lhs) override;
184 void generate_BitXor(int lhs) override;
185 void generate_UShr(int lhs) override;
186 void generate_Shr(int lhs) override;
187 void generate_Shl(int lhs) override;
188 void generate_BitAndConst(int rhs) override;
189 void generate_BitOrConst(int rhs) override;
190 void generate_BitXorConst(int rhs) override;
191 void generate_UShrConst(int rhs) override;
192 void generate_ShrConst(int value) override;
193 void generate_ShlConst(int rhs) override;
194 void generate_Exp(int lhs) override;
195 void generate_Mul(int lhs) override;
196 void generate_Div(int lhs) override;
197 void generate_Mod(int lhs) override;
198 void generate_Sub(int lhs) override;
199 void generate_InitializeBlockDeadTemporalZone(int firstReg, int count) override;
200 void generate_ThrowOnNullOrUndefined() override;
201 void generate_GetTemplateObject(int index) override;
202
203 Verdict startInstruction(QV4::Moth::Instr::Type) override;
204 void endInstruction(QV4::Moth::Instr::Type) override;
205
206 void addInclude(const QString &include)
207 {
208 Q_ASSERT(!include.isEmpty());
209 m_includes.append(include);
210 }
211
212 QString conversion(const QQmlJSRegisterContent &from,
213 const QQmlJSRegisterContent &to,
214 const QString &variable);
215
217 const QQmlJSRegisterContent &to,
218 const QString &variable)
219 {
220 const QQmlJSScope::ConstPtr contained = m_typeResolver->containedType(to);
221 if (m_typeResolver->equals(to.storedType(), contained)
222 || m_typeResolver->isNumeric(to.storedType())
223 || to.storedType()->isReferenceType()
224 || m_typeResolver->equals(from, contained)) {
225 // If:
226 // * the output is not actually wrapped at all, or
227 // * the output is a number (as there are no internals to a number)
228 // * the output is a QObject pointer, or
229 // * we merely wrap the value into a new container,
230 // we can convert by stored type.
231 return convertStored(from, to.storedType(), variable);
232 } else {
233 return convertContained(m_typeResolver->globalType(from), to, variable);
234 }
235 }
236
237 QString convertStored(const QQmlJSScope::ConstPtr &from,
238 const QQmlJSScope::ConstPtr &to,
239 const QString &variable);
240
241 QString convertContained(const QQmlJSRegisterContent &from,
242 const QQmlJSRegisterContent &to,
243 const QString &variable);
244
245 void generateReturnError();
246 void reject(const QString &thing);
247
248 QString metaTypeFromType(const QQmlJSScope::ConstPtr &type) const;
249 QString metaTypeFromName(const QQmlJSScope::ConstPtr &type) const;
250 QString compositeMetaType(const QString &elementName) const;
251 QString compositeListMetaType(const QString &elementName) const;
252
253 QString contentPointer(const QQmlJSRegisterContent &content, const QString &var);
254 QString contentType(const QQmlJSRegisterContent &content, const QString &var);
255
256 void generateSetInstructionPointer();
257 void generateLookup(const QString &lookup, const QString &initialization,
258 const QString &resultPreparation = QString());
259 QString getLookupPreparation(
260 const QQmlJSRegisterContent &content, const QString &var, int lookup);
261 QString setLookupPreparation(
262 const QQmlJSRegisterContent &content, const QString &arg, int lookup);
263 void generateEnumLookup(int index);
264
265 QString registerVariable(int index) const;
266 QString lookupVariable(int lookupIndex) const;
267 QString consumedRegisterVariable(int index) const;
268 QString consumedAccumulatorVariableIn() const;
269
270 QString changedRegisterVariable() const;
271 QQmlJSRegisterContent registerType(int index) const;
272 QQmlJSRegisterContent lookupType(int lookupIndex) const;
273 bool shouldMoveRegister(int index) const;
274
277
278 void resetState() { m_state = CodegenState(); }
279
280private:
281 void generateExceptionCheck();
282
283 void generateEqualityOperation(
284 const QQmlJSRegisterContent &lhsContent, const QString &lhsName,
285 const QString &function, bool invert) {
286 generateEqualityOperation(
287 lhsContent, m_state.accumulatorIn(), lhsName, m_state.accumulatorVariableIn,
288 function, invert);
289 }
290
291 void generateEqualityOperation(
292 const QQmlJSRegisterContent &lhsContent, const QQmlJSRegisterContent &rhsContent,
293 const QString &lhsName, const QString &rhsName, const QString &function, bool invert);
294 void generateCompareOperation(int lhs, const QString &cppOperator);
295 void generateArithmeticOperation(int lhs, const QString &cppOperator);
296 void generateShiftOperation(int lhs, const QString &cppOperator);
297 void generateArithmeticOperation(
298 const QString &lhs, const QString &rhs, const QString &cppOperator);
299 void generateArithmeticConstOperation(int lhsConst, const QString &cppOperator);
300 void generateJumpCodeWithTypeConversions(int relativeOffset);
301 void generateUnaryOperation(const QString &cppOperator);
302 void generateInPlaceOperation(const QString &cppOperator);
303 void generateMoveOutVar(const QString &outVar);
304 void generateTypeLookup(int index);
305 void generateVariantEqualityComparison(
306 const QQmlJSRegisterContent &nonStorable, const QString &registerName, bool invert);
307 void generateVariantEqualityComparison(
308 const QQmlJSRegisterContent &storableContent, const QString &typedRegisterName,
309 const QString &varRegisterName, bool invert);
310 void generateArrayInitializer(int argc, int argv);
311 void generateWriteBack(int registerIndex);
312 void rejectIfNonQObjectOut(const QString &error);
313 void rejectIfBadArray();
314
315
316 QString eqIntExpression(int lhsConst);
317 QString argumentsList(int argc, int argv, QString *outVar);
318 QString castTargetName(const QQmlJSScope::ConstPtr &type) const;
319
320 bool inlineStringMethod(const QString &name, int base, int argc, int argv);
321 bool inlineTranslateMethod(const QString &name, int argc, int argv);
322 bool inlineMathMethod(const QString &name, int argc, int argv);
323 bool inlineConsoleMethod(const QString &name, int argc, int argv);
324 bool inlineArrayMethod(const QString &name, int base, int argc, int argv);
325
326 void generate_GetLookupHelper(int index);
327
328 QString resolveValueTypeContentPointer(
329 const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual,
330 const QString &variable, const QString &errorMessage);
331 QString resolveQObjectPointer(
332 const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual,
333 const QString &variable, const QString &errorMessage);
334 bool generateContentPointerCheck(
335 const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual,
336 const QString &variable, const QString &errorMessage);
337
338 // map from instruction offset to sequential label number
339 QHash<int, QString> m_labels;
340
341 const QV4::Compiler::Context *m_context = nullptr;
342
343 bool m_skipUntilNextLabel = false;
344
345 QStringList m_includes;
346
347 struct RegisterVariablesKey
348 {
350 int registerIndex = -1;
352
353 private:
354 friend size_t qHash(const RegisterVariablesKey &key, size_t seed = 0) noexcept
355 {
356 return qHashMulti(seed, key.internalName, key.registerIndex, key.lookupIndex);
357 }
358
359 friend bool operator==(
360 const RegisterVariablesKey &lhs, const RegisterVariablesKey &rhs) noexcept
361 {
362 return lhs.registerIndex == rhs.registerIndex
363 && lhs.lookupIndex == rhs.lookupIndex
364 && lhs.internalName == rhs.internalName;
365 }
366
367 friend bool operator!=(
368 const RegisterVariablesKey &lhs, const RegisterVariablesKey &rhs) noexcept
369 {
370 return !(lhs == rhs);
371 }
372 };
373
374 struct RegisterVariablesValue
375 {
376 QString variableName;
377 QQmlJSScope::ConstPtr storedType;
378 int numTracked = 0;
379 };
380
381 QHash<RegisterVariablesKey, RegisterVariablesValue> m_registerVariables;
382};
383
385
386#endif // QQMLJSCODEGENERATOR_P_H
QString conversion(const QQmlJSScope::ConstPtr &from, const QQmlJSRegisterContent &to, const QString &variable)
void addInclude(const QString &include)
~QQmlJSCodeGenerator()=default
QQmlJSScope::ConstPtr storedType() const
bool isReferenceType() const
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
Combined button and popup list for selecting options.
constexpr bool operator!=(const timespec &t1, const timespec &t2)
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
constexpr QtPrivate::QHashMultiReturnType< T... > qHashMulti(size_t seed, const T &... args) noexcept(std::conjunction_v< QtPrivate::QNothrowHashable< T >... >)
GLenum GLuint GLint level
GLuint64 key
GLuint index
[2]
GLenum GLenum GLsizei count
GLenum type
GLenum GLuint GLintptr offset
GLuint name
GLenum func
Definition qopenglext.h:663
GLenum GLenum variable
GLboolean invert
Definition qopenglext.h:226
static QString internalName(const QQmlJSScope::ConstPtr &scope)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int void * arg
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3517
static const uint base
Definition qurlidna.cpp:20
const char property[13]
Definition qwizard.cpp:101
QRandomGenerator generator(sseq)
obj metaObject() -> className()
QJSValueList args