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 QtDeclarative 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 QDECLARATIVESCRIPT_P_H
42 #define QDECLARATIVESCRIPT_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 <QtDeclarative/qdeclarativeerror.h>
57 #include <private/qfieldlist_p.h>
58 #include <private/qhashfield_p.h>
59 #include <private/qfastmetabuilder_p.h>
60 #include <private/qdeclarativepool_p.h>
61 #include <private/qdeclarativepropertycache_p.h>
63 #include <QtCore/QList>
64 #include <QtCore/QUrl>
72 class QDeclarativePropertyCache;
73 namespace QDeclarativeJS { namespace AST { class Node; class StringLiteral; } }
74 namespace QDeclarativeCompilerTypes { struct BindingReference; struct ComponentCompileState; }
76 namespace QDeclarativeScript {
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) {}
114 enum Type { Library, File, Script };
121 void extractVersion(int *maj, int *min) const;
123 QDeclarativeScript::LocationSpan location;
130 TypeReference(int typeId, const QString &typeName) : id(typeId), name(typeName) {}
133 // type as it has been referenced in Qml
135 // objects in parse tree referencing the type
136 QList<QDeclarativeScript::Object*> refObjects;
142 class Q_DECLARATIVE_EXPORT Variant
154 Variant(const Variant &);
155 explicit Variant(bool);
156 explicit Variant(double, const QStringRef &asWritten = QStringRef());
157 explicit Variant(QDeclarativeJS::AST::StringLiteral *);
158 explicit Variant(const QStringRef &asWritten, QDeclarativeJS::AST::Node *);
159 Variant &operator=(const Variant &);
163 bool isBoolean() const { return type() == Boolean; }
164 bool isNumber() const { return type() == Number; }
165 bool isString() const { return type() == String; }
166 bool isScript() const { return type() == Script; }
167 bool isStringList() const;
169 bool asBoolean() const;
170 QString asString() const;
171 double asNumber() const;
172 QString asScript() const;
173 QDeclarativeJS::AST::Node *asAST() const;
174 QStringList asStringList() const;
181 QDeclarativeJS::AST::StringLiteral *l;
182 QDeclarativeJS::AST::Node *n;
184 QStringRef asWritten;
187 class Value : public QDeclarativePool::POD
193 // The type of this value assignment is not yet known
195 // This is used as a literal property assignment
197 // This is used as a property binding assignment
199 // This is used as a QDeclarativePropertyValueSource assignment
201 // This is used as a QDeclarativePropertyValueInterceptor assignment
203 // This is used as a property QObject assignment
205 // This is used as a signal object assignment
207 // This is used as a signal expression assignment
209 // This is used as an id assignment only
214 // ### Temporary (for id only)
215 QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
222 LocationSpan location;
226 QDeclarativeCompilerTypes::BindingReference *bindingReference;
227 int signalExpressionContextStack;
230 // Used in Property::ValueList lists
234 class Property : public QDeclarativePool::POD
239 // The Object to which this property is attached
242 Object *getValue(const LocationSpan &);
243 void addValue(Value *v);
244 void addOnValue(Value *v);
246 // The QVariant::Type of the property, or 0 (QVariant::Invalid) if
249 // The metaobject index of this property, or -1 if unknown.
251 // The core data in the case of a regular property.
252 // XXX This has to be a value now as the synthCache may change during
253 // compilation which invalidates pointers. We should fix this.
254 QDeclarativePropertyData core;
256 // Returns true if this is an empty property - both value and values
258 bool isEmpty() const;
260 typedef QFieldList<Value, &Value::nextValue> ValueList;
261 // The list of values assigned to this property. Content in values
262 // and value are mutually exclusive
264 // The list of values assigned to this property using the "on" syntax
266 // The accessed property. This is used to represent dot properties.
267 // Content in value and values are mutually exclusive.
270 const QHashedStringRef &name() const { return _name; }
271 void setName(const QString &n) { _name = QHashedStringRef(pool()->NewString(n)); }
272 void setName(const QHashedStringRef &n) { _name = n; }
273 // True if this property was accessed as the default property.
275 // True if the setting of this property will be deferred. Set by the
276 // QDeclarativeCompiler
278 // True if this property is a value-type pseudo-property
279 bool isValueTypeSubProperty;
280 // True if this property is a property alias. Set by the
281 // QDeclarativeCompiler
283 // True if this is a readonly property declaration
284 bool isReadOnlyDeclaration;
286 // Used for scriptStringProperties
287 int scriptStringScope;
289 LocationSpan location;
290 LocationRange listValueRange;
292 // Used in Object::MainPropertyList
293 Property *nextMainProperty;
295 // Used in Object::PropertyList lists
296 Property *nextProperty;
300 QHashedStringRef _name;
303 class Object : public QDeclarativePool::Class
309 // Type of the object. The integer is an index into the
310 // QDeclarativeCompiledData::types array, or -1 if the object is a property
314 // The fully-qualified name of this type
316 // The id assigned to the object (if any). Set by the QDeclarativeCompiler
318 // The id index assigned to the object (if any). Set by the QDeclarativeCompiler
320 // Custom parsed data
322 // Bit mask of the properties assigned bindings
323 QByteArray bindingBitmask;
324 void setBindingBit(int);
325 // Returns the metaobject for this type, or 0 if not available.
326 // Internally selectd between the metatype and extObject variables
327 const QMetaObject *metaObject() const;
329 // The compile time metaobject for this type
330 const QMetaObject *metatype;
331 // The synthesized metaobject, if QML added signals or properties to
332 // this type. Otherwise null
333 QAbstractDynamicMetaObject extObject;
334 QByteArray metadata; // Generated by compiler
335 QByteArray synthdata; // Generated by compiler
336 QDeclarativePropertyCache *synthCache; // Generated by compiler
338 Property *getDefaultProperty();
339 // name ptr must be guarenteed to remain valid
340 Property *getProperty(const QHashedStringRef &name, bool create=true);
341 Property *getProperty(const QStringRef &name, bool create=true);
342 Property *getProperty(const QString &name, bool create=true);
344 Property *defaultProperty;
346 typedef QFieldList<Property, &Property::nextMainProperty> MainPropertyList;
347 MainPropertyList properties;
348 QHashField propertiesHashField;
350 // Output of the compilation phase (these properties continue to exist
351 // in either the defaultProperty or properties members too)
352 void addValueProperty(Property *);
353 void addSignalProperty(Property *);
354 void addAttachedProperty(Property *);
355 void addGroupedProperty(Property *);
356 void addValueTypeProperty(Property *);
357 void addScriptStringProperty(Property *);
359 typedef QFieldList<Property, &Property::nextProperty> PropertyList;
360 PropertyList valueProperties;
361 PropertyList signalProperties;
362 PropertyList attachedProperties;
363 PropertyList groupedProperties;
364 PropertyList valueTypeProperties;
365 PropertyList scriptStringProperties;
367 // Script blocks that were nested under this object
373 Q_DECLARE_FLAGS(Pragmas, Pragma)
380 // The bytes to cast instances by to get to the QDeclarativeParserStatus
381 // interface. -1 indicates the type doesn't support this interface.
382 // Set by the QDeclarativeCompiler.
383 int parserStatusCast;
385 LocationSpan location;
387 struct DynamicProperty : public QDeclarativePool::POD
391 enum Type { Var, Variant, Int, Bool, Real, String, Url, Color,
392 Time, Date, DateTime, Alias, Custom, CustomList };
394 quint32 isDefaultProperty:1;
395 quint32 isReadOnly:1;
399 QHashedStringRef customType;
400 QHashedStringRef name;
401 QDeclarativeScript::Property *defaultValue;
402 LocationSpan location;
403 Location nameLocation;
405 // Used by Object::DynamicPropertyList
406 DynamicProperty *nextProperty;
408 // Used by the compiler
409 QByteArray *resolvedCustomTypeName;
410 QFastMetaBuilder::StringRef typeRef;
411 QFastMetaBuilder::StringRef nameRef;
412 QFastMetaBuilder::StringRef changedSignatureRef;
415 struct DynamicSignal : public QDeclarativePool::POD
419 QHashedStringRef name;
420 QDeclarativePool::List<QHashedCStringRef> parameterTypes;
421 QDeclarativePool::List<QHashedStringRef> parameterNames;
423 int parameterTypesLength() const;
424 int parameterNamesLength() const;
426 // Used by Object::DynamicSignalList
427 DynamicSignal *nextSignal;
429 // Used by the compiler
430 QFastMetaBuilder::StringRef signatureRef;
431 QFastMetaBuilder::StringRef parameterNamesRef;
432 LocationSpan location;
435 struct DynamicSlot : public QDeclarativePool::Class
439 QHashedStringRef name;
441 QList<QByteArray> parameterNames;
442 LocationSpan location;
444 int parameterNamesLength() const;
446 // Used by Object::DynamicSlotList
447 DynamicSlot *nextSlot;
449 // Used by the compiler
450 QFastMetaBuilder::StringRef signatureRef;
451 QFastMetaBuilder::StringRef parameterNamesRef;
454 // The list of dynamic properties
455 typedef QFieldList<DynamicProperty, &DynamicProperty::nextProperty> DynamicPropertyList;
456 DynamicPropertyList dynamicProperties;
457 // The list of dynamic signals
458 typedef QFieldList<DynamicSignal, &DynamicSignal::nextSignal> DynamicSignalList;
459 DynamicSignalList dynamicSignals;
460 // The list of dynamic slots
461 typedef QFieldList<DynamicSlot, &DynamicSlot::nextSlot> DynamicSlotList;
462 DynamicSlotList dynamicSlots;
465 QDeclarativeCompilerTypes::ComponentCompileState *componentCompileState;
467 // Used by ComponentCompileState::AliasingObjectsList
468 Object *nextAliasingObject;
469 // Used by ComponentComppileState::IdList
470 Object *nextIdObject;
473 class ParserJsASTData;
474 class Q_AUTOTEST_EXPORT Parser
480 bool parse(const QByteArray &data, const QUrl &url = QUrl());
482 QList<TypeReference*> referencedTypes() const;
484 QDeclarativeScript::Object *tree() const;
485 QList<Import> imports() const;
489 QList<QDeclarativeError> errors() const;
491 class JavaScriptMetaData {
494 : pragmas(QDeclarativeScript::Object::ScriptBlock::None) {}
496 QDeclarativeScript::Object::ScriptBlock::Pragmas pragmas;
497 QList<Import> imports;
500 static QDeclarativeScript::Object::ScriptBlock::Pragmas extractPragmas(QString &);
501 static JavaScriptMetaData extractMetaData(QString &);
505 TypeReference *findOrCreateType(const QString &name);
506 void setTree(QDeclarativeScript::Object *tree);
508 void setScriptFile(const QString &filename) {_scriptFile = filename; }
509 QString scriptFile() const { return _scriptFile; }
512 QList<QDeclarativeError> _errors;
514 QDeclarativePool _pool;
515 QDeclarativeScript::Object *root;
516 QList<Import> _imports;
517 QList<TypeReference*> _refTypes;
519 ParserJsASTData *data;
524 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeScript::Object::ScriptBlock::Pragmas);
528 Q_DECLARE_METATYPE(QDeclarativeScript::Variant)
532 #endif // QDECLARATIVESCRIPT_P_H