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 QQMLJSTYPEPROPAGATOR_P_H
5#define QQMLJSTYPEPROPAGATOR_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/qqmljsast_p.h>
18#include <private/qqmljsscope_p.h>
19#include <private/qqmljscompilepass_p.h>
20
21QT_BEGIN_NAMESPACE
22
23namespace QQmlSA {
24class PassManager;
25};
26
27struct Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSTypePropagator : public QQmlJSCompilePass
28{
29 QQmlJSTypePropagator(const QV4::Compiler::JSUnitGenerator *unitGenerator,
30 const QQmlJSTypeResolver *typeResolver, QQmlJSLogger *logger,
31 QQmlSA::PassManager *passManager = nullptr);
32
33 InstructionAnnotations run(const Function *m_function, QQmlJS::DiagnosticMessage *error);
34
35 void generate_Ret() override;
36 void generate_Debug() override;
37 void generate_LoadConst(int index) override;
38 void generate_LoadZero() override;
39 void generate_LoadTrue() override;
40 void generate_LoadFalse() override;
41 void generate_LoadNull() override;
42 void generate_LoadUndefined() override;
43 void generate_LoadInt(int value) override;
44 void generate_MoveConst(int constIndex, int destTemp) override;
45 void generate_LoadReg(int reg) override;
46 void generate_StoreReg(int reg) override;
47 void generate_MoveReg(int srcReg, int destReg) override;
48 void generate_LoadImport(int index) override;
49 void generate_LoadLocal(int index) override;
50 void generate_StoreLocal(int index) override;
51 void generate_LoadScopedLocal(int scope, int index) override;
52 void generate_StoreScopedLocal(int scope, int index) override;
53 void generate_LoadRuntimeString(int stringId) override;
54 void generate_MoveRegExp(int regExpId, int destReg) override;
55 void generate_LoadClosure(int value) override;
56 void generate_LoadName(int nameIndex) override;
57 void generate_LoadGlobalLookup(int index) override;
58 void generate_LoadQmlContextPropertyLookup(int index) override;
59 void generate_StoreNameCommon(int nameIndex);
60 void generate_StoreNameSloppy(int nameIndex) override;
61 void generate_StoreNameStrict(int name) override;
62 void generate_LoadElement(int base) override;
63 void generate_StoreElement(int base, int index) override;
64 void generate_LoadProperty(int nameIndex) override;
65 void generate_LoadOptionalProperty(int name, int offset) override;
66 void generate_GetLookup(int index) override;
67 void generate_GetOptionalLookup(int index, int offset) override;
68 void generate_StoreProperty(int name, int base) override;
69 void generate_SetLookup(int index, int base) override;
70 void generate_LoadSuperProperty(int property) override;
71 void generate_StoreSuperProperty(int property) override;
72 void generate_Yield() override;
73 void generate_YieldStar() override;
74 void generate_Resume(int) override;
75
76 void generate_CallValue(int name, int argc, int argv) override;
77 void generate_CallWithReceiver(int name, int thisObject, int argc, int argv) override;
78 void generate_CallProperty(int name, int base, int argc, int argv) override;
79 void generate_CallPropertyLookup(int lookupIndex, int base, int argc, int argv) override;
80 void generate_CallName(int name, int argc, int argv) override;
81 void generate_CallPossiblyDirectEval(int argc, int argv) override;
82 void generate_CallGlobalLookup(int index, int argc, int argv) override;
83 void generate_CallQmlContextPropertyLookup(int index, int argc, int argv) override;
84 void generate_CallWithSpread(int func, int thisObject, int argc, int argv) override;
85 void generate_TailCall(int func, int thisObject, int argc, int argv) override;
86 void generate_Construct(int func, int argc, int argv) override;
87 void generate_ConstructWithSpread(int func, int argc, int argv) override;
88 void generate_SetUnwindHandler(int offset) override;
89 void generate_UnwindDispatch() override;
90 void generate_UnwindToLabel(int level, int offset) override;
91 void generate_DeadTemporalZoneCheck(int name) override;
92 void generate_ThrowException() override;
93 void generate_GetException() override;
94 void generate_SetException() override;
95 void generate_CreateCallContext() override;
96 void generate_PushCatchContext(int index, int name) override;
97 void generate_PushWithContext() override;
98 void generate_PushBlockContext(int index) override;
99 void generate_CloneBlockContext() override;
100 void generate_PushScriptContext(int index) override;
101 void generate_PopScriptContext() override;
102 void generate_PopContext() override;
103 void generate_GetIterator(int iterator) override;
104 void generate_IteratorNext(int value, int done) override;
105 void generate_IteratorNextForYieldStar(int iterator, int object) override;
106 void generate_IteratorClose(int done) override;
107 void generate_DestructureRestElement() override;
108 void generate_DeleteProperty(int base, int index) override;
109 void generate_DeleteName(int name) override;
110 void generate_TypeofName(int name) override;
111 void generate_TypeofValue() override;
112 void generate_DeclareVar(int varName, int isDeletable) override;
113 void generate_DefineArray(int argc, int args) override;
114 void generate_DefineObjectLiteral(int internalClassId, int argc, int args) override;
115 void generate_CreateClass(int classIndex, int heritage, int computedNames) override;
116 void generate_CreateMappedArgumentsObject() override;
117 void generate_CreateUnmappedArgumentsObject() override;
118 void generate_CreateRestParameter(int argIndex) override;
119 void generate_ConvertThisToObject() override;
120 void generate_LoadSuperConstructor() override;
121 void generate_ToObject() override;
122 void generate_Jump(int offset) override;
123 void generate_JumpTrue(int offset) override;
124 void generate_JumpFalse(int offset) override;
125 void generate_JumpNoException(int offset) override;
126 void generate_JumpNotUndefined(int offset) override;
127 void generate_CheckException() override;
128 void generate_CmpEqNull() override;
129 void generate_CmpNeNull() override;
130 void generate_CmpEqInt(int lhsConst) override;
131 void generate_CmpNeInt(int lhs) override;
132 void generate_CmpEq(int lhs) override;
133 void generate_CmpNe(int lhs) override;
134 void generate_CmpGt(int lhs) override;
135 void generate_CmpGe(int lhs) override;
136 void generate_CmpLt(int lhs) override;
137 void generate_CmpLe(int lhs) override;
138 void generate_CmpStrictEqual(int lhs) override;
139 void generate_CmpStrictNotEqual(int lhs) override;
140 void generate_CmpIn(int lhs) override;
141 void generate_CmpInstanceOf(int lhs) override;
142 void generate_As(int lhs) override;
143 void generate_UNot() override;
144 void generate_UPlus() override;
145 void generate_UMinus() override;
146 void generate_UCompl() override;
147 void generate_Increment() override;
148 void generate_Decrement() override;
149 void generate_Add(int lhs) override;
150 void generate_BitAnd(int lhs) override;
151 void generate_BitOr(int lhs) override;
152 void generate_BitXor(int lhs) override;
153 void generate_UShr(int lhs) override;
154 void generate_Shr(int lhs) override;
155 void generate_Shl(int lhs) override;
156 void generate_BitAndConst(int rhsConst) override;
157 void generate_BitOrConst(int rhsConst) override;
158 void generate_BitXorConst(int rhsConst) override;
159 void generate_UShrConst(int rhsConst) override;
160 void generate_ShrConst(int rhs) override;
161 void generate_ShlConst(int rhs) override;
162 void generate_Exp(int lhs) override;
163 void generate_Mul(int lhs) override;
164 void generate_Div(int lhs) override;
165 void generate_Mod(int lhs) override;
166 void generate_Sub(int lhs) override;
167 void generate_InitializeBlockDeadTemporalZone(int firstReg, int count) override;
168 void generate_ThrowOnNullOrUndefined() override;
169 void generate_GetTemplateObject(int index) override;
170
171 bool checkForEnumProblems(const QQmlJSRegisterContent &base, const QString &propertyName) const;
172
173 Verdict startInstruction(QV4::Moth::Instr::Type instr) override;
174 void endInstruction(QV4::Moth::Instr::Type instr) override;
175
176private:
177 struct ExpectedRegisterState
178 {
179 int originatingOffset = 0;
180 VirtualRegisters registers;
181 };
182
183 struct PassState : QQmlJSCompilePass::State
184 {
185 InstructionAnnotations annotations;
186 QSet<int> jumpTargets;
187 bool skipInstructionsUntilNextJumpTarget = false;
188 bool needsMorePasses = false;
189 };
190
191 void handleUnqualifiedAccess(const QString &name, bool isMethod) const;
192 void checkDeprecated(QQmlJSScope::ConstPtr scope, const QString &name, bool isMethod) const;
193 bool isCallingProperty(QQmlJSScope::ConstPtr scope, const QString &name) const;
194
195 enum PropertyResolution {
196 PropertyMissing,
197 PropertyTypeUnresolved,
198 PropertyFullyResolved
199 };
200
201 PropertyResolution propertyResolution(QQmlJSScope::ConstPtr scope, const QString &type) const;
202 QQmlJS::SourceLocation getCurrentSourceLocation() const;
203 QQmlJS::SourceLocation getCurrentBindingSourceLocation() const;
204
205 void checkConversion(const QQmlJSRegisterContent &from, const QQmlJSRegisterContent &to);
206 void generateUnaryArithmeticOperation(QQmlJSTypeResolver::UnaryOperator op);
207
208 QQmlJSRegisterContent propagateBinaryOperation(QSOperator::Op op, int lhs);
209 void generateBinaryArithmeticOperation(QSOperator::Op op, int lhs);
210 void generateBinaryConstArithmeticOperation(QSOperator::Op op);
211
212 void propagateCall(
213 const QList<QQmlJSMetaMethod> &methods, int argc, int argv,
214 const QQmlJSScope::ConstPtr &scope);
215 bool propagateTranslationMethod(const QList<QQmlJSMetaMethod> &methods, int argc, int argv);
216 void propagateStringArgCall(int argv);
217 bool propagateArrayMethod(const QString &name, int argc, int argv, const QQmlJSRegisterContent &valueType);
218 void propagatePropertyLookup(const QString &name);
219 void propagateScopeLookupCall(const QString &functionName, int argc, int argv);
220 void saveRegisterStateForJump(int offset);
221 bool canConvertFromTo(const QQmlJSRegisterContent &from, const QQmlJSRegisterContent &to);
222
223 QString registerName(int registerIndex) const;
224
225 QQmlJSRegisterContent checkedInputRegister(int reg);
226 QQmlJSMetaMethod bestMatchForCall(const QList<QQmlJSMetaMethod> &methods, int argc, int argv,
227 QStringList *errors);
228
229 void setAccumulator(const QQmlJSRegisterContent &content);
230 void setRegister(int index, const QQmlJSRegisterContent &content);
231 void mergeRegister(int index, const QQmlJSRegisterContent &a, const QQmlJSRegisterContent &b);
232
233 void addReadRegister(int index, const QQmlJSRegisterContent &convertTo);
234 void addReadAccumulator(const QQmlJSRegisterContent &convertTo)
235 {
236 addReadRegister(index: Accumulator, convertTo);
237 }
238
239 void recordEqualsNullType();
240 void recordEqualsIntType();
241 void recordEqualsType(int lhs);
242 void recordCompareType(int lhs);
243
244 QQmlJSRegisterContent m_returnType;
245 QQmlSA::PassManager *m_passManager = nullptr;
246 QQmlJSScope::ConstPtr m_attachedContext;
247
248 // Not part of the state, as the back jumps are the reason for running multiple passes
249 QMultiHash<int, ExpectedRegisterState> m_jumpOriginRegisterStateByTargetInstructionOffset;
250
251 InstructionAnnotations m_prevStateAnnotations;
252 PassState m_state;
253};
254
255QT_END_NAMESPACE
256
257#endif // QQMLJSTYPEPROPAGATOR_P_H
258

source code of qtdeclarative/src/qmlcompiler/qqmljstypepropagator_p.h