src/declarative/qml/qdeclarativeparser.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     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 #include "private/qdeclarativeparser_p.h"
       
    43 
       
    44 #include "qdeclarativepropertyvaluesource.h"
       
    45 #include "private/qdeclarativevme_p.h"
       
    46 #include "qdeclarative.h"
       
    47 #include "private/qdeclarativecomponent_p.h"
       
    48 #include "qdeclarativecomponent.h"
       
    49 #include "private/qmetaobjectbuilder_p.h"
       
    50 #include "private/qdeclarativevmemetaobject_p.h"
       
    51 #include "private/qdeclarativecompiler_p.h"
       
    52 #include "parser/qdeclarativejsast_p.h"
       
    53 #include "parser/qdeclarativejsengine_p.h"
       
    54 
       
    55 #include <QStack>
       
    56 #include <QColor>
       
    57 #include <QPointF>
       
    58 #include <QSizeF>
       
    59 #include <QRectF>
       
    60 #include <QStringBuilder>
       
    61 #include <QtDebug>
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 using namespace QDeclarativeJS;
       
    66 using namespace QDeclarativeParser;
       
    67 
       
    68 QDeclarativeParser::Object::Object()
       
    69 : type(-1), majorVersion(-1), minorVersion(-1), idIndex(-1), metatype(0), defaultProperty(0), parserStatusCast(-1)
       
    70 {
       
    71 }
       
    72 
       
    73 QDeclarativeParser::Object::~Object() 
       
    74 { 
       
    75     if (defaultProperty) defaultProperty->release();
       
    76     foreach(Property *prop, properties)
       
    77         prop->release();
       
    78     foreach(Property *prop, valueProperties)
       
    79         prop->release();
       
    80     foreach(Property *prop, signalProperties)
       
    81         prop->release();
       
    82     foreach(Property *prop, attachedProperties)
       
    83         prop->release();
       
    84     foreach(Property *prop, groupedProperties)
       
    85         prop->release();
       
    86     foreach(Property *prop, valueTypeProperties)
       
    87         prop->release();
       
    88     typedef QPair<Property *, int> PropPair;
       
    89     foreach(const PropPair &prop, scriptStringProperties)
       
    90         prop.first->release();
       
    91     foreach(const DynamicProperty &prop, dynamicProperties)
       
    92         if (prop.defaultValue) prop.defaultValue->release();
       
    93 }
       
    94 
       
    95 void Object::setBindingBit(int b)
       
    96 {
       
    97     while (bindingBitmask.size() < 4 * (1 + b / 32))
       
    98         bindingBitmask.append(char(0));
       
    99 
       
   100     quint32 *bits = (quint32 *)bindingBitmask.data();
       
   101     bits[b / 32] |= (1 << (b % 32));
       
   102 }
       
   103 
       
   104 const QMetaObject *Object::metaObject() const
       
   105 {
       
   106     if (!metadata.isEmpty() && metatype)
       
   107         return &extObject;
       
   108     else
       
   109         return metatype;
       
   110 }
       
   111 
       
   112 QDeclarativeParser::Property *Object::getDefaultProperty()
       
   113 {
       
   114     if (!defaultProperty) {
       
   115         defaultProperty = new Property;
       
   116         defaultProperty->parent = this;
       
   117     }
       
   118     return defaultProperty;
       
   119 }
       
   120 
       
   121 void QDeclarativeParser::Object::addValueProperty(Property *p)
       
   122 {
       
   123     p->addref();
       
   124     valueProperties << p;
       
   125 }
       
   126 
       
   127 void QDeclarativeParser::Object::addSignalProperty(Property *p)
       
   128 {
       
   129     p->addref();
       
   130     signalProperties << p;
       
   131 }
       
   132 
       
   133 void QDeclarativeParser::Object::addAttachedProperty(Property *p)
       
   134 {
       
   135     p->addref();
       
   136     attachedProperties << p;
       
   137 }
       
   138 
       
   139 void QDeclarativeParser::Object::addGroupedProperty(Property *p)
       
   140 {
       
   141     p->addref();
       
   142     groupedProperties << p;
       
   143 }
       
   144 
       
   145 void QDeclarativeParser::Object::addValueTypeProperty(Property *p)
       
   146 {
       
   147     p->addref();
       
   148     valueTypeProperties << p;
       
   149 }
       
   150 
       
   151 void QDeclarativeParser::Object::addScriptStringProperty(Property *p, int stack)
       
   152 {
       
   153     p->addref();
       
   154     scriptStringProperties << qMakePair(p, stack);
       
   155 }
       
   156 
       
   157 
       
   158 Property *QDeclarativeParser::Object::getProperty(const QByteArray &name, bool create)
       
   159 {
       
   160     if (!properties.contains(name)) {
       
   161         if (create) {
       
   162             Property *property = new Property(name);
       
   163             property->parent = this;
       
   164             properties.insert(name, property);
       
   165         } else {
       
   166             return 0;
       
   167         }
       
   168     }
       
   169     return properties[name];
       
   170 }
       
   171 
       
   172 QDeclarativeParser::Object::DynamicProperty::DynamicProperty()
       
   173 : isDefaultProperty(false), type(Variant), defaultValue(0)
       
   174 {
       
   175 }
       
   176 
       
   177 QDeclarativeParser::Object::DynamicProperty::DynamicProperty(const DynamicProperty &o)
       
   178 : isDefaultProperty(o.isDefaultProperty),
       
   179   type(o.type),
       
   180   customType(o.customType),
       
   181   name(o.name),
       
   182   defaultValue(o.defaultValue),
       
   183   location(o.location)
       
   184 {
       
   185 }
       
   186 
       
   187 QDeclarativeParser::Object::DynamicSignal::DynamicSignal()
       
   188 {
       
   189 }
       
   190 
       
   191 QDeclarativeParser::Object::DynamicSignal::DynamicSignal(const DynamicSignal &o)
       
   192 : name(o.name), parameterTypes(o.parameterTypes), 
       
   193   parameterNames(o.parameterNames)
       
   194 {
       
   195 }
       
   196 
       
   197 QDeclarativeParser::Object::DynamicSlot::DynamicSlot()
       
   198 {
       
   199 }
       
   200 
       
   201 QDeclarativeParser::Object::DynamicSlot::DynamicSlot(const DynamicSlot &o)
       
   202 : name(o.name), body(o.body), parameterNames(o.parameterNames), location(o.location)
       
   203 {
       
   204 }
       
   205 
       
   206 QDeclarativeParser::Property::Property()
       
   207 : parent(0), type(0), index(-1), value(0), isDefault(true), isDeferred(false), 
       
   208   isValueTypeSubProperty(false)
       
   209 {
       
   210 }
       
   211 
       
   212 QDeclarativeParser::Property::Property(const QByteArray &n)
       
   213 : parent(0), type(0), index(-1), value(0), name(n), isDefault(false), 
       
   214   isDeferred(false), isValueTypeSubProperty(false)
       
   215 {
       
   216 }
       
   217 
       
   218 QDeclarativeParser::Property::~Property() 
       
   219 { 
       
   220     foreach(Value *value, values)
       
   221         value->release();
       
   222     foreach(Value *value, onValues)
       
   223         value->release();
       
   224     if (value) value->release(); 
       
   225 }
       
   226 
       
   227 Object *QDeclarativeParser::Property::getValue(const LocationSpan &l)
       
   228 {
       
   229     if (!value) { value = new Object; value->location = l; }
       
   230     return value;
       
   231 }
       
   232 
       
   233 void QDeclarativeParser::Property::addValue(Value *v)
       
   234 {
       
   235     values << v;
       
   236 }
       
   237 
       
   238 void QDeclarativeParser::Property::addOnValue(Value *v)
       
   239 {
       
   240     onValues << v;
       
   241 }
       
   242 
       
   243 bool QDeclarativeParser::Property::isEmpty() const
       
   244 {
       
   245     return !value && values.isEmpty() && onValues.isEmpty();
       
   246 }
       
   247 
       
   248 QDeclarativeParser::Value::Value()
       
   249 : type(Unknown), object(0)
       
   250 {
       
   251 }
       
   252 
       
   253 QDeclarativeParser::Value::~Value() 
       
   254 { 
       
   255     if (object) object->release();
       
   256 }
       
   257 
       
   258 QDeclarativeParser::Variant::Variant()
       
   259 : t(Invalid) {}
       
   260 
       
   261 QDeclarativeParser::Variant::Variant(const Variant &o)
       
   262 : t(o.t), d(o.d), s(o.s)
       
   263 {
       
   264 }
       
   265 
       
   266 QDeclarativeParser::Variant::Variant(bool v)
       
   267 : t(Boolean), b(v)
       
   268 {
       
   269 }
       
   270 
       
   271 QDeclarativeParser::Variant::Variant(double v, const QString &asWritten)
       
   272 : t(Number), d(v), s(asWritten)
       
   273 {
       
   274 }
       
   275 
       
   276 QDeclarativeParser::Variant::Variant(const QString &v)
       
   277 : t(String), s(v)
       
   278 {
       
   279 }
       
   280 
       
   281 QDeclarativeParser::Variant::Variant(const QString &v, QDeclarativeJS::AST::Node *n)
       
   282 : t(Script), n(n), s(v)
       
   283 {
       
   284 }
       
   285 
       
   286 QDeclarativeParser::Variant &QDeclarativeParser::Variant::operator=(const Variant &o)
       
   287 {
       
   288     t = o.t;
       
   289     d = o.d;
       
   290     s = o.s;
       
   291     return *this;
       
   292 }
       
   293 
       
   294 QDeclarativeParser::Variant::Type QDeclarativeParser::Variant::type() const
       
   295 {
       
   296     return t;
       
   297 }
       
   298 
       
   299 bool QDeclarativeParser::Variant::asBoolean() const
       
   300 {
       
   301     return b;
       
   302 }
       
   303 
       
   304 QString QDeclarativeParser::Variant::asString() const
       
   305 {
       
   306     return s;
       
   307 }
       
   308 
       
   309 double QDeclarativeParser::Variant::asNumber() const
       
   310 {
       
   311     return d;
       
   312 }
       
   313 
       
   314 //reverse of Lexer::singleEscape()
       
   315 QString escapedString(const QString &string)
       
   316 {
       
   317     QString tmp = QLatin1String("\"");
       
   318     for (int i = 0; i < string.length(); ++i) {
       
   319         const QChar &c = string.at(i);
       
   320         switch(c.unicode()) {
       
   321         case 0x08:
       
   322             tmp += QLatin1String("\\b");
       
   323             break;
       
   324         case 0x09:
       
   325             tmp += QLatin1String("\\t");
       
   326             break;
       
   327         case 0x0A:
       
   328             tmp += QLatin1String("\\n");
       
   329             break;
       
   330         case 0x0B:
       
   331             tmp += QLatin1String("\\v");
       
   332             break;
       
   333         case 0x0C:
       
   334             tmp += QLatin1String("\\f");
       
   335             break;
       
   336         case 0x0D:
       
   337             tmp += QLatin1String("\\r");
       
   338             break;
       
   339         case 0x22:
       
   340             tmp += QLatin1String("\\\"");
       
   341             break;
       
   342         case 0x27:
       
   343             tmp += QLatin1String("\\\'");
       
   344             break;
       
   345         case 0x5C:
       
   346             tmp += QLatin1String("\\\\");
       
   347             break;
       
   348         default:
       
   349             tmp += c;
       
   350             break;
       
   351         }
       
   352     }
       
   353     tmp += QLatin1Char('\"');
       
   354     return tmp;
       
   355 }
       
   356 
       
   357 QString QDeclarativeParser::Variant::asScript() const
       
   358 {
       
   359     switch(type()) { 
       
   360     default:
       
   361     case Invalid:
       
   362         return QString();
       
   363     case Boolean:
       
   364         return b?QLatin1String("true"):QLatin1String("false");
       
   365     case Number:
       
   366         if (s.isEmpty())
       
   367             return QString::number(d);
       
   368         else
       
   369             return s;
       
   370     case String:
       
   371         return escapedString(s);
       
   372     case Script:
       
   373         return s;
       
   374     }
       
   375 }
       
   376 
       
   377 QDeclarativeJS::AST::Node *QDeclarativeParser::Variant::asAST() const
       
   378 {
       
   379     if (type() == Script)
       
   380         return n;
       
   381     else
       
   382         return 0;
       
   383 }
       
   384 
       
   385 bool QDeclarativeParser::Variant::isStringList() const
       
   386 {
       
   387     if (isString())
       
   388         return true;
       
   389 
       
   390     if (type() != Script || !n)
       
   391         return false;
       
   392 
       
   393     AST::ArrayLiteral *array = AST::cast<AST::ArrayLiteral *>(n);
       
   394     if (!array)
       
   395         return false;
       
   396 
       
   397     AST::ElementList *elements = array->elements;
       
   398 
       
   399     while (elements) {
       
   400 
       
   401         if (!AST::cast<AST::StringLiteral *>(elements->expression))
       
   402             return false;
       
   403 
       
   404         elements = elements->next;
       
   405     }
       
   406 
       
   407     return true;
       
   408 }
       
   409 
       
   410 QStringList QDeclarativeParser::Variant::asStringList() const
       
   411 {
       
   412     QStringList rv;
       
   413     if (isString()) {
       
   414         rv << asString();
       
   415         return rv;
       
   416     }
       
   417 
       
   418     AST::ArrayLiteral *array = AST::cast<AST::ArrayLiteral *>(n);
       
   419     if (!array)
       
   420         return rv;
       
   421 
       
   422     AST::ElementList *elements = array->elements;
       
   423     while (elements) {
       
   424 
       
   425         AST::StringLiteral *string = AST::cast<AST::StringLiteral *>(elements->expression);
       
   426         if (!string)
       
   427             return QStringList();
       
   428         rv.append(string->value->asString());
       
   429 
       
   430         elements = elements->next;
       
   431     }
       
   432 
       
   433     return  rv;
       
   434 }
       
   435 
       
   436 QT_END_NAMESPACE