1// Copyright (C) 2019 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
4#include "qqmlirloader_p.h"
5#include <private/qqmlirbuilder_p.h>
6
7QT_BEGIN_NAMESPACE
8
9QQmlIRLoader::QQmlIRLoader(const QV4::CompiledData::Unit *qmlData, QmlIR::Document *output)
10 : unit(qmlData)
11 , output(output)
12{
13 pool = output->jsParserEngine.pool();
14}
15
16void QQmlIRLoader::load()
17{
18 output->jsGenerator.stringTable.initializeFromBackingUnit(unit);
19
20 const QV4::CompiledData::QmlUnit *qmlUnit = unit->qmlUnit();
21
22 for (quint32 i = 0; i < qmlUnit->nImports; ++i)
23 output->imports << qmlUnit->importAt(idx: i);
24
25 using QmlIR::Pragma;
26 const auto createPragma = [&](Pragma::PragmaType type) {
27 Pragma *p = New<Pragma>();
28 p->location = QV4::CompiledData::Location();
29 p->type = type;
30 output->pragmas << p;
31 return p;
32 };
33
34 const auto createListPragma = [&](
35 Pragma::PragmaType type,
36 Pragma::ListPropertyAssignBehaviorValue value) {
37 createPragma(type)->listPropertyAssignBehavior = value;
38 };
39
40 const auto createComponentPragma = [&](
41 Pragma::PragmaType type,
42 Pragma::ComponentBehaviorValue value) {
43 createPragma(type)->componentBehavior = value;
44 };
45
46 const auto createFunctionSignaturePragma = [&](
47 Pragma::PragmaType type,
48 Pragma::FunctionSignatureBehaviorValue value) {
49 createPragma(type)->functionSignatureBehavior = value;
50 };
51
52 const auto createNativeMethodPragma = [&](
53 Pragma::PragmaType type,
54 Pragma::NativeMethodBehaviorValue value) {
55 createPragma(type)->nativeMethodBehavior = value;
56 };
57
58 const auto createValueTypePragma = [&](
59 Pragma::PragmaType type,
60 Pragma::ValueTypeBehaviorValues value) {
61 createPragma(type)->valueTypeBehavior = value;
62 };
63
64 if (unit->flags & QV4::CompiledData::Unit::IsSingleton)
65 createPragma(Pragma::Singleton);
66 if (unit->flags & QV4::CompiledData::Unit::IsStrict)
67 createPragma(Pragma::Strict);
68
69 if (unit->flags & QV4::CompiledData::Unit::ListPropertyAssignReplace)
70 createListPragma(Pragma::ListPropertyAssignBehavior, Pragma::Replace);
71 else if (unit->flags & QV4::CompiledData::Unit::ListPropertyAssignReplaceIfNotDefault)
72 createListPragma(Pragma::ListPropertyAssignBehavior, Pragma::ReplaceIfNotDefault);
73
74 if (unit->flags & QV4::CompiledData::Unit::ComponentsBound)
75 createComponentPragma(Pragma::ComponentBehavior, Pragma::Bound);
76
77 if (unit->flags & QV4::CompiledData::Unit::FunctionSignaturesEnforced)
78 createFunctionSignaturePragma(Pragma::FunctionSignatureBehavior, Pragma::Enforced);
79
80 if (unit->flags & QV4::CompiledData::Unit::NativeMethodsAcceptThisObject)
81 createNativeMethodPragma(Pragma::NativeMethodBehavior, Pragma::AcceptThisObject);
82
83 Pragma::ValueTypeBehaviorValues valueTypeBehavior = {};
84 if (unit->flags & QV4::CompiledData::Unit::ValueTypesCopied)
85 valueTypeBehavior |= Pragma::Copy;
86 if (unit->flags & QV4::CompiledData::Unit::ValueTypesAddressable)
87 valueTypeBehavior |= Pragma::Addressable;
88 if (valueTypeBehavior)
89 createValueTypePragma(Pragma::ValueTypeBehavior, valueTypeBehavior);
90
91 for (uint i = 0; i < qmlUnit->nObjects; ++i) {
92 const QV4::CompiledData::Object *serializedObject = qmlUnit->objectAt(idx: i);
93 QmlIR::Object *object = loadObject(serializedObject);
94 output->objects.append(t: object);
95 }
96}
97
98struct FakeExpression : public QQmlJS::AST::NullExpression
99{
100 FakeExpression(int start, int length)
101 : location(start, length)
102 {}
103
104 QQmlJS::SourceLocation firstSourceLocation() const override
105 { return location; }
106
107 QQmlJS::SourceLocation lastSourceLocation() const override
108 { return location; }
109
110private:
111 QQmlJS::SourceLocation location;
112};
113
114QmlIR::Object *QQmlIRLoader::loadObject(const QV4::CompiledData::Object *serializedObject)
115{
116 QmlIR::Object *object = pool->New<QmlIR::Object>();
117 object->init(pool, typeNameIndex: serializedObject->inheritedTypeNameIndex, idIndex: serializedObject->idNameIndex,
118 location: serializedObject->location);
119
120 object->indexOfDefaultPropertyOrAlias = serializedObject->indexOfDefaultPropertyOrAlias;
121 object->defaultPropertyIsAlias = serializedObject->hasAliasAsDefaultProperty();
122 object->flags = serializedObject->flags();
123 object->id = serializedObject->objectId();
124 object->locationOfIdProperty = serializedObject->locationOfIdProperty;
125
126 QVector<int> functionIndices;
127 functionIndices.reserve(asize: serializedObject->nFunctions + serializedObject->nBindings / 2);
128
129 for (uint i = 0; i < serializedObject->nBindings; ++i) {
130 QmlIR::Binding *b = pool->New<QmlIR::Binding>();
131 *static_cast<QV4::CompiledData::Binding*>(b) = serializedObject->bindingTable()[i];
132 object->bindings->append(item: b);
133 if (b->type() == QV4::CompiledData::Binding::Type_Script) {
134 functionIndices.append(t: b->value.compiledScriptIndex);
135 b->value.compiledScriptIndex = functionIndices.size() - 1;
136
137 QmlIR::CompiledFunctionOrExpression *foe = pool->New<QmlIR::CompiledFunctionOrExpression>();
138 foe->nameIndex = 0;
139
140 QQmlJS::AST::ExpressionNode *expr;
141
142 if (b->stringIndex != quint32(0)) {
143 const int start = output->code.size();
144 const QString script = output->stringAt(index: b->stringIndex);
145 const int length = script.size();
146 output->code.append(s: script);
147 expr = new (pool) FakeExpression(start, length);
148 } else
149 expr = new (pool) QQmlJS::AST::NullExpression();
150 foe->node = new (pool) QQmlJS::AST::ExpressionStatement(expr); // dummy
151 object->functionsAndExpressions->append(item: foe);
152 }
153 }
154
155 Q_ASSERT(object->functionsAndExpressions->count == functionIndices.size());
156
157 for (uint i = 0; i < serializedObject->nSignals; ++i) {
158 const QV4::CompiledData::Signal *serializedSignal = serializedObject->signalAt(idx: i);
159 QmlIR::Signal *s = pool->New<QmlIR::Signal>();
160 s->nameIndex = serializedSignal->nameIndex;
161 s->location = serializedSignal->location;
162 s->parameters = pool->New<QmlIR::PoolList<QmlIR::Parameter> >();
163
164 for (uint i = 0; i < serializedSignal->nParameters; ++i) {
165 QmlIR::Parameter *p = pool->New<QmlIR::Parameter>();
166 *static_cast<QV4::CompiledData::Parameter*>(p) = *serializedSignal->parameterAt(idx: i);
167 s->parameters->append(item: p);
168 }
169
170 object->qmlSignals->append(item: s);
171 }
172
173 for (uint i = 0; i < serializedObject->nEnums; ++i) {
174 const QV4::CompiledData::Enum *serializedEnum = serializedObject->enumAt(idx: i);
175 QmlIR::Enum *e = pool->New<QmlIR::Enum>();
176 e->nameIndex = serializedEnum->nameIndex;
177 e->location = serializedEnum->location;
178 e->enumValues = pool->New<QmlIR::PoolList<QmlIR::EnumValue> >();
179
180 for (uint i = 0; i < serializedEnum->nEnumValues; ++i) {
181 QmlIR::EnumValue *v = pool->New<QmlIR::EnumValue>();
182 *static_cast<QV4::CompiledData::EnumValue*>(v) = *serializedEnum->enumValueAt(idx: i);
183 e->enumValues->append(item: v);
184 }
185
186 object->qmlEnums->append(item: e);
187 }
188
189 const QV4::CompiledData::Property *serializedProperty = serializedObject->propertyTable();
190 for (uint i = 0; i < serializedObject->nProperties; ++i, ++serializedProperty) {
191 QmlIR::Property *p = pool->New<QmlIR::Property>();
192 *static_cast<QV4::CompiledData::Property*>(p) = *serializedProperty;
193 object->properties->append(item: p);
194 }
195
196 {
197 const QV4::CompiledData::Alias *serializedAlias = serializedObject->aliasTable();
198 for (uint i = 0; i < serializedObject->nAliases; ++i, ++serializedAlias) {
199 QmlIR::Alias *a = pool->New<QmlIR::Alias>();
200 *static_cast<QV4::CompiledData::Alias*>(a) = *serializedAlias;
201 object->aliases->append(item: a);
202 }
203 }
204
205 const quint32_le *functionIdx = serializedObject->functionOffsetTable();
206 for (uint i = 0; i < serializedObject->nFunctions; ++i, ++functionIdx) {
207 QmlIR::Function *f = pool->New<QmlIR::Function>();
208 const QV4::CompiledData::Function *compiledFunction = unit->functionAt(idx: *functionIdx);
209
210 functionIndices.append(t: *functionIdx);
211 f->index = functionIndices.size() - 1;
212 f->location = compiledFunction->location;
213 f->nameIndex = compiledFunction->nameIndex;
214 f->returnType = compiledFunction->returnType;
215
216 f->formals.allocate(pool, size: int(compiledFunction->nFormals));
217 const QV4::CompiledData::Parameter *formalNameIdx = compiledFunction->formalsTable();
218 for (uint i = 0; i < compiledFunction->nFormals; ++i, ++formalNameIdx)
219 *static_cast<QV4::CompiledData::Parameter*>(&f->formals[i]) = *formalNameIdx;
220
221 object->functions->append(item: f);
222 }
223
224 object->runtimeFunctionIndices.allocate(pool, vector: functionIndices);
225
226 const QV4::CompiledData::InlineComponent *serializedInlineComponent = serializedObject->inlineComponentTable();
227 for (uint i = 0; i < serializedObject->nInlineComponents; ++i, ++serializedInlineComponent) {
228 QmlIR::InlineComponent *ic = pool->New<QmlIR::InlineComponent>();
229 *static_cast<QV4::CompiledData::InlineComponent*>(ic) = *serializedInlineComponent;
230 object->inlineComponents->append(item: ic);
231 }
232
233 const QV4::CompiledData::RequiredPropertyExtraData *serializedRequiredPropertyExtraData = serializedObject->requiredPropertyExtraDataTable();
234 for (uint i = 0u; i < serializedObject->nRequiredPropertyExtraData; ++i, ++serializedRequiredPropertyExtraData) {
235 QmlIR::RequiredPropertyExtraData *extra = pool->New<QmlIR::RequiredPropertyExtraData>();
236 *static_cast<QV4::CompiledData::RequiredPropertyExtraData *>(extra) = *serializedRequiredPropertyExtraData;
237 object->requiredPropertyExtraDatas->append(item: extra);
238 }
239
240 return object;
241}
242
243QT_END_NAMESPACE
244

source code of qtdeclarative/src/qml/qml/qqmlirloader.cpp