Use memory more conservatively during QML compilation
[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
211         // Used in Property::ValueList lists
212         Value *nextValue;
213     };
214
215     class Property : public QDeclarativePool::POD
216     {
217     public:
218         Property();
219
220         // The Object to which this property is attached
221         Object *parent;
222
223         Object *getValue(const LocationSpan &);
224         void addValue(Value *v);
225         void addOnValue(Value *v);
226
227         // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
228         // unknown.
229         int type;
230         // The metaobject index of this property, or -1 if unknown.
231         int index;
232
233         // Returns true if this is an empty property - both value and values
234         // are unset.
235         bool isEmpty() const;
236
237         typedef QFieldList<Value, &Value::nextValue> ValueList;
238         // The list of values assigned to this property.  Content in values
239         // and value are mutually exclusive
240         ValueList values;
241         // The list of values assigned to this property using the "on" syntax
242         ValueList onValues;
243         // The accessed property.  This is used to represent dot properties.
244         // Content in value and values are mutually exclusive.
245         Object *value;
246         // The property name
247         QString name() const { return _name?*_name:QString(); }
248         void setName(const QString &n) { _name = pool()->NewString(n); }
249         // True if this property was accessed as the default property.  
250         bool isDefault;
251         // True if the setting of this property will be deferred.  Set by the
252         // QDeclarativeCompiler
253         bool isDeferred;
254         // True if this property is a value-type pseudo-property
255         bool isValueTypeSubProperty;
256         // True if this property is a property alias.  Set by the 
257         // QDeclarativeCompiler
258         bool isAlias;
259
260         // Used for scriptStringProperties
261         int scriptStringScope;
262
263         LocationSpan location;
264         LocationRange listValueRange;
265
266         // Used in Object::MainPropertyList
267         Property *nextMainProperty;
268
269         // Used in Object::PropertyList lists
270         Property *nextProperty;
271
272     private:
273         friend class Object;
274         QString *_name;
275     };
276
277     class Object : public QDeclarativePool::Class
278     {
279     public:
280         Object();
281         virtual ~Object(); 
282
283         // Type of the object.  The integer is an index into the 
284         // QDeclarativeCompiledData::types array, or -1 if the object is a property
285         // group.
286         int type;
287
288         // The fully-qualified name of this type
289         QByteArray typeName;
290         // The id assigned to the object (if any).  Set by the QDeclarativeCompiler
291         QString id;
292         // The id index assigned to the object (if any).  Set by the QDeclarativeCompiler
293         int idIndex;
294         // Custom parsed data
295         QByteArray custom;
296         // Bit mask of the properties assigned bindings
297         QByteArray bindingBitmask; 
298         void setBindingBit(int);
299         // Returns the metaobject for this type, or 0 if not available.  
300         // Internally selectd between the metatype and extObject variables
301         const QMetaObject *metaObject() const;
302
303         // The compile time metaobject for this type
304         const QMetaObject *metatype;
305         // The synthesized metaobject, if QML added signals or properties to
306         // this type.  Otherwise null
307         QAbstractDynamicMetaObject extObject;
308         QByteArray metadata; // Generated by compiler
309         QByteArray synthdata; // Generated by compiler
310         QDeclarativePropertyCache *synthCache; // Generated by compiler
311
312         Property *getDefaultProperty();
313         // name ptr must be guarenteed to remain valid
314         Property *getProperty(const QString *name, bool create=true);
315         Property *getProperty(const QString &name, bool create=true);
316
317         Property *defaultProperty;
318
319         typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
320         MainPropertyList properties;
321
322         // Output of the compilation phase (these properties continue to exist
323         // in either the defaultProperty or properties members too)
324         void addValueProperty(Property *);
325         void addSignalProperty(Property *);
326         void addAttachedProperty(Property *);
327         void addGroupedProperty(Property *);
328         void addValueTypeProperty(Property *);
329         void addScriptStringProperty(Property *);
330
331         typedef QFieldList<Property, &Property::nextProperty> PropertyList;
332         PropertyList valueProperties;
333         PropertyList signalProperties;
334         PropertyList attachedProperties;
335         PropertyList groupedProperties;
336         PropertyList valueTypeProperties;
337         PropertyList scriptStringProperties;
338
339         // Script blocks that were nested under this object
340         struct ScriptBlock {
341             enum Pragma { 
342                 None   = 0x00000000,
343                 Shared = 0x00000001
344             };
345             Q_DECLARE_FLAGS(Pragmas, Pragma)
346
347             QString code;
348             QString file;
349             Pragmas pragmas;
350         };
351
352         // The bytes to cast instances by to get to the QDeclarativeParserStatus 
353         // interface.  -1 indicates the type doesn't support this interface.
354         // Set by the QDeclarativeCompiler.
355         int parserStatusCast;
356
357         LocationSpan location;
358
359         struct DynamicProperty {
360             DynamicProperty();
361             DynamicProperty(const DynamicProperty &);
362
363             enum Type { Variant, Int, Bool, Real, String, Url, Color, Time, Date, DateTime, Alias, Custom, CustomList };
364
365             bool isDefaultProperty;
366             Type type;
367             QByteArray customType;
368             QByteArray name;
369             QDeclarativeParser::Property *defaultValue;
370             LocationSpan location;
371         };
372         struct DynamicSignal {
373             DynamicSignal();
374             DynamicSignal(const DynamicSignal &);
375
376             QByteArray name;
377             QList<QByteArray> parameterTypes;
378             QList<QByteArray> parameterNames;
379         };
380         struct DynamicSlot {
381             DynamicSlot();
382             DynamicSlot(const DynamicSlot &);
383
384             QByteArray name;
385             QString body;
386             QList<QByteArray> parameterNames;
387             LocationSpan location;
388         };
389
390         // The list of dynamic properties
391         QList<DynamicProperty> dynamicProperties;
392         // The list of dynamic signals
393         QList<DynamicSignal> dynamicSignals;
394         // The list of dynamic slots
395         QList<DynamicSlot> dynamicSlots;
396
397         // Used by compiler
398         QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
399     };
400
401 }
402
403 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
404
405 QT_END_NAMESPACE
406
407 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
408
409 QT_END_HEADER
410
411 #endif // QDECLARATIVEPARSER_P_H