1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #ifndef QQMLSCRIPT_P_H
42 #define QQMLSCRIPT_P_H
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.
55 #include <QtQml/qqmlerror.h>
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>
63 #include <QtCore/QList>
64 #include <QtCore/QUrl>
72 class QQmlPropertyCache;
73 namespace QQmlJS { namespace AST { class Node; class StringLiteral; } }
74 namespace QQmlCompilerTypes { struct BindingReference; struct ComponentCompileState; }
76 namespace QQmlScript {
80 Location() : line(-1), column(-1) {}
84 inline bool operator<(const Location &other) {
85 return line < other.line ||
86 (line == other.line && column < other.column);
92 LocationRange() : offset(0), length(0) {}
103 bool operator<(LocationSpan &o) const {
104 return (start.line < o.start.line) ||
105 (start.line == o.start.line && start.column < o.start.column);
112 Import() : type(Library), majorVersion(-1), minorVersion(-1) {}
114 enum Type { Library, File, Script };
123 QQmlScript::LocationSpan location;
127 class TypeReference : public QQmlPool::Class
130 // type as it has been referenced in Qml
132 // The first use of this type in the parse tree. Useful for error locations.
133 QQmlScript::Object *firstUse;
139 class Q_QML_EXPORT 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 &);
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;
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;
178 QQmlJS::AST::StringLiteral *l;
179 QQmlJS::AST::Node *n;
181 QStringRef asWritten;
184 class Value : public QQmlPool::POD
190 // The type of this value assignment is not yet known
192 // This is used as a literal property assignment
194 // This is used as a property binding assignment
196 // This is used as a QQmlPropertyValueSource assignment
198 // This is used as a QQmlPropertyValueInterceptor assignment
200 // This is used as a property QObject assignment
202 // This is used as a signal object assignment
204 // This is used as a signal expression assignment
206 // This is used as an id assignment only
211 // ### Temporary (for id only)
212 QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
219 LocationSpan location;
223 QQmlCompilerTypes::BindingReference *bindingReference;
224 int signalExpressionContextStack;
227 // Used in Property::ValueList lists
231 class Property : public QQmlPool::POD
236 // The Object to which this property is attached
239 Object *getValue(const LocationSpan &);
240 void addValue(Value *v);
241 void addOnValue(Value *v);
243 // The QVariant::Type of the property, or 0 (QVariant::Invalid) if
246 // The metaobject index of this property, or -1 if unknown.
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;
253 // Returns true if this is an empty property - both value and values
255 bool isEmpty() const;
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
261 // The list of values assigned to this property using the "on" syntax
263 // The accessed property. This is used to represent dot properties.
264 // Content in value and values are mutually exclusive.
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.
272 // True if the setting of this property will be deferred. Set by the
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
280 // True if this is a readonly property declaration
281 bool isReadOnlyDeclaration;
283 // Used for scriptStringProperties
284 int scriptStringScope;
286 LocationSpan location;
287 LocationRange listValueRange;
289 // Used in Object::MainPropertyList
290 Property *nextMainProperty;
292 // Used in Object::PropertyList lists
293 Property *nextProperty;
297 QHashedStringRef _name;
300 class Object : public QQmlPool::Class
306 // Type of the object. The integer is an index into the
307 // QQmlCompiledData::types array, or -1 if the object is a property
310 // A back pointer to the QQmlScript::TypeReference for this type, if any.
311 // Set by the parser.
312 TypeReference *typeReference;
314 // The id assigned to the object (if any). Set by the QQmlCompiler
316 // The id index assigned to the object (if any). Set by the QQmlCompiler
318 // Custom parsed data
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;
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
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);
342 Property *defaultProperty;
344 typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
345 MainPropertyList properties;
346 QHashField propertiesHashField;
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 *);
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;
365 // Script blocks that were nested under this object
371 Q_DECLARE_FLAGS(Pragmas, Pragma)
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;
383 LocationSpan location;
385 struct DynamicProperty : public QQmlPool::POD
389 enum Type { Var, Variant, Int, Bool, Real, String, Url, Color,
390 Time, Date, DateTime, Rect, Alias, Custom, CustomList };
392 quint32 isDefaultProperty:1;
393 quint32 isReadOnly:1;
397 QHashedStringRef customType;
398 QHashedStringRef name;
399 QQmlScript::Property *defaultValue;
400 LocationSpan location;
401 Location nameLocation;
403 // Used by Object::DynamicPropertyList
404 DynamicProperty *nextProperty;
406 // Used by the compiler
407 QFastMetaBuilder::StringRef nameRef;
408 QFastMetaBuilder::StringRef changedNameRef;
411 struct DynamicSignal : public QQmlPool::POD
415 QHashedStringRef name;
416 QQmlPool::List<DynamicProperty::Type> parameterTypes;
417 QQmlPool::List<QHashedStringRef> parameterNames;
419 // Used by Object::DynamicSignalList
420 DynamicSignal *nextSignal;
422 // Used by the compiler
423 QFastMetaBuilder::StringRef nameRef;
424 QQmlPool::List<QFastMetaBuilder::StringRef> parameterNamesRef;
425 LocationSpan location;
428 struct DynamicSlot : public QQmlPool::Class
432 QHashedStringRef name;
434 QList<QByteArray> parameterNames;
435 LocationSpan location;
437 int parameterNamesLength() const;
439 // Used by Object::DynamicSlotList
440 DynamicSlot *nextSlot;
442 // Used by the compiler
443 QFastMetaBuilder::StringRef nameRef;
444 QQmlPool::List<QFastMetaBuilder::StringRef> parameterNamesRef;
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;
457 int aggregateDynamicSignalParameterCount() const;
458 int aggregateDynamicSlotParameterCount() const;
461 QQmlCompilerTypes::ComponentCompileState *componentCompileState;
463 // Used by ComponentCompileState::AliasingObjectsList
464 Object *nextAliasingObject;
465 // Used by ComponentComppileState::IdList
466 Object *nextIdObject;
469 class ParserJsASTData;
470 class Q_QML_EXPORT Parser
476 bool parse(const QString &data, const QByteArray &preparseData,
477 const QUrl &url = QUrl(), const QString &urlString = QString());
479 QByteArray preparseData() const;
481 QList<TypeReference*> referencedTypes() const;
483 QQmlScript::Object *tree() const;
484 QList<Import> imports() const;
488 QList<QQmlError> errors() const;
490 class JavaScriptMetaData {
493 : pragmas(QQmlScript::Object::ScriptBlock::None) {}
495 QQmlScript::Object::ScriptBlock::Pragmas pragmas;
496 QList<Import> imports;
499 static QQmlScript::Object::ScriptBlock::Pragmas extractPragmas(QString &);
500 static JavaScriptMetaData extractMetaData(QString &);
504 int findOrCreateTypeId(const QString &name, Object *);
505 void setTree(QQmlScript::Object *tree);
507 void setScriptFile(const QString &filename) {_scriptFile = filename; }
508 QString scriptFile() const { return _scriptFile; }
511 QList<QQmlError> _errors;
514 QQmlScript::Object *root;
515 QList<Import> _imports;
516 QList<TypeReference*> _refTypes;
518 ParserJsASTData *data;
523 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlScript::Object::ScriptBlock::Pragmas);
527 Q_DECLARE_METATYPE(QQmlScript::Variant)
531 #endif // QQMLSCRIPT_P_H