Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativescript_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 QtDeclarative 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 QDECLARATIVESCRIPT_P_H
42 #define QDECLARATIVESCRIPT_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 <QtDeclarative/qdeclarativeerror.h>
56
57 #include <private/qfieldlist_p.h>
58 #include <private/qhashfield_p.h>
59 #include <private/qfastmetabuilder_p.h>
60 #include <private/qdeclarativepool_p.h>
61 #include <private/qdeclarativepropertycache_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 QDeclarativePropertyCache;
73 namespace QDeclarativeJS { namespace AST { class Node; class StringLiteral; } }
74 namespace QDeclarativeCompilerTypes { struct BindingReference; struct ComponentCompileState; }
75
76 namespace QDeclarativeScript {
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) {}
113
114     enum Type { Library, File, Script };
115     Type type;
116
117     QString uri;
118     QString qualifier;
119     QString version;
120
121     void extractVersion(int *maj, int *min) const;
122
123     QDeclarativeScript::LocationSpan location;
124 };
125
126 class Object;
127 class TypeReference
128 {
129 public:
130     TypeReference(int typeId, const QString &typeName) : id(typeId), name(typeName) {}
131
132     int id;
133     // type as it has been referenced in Qml
134     QString name;
135     // objects in parse tree referencing the type
136     QList<QDeclarativeScript::Object*> refObjects;
137 };
138
139 class Object;
140 class Property;
141
142 class Q_DECLARATIVE_EXPORT Variant 
143 {
144 public:
145     enum Type {
146         Invalid,
147         Boolean,
148         Number,
149         String,
150         Script
151     };
152
153     Variant();
154     Variant(const Variant &);
155     explicit Variant(bool);
156     explicit Variant(double, const QStringRef &asWritten = QStringRef());
157     explicit Variant(QDeclarativeJS::AST::StringLiteral *);
158     explicit Variant(const QStringRef &asWritten, QDeclarativeJS::AST::Node *);
159     Variant &operator=(const Variant &);
160
161     Type type() const;
162
163     bool isBoolean() const { return type() == Boolean; }
164     bool isNumber() const { return type() == Number; }
165     bool isString() const { return type() == String; }
166     bool isScript() const { return type() == Script; }
167     bool isStringList() const;
168
169     bool asBoolean() const;
170     QString asString() const;
171     double asNumber() const;
172     QString asScript() const;
173     QDeclarativeJS::AST::Node *asAST() const;
174     QStringList asStringList() const;
175
176 private:
177     Type t;
178     union {
179         bool b;
180         double d;
181         QDeclarativeJS::AST::StringLiteral *l;
182         QDeclarativeJS::AST::Node *n;
183     };
184     QStringRef asWritten;
185 };
186
187 class Value : public QDeclarativePool::POD
188 {
189 public:
190     Value();
191
192     enum Type {
193         // The type of this value assignment is not yet known
194         Unknown,
195         // This is used as a literal property assignment
196         Literal,
197         // This is used as a property binding assignment
198         PropertyBinding,
199         // This is used as a QDeclarativePropertyValueSource assignment
200         ValueSource,
201         // This is used as a QDeclarativePropertyValueInterceptor assignment
202         ValueInterceptor,
203         // This is used as a property QObject assignment
204         CreatedObject,
205         // This is used as a signal object assignment
206         SignalObject,
207         // This is used as a signal expression assignment
208         SignalExpression,
209         // This is used as an id assignment only
210         Id
211     };
212     Type type;
213
214     // ### Temporary (for id only)
215     QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
216
217     // Primitive value
218     Variant value;
219     // Object value
220     Object *object;
221
222     LocationSpan location;
223
224     // Used by compiler
225     union {
226         QDeclarativeCompilerTypes::BindingReference *bindingReference;
227         int signalExpressionContextStack;
228     };
229
230     // Used in Property::ValueList lists
231     Value *nextValue;
232 };
233
234 class Property : public QDeclarativePool::POD
235 {
236 public:
237     Property();
238
239     // The Object to which this property is attached
240     Object *parent;
241
242     Object *getValue(const LocationSpan &);
243     void addValue(Value *v);
244     void addOnValue(Value *v);
245
246     // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
247     // unknown.
248     int type;
249     // The metaobject index of this property, or -1 if unknown.
250     int index;
251     // The core data in the case of a regular property.  
252     // XXX This has to be a value now as the synthCache may change during
253     // compilation which invalidates pointers.  We should fix this.
254     QDeclarativePropertyData core;
255
256     // Returns true if this is an empty property - both value and values
257     // are unset.
258     bool isEmpty() const;
259
260     typedef QFieldList<Value, &Value::nextValue> ValueList;
261     // The list of values assigned to this property.  Content in values
262     // and value are mutually exclusive
263     ValueList values;
264     // The list of values assigned to this property using the "on" syntax
265     ValueList onValues;
266     // The accessed property.  This is used to represent dot properties.
267     // Content in value and values are mutually exclusive.
268     Object *value;
269     // The property name
270     const QHashedStringRef &name() const { return _name; }
271     void setName(const QString &n) { _name = QHashedStringRef(pool()->NewString(n)); }
272     void setName(const QHashedStringRef &n) { _name = n; }
273     // True if this property was accessed as the default property.  
274     bool isDefault;
275     // True if the setting of this property will be deferred.  Set by the
276     // QDeclarativeCompiler
277     bool isDeferred;
278     // True if this property is a value-type pseudo-property
279     bool isValueTypeSubProperty;
280     // True if this property is a property alias.  Set by the 
281     // QDeclarativeCompiler
282     bool isAlias;
283     // True if this is a readonly property declaration
284     bool isReadOnlyDeclaration;
285
286     // Used for scriptStringProperties
287     int scriptStringScope;
288
289     LocationSpan location;
290     LocationRange listValueRange;
291
292     // Used in Object::MainPropertyList
293     Property *nextMainProperty;
294
295     // Used in Object::PropertyList lists
296     Property *nextProperty;
297
298 private:
299     friend class Object;
300     QHashedStringRef _name;
301 };
302
303 class Object : public QDeclarativePool::Class
304 {
305 public:
306     Object();
307     virtual ~Object(); 
308
309     // Type of the object.  The integer is an index into the 
310     // QDeclarativeCompiledData::types array, or -1 if the object is a property
311     // group.
312     int type;
313
314     // The fully-qualified name of this type
315     QString typeName;
316     // The id assigned to the object (if any).  Set by the QDeclarativeCompiler
317     QString id;
318     // The id index assigned to the object (if any).  Set by the QDeclarativeCompiler
319     int idIndex;
320     // Custom parsed data
321     QByteArray custom;
322     // Bit mask of the properties assigned bindings
323     QByteArray bindingBitmask; 
324     void setBindingBit(int);
325     // Returns the metaobject for this type, or 0 if not available.  
326     // Internally selectd between the metatype and extObject variables
327     const QMetaObject *metaObject() const;
328
329     // The compile time metaobject for this type
330     const QMetaObject *metatype;
331     // The synthesized metaobject, if QML added signals or properties to
332     // this type.  Otherwise null
333     QAbstractDynamicMetaObject extObject;
334     QByteArray metadata; // Generated by compiler
335     QByteArray synthdata; // Generated by compiler
336     QDeclarativePropertyCache *synthCache; // Generated by compiler
337
338     Property *getDefaultProperty();
339     // name ptr must be guarenteed to remain valid
340     Property *getProperty(const QHashedStringRef &name, bool create=true);
341     Property *getProperty(const QStringRef &name, bool create=true);
342     Property *getProperty(const QString &name, bool create=true);
343
344     Property *defaultProperty;
345
346     typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
347     MainPropertyList properties;
348     QHashField propertiesHashField;
349
350     // Output of the compilation phase (these properties continue to exist
351     // in either the defaultProperty or properties members too)
352     void addValueProperty(Property *);
353     void addSignalProperty(Property *);
354     void addAttachedProperty(Property *);
355     void addGroupedProperty(Property *);
356     void addValueTypeProperty(Property *);
357     void addScriptStringProperty(Property *);
358
359     typedef QFieldList<Property, &Property::nextProperty> PropertyList;
360     PropertyList valueProperties;
361     PropertyList signalProperties;
362     PropertyList attachedProperties;
363     PropertyList groupedProperties;
364     PropertyList valueTypeProperties;
365     PropertyList scriptStringProperties;
366
367     // Script blocks that were nested under this object
368     struct ScriptBlock {
369         enum Pragma { 
370             None   = 0x00000000,
371             Shared = 0x00000001
372         };
373         Q_DECLARE_FLAGS(Pragmas, Pragma)
374
375         QString code;
376         QString file;
377         Pragmas pragmas;
378     };
379
380     // The bytes to cast instances by to get to the QDeclarativeParserStatus 
381     // interface.  -1 indicates the type doesn't support this interface.
382     // Set by the QDeclarativeCompiler.
383     int parserStatusCast;
384
385     LocationSpan location;
386
387     struct DynamicProperty : public QDeclarativePool::POD 
388     {
389         DynamicProperty();
390
391         enum Type { Var, Variant, Int, Bool, Real, String, Url, Color,
392                     Time, Date, DateTime, Alias, Custom, CustomList };
393
394         quint32 isDefaultProperty:1;
395         quint32 isReadOnly:1;
396
397         Type type;
398
399         QHashedStringRef customType;
400         QHashedStringRef name;
401         QDeclarativeScript::Property *defaultValue;
402         LocationSpan location;
403         Location nameLocation;
404
405         // Used by Object::DynamicPropertyList
406         DynamicProperty *nextProperty;
407
408         // Used by the compiler
409         QByteArray *resolvedCustomTypeName;
410         QFastMetaBuilder::StringRef typeRef;
411         QFastMetaBuilder::StringRef nameRef;
412         QFastMetaBuilder::StringRef changedSignatureRef;
413     };
414
415     struct DynamicSignal : public QDeclarativePool::POD
416     {
417         DynamicSignal();
418
419         QHashedStringRef name;
420         QDeclarativePool::List<QHashedCStringRef> parameterTypes;
421         QDeclarativePool::List<QHashedStringRef> parameterNames;
422
423         int parameterTypesLength() const;
424         int parameterNamesLength() const;
425
426         // Used by Object::DynamicSignalList
427         DynamicSignal *nextSignal;
428
429         // Used by the compiler
430         QFastMetaBuilder::StringRef signatureRef;
431         QFastMetaBuilder::StringRef parameterNamesRef;
432         LocationSpan location;
433     };
434
435     struct DynamicSlot : public QDeclarativePool::Class
436     {
437         DynamicSlot();
438
439         QHashedStringRef name;
440         QString body;
441         QList<QByteArray> parameterNames;
442         LocationSpan location;
443
444         int parameterNamesLength() const;
445
446         // Used by Object::DynamicSlotList
447         DynamicSlot *nextSlot;
448
449         // Used by the compiler
450         QFastMetaBuilder::StringRef signatureRef;
451         QFastMetaBuilder::StringRef parameterNamesRef;
452     };
453
454     // The list of dynamic properties
455     typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
456     DynamicPropertyList dynamicProperties;
457     // The list of dynamic signals
458     typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
459     DynamicSignalList dynamicSignals;
460     // The list of dynamic slots
461     typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
462     DynamicSlotList dynamicSlots;
463
464     // Used by compiler
465     QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
466
467     // Used by ComponentCompileState::AliasingObjectsList
468     Object *nextAliasingObject;
469     // Used by ComponentComppileState::IdList
470     Object *nextIdObject;
471 };
472
473 class ParserJsASTData;
474 class Q_AUTOTEST_EXPORT Parser
475 {
476 public:
477     Parser();
478     ~Parser();
479
480     bool parse(const QByteArray &data, const QUrl &url = QUrl());
481
482     QList<TypeReference*> referencedTypes() const;
483
484     QDeclarativeScript::Object *tree() const;
485     QList<Import> imports() const;
486
487     void clear();
488
489     QList<QDeclarativeError> errors() const;
490
491     class JavaScriptMetaData {
492     public:
493         JavaScriptMetaData() 
494         : pragmas(QDeclarativeScript::Object::ScriptBlock::None) {}
495
496         QDeclarativeScript::Object::ScriptBlock::Pragmas pragmas;
497         QList<Import> imports;
498     };
499
500     static QDeclarativeScript::Object::ScriptBlock::Pragmas extractPragmas(QString &);
501     static JavaScriptMetaData extractMetaData(QString &);
502
503
504 // ### private:
505     TypeReference *findOrCreateType(const QString &name);
506     void setTree(QDeclarativeScript::Object *tree);
507
508     void setScriptFile(const QString &filename) {_scriptFile = filename; }
509     QString scriptFile() const { return _scriptFile; }
510
511 // ### private:
512     QList<QDeclarativeError> _errors;
513
514     QDeclarativePool _pool;
515     QDeclarativeScript::Object *root;
516     QList<Import> _imports;
517     QList<TypeReference*> _refTypes;
518     QString _scriptFile;
519     ParserJsASTData *data;
520 };
521
522 }
523
524 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeScript::Object::ScriptBlock::Pragmas);
525
526 QT_END_NAMESPACE
527
528 Q_DECLARE_METATYPE(QDeclarativeScript::Variant)
529
530 QT_END_HEADER
531
532 #endif // QDECLARATIVESCRIPT_P_H