summaryrefslogtreecommitdiff
path: root/src/libs/qmljs/qmljsinterpreter.h
blob: 1756fbf3f6da0923e5be956c7fcba6434a65f1e9 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef QMLJS_INTERPRETER_H
#define QMLJS_INTERPRETER_H

#include <qmljs/qmljs_global.h>
#include <qmljs/qmljsconstants.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsimportdependencies.h>

#include <QFileInfoList>
#include <QHash>
#include <QList>
#include <QMutex>
#include <QSet>
#include <QSharedPointer>
#include <QString>

namespace QmlJS {
////////////////////////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////////////////////////
class ASTFunctionValue;
class ASTObjectValue;
class ASTPropertyReference;
class ASTSignal;
class ASTVariableReference;
class AnchorLineValue;
class BooleanValue;
class ColorValue;
class Context;
class CppComponentValue;
class Document;
class Function;
class FunctionValue;
class Imports;
class IntValue;
class JSImportScope;
class NameId;
class NullValue;
class NumberValue;
class ObjectValue;
class QmlEnumValue;
class QmlPrototypeReference;
class RealValue;
class Reference;
class ReferenceContext;
class StringValue;
class TypeScope;
class UndefinedValue;
class UnknownValue;
class UrlValue;
class Value;
class ValueOwner;
typedef QSharedPointer<const Context> ContextPtr;

namespace Internal {
class MetaFunction;
class QtObjectPrototypeReference;
} // namespace Internal

typedef QList<const Value *> ValueList;

////////////////////////////////////////////////////////////////////////////////
// Value visitor
////////////////////////////////////////////////////////////////////////////////
class QMLJS_EXPORT ValueVisitor
{
public:
    ValueVisitor();
    virtual ~ValueVisitor();

    virtual void visit(const NullValue *);
    virtual void visit(const UndefinedValue *);
    virtual void visit(const UnknownValue *);
    virtual void visit(const NumberValue *);
    virtual void visit(const BooleanValue *);
    virtual void visit(const StringValue *);
    virtual void visit(const ObjectValue *);
    virtual void visit(const FunctionValue *);
    virtual void visit(const Reference *);
    virtual void visit(const ColorValue *);
    virtual void visit(const AnchorLineValue *);
};

////////////////////////////////////////////////////////////////////////////////
// QML/JS value
////////////////////////////////////////////////////////////////////////////////
class QMLJS_EXPORT Value
{
    Value(const Value &other);
    void operator = (const Value &other);

public:
    Value();
    virtual ~Value();

    virtual const NullValue *asNullValue() const;
    virtual const UndefinedValue *asUndefinedValue() const;
    virtual const UnknownValue *asUnknownValue() const;
    virtual const NumberValue *asNumberValue() const;
    virtual const IntValue *asIntValue() const;
    virtual const RealValue *asRealValue() const;
    virtual const BooleanValue *asBooleanValue() const;
    virtual const StringValue *asStringValue() const;
    virtual const UrlValue *asUrlValue() const;
    virtual const ObjectValue *asObjectValue() const;
    virtual const FunctionValue *asFunctionValue() const;
    virtual const Reference *asReference() const;
    virtual const ColorValue *asColorValue() const;
    virtual const AnchorLineValue *asAnchorLineValue() const;
    virtual const CppComponentValue *asCppComponentValue() const;
    virtual const ASTObjectValue *asAstObjectValue() const;
    virtual const QmlEnumValue *asQmlEnumValue() const;
    virtual const QmlPrototypeReference *asQmlPrototypeReference() const;
    virtual const ASTPropertyReference *asAstPropertyReference() const;
    virtual const ASTVariableReference *asAstVariableReference() const;
    virtual const Internal::QtObjectPrototypeReference *asQtObjectPrototypeReference() const;
    virtual const ASTSignal *asAstSignal() const;
    virtual const ASTFunctionValue *asAstFunctionValue() const;
    virtual const Function *asFunction() const;
    virtual const Internal::MetaFunction *asMetaFunction() const;
    virtual const JSImportScope *asJSImportScope() const;
    virtual const TypeScope *asTypeScope() const;

    virtual void accept(ValueVisitor *) const = 0;

    virtual bool getSourceLocation(QString *fileName, int *line, int *column) const;
};

template <typename RetTy> const RetTy *value_cast(const Value *)
{
    // Produce a good error message if a specialization is missing.
    RetTy::ERROR_MissingValueCastSpecialization();
    return 0;
}

template <> Q_INLINE_TEMPLATE const NullValue *value_cast(const Value *v)
{
    if (v) return v->asNullValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const UndefinedValue *value_cast(const Value *v)
{
    if (v) return v->asUndefinedValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const UnknownValue *value_cast(const Value *v)
{
    if (v) return v->asUnknownValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const NumberValue *value_cast(const Value *v)
{
    if (v) return v->asNumberValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const IntValue *value_cast(const Value *v)
{
    if (v) return v->asIntValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const RealValue *value_cast(const Value *v)
{
    if (v) return v->asRealValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const BooleanValue *value_cast(const Value *v)
{
    if (v) return v->asBooleanValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const StringValue *value_cast(const Value *v)
{
    if (v) return v->asStringValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const UrlValue *value_cast(const Value *v)
{
    if (v) return v->asUrlValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ObjectValue *value_cast(const Value *v)
{
    if (v) return v->asObjectValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ASTFunctionValue *value_cast(const Value *v)
{
    if (v) return v->asAstFunctionValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const FunctionValue *value_cast(const Value *v)
{
    if (v) return v->asFunctionValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const Reference *value_cast(const Value *v)
{
    if (v) return v->asReference();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ColorValue *value_cast(const Value *v)
{
    if (v) return v->asColorValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const AnchorLineValue *value_cast(const Value *v)
{
    if (v) return v->asAnchorLineValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const CppComponentValue *value_cast(const Value *v)
{
    if (v) return v->asCppComponentValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ASTObjectValue *value_cast(const Value *v)
{
    if (v) return v->asAstObjectValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const QmlEnumValue *value_cast(const Value *v)
{
    if (v) return v->asQmlEnumValue();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const QmlPrototypeReference *value_cast(const Value *v)
{
    if (v) return v->asQmlPrototypeReference();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ASTPropertyReference *value_cast(const Value *v)
{
    if (v) return v->asAstPropertyReference();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const Internal::QtObjectPrototypeReference *value_cast(const Value *v)
{
    if (v) return v->asQtObjectPrototypeReference();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ASTVariableReference *value_cast(const Value *v)
{
    if (v) return v->asAstVariableReference();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const Function *value_cast(const Value *v)
{
    if (v) return v->asFunction();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const Internal::MetaFunction*value_cast(const Value *v)
{
    if (v) return v->asMetaFunction();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const JSImportScope *value_cast(const Value *v)
{
    if (v) return v->asJSImportScope();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const TypeScope *value_cast(const Value *v)
{
    if (v) return v->asTypeScope();
    else   return 0;
}

template <> Q_INLINE_TEMPLATE const ASTSignal *value_cast(const Value *v)
{
    if (v) return v->asAstSignal();
    else   return 0;
}

////////////////////////////////////////////////////////////////////////////////
// Value nodes
////////////////////////////////////////////////////////////////////////////////
class QMLJS_EXPORT NullValue: public Value
{
public:
    const NullValue *asNullValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT UndefinedValue: public Value
{
public:
    const UndefinedValue *asUndefinedValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT UnknownValue: public Value
{
public:
    const UnknownValue *asUnknownValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT NumberValue: public Value
{
public:
    const NumberValue *asNumberValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT RealValue: public NumberValue
{
public:
    const RealValue *asRealValue() const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT IntValue: public NumberValue
{
public:
    const IntValue *asIntValue() const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT BooleanValue: public Value
{
public:
    const BooleanValue *asBooleanValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT StringValue: public Value
{
public:
    const StringValue *asStringValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT UrlValue: public StringValue
{
public:
    const UrlValue *asUrlValue() const Q_DECL_OVERRIDE;
};

class PropertyInfo {
public:
    enum PropertyFlag {
        Readable    = 1,
        Writeable   = 2,
        ListType    = 4,
        PointerType= 8,
        ValueType  = 16,
        PointerOrValue = PointerType|ValueType,
        Default     = Readable|Writeable|PointerOrValue
    };

    PropertyInfo(uint flags = Default);
    uint flags;
    bool isPointer() const {
        return (flags & PointerOrValue) == PointerType;
    }
    bool isValue() const {
        return (flags & PointerOrValue) == ValueType;
    }
    bool canBePointer() const {
        return (flags & PointerType) != 0;
    }
    bool canBeValue() const {
        return (flags & ValueType) != 0;
    }
    bool isReadable() const {
        return (flags & Readable) != 0;
    }
    bool isWriteable() const {
        return (flags & Writeable) != 0;
    }
    bool isList() const {
        return (flags & ListType) != 0;
    }
    QString toString() const;
};

class QMLJS_EXPORT MemberProcessor
{
    MemberProcessor(const MemberProcessor &other);
    void operator = (const MemberProcessor &other);

public:
    MemberProcessor();
    virtual ~MemberProcessor();

    // Returns false to stop the processor.
    virtual bool processProperty(const QString &name, const Value *value,
                                 const PropertyInfo &propertyInfo);
    virtual bool processEnumerator(const QString &name, const Value *value);
    virtual bool processSignal(const QString &name, const Value *value);
    virtual bool processSlot(const QString &name, const Value *value);
    virtual bool processGeneratedSlot(const QString &name, const Value *value);
};

class QMLJS_EXPORT Reference: public Value
{
public:
    Reference(ValueOwner *valueOwner);
    ~Reference();

    ValueOwner *valueOwner() const;

    // Value interface
    const Reference *asReference() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *) const Q_DECL_OVERRIDE;

private:
    virtual const Value *value(ReferenceContext *referenceContext) const;

    ValueOwner *m_valueOwner;
    friend class ReferenceContext;
};

class QMLJS_EXPORT ColorValue: public Value
{
public:
    // Value interface
    const ColorValue *asColorValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT AnchorLineValue: public Value
{
public:
    // Value interface
    const AnchorLineValue *asAnchorLineValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT PropertyData {
public:
    const Value *value;
    PropertyInfo propertyInfo;
    PropertyData(const Value *value = 0,
                 PropertyInfo propertyInfo = PropertyInfo(PropertyInfo::Default))
        : value(value), propertyInfo(propertyInfo)
    { }
};

class QMLJS_EXPORT ObjectValue: public Value
{
public:
    ObjectValue(ValueOwner *valueOwner, const QString &originId = QString());
    ~ObjectValue();

    ValueOwner *valueOwner() const;

    QString className() const;
    void setClassName(const QString &className);

    // may return a reference, prototypes may form a cycle: use PrototypeIterator!
    const Value *prototype() const;
    // prototypes may form a cycle: use PrototypeIterator!
    const ObjectValue *prototype(const Context *context) const;
    const ObjectValue *prototype(const ContextPtr &context) const
    { return prototype(context.data()); }
    void setPrototype(const Value *prototype);

    virtual void processMembers(MemberProcessor *processor) const;

    virtual void setMember(const QString &name, const Value *value);
    virtual void setPropertyInfo(const QString &name, const PropertyInfo &propertyInfo);
    virtual void removeMember(const QString &name);

    virtual const Value *lookupMember(const QString &name, const Context *context,
                                      const ObjectValue **foundInObject = 0,
                                      bool examinePrototypes = true) const;
    virtual const Value *lookupMember(const QString &name, const ContextPtr &context,
                              const ObjectValue **foundInObject = 0,
                              bool examinePrototypes = true) const
    { return lookupMember(name, context.data(), foundInObject, examinePrototypes); }

    // Value interface
    const ObjectValue *asObjectValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
    QString originId() const
    { return m_originId; }


private:
    bool checkPrototype(const ObjectValue *prototype, QSet<const ObjectValue *> *processed) const;

private:
    ValueOwner *m_valueOwner;
    QHash<QString, PropertyData> m_members;
    QString m_className;
    QString m_originId;

protected:
    const Value *_prototype;
};

class QMLJS_EXPORT PrototypeIterator
{
public:
    enum Error
    {
        NoError,
        ReferenceResolutionError,
        CycleError
    };

    PrototypeIterator(const ObjectValue *start, const Context *context);
    PrototypeIterator(const ObjectValue *start, const ContextPtr &context);

    bool hasNext();
    const ObjectValue *peekNext();
    const ObjectValue *next();
    Error error() const;

    QList<const ObjectValue *> all();

private:
    const ObjectValue *m_current;
    const ObjectValue *m_next;
    QList<const ObjectValue *> m_prototypes;
    const Context *m_context;
    Error m_error;
};

class QMLJS_EXPORT QmlEnumValue: public NumberValue
{
public:
    QmlEnumValue(const CppComponentValue *owner, int index);
    ~QmlEnumValue();

    const QmlEnumValue *asQmlEnumValue() const Q_DECL_OVERRIDE;

    QString name() const;
    QStringList keys() const;
    const CppComponentValue *owner() const;

private:
    const CppComponentValue *m_owner;
    int m_enumIndex;
};


// A ObjectValue based on a FakeMetaObject.
// May only have other CppComponentValue as ancestors.
class QMLJS_EXPORT CppComponentValue: public ObjectValue
{
public:
    CppComponentValue(LanguageUtils::FakeMetaObject::ConstPtr metaObject, const QString &className,
                   const QString &moduleName, const LanguageUtils::ComponentVersion &componentVersion,
                   const LanguageUtils::ComponentVersion &importVersion, int metaObjectRevision,
                   ValueOwner *valueOwner, const QString &originId);
    ~CppComponentValue();

    const CppComponentValue *asCppComponentValue() const Q_DECL_OVERRIDE;

    void processMembers(MemberProcessor *processor) const Q_DECL_OVERRIDE;
    const Value *valueForCppName(const QString &typeName) const;

    using ObjectValue::prototype;
    const CppComponentValue *prototype() const;
    QList<const CppComponentValue *> prototypes() const;

    LanguageUtils::FakeMetaObject::ConstPtr metaObject() const;

    QString moduleName() const;
    LanguageUtils::ComponentVersion componentVersion() const;
    LanguageUtils::ComponentVersion importVersion() const;

    QString defaultPropertyName() const;
    QString propertyType(const QString &propertyName) const;
    bool isListProperty(const QString &name) const;
    bool isWritable(const QString &propertyName) const;
    bool isPointer(const QString &propertyName) const;
    bool hasLocalProperty(const QString &typeName) const;
    bool hasProperty(const QString &typeName) const;

    LanguageUtils::FakeMetaEnum getEnum(const QString &typeName, const CppComponentValue **foundInScope = 0) const;
    const QmlEnumValue *getEnumValue(const QString &typeName, const CppComponentValue **foundInScope = 0) const;

    const ObjectValue *signalScope(const QString &signalName) const;
protected:
    bool isDerivedFrom(LanguageUtils::FakeMetaObject::ConstPtr base) const;

private:
    LanguageUtils::FakeMetaObject::ConstPtr m_metaObject;
    const QString m_moduleName;
    // _componentVersion is the version of the export
    // _importVersion is the version it's imported as, used to find correct prototypes
    // needed in cases when B 1.0 has A 1.1 as prototype when imported as 1.1
    const LanguageUtils::ComponentVersion m_componentVersion;
    const LanguageUtils::ComponentVersion m_importVersion;
    mutable QAtomicPointer< QList<const Value *> > m_metaSignatures;
    mutable QAtomicPointer< QHash<QString, const ObjectValue *> > m_signalScopes;
    QHash<QString, const QmlEnumValue * > m_enums;
    int m_metaObjectRevision;
};

class QMLJS_EXPORT FunctionValue: public ObjectValue
{
public:
    FunctionValue(ValueOwner *valueOwner);
    ~FunctionValue();

    virtual const Value *returnValue() const;

    // Access to the names of arguments
    // Named arguments can be optional (usually known for builtins only)
    virtual int namedArgumentCount() const;
    virtual QString argumentName(int index) const;

    // The number of optional named arguments
    // Example: JSON.stringify(value[, replacer[, space]])
    //          has namedArgumentCount = 3
    //          and optionalNamedArgumentCount = 2
    virtual int optionalNamedArgumentCount() const;

    // Whether the function accepts an unlimited number of arguments
    // after the named ones. Defaults to false.
    // Example: Math.max(...)
    virtual bool isVariadic() const;

    virtual const Value *argument(int index) const;

    // Value interface
    const FunctionValue *asFunctionValue() const Q_DECL_OVERRIDE;
    void accept(ValueVisitor *visitor) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT Function: public FunctionValue
{
public:
    Function(ValueOwner *valueOwner);
    ~Function();

    void addArgument(const Value *argument, const QString &name = QString());
    void setReturnValue(const Value *returnValue);
    void setVariadic(bool variadic);
    void setOptionalNamedArgumentCount(int count);

    // FunctionValue interface
    const Value *returnValue() const Q_DECL_OVERRIDE;
    int namedArgumentCount() const Q_DECL_OVERRIDE;
    int optionalNamedArgumentCount() const Q_DECL_OVERRIDE;
    const Value *argument(int index) const Q_DECL_OVERRIDE;
    QString argumentName(int index) const Q_DECL_OVERRIDE;
    bool isVariadic() const Q_DECL_OVERRIDE;
    const Function *asFunction() const Q_DECL_OVERRIDE;

private:
    ValueList m_arguments;
    QStringList m_argumentNames;
    const Value *m_returnValue;
    int m_optionalNamedArgumentCount;
    bool m_isVariadic;
};


////////////////////////////////////////////////////////////////////////////////
// typing environment
////////////////////////////////////////////////////////////////////////////////

class QMLJS_EXPORT CppQmlTypesLoader
{
public:
    typedef QHash<QString, LanguageUtils::FakeMetaObject::ConstPtr> BuiltinObjects;

    /** Loads a set of qmltypes files into the builtin objects list
        and returns errors and warnings
    */
    static BuiltinObjects loadQmlTypes(const QFileInfoList &qmltypesFiles,
                             QStringList *errors, QStringList *warnings);

    static BuiltinObjects defaultQtObjects;
    static BuiltinObjects defaultLibraryObjects;

    // parses the contents of a qmltypes file and fills the newObjects map
    static void parseQmlTypeDescriptions(const QByteArray &contents,
        BuiltinObjects *newObjects,
        QList<ModuleApiInfo> *newModuleApis, QString *errorMessage, QString *warningMessage, const QString &fileName);
};

class QMLJS_EXPORT FakeMetaObjectWithOrigin
{
public:
    LanguageUtils::FakeMetaObject::ConstPtr fakeMetaObject;
    QString originId;
    FakeMetaObjectWithOrigin(LanguageUtils::FakeMetaObject::ConstPtr fakeMetaObject,
                             const QString &originId);
    bool operator ==(const FakeMetaObjectWithOrigin &o) const;
};

QMLJS_EXPORT uint qHash(const FakeMetaObjectWithOrigin &fmoo);

class QMLJS_EXPORT CppQmlTypes
{
public:
    CppQmlTypes(ValueOwner *valueOwner);

    // package name for objects that should be always available
    static const QLatin1String defaultPackage;
    // package name for objects with their raw cpp name
    static const QLatin1String cppPackage;

    template <typename T>
    void load(const QString &originId, const T &fakeMetaObjects, const QString &overridePackage = QString());

    QList<const CppComponentValue *> createObjectsForImport(const QString &package, LanguageUtils::ComponentVersion version);
    bool hasModule(const QString &module) const;

    static QString qualifiedName(const QString &module, const QString &type,
                                 LanguageUtils::ComponentVersion version);
    const CppComponentValue *objectByQualifiedName(const QString &fullyQualifiedName) const;
    const CppComponentValue *objectByQualifiedName(
            const QString &package, const QString &type,
            LanguageUtils::ComponentVersion version) const;
    const CppComponentValue *objectByCppName(const QString &cppName) const;

    void setCppContextProperties(const ObjectValue *contextProperties);
    const ObjectValue *cppContextProperties() const;

private:
    // "Package.CppName ImportVersion" ->  CppComponentValue
    QHash<QString, const CppComponentValue *> m_objectsByQualifiedName;
    QHash<QString, QSet<FakeMetaObjectWithOrigin> > m_fakeMetaObjectsByPackage;
    const ObjectValue *m_cppContextProperties;
    ValueOwner *m_valueOwner;
};

class ConvertToNumber: protected ValueVisitor // ECMAScript ToInt()
{
public:
    ConvertToNumber(ValueOwner *valueOwner);

    const Value *operator()(const Value *value);

protected:
    const Value *switchResult(const Value *value);

    void visit(const NullValue *) Q_DECL_OVERRIDE;
    void visit(const UndefinedValue *) Q_DECL_OVERRIDE;
    void visit(const NumberValue *) Q_DECL_OVERRIDE;
    void visit(const BooleanValue *) Q_DECL_OVERRIDE;
    void visit(const StringValue *) Q_DECL_OVERRIDE;
    void visit(const ObjectValue *) Q_DECL_OVERRIDE;
    void visit(const FunctionValue *) Q_DECL_OVERRIDE;

private:
    ValueOwner *m_valueOwner;
    const Value *m_result;
};

class ConvertToString: protected ValueVisitor // ECMAScript ToString
{
public:
    ConvertToString(ValueOwner *valueOwner);

    const Value *operator()(const Value *value);

protected:
    const Value *switchResult(const Value *value);

    void visit(const NullValue *) Q_DECL_OVERRIDE;
    void visit(const UndefinedValue *) Q_DECL_OVERRIDE;
    void visit(const NumberValue *) Q_DECL_OVERRIDE;
    void visit(const BooleanValue *) Q_DECL_OVERRIDE;
    void visit(const StringValue *) Q_DECL_OVERRIDE;
    void visit(const ObjectValue *) Q_DECL_OVERRIDE;
    void visit(const FunctionValue *) Q_DECL_OVERRIDE;

private:
    ValueOwner *m_valueOwner;
    const Value *m_result;
};

class ConvertToObject: protected ValueVisitor // ECMAScript ToObject
{
public:
    ConvertToObject(ValueOwner *valueOwner);

    const Value *operator()(const Value *value);

protected:
    const Value *switchResult(const Value *value);

    void visit(const NullValue *) Q_DECL_OVERRIDE;
    void visit(const UndefinedValue *) Q_DECL_OVERRIDE;
    void visit(const NumberValue *) Q_DECL_OVERRIDE;
    void visit(const BooleanValue *) Q_DECL_OVERRIDE;
    void visit(const StringValue *) Q_DECL_OVERRIDE;
    void visit(const ObjectValue *) Q_DECL_OVERRIDE;
    void visit(const FunctionValue *) Q_DECL_OVERRIDE;

private:
    ValueOwner *m_valueOwner;
    const Value *m_result;
};

class QMLJS_EXPORT TypeId: protected ValueVisitor
{
    QString _result;

public:
    QString operator()(const Value *value);

protected:
    void visit(const NullValue *) Q_DECL_OVERRIDE;
    void visit(const UndefinedValue *) Q_DECL_OVERRIDE;
    void visit(const NumberValue *) Q_DECL_OVERRIDE;
    void visit(const BooleanValue *) Q_DECL_OVERRIDE;
    void visit(const StringValue *) Q_DECL_OVERRIDE;
    void visit(const ObjectValue *object) Q_DECL_OVERRIDE;
    void visit(const FunctionValue *object) Q_DECL_OVERRIDE;
    void visit(const ColorValue *) Q_DECL_OVERRIDE;
    void visit(const AnchorLineValue *) Q_DECL_OVERRIDE;
};

// internal
class QMLJS_EXPORT QmlPrototypeReference: public Reference
{
public:
    QmlPrototypeReference(AST::UiQualifiedId *qmlTypeName, const Document *doc, ValueOwner *valueOwner);
    ~QmlPrototypeReference();

    const QmlPrototypeReference *asQmlPrototypeReference() const Q_DECL_OVERRIDE;

    AST::UiQualifiedId *qmlTypeName() const;
    const Document *document() const;

private:
    const Value *value(ReferenceContext *referenceContext) const Q_DECL_OVERRIDE;

    AST::UiQualifiedId *m_qmlTypeName;
    const Document *m_doc;
};

class QMLJS_EXPORT ASTVariableReference: public Reference
{
    AST::VariableDeclaration *m_ast;
    const Document *m_doc;

public:
    ASTVariableReference(AST::VariableDeclaration *ast, const Document *doc, ValueOwner *valueOwner);
    ~ASTVariableReference();
    const ASTVariableReference *asAstVariableReference() const Q_DECL_OVERRIDE;
    const AST::VariableDeclaration *ast() const;
private:
    const Value *value(ReferenceContext *referenceContext) const Q_DECL_OVERRIDE;
    bool getSourceLocation(QString *fileName, int *line, int *column) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT ASTFunctionValue: public FunctionValue
{
    AST::FunctionExpression *m_ast;
    const Document *m_doc;
    QList<QString> m_argumentNames;
    bool m_isVariadic;

public:
    ASTFunctionValue(AST::FunctionExpression *ast, const Document *doc, ValueOwner *valueOwner);
    ~ASTFunctionValue();

    AST::FunctionExpression *ast() const;

    int namedArgumentCount() const Q_DECL_OVERRIDE;
    QString argumentName(int index) const Q_DECL_OVERRIDE;
    bool isVariadic() const Q_DECL_OVERRIDE;
    const ASTFunctionValue *asAstFunctionValue() const Q_DECL_OVERRIDE;

    bool getSourceLocation(QString *fileName, int *line, int *column) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT ASTPropertyReference: public Reference
{
    AST::UiPublicMember *m_ast;
    const Document *m_doc;
    QString m_onChangedSlotName;

public:
    ASTPropertyReference(AST::UiPublicMember *ast, const Document *doc, ValueOwner *valueOwner);
    ~ASTPropertyReference();

    const ASTPropertyReference *asAstPropertyReference() const Q_DECL_OVERRIDE;

    AST::UiPublicMember *ast() const { return m_ast; }
    QString onChangedSlotName() const { return m_onChangedSlotName; }

    bool getSourceLocation(QString *fileName, int *line, int *column) const Q_DECL_OVERRIDE;

private:
    const Value *value(ReferenceContext *referenceContext) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT ASTSignal: public FunctionValue
{
    AST::UiPublicMember *m_ast;
    const Document *m_doc;
    QString m_slotName;
    const ObjectValue *m_bodyScope;

public:
    ASTSignal(AST::UiPublicMember *ast, const Document *doc, ValueOwner *valueOwner);
    ~ASTSignal();

    const ASTSignal *asAstSignal() const Q_DECL_OVERRIDE;

    AST::UiPublicMember *ast() const { return m_ast; }
    QString slotName() const { return m_slotName; }
    const ObjectValue *bodyScope() const { return m_bodyScope; }

    // FunctionValue interface
    int namedArgumentCount() const Q_DECL_OVERRIDE;
    const Value *argument(int index) const Q_DECL_OVERRIDE;
    QString argumentName(int index) const Q_DECL_OVERRIDE;

    // Value interface
    bool getSourceLocation(QString *fileName, int *line, int *column) const Q_DECL_OVERRIDE;
};

class QMLJS_EXPORT ASTObjectValue: public ObjectValue
{
    AST::UiQualifiedId *m_typeName;
    AST::UiObjectInitializer *m_initializer;
    const Document *m_doc;
    QList<ASTPropertyReference *> m_properties;
    QList<ASTSignal *> m_signals;
    ASTPropertyReference *m_defaultPropertyRef;

public:
    ASTObjectValue(AST::UiQualifiedId *typeName,
                   AST::UiObjectInitializer *initializer,
                   const Document *doc,
                   ValueOwner *valueOwner);
    ~ASTObjectValue();

    const ASTObjectValue *asAstObjectValue() const Q_DECL_OVERRIDE;

    bool getSourceLocation(QString *fileName, int *line, int *column) const;
    void processMembers(MemberProcessor *processor) const Q_DECL_OVERRIDE;

    QString defaultPropertyName() const;

    AST::UiObjectInitializer *initializer() const;
    AST::UiQualifiedId *typeName() const;
    const Document *document() const;
};

class QMLJS_EXPORT ImportInfo
{
public:
    ImportInfo();

    static ImportInfo moduleImport(QString uri, LanguageUtils::ComponentVersion version,
                                   const QString &as, AST::UiImport *ast = 0);
    static ImportInfo pathImport(const QString &docPath, const QString &path,
                                 LanguageUtils::ComponentVersion version,
                                 const QString &as, AST::UiImport *ast = 0);
    static ImportInfo invalidImport(AST::UiImport *ast = 0);
    static ImportInfo implicitDirectoryImport(const QString &directory);

    bool isValid() const;
    ImportType::Enum type() const;

    // LibraryImport: uri with ',' separator
    // Other: non-absolute path
    QString name() const;

    // LibraryImport: uri with '/' separator
    // Other: absoluteFilePath
    QString path() const;

    // null if the import has no 'as', otherwise the target id
    QString as() const;

    LanguageUtils::ComponentVersion version() const;
    AST::UiImport *ast() const;

private:
    ImportType::Enum m_type;
    LanguageUtils::ComponentVersion m_version;
    QString m_name;
    QString m_path;
    QString m_as;
    AST::UiImport *m_ast;
};

class QMLJS_EXPORT Import {
public:
    Import();
    Import(const Import &other);

    // const!
    ObjectValue *object;
    ImportInfo info;
    DependencyInfo::ConstPtr deps;
    // uri imports: path to library, else empty
    QString libraryPath;
    // whether the import succeeded
    bool valid;
    mutable bool used;
};

class Imports;

class QMLJS_EXPORT TypeScope: public ObjectValue
{
public:
    TypeScope(const Imports *imports, ValueOwner *valueOwner);

    virtual const Value *lookupMember(const QString &name, const Context *context,
                                      const ObjectValue **foundInObject = 0,
                                      bool examinePrototypes = true) const;
    void processMembers(MemberProcessor *processor) const Q_DECL_OVERRIDE;
    const TypeScope *asTypeScope() const Q_DECL_OVERRIDE;
private:
    const Imports *m_imports;
};

class QMLJS_EXPORT JSImportScope: public ObjectValue
{
public:
    JSImportScope(const Imports *imports, ValueOwner *valueOwner);

    virtual const Value *lookupMember(const QString &name, const Context *context,
                                      const ObjectValue **foundInObject = 0,
                                      bool examinePrototypes = true) const;
    void processMembers(MemberProcessor *processor) const Q_DECL_OVERRIDE;
    const JSImportScope *asJSImportScope() const Q_DECL_OVERRIDE;
private:
    const Imports *m_imports;
};

class QMLJS_EXPORT Imports
{
public:
    Imports(ValueOwner *valueOwner);

    void append(const Import &import);
    void setImportFailed();

    ImportInfo info(const QString &name, const Context *context) const;
    QString nameForImportedObject(const ObjectValue *value, const Context *context) const;
    bool importFailed() const;

    const QList<Import> &all() const;

    const TypeScope *typeScope() const;
    const JSImportScope *jsImportScope() const;

#ifdef QT_DEBUG
    void dump() const;
#endif

private:
    // holds imports in the order they appeared,
    // lookup order is back to front
    QList<Import> m_imports;
    TypeScope *m_typeScope;
    JSImportScope *m_jsImportScope;
    bool m_importFailed;
};

} // namespace QmlJS

#endif // QMLJS_INTERPRETER_H