Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecompiler_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVECOMPILER_P_H
43 #define QDECLARATIVECOMPILER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
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"
66
67 #include <QtCore/qbytearray.h>
68 #include <QtCore/qset.h>
69 #include <QtCore/QCoreApplication>
70
71 QT_BEGIN_NAMESPACE
72
73 class QDeclarativeEngine;
74 class QDeclarativeComponent;
75 class QDeclarativeContext;
76 class QDeclarativeContextData;
77
78 class QScriptProgram;
79 class Q_AUTOTEST_EXPORT QDeclarativeCompiledData : public QDeclarativeRefCount, public QDeclarativeCleanup
80 {
81 public:
82     QDeclarativeCompiledData(QDeclarativeEngine *engine);
83     virtual ~QDeclarativeCompiledData();
84
85     QString name;
86     QUrl url;
87     QDeclarativeTypeNameCache *importCache;
88
89     struct TypeReference 
90     {
91         TypeReference()
92         : type(0), typePropertyCache(0), component(0) {}
93
94         QByteArray className;
95         QDeclarativeType *type;
96         QDeclarativePropertyCache *typePropertyCache;
97         QDeclarativeCompiledData *component;
98
99         QObject *createInstance(QDeclarativeContextData *, const QBitField &, QList<QDeclarativeError> *) const;
100         const QMetaObject *metaObject() const;
101         QDeclarativePropertyCache *propertyCache() const;
102         QDeclarativePropertyCache *createPropertyCache(QDeclarativeEngine *);
103     };
104     QList<TypeReference> types;
105     struct CustomTypeData
106     {
107         int index;
108         int type;
109     };
110
111     const QMetaObject *root;
112     QAbstractDynamicMetaObject rootData;
113     QDeclarativePropertyCache *rootPropertyCache;
114     QList<QString> primitives;
115     QList<float> floatData;
116     QList<int> intData;
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;
126     QList<QUrl> urls;
127
128     void dumpInstructions();
129
130 protected:
131     virtual void clear(); // From QDeclarativeCleanup
132
133 private:
134     void dump(QDeclarativeInstruction *, int idx = -1);
135     QDeclarativeCompiledData(const QDeclarativeCompiledData &other);
136     QDeclarativeCompiledData &operator=(const QDeclarativeCompiledData &other);
137     QByteArray packData;
138     friend class QDeclarativeCompiler;
139     int pack(const char *, size_t);
140
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 &);
148 };
149
150 class QMetaObjectBuilder;
151 class Q_AUTOTEST_EXPORT QDeclarativeCompiler
152 {
153     Q_DECLARE_TR_FUNCTIONS(QDeclarativeCompiler)
154 public:
155     QDeclarativeCompiler();
156
157     bool compile(QDeclarativeEngine *, QDeclarativeTypeData *, QDeclarativeCompiledData *);
158
159     bool isError() const;
160     QList<QDeclarativeError> errors() const;
161
162     static bool isAttachedPropertyName(const QByteArray &);
163     static bool isSignalPropertyName(const QByteArray &);
164
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
168
169 private:
170     static void reset(QDeclarativeCompiledData *);
171
172     struct BindingContext {
173         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;
180             return rv;
181         }
182         bool isSubContext() const { return stack != 0; }
183         int stack;
184         int owner;
185         QDeclarativeParser::Object *object;
186     };
187
188     void compileTree(QDeclarativeParser::Object *tree);
189
190
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,
241                                      bool *isAssignment);
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 &, 
250                       QByteArray &data,
251                       QDeclarativeParser::Object *obj, 
252                       const QDeclarativeParser::Object::DynamicProperty &);
253     bool completeComponentBuild();
254     bool checkValidId(QDeclarativeParser::Value *, const QString &);
255
256
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();
273
274     int genValueTypeData(QDeclarativeParser::Property *prop, QDeclarativeParser::Property *valueTypeProp);
275     int genPropertyData(QDeclarativeParser::Property *prop);
276
277     int componentTypeRef();
278
279     static QDeclarativeType *toQmlType(QDeclarativeParser::Object *from);
280     bool canCoerce(int to, QDeclarativeParser::Object *from);
281
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);
285
286     void addId(const QString &, QDeclarativeParser::Object *);
287
288     void dumpStats();
289
290     struct BindingReference {
291         QDeclarativeParser::Variant expression;
292         QDeclarativeParser::Property *property;
293         QDeclarativeParser::Value *value;
294
295         enum DataType { QtScript, Experimental };
296         DataType dataType;
297
298         int compiledIndex;
299
300         QByteArray compiledData;
301         BindingContext bindingContext;
302     };
303     void addBindingReference(const BindingReference &);
304
305     struct ComponentCompileState
306     {
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;
313
314         QByteArray compiledBindingData;
315
316         QHash<QDeclarativeParser::Value *, BindingReference> bindings;
317         QHash<QDeclarativeParser::Value *, BindingContext> signalExpressions;
318         QList<QDeclarativeParser::Object *> aliasingObjects;
319         QDeclarativeParser::Object *root;
320     };
321     ComponentCompileState compileState;
322
323     struct ComponentStat
324     {
325         ComponentStat() : ids(0), objects(0) {}
326
327         int lineNumber;
328
329         int ids;
330         QList<QDeclarativeParser::LocationSpan> scriptBindings;
331         QList<QDeclarativeParser::LocationSpan> optimizedBindings;
332         int objects;
333     };
334     ComponentStat componentStat;
335
336     void saveComponentState();
337
338     ComponentCompileState componentState(QDeclarativeParser::Object *);
339     QHash<QDeclarativeParser::Object *, ComponentCompileState> savedCompileStates;
340     QList<ComponentStat> savedComponentStats;
341
342     QList<QDeclarativeError> exceptions;
343     QDeclarativeCompiledData *output;
344     QDeclarativeEngine *engine;
345     QDeclarativeEnginePrivate *enginePrivate;
346     QDeclarativeParser::Object *unitRoot;
347     QDeclarativeTypeData *unit;
348 };
349 QT_END_NAMESPACE
350
351 #endif // QDECLARATIVECOMPILER_P_H