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