Add rect property type
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlscript_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #ifndef QQMLSCRIPT_P_H
42 #define QQMLSCRIPT_P_H
43
44 //
45 //  W A R N I N G
46 //  -------------
47 //
48 // This file is not part of the Qt API.  It exists purely as an
49 // implementation detail.  This header file may change from version to
50 // version without notice, or even be removed.
51 //
52 // We mean it.
53 //
54
55 #include <QtQml/qqmlerror.h>
56
57 #include <private/qfieldlist_p.h>
58 #include <private/qhashfield_p.h>
59 #include <private/qfastmetabuilder_p.h>
60 #include <private/qqmlpool_p.h>
61 #include <private/qqmlpropertycache_p.h>
62
63 #include <QtCore/QList>
64 #include <QtCore/QUrl>
65
66 QT_BEGIN_HEADER
67
68 QT_BEGIN_NAMESPACE
69
70
71 class QByteArray;
72 class QQmlPropertyCache;
73 namespace QQmlJS { namespace AST { class Node; class StringLiteral; } }
74 namespace QQmlCompilerTypes { struct BindingReference; struct ComponentCompileState; }
75
76 namespace QQmlScript {
77
78 struct Location 
79 {
80     Location() : line(-1), column(-1) {}
81     int line;
82     int column;
83
84     inline bool operator<(const Location &other) {
85         return line < other.line || 
86                (line == other.line && column < other.column);
87     }
88 };
89
90 struct LocationRange
91 {
92     LocationRange() : offset(0), length(0) {}
93     quint32 offset;
94     quint32 length;
95 };
96
97 struct LocationSpan
98 {
99     Location start;
100     Location end;
101     LocationRange range;
102
103     bool operator<(LocationSpan &o) const {
104         return (start.line < o.start.line) ||
105                (start.line == o.start.line && start.column < o.start.column);
106     }
107 };
108
109 class Import
110 {
111 public:
112     Import() : type(Library), majorVersion(-1), minorVersion(-1) {}
113
114     enum Type { Library, File, Script };
115     Type type;
116
117     QString uri;
118     QString qualifier;
119
120     int majorVersion;
121     int minorVersion;
122
123     QQmlScript::LocationSpan location;
124 };
125
126 class Object;
127 class TypeReference : public QQmlPool::Class
128 {
129 public:
130     // type as it has been referenced in Qml
131     QString name;
132     // The first use of this type in the parse tree.  Useful for error locations.
133     QQmlScript::Object *firstUse;
134 };
135
136 class Object;
137 class Property;
138
139 class Q_QML_EXPORT Variant 
140 {
141 public:
142     enum Type {
143         Invalid,
144         Boolean,
145         Number,
146         String,
147         Script
148     };
149
150     Variant();
151     Variant(const Variant &);
152     explicit Variant(bool);
153     explicit Variant(double, const QStringRef &asWritten = QStringRef());
154     explicit Variant(QQmlJS::AST::StringLiteral *);
155     explicit Variant(const QStringRef &asWritten, QQmlJS::AST::Node *);
156     Variant &operator=(const Variant &);
157
158     Type type() const;
159
160     bool isBoolean() const { return type() == Boolean; }
161     bool isNumber() const { return type() == Number; }
162     bool isString() const { return type() == String; }
163     bool isScript() const { return type() == Script; }
164     bool isStringList() const;
165
166     bool asBoolean() const;
167     QString asString() const;
168     double asNumber() const;
169     QString asScript() const;
170     QQmlJS::AST::Node *asAST() const;
171     QStringList asStringList() const;
172
173 private:
174     Type t;
175     union {
176         bool b;
177         double d;
178         QQmlJS::AST::StringLiteral *l;
179         QQmlJS::AST::Node *n;
180     };
181     QStringRef asWritten;
182 };
183
184 class Value : public QQmlPool::POD
185 {
186 public:
187     Value();
188
189     enum Type {
190         // The type of this value assignment is not yet known
191         Unknown,
192         // This is used as a literal property assignment
193         Literal,
194         // This is used as a property binding assignment
195         PropertyBinding,
196         // This is used as a QQmlPropertyValueSource assignment
197         ValueSource,
198         // This is used as a QQmlPropertyValueInterceptor assignment
199         ValueInterceptor,
200         // This is used as a property QObject assignment
201         CreatedObject,
202         // This is used as a signal object assignment
203         SignalObject,
204         // This is used as a signal expression assignment
205         SignalExpression,
206         // This is used as an id assignment only
207         Id
208     };
209     Type type;
210
211     // ### Temporary (for id only)
212     QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
213
214     // Primitive value
215     Variant value;
216     // Object value
217     Object *object;
218
219     LocationSpan location;
220
221     // Used by compiler
222     union {
223         QQmlCompilerTypes::BindingReference *bindingReference;
224         int signalExpressionContextStack;
225     };
226
227     // Used in Property::ValueList lists
228     Value *nextValue;
229 };
230
231 class Property : public QQmlPool::POD
232 {
233 public:
234     Property();
235
236     // The Object to which this property is attached
237     Object *parent;
238
239     Object *getValue(const LocationSpan &);
240     void addValue(Value *v);
241     void addOnValue(Value *v);
242
243     // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
244     // unknown.
245     int type;
246     // The metaobject index of this property, or -1 if unknown.
247     int index;
248     // The core data in the case of a regular property.  
249     // XXX This has to be a value now as the synthCache may change during
250     // compilation which invalidates pointers.  We should fix this.
251     QQmlPropertyData core;
252
253     // Returns true if this is an empty property - both value and values
254     // are unset.
255     bool isEmpty() const;
256
257     typedef QFieldList<Value, &Value::nextValue> ValueList;
258     // The list of values assigned to this property.  Content in values
259     // and value are mutually exclusive
260     ValueList values;
261     // The list of values assigned to this property using the "on" syntax
262     ValueList onValues;
263     // The accessed property.  This is used to represent dot properties.
264     // Content in value and values are mutually exclusive.
265     Object *value;
266     // The property name
267     const QHashedStringRef &name() const { return _name; }
268     void setName(const QString &n) { _name = QHashedStringRef(pool()->NewString(n)); }
269     void setName(const QHashedStringRef &n) { _name = n; }
270     // True if this property was accessed as the default property.  
271     bool isDefault;
272     // True if the setting of this property will be deferred.  Set by the
273     // QQmlCompiler
274     bool isDeferred;
275     // True if this property is a value-type pseudo-property
276     bool isValueTypeSubProperty;
277     // True if this property is a property alias.  Set by the 
278     // QQmlCompiler
279     bool isAlias;
280     // True if this is a readonly property declaration
281     bool isReadOnlyDeclaration;
282
283     // Used for scriptStringProperties
284     int scriptStringScope;
285
286     LocationSpan location;
287     LocationRange listValueRange;
288
289     // Used in Object::MainPropertyList
290     Property *nextMainProperty;
291
292     // Used in Object::PropertyList lists
293     Property *nextProperty;
294
295 private:
296     friend class Object;
297     QHashedStringRef _name;
298 };
299
300 class Object : public QQmlPool::Class
301 {
302 public:
303     Object();
304     virtual ~Object(); 
305
306     // Type of the object.  The integer is an index into the 
307     // QQmlCompiledData::types array, or -1 if the object is a property
308     // group.
309     int type;
310     // A back pointer to the QQmlScript::TypeReference for this type, if any.
311     // Set by the parser.
312     TypeReference *typeReference;
313
314     // The id assigned to the object (if any).  Set by the QQmlCompiler
315     QString id;
316     // The id index assigned to the object (if any).  Set by the QQmlCompiler
317     int idIndex;
318     // Custom parsed data
319     QByteArray custom;
320     // Bit mask of the properties assigned bindings
321     QByteArray bindingBitmask; 
322     void setBindingBit(int);
323     // Returns the metaobject for this type, or 0 if not available.  
324     // Internally selectd between the metatype and extObject variables
325     const QMetaObject *metaObject() const;
326
327     // The compile time metaobject for this type
328     const QMetaObject *metatype;
329     // The synthesized metaobject, if QML added signals or properties to
330     // this type.  Otherwise null
331     QAbstractDynamicMetaObject extObject;
332     QByteArray metadata; // Generated by compiler
333     QByteArray synthdata; // Generated by compiler
334     QQmlPropertyCache *synthCache; // Generated by compiler
335
336     Property *getDefaultProperty();
337     // name ptr must be guarenteed to remain valid
338     Property *getProperty(const QHashedStringRef &name, bool create=true);
339     Property *getProperty(const QStringRef &name, bool create=true);
340     Property *getProperty(const QString &name, bool create=true);
341
342     Property *defaultProperty;
343
344     typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
345     MainPropertyList properties;
346     QHashField propertiesHashField;
347
348     // Output of the compilation phase (these properties continue to exist
349     // in either the defaultProperty or properties members too)
350     void addValueProperty(Property *);
351     void addSignalProperty(Property *);
352     void addAttachedProperty(Property *);
353     void addGroupedProperty(Property *);
354     void addValueTypeProperty(Property *);
355     void addScriptStringProperty(Property *);
356
357     typedef QFieldList<Property, &Property::nextProperty> PropertyList;
358     PropertyList valueProperties;
359     PropertyList signalProperties;
360     PropertyList attachedProperties;
361     PropertyList groupedProperties;
362     PropertyList valueTypeProperties;
363     PropertyList scriptStringProperties;
364
365     // Script blocks that were nested under this object
366     struct ScriptBlock {
367         enum Pragma { 
368             None   = 0x00000000,
369             Shared = 0x00000001
370         };
371         Q_DECLARE_FLAGS(Pragmas, Pragma)
372
373         QString code;
374         QString file;
375         Pragmas pragmas;
376     };
377
378     // The bytes to cast instances by to get to the QQmlParserStatus 
379     // interface.  -1 indicates the type doesn't support this interface.
380     // Set by the QQmlCompiler.
381     int parserStatusCast;
382
383     LocationSpan location;
384
385     struct DynamicProperty : public QQmlPool::POD 
386     {
387         DynamicProperty();
388
389         enum Type { Var, Variant, Int, Bool, Real, String, Url, Color,
390                     Time, Date, DateTime, Rect, Alias, Custom, CustomList };
391
392         quint32 isDefaultProperty:1;
393         quint32 isReadOnly:1;
394
395         Type type;
396
397         QHashedStringRef customType;
398         QHashedStringRef name;
399         QQmlScript::Property *defaultValue;
400         LocationSpan location;
401         Location nameLocation;
402
403         // Used by Object::DynamicPropertyList
404         DynamicProperty *nextProperty;
405
406         // Used by the compiler
407         QFastMetaBuilder::StringRef nameRef;
408         QFastMetaBuilder::StringRef changedNameRef;
409     };
410
411     struct DynamicSignal : public QQmlPool::POD
412     {
413         DynamicSignal();
414
415         QHashedStringRef name;
416         QQmlPool::List<DynamicProperty::Type> parameterTypes;
417         QQmlPool::List<QHashedStringRef> parameterNames;
418
419         // Used by Object::DynamicSignalList
420         DynamicSignal *nextSignal;
421
422         // Used by the compiler
423         QFastMetaBuilder::StringRef nameRef;
424         QQmlPool::List<QFastMetaBuilder::StringRef> parameterNamesRef;
425         LocationSpan location;
426     };
427
428     struct DynamicSlot : public QQmlPool::Class
429     {
430         DynamicSlot();
431
432         QHashedStringRef name;
433         QString body;
434         QList<QByteArray> parameterNames;
435         LocationSpan location;
436
437         int parameterNamesLength() const;
438
439         // Used by Object::DynamicSlotList
440         DynamicSlot *nextSlot;
441
442         // Used by the compiler
443         QFastMetaBuilder::StringRef nameRef;
444         QQmlPool::List<QFastMetaBuilder::StringRef> parameterNamesRef;
445     };
446
447     // The list of dynamic properties
448     typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
449     DynamicPropertyList dynamicProperties;
450     // The list of dynamic signals
451     typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
452     DynamicSignalList dynamicSignals;
453     // The list of dynamic slots
454     typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
455     DynamicSlotList dynamicSlots;
456
457     int aggregateDynamicSignalParameterCount() const;
458     int aggregateDynamicSlotParameterCount() const;
459
460     // Used by compiler
461     QQmlCompilerTypes::ComponentCompileState *componentCompileState;
462
463     // Used by ComponentCompileState::AliasingObjectsList
464     Object *nextAliasingObject;
465     // Used by ComponentComppileState::IdList
466     Object *nextIdObject;
467 };
468
469 class ParserJsASTData;
470 class Q_QML_EXPORT Parser
471 {
472 public:
473     Parser();
474     ~Parser();
475
476     bool parse(const QString &data, const QByteArray &preparseData,
477                const QUrl &url = QUrl(), const QString &urlString = QString());
478
479     QByteArray preparseData() const;
480
481     QList<TypeReference*> referencedTypes() const;
482
483     QQmlScript::Object *tree() const;
484     QList<Import> imports() const;
485
486     void clear();
487
488     QList<QQmlError> errors() const;
489
490     class JavaScriptMetaData {
491     public:
492         JavaScriptMetaData() 
493         : pragmas(QQmlScript::Object::ScriptBlock::None) {}
494
495         QQmlScript::Object::ScriptBlock::Pragmas pragmas;
496         QList<Import> imports;
497     };
498
499     static QQmlScript::Object::ScriptBlock::Pragmas extractPragmas(QString &);
500     static JavaScriptMetaData extractMetaData(QString &);
501
502
503 // ### private:
504     int findOrCreateTypeId(const QString &name, Object *);
505     void setTree(QQmlScript::Object *tree);
506
507     void setScriptFile(const QString &filename) {_scriptFile = filename; }
508     QString scriptFile() const { return _scriptFile; }
509
510 // ### private:
511     QList<QQmlError> _errors;
512
513     QQmlPool _pool;
514     QQmlScript::Object *root;
515     QList<Import> _imports;
516     QList<TypeReference*> _refTypes;
517     QString _scriptFile;
518     ParserJsASTData *data;
519 };
520
521 }
522
523 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlScript::Object::ScriptBlock::Pragmas);
524
525 QT_END_NAMESPACE
526
527 Q_DECLARE_METATYPE(QQmlScript::Variant)
528
529 QT_END_HEADER
530
531 #endif // QQMLSCRIPT_P_H