summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qmlcompiler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/qml/qmlcompiler.cpp')
-rw-r--r--src/declarative/qml/qmlcompiler.cpp314
1 files changed, 47 insertions, 267 deletions
diff --git a/src/declarative/qml/qmlcompiler.cpp b/src/declarative/qml/qmlcompiler.cpp
index c9bdfec..d4003ab 100644
--- a/src/declarative/qml/qmlcompiler.cpp
+++ b/src/declarative/qml/qmlcompiler.cpp
@@ -64,17 +64,6 @@
#include "qmlscriptparser_p.h"
QT_BEGIN_NAMESPACE
-/*
- New properties and signals can be added to any QObject type from QML.
- <QObject>
- <properties><Property name="myProperty" /></properties>
- <signals><Signal name="mySignal" /></signals>
- </QObject
- The special names used as magical properties (in the above case "properties"
- and "signals") are defined here.
-*/
-#define PROPERTIES_NAME "properties"
-#define SIGNALS_NAME "signals"
using namespace QmlParser;
@@ -147,7 +136,7 @@ int QmlCompiledData::indexForInt(int *data, int count)
}
QmlCompiler::QmlCompiler()
-: exceptionLine(-1), output(0)
+: exceptionLine(-1), exceptionColumn(-1), output(0)
{
}
@@ -156,14 +145,19 @@ bool QmlCompiler::isError() const
return exceptionLine != -1;
}
-qint64 QmlCompiler::errorLine() const
+QList<QmlError> QmlCompiler::errors() const
{
- return exceptionLine;
-}
+ QList<QmlError> rv;
+
+ if(isError()) {
+ QmlError error;
+ error.setDescription(exceptionDescription);
+ error.setLine(exceptionLine);
+ error.setColumn(exceptionColumn);
+ rv << error;
+ }
-QString QmlCompiler::errorDescription() const
-{
- return exceptionDescription;
+ return rv;
}
bool QmlCompiler::isValidId(const QString &val)
@@ -437,9 +431,19 @@ void QmlCompiler::reset(QmlCompiledComponent *cc, bool deleteMemory)
cc->bytecode.clear();
}
+#define COMPILE_EXCEPTION2(token, desc) \
+ { \
+ exceptionLine = token->line; \
+ exceptionColumn = token->column; \
+ QDebug d(&exceptionDescription); \
+ d << desc; \
+ return false; \
+ }
+
#define COMPILE_EXCEPTION(desc) \
{ \
exceptionLine = obj->line; \
+ exceptionColumn = obj->column; \
QDebug d(&exceptionDescription); \
d << desc; \
return false; \
@@ -532,49 +536,16 @@ bool QmlCompiler::compileObject(Object *obj, int ctxt)
ctxt = 0;
- // Only use magical "properties" and "signals" properties if the type
- // doesn't have already have them
- bool ignoreProperties = false;
- bool ignoreSignals = false;
- if (obj->metatype && obj->metatype->indexOfProperty(PROPERTIES_NAME) != -1)
- ignoreProperties = true;
- if (obj->metatype && obj->metatype->indexOfProperty(SIGNALS_NAME) != -1)
- ignoreSignals = true;
-
- Property *propertiesProperty = ignoreProperties?0:obj->getProperty(PROPERTIES_NAME, false);
- Property *signalsProperty = ignoreSignals?0:obj->getProperty(SIGNALS_NAME, false);
-
- if (propertiesProperty) {
- obj->dynamicPropertiesProperty = propertiesProperty;
- obj->properties.remove(PROPERTIES_NAME);
- }
- if (signalsProperty) {
- obj->dynamicSignalsProperty = signalsProperty;
- obj->properties.remove(SIGNALS_NAME);
- }
-
int createInstrIdx = output->bytecode.count();
- if (obj->type != -1 && output->types.at(obj->type).parser) {
- QByteArray data = obj->custom;
- int ref = output->indexForByteArray(data);
-
- QmlInstruction create;
- create.type = QmlInstruction::CreateCustomObject;
- create.line = obj->line;
- create.createCustom.type = obj->type;
- create.createCustom.data = ref;
- output->bytecode << create;
- } else {
- // Create the object
- QmlInstruction create;
- create.type = QmlInstruction::CreateObject;
- create.line = obj->line;
- create.create.data = -1;
- create.create.type = obj->type;
- output->bytecode << create;
- }
+ // Create the object
+ QmlInstruction create;
+ create.type = QmlInstruction::CreateObject;
+ create.line = obj->line;
+ create.create.data = -1;
+ create.create.type = obj->type;
+ output->bytecode << create;
- COMPILE_CHECK(compileDynamicPropertiesAndSignals(obj));
+ COMPILE_CHECK(compileDynamicMeta(obj));
if (obj->type != -1) {
if (output->types.at(obj->type).component) {
@@ -599,9 +570,7 @@ bool QmlCompiler::compileObject(Object *obj, int ctxt)
QList<QmlCustomParserProperty> customProps;
foreach(Property *prop, obj->properties) {
- if (!ignoreProperties && prop->name == PROPERTIES_NAME) {
- } else if (!ignoreSignals && prop->name == SIGNALS_NAME) {
- } else if (prop->name.length() >= 3 && prop->name.startsWith("on") &&
+ if (prop->name.length() >= 3 && prop->name.startsWith("on") &&
('A' <= prop->name.at(2) && 'Z' >= prop->name.at(2))) {
if (!isCustomParser) {
COMPILE_CHECK(compileSignal(prop, obj));
@@ -1215,10 +1184,10 @@ bool QmlCompiler::compilePropertyLiteralAssignment(QmlParser::Property *prop,
//### we are restricted to a rather generic message here. If we can find a way to move
// the exception into generateStoreInstruction we could potentially have better messages.
// (the problem is that both compile and run exceptions can be generated, though)
- COMPILE_EXCEPTION("Cannot assign value" << v->primitive << "to property" << obj->metaObject()->property(prop->index).name());
+ COMPILE_EXCEPTION2(v, "Cannot assign value" << v->primitive << "to property" << obj->metaObject()->property(prop->index).name());
doassign = false;
} else if (r == ReadOnly) {
- COMPILE_EXCEPTION("Cannot assign value" << v->primitive << "to the read-only property" << obj->metaObject()->property(prop->index).name());
+ COMPILE_EXCEPTION2(v, "Cannot assign value" << v->primitive << "to the read-only property" << obj->metaObject()->property(prop->index).name());
} else {
doassign = true;
}
@@ -1243,210 +1212,12 @@ bool QmlCompiler::compilePropertyLiteralAssignment(QmlParser::Property *prop,
return true;
}
-bool QmlCompiler::findDynamicProperties(QmlParser::Property *prop,
- QmlParser::Object *obj)
-{
- QList<Object::DynamicProperty> definedProperties;
-
- struct TypeNameToType {
- const char *name;
- Object::DynamicProperty::Type type;
- } propTypeNameToTypes[] = {
- { "", Object::DynamicProperty::Variant },
- { "int", Object::DynamicProperty::Int },
- { "bool", Object::DynamicProperty::Bool },
- { "double", Object::DynamicProperty::Real },
- { "real", Object::DynamicProperty::Real },
- { "string", Object::DynamicProperty::String },
- { "color", Object::DynamicProperty::Color },
- { "date", Object::DynamicProperty::Date },
- { "variant", Object::DynamicProperty::Variant }
- };
- const int propTypeNameToTypesCount = sizeof(propTypeNameToTypes) /
- sizeof(propTypeNameToTypes[0]);
-
-
- if (prop->value)
- COMPILE_EXCEPTION("Invalid property specification");
-
- bool seenDefault = false;
- for (int ii = 0; ii < prop->values.count(); ++ii) {
- QmlParser::Value *val = prop->values.at(ii);
- if (!val->object)
- COMPILE_EXCEPTION("Invalid property specification");
-
- QmlParser::Object *obj = val->object;
- if (obj->type == -1 || output->types.at(obj->type).className != "Property")
- COMPILE_EXCEPTION("Use Property tag to specify properties");
-
-
- enum Seen { None = 0, Name = 0x01,
- Type = 0x02, Value = 0x04,
- ValueChanged = 0x08,
- Default = 0x10 } seen = None;
-
- Object::DynamicProperty propDef;
-
- for (QHash<QByteArray, QmlParser::Property *>::ConstIterator iter =
- obj->properties.begin();
- iter != obj->properties.end();
- ++iter) {
-
- QmlParser::Property *property = *iter;
- if (property->name == "name") {
- if (seen & Name)
- COMPILE_EXCEPTION("May only specify Property name once");
- seen = (Seen)(seen | Name);
-
- if (property->value || property->values.count() != 1 ||
- property->values.at(0)->object)
- COMPILE_EXCEPTION("Invalid Property name");
-
- propDef.name = property->values.at(0)->primitive.toLatin1();
-
- } else if (property->name == "type") {
- if (seen & Type)
- COMPILE_EXCEPTION("May only specify Property type once");
- seen = (Seen)(seen | Type);
-
- if (property->value || property->values.count() != 1 ||
- property->values.at(0)->object)
- COMPILE_EXCEPTION("Invalid Property type");
-
- QString type = property->values.at(0)->primitive.toLower();
- bool found = false;
- for (int ii = 0; !found && ii < propTypeNameToTypesCount; ++ii) {
- if (type == QLatin1String(propTypeNameToTypes[ii].name)){
- found = true;
- propDef.type = propTypeNameToTypes[ii].type;
- }
-
- }
-
- if (!found)
- COMPILE_EXCEPTION("Invalid Property type");
-
- } else if (property->name == "value") {
- if (seen & Value)
- COMPILE_EXCEPTION("May only specify Property value once");
- seen = (Seen)(seen | Value);
-
- propDef.defaultValue = property;
- } else if (property->name == "onValueChanged") {
- if (seen & ValueChanged)
- COMPILE_EXCEPTION("May only specify Property onValueChanged once");
- seen = (Seen)(seen | ValueChanged);
-
- if (property->value || property->values.count() != 1 ||
- property->values.at(0)->object)
- COMPILE_EXCEPTION("Invalid Property onValueChanged");
-
- propDef.onValueChanged = property->values.at(0)->primitive;
-
- } else if (property->name == "default") {
- if (seen & Default)
- COMPILE_EXCEPTION("May only specify Property default once");
- seen = (Seen)(seen | Default);
- if (property->value || property->values.count() != 1 ||
- property->values.at(0)->object)
- COMPILE_EXCEPTION("Invalid Property default");
-
- bool defaultValue =
- QmlStringConverters::boolFromString(property->values.at(0)->primitive);
- propDef.isDefaultProperty = defaultValue;
- if (defaultValue) {
- if (seenDefault)
- COMPILE_EXCEPTION("Only one property may be the default");
- seenDefault = true;
- }
-
- } else {
- COMPILE_EXCEPTION("Invalid Property property");
- }
-
- }
- if (obj->defaultProperty) {
- if (seen & Value)
- COMPILE_EXCEPTION("May only specify Property value once");
-
- seen = (Seen)(seen | Value);
- propDef.defaultValue = obj->defaultProperty;
- }
-
- if (!(seen & Name))
- COMPILE_EXCEPTION("Must specify Property name");
-
- definedProperties << propDef;
- }
-
- obj->dynamicProperties << definedProperties;
- return true;
-}
-
-bool QmlCompiler::findDynamicSignals(QmlParser::Property *sigs,
- QmlParser::Object *obj)
-{
- QList<Object::DynamicSignal> definedSignals;
-
- if (sigs->value)
- COMPILE_EXCEPTION("Invalid signal specification");
-
- for (int ii = 0; ii < sigs->values.count(); ++ii) {
- QmlParser::Value *val = sigs->values.at(ii);
- if (!val->object)
- COMPILE_EXCEPTION("Invalid signal specification");
-
- QmlParser::Object *obj = val->object;
- if (obj->type == -1 || output->types.at(obj->type).className != "Signal")
- COMPILE_EXCEPTION("Use Signal tag to specify signals");
-
- enum Seen { None = 0, Name = 0x01 } seen = None;
- Object::DynamicSignal sigDef;
-
- for (QHash<QByteArray, QmlParser::Property *>::ConstIterator iter =
- obj->properties.begin();
- iter != obj->properties.end();
- ++iter) {
-
- QmlParser::Property *property = *iter;
- if (property->name == "name") {
- if (seen & Name)
- COMPILE_EXCEPTION("May only specify Signal name once");
- seen = (Seen)(seen | Name);
-
- if (property->value || property->values.count() != 1 ||
- property->values.at(0)->object)
- COMPILE_EXCEPTION("Invalid Signal name");
-
- sigDef.name = property->values.at(0)->primitive.toLatin1();
-
- } else {
- COMPILE_EXCEPTION("Invalid Signal property");
- }
-
- }
-
- if (obj->defaultProperty)
- COMPILE_EXCEPTION("Invalid Signal property");
-
- if (!(seen & Name))
- COMPILE_EXCEPTION("Must specify Signal name");
-
- definedSignals << sigDef;
- }
-
- obj->dynamicSignals << definedSignals;
- return true;
-}
-
-bool QmlCompiler::compileDynamicPropertiesAndSignals(QmlParser::Object *obj)
+bool QmlCompiler::compileDynamicMeta(QmlParser::Object *obj)
{
- if (obj->dynamicPropertiesProperty)
- findDynamicProperties(obj->dynamicPropertiesProperty, obj);
- if (obj->dynamicSignalsProperty)
- findDynamicSignals(obj->dynamicSignalsProperty, obj);
-
- if (obj->dynamicProperties.isEmpty() && obj->dynamicSignals.isEmpty())
+ // ### FIXME - Check that there is only one default property etc.
+ if (obj->dynamicProperties.isEmpty() &&
+ obj->dynamicSignals.isEmpty() &&
+ obj->dynamicSlots.isEmpty())
return true;
QMetaObjectBuilder builder;
@@ -1494,6 +1265,14 @@ bool QmlCompiler::compileDynamicPropertiesAndSignals(QmlParser::Object *obj)
builder.addSignal(s.name + "()");
}
+ int slotStart = obj->dynamicSlots.isEmpty()?-1:output->primitives.count();
+
+ for (int ii = 0; ii < obj->dynamicSlots.count(); ++ii) {
+ const Object::DynamicSlot &s = obj->dynamicSlots.at(ii);
+ builder.addSlot(s.name + "()");
+ output->primitives << s.body;
+ }
+
if (obj->metatype)
builder.setSuperClass(obj->metatype);
@@ -1503,6 +1282,7 @@ bool QmlCompiler::compileDynamicPropertiesAndSignals(QmlParser::Object *obj)
QmlInstruction store;
store.type = QmlInstruction::StoreMetaObject;
store.storeMeta.data = output->mos.count() - 1;
+ store.storeMeta.slotData = slotStart;
store.line = obj->line;
output->bytecode << store;