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/qqmlpool_p.h>
60 #include <private/qqmlpropertycache_p.h>
62 #include <QtCore/QList>
63 #include <QtCore/QUrl>
71 class QQmlPropertyCache;
72 namespace QQmlJS { namespace AST { class Node; class StringLiteral; } }
73 namespace QQmlCompilerTypes { struct BindingReference; struct ComponentCompileState; }
75 namespace QQmlScript {
79 Location() : line(-1), column(-1) {}
83 inline bool operator<(const Location &other) {
84 return line < other.line ||
85 (line == other.line && column < other.column);
91 LocationRange() : offset(0), length(0) {}
102 bool operator<(LocationSpan &o) const {
103 return (start.line < o.start.line) ||
104 (start.line == o.start.line && start.column < o.start.column);
111 Import() : type(Library), majorVersion(-1), minorVersion(-1) {}
113 enum Type { Library, File, Script };
122 QQmlScript::LocationSpan location;
126 class TypeReference : public QQmlPool::Class
129 // type as it has been referenced in Qml
131 // The first use of this type in the parse tree. Useful for error locations.
132 QQmlScript::Object *firstUse;
138 class Q_QML_PRIVATE_EXPORT Variant
150 Variant(const Variant &);
151 explicit Variant(bool);
152 explicit Variant(double, const QStringRef &asWritten = QStringRef());
153 explicit Variant(QQmlJS::AST::StringLiteral *);
154 explicit Variant(const QStringRef &asWritten, QQmlJS::AST::Node *);
155 Variant &operator=(const Variant &);
159 bool isBoolean() const { return type() == Boolean; }
160 bool isNumber() const { return type() == Number; }
161 bool isString() const { return type() == String; }
162 bool isScript() const { return type() == Script; }
163 bool isStringList() const;
165 bool asBoolean() const;
166 QString asString() const;
167 double asNumber() const;
168 QString asScript() const;
169 QQmlJS::AST::Node *asAST() const;
170 QStringList asStringList() const;
177 QQmlJS::AST::StringLiteral *l;
178 QQmlJS::AST::Node *n;
180 QStringRef asWritten;
183 class Value : public QQmlPool::POD
189 // The type of this value assignment is not yet known
191 // This is used as a literal property assignment
193 // This is used as a property binding assignment
195 // This is used as a QQmlPropertyValueSource assignment
197 // This is used as a QQmlPropertyValueInterceptor assignment
199 // This is used as a property QObject assignment
201 // This is used as a signal object assignment
203 // This is used as a signal expression assignment
205 // This is used as an id assignment only
210 // ### Temporary (for id only)
211 QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
218 LocationSpan location;
222 QQmlCompilerTypes::BindingReference *bindingReference;
223 int signalExpressionContextStack;
226 // Used in Property::ValueList lists
230 class Property : public QQmlPool::POD
235 // The Object to which this property is attached
238 Object *getValue(const LocationSpan &);
239 void addValue(Value *v);
240 void addOnValue(Value *v);
242 // The QVariant::Type of the property, or 0 (QVariant::Invalid) if
245 // The metaobject index of this property, or -1 if unknown.
247 // The core data in the case of a regular property.
248 // XXX This has to be a value now as the synthCache may change during
249 // compilation which invalidates pointers. We should fix this.
250 QQmlPropertyData core;
252 // Returns true if this is an empty property - both value and values
254 bool isEmpty() const;
256 typedef QFieldList<Value, &Value::nextValue> ValueList;
257 // The list of values assigned to this property. Content in values
258 // and value are mutually exclusive
260 // The list of values assigned to this property using the "on" syntax
262 // The accessed property. This is used to represent dot properties.
263 // Content in value and values are mutually exclusive.
266 const QHashedStringRef &name() const { return _name; }
267 void setName(const QString &n) { _name = QHashedStringRef(pool()->NewString(n)); }
268 void setName(const QHashedStringRef &n) { _name = n; }
269 // True if this property was accessed as the default property.
271 // True if the setting of this property will be deferred. Set by the
274 // True if this property is a value-type pseudo-property
275 bool isValueTypeSubProperty;
276 // True if this property is a property alias. Set by the
279 // True if this is a readonly property declaration
280 bool isReadOnlyDeclaration;
282 // Used for scriptStringProperties
283 int scriptStringScope;
285 LocationSpan location;
286 LocationRange listValueRange;
288 // Used in Object::MainPropertyList
289 Property *nextMainProperty;
291 // Used in Object::PropertyList lists
292 Property *nextProperty;
296 QHashedStringRef _name;
299 class Object : public QQmlPool::Class
305 // Type of the object. The integer is an index into the
306 // QQmlCompiledData::types array, or -1 if the object is a property
309 // A back pointer to the QQmlScript::TypeReference for this type, if any.
310 // Set by the parser.
311 TypeReference *typeReference;
313 // The id assigned to the object (if any). Set by the QQmlCompiler
315 // The id index assigned to the object (if any). Set by the QQmlCompiler
317 // Custom parsed data
319 // Bit mask of the properties assigned bindings
320 QByteArray bindingBitmask;
321 void setBindingBit(int);
323 QQmlPropertyCache *metatype;
325 // The synthesized metaobject, if QML added signals or properties to
326 // this type. Otherwise null
327 QByteArray synthdata; // Generated by compiler
328 QQmlPropertyCache *synthCache; // Generated by compiler
330 Property *getDefaultProperty();
331 // name ptr must be guarenteed to remain valid
332 Property *getProperty(const QHashedStringRef &name, bool create=true);
333 Property *getProperty(const QStringRef &name, bool create=true);
334 Property *getProperty(const QString &name, bool create=true);
336 Property *defaultProperty;
338 typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
339 MainPropertyList properties;
340 QHashField propertiesHashField;
342 // Output of the compilation phase (these properties continue to exist
343 // in either the defaultProperty or properties members too)
344 void addValueProperty(Property *);
345 void addSignalProperty(Property *);
346 void addAttachedProperty(Property *);
347 void addGroupedProperty(Property *);
348 void addValueTypeProperty(Property *);
349 void addScriptStringProperty(Property *);
351 typedef QFieldList<Property, &Property::nextProperty> PropertyList;
352 PropertyList valueProperties;
353 PropertyList signalProperties;
354 PropertyList attachedProperties;
355 PropertyList groupedProperties;
356 PropertyList valueTypeProperties;
357 PropertyList scriptStringProperties;
359 // Script blocks that were nested under this object
365 Q_DECLARE_FLAGS(Pragmas, Pragma)
372 // The bytes to cast instances by to get to the QQmlParserStatus
373 // interface. -1 indicates the type doesn't support this interface.
374 // Set by the QQmlCompiler.
375 int parserStatusCast;
377 LocationSpan location;
379 struct DynamicProperty : public QQmlPool::POD
383 enum Type { Var, Variant, Int, Bool, Real, String, Url, Color,
384 Font, Time, Date, DateTime, Rect, Point, Size,
385 Vector2D, Vector3D, Vector4D, Matrix4x4, Quaternion,
386 Alias, Custom, CustomList };
388 quint32 isDefaultProperty:1;
389 quint32 isReadOnly:1;
393 QHashedStringRef customType;
394 QHashedStringRef name;
395 QQmlScript::Property *defaultValue;
396 LocationSpan location;
397 Location nameLocation;
399 // Used by Object::DynamicPropertyList
400 DynamicProperty *nextProperty;
402 // Used by the compiler
403 int nameIndex; // Points at the name and name + "Changed()" strings
406 struct DynamicSignal : public QQmlPool::POD
410 QHashedStringRef name;
411 QQmlPool::List<DynamicProperty::Type> parameterTypes;
412 QQmlPool::List<QHashedStringRef> parameterNames;
414 // Used by Object::DynamicSignalList
415 DynamicSignal *nextSignal;
417 // Used by the compiler
419 LocationSpan location;
422 struct DynamicSlot : public QQmlPool::Class
426 QHashedStringRef name;
428 QList<QByteArray> parameterNames;
429 LocationSpan location;
431 int parameterNamesLength() const;
433 // Used by Object::DynamicSlotList
434 DynamicSlot *nextSlot;
436 // Used by the compiler
440 // The list of dynamic properties
441 typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
442 DynamicPropertyList dynamicProperties;
443 // The list of dynamic signals
444 typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
445 DynamicSignalList dynamicSignals;
446 // The list of dynamic slots
447 typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
448 DynamicSlotList dynamicSlots;
450 int aggregateDynamicSignalParameterCount() const;
451 int aggregateDynamicSlotParameterCount() const;
454 QQmlCompilerTypes::ComponentCompileState *componentCompileState;
456 // Used by ComponentCompileState::AliasingObjectsList
457 Object *nextAliasingObject;
458 // Used by ComponentComppileState::IdList
459 Object *nextIdObject;
462 class ParserJsASTData;
463 class Q_QML_PRIVATE_EXPORT Parser
469 bool parse(const QString &data, const QByteArray &preparseData,
470 const QUrl &url = QUrl(), const QString &urlString = QString());
472 QByteArray preparseData() const;
474 QList<TypeReference*> referencedTypes() const;
476 QQmlScript::Object *tree() const;
477 QList<Import> imports() const;
481 QList<QQmlError> errors() const;
483 class JavaScriptMetaData {
486 : pragmas(QQmlScript::Object::ScriptBlock::None) {}
488 QQmlScript::Object::ScriptBlock::Pragmas pragmas;
489 QList<Import> imports;
492 static QQmlScript::Object::ScriptBlock::Pragmas extractPragmas(QString &);
493 static JavaScriptMetaData extractMetaData(QString &);
497 int findOrCreateTypeId(const QString &name, Object *);
498 void setTree(QQmlScript::Object *tree);
500 void setScriptFile(const QString &filename) {_scriptFile = filename; }
501 QString scriptFile() const { return _scriptFile; }
504 QList<QQmlError> _errors;
507 QQmlScript::Object *root;
508 QList<Import> _imports;
509 QList<TypeReference*> _refTypes;
511 ParserJsASTData *data;
516 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlScript::Object::ScriptBlock::Pragmas);
520 Q_DECLARE_METATYPE(QQmlScript::Variant)
524 #endif // QQMLSCRIPT_P_H