| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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/qdeclarativeparser_p.h"
|
|---|
| 60 | #include "private/qdeclarativeengine_p.h"
|
|---|
| 61 | #include "private/qbitfield_p.h"
|
|---|
| 62 | #include "private/qdeclarativepropertycache_p.h"
|
|---|
| 63 | #include "private/qdeclarativeintegercache_p.h"
|
|---|
| 64 | #include "private/qdeclarativetypenamecache_p.h"
|
|---|
| 65 | #include "private/qdeclarativetypeloader_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) {}
|
|---|
| 93 |
|
|---|
| 94 | QByteArray className;
|
|---|
| 95 | QDeclarativeType *type;
|
|---|
| 96 | QDeclarativeCompiledData *component;
|
|---|
| 97 |
|
|---|
| 98 | QObject *createInstance(QDeclarativeContextData *, const QBitField &, QList<QDeclarativeError> *) const;
|
|---|
| 99 | const QMetaObject *metaObject() const;
|
|---|
| 100 | };
|
|---|
| 101 | QList<TypeReference> types;
|
|---|
| 102 | struct CustomTypeData
|
|---|
| 103 | {
|
|---|
| 104 | int index;
|
|---|
| 105 | int type;
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | const QMetaObject *root;
|
|---|
| 109 | QAbstractDynamicMetaObject rootData;
|
|---|
| 110 | QDeclarativePropertyCache *rootPropertyCache;
|
|---|
| 111 | QList<QString> primitives;
|
|---|
| 112 | QList<float> floatData;
|
|---|
| 113 | QList<int> intData;
|
|---|
| 114 | QList<CustomTypeData> customTypeData;
|
|---|
| 115 | QList<QByteArray> datas;
|
|---|
| 116 | QList<QDeclarativeParser::Location> locations;
|
|---|
| 117 | QList<QDeclarativeInstruction> bytecode;
|
|---|
| 118 | QList<QScriptProgram *> cachedPrograms;
|
|---|
| 119 | QList<QScriptValue *> cachedClosures;
|
|---|
| 120 | QList<QDeclarativePropertyCache *> propertyCaches;
|
|---|
| 121 | QList<QDeclarativeIntegerCache *> contextCaches;
|
|---|
| 122 | QList<QDeclarativeParser::Object::ScriptBlock> scripts;
|
|---|
| 123 | QList<QUrl> urls;
|
|---|
| 124 |
|
|---|
| 125 | void dumpInstructions();
|
|---|
| 126 |
|
|---|
| 127 | protected:
|
|---|
| 128 | virtual void clear(); // From QDeclarativeCleanup
|
|---|
| 129 |
|
|---|
| 130 | private:
|
|---|
| 131 | void dump(QDeclarativeInstruction *, int idx = -1);
|
|---|
| 132 | QDeclarativeCompiledData(const QDeclarativeCompiledData &other);
|
|---|
| 133 | QDeclarativeCompiledData &operator=(const QDeclarativeCompiledData &other);
|
|---|
| 134 | QByteArray packData;
|
|---|
| 135 | friend class QDeclarativeCompiler;
|
|---|
| 136 | int pack(const char *, size_t);
|
|---|
| 137 |
|
|---|
| 138 | int indexForString(const QString &);
|
|---|
| 139 | int indexForByteArray(const QByteArray &);
|
|---|
| 140 | int indexForFloat(float *, int);
|
|---|
| 141 | int indexForInt(int *, int);
|
|---|
| 142 | int indexForLocation(const QDeclarativeParser::Location &);
|
|---|
| 143 | int indexForLocation(const QDeclarativeParser::LocationSpan &);
|
|---|
| 144 | int indexForUrl(const QUrl &);
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | class QMetaObjectBuilder;
|
|---|
| 148 | class Q_AUTOTEST_EXPORT QDeclarativeCompiler
|
|---|
| 149 | {
|
|---|
| 150 | Q_DECLARE_TR_FUNCTIONS(QDeclarativeCompiler)
|
|---|
| 151 | public:
|
|---|
| 152 | QDeclarativeCompiler();
|
|---|
| 153 |
|
|---|
| 154 | bool compile(QDeclarativeEngine *, QDeclarativeTypeData *, QDeclarativeCompiledData *);
|
|---|
| 155 |
|
|---|
| 156 | bool isError() const;
|
|---|
| 157 | QList<QDeclarativeError> errors() const;
|
|---|
| 158 |
|
|---|
| 159 | static bool isAttachedPropertyName(const QByteArray &);
|
|---|
| 160 | static bool isSignalPropertyName(const QByteArray &);
|
|---|
| 161 |
|
|---|
| 162 | int evaluateEnum(const QByteArray& script) const; // for QDeclarativeCustomParser::evaluateEnum
|
|---|
| 163 | const QMetaObject *resolveType(const QByteArray& name) const; // for QDeclarativeCustomParser::resolveType
|
|---|
| 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 genValueTypeProperty(QDeclarativeParser::Object *obj,QDeclarativeParser::Property *);
|
|---|
| 256 | void genComponent(QDeclarativeParser::Object *obj);
|
|---|
| 257 | void genValueProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
|
|---|
| 258 | void genListProperty(QDeclarativeParser::Property *prop, QDeclarativeParser::Object *obj);
|
|---|
| 259 | void genPropertyAssignment(QDeclarativeParser::Property *prop,
|
|---|
| 260 | QDeclarativeParser::Object *obj,
|
|---|
| 261 | QDeclarativeParser::Property *valueTypeProperty = 0);
|
|---|
| 262 | void genLiteralAssignment(const QMetaProperty &prop,
|
|---|
| 263 | QDeclarativeParser::Value *value);
|
|---|
| 264 | void genBindingAssignment(QDeclarativeParser::Value *binding,
|
|---|
| 265 | QDeclarativeParser::Property *prop,
|
|---|
| 266 | QDeclarativeParser::Object *obj,
|
|---|
| 267 | QDeclarativeParser::Property *valueTypeProperty = 0);
|
|---|
| 268 | int genContextCache();
|
|---|
| 269 |
|
|---|
| 270 | int genValueTypeData(QDeclarativeParser::Property *prop, QDeclarativeParser::Property *valueTypeProp);
|
|---|
| 271 | int genPropertyData(QDeclarativeParser::Property *prop);
|
|---|
| 272 |
|
|---|
| 273 | int componentTypeRef();
|
|---|
| 274 |
|
|---|
| 275 | static QDeclarativeType *toQmlType(QDeclarativeParser::Object *from);
|
|---|
| 276 | bool canCoerce(int to, QDeclarativeParser::Object *from);
|
|---|
| 277 |
|
|---|
| 278 | QStringList deferredProperties(QDeclarativeParser::Object *);
|
|---|
| 279 |
|
|---|
| 280 | void addId(const QString &, QDeclarativeParser::Object *);
|
|---|
| 281 |
|
|---|
| 282 | void dumpStats();
|
|---|
| 283 |
|
|---|
| 284 | struct BindingReference {
|
|---|
| 285 | QDeclarativeParser::Variant expression;
|
|---|
| 286 | QDeclarativeParser::Property *property;
|
|---|
| 287 | QDeclarativeParser::Value *value;
|
|---|
| 288 |
|
|---|
| 289 | enum DataType { QtScript, Experimental };
|
|---|
| 290 | DataType dataType;
|
|---|
| 291 |
|
|---|
| 292 | int compiledIndex;
|
|---|
| 293 |
|
|---|
| 294 | QByteArray compiledData;
|
|---|
| 295 | BindingContext bindingContext;
|
|---|
| 296 | };
|
|---|
| 297 | void addBindingReference(const BindingReference &);
|
|---|
| 298 |
|
|---|
| 299 | struct ComponentCompileState
|
|---|
| 300 | {
|
|---|
| 301 | ComponentCompileState()
|
|---|
| 302 | : parserStatusCount(0), pushedProperties(0), root(0) {}
|
|---|
| 303 | QHash<QString, QDeclarativeParser::Object *> ids;
|
|---|
| 304 | QHash<int, QDeclarativeParser::Object *> idIndexes;
|
|---|
| 305 | int parserStatusCount;
|
|---|
| 306 | int pushedProperties;
|
|---|
| 307 |
|
|---|
| 308 | QByteArray compiledBindingData;
|
|---|
| 309 |
|
|---|
| 310 | QHash<QDeclarativeParser::Value *, BindingReference> bindings;
|
|---|
| 311 | QHash<QDeclarativeParser::Value *, BindingContext> signalExpressions;
|
|---|
| 312 | QList<QDeclarativeParser::Object *> aliasingObjects;
|
|---|
| 313 | QDeclarativeParser::Object *root;
|
|---|
| 314 | };
|
|---|
| 315 | ComponentCompileState compileState;
|
|---|
| 316 |
|
|---|
| 317 | struct ComponentStat
|
|---|
| 318 | {
|
|---|
| 319 | ComponentStat() : ids(0), objects(0) {}
|
|---|
| 320 |
|
|---|
| 321 | int lineNumber;
|
|---|
| 322 |
|
|---|
| 323 | int ids;
|
|---|
| 324 | QList<QDeclarativeParser::LocationSpan> scriptBindings;
|
|---|
| 325 | QList<QDeclarativeParser::LocationSpan> optimizedBindings;
|
|---|
| 326 | int objects;
|
|---|
| 327 | };
|
|---|
| 328 | ComponentStat componentStat;
|
|---|
| 329 |
|
|---|
| 330 | void saveComponentState();
|
|---|
| 331 |
|
|---|
| 332 | ComponentCompileState componentState(QDeclarativeParser::Object *);
|
|---|
| 333 | QHash<QDeclarativeParser::Object *, ComponentCompileState> savedCompileStates;
|
|---|
| 334 | QList<ComponentStat> savedComponentStats;
|
|---|
| 335 |
|
|---|
| 336 | QList<QDeclarativeError> exceptions;
|
|---|
| 337 | QDeclarativeCompiledData *output;
|
|---|
| 338 | QDeclarativeEngine *engine;
|
|---|
| 339 | QDeclarativeEnginePrivate *enginePrivate;
|
|---|
| 340 | QDeclarativeParser::Object *unitRoot;
|
|---|
| 341 | QDeclarativeTypeData *unit;
|
|---|
| 342 | };
|
|---|
| 343 | QT_END_NAMESPACE
|
|---|
| 344 |
|
|---|
| 345 | #endif // QDECLARATIVECOMPILER_P_H
|
|---|