Initial import from the monolithic Qt.
[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 ** 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 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
68 QT_BEGIN_HEADER
69
70 QT_BEGIN_NAMESPACE
71
72 QT_MODULE(Declarative)
73
74 class QDeclarativePropertyCache;
75 namespace QDeclarativeJS { namespace AST { class Node; } }
76
77 /*
78     XXX
79
80     These types are created (and owned) by the QDeclarativeXmlParser and consumed by the 
81     QDeclarativeCompiler.  During the compilation phase the compiler will update some of
82     the fields for both its own use and for the use of the upcoming QDeclarativeDom API.
83
84     The types are part of the generic sounding "QDeclarativeParser" namespace for legacy 
85     reasons (there used to be more in this namespace) and will be cleaned up and
86     migrated into a more appropriate location shortly.
87 */
88 namespace QDeclarativeParser
89 {
90     struct Location 
91     {
92         Location() : line(-1), column(-1) {}
93         int line;
94         int column;
95     };
96
97     struct LocationRange
98     {
99         LocationRange() : offset(0), length(0) {}
100         quint32 offset;
101         quint32 length;
102     };
103
104     struct LocationSpan
105     {
106         Location start;
107         Location end;
108         LocationRange range;
109
110         bool operator<(LocationSpan &o) const {
111             return (start.line < o.start.line) ||
112                    (start.line == o.start.line && start.column < o.start.column);
113         }
114     };
115
116     class Property;
117     class Object : public QDeclarativeRefCount
118     {
119     public:
120         Object();
121         virtual ~Object(); 
122
123         // Type of the object.  The integer is an index into the 
124         // QDeclarativeCompiledData::types array, or -1 if the object is a property
125         // group.
126         int type;
127         // The url of this object if it is an external type.  Used by the DOM
128         QUrl url;
129
130         // version information if type is defined in library or C++
131         int majorVersion;
132         int minorVersion;
133
134         // The fully-qualified name of this type
135         QByteArray typeName;
136         // The class name
137         QByteArray className;
138         // The id assigned to the object (if any).  Set by the QDeclarativeCompiler
139         QString id;
140         // The id index assigned to the object (if any).  Set by the QDeclarativeCompiler
141         int idIndex;
142         // Custom parsed data
143         QByteArray custom;
144         // Bit mask of the properties assigned bindings
145         QByteArray bindingBitmask; 
146         void setBindingBit(int);
147         // Returns the metaobject for this type, or 0 if not available.  
148         // Internally selectd between the metatype and extObject variables
149         const QMetaObject *metaObject() const;
150
151         // The compile time metaobject for this type
152         const QMetaObject *metatype;
153         // The synthesized metaobject, if QML added signals or properties to
154         // this type.  Otherwise null
155         QAbstractDynamicMetaObject extObject;
156         QByteArray metadata; // Generated by compiler
157         QByteArray synthdata; // Generated by compiler
158         QDeclarativePropertyCache *synthCache; // Generated by compiler
159
160         Property *getDefaultProperty();
161         Property *getProperty(const QByteArray &name, bool create=true);
162
163         Property *defaultProperty;
164         QHash<QByteArray, Property *> properties;
165
166         // Output of the compilation phase (these properties continue to exist
167         // in either the defaultProperty or properties members too)
168         void addValueProperty(Property *);
169         void addSignalProperty(Property *);
170         void addAttachedProperty(Property *);
171         void addGroupedProperty(Property *);
172         void addValueTypeProperty(Property *);
173         void addScriptStringProperty(Property *, int = 0);
174         QList<Property *> valueProperties;
175         QList<Property *> signalProperties;
176         QList<Property *> attachedProperties;
177         QList<Property *> groupedProperties;
178         QList<Property *> valueTypeProperties;
179         QList<QPair<Property *, int> > scriptStringProperties;
180
181         // Script blocks that were nested under this object
182         struct ScriptBlock {
183             enum Pragma { 
184                 None   = 0x00000000,
185                 Shared = 0x00000001
186             };
187             Q_DECLARE_FLAGS(Pragmas, Pragma)
188
189             QString code;
190             QString file;
191             Pragmas pragmas;
192         };
193
194         // The bytes to cast instances by to get to the QDeclarativeParserStatus 
195         // interface.  -1 indicates the type doesn't support this interface.
196         // Set by the QDeclarativeCompiler.
197         int parserStatusCast;
198
199         LocationSpan location;
200
201         struct DynamicProperty {
202             DynamicProperty();
203             DynamicProperty(const DynamicProperty &);
204
205             enum Type { Variant, Int, Bool, Real, String, Url, Color, Time, Date, DateTime, Alias, Custom, CustomList };
206
207             bool isDefaultProperty;
208             Type type;
209             QByteArray customType;
210             QByteArray name;
211             QDeclarativeParser::Property *defaultValue;
212             LocationSpan location;
213         };
214         struct DynamicSignal {
215             DynamicSignal();
216             DynamicSignal(const DynamicSignal &);
217
218             QByteArray name;
219             QList<QByteArray> parameterTypes;
220             QList<QByteArray> parameterNames;
221         };
222         struct DynamicSlot {
223             DynamicSlot();
224             DynamicSlot(const DynamicSlot &);
225
226             QByteArray name;
227             QString body;
228             QList<QByteArray> parameterNames;
229             LocationSpan location;
230         };
231
232         // The list of dynamic properties
233         QList<DynamicProperty> dynamicProperties;
234         // The list of dynamic signals
235         QList<DynamicSignal> dynamicSignals;
236         // The list of dynamic slots
237         QList<DynamicSlot> dynamicSlots;
238     };
239
240     class Q_DECLARATIVE_EXPORT Variant 
241     {
242     public:
243         enum Type {
244             Invalid,
245             Boolean,
246             Number,
247             String,
248             Script
249         };
250
251         Variant();
252         Variant(const Variant &);
253         Variant(bool);
254         Variant(double, const QString &asWritten=QString());
255         Variant(const QString &);
256         Variant(const QString &, QDeclarativeJS::AST::Node *);
257         Variant &operator=(const Variant &);
258
259         Type type() const;
260
261         bool isBoolean() const { return type() == Boolean; }
262         bool isNumber() const { return type() == Number; }
263         bool isString() const { return type() == String; }
264         bool isScript() const { return type() == Script; }
265         bool isStringList() const;
266
267         bool asBoolean() const;
268         QString asString() const;
269         double asNumber() const;
270         QString asScript() const;
271         QDeclarativeJS::AST::Node *asAST() const;
272         QStringList asStringList() const;
273
274     private:
275         Type t;
276         union {
277             bool b;
278             double d;
279             QDeclarativeJS::AST::Node *n;
280         };
281         QString s;
282     };
283
284     class Value : public QDeclarativeRefCount
285     {
286     public:
287         Value();
288         virtual ~Value();
289
290         enum Type {
291             // The type of this value assignment is not yet known
292             Unknown,
293             // This is used as a literal property assignment
294             Literal,
295             // This is used as a property binding assignment
296             PropertyBinding,
297             // This is used as a QDeclarativePropertyValueSource assignment
298             ValueSource,
299             // This is used as a QDeclarativePropertyValueInterceptor assignment
300             ValueInterceptor,
301             // This is used as a property QObject assignment
302             CreatedObject,
303             // This is used as a signal object assignment
304             SignalObject,
305             // This is used as a signal expression assignment
306             SignalExpression,
307             // This is used as an id assignment only
308             Id
309         };
310         Type type;
311
312         // ### Temporary (for id only)
313         QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
314
315         // Primitive value
316         Variant value;
317         // Object value
318         Object *object;
319
320         LocationSpan location;
321     };
322
323     class Property : public QDeclarativeRefCount
324     {
325     public:
326         Property();
327         Property(const QByteArray &n);
328         virtual ~Property();
329
330         // The Object to which this property is attached
331         Object *parent;
332
333         Object *getValue(const LocationSpan &);
334         void addValue(Value *v);
335         void addOnValue(Value *v);
336
337         // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
338         // unknown.
339         int type;
340         // The metaobject index of this property, or -1 if unknown.
341         int index;
342
343         // Returns true if this is an empty property - both value and values
344         // are unset.
345         bool isEmpty() const;
346         // The list of values assigned to this property.  Content in values
347         // and value are mutually exclusive
348         QList<Value *> values;
349         // The list of values assigned to this property using the "on" syntax
350         QList<Value *> onValues;
351         // The accessed property.  This is used to represent dot properties.
352         // Content in value and values are mutually exclusive.
353         Object *value;
354         // The property name
355         QByteArray name;
356         // True if this property was accessed as the default property.  
357         bool isDefault;
358         // True if the setting of this property will be deferred.  Set by the
359         // QDeclarativeCompiler
360         bool isDeferred;
361         // True if this property is a value-type pseudo-property
362         bool isValueTypeSubProperty;
363         // True if this property is a property alias.  Set by the 
364         // QDeclarativeCompiler
365         bool isAlias;
366
367         LocationSpan location;
368         LocationRange listValueRange;
369         QList<int> listCommaPositions;
370     };
371 }
372
373 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
374
375 QT_END_NAMESPACE
376
377 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
378
379 QT_END_HEADER
380
381 #endif // QDECLARATIVEPARSER_P_H