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 // True if this property was accessed as the default property.
260 // True if the setting of this property will be deferred. Set by the
261 // QDeclarativeCompiler
263 // True if this property is a value-type pseudo-property
264 bool isValueTypeSubProperty;
265 // True if this property is a property alias. Set by the
266 // QDeclarativeCompiler
269 // Used for scriptStringProperties
270 int scriptStringScope;
272 LocationSpan location;
273 LocationRange listValueRange;
275 // Used in Object::MainPropertyList
276 Property *nextMainProperty;
278 // Used in Object::PropertyList lists
279 Property *nextProperty;
283 QHashedStringRef _name;
286 class Object : public QDeclarativePool::Class
292 // Type of the object. The integer is an index into the
293 // QDeclarativeCompiledData::types array, or -1 if the object is a property
297 // The fully-qualified name of this type
299 // The id assigned to the object (if any). Set by the QDeclarativeCompiler
301 // The id index assigned to the object (if any). Set by the QDeclarativeCompiler
303 // Custom parsed data
305 // Bit mask of the properties assigned bindings
306 QByteArray bindingBitmask;
307 void setBindingBit(int);
308 // Returns the metaobject for this type, or 0 if not available.
309 // Internally selectd between the metatype and extObject variables
310 const QMetaObject *metaObject() const;
312 // The compile time metaobject for this type
313 const QMetaObject *metatype;
314 // The synthesized metaobject, if QML added signals or properties to
315 // this type. Otherwise null
316 QAbstractDynamicMetaObject extObject;
317 QByteArray metadata; // Generated by compiler
318 QByteArray synthdata; // Generated by compiler
319 QDeclarativePropertyCache *synthCache; // Generated by compiler
321 Property *getDefaultProperty();
322 // name ptr must be guarenteed to remain valid
323 Property *getProperty(const QHashedStringRef &name, bool create=true);
324 Property *getProperty(const QStringRef &name, bool create=true);
325 Property *getProperty(const QString &name, bool create=true);
327 Property *defaultProperty;
329 typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
330 MainPropertyList properties;
331 QHashField propertiesHashField;
333 // Output of the compilation phase (these properties continue to exist
334 // in either the defaultProperty or properties members too)
335 void addValueProperty(Property *);
336 void addSignalProperty(Property *);
337 void addAttachedProperty(Property *);
338 void addGroupedProperty(Property *);
339 void addValueTypeProperty(Property *);
340 void addScriptStringProperty(Property *);
342 typedef QFieldList<Property, &Property::nextProperty> PropertyList;
343 PropertyList valueProperties;
344 PropertyList signalProperties;
345 PropertyList attachedProperties;
346 PropertyList groupedProperties;
347 PropertyList valueTypeProperties;
348 PropertyList scriptStringProperties;
350 // Script blocks that were nested under this object
356 Q_DECLARE_FLAGS(Pragmas, Pragma)
363 // The bytes to cast instances by to get to the QDeclarativeParserStatus
364 // interface. -1 indicates the type doesn't support this interface.
365 // Set by the QDeclarativeCompiler.
366 int parserStatusCast;
368 LocationSpan location;
370 struct DynamicProperty : public QDeclarativePool::POD
374 enum Type { Variant, Int, Bool, Real, String, Url, Color, Time,
375 Date, DateTime, Alias, Custom, CustomList };
377 bool isDefaultProperty;
380 QHashedStringRef customType;
381 QHashedStringRef name;
382 QDeclarativeParser::Property *defaultValue;
383 LocationSpan location;
385 // Used by Object::DynamicPropertyList
386 DynamicProperty *nextProperty;
388 // Used by the compiler
389 QByteArray *resolvedCustomTypeName;
390 QFastMetaBuilder::StringRef typeRef;
391 QFastMetaBuilder::StringRef nameRef;
392 QFastMetaBuilder::StringRef changedSignatureRef;
395 struct DynamicSignal : public QDeclarativePool::Class
399 QHashedStringRef name;
400 QList<QHashedCStringRef> parameterTypes;
401 QList<QByteArray> parameterNames;
403 int parameterTypesLength() const;
404 int parameterNamesLength() const;
406 // Used by Object::DynamicSignalList
407 DynamicSignal *nextSignal;
409 // Used by the compiler
410 QFastMetaBuilder::StringRef signatureRef;
411 QFastMetaBuilder::StringRef parameterNamesRef;
414 struct DynamicSlot : public QDeclarativePool::Class
418 QHashedStringRef name;
420 QList<QByteArray> parameterNames;
421 LocationSpan location;
423 int parameterNamesLength() const;
425 // Used by Object::DynamicSlotList
426 DynamicSlot *nextSlot;
428 // Used by the compiler
429 QFastMetaBuilder::StringRef signatureRef;
430 QFastMetaBuilder::StringRef parameterNamesRef;
433 // The list of dynamic properties
434 typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
435 DynamicPropertyList dynamicProperties;
436 // The list of dynamic signals
437 typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
438 DynamicSignalList dynamicSignals;
439 // The list of dynamic slots
440 typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
441 DynamicSlotList dynamicSlots;
444 QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
446 // Used by ComponentCompileState::AliasingObjectsList
447 Object *nextAliasingObject;
448 // Used by ComponentComppileState::IdList
449 Object *nextIdObject;
454 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
458 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
462 #endif // QDECLARATIVEPARSER_P_H