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