/
qqmlcompiler.cpp
3906 lines (3243 loc) · 141 KB
/
qqmlcompiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qqmlcompiler_p.h"
#include "qqmlpropertyvaluesource.h"
#include "qqmlcomponent.h"
#include <private/qmetaobjectbuilder_p.h>
#include <private/qfastmetabuilder_p.h>
#include "qqmlstringconverters_p.h"
#include "qqmlengine_p.h"
#include "qqmlengine.h"
#include "qqmlcontext.h"
#include "qqmlmetatype_p.h"
#include "qqmlcustomparser_p_p.h"
#include "qqmlcontext_p.h"
#include "qqmlcomponent_p.h"
#include <private/qqmljsast_p.h>
#include "qqmlvmemetaobject_p.h"
#include "qqmlexpression_p.h"
#include "qqmlproperty_p.h"
#include "qqmlrewrite_p.h"
#include "qqmlscriptstring.h"
#include "qqmlglobal_p.h"
#include "qqmlbinding_p.h"
#include <private/qv4compiler_p.h>
#include <QColor>
#include <QDebug>
#include <QPointF>
#include <QSizeF>
#include <QRectF>
#include <QAtomicInt>
#include <QtCore/qdebug.h>
#include <QtCore/qdatetime.h>
Q_DECLARE_METATYPE(QList<int>)
Q_DECLARE_METATYPE(QList<qreal>)
Q_DECLARE_METATYPE(QList<bool>)
Q_DECLARE_METATYPE(QList<QString>)
Q_DECLARE_METATYPE(QList<QUrl>)
QT_BEGIN_NAMESPACE
DEFINE_BOOL_CONFIG_OPTION(compilerDump, QML_COMPILER_DUMP);
DEFINE_BOOL_CONFIG_OPTION(compilerStatDump, QML_COMPILER_STATS);
using namespace QQmlJS;
using namespace QQmlScript;
using namespace QQmlCompilerTypes;
static QString id_string(QLatin1String("id"));
static QString on_string(QLatin1String("on"));
static QString Changed_string(QLatin1String("Changed"));
static QString Component_string(QLatin1String("Component"));
static QString Component_import_string(QLatin1String("QML/Component"));
static QString qsTr_string(QLatin1String("qsTr"));
static QString qsTrId_string(QLatin1String("qsTrId"));
/*!
Instantiate a new QQmlCompiler.
*/
QQmlCompiler::QQmlCompiler(QQmlPool *pool)
: pool(pool), output(0), engine(0), unitRoot(0), unit(0), cachedComponentTypeRef(-1),
cachedTranslationContextIndex(-1), componentStats(0)
{
if (compilerStatDump())
componentStats = pool->New<ComponentStats>();
}
/*!
Returns true if the last call to compile() caused errors.
\sa errors()
*/
bool QQmlCompiler::isError() const
{
return !exceptions.isEmpty();
}
/*!
Return the list of errors from the last call to compile(), or an empty list
if there were no errors.
*/
QList<QQmlError> QQmlCompiler::errors() const
{
return exceptions;
}
/*!
Returns true if \a name refers to an attached property, false otherwise.
Attached property names are those that start with a capital letter.
*/
bool QQmlCompiler::isAttachedPropertyName(const QString &name)
{
return isAttachedPropertyName(QHashedStringRef(&name));
}
bool QQmlCompiler::isAttachedPropertyName(const QHashedStringRef &name)
{
return !name.isEmpty() && name.at(0).isUpper();
}
/*!
Returns true if \a name refers to a signal property, false otherwise.
Signal property names are those that start with "on", followed by a first
character which is either a capital letter or one or more underscores followed
by a capital letter, which is then followed by other allowed characters.
Note that although ECMA-262r3 supports dollarsigns and escaped unicode
character codes in property names, for simplicity and performance reasons
QML only supports letters, numbers and underscores.
*/
bool QQmlCompiler::isSignalPropertyName(const QString &name)
{
return isSignalPropertyName(QStringRef(&name));
}
bool QQmlCompiler::isSignalPropertyName(const QHashedStringRef &name)
{
if (name.length() < 3) return false;
if (!name.startsWith(on_string)) return false;
int ns = name.length();
for (int i = 2; i < ns; ++i) {
const QChar curr = name.at(i);
if (curr.unicode() == '_') continue;
if (curr.isUpper()) return true;
return false;
}
return false; // consists solely of underscores - invalid.
}
/*!
\macro COMPILE_EXCEPTION
\internal
Inserts an error into the QQmlCompiler error list, and returns false
(failure).
\a token is used to source the error line and column, and \a desc is the
error itself. \a desc can be an expression that can be piped into QDebug.
For example:
\code
COMPILE_EXCEPTION(property, tr("Error for property \"%1\"").arg(property->name));
\endcode
*/
#define COMPILE_EXCEPTION_LOCATION(line, column, desc) \
{ \
QQmlError error; \
error.setUrl(output->url); \
error.setLine(line); \
error.setColumn(column); \
error.setDescription(desc.trimmed()); \
exceptions << error; \
return false; \
}
#define COMPILE_EXCEPTION(token, desc) \
COMPILE_EXCEPTION_LOCATION((token)->location.start.line, (token)->location.start.column, desc)
/*!
\macro COMPILE_CHECK
\internal
Returns false if \a is false, otherwise does nothing.
*/
#define COMPILE_CHECK(a) \
{ \
if (!a) return false; \
}
/*!
Returns true if literal \a v can be assigned to property \a prop, otherwise
false.
This test corresponds to action taken by genLiteralAssignment(). Any change
made here, must have a corresponding action in genLiteralAssigment().
*/
bool QQmlCompiler::testLiteralAssignment(QQmlScript::Property *prop,
QQmlScript::Value *v)
{
const QQmlScript::Variant &value = v->value;
if (!prop->core.isWritable() && !prop->isReadOnlyDeclaration)
COMPILE_EXCEPTION(v, tr("Invalid property assignment: \"%1\" is a read-only property").arg(prop->name().toString()));
if (prop->core.isEnum()) {
QMetaProperty p = prop->parent->metaObject()->property(prop->index);
int enumValue;
bool ok;
if (p.isFlagType()) {
enumValue = p.enumerator().keysToValue(value.asString().toUtf8().constData(), &ok);
} else
enumValue = p.enumerator().keyToValue(value.asString().toUtf8().constData(), &ok);
if (!ok)
COMPILE_EXCEPTION(v, tr("Invalid property assignment: unknown enumeration"));
v->value = QQmlScript::Variant((double)enumValue);
return true;
}
int type = prop->type;
switch(type) {
case QMetaType::QVariant:
break;
case QVariant::String:
if (!v->value.isString()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: string expected"));
break;
case QVariant::StringList: // we expect a string literal. A string list is not a literal assignment.
if (!v->value.isString()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: string or string list expected"));
break;
case QVariant::ByteArray:
if (!v->value.isString()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: byte array expected"));
break;
case QVariant::Url:
if (!v->value.isString()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: url expected"));
break;
case QVariant::RegExp:
COMPILE_EXCEPTION(v, tr("Invalid property assignment: regular expression expected; use /pattern/ syntax"));
break;
case QVariant::UInt:
{
bool ok = v->value.isNumber();
if (ok) {
double n = v->value.asNumber();
if (double(uint(n)) != n)
ok = false;
}
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: unsigned int expected"));
}
break;
case QVariant::Int:
{
bool ok = v->value.isNumber();
if (ok) {
double n = v->value.asNumber();
if (double(int(n)) != n)
ok = false;
}
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: int expected"));
}
break;
case QMetaType::Float:
if (!v->value.isNumber()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: number expected"));
break;
case QVariant::Double:
if (!v->value.isNumber()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: number expected"));
break;
case QVariant::Color:
{
bool ok;
QQmlStringConverters::colorFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: color expected"));
}
break;
#ifndef QT_NO_DATESTRING
case QVariant::Date:
{
bool ok;
QQmlStringConverters::dateFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: date expected"));
}
break;
case QVariant::Time:
{
bool ok;
QQmlStringConverters::timeFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: time expected"));
}
break;
case QVariant::DateTime:
{
bool ok;
QQmlStringConverters::dateTimeFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: datetime expected"));
}
break;
#endif // QT_NO_DATESTRING
case QVariant::Point:
case QVariant::PointF:
{
bool ok;
QQmlStringConverters::pointFFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: point expected"));
}
break;
case QVariant::Size:
case QVariant::SizeF:
{
bool ok;
QQmlStringConverters::sizeFFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: size expected"));
}
break;
case QVariant::Rect:
case QVariant::RectF:
{
bool ok;
QQmlStringConverters::rectFFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: rect expected"));
}
break;
case QVariant::Bool:
{
if (!v->value.isBoolean()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: boolean expected"));
}
break;
case QVariant::Vector3D:
{
bool ok;
QQmlStringConverters::vector3DFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: 3D vector expected"));
}
break;
case QVariant::Vector4D:
{
bool ok;
QQmlStringConverters::vector4DFromString(value.asString(), &ok);
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: 4D vector expected"));
}
break;
default:
{
// check if assigning a literal value to a list property.
// in each case, check the singular, since an Array of the specified type
// will not go via this literal assignment codepath.
if (type == qMetaTypeId<QList<qreal> >()) {
if (!v->value.isNumber()) {
COMPILE_EXCEPTION(v, tr("Invalid property assignment: real or array of reals expected"));
}
break;
} else if (type == qMetaTypeId<QList<int> >()) {
bool ok = v->value.isNumber();
if (ok) {
double n = v->value.asNumber();
if (double(int(n)) != n)
ok = false;
}
if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: int or array of ints expected"));
break;
} else if (type == qMetaTypeId<QList<bool> >()) {
if (!v->value.isBoolean()) {
COMPILE_EXCEPTION(v, tr("Invalid property assignment: bool or array of bools expected"));
}
break;
} else if (type == qMetaTypeId<QList<QString> >()) { // we expect a string literal. A string list is not a literal assignment.
if (!v->value.isString()) {
COMPILE_EXCEPTION(v, tr("Invalid property assignment: string or array of strings expected"));
}
break;
} else if (type == qMetaTypeId<QList<QUrl> >()) {
if (!v->value.isString()) {
COMPILE_EXCEPTION(v, tr("Invalid property assignment: url or array of urls expected"));
}
break;
}
// otherwise, check for existence of string converter to custom type
QQmlMetaType::StringConverter converter = QQmlMetaType::customStringConverter(type);
if (!converter)
COMPILE_EXCEPTION(v, tr("Invalid property assignment: unsupported type \"%1\"").arg(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type))));
}
break;
}
return true;
}
static QUrl urlFromUserString(const QString &data)
{
QUrl u;
// Preserve any valid percent-encoded octets supplied by the source
u.setEncodedUrl(data.toUtf8(), QUrl::TolerantMode);
return u;
}
/*!
Generate a store instruction for assigning literal \a v to property \a prop.
Any literal assignment that is approved in testLiteralAssignment() must have
a corresponding action in this method.
*/
void QQmlCompiler::genLiteralAssignment(QQmlScript::Property *prop,
QQmlScript::Value *v)
{
if (prop->core.isEnum()) {
Q_ASSERT(v->value.isNumber());
// Preresolved value
int value = (int)v->value.asNumber();
Instruction::StoreInteger instr;
instr.propertyIndex = prop->index;
instr.value = value;
output->addInstruction(instr);
return;
}
int type = prop->type;
switch(type) {
case QMetaType::QVariant:
{
if (v->value.isNumber()) {
double n = v->value.asNumber();
if (double(int(n)) == n) {
if (prop->core.isVMEProperty()) {
Instruction::StoreVarInteger instr;
instr.propertyIndex = prop->index;
instr.value = int(n);
output->addInstruction(instr);
} else {
Instruction::StoreVariantInteger instr;
instr.propertyIndex = prop->index;
instr.value = int(n);
output->addInstruction(instr);
}
} else {
if (prop->core.isVMEProperty()) {
Instruction::StoreVarDouble instr;
instr.propertyIndex = prop->index;
instr.value = n;
output->addInstruction(instr);
} else {
Instruction::StoreVariantDouble instr;
instr.propertyIndex = prop->index;
instr.value = n;
output->addInstruction(instr);
}
}
} else if (v->value.isBoolean()) {
if (prop->core.isVMEProperty()) {
Instruction::StoreVarBool instr;
instr.propertyIndex = prop->index;
instr.value = v->value.asBoolean();
output->addInstruction(instr);
} else {
Instruction::StoreVariantBool instr;
instr.propertyIndex = prop->index;
instr.value = v->value.asBoolean();
output->addInstruction(instr);
}
} else {
if (prop->core.isVMEProperty()) {
Instruction::StoreVar instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForString(v->value.asString());
output->addInstruction(instr);
} else {
Instruction::StoreVariant instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForString(v->value.asString());
output->addInstruction(instr);
}
}
}
break;
case QVariant::String:
{
Instruction::StoreString instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForString(v->value.asString());
output->addInstruction(instr);
}
break;
case QVariant::StringList:
{
Instruction::StoreStringList instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForString(v->value.asString());
output->addInstruction(instr);
}
break;
case QVariant::ByteArray:
{
Instruction::StoreByteArray instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForByteArray(v->value.asString().toLatin1());
output->addInstruction(instr);
}
break;
case QVariant::Url:
{
Instruction::StoreUrl instr;
QString string = v->value.asString();
QUrl u = string.isEmpty() ? QUrl() : output->url.resolved(urlFromUserString(string));
instr.propertyIndex = prop->index;
instr.value = output->indexForUrl(u);
output->addInstruction(instr);
}
break;
case QVariant::UInt:
{
Instruction::StoreInteger instr;
instr.propertyIndex = prop->index;
instr.value = uint(v->value.asNumber());
output->addInstruction(instr);
}
break;
case QVariant::Int:
{
Instruction::StoreInteger instr;
instr.propertyIndex = prop->index;
instr.value = int(v->value.asNumber());
output->addInstruction(instr);
}
break;
case QMetaType::Float:
{
Instruction::StoreFloat instr;
instr.propertyIndex = prop->index;
instr.value = float(v->value.asNumber());
output->addInstruction(instr);
}
break;
case QVariant::Double:
{
Instruction::StoreDouble instr;
instr.propertyIndex = prop->index;
instr.value = v->value.asNumber();
output->addInstruction(instr);
}
break;
case QVariant::Color:
{
Instruction::StoreColor instr;
QColor c = QQmlStringConverters::colorFromString(v->value.asString());
instr.propertyIndex = prop->index;
instr.value = c.rgba();
output->addInstruction(instr);
}
break;
#ifndef QT_NO_DATESTRING
case QVariant::Date:
{
Instruction::StoreDate instr;
QDate d = QQmlStringConverters::dateFromString(v->value.asString());
instr.propertyIndex = prop->index;
instr.value = d.toJulianDay();
output->addInstruction(instr);
}
break;
case QVariant::Time:
{
Instruction::StoreTime instr;
QTime time = QQmlStringConverters::timeFromString(v->value.asString());
instr.propertyIndex = prop->index;
Q_ASSERT(sizeof(instr.time) == sizeof(QTime));
::memcpy(&instr.time, &time, sizeof(QTime));
output->addInstruction(instr);
}
break;
case QVariant::DateTime:
{
Instruction::StoreDateTime instr;
QDateTime dateTime = QQmlStringConverters::dateTimeFromString(v->value.asString());
QTime time = dateTime.time();
instr.propertyIndex = prop->index;
instr.date = dateTime.date().toJulianDay();
Q_ASSERT(sizeof(instr.time) == sizeof(QTime));
::memcpy(&instr.time, &time, sizeof(QTime));
output->addInstruction(instr);
}
break;
#endif // QT_NO_DATESTRING
case QVariant::Point:
{
Instruction::StorePoint instr;
bool ok;
QPoint point = QQmlStringConverters::pointFFromString(v->value.asString(), &ok).toPoint();
instr.propertyIndex = prop->index;
instr.point.xp = point.x();
instr.point.yp = point.y();
output->addInstruction(instr);
}
break;
case QVariant::PointF:
{
Instruction::StorePointF instr;
bool ok;
QPointF point = QQmlStringConverters::pointFFromString(v->value.asString(), &ok);
instr.propertyIndex = prop->index;
instr.point.xp = point.x();
instr.point.yp = point.y();
output->addInstruction(instr);
}
break;
case QVariant::Size:
{
Instruction::StoreSize instr;
bool ok;
QSize size = QQmlStringConverters::sizeFFromString(v->value.asString(), &ok).toSize();
instr.propertyIndex = prop->index;
instr.size.wd = size.width();
instr.size.ht = size.height();
output->addInstruction(instr);
}
break;
case QVariant::SizeF:
{
Instruction::StoreSizeF instr;
bool ok;
QSizeF size = QQmlStringConverters::sizeFFromString(v->value.asString(), &ok);
instr.propertyIndex = prop->index;
instr.size.wd = size.width();
instr.size.ht = size.height();
output->addInstruction(instr);
}
break;
case QVariant::Rect:
{
Instruction::StoreRect instr;
bool ok;
QRect rect = QQmlStringConverters::rectFFromString(v->value.asString(), &ok).toRect();
instr.propertyIndex = prop->index;
instr.rect.x1 = rect.left();
instr.rect.y1 = rect.top();
instr.rect.x2 = rect.right();
instr.rect.y2 = rect.bottom();
output->addInstruction(instr);
}
break;
case QVariant::RectF:
{
Instruction::StoreRectF instr;
bool ok;
QRectF rect = QQmlStringConverters::rectFFromString(v->value.asString(), &ok);
instr.propertyIndex = prop->index;
instr.rect.xp = rect.left();
instr.rect.yp = rect.top();
instr.rect.w = rect.width();
instr.rect.h = rect.height();
output->addInstruction(instr);
}
break;
case QVariant::Bool:
{
Instruction::StoreBool instr;
bool b = v->value.asBoolean();
instr.propertyIndex = prop->index;
instr.value = b;
output->addInstruction(instr);
}
break;
case QVariant::Vector3D:
{
Instruction::StoreVector3D instr;
bool ok;
QVector3D vector = QQmlStringConverters::vector3DFromString(v->value.asString(), &ok);
instr.propertyIndex = prop->index;
instr.vector.xp = vector.x();
instr.vector.yp = vector.y();
instr.vector.zp = vector.z();
output->addInstruction(instr);
}
break;
case QVariant::Vector4D:
{
Instruction::StoreVector4D instr;
bool ok;
QVector4D vector = QQmlStringConverters::vector4DFromString(v->value.asString(), &ok);
instr.propertyIndex = prop->index;
instr.vector.xp = vector.x();
instr.vector.yp = vector.y();
instr.vector.zp = vector.z();
instr.vector.wp = vector.w();
output->addInstruction(instr);
}
break;
default:
{
// generate single literal value assignment to a list property if required
if (type == qMetaTypeId<QList<qreal> >()) {
Instruction::StoreDoubleQList instr;
instr.propertyIndex = prop->index;
instr.value = v->value.asNumber();
output->addInstruction(instr);
break;
} else if (type == qMetaTypeId<QList<int> >()) {
Instruction::StoreIntegerQList instr;
instr.propertyIndex = prop->index;
instr.value = int(v->value.asNumber());
output->addInstruction(instr);
break;
} else if (type == qMetaTypeId<QList<bool> >()) {
Instruction::StoreBoolQList instr;
bool b = v->value.asBoolean();
instr.propertyIndex = prop->index;
instr.value = b;
output->addInstruction(instr);
break;
} else if (type == qMetaTypeId<QList<QUrl> >()) {
Instruction::StoreUrlQList instr;
QString string = v->value.asString();
QUrl u = string.isEmpty() ? QUrl() : output->url.resolved(urlFromUserString(string));
instr.propertyIndex = prop->index;
instr.value = output->indexForUrl(u);
output->addInstruction(instr);
break;
} else if (type == qMetaTypeId<QList<QString> >()) {
Instruction::StoreStringQList instr;
instr.propertyIndex = prop->index;
instr.value = output->indexForString(v->value.asString());
output->addInstruction(instr);
break;
}
// otherwise, generate custom type literal assignment
Instruction::AssignCustomType instr;
instr.propertyIndex = prop->index;
instr.primitive = output->indexForString(v->value.asString());
instr.type = type;
output->addInstruction(instr);
}
break;
}
}
/*!
Resets data by clearing the lists that the QQmlCompiler modifies.
*/
void QQmlCompiler::reset(QQmlCompiledData *data)
{
data->types.clear();
data->primitives.clear();
data->datas.clear();
data->bytecode.resize(0);
}
/*!
Compile \a unit, and store the output in \a out. \a engine is the QQmlEngine
with which the QQmlCompiledData will be associated.
Returns true on success, false on failure. On failure, the compile errors
are available from errors().
If the environment variant QML_COMPILER_DUMP is set
(eg. QML_COMPILER_DUMP=1) the compiled instructions will be dumped to stderr
on a successful compiler.
*/
bool QQmlCompiler::compile(QQmlEngine *engine,
QQmlTypeData *unit,
QQmlCompiledData *out)
{
exceptions.clear();
Q_ASSERT(out);
reset(out);
QQmlScript::Object *root = unit->parser().tree();
Q_ASSERT(root);
this->engine = engine;
this->enginePrivate = QQmlEnginePrivate::get(engine);
this->unit = unit;
this->unitRoot = root;
this->output = out;
// Compile types
const QList<QQmlTypeData::TypeReference> &resolvedTypes = unit->resolvedTypes();
QList<QQmlScript::TypeReference *> referencedTypes = unit->parser().referencedTypes();
for (int ii = 0; ii < resolvedTypes.count(); ++ii) {
QQmlCompiledData::TypeReference ref;
const QQmlTypeData::TypeReference &tref = resolvedTypes.at(ii);
QQmlScript::TypeReference *parserRef = referencedTypes.at(ii);
if (tref.type) {
ref.type = tref.type;
if (!ref.type->isCreatable()) {
QString err = ref.type->noCreationReason();
if (err.isEmpty())
err = tr( "Element is not creatable.");
COMPILE_EXCEPTION(parserRef->refObjects.first(), err);
}
if (ref.type->containsRevisionedAttributes()) {
QQmlError cacheError;
ref.typePropertyCache = enginePrivate->cache(ref.type, resolvedTypes.at(ii).minorVersion,
cacheError);
if (!ref.typePropertyCache)
COMPILE_EXCEPTION(parserRef->refObjects.first(), cacheError.description());
ref.typePropertyCache->addref();
}
} else if (tref.typeData) {
ref.component = tref.typeData->compiledData();
}
ref.className = parserRef->name;
out->types << ref;
}
compileTree(root);
if (!isError()) {
if (compilerDump())
out->dumpInstructions();
if (componentStats)
dumpStats();
Q_ASSERT(out->rootPropertyCache);
} else {
reset(out);
}
compileState = 0;
output = 0;
this->engine = 0;
this->enginePrivate = 0;
this->unit = 0;
this->cachedComponentTypeRef = -1;
this->cachedTranslationContextIndex = -1;
this->unitRoot = 0;
return !isError();
}
void QQmlCompiler::compileTree(QQmlScript::Object *tree)
{
compileState = pool->New<ComponentCompileState>();
compileState->root = tree;
if (componentStats)
componentStats->componentStat.lineNumber = tree->location.start.line;
// We generate the importCache before we build the tree so that
// it can be used in the binding compiler. Given we "expect" the
// QML compilation to succeed, this isn't a waste.
output->importCache = new QQmlTypeNameCache();
foreach (const QString &ns, unit->namespaces()) {
output->importCache->add(ns);
}
int scriptIndex = 0;
foreach (const QQmlTypeData::ScriptReference &script, unit->resolvedScripts()) {
QString qualifier = script.qualifier;
QString enclosingNamespace;
const int lastDotIndex = qualifier.lastIndexOf(QLatin1Char('.'));
if (lastDotIndex != -1) {
enclosingNamespace = qualifier.left(lastDotIndex);
qualifier = qualifier.mid(lastDotIndex+1);
}
output->importCache->add(qualifier, scriptIndex++, enclosingNamespace);
}
unit->imports().populateCache(output->importCache, engine);
if (!buildObject(tree, BindingContext()) || !completeComponentBuild())
return;
Instruction::Init init;
init.bindingsSize = compileState->totalBindingsCount;
init.parserStatusSize = compileState->parserStatusCount;
init.contextCache = genContextCache();
init.objectStackSize = compileState->objectDepth.maxDepth();
init.listStackSize = compileState->listDepth.maxDepth();
if (compileState->compiledBindingData.isEmpty())
init.compiledBinding = -1;
else
init.compiledBinding = output->indexForByteArray(compileState->compiledBindingData);
output->addInstruction(init);
foreach (const QQmlTypeData::ScriptReference &script, unit->resolvedScripts()) {
Instruction::StoreImportedScript import;
import.value = output->scripts.count();
QQmlScriptData *scriptData = script.script->scriptData();
scriptData->addref();
output->scripts << scriptData;
output->addInstruction(import);
}
if (!compileState->v8BindingProgram.isEmpty()) {
Instruction::InitV8Bindings bindings;
int index = output->programs.count();
typedef QQmlCompiledData::V8Program V8Program;
output->programs.append(V8Program(compileState->v8BindingProgram, output));
bindings.programIndex = index;
bindings.line = compileState->v8BindingProgramLine;
output->addInstruction(bindings);
}
genObject(tree);
Instruction::SetDefault def;
output->addInstruction(def);
Instruction::Done done;
output->addInstruction(done);
Q_ASSERT(tree->metatype);
if (tree->metadata.isEmpty()) {
output->root = tree->metatype;
} else {
static_cast<QMetaObject &>(output->rootData) = *tree->metaObject();
output->root = &output->rootData;
}
if (!tree->metadata.isEmpty())
enginePrivate->registerCompositeType(output);
}
static bool QStringList_contains(const QStringList &list, const QHashedStringRef &string)
{
for (int ii = 0; ii < list.count(); ++ii)
if (string == list.at(ii))
return true;
return false;
}
bool QQmlCompiler::buildObject(QQmlScript::Object *obj, const BindingContext &ctxt)
{
if (componentStats)
componentStats->componentStat.objects++;
Q_ASSERT (obj->type != -1);
const QQmlCompiledData::TypeReference &tr = output->types.at(obj->type);
obj->metatype = tr.metaObject();
if (tr.type)
obj->typeName = tr.type->qmlTypeName();
// This object is a "Component" element
if (tr.type && obj->metatype == &QQmlComponent::staticMetaObject) {
COMPILE_CHECK(buildComponent(obj, ctxt));
return true;
}
if (tr.component) {
typedef QQmlInstruction I;
const I *init = ((const I *)tr.component->bytecode.constData());
Q_ASSERT(init && tr.component->instructionType(init) == QQmlInstruction::Init);
// Adjust stack depths to include nested components
compileState->objectDepth.pushPop(init->init.objectStackSize);
compileState->listDepth.pushPop(init->init.listStackSize);
compileState->parserStatusCount += init->init.parserStatusSize;
compileState->totalBindingsCount += init->init.bindingsSize;
}
compileState->objectDepth.push();
// Object instantiations reset the binding context
BindingContext objCtxt(obj);
// Create the synthesized meta object, ignoring aliases
COMPILE_CHECK(checkDynamicMeta(obj));
COMPILE_CHECK(mergeDynamicMetaProperties(obj));
COMPILE_CHECK(buildDynamicMeta(obj, IgnoreAliases));
// Find the native type and check for the QQmlParserStatus interface
QQmlType *type = toQmlType(obj);
Q_ASSERT(type);
obj->parserStatusCast = type->parserStatusCast();
if (obj->parserStatusCast != -1)
compileState->parserStatusCount++;