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(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(i);
70 QmlIR::Object *object = loadObject(serializedObject);
71 output->objects.append(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::AST::SourceLocation firstSourceLocation() const
82 { return location; }
83
84 virtual QQmlJS::AST::SourceLocation lastSourceLocation() const
85 { return location; }
86
87private:
88 QQmlJS::AST::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, serializedObject->inheritedTypeNameIndex, serializedObject->idNameIndex);
95
96 object->indexOfDefaultPropertyOrAlias = serializedObject->indexOfDefaultPropertyOrAlias;
97 object->defaultPropertyIsAlias = serializedObject->defaultPropertyIsAlias;
98 object->flags = serializedObject->flags;
99 object->id = serializedObject->id;
100 object->location = serializedObject->location;
101 object->locationOfIdProperty = serializedObject->locationOfIdProperty;
102
103 QVector<int> functionIndices;
104 functionIndices.reserve(serializedObject->nFunctions + serializedObject->nBindings / 2);
105
106 for (uint i = 0; i < serializedObject->nBindings; ++i) {
107 QmlIR::Binding *b = pool->New<QmlIR::Binding>();
108 *static_cast<QV4::CompiledData::Binding*>(b) = serializedObject->bindingTable()[i];
109 object->bindings->append(b);
110 if (b->type == QV4::CompiledData::Binding::Type_Script) {
111 functionIndices.append(b->value.compiledScriptIndex);
112 b->value.compiledScriptIndex = functionIndices.count() - 1;
113
114 QmlIR::CompiledFunctionOrExpression *foe = pool->New<QmlIR::CompiledFunctionOrExpression>();
115 foe->nameIndex = 0;
116
117 QQmlJS::AST::ExpressionNode *expr;
118
119 if (b->stringIndex != quint32(0)) {
120 const int start = output->code.length();
121 const QString script = output->stringAt(b->stringIndex);
122 const int length = script.length();
123 output->code.append(script);
124 expr = new (pool) FakeExpression(start, length);
125 } else
126 expr = new (pool) QQmlJS::AST::NullExpression();
127 foe->node = new (pool) QQmlJS::AST::ExpressionStatement(expr); // dummy
128 object->functionsAndExpressions->append(foe);
129 }
130 }
131
132 Q_ASSERT(object->functionsAndExpressions->count == functionIndices.count());
133
134 for (uint i = 0; i < serializedObject->nSignals; ++i) {
135 const QV4::CompiledData::Signal *serializedSignal = serializedObject->signalAt(i);
136 QmlIR::Signal *s = pool->New<QmlIR::Signal>();
137 s->nameIndex = serializedSignal->nameIndex;
138 s->location = serializedSignal->location;
139 s->parameters = pool->New<QmlIR::PoolList<QmlIR::Parameter> >();
140
141 for (uint i = 0; i < serializedSignal->nParameters; ++i) {
142 QmlIR::Parameter *p = pool->New<QmlIR::Parameter>();
143 *static_cast<QV4::CompiledData::Parameter*>(p) = *serializedSignal->parameterAt(i);
144 s->parameters->append(p);
145 }
146
147 object->qmlSignals->append(s);
148 }
149
150 for (uint i = 0; i < serializedObject->nEnums; ++i) {
151 const QV4::CompiledData::Enum *serializedEnum = serializedObject->enumAt(i);
152 QmlIR::Enum *e = pool->New<QmlIR::Enum>();
153 e->nameIndex = serializedEnum->nameIndex;
154 e->location = serializedEnum->location;
155 e->enumValues = pool->New<QmlIR::PoolList<QmlIR::EnumValue> >();
156
157 for (uint i = 0; i < serializedEnum->nEnumValues; ++i) {
158 QmlIR::EnumValue *v = pool->New<QmlIR::EnumValue>();
159 *static_cast<QV4::CompiledData::EnumValue*>(v) = *serializedEnum->enumValueAt(i);
160 e->enumValues->append(v);
161 }
162
163 object->qmlEnums->append(e);
164 }
165
166 const QV4::CompiledData::Property *serializedProperty = serializedObject->propertyTable();
167 for (uint i = 0; i < serializedObject->nProperties; ++i, ++serializedProperty) {
168 QmlIR::Property *p = pool->New<QmlIR::Property>();
169 *static_cast<QV4::CompiledData::Property*>(p) = *serializedProperty;
170 object->properties->append(p);
171 }
172
173 {
174 const QV4::CompiledData::Alias *serializedAlias = serializedObject->aliasTable();
175 for (uint i = 0; i < serializedObject->nAliases; ++i, ++serializedAlias) {
176 QmlIR::Alias *a = pool->New<QmlIR::Alias>();
177 *static_cast<QV4::CompiledData::Alias*>(a) = *serializedAlias;
178 object->aliases->append(a);
179 }
180 }
181
182 const quint32_le *functionIdx = serializedObject->functionOffsetTable();
183 for (uint i = 0; i < serializedObject->nFunctions; ++i, ++functionIdx) {
184 QmlIR::Function *f = pool->New<QmlIR::Function>();
185 const QV4::CompiledData::Function *compiledFunction = unit->functionAt(*functionIdx);
186
187 functionIndices.append(*functionIdx);
188 f->index = functionIndices.count() - 1;
189 f->location = compiledFunction->location;
190 f->nameIndex = compiledFunction->nameIndex;
191 f->returnType = compiledFunction->returnType;
192
193 f->formals.allocate(pool, int(compiledFunction->nFormals));
194 const QV4::CompiledData::Parameter *formalNameIdx = compiledFunction->formalsTable();
195 for (uint i = 0; i < compiledFunction->nFormals; ++i, ++formalNameIdx)
196 *static_cast<QV4::CompiledData::Parameter*>(&f->formals[i]) = *formalNameIdx;
197
198 object->functions->append(f);
199 }
200
201 object->runtimeFunctionIndices.allocate(pool, functionIndices);
202
203 return object;
204}
205
206QT_END_NAMESPACE
207