1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEPARSER_P_H
43 #define QDECLARATIVEPARSER_P_H
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.
56 #include "qdeclarative.h"
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>
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 #include <private/qdeclarativepropertycache_p.h>
70 #include <private/qfastmetabuilder_p.h>
71 #include <private/qhashedstring_p.h>
72 #include <private/qhashfield_p.h>
78 QT_MODULE(Declarative)
80 class QDeclarativePropertyCache;
81 namespace QDeclarativeJS { namespace AST { class Node; class StringLiteral; } }
82 namespace QDeclarativeCompilerTypes { class BindingReference; class ComponentCompileState; }
87 These types are created (and owned) by the QDeclarativeScriptParser and consumed by the
88 QDeclarativeCompiler. During the compilation phase the compiler will update some of
89 the fields for its own use.
91 The types are part of the generic sounding "QDeclarativeParser" namespace for legacy
92 reasons (there used to be more in this namespace) and will be cleaned up and
93 migrated into a more appropriate location eventually.
95 namespace QDeclarativeParser
99 Location() : line(-1), column(-1) {}
103 inline bool operator<(const Location &other) {
104 return line < other.line ||
105 (line == other.line && column < other.column);
111 LocationRange() : offset(0), length(0) {}
122 bool operator<(LocationSpan &o) const {
123 return (start.line < o.start.line) ||
124 (start.line == o.start.line && start.column < o.start.column);
130 class Q_DECLARATIVE_EXPORT Variant
142 Variant(const Variant &);
143 explicit Variant(bool);
144 explicit Variant(double, const QStringRef &asWritten = QStringRef());
145 explicit Variant(QDeclarativeJS::AST::StringLiteral *);
146 explicit Variant(const QStringRef &asWritten, QDeclarativeJS::AST::Node *);
147 Variant &operator=(const Variant &);
151 bool isBoolean() const { return type() == Boolean; }
152 bool isNumber() const { return type() == Number; }
153 bool isString() const { return type() == String; }
154 bool isScript() const { return type() == Script; }
155 bool isStringList() const;
157 bool asBoolean() const;
158 QString asString() const;
159 double asNumber() const;
160 QString asScript() const;
161 QDeclarativeJS::AST::Node *asAST() const;
162 QStringList asStringList() const;
169 QDeclarativeJS::AST::StringLiteral *l;
170 QDeclarativeJS::AST::Node *n;
172 QStringRef asWritten;
175 class Value : public QDeclarativePool::POD
181 // The type of this value assignment is not yet known
183 // This is used as a literal property assignment
185 // This is used as a property binding assignment
187 // This is used as a QDeclarativePropertyValueSource assignment
189 // This is used as a QDeclarativePropertyValueInterceptor assignment
191 // This is used as a property QObject assignment
193 // This is used as a signal object assignment
195 // This is used as a signal expression assignment
197 // This is used as an id assignment only
202 // ### Temporary (for id only)
203 QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
210 LocationSpan location;
213 QDeclarativeCompilerTypes::BindingReference *bindingReference;
214 int signalExpressionContextStack;
216 // Used in Property::ValueList lists
220 class Property : public QDeclarativePool::POD
225 // The Object to which this property is attached
228 Object *getValue(const LocationSpan &);
229 void addValue(Value *v);
230 void addOnValue(Value *v);
232 // The QVariant::Type of the property, or 0 (QVariant::Invalid) if
235 // The metaobject index of this property, or -1 if unknown.
237 // The core data in the case of a regular property.
238 // XXX This has to be a value now as the synthCache may change during
239 // compilation which invalidates pointers. We should fix this.
240 QDeclarativePropertyCache::Data core;
242 // Returns true if this is an empty property - both value and values
244 bool isEmpty() const;
246 typedef QFieldList<Value, &Value::nextValue> ValueList;
247 // The list of values assigned to this property. Content in values
248 // and value are mutually exclusive
250 // The list of values assigned to this property using the "on" syntax
252 // The accessed property. This is used to represent dot properties.
253 // Content in value and values are mutually exclusive.
256 const QHashedStringRef &name() const { return _name; }
257 void setName(const QString &n) { _name = QHashedStringRef(pool()->NewString(n)); }
258 void setName(const QHashedStringRef &n) { _name = n; }
259 // True if this property was accessed as the default property.
261 // True if the setting of this property will be deferred. Set by the
262 // QDeclarativeCompiler
264 // True if this property is a value-type pseudo-property
265 bool isValueTypeSubProperty;
266 // True if this property is a property alias. Set by the
267 // QDeclarativeCompiler
270 // Used for scriptStringProperties
271 int scriptStringScope;
273 LocationSpan location;
274 LocationRange listValueRange;
276 // Used in Object::MainPropertyList
277 Property *nextMainProperty;
279 // Used in Object::PropertyList lists
280 Property *nextProperty;
284 QHashedStringRef _name;
287 class Object : public QDeclarativePool::Class
293 // Type of the object. The integer is an index into the
294 // QDeclarativeCompiledData::types array, or -1 if the object is a property
298 // The fully-qualified name of this type
300 // The id assigned to the object (if any). Set by the QDeclarativeCompiler
302 // The id index assigned to the object (if any). Set by the QDeclarativeCompiler
304 // Custom parsed data
306 // Bit mask of the properties assigned bindings
307 QByteArray bindingBitmask;
308 void setBindingBit(int);
309 // Returns the metaobject for this type, or 0 if not available.
310 // Internally selectd between the metatype and extObject variables
311 const QMetaObject *metaObject() const;
313 // The compile time metaobject for this type
314 const QMetaObject *metatype;
315 // The synthesized metaobject, if QML added signals or properties to
316 // this type. Otherwise null
317 QAbstractDynamicMetaObject extObject;
318 QByteArray metadata; // Generated by compiler
319 QByteArray synthdata; // Generated by compiler
320 QDeclarativePropertyCache *synthCache; // Generated by compiler
322 Property *getDefaultProperty();
323 // name ptr must be guarenteed to remain valid
324 Property *getProperty(const QHashedStringRef &name, bool create=true);
325 Property *getProperty(const QStringRef &name, bool create=true);
326 Property *getProperty(const QString &name, bool create=true);
328 Property *defaultProperty;
330 typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
331 MainPropertyList properties;
332 QHashField propertiesHashField;
334 // Output of the compilation phase (these properties continue to exist
335 // in either the defaultProperty or properties members too)
336 void addValueProperty(Property *);
337 void addSignalProperty(Property *);
338 void addAttachedProperty(Property *);
339 void addGroupedProperty(Property *);
340 void addValueTypeProperty(Property *);
341 void addScriptStringProperty(Property *);
343 typedef QFieldList<Property, &Property::nextProperty> PropertyList;
344 PropertyList valueProperties;
345 PropertyList signalProperties;
346 PropertyList attachedProperties;
347 PropertyList groupedProperties;
348 PropertyList valueTypeProperties;
349 PropertyList scriptStringProperties;
351 // Script blocks that were nested under this object
357 Q_DECLARE_FLAGS(Pragmas, Pragma)
364 // The bytes to cast instances by to get to the QDeclarativeParserStatus
365 // interface. -1 indicates the type doesn't support this interface.
366 // Set by the QDeclarativeCompiler.
367 int parserStatusCast;
369 LocationSpan location;
371 struct DynamicProperty : public QDeclarativePool::POD
375 enum Type { Variant, Int, Bool, Real, String, Url, Color, Time,
376 Date, DateTime, Alias, Custom, CustomList };
378 bool isDefaultProperty;
381 QHashedStringRef customType;
382 QHashedStringRef name;
383 QDeclarativeParser::Property *defaultValue;
384 LocationSpan location;
386 // Used by Object::DynamicPropertyList
387 DynamicProperty *nextProperty;
389 // Used by the compiler
390 QByteArray *resolvedCustomTypeName;
391 QFastMetaBuilder::StringRef typeRef;
392 QFastMetaBuilder::StringRef nameRef;
393 QFastMetaBuilder::StringRef changedSignatureRef;
396 struct DynamicSignal : public QDeclarativePool::Class
400 QHashedStringRef name;
401 QList<QHashedCStringRef> parameterTypes;
402 QList<QByteArray> parameterNames;
404 int parameterTypesLength() const;
405 int parameterNamesLength() const;
407 // Used by Object::DynamicSignalList
408 DynamicSignal *nextSignal;
410 // Used by the compiler
411 QFastMetaBuilder::StringRef signatureRef;
412 QFastMetaBuilder::StringRef parameterNamesRef;
415 struct DynamicSlot : public QDeclarativePool::Class
419 QHashedStringRef name;
421 QList<QByteArray> parameterNames;
422 LocationSpan location;
424 int parameterNamesLength() const;
426 // Used by Object::DynamicSlotList
427 DynamicSlot *nextSlot;
429 // Used by the compiler
430 QFastMetaBuilder::StringRef signatureRef;
431 QFastMetaBuilder::StringRef parameterNamesRef;
434 // The list of dynamic properties
435 typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
436 DynamicPropertyList dynamicProperties;
437 // The list of dynamic signals
438 typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
439 DynamicSignalList dynamicSignals;
440 // The list of dynamic slots
441 typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
442 DynamicSlotList dynamicSlots;
445 QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
447 // Used by ComponentCompileState::AliasingObjectsList
448 Object *nextAliasingObject;
449 // Used by ComponentComppileState::IdList
450 Object *nextIdObject;
455 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
459 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
463 #endif // QDECLARATIVEPARSER_P_H