src/declarative/qml/qdeclarativecompiler_p.h
changeset 30 5dc02b23752f
child 33 3e2da88830cd
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 QDECLARATIVECOMPILER_P_H
       
    43 #define QDECLARATIVECOMPILER_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 "qdeclarativeerror.h"
       
    58 #include "private/qdeclarativeinstruction_p.h"
       
    59 #include "private/qdeclarativecompositetypemanager_p.h"
       
    60 #include "private/qdeclarativeparser_p.h"
       
    61 #include "private/qdeclarativeengine_p.h"
       
    62 #include "private/qbitfield_p.h"
       
    63 #include "private/qdeclarativepropertycache_p.h"
       
    64 #include "private/qdeclarativeintegercache_p.h"
       
    65 #include "private/qdeclarativetypenamecache_p.h"
       
    66 
       
    67 #include <QtCore/qbytearray.h>
       
    68 #include <QtCore/qset.h>
       
    69 #include <QtCore/QCoreApplication>
       
    70 
       
    71 QT_BEGIN_NAMESPACE
       
    72 
       
    73 class QDeclarativeEngine;
       
    74 class QDeclarativeComponent;
       
    75 class QDeclarativeContext;
       
    76 class QDeclarativeContextData;
       
    77 
       
    78 class QScriptProgram;
       
    79 class Q_AUTOTEST_EXPORT QDeclarativeCompiledData : public QDeclarativeRefCount, public QDeclarativeCleanup
       
    80 {
       
    81 public:
       
    82     QDeclarativeCompiledData(QDeclarativeEngine *engine);
       
    83     virtual ~QDeclarativeCompiledData();
       
    84 
       
    85     QString name;
       
    86     QUrl url;
       
    87     QDeclarativeTypeNameCache *importCache;
       
    88 
       
    89     struct TypeReference 
       
    90     {
       
    91         TypeReference()
       
    92         : type(0), component(0), ref(0) {}
       
    93 
       
    94         QByteArray className;
       
    95         QDeclarativeType *type;
       
    96         QDeclarativeComponent *component;
       
    97 
       
    98         QDeclarativeRefCount *ref;
       
    99         QObject *createInstance(QDeclarativeContextData *, const QBitField &) const;
       
   100         const QMetaObject *metaObject() const;
       
   101     };
       
   102     QList<TypeReference> types;
       
   103     struct CustomTypeData
       
   104     {
       
   105         int index;
       
   106         int type;
       
   107     };
       
   108 
       
   109     const QMetaObject *root;
       
   110     QAbstractDynamicMetaObject rootData;
       
   111     QDeclarativePropertyCache *rootPropertyCache;
       
   112     QList<QString> primitives;
       
   113     QList<float> floatData;
       
   114     QList<int> intData;
       
   115     QList<CustomTypeData> customTypeData;
       
   116     QList<QByteArray> datas;
       
   117     QList<QDeclarativeParser::Location> locations;
       
   118     QList<QDeclarativeInstruction> bytecode;
       
   119     QList<QScriptProgram *> cachedPrograms;
       
   120     QList<QScriptValue *> cachedClosures;
       
   121     QList<QDeclarativePropertyCache *> propertyCaches;
       
   122     QList<QDeclarativeIntegerCache *> contextCaches;
       
   123     QList<QDeclarativeParser::Object::ScriptBlock> scripts;
       
   124     QList<QUrl> urls;
       
   125 
       
   126     void dumpInstructions();
       
   127 
       
   128 protected:
       
   129     virtual void clear(); // From QDeclarativeCleanup
       
   130 
       
   131 private:
       
   132     void dump(QDeclarativeInstruction *, int idx = -1);
       
   133     QDeclarativeCompiledData(const QDeclarativeCompiledData &other);
       
   134     QDeclarativeCompiledData &operator=(const QDeclarativeCompiledData &other);
       
   135     QByteArray packData;
       
   136     friend class QDeclarativeCompiler;
       
   137     int pack(const char *, size_t);
       
   138 
       
   139     int indexForString(const QString &);
       
   140     int indexForByteArray(const QByteArray &);
       
   141     int indexForFloat(float *, int);
       
   142     int indexForInt(int *, int);
       
   143     int indexForLocation(const QDeclarativeParser::Location &);
       
   144     int indexForLocation(const QDeclarativeParser::LocationSpan &);
       
   145     int indexForUrl(const QUrl &);
       
   146 };
       
   147 
       
   148 class QMetaObjectBuilder;
       
   149 class Q_DECLARATIVE_EXPORT QDeclarativeCompiler
       
   150 {
       
   151     Q_DECLARE_TR_FUNCTIONS(QDeclarativeCompiler)
       
   152 public:
       
   153     QDeclarativeCompiler();
       
   154 
       
   155     bool compile(QDeclarativeEngine *, QDeclarativeCompositeTypeData *, QDeclarativeCompiledData *);
       
   156 
       
   157     bool isError() const;
       
   158     QList<QDeclarativeError> errors() const;
       
   159 
       
   160     static bool isAttachedPropertyName(const QByteArray &);
       
   161     static bool isSignalPropertyName(const QByteArray &);
       
   162 
       
   163     int evaluateEnum(const QByteArray& script) const; // for QDeclarativeCustomParser::evaluateEnum
       
   164 
       
   165 private:
       
   166     static void reset(QDeclarativeCompiledData *);
       
   167 
       
   168     struct BindingContext {
       
   169         BindingContext()
       
   170             : stack(0), owner(0), object(0) {}
       
   171         BindingContext(QDeclarativeParser::Object *o)
       
   172             : stack(0), owner(0), object(o) {}
       
   173         BindingContext incr() const {
       
   174             BindingContext rv(object);
       
   175             rv.stack = stack + 1;
       
   176             return rv;
       
   177         }
       
   178         bool isSubContext() const { return stack != 0; }
       
   179         int stack;
       
   180         int owner;
       
   181         QDeclarativeParser::Object *object;
       
   182     };
       
   183 
       
   184     void compileTree(QDeclarativeParser::Object *tree);
       
   185 
       
   186 
       
   187     bool buildObject(QDeclarativeParser::Object *obj, const BindingContext &);
       
   188     bool buildComponent(QDeclarativeParser::Object *obj, const BindingContext &);
       
   189     bool buildSubObject(QDeclarativeParser::Object *obj, const BindingContext &);
       
   190     bool buildSignal(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj, 
       
   191                      const BindingContext &);
       
   192     bool buildProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj, 
       
   193                        const BindingContext &);
       
   194     bool buildPropertyInNamespace(QDeclarativeImportedNamespace *ns,
       
   195                                   QDeclarativeParser::Property *prop, 
       
   196                                   QDeclarativeParser::Object *obj, 
       
   197                                   const BindingContext &);
       
   198     bool buildIdProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
       
   199     bool buildAttachedProperty(QDeclarativeParser::Property *prop, 
       
   200                                QDeclarativeParser::Object *obj,
       
   201                                const BindingContext &ctxt);
       
   202     bool buildGroupedProperty(QDeclarativeParser::Property *prop,
       
   203                               QDeclarativeParser::Object *obj,
       
   204                               const BindingContext &ctxt);
       
   205     bool buildValueTypeProperty(QObject *type, 
       
   206                                 QDeclarativeParser::Object *obj, 
       
   207                                 QDeclarativeParser::Object *baseObj,
       
   208                                 const BindingContext &ctxt);
       
   209     bool buildListProperty(QDeclarativeParser::Property *prop,
       
   210                            QDeclarativeParser::Object *obj,
       
   211                            const BindingContext &ctxt);
       
   212     bool buildScriptStringProperty(QDeclarativeParser::Property *prop,
       
   213                                    QDeclarativeParser::Object *obj,
       
   214                                    const BindingContext &ctxt);
       
   215     bool buildPropertyAssignment(QDeclarativeParser::Property *prop,
       
   216                                  QDeclarativeParser::Object *obj,
       
   217                                  const BindingContext &ctxt);
       
   218     bool buildPropertyObjectAssignment(QDeclarativeParser::Property *prop,
       
   219                                        QDeclarativeParser::Object *obj,
       
   220                                        QDeclarativeParser::Value *value,
       
   221                                        const BindingContext &ctxt);
       
   222     bool buildPropertyOnAssignment(QDeclarativeParser::Property *prop,
       
   223                                    QDeclarativeParser::Object *obj,
       
   224                                    QDeclarativeParser::Object *baseObj,
       
   225                                    QDeclarativeParser::Value *value,
       
   226                                    const BindingContext &ctxt);
       
   227     bool buildPropertyLiteralAssignment(QDeclarativeParser::Property *prop,
       
   228                                         QDeclarativeParser::Object *obj,
       
   229                                         QDeclarativeParser::Value *value,
       
   230                                         const BindingContext &ctxt);
       
   231     bool doesPropertyExist(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
       
   232     bool testLiteralAssignment(const QMetaProperty &prop, 
       
   233                                QDeclarativeParser::Value *value);
       
   234     bool testQualifiedEnumAssignment(const QMetaProperty &prop,
       
   235                                      QDeclarativeParser::Object *obj,
       
   236                                      QDeclarativeParser::Value *value,
       
   237                                      bool *isAssignment);
       
   238     enum DynamicMetaMode { IgnoreAliases, ResolveAliases, ForceCreation };
       
   239     bool mergeDynamicMetaProperties(QDeclarativeParser::Object *obj);
       
   240     bool buildDynamicMeta(QDeclarativeParser::Object *obj, DynamicMetaMode mode);
       
   241     bool checkDynamicMeta(QDeclarativeParser::Object *obj);
       
   242     bool buildBinding(QDeclarativeParser::Value *, QDeclarativeParser::Property *prop,
       
   243                       const BindingContext &ctxt);
       
   244     bool buildComponentFromRoot(QDeclarativeParser::Object *obj, const BindingContext &);
       
   245     bool compileAlias(QMetaObjectBuilder &, 
       
   246                       QByteArray &data,
       
   247                       QDeclarativeParser::Object *obj, 
       
   248                       const QDeclarativeParser::Object::DynamicProperty &);
       
   249     bool completeComponentBuild();
       
   250     bool checkValidId(QDeclarativeParser::Value *, const QString &);
       
   251 
       
   252 
       
   253     void genObject(QDeclarativeParser::Object *obj);
       
   254     void genObjectBody(QDeclarativeParser::Object *obj);
       
   255     void genComponent(QDeclarativeParser::Object *obj);
       
   256     void genValueProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
       
   257     void genListProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
       
   258     void genPropertyAssignment(QDeclarativeParser::Property *prop, 
       
   259                                QDeclarativeParser::Object *obj,
       
   260                                QDeclarativeParser::Property *valueTypeProperty = 0);
       
   261     void genLiteralAssignment(const QMetaProperty &prop, 
       
   262                               QDeclarativeParser::Value *value);
       
   263     void genBindingAssignment(QDeclarativeParser::Value *binding, 
       
   264                               QDeclarativeParser::Property *prop, 
       
   265                               QDeclarativeParser::Object *obj,
       
   266                               QDeclarativeParser::Property *valueTypeProperty = 0);
       
   267     int genContextCache();
       
   268 
       
   269     int genValueTypeData(QDeclarativeParser::Property *prop, QDeclarativeParser::Property *valueTypeProp);
       
   270     int genPropertyData(QDeclarativeParser::Property *prop);
       
   271 
       
   272     int componentTypeRef();
       
   273 
       
   274     static QDeclarativeType *toQmlType(QDeclarativeParser::Object *from);
       
   275     bool canCoerce(int to, QDeclarativeParser::Object *from);
       
   276 
       
   277     QStringList deferredProperties(QDeclarativeParser::Object *);
       
   278 
       
   279     void addId(const QString &, QDeclarativeParser::Object *);
       
   280 
       
   281     void dumpStats();
       
   282 
       
   283     struct BindingReference {
       
   284         QDeclarativeParser::Variant expression;
       
   285         QDeclarativeParser::Property *property;
       
   286         QDeclarativeParser::Value *value;
       
   287 
       
   288         enum DataType { QtScript, Experimental };
       
   289         DataType dataType;
       
   290 
       
   291         int compiledIndex;
       
   292 
       
   293         QByteArray compiledData;
       
   294         BindingContext bindingContext;
       
   295     };
       
   296     void addBindingReference(const BindingReference &);
       
   297 
       
   298     struct ComponentCompileState
       
   299     {
       
   300         ComponentCompileState() 
       
   301             : parserStatusCount(0), pushedProperties(0), root(0) {}
       
   302         QHash<QString, QDeclarativeParser::Object *> ids;
       
   303         QHash<int, QDeclarativeParser::Object *> idIndexes;
       
   304         int parserStatusCount;
       
   305         int pushedProperties;
       
   306 
       
   307         QByteArray compiledBindingData;
       
   308 
       
   309         QHash<QDeclarativeParser::Value *, BindingReference> bindings;
       
   310         QHash<QDeclarativeParser::Value *, BindingContext> signalExpressions;
       
   311         QList<QDeclarativeParser::Object *> aliasingObjects;
       
   312         QDeclarativeParser::Object *root;
       
   313     };
       
   314     ComponentCompileState compileState;
       
   315 
       
   316     struct ComponentStat
       
   317     {
       
   318         ComponentStat() : ids(0), objects(0) {}
       
   319 
       
   320         int lineNumber;
       
   321 
       
   322         int ids;
       
   323         QList<QDeclarativeParser::LocationSpan> scriptBindings;
       
   324         QList<QDeclarativeParser::LocationSpan> optimizedBindings;
       
   325         int objects;
       
   326     };
       
   327     ComponentStat componentStat;
       
   328 
       
   329     void saveComponentState();
       
   330 
       
   331     ComponentCompileState componentState(QDeclarativeParser::Object *);
       
   332     QHash<QDeclarativeParser::Object *, ComponentCompileState> savedCompileStates;
       
   333     QList<ComponentStat> savedComponentStats;
       
   334 
       
   335     QList<QDeclarativeError> exceptions;
       
   336     QDeclarativeCompiledData *output;
       
   337     QDeclarativeEngine *engine;
       
   338     QDeclarativeEnginePrivate *enginePrivate;
       
   339     QDeclarativeParser::Object *unitRoot;
       
   340     QDeclarativeCompositeTypeData *unit;
       
   341 };
       
   342 QT_END_NAMESPACE
       
   343 
       
   344 #endif // QDECLARATIVECOMPILER_P_H