Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecompileddata.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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/qdeclarativecompiler_p.h"
43 #include "qdeclarativeengine.h"
44 #include "qdeclarativecomponent.h"
45 #include "private/qdeclarativecomponent_p.h"
46 #include "qdeclarativecontext.h"
47 #include "private/qdeclarativecontext_p.h"
48
49 #include <QtCore/qdebug.h>
50
51 #include <private/qobject_p.h>
52
53 QT_BEGIN_NAMESPACE
54
55 int QDeclarativeCompiledData::pack(const char *data, size_t size)
56 {
57     const char *p = packData.constData();
58     unsigned int ps = packData.size();
59
60     for (unsigned int ii = 0; (ii + size) <= ps; ii += sizeof(int)) {
61         if (0 == ::memcmp(p + ii, data, size))
62             return ii;
63     }
64
65     int rv = packData.size();
66     packData.append(data, size);
67     return rv;
68 }
69
70 int QDeclarativeCompiledData::indexForString(const QString &data)
71 {
72     int idx = primitives.indexOf(data);
73     if (idx == -1) {
74         idx = primitives.count();
75         primitives << data;
76     }
77     return idx;
78 }
79
80 int QDeclarativeCompiledData::indexForByteArray(const QByteArray &data)
81 {
82     int idx = datas.indexOf(data);
83     if (idx == -1) {
84         idx = datas.count();
85         datas << data;
86     }
87     return idx;
88 }
89
90 int QDeclarativeCompiledData::indexForUrl(const QUrl &data)
91 {
92     int idx = urls.indexOf(data);
93     if (idx == -1) {
94         idx = urls.count();
95         urls << data;
96     }
97     return idx;
98 }
99
100 int QDeclarativeCompiledData::indexForFloat(float *data, int count)
101 {
102     Q_ASSERT(count > 0);
103
104     for (int ii = 0; ii <= floatData.count() - count; ++ii) {
105         bool found = true;
106         for (int jj = 0; jj < count; ++jj) {
107             if (floatData.at(ii + jj) != data[jj]) {
108                 found = false;
109                 break;
110             }
111         }
112
113         if (found)
114             return ii;
115     }
116
117     int idx = floatData.count();
118     for (int ii = 0; ii < count; ++ii)
119         floatData << data[ii];
120
121     return idx;
122 }
123
124 int QDeclarativeCompiledData::indexForInt(int *data, int count)
125 {
126     Q_ASSERT(count > 0);
127
128     for (int ii = 0; ii <= intData.count() - count; ++ii) {
129         bool found = true;
130         for (int jj = 0; jj < count; ++jj) {
131             if (intData.at(ii + jj) != data[jj]) {
132                 found = false;
133                 break;
134             }
135         }
136
137         if (found)
138             return ii;
139     }
140
141     int idx = intData.count();
142     for (int ii = 0; ii < count; ++ii)
143         intData << data[ii];
144
145     return idx;
146 }
147
148 int QDeclarativeCompiledData::indexForLocation(const QDeclarativeParser::Location &l)
149 {
150     // ### FIXME
151     int rv = locations.count();
152     locations << l;
153     return rv;
154 }
155
156 int QDeclarativeCompiledData::indexForLocation(const QDeclarativeParser::LocationSpan &l)
157 {
158     // ### FIXME
159     int rv = locations.count();
160     locations << l.start << l.end;
161     return rv;
162 }
163
164 QDeclarativeCompiledData::QDeclarativeCompiledData(QDeclarativeEngine *engine)
165 : QDeclarativeCleanup(engine), importCache(0), root(0), rootPropertyCache(0)
166 {
167 }
168
169 QDeclarativeCompiledData::~QDeclarativeCompiledData()
170 {
171     for (int ii = 0; ii < types.count(); ++ii) {
172         if (types.at(ii).component)
173             types.at(ii).component->release();
174         if (types.at(ii).typePropertyCache)
175             types.at(ii).typePropertyCache->release();
176     }
177
178     for (int ii = 0; ii < propertyCaches.count(); ++ii) 
179         propertyCaches.at(ii)->release();
180
181     for (int ii = 0; ii < contextCaches.count(); ++ii)
182         contextCaches.at(ii)->release();
183
184     if (importCache)
185         importCache->release();
186
187     if (rootPropertyCache)
188         rootPropertyCache->release();
189
190     qDeleteAll(cachedPrograms);
191     qDeleteAll(cachedClosures);
192 }
193
194 void QDeclarativeCompiledData::clear()
195 {
196     qDeleteAll(cachedPrograms);
197     qDeleteAll(cachedClosures);
198     for (int ii = 0; ii < cachedClosures.count(); ++ii)
199         cachedClosures[ii] = 0;
200     for (int ii = 0; ii < cachedPrograms.count(); ++ii)
201         cachedPrograms[ii] = 0;
202 }
203
204 const QMetaObject *QDeclarativeCompiledData::TypeReference::metaObject() const
205 {
206     if (type) {
207         return type->metaObject();
208     } else {
209         Q_ASSERT(component);
210         return component->root;
211     }
212 }
213
214 /*!
215 Returns the property cache, if one alread exists.  The cache is not referenced.
216 */
217 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::propertyCache() const
218 {
219     if (type)
220         return typePropertyCache;
221     else
222         return component->rootPropertyCache;
223 }
224
225 /*!
226 Returns the property cache, creating one if it doesn't already exist.  The cache is not referenced.
227 */
228 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::createPropertyCache(QDeclarativeEngine *engine) 
229 {
230     if (typePropertyCache) {
231         return typePropertyCache;
232     } else if (type) {
233         typePropertyCache = QDeclarativeEnginePrivate::get(engine)->cache(type->metaObject());
234         typePropertyCache->addref();
235         return typePropertyCache;
236     } else {
237         return component->rootPropertyCache;
238     }
239 }
240
241
242 void QDeclarativeCompiledData::dumpInstructions()
243 {
244     if (!name.isEmpty())
245         qWarning() << name;
246     qWarning().nospace() << "Index\tLine\tOperation\t\tData1\tData2\tData3\tComments";
247     qWarning().nospace() << "-------------------------------------------------------------------------------";
248     for (int ii = 0; ii < bytecode.count(); ++ii) {
249         dump(&bytecode[ii], ii);
250     }
251     qWarning().nospace() << "-------------------------------------------------------------------------------";
252 }
253
254
255 QT_END_NAMESPACE