src/declarative/qml/qdeclarativeparser_p.h
changeset 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QDECLARATIVEPARSER_P_H
       
    43 #define QDECLARATIVEPARSER_P_H
       
    44 
       
    45 //
       
    46 //  W A R N I N G
       
    47 //  -------------
       
    48 //
       
    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.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 
       
    56 #include "qdeclarative.h"
       
    57 #include "private/qdeclarativerefcount_p.h"
       
    58 
       
    59 #include <QtCore/qbytearray.h>
       
    60 #include <QtCore/qlist.h>
       
    61 #include <QtCore/qurl.h>
       
    62 #include <QtCore/qstring.h>
       
    63 #include <QtCore/qstringlist.h>
       
    64 
       
    65 #include <private/qobject_p.h>
       
    66 
       
    67 QT_BEGIN_HEADER
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 QT_MODULE(Declarative)
       
    72 
       
    73 namespace QDeclarativeJS { namespace AST { class Node; } }
       
    74 
       
    75 /*
       
    76     XXX
       
    77 
       
    78     These types are created (and owned) by the QDeclarativeXmlParser and consumed by the 
       
    79     QDeclarativeCompiler.  During the compilation phase the compiler will update some of
       
    80     the fields for both its own use and for the use of the upcoming QDeclarativeDom API.
       
    81 
       
    82     The types are part of the generic sounding "QDeclarativeParser" namespace for legacy 
       
    83     reasons (there used to be more in this namespace) and will be cleaned up and
       
    84     migrated into a more appropriate location shortly.
       
    85 */
       
    86 namespace QDeclarativeParser
       
    87 {
       
    88     struct Location 
       
    89     {
       
    90         Location() : line(-1), column(-1) {}
       
    91         int line;
       
    92         int column;
       
    93     };
       
    94 
       
    95     struct LocationRange
       
    96     {
       
    97         LocationRange() : offset(0), length(0) {}
       
    98         quint32 offset;
       
    99         quint32 length;
       
   100     };
       
   101 
       
   102     struct LocationSpan
       
   103     {
       
   104         Location start;
       
   105         Location end;
       
   106         LocationRange range;
       
   107 
       
   108         bool operator<(LocationSpan &o) const {
       
   109             return (start.line < o.start.line) ||
       
   110                    (start.line == o.start.line && start.column < o.start.column);
       
   111         }
       
   112     };
       
   113 
       
   114     class Property;
       
   115     class Object : public QDeclarativeRefCount
       
   116     {
       
   117     public:
       
   118         Object();
       
   119         virtual ~Object(); 
       
   120 
       
   121         // Type of the object.  The integer is an index into the 
       
   122         // QDeclarativeCompiledData::types array, or -1 if the object is a property
       
   123         // group.
       
   124         int type;
       
   125         // The url of this object if it is an external type.  Used by the DOM
       
   126         QUrl url;
       
   127 
       
   128         // version information if type is defined in library or C++
       
   129         int majorVersion;
       
   130         int minorVersion;
       
   131 
       
   132         // The fully-qualified name of this type
       
   133         QByteArray typeName;
       
   134         // The class name
       
   135         QByteArray className;
       
   136         // The id assigned to the object (if any).  Set by the QDeclarativeCompiler
       
   137         QString id;
       
   138         // The id index assigned to the object (if any).  Set by the QDeclarativeCompiler
       
   139         int idIndex;
       
   140         // Custom parsed data
       
   141         QByteArray custom;
       
   142         // Bit mask of the properties assigned bindings
       
   143         QByteArray bindingBitmask; 
       
   144         void setBindingBit(int);
       
   145         // Returns the metaobject for this type, or 0 if not available.  
       
   146         // Internally selectd between the metatype and extObject variables
       
   147         const QMetaObject *metaObject() const;
       
   148 
       
   149         // The compile time metaobject for this type
       
   150         const QMetaObject *metatype;
       
   151         // The synthesized metaobject, if QML added signals or properties to
       
   152         // this type.  Otherwise null
       
   153         QAbstractDynamicMetaObject extObject;
       
   154         QByteArray metadata; // Generated by compiler
       
   155         QByteArray synthdata; // Generated by compiler
       
   156 
       
   157         Property *getDefaultProperty();
       
   158         Property *getProperty(const QByteArray &name, bool create=true);
       
   159 
       
   160         Property *defaultProperty;
       
   161         QHash<QByteArray, Property *> properties;
       
   162 
       
   163         // Output of the compilation phase (these properties continue to exist
       
   164         // in either the defaultProperty or properties members too)
       
   165         void addValueProperty(Property *);
       
   166         void addSignalProperty(Property *);
       
   167         void addAttachedProperty(Property *);
       
   168         void addGroupedProperty(Property *);
       
   169         void addValueTypeProperty(Property *);
       
   170         void addScriptStringProperty(Property *, int = 0);
       
   171         QList<Property *> valueProperties;
       
   172         QList<Property *> signalProperties;
       
   173         QList<Property *> attachedProperties;
       
   174         QList<Property *> groupedProperties;
       
   175         QList<Property *> valueTypeProperties;
       
   176         QList<QPair<Property *, int> > scriptStringProperties;
       
   177 
       
   178         // Script blocks that were nested under this object
       
   179         struct ScriptBlock {
       
   180             enum Pragma { 
       
   181                 None   = 0x00000000,
       
   182                 Shared = 0x00000001
       
   183             };
       
   184             Q_DECLARE_FLAGS(Pragmas, Pragma)
       
   185 
       
   186             QStringList codes;
       
   187             QStringList files;
       
   188             QList<int> lineNumbers;
       
   189             QList<Pragmas> pragmas;
       
   190         };
       
   191 
       
   192         // The bytes to cast instances by to get to the QDeclarativeParserStatus 
       
   193         // interface.  -1 indicates the type doesn't support this interface.
       
   194         // Set by the QDeclarativeCompiler.
       
   195         int parserStatusCast;
       
   196 
       
   197         LocationSpan location;
       
   198 
       
   199         struct DynamicProperty {
       
   200             DynamicProperty();
       
   201             DynamicProperty(const DynamicProperty &);
       
   202 
       
   203             enum Type { Variant, Int, Bool, Real, String, Url, Color, Time, Date, DateTime, Alias, Custom, CustomList };
       
   204 
       
   205             bool isDefaultProperty;
       
   206             Type type;
       
   207             QByteArray customType;
       
   208             QByteArray name;
       
   209             QDeclarativeParser::Property *defaultValue;
       
   210             LocationSpan location;
       
   211         };
       
   212         struct DynamicSignal {
       
   213             DynamicSignal();
       
   214             DynamicSignal(const DynamicSignal &);
       
   215 
       
   216             QByteArray name;
       
   217             QList<QByteArray> parameterTypes;
       
   218             QList<QByteArray> parameterNames;
       
   219         };
       
   220         struct DynamicSlot {
       
   221             DynamicSlot();
       
   222             DynamicSlot(const DynamicSlot &);
       
   223 
       
   224             QByteArray name;
       
   225             QString body;
       
   226             QList<QByteArray> parameterNames;
       
   227             LocationSpan location;
       
   228         };
       
   229 
       
   230         // The list of dynamic properties
       
   231         QList<DynamicProperty> dynamicProperties;
       
   232         // The list of dynamic signals
       
   233         QList<DynamicSignal> dynamicSignals;
       
   234         // The list of dynamic slots
       
   235         QList<DynamicSlot> dynamicSlots;
       
   236     };
       
   237 
       
   238     class Q_DECLARATIVE_EXPORT Variant 
       
   239     {
       
   240     public:
       
   241         enum Type {
       
   242             Invalid,
       
   243             Boolean,
       
   244             Number,
       
   245             String,
       
   246             Script
       
   247         };
       
   248 
       
   249         Variant();
       
   250         Variant(const Variant &);
       
   251         Variant(bool);
       
   252         Variant(double, const QString &asWritten=QString());
       
   253         Variant(const QString &);
       
   254         Variant(const QString &, QDeclarativeJS::AST::Node *);
       
   255         Variant &operator=(const Variant &);
       
   256 
       
   257         Type type() const;
       
   258 
       
   259         bool isBoolean() const { return type() == Boolean; }
       
   260         bool isNumber() const { return type() == Number; }
       
   261         bool isString() const { return type() == String; }
       
   262         bool isScript() const { return type() == Script; }
       
   263         bool isStringList() const;
       
   264 
       
   265         bool asBoolean() const;
       
   266         QString asString() const;
       
   267         double asNumber() const;
       
   268         QString asScript() const;
       
   269         QDeclarativeJS::AST::Node *asAST() const;
       
   270         QStringList asStringList() const;
       
   271 
       
   272     private:
       
   273         Type t;
       
   274         union {
       
   275             bool b;
       
   276             double d;
       
   277             QDeclarativeJS::AST::Node *n;
       
   278         };
       
   279         QString s;
       
   280     };
       
   281 
       
   282     class Value : public QDeclarativeRefCount
       
   283     {
       
   284     public:
       
   285         Value();
       
   286         virtual ~Value();
       
   287 
       
   288         enum Type {
       
   289             // The type of this value assignment is not yet known
       
   290             Unknown,
       
   291             // This is used as a literal property assignment
       
   292             Literal,
       
   293             // This is used as a property binding assignment
       
   294             PropertyBinding,
       
   295             // This is used as a QDeclarativePropertyValueSource assignment
       
   296             ValueSource,
       
   297             // This is used as a QDeclarativePropertyValueInterceptor assignment
       
   298             ValueInterceptor,
       
   299             // This is used as a property QObject assignment
       
   300             CreatedObject,
       
   301             // This is used as a signal object assignment
       
   302             SignalObject,
       
   303             // This is used as a signal expression assignment
       
   304             SignalExpression,
       
   305             // This is used as an id assignment only
       
   306             Id
       
   307         };
       
   308         Type type;
       
   309 
       
   310         // ### Temporary (for id only)
       
   311         QString primitive() const { return value.isString() ? value.asString() : value.asScript(); }
       
   312 
       
   313         // Primitive value
       
   314         Variant value;
       
   315         // Object value
       
   316         Object *object;
       
   317 
       
   318         LocationSpan location;
       
   319     };
       
   320 
       
   321     class Property : public QDeclarativeRefCount
       
   322     {
       
   323     public:
       
   324         Property();
       
   325         Property(const QByteArray &n);
       
   326         virtual ~Property();
       
   327 
       
   328         // The Object to which this property is attached
       
   329         Object *parent;
       
   330 
       
   331         Object *getValue(const LocationSpan &);
       
   332         void addValue(Value *v);
       
   333         void addOnValue(Value *v);
       
   334 
       
   335         // The QVariant::Type of the property, or 0 (QVariant::Invalid) if 
       
   336         // unknown.
       
   337         int type;
       
   338         // The metaobject index of this property, or -1 if unknown.
       
   339         int index;
       
   340 
       
   341         // Returns true if this is an empty property - both value and values
       
   342         // are unset.
       
   343         bool isEmpty() const;
       
   344         // The list of values assigned to this property.  Content in values
       
   345         // and value are mutually exclusive
       
   346         QList<Value *> values;
       
   347         // The list of values assigned to this property using the "on" syntax
       
   348         QList<Value *> onValues;
       
   349         // The accessed property.  This is used to represent dot properties.
       
   350         // Content in value and values are mutually exclusive.
       
   351         Object *value;
       
   352         // The property name
       
   353         QByteArray name;
       
   354         // True if this property was accessed as the default property.  
       
   355         bool isDefault;
       
   356         // True if the setting of this property will be deferred.  Set by the
       
   357         // QDeclarativeCompiler
       
   358         bool isDeferred;
       
   359         // True if this property is a value-type psuedo-property
       
   360         bool isValueTypeSubProperty;
       
   361 
       
   362         LocationSpan location;
       
   363         LocationRange listValueRange;
       
   364         QList<int> listCommaPositions;
       
   365     };
       
   366 }
       
   367 
       
   368 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeParser::Object::ScriptBlock::Pragmas);
       
   369 
       
   370 QT_END_NAMESPACE
       
   371 
       
   372 Q_DECLARE_METATYPE(QDeclarativeParser::Variant)
       
   373 
       
   374 QT_END_HEADER
       
   375 
       
   376 #endif // QDECLARATIVEPARSER_P_H