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