1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #ifndef QDECLARATIVECOMPILER_P_H
43 #define QDECLARATIVECOMPILER_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "qdeclarative.h"
57 #include "qdeclarativeerror.h"
58 #include "private/qdeclarativeinstruction_p.h"
59 #include "private/qdeclarativeparser_p.h"
60 #include "private/qdeclarativeengine_p.h"
61 #include "private/qbitfield_p.h"
62 #include "private/qdeclarativepropertycache_p.h"
63 #include "private/qdeclarativeintegercache_p.h"
64 #include "private/qdeclarativetypenamecache_p.h"
65 #include "private/qdeclarativetypeloader_p.h"
67 #include <QtCore/qbytearray.h>
68 #include <QtCore/qset.h>
69 #include <QtCore/QCoreApplication>
73 class QDeclarativeEngine;
74 class QDeclarativeComponent;
75 class QDeclarativeContext;
76 class QDeclarativeContextData;
79 class Q_AUTOTEST_EXPORT QDeclarativeCompiledData : public QDeclarativeRefCount, public QDeclarativeCleanup
82 QDeclarativeCompiledData(QDeclarativeEngine *engine);
83 virtual ~QDeclarativeCompiledData();
87 QDeclarativeTypeNameCache *importCache;
92 : type(0), typePropertyCache(0), component(0) {}
95 QDeclarativeType *type;
96 QDeclarativePropertyCache *typePropertyCache;
97 QDeclarativeCompiledData *component;
99 QObject *createInstance(QDeclarativeContextData *, const QBitField &, QList<QDeclarativeError> *) const;
100 const QMetaObject *metaObject() const;
101 QDeclarativePropertyCache *propertyCache() const;
102 QDeclarativePropertyCache *createPropertyCache(QDeclarativeEngine *);
104 QList<TypeReference> types;
105 struct CustomTypeData
111 const QMetaObject *root;
112 QAbstractDynamicMetaObject rootData;
113 QDeclarativePropertyCache *rootPropertyCache;
114 QList<QString> primitives;
115 QList<float> floatData;
117 QList<CustomTypeData> customTypeData;
118 QList<QByteArray> datas;
119 QList<QDeclarativeParser::Location> locations;
120 QList<QDeclarativeInstruction> bytecode;
121 QList<QScriptProgram *> cachedPrograms;
122 QList<QScriptValue *> cachedClosures;
123 QList<QDeclarativePropertyCache *> propertyCaches;
124 QList<QDeclarativeIntegerCache *> contextCaches;
125 QList<QDeclarativeParser::Object::ScriptBlock> scripts;
128 void dumpInstructions();
131 virtual void clear(); // From QDeclarativeCleanup
134 void dump(QDeclarativeInstruction *, int idx = -1);
135 QDeclarativeCompiledData(const QDeclarativeCompiledData &other);
136 QDeclarativeCompiledData &operator=(const QDeclarativeCompiledData &other);
138 friend class QDeclarativeCompiler;
139 int pack(const char *, size_t);
141 int indexForString(const QString &);
142 int indexForByteArray(const QByteArray &);
143 int indexForFloat(float *, int);
144 int indexForInt(int *, int);
145 int indexForLocation(const QDeclarativeParser::Location &);
146 int indexForLocation(const QDeclarativeParser::LocationSpan &);
147 int indexForUrl(const QUrl &);
150 class QMetaObjectBuilder;
151 class Q_AUTOTEST_EXPORT QDeclarativeCompiler
153 Q_DECLARE_TR_FUNCTIONS(QDeclarativeCompiler)
155 QDeclarativeCompiler();
157 bool compile(QDeclarativeEngine *, QDeclarativeTypeData *, QDeclarativeCompiledData *);
159 bool isError() const;
160 QList<QDeclarativeError> errors() const;
162 static bool isAttachedPropertyName(const QByteArray &);
163 static bool isSignalPropertyName(const QByteArray &);
165 int evaluateEnum(const QByteArray& script) const; // for QDeclarativeCustomParser::evaluateEnum
166 const QMetaObject *resolveType(const QByteArray& name) const; // for QDeclarativeCustomParser::resolveType
167 int rewriteBinding(const QString& expression, const QByteArray& name); // for QDeclarativeCustomParser::rewriteBinding
170 static void reset(QDeclarativeCompiledData *);
172 struct BindingContext {
174 : stack(0), owner(0), object(0) {}
175 BindingContext(QDeclarativeParser::Object *o)
176 : stack(0), owner(0), object(o) {}
177 BindingContext incr() const {
178 BindingContext rv(object);
179 rv.stack = stack + 1;
182 bool isSubContext() const { return stack != 0; }
185 QDeclarativeParser::Object *object;
188 void compileTree(QDeclarativeParser::Object *tree);
191 bool buildObject(QDeclarativeParser::Object *obj, const BindingContext &);
192 bool buildComponent(QDeclarativeParser::Object *obj, const BindingContext &);
193 bool buildSubObject(QDeclarativeParser::Object *obj, const BindingContext &);
194 bool buildSignal(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj,
195 const BindingContext &);
196 bool buildProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj,
197 const BindingContext &);
198 bool buildPropertyInNamespace(QDeclarativeImportedNamespace *ns,
199 QDeclarativeParser::Property *prop,
200 QDeclarativeParser::Object *obj,
201 const BindingContext &);
202 bool buildIdProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
203 bool buildAttachedProperty(QDeclarativeParser::Property *prop,
204 QDeclarativeParser::Object *obj,
205 const BindingContext &ctxt);
206 bool buildGroupedProperty(QDeclarativeParser::Property *prop,
207 QDeclarativeParser::Object *obj,
208 const BindingContext &ctxt);
209 bool buildValueTypeProperty(QObject *type,
210 QDeclarativeParser::Object *obj,
211 QDeclarativeParser::Object *baseObj,
212 const BindingContext &ctxt);
213 bool buildListProperty(QDeclarativeParser::Property *prop,
214 QDeclarativeParser::Object *obj,
215 const BindingContext &ctxt);
216 bool buildScriptStringProperty(QDeclarativeParser::Property *prop,
217 QDeclarativeParser::Object *obj,
218 const BindingContext &ctxt);
219 bool buildPropertyAssignment(QDeclarativeParser::Property *prop,
220 QDeclarativeParser::Object *obj,
221 const BindingContext &ctxt);
222 bool buildPropertyObjectAssignment(QDeclarativeParser::Property *prop,
223 QDeclarativeParser::Object *obj,
224 QDeclarativeParser::Value *value,
225 const BindingContext &ctxt);
226 bool buildPropertyOnAssignment(QDeclarativeParser::Property *prop,
227 QDeclarativeParser::Object *obj,
228 QDeclarativeParser::Object *baseObj,
229 QDeclarativeParser::Value *value,
230 const BindingContext &ctxt);
231 bool buildPropertyLiteralAssignment(QDeclarativeParser::Property *prop,
232 QDeclarativeParser::Object *obj,
233 QDeclarativeParser::Value *value,
234 const BindingContext &ctxt);
235 bool doesPropertyExist(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
236 bool testLiteralAssignment(const QMetaProperty &prop,
237 QDeclarativeParser::Value *value);
238 bool testQualifiedEnumAssignment(const QMetaProperty &prop,
239 QDeclarativeParser::Object *obj,
240 QDeclarativeParser::Value *value,
242 enum DynamicMetaMode { IgnoreAliases, ResolveAliases, ForceCreation };
243 bool mergeDynamicMetaProperties(QDeclarativeParser::Object *obj);
244 bool buildDynamicMeta(QDeclarativeParser::Object *obj, DynamicMetaMode mode);
245 bool checkDynamicMeta(QDeclarativeParser::Object *obj);
246 bool buildBinding(QDeclarativeParser::Value *, QDeclarativeParser::Property *prop,
247 const BindingContext &ctxt);
248 bool buildComponentFromRoot(QDeclarativeParser::Object *obj, const BindingContext &);
249 bool compileAlias(QMetaObjectBuilder &,
251 QDeclarativeParser::Object *obj,
252 const QDeclarativeParser::Object::DynamicProperty &);
253 bool completeComponentBuild();
254 bool checkValidId(QDeclarativeParser::Value *, const QString &);
257 void genObject(QDeclarativeParser::Object *obj);
258 void genObjectBody(QDeclarativeParser::Object *obj);
259 void genValueTypeProperty(QDeclarativeParser::Object *obj,QDeclarativeParser::Property *);
260 void genComponent(QDeclarativeParser::Object *obj);
261 void genValueProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
262 void genListProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
263 void genPropertyAssignment(QDeclarativeParser::Property *prop,
264 QDeclarativeParser::Object *obj,
265 QDeclarativeParser::Property *valueTypeProperty = 0);
266 void genLiteralAssignment(const QMetaProperty &prop,
267 QDeclarativeParser::Value *value);
268 void genBindingAssignment(QDeclarativeParser::Value *binding,
269 QDeclarativeParser::Property *prop,
270 QDeclarativeParser::Object *obj,
271 QDeclarativeParser::Property *valueTypeProperty = 0);
272 int genContextCache();
274 int genValueTypeData(QDeclarativeParser::Property *prop, QDeclarativeParser::Property *valueTypeProp);
275 int genPropertyData(QDeclarativeParser::Property *prop);
277 int componentTypeRef();
279 static QDeclarativeType *toQmlType(QDeclarativeParser::Object *from);
280 bool canCoerce(int to, QDeclarativeParser::Object *from);
282 QStringList deferredProperties(QDeclarativeParser::Object *);
283 int indexOfProperty(QDeclarativeParser::Object *, const QByteArray &, bool *notInRevision = 0);
284 int indexOfSignal(QDeclarativeParser::Object *, const QByteArray &, bool *notInRevision = 0);
286 void addId(const QString &, QDeclarativeParser::Object *);
290 struct BindingReference {
291 QDeclarativeParser::Variant expression;
292 QDeclarativeParser::Property *property;
293 QDeclarativeParser::Value *value;
295 enum DataType { QtScript, Experimental };
300 QByteArray compiledData;
301 BindingContext bindingContext;
303 void addBindingReference(const BindingReference &);
305 struct ComponentCompileState
307 ComponentCompileState()
308 : parserStatusCount(0), pushedProperties(0), root(0) {}
309 QHash<QString, QDeclarativeParser::Object *> ids;
310 QHash<int, QDeclarativeParser::Object *> idIndexes;
311 int parserStatusCount;
312 int pushedProperties;
314 QByteArray compiledBindingData;
316 QHash<QDeclarativeParser::Value *, BindingReference> bindings;
317 QHash<QDeclarativeParser::Value *, BindingContext> signalExpressions;
318 QList<QDeclarativeParser::Object *> aliasingObjects;
319 QDeclarativeParser::Object *root;
321 ComponentCompileState compileState;
325 ComponentStat() : ids(0), objects(0) {}
330 QList<QDeclarativeParser::LocationSpan> scriptBindings;
331 QList<QDeclarativeParser::LocationSpan> optimizedBindings;
334 ComponentStat componentStat;
336 void saveComponentState();
338 ComponentCompileState componentState(QDeclarativeParser::Object *);
339 QHash<QDeclarativeParser::Object *, ComponentCompileState> savedCompileStates;
340 QList<ComponentStat> savedComponentStats;
342 QList<QDeclarativeError> exceptions;
343 QDeclarativeCompiledData *output;
344 QDeclarativeEngine *engine;
345 QDeclarativeEnginePrivate *enginePrivate;
346 QDeclarativeParser::Object *unitRoot;
347 QDeclarativeTypeData *unit;
351 #endif // QDECLARATIVECOMPILER_P_H