1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqmlirloader_p.h"
41#include <private/qqmlirbuilder_p.h>
42
43QT_BEGIN_NAMESPACE
44
45QQmlIRLoader::QQmlIRLoader(const QV4::CompiledData::Unit *qmlData, QmlIR::Document *output)
46 : unit(qmlData)
47 , output(output)
48{
49 pool = output->jsParserEngine.pool();
50}
51
52void QQmlIRLoader::load()
53{
54 output->jsGenerator.stringTable.initializeFromBackingUnit(unit);
55
56 const QV4::CompiledData::QmlUnit *qmlUnit = unit->qmlUnit();
57
58 for (quint32 i = 0; i < qmlUnit->nImports; ++i)
59 output->imports << qmlUnit->importAt(idx: i);
60
61 if (unit->flags & QV4::CompiledData::Unit::IsSingleton) {
62 QmlIR::Pragma *p = New<QmlIR::Pragma>();
63 p->location = QV4::CompiledData::Location();
64 p->type = QmlIR::Pragma::PragmaSingleton;
65 output->pragmas << p;
66 }
67
68 for (uint i = 0; i < qmlUnit->nObjects; ++i) {
69 const QV4::CompiledData::Object *serializedObject = qmlUnit->objectAt(idx: i);
70 QmlIR::Object *object = loadObject(serializedObject);
71 output->objects.append(t: object);
72 }
73}
74
75struct FakeExpression : public QQmlJS::AST::NullExpression
76{
77 FakeExpression(int start, int length)
78 : location(start, length)
79 {}
80
81 virtual QQmlJS::SourceLocation firstSourceLocation() const
82 { return location; }
83
84 virtual QQmlJS::SourceLocation lastSourceLocation() const
85 { return location; }
86
87private:
88 QQmlJS::SourceLocation location;
89};
90
91QmlIR::Object *QQmlIRLoader::loadObject(const QV4::CompiledData::Object *serializedObject)
92{
93 QmlIR::Object *object = pool->New<QmlIR::Object>();
94 object->init(pool, typeNameIndex: serializedObject->inheritedTypeNameIndex, idIndex: serializedObject->idNameIndex);
95
96 object->indexOfDefaultPropertyOrAlias = serializedObject->indexOfDefaultPropertyOrAlias;
97 object->defaultPropertyIsAlias = serializedObject->defaultPropertyIsAlias;
98 object->isInlineComponent = serializedObject->flags & QV4::CompiledData::Object::IsInlineComponentRoot;
99 object->flags = serializedObject->flags;
100 object->id = serializedObject->id;
101 object->location = serializedObject->location;
102 object->locationOfIdProperty = serializedObject->locationOfIdProperty;
103
104 QVector<int> functionIndices;
105 functionIndices.reserve(asize: serializedObject->nFunctions + serializedObject->nBindings / 2);
106
107 for (uint i = 0; i < serializedObject->nBindings; ++i) {
108 QmlIR::Binding *b = pool->New<QmlIR::Binding>();
109 *static_cast<QV4::CompiledData::Binding*>(b) = serializedObject->bindingTable()[i];
110 object->bindings->append(item: b);
111 if (b->type == QV4::CompiledData::Binding::Type_Script) {
112 functionIndices.append(t: b->value.compiledScriptIndex);
113 b->value.compiledScriptIndex = functionIndices.count() - 1;
114
115 QmlIR::CompiledFunctionOrExpression *foe = pool->New<QmlIR::CompiledFunctionOrExpression>();
116 foe->nameIndex = 0;
117
118 QQmlJS::AST::ExpressionNode *expr;
119
120 if (b->stringIndex != quint32(0)) {
121 const int start = output->code.length();
122 const QString script = output->stringAt(index: b->stringIndex);
123 const int length = script.length();
124 output->code.append(s: script);
125 expr = new (pool) FakeExpression(start, length);
126 } else
127 expr = new (pool) QQmlJS::AST::NullExpression();
128 foe->node = new (pool) QQmlJS::AST::ExpressionStatement(expr); // dummy
129 object->functionsAndExpressions->append(item: foe);
130 }
131 }
132
133 Q_ASSERT(object->functionsAndExpressions->count == functionIndices.count());
134
135 for (uint i = 0; i < serializedObject->nSignals; ++i) {
136 const QV4::CompiledData::Signal *serializedSignal = serializedObject->signalAt(idx: i);
137 QmlIR::Signal *s = pool->New<QmlIR::Signal>();
138 s->nameIndex = serializedSignal->nameIndex;
139 s->location = serializedSignal->location;
140 s->parameters = pool->New<QmlIR::PoolList<QmlIR::Parameter> >();
141
142 for (uint i = 0; i < serializedSignal->nParameters; ++i) {
143 QmlIR::Parameter *p = pool->New<QmlIR::Parameter>();
144 *static_cast<QV4::CompiledData::Parameter*>(p) = *serializedSignal->parameterAt(idx: i);
145 s->parameters->append(item: p);
146 }
147
148 object->qmlSignals->append(item: s);
149 }
150
151 for (uint i = 0; i < serializedObject->nEnums; ++i) {
152 const QV4::CompiledData::Enum *serializedEnum = serializedObject->enumAt(idx: i);
153 QmlIR::Enum *e = pool->New<QmlIR::Enum>();
154 e->nameIndex = serializedEnum->nameIndex;
155 e->location = serializedEnum->location;
156 e->enumValues = pool->New<QmlIR::PoolList<QmlIR::EnumValue> >();
157
158 for (uint i = 0; i < serializedEnum->nEnumValues; ++i) {
159 QmlIR::EnumValue *v = pool->New<QmlIR::EnumValue>();
160 *static_cast<QV4::CompiledData::EnumValue*>(v) = *serializedEnum->enumValueAt(idx: i);
161 e->enumValues->append(item: v);
162 }
163
164 object->qmlEnums->append(item: e);
165 }
166
167 const QV4::CompiledData::Property *serializedProperty = serializedObject->propertyTable();
168 for (uint i = 0; i < serializedObject->nProperties; ++i, ++serializedProperty) {
169 QmlIR::Property *p = pool->New<QmlIR::Property>();
170 *static_cast<QV4::CompiledData::Property*>(p) = *serializedProperty;
171 object->properties->append(item: p);
172 }
173
174 {
175 const QV4::CompiledData::Alias *serializedAlias = serializedObject->aliasTable();
176 for (uint i = 0; i < serializedObject->nAliases; ++i, ++serializedAlias) {
177 QmlIR::Alias *a = pool->New<QmlIR::Alias>();
178 *static_cast<QV4::CompiledData::Alias*>(a) = *serializedAlias;
179 object->aliases->append(item: a);
180 }
181 }
182
183 const quint32_le *functionIdx = serializedObject->functionOffsetTable();
184 for (uint i = 0; i < serializedObject->nFunctions; ++i, ++functionIdx) {
185 QmlIR::Function *f = pool->New<QmlIR::Function>();
186 const QV4::CompiledData::Function *compiledFunction = unit->functionAt(idx: *functionIdx);
187
188 functionIndices.append(t: *functionIdx);
189 f->index = functionIndices.count() - 1;
190 f->location = compiledFunction->location;
191 f->nameIndex = compiledFunction->nameIndex;
192 f->returnType = compiledFunction->returnType;
193
194 f->formals.allocate(pool, size: int(compiledFunction->nFormals));
195 const QV4::CompiledData::Parameter *formalNameIdx = compiledFunction->formalsTable();
196 for (uint i = 0; i < compiledFunction->nFormals; ++i, ++formalNameIdx)
197 *static_cast<QV4::CompiledData::Parameter*>(&f->formals[i]) = *formalNameIdx;
198
199 object->functions->append(item: f);
200 }
201
202 object->runtimeFunctionIndices.allocate(pool, vector: functionIndices);
203
204 const QV4::CompiledData::InlineComponent *serializedInlineComponent = serializedObject->inlineComponentTable();
205 for (uint i = 0; i < serializedObject->nInlineComponents; ++i, ++serializedInlineComponent) {
206 QmlIR::InlineComponent *ic = pool->New<QmlIR::InlineComponent>();
207 *static_cast<QV4::CompiledData::InlineComponent*>(ic) = *serializedInlineComponent;
208 object->inlineComponents->append(item: ic);
209 }
210
211 const QV4::CompiledData::RequiredPropertyExtraData *serializedRequiredPropertyExtraData = serializedObject->requiredPropertyExtraDataTable();
212 for (uint i = 0u; i < serializedObject->nRequiredPropertyExtraData; ++i, ++serializedRequiredPropertyExtraData) {
213 QmlIR::RequiredPropertyExtraData *extra = pool->New<QmlIR::RequiredPropertyExtraData>();
214 *static_cast<QV4::CompiledData::RequiredPropertyExtraData *>(extra) = *serializedRequiredPropertyExtraData;
215 object->requiredPropertyExtraDatas->append(item: extra);
216 }
217
218 return object;
219}
220
221QT_END_NAMESPACE
222

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