Qt 4.8
qdeclarativeparser.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativeparser_p.h"
43 
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"
54 
55 #include <QStack>
56 #include <QColor>
57 #include <QPointF>
58 #include <QSizeF>
59 #include <QRectF>
60 #include <QStringBuilder>
61 #include <QtDebug>
62 
64 
65 using namespace QDeclarativeJS;
66 using namespace QDeclarativeParser;
67 
69 : type(-1), majorVersion(-1), minorVersion(-1), idIndex(-1), metatype(0), synthCache(0), defaultProperty(0), parserStatusCast(-1)
70 {
71  // initialize the members in the meta object
72  extObject.d.superdata = 0;
74  extObject.d.data = 0;
75  extObject.d.extradata = 0;
76 }
77 
79 {
82  foreach(Property *prop, properties)
83  prop->release();
84  foreach(Property *prop, valueProperties)
85  prop->release();
86  foreach(Property *prop, signalProperties)
87  prop->release();
88  foreach(Property *prop, attachedProperties)
89  prop->release();
90  foreach(Property *prop, groupedProperties)
91  prop->release();
92  foreach(Property *prop, valueTypeProperties)
93  prop->release();
94  typedef QPair<Property *, int> PropPair;
95  foreach(const PropPair &prop, scriptStringProperties)
96  prop.first->release();
97  foreach(const DynamicProperty &prop, dynamicProperties)
98  if (prop.defaultValue) prop.defaultValue->release();
99 }
100 
102 {
103  while (bindingBitmask.size() < 4 * (1 + b / 32))
104  bindingBitmask.append(char(0));
105 
106  quint32 *bits = (quint32 *)bindingBitmask.data();
107  bits[b / 32] |= (1 << (b % 32));
108 }
109 
111 {
112  if (!metadata.isEmpty() && metatype)
113  return &extObject;
114  else
115  return metatype;
116 }
117 
119 {
120  if (!defaultProperty) {
122  defaultProperty->parent = this;
123  }
124  return defaultProperty;
125 }
126 
128 {
129  p->addref();
130  valueProperties << p;
131 }
132 
134 {
135  p->addref();
136  signalProperties << p;
137 }
138 
140 {
141  p->addref();
142  attachedProperties << p;
143 }
144 
146 {
147  p->addref();
148  groupedProperties << p;
149 }
150 
152 {
153  p->addref();
154  valueTypeProperties << p;
155 }
156 
158 {
159  p->addref();
160  scriptStringProperties << qMakePair(p, stack);
161 }
162 
163 
165 {
166  if (!properties.contains(name)) {
167  if (create) {
168  Property *property = new Property(name);
169  property->parent = this;
170  properties.insert(name, property);
171  } else {
172  return 0;
173  }
174  }
175  return properties[name];
176 }
177 
179 : isDefaultProperty(false), type(Variant), defaultValue(0)
180 {
181 }
182 
185  type(o.type),
187  name(o.name),
189  location(o.location)
190 {
191 }
192 
194 {
195 }
196 
198 : name(o.name), parameterTypes(o.parameterTypes),
199  parameterNames(o.parameterNames)
200 {
201 }
202 
204 {
205 }
206 
208 : name(o.name), body(o.body), parameterNames(o.parameterNames), location(o.location)
209 {
210 }
211 
213 : parent(0), type(0), index(-1), value(0), isDefault(true), isDeferred(false),
214  isValueTypeSubProperty(false), isAlias(false)
215 {
216 }
217 
219 : parent(0), type(0), index(-1), value(0), name(n), isDefault(false),
220  isDeferred(false), isValueTypeSubProperty(false), isAlias(false)
221 {
222 }
223 
225 {
226  foreach(Value *value, values)
227  value->release();
228  foreach(Value *value, onValues)
229  value->release();
230  if (value) value->release();
231 }
232 
234 {
236  return value;
237 }
238 
240 {
241  values << v;
242 }
243 
245 {
246  onValues << v;
247 }
248 
250 {
251  return !value && values.isEmpty() && onValues.isEmpty();
252 }
253 
255 : type(Unknown), object(0)
256 {
257 }
258 
260 {
261  if (object) object->release();
262 }
263 
265 : t(Invalid) {}
266 
268 : t(o.t), d(o.d), s(o.s)
269 {
270 }
271 
273 : t(Boolean), b(v)
274 {
275 }
276 
278 : t(Number), d(v), s(asWritten)
279 {
280 }
281 
283 : t(String), s(v)
284 {
285 }
286 
288 : t(Script), n(n), s(v)
289 {
290 }
291 
293 {
294  t = o.t;
295  d = o.d;
296  s = o.s;
297  return *this;
298 }
299 
301 {
302  return t;
303 }
304 
306 {
307  return b;
308 }
309 
311 {
312  return s;
313 }
314 
316 {
317  return d;
318 }
319 
320 //reverse of Lexer::singleEscape()
322 {
323  QString tmp = QLatin1String("\"");
324  for (int i = 0; i < string.length(); ++i) {
325  const QChar &c = string.at(i);
326  switch(c.unicode()) {
327  case 0x08:
328  tmp += QLatin1String("\\b");
329  break;
330  case 0x09:
331  tmp += QLatin1String("\\t");
332  break;
333  case 0x0A:
334  tmp += QLatin1String("\\n");
335  break;
336  case 0x0B:
337  tmp += QLatin1String("\\v");
338  break;
339  case 0x0C:
340  tmp += QLatin1String("\\f");
341  break;
342  case 0x0D:
343  tmp += QLatin1String("\\r");
344  break;
345  case 0x22:
346  tmp += QLatin1String("\\\"");
347  break;
348  case 0x27:
349  tmp += QLatin1String("\\\'");
350  break;
351  case 0x5C:
352  tmp += QLatin1String("\\\\");
353  break;
354  default:
355  tmp += c;
356  break;
357  }
358  }
359  tmp += QLatin1Char('\"');
360  return tmp;
361 }
362 
364 {
365  switch(type()) {
366  default:
367  case Invalid:
368  return QString();
369  case Boolean:
370  return b?QLatin1String("true"):QLatin1String("false");
371  case Number:
372  if (s.isEmpty())
373  return QString::number(d);
374  else
375  return s;
376  case String:
377  return escapedString(s);
378  case Script:
379  return s;
380  }
381 }
382 
384 {
385  if (type() == Script)
386  return n;
387  else
388  return 0;
389 }
390 
392 {
393  if (isString())
394  return true;
395 
396  if (type() != Script || !n)
397  return false;
398 
399  AST::ArrayLiteral *array = AST::cast<AST::ArrayLiteral *>(n);
400  if (!array)
401  return false;
402 
404 
405  while (elements) {
406 
407  if (!AST::cast<AST::StringLiteral *>(elements->expression))
408  return false;
409 
410  elements = elements->next;
411  }
412 
413  return true;
414 }
415 
417 {
418  QStringList rv;
419  if (isString()) {
420  rv << asString();
421  return rv;
422  }
423 
424  AST::ArrayLiteral *array = AST::cast<AST::ArrayLiteral *>(n);
425  if (!array)
426  return rv;
427 
429  while (elements) {
430 
431  AST::StringLiteral *string = AST::cast<AST::StringLiteral *>(elements->expression);
432  if (!string)
433  return QStringList();
434  rv.append(string->value->asString());
435 
436  elements = elements->next;
437  }
438 
439  return rv;
440 }
441 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
const QString asString() const
int type
Definition: qmetatype.cpp:239
unsigned char c[8]
Definition: qnumeric_p.h:62
const void * extradata
Definition: qobjectdefs.h:472
QList< Property * > attachedProperties
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void addScriptStringProperty(Property *, int=0)
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QByteArray & append(char c)
Appends the character ch to this byte array.
Variant & operator=(const Variant &)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
T1 first
Definition: qpair.h:65
QList< Property * > valueProperties
const QMetaObject * metaObject() const
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static Object * object(const QScriptValue &)
The QString class provides a Unicode character string.
Definition: qstring.h:83
const char * stringdata
Definition: qobjectdefs.h:470
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QAbstractDynamicMetaObject extObject
QDeclarativePropertyCache * synthCache
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QList< DynamicProperty > dynamicProperties
const char * name
QList< QPair< Property *, int > > scriptStringProperties
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QString escapedString(const QString &string)
const uint * data
Definition: qobjectdefs.h:471
QList< Property * > signalProperties
QDeclarativeJS::AST::Node * n
QDeclarativeJS::AST::Node * asAST() const
QList< Property * > groupedProperties
QList< Property * > valueTypeProperties
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
unsigned int quint32
Definition: qglobal.h:938
const QMetaObject * superdata
Definition: qobjectdefs.h:469
const char * property
Definition: qwizard.cpp:138
struct QMetaObject::@38 d
QFactoryLoader * l
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
quint16 index
static const QTextHtmlElement elements[Html_NumElements]
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
Property * getProperty(const QByteArray &name, bool create=true)
Object * getValue(const LocationSpan &)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QHash< QByteArray, Property * > properties