Add QFastMetaBuilder
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeparser_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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPARSER_P_H
43 #define QDECLARATIVEPARSER_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
58 #include <QtCore/qbytearray.h>
59 #include <QtCore/qlist.h>
60 #include <QtCore/qurl.h>
61 #include <QtCore/qstring.h>
62 #include <QtCore/qstringlist.h>
63
64 #include <private/qobject_p.h>
65 #include <private/qdeclarativerefcount_p.h>
66 #include <private/qdeclarativeglobal_p.h>
67 #include <private/qdeclarativepool_p.h>
68 #include <private/qfieldlist_p.h>
69 #include <private/qdeclarativepropertycache_p.h>
70 #include <private/qfastmetabuilder_p.h>
71
72 QT_BEGIN_HEADER
73
74 QT_BEGIN_NAMESPACE
75
76 QT_MODULE(Declarative)
77
78 class QDeclarativePropertyCache;
79 namespace QDeclarativeJS { namespace AST { class Node; class StringLiteral; } }
80 namespace QDeclarativeCompilerTypes { class BindingReference; class ComponentCompileState; }
81
82 /*
83     XXX
84
85     These types are created (and owned) by the QDeclarativeScriptParser and consumed by the 
86     QDeclarativeCompiler.  During the compilation phase the compiler will update some of
87     the fields for its own use.
88
89     The types are part of the generic sounding "QDeclarativeParser" namespace for legacy 
90     reasons (there used to be more in this namespace) and will be cleaned up and
91     migrated into a more appropriate location eventually.
92 */
93 namespace QDeclarativeParser
94 {
95     struct Location 
96     {
97         Location() : line(-1), column(-1) {}
98         int line;
99         int column;
100
101         inline bool operator<(const Location &other) {
102             return line < other.line || 
103                    (line == other.line && column < other.column);
104         }
105     };
106
107     struct LocationRange
108     {
109         LocationRange() : offset(0), length(0) {}
110         quint32 offset;
111         quint32 length;
112     };
113
114     struct LocationSpan
115     {
116         Location start;
117         Location end;
118         LocationRange range;
119
120         bool operator<(LocationSpan &o) const {
121             return (start.line < o.start.line) ||
122                    (start.line == o.start.line && start.column < o.start.column);
123         }
124     };
125
126     class Object;
127     class Property;
128     class Q_DECLARATIVE_EXPORT Variant 
129     {
130     public:
131         enum Type {
132             Invalid,
133             Boolean,
134             Number,
135             String,
136             Script
137         };
138
139         Variant();
140         Variant(const Variant &);
141         explicit Variant(bool);
142         explicit Variant(double, const QStringRef &asWritten = QStringRef());
143         explicit Variant(QDeclarativeJS::AST::StringLiteral *);
144         explicit Variant(const QStringRef &asWritten, QDeclarativeJS::AST::Node *);
145         Variant &operator=(const Variant &);
146
147         Type type() const;
148
149         bool isBoolean() const { return type() == Boolean; }
150         bool isNumber() const { return type() == Number; }
151         bool isString() const { return type() == String; }
152         bool isScript() const { return type() == Script; }
153         bool isStringList() const;
154
155         bool asBoolean() const;
156         QString asString() const;
157         double asNumber() const;
158         QString asScript() const;
159         QDeclarativeJS::AST::Node *asAST() const;
160         QStringList asStringList() const;
161
162     private:
163         Type t;
164         union {
165             bool b;
166             double d;
167             QDeclarativeJS::AST::StringLiteral *l;
168             QDeclarativeJS::AST::Node *n;
169         };
170         QStringRef asWritten;
171     };
172
173     class Value : public QDeclarativePool::POD
174     {
175     public:
176         Value();
177
178         enum Type {
179             // The type of this value assignment is not yet known
180             Unknown,
181             // This is used as a literal property assignment
182             Literal,
183             // This is used as a property binding assignment
184             PropertyBinding,
185             // This is used as a QDeclarativePropertyValueSource assignment
186             ValueSource,
187             // This is used as a QDeclarativePropertyValueInterceptor assignment
188             ValueInterceptor,
189             // This is used as a property QObject assignment
190             CreatedObject,
191             // This is used as a signal object assignment
192             SignalObject,
193             // This is used as a signal expression assignment
194             SignalExpression,
195             // This is used as an id assignment only
196             Id
197         };
198         Type type;
199
200         // ### Temporary (for id only)
201         QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
202
203         // Primitive value
204         Variant value;
205         // Object value
206         Object *object;
207
208         LocationSpan location;
209
210         // Used by compiler
211         QDeclarativeCompilerTypes::BindingReference *bindingReference;
212         int signalExpressionContextStack;
213
214         // Used in Property::ValueList lists
215         Value *nextValue;
216     };
217
218     class Property : public QDeclarativePool::POD
219     {
220     public:
221         Property();
222
223         // The Object to which this property is attached
224         Object *parent;
225
226         Object *getValue(const LocationSpan &);
227         void addValue(Value *v);
228         void addOnValue(Value *v);
229
230         // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
231         // unknown.
232         int type;
233         // The metaobject index of this property, or -1 if unknown.
234         int index;
235         // The core data in the case of a regular property.  
236         // XXX This has to be a value now as the synthCache may change during
237         // compilation which invalidates pointers.  We should fix this.
238         QDeclarativePropertyCache::Data core;
239
240         // Returns true if this is an empty property - both value and values
241         // are unset.
242         bool isEmpty() const;
243
244         typedef QFieldList<Value, &Value::nextValue> ValueList;
245         // The list of values assigned to this property.  Content in values
246         // and value are mutually exclusive
247         ValueList values;
248         // The list of values assigned to this property using the "on" syntax
249         ValueList onValues;
250         // The accessed property.  This is used to represent dot properties.
251         // Content in value and values are mutually exclusive.
252         Object *value;
253         // The property name
254         QStringRef name() const { return _name; }
255         void setName(const QString &n) { _name = QStringRef(pool()->NewString(n)); }
256         // True if this property was accessed as the default property.  
257         bool isDefault;
258         // True if the setting of this property will be deferred.  Set by the
259         // QDeclarativeCompiler
260         bool isDeferred;
261         // True if this property is a value-type pseudo-property
262         bool isValueTypeSubProperty;
263         // True if this property is a property alias.  Set by the 
264         // QDeclarativeCompiler
265         bool isAlias;
266
267         // Used for scriptStringProperties
268         int scriptStringScope;
269
270         LocationSpan location;
271         LocationRange listValueRange;
272
273         // Used in Object::MainPropertyList
274         Property *nextMainProperty;
275
276         // Used in Object::PropertyList lists
277         Property *nextProperty;
278
279     private:
280         friend class Object;
281         QStringRef _name;
282     };
283
284     class Object : public QDeclarativePool::Class
285     {
286     public:
287         Object();
288         virtual ~Object(); 
289
290         // Type of the object.  The integer is an index into the 
291         // QDeclarativeCompiledData::types array, or -1 if the object is a property
292         // group.
293         int type;
294
295         // The fully-qualified name of this type
296         QByteArray typeName;
297         // The id assigned to the object (if any).  Set by the QDeclarativeCompiler
298         QString id;
299         // The id index assigned to the object (if any).  Set by the QDeclarativeCompiler
300         int idIndex;
301         // Custom parsed data
302         QByteArray custom;
303         // Bit mask of the properties assigned bindings
304         QByteArray bindingBitmask; 
305         void setBindingBit(int);
306         // Returns the metaobject for this type, or 0 if not available.  
307         // Internally selectd between the metatype and extObject variables
308         const QMetaObject *metaObject() const;
309
310         // The compile time metaobject for this type
311         const QMetaObject *metatype;
312         // The synthesized metaobject, if QML added signals or properties to
313         // this type.  Otherwise null
314         QAbstractDynamicMetaObject extObject;
315         QByteArray metadata; // Generated by compiler
316         QByteArray synthdata; // Generated by compiler
317         QDeclarativePropertyCache *synthCache; // Generated by compiler
318
319         Property *getDefaultProperty();
320         // name ptr must be guarenteed to remain valid
321         Property *getProperty(const QStringRef &name, bool create=true);
322         Property *getProperty(const QString &name, bool create=true);
323
324         Property *defaultProperty;
325
326         typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
327         MainPropertyList properties;
328
329         // Output of the compilation phase (these properties continue to exist
330         // in either the defaultProperty or properties members too)
331         void addValueProperty(Property *);
332         void addSignalProperty(Property *);
333         void addAttachedProperty(Property *);
334         void addGroupedProperty(Property *);
335         void addValueTypeProperty(Property *);
336         void addScriptStringProperty(Property *);
337
338         typedef QFieldList<Property, &Property::nextProperty> PropertyList;
339         PropertyList valueProperties;
340         PropertyList signalProperties;
341         PropertyList attachedProperties;
342         PropertyList groupedProperties;
343         PropertyList valueTypeProperties;
344         PropertyList scriptStringProperties;
345
346         // Script blocks that were nested under this object
347         struct ScriptBlock {
348             enum Pragma { 
349                 None   = 0x00000000,
350                 Shared = 0x00000001
351             };
352             Q_DECLARE_FLAGS(Pragmas, Pragma)
353
354             QString code;
355             QString file;
356             Pragmas pragmas;
357         };
358
359         // The bytes to cast instances by to get to the QDeclarativeParserStatus 
360         // interface.  -1 indicates the type doesn't support this interface.
361         // Set by the QDeclarativeCompiler.
362         int parserStatusCast;
363
364         LocationSpan location;
365
366         struct DynamicProperty {
367             DynamicProperty();
368             DynamicProperty(const DynamicProperty &);
369
370             enum Type { Variant, Int, Bool, Real, String, Url, Color, Time, Date, DateTime, Alias, Custom, CustomList };
371
372             bool isDefaultProperty;
373             Type type;
374             QByteArray customType;
375             QByteArray name;
376             QDeclarativeParser::Property *defaultValue;
377             LocationSpan location;
378
379             // Used by the compiler
380             QByteArray resolvedCustomTypeName;
381             QFastMetaBuilder::StringRef typeRef;
382             QFastMetaBuilder::StringRef nameRef;
383             QFastMetaBuilder::StringRef changedSignatureRef;
384         };
385         struct DynamicSignal {
386             DynamicSignal();
387             DynamicSignal(const DynamicSignal &);
388
389             QByteArray name;
390             QList<QByteArray> parameterTypes;
391             QList<QByteArray> parameterNames;
392
393             int parameterTypesLength() const;
394             int parameterNamesLength() const;
395
396             // Used by the compiler
397             QFastMetaBuilder::StringRef signatureRef;
398             QFastMetaBuilder::StringRef parameterNamesRef;
399         };
400         struct DynamicSlot {
401             DynamicSlot();
402             DynamicSlot(const DynamicSlot &);
403
404             QByteArray name;
405             QString body;
406             QList<QByteArray> parameterNames;
407             LocationSpan location;
408
409             int parameterNamesLength() const;
410
411             // Used by the compiler
412             QFastMetaBuilder::StringRef signatureRef;
413             QFastMetaBuilder::StringRef parameterNamesRef;
414         };
415
416         // The list of dynamic properties
417         QList<DynamicProperty> dynamicProperties;
418         // The list of dynamic signals
419         QList<DynamicSignal> dynamicSignals;
420         // The list of dynamic slots
421         QList<DynamicSlot> dynamicSlots;
422
423         // Used by compiler
424         QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
425
426         // Used by ComponentCompileState::AliasingObjectsList
427         Object *nextAliasingObject;
428         // Used by ComponentComppileState::IdList
429         Object *nextIdObject;
430     };
431
432 }
433
434 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
435
436 QT_END_NAMESPACE
437
438 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
439
440 QT_END_HEADER
441
442 #endif // QDECLARATIVEPARSER_P_H