1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLJSAST_P_H
5#define QQMLJSAST_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmljsastvisitor_p.h"
19#include "qqmljsglobal_p.h"
20
21#include <private/qqmljsmemorypool_p.h>
22
23#include <QtCore/qtaggedpointer.h>
24#include <QtCore/qversionnumber.h>
25
26#include <type_traits>
27
28QT_BEGIN_NAMESPACE
29
30class QString;
31
32namespace QQmlJS {
33 class Parser;
34}
35
36#define QQMLJS_DECLARE_AST_NODE(name) \
37 enum { K = Kind_##name };
38
39namespace QSOperator // ### rename
40{
41
42enum Op {
43 Add,
44 And,
45 InplaceAnd,
46 Assign,
47 BitAnd,
48 BitOr,
49 BitXor,
50 InplaceSub,
51 Div,
52 InplaceDiv,
53 Equal,
54 Exp,
55 InplaceExp,
56 Ge,
57 Gt,
58 In,
59 InplaceAdd,
60 InstanceOf,
61 Le,
62 LShift,
63 InplaceLeftShift,
64 Lt,
65 Mod,
66 InplaceMod,
67 Mul,
68 InplaceMul,
69 NotEqual,
70 Or,
71 InplaceOr,
72 RShift,
73 InplaceRightShift,
74 StrictEqual,
75 StrictNotEqual,
76 Sub,
77 URShift,
78 InplaceURightShift,
79 InplaceXor,
80 As,
81 Coalesce,
82 Invalid
83};
84
85} // namespace QSOperator
86
87namespace QQmlJS {
88
89namespace AST {
90
91enum class VariableScope {
92 NoScope,
93 Var,
94 Let,
95 Const
96};
97
98template <typename T1, typename T2>
99T1 cast(T2 *ast)
100{
101 if (ast && ast->kind == std::remove_pointer_t<T1>::K)
102 return static_cast<T1>(ast);
103
104 return nullptr;
105}
106
107FunctionExpression *asAnonymousFunctionDefinition(AST::Node *n);
108ClassExpression *asAnonymousClassDefinition(AST::Node *n);
109
110class QML_PARSER_EXPORT Node: public Managed
111{
112public:
113 enum Kind {
114 Kind_Undefined,
115
116 Kind_ArgumentList,
117 Kind_ArrayPattern,
118 Kind_ArrayMemberExpression,
119 Kind_BinaryExpression,
120 Kind_Block,
121 Kind_BreakStatement,
122 Kind_CallExpression,
123 Kind_CaseBlock,
124 Kind_CaseClause,
125 Kind_CaseClauses,
126 Kind_Catch,
127 Kind_ConditionalExpression,
128 Kind_ContinueStatement,
129 Kind_DebuggerStatement,
130 Kind_DefaultClause,
131 Kind_DeleteExpression,
132 Kind_DoWhileStatement,
133 Kind_ElementList,
134 Kind_Elision,
135 Kind_EmptyStatement,
136 Kind_Expression,
137 Kind_ExpressionStatement,
138 Kind_FalseLiteral,
139 Kind_SuperLiteral,
140 Kind_FieldMemberExpression,
141 Kind_Finally,
142 Kind_ForEachStatement,
143 Kind_ForStatement,
144 Kind_FormalParameterList,
145 Kind_FunctionBody,
146 Kind_FunctionDeclaration,
147 Kind_FunctionExpression,
148 Kind_ClassExpression,
149 Kind_ClassDeclaration,
150 Kind_IdentifierExpression,
151 Kind_IdentifierPropertyName,
152 Kind_ComputedPropertyName,
153 Kind_IfStatement,
154 Kind_LabelledStatement,
155 Kind_NameSpaceImport,
156 Kind_ImportSpecifier,
157 Kind_ImportsList,
158 Kind_NamedImports,
159 Kind_ImportClause,
160 Kind_FromClause,
161 Kind_ImportDeclaration,
162 Kind_Module,
163 Kind_ExportSpecifier,
164 Kind_ExportsList,
165 Kind_ExportClause,
166 Kind_ExportDeclaration,
167 Kind_NewExpression,
168 Kind_NewMemberExpression,
169 Kind_NotExpression,
170 Kind_NullExpression,
171 Kind_YieldExpression,
172 Kind_NumericLiteral,
173 Kind_NumericLiteralPropertyName,
174 Kind_ObjectPattern,
175 Kind_PostDecrementExpression,
176 Kind_PostIncrementExpression,
177 Kind_PreDecrementExpression,
178 Kind_PreIncrementExpression,
179 Kind_Program,
180 Kind_PropertyDefinitionList,
181 Kind_PropertyGetterSetter,
182 Kind_PropertyName,
183 Kind_PropertyNameAndValue,
184 Kind_RegExpLiteral,
185 Kind_ReturnStatement,
186 Kind_StatementList,
187 Kind_StringLiteral,
188 Kind_StringLiteralPropertyName,
189 Kind_SwitchStatement,
190 Kind_TemplateLiteral,
191 Kind_TaggedTemplate,
192 Kind_TypeExpression,
193 Kind_ThisExpression,
194 Kind_ThrowStatement,
195 Kind_TildeExpression,
196 Kind_TrueLiteral,
197 Kind_TryStatement,
198 Kind_TypeOfExpression,
199 Kind_UnaryMinusExpression,
200 Kind_UnaryPlusExpression,
201 Kind_VariableDeclaration,
202 Kind_VariableDeclarationList,
203 Kind_VariableStatement,
204 Kind_VoidExpression,
205 Kind_WhileStatement,
206 Kind_WithStatement,
207 Kind_NestedExpression,
208 Kind_ClassElementList,
209 Kind_PatternElement,
210 Kind_PatternElementList,
211 Kind_PatternProperty,
212 Kind_PatternPropertyList,
213 Kind_Type,
214 Kind_TypeArgument,
215 Kind_TypeAnnotation,
216
217 Kind_UiArrayBinding,
218 Kind_UiImport,
219 Kind_UiObjectBinding,
220 Kind_UiObjectDefinition,
221 Kind_UiInlineComponent,
222 Kind_UiObjectInitializer,
223 Kind_UiObjectMemberList,
224 Kind_UiArrayMemberList,
225 Kind_UiPragmaValueList,
226 Kind_UiPragma,
227 Kind_UiProgram,
228 Kind_UiParameterList,
229 Kind_UiPropertyAttributes,
230 Kind_UiPublicMember,
231 Kind_UiQualifiedId,
232 Kind_UiScriptBinding,
233 Kind_UiSourceElement,
234 Kind_UiHeaderItemList,
235 Kind_UiEnumDeclaration,
236 Kind_UiEnumMemberList,
237 Kind_UiVersionSpecifier,
238 Kind_UiRequired,
239 Kind_UiAnnotation,
240 Kind_UiAnnotationList
241 };
242
243 inline Node() {}
244
245 // NOTE: node destructors are never called,
246 // instead we block free the memory
247 // (see the NodePool class)
248 virtual ~Node() {}
249
250 virtual ExpressionNode *expressionCast();
251 virtual BinaryExpression *binaryExpressionCast();
252 virtual Statement *statementCast();
253 virtual UiObjectMember *uiObjectMemberCast();
254 virtual LeftHandSideExpression *leftHandSideExpressionCast();
255 virtual Pattern *patternCast();
256 // implements the IsFunctionDefinition rules in the spec
257 virtual FunctionExpression *asFunctionDefinition();
258 virtual ClassExpression *asClassDefinition();
259
260 bool ignoreRecursionDepth() const;
261
262 inline void accept(BaseVisitor *visitor)
263 {
264 BaseVisitor::RecursionDepthCheck recursionCheck(visitor);
265
266 // Stack overflow is uncommon, ignoreRecursionDepth() only returns true if
267 // QV4_CRASH_ON_STACKOVERFLOW is set, and ignoreRecursionDepth() needs to be out of line.
268 // Therefore, check for ignoreRecursionDepth() _after_ calling the inline recursionCheck().
269 if (recursionCheck() || ignoreRecursionDepth()) {
270 if (visitor->preVisit(this))
271 accept0(visitor);
272 visitor->postVisit(this);
273 } else {
274 visitor->throwRecursionDepthError();
275 }
276 }
277
278 inline static void accept(Node *node, BaseVisitor *visitor)
279 {
280 if (node)
281 node->accept(visitor);
282 }
283
284 virtual void accept0(BaseVisitor *visitor) = 0;
285 virtual SourceLocation firstSourceLocation() const = 0;
286 virtual SourceLocation lastSourceLocation() const = 0;
287
288// attributes
289 int kind = Kind_Undefined;
290};
291
292template<typename T>
293T lastListElement(T head)
294{
295 auto current = head;
296 while (current->next)
297 current = current->next;
298 return current;
299}
300
301class QML_PARSER_EXPORT UiQualifiedId: public Node
302{
303public:
304 QQMLJS_DECLARE_AST_NODE(UiQualifiedId)
305
306 UiQualifiedId(QStringView name)
307 : next(this), name(name)
308 { kind = K; }
309
310 UiQualifiedId(UiQualifiedId *previous, QStringView name)
311 : name(name)
312 {
313 kind = K;
314 next = previous->next;
315 previous->next = this;
316 }
317
318 UiQualifiedId *finish()
319 {
320 UiQualifiedId *head = next;
321 next = nullptr;
322 return head;
323 }
324
325 void accept0(BaseVisitor *visitor) override;
326
327 SourceLocation firstSourceLocation() const override
328 { return identifierToken; }
329
330 SourceLocation lastSourceLocation() const override
331 {
332 return lastListElement(head: this)->lastOwnSourceLocation();
333 }
334
335 SourceLocation lastOwnSourceLocation() const { return identifierToken; }
336
337 QString toString() const
338 {
339 QString result;
340 toString(out: &result);
341 return result;
342 }
343
344 void toString(QString *out) const
345 {
346 for (const UiQualifiedId *it = this; it; it = it->next) {
347 out->append(v: it->name);
348 if (it->next)
349 out->append(c: QLatin1Char('.'));
350 }
351 }
352
353// attributes
354 UiQualifiedId *next;
355 QStringView name;
356 SourceLocation identifierToken;
357};
358
359class QML_PARSER_EXPORT Type: public Node
360{
361public:
362 QQMLJS_DECLARE_AST_NODE(Type)
363
364 Type(UiQualifiedId *typeId, Type *typeArgument = nullptr)
365 : typeId(typeId)
366 , typeArgument(typeArgument ? typeArgument->typeId : nullptr)
367 { kind = K; }
368
369 void accept0(BaseVisitor *visitor) override;
370
371 SourceLocation firstSourceLocation() const override
372 { return typeId->firstSourceLocation(); }
373
374 SourceLocation lastSourceLocation() const override
375 { return typeArgument ? typeArgument->lastSourceLocation() : typeId->lastSourceLocation(); }
376
377 QString toString() const;
378 void toString(QString *out) const;
379
380// attributes
381 UiQualifiedId *typeId;
382 UiQualifiedId *typeArgument;
383};
384
385class QML_PARSER_EXPORT TypeAnnotation: public Node
386{
387public:
388 QQMLJS_DECLARE_AST_NODE(TypeAnnotation)
389
390 TypeAnnotation(Type *type)
391 : type(type)
392 { kind = K; }
393
394 void accept0(BaseVisitor *visitor) override;
395
396 SourceLocation firstSourceLocation() const override
397 { return colonToken; }
398
399 SourceLocation lastSourceLocation() const override
400 { return type->lastSourceLocation(); }
401
402// attributes
403 Type *type;
404 SourceLocation colonToken;
405};
406class QML_PARSER_EXPORT ExpressionNode: public Node
407{
408public:
409 ExpressionNode() {}
410
411 ExpressionNode *expressionCast() override;
412 bool containsOptionalChain() const;
413
414 AST::FormalParameterList *reparseAsFormalParameterList(MemoryPool *pool);
415
416};
417
418class QML_PARSER_EXPORT LeftHandSideExpression : public ExpressionNode
419{
420 LeftHandSideExpression *leftHandSideExpressionCast() override;
421};
422
423class QML_PARSER_EXPORT Statement: public Node
424{
425public:
426 Statement() {}
427
428 Statement *statementCast() override;
429};
430
431class QML_PARSER_EXPORT NestedExpression: public LeftHandSideExpression
432{
433public:
434 QQMLJS_DECLARE_AST_NODE(NestedExpression)
435
436 NestedExpression(ExpressionNode *expression)
437 : expression(expression)
438 { kind = K; }
439
440 void accept0(BaseVisitor *visitor) override;
441
442 SourceLocation firstSourceLocation() const override
443 { return lparenToken; }
444
445 SourceLocation lastSourceLocation() const override
446 { return rparenToken; }
447
448 FunctionExpression *asFunctionDefinition() override;
449 ClassExpression *asClassDefinition() override;
450
451
452// attributes
453 ExpressionNode *expression;
454 SourceLocation lparenToken;
455 SourceLocation rparenToken;
456};
457
458
459class QML_PARSER_EXPORT TypeExpression : public ExpressionNode
460{
461public:
462 QQMLJS_DECLARE_AST_NODE(TypeExpression)
463 TypeExpression(Type *t) : m_type(t) { kind = K; }
464
465 void accept0(BaseVisitor *visitor) override;
466
467 SourceLocation firstSourceLocation() const override {
468 return m_type->firstSourceLocation();
469 }
470
471 SourceLocation lastSourceLocation() const override {
472 return m_type->lastSourceLocation();
473 }
474
475 Type *m_type;
476};
477
478class QML_PARSER_EXPORT ThisExpression: public LeftHandSideExpression
479{
480public:
481 QQMLJS_DECLARE_AST_NODE(ThisExpression)
482
483 ThisExpression() { kind = K; }
484
485 void accept0(BaseVisitor *visitor) override;
486
487 SourceLocation firstSourceLocation() const override
488 { return thisToken; }
489
490 SourceLocation lastSourceLocation() const override
491 { return thisToken; }
492
493// attributes
494 SourceLocation thisToken;
495};
496
497class QML_PARSER_EXPORT IdentifierExpression: public LeftHandSideExpression
498{
499public:
500 QQMLJS_DECLARE_AST_NODE(IdentifierExpression)
501
502 IdentifierExpression(QStringView n):
503 name (n) { kind = K; }
504
505 void accept0(BaseVisitor *visitor) override;
506
507 SourceLocation firstSourceLocation() const override
508 { return identifierToken; }
509
510 SourceLocation lastSourceLocation() const override
511 { return identifierToken; }
512
513// attributes
514 QStringView name;
515 SourceLocation identifierToken;
516};
517
518class QML_PARSER_EXPORT NullExpression: public LeftHandSideExpression
519{
520public:
521 QQMLJS_DECLARE_AST_NODE(NullExpression)
522
523 NullExpression() { kind = K; }
524
525 void accept0(BaseVisitor *visitor) override;
526
527 SourceLocation firstSourceLocation() const override
528 { return nullToken; }
529
530 SourceLocation lastSourceLocation() const override
531 { return nullToken; }
532
533// attributes
534 SourceLocation nullToken;
535};
536
537class QML_PARSER_EXPORT TrueLiteral: public LeftHandSideExpression
538{
539public:
540 QQMLJS_DECLARE_AST_NODE(TrueLiteral)
541
542 TrueLiteral() { kind = K; }
543
544 void accept0(BaseVisitor *visitor) override;
545
546 SourceLocation firstSourceLocation() const override
547 { return trueToken; }
548
549 SourceLocation lastSourceLocation() const override
550 { return trueToken; }
551
552// attributes
553 SourceLocation trueToken;
554};
555
556class QML_PARSER_EXPORT FalseLiteral: public LeftHandSideExpression
557{
558public:
559 QQMLJS_DECLARE_AST_NODE(FalseLiteral)
560
561 FalseLiteral() { kind = K; }
562
563 void accept0(BaseVisitor *visitor) override;
564
565 SourceLocation firstSourceLocation() const override
566 { return falseToken; }
567
568 SourceLocation lastSourceLocation() const override
569 { return falseToken; }
570
571// attributes
572 SourceLocation falseToken;
573};
574
575class QML_PARSER_EXPORT SuperLiteral : public LeftHandSideExpression
576{
577public:
578 QQMLJS_DECLARE_AST_NODE(SuperLiteral)
579
580 SuperLiteral() { kind = K; }
581
582 void accept0(BaseVisitor *visitor) override;
583
584 SourceLocation firstSourceLocation() const override
585 { return superToken; }
586
587 SourceLocation lastSourceLocation() const override
588 { return superToken; }
589
590// attributes
591 SourceLocation superToken;
592};
593
594
595class QML_PARSER_EXPORT NumericLiteral: public LeftHandSideExpression
596{
597public:
598 QQMLJS_DECLARE_AST_NODE(NumericLiteral)
599
600 NumericLiteral(double v):
601 value(v) { kind = K; }
602
603 void accept0(BaseVisitor *visitor) override;
604
605 SourceLocation firstSourceLocation() const override
606 { return literalToken; }
607
608 SourceLocation lastSourceLocation() const override
609 { return literalToken; }
610
611// attributes:
612 double value;
613 SourceLocation literalToken;
614};
615
616class QML_PARSER_EXPORT UiVersionSpecifier : public Node
617{
618public:
619 QQMLJS_DECLARE_AST_NODE(UiVersionSpecifier)
620
621 UiVersionSpecifier(int majorum) : version(QTypeRevision::fromMajorVersion(majorVersion: majorum))
622 {
623 kind = K;
624 }
625
626 UiVersionSpecifier(int majorum, int minorum) : version(QTypeRevision::fromVersion(majorVersion: majorum, minorVersion: minorum))
627 {
628 kind = K;
629 }
630
631 void accept0(BaseVisitor *visitor) override;
632
633 SourceLocation firstSourceLocation() const override { return majorToken; }
634
635 SourceLocation lastSourceLocation() const override
636 {
637 return minorToken.isValid() ? minorToken : majorToken;
638 }
639
640 // attributes:
641 QTypeRevision version;
642 SourceLocation majorToken;
643 SourceLocation minorToken;
644};
645
646class QML_PARSER_EXPORT StringLiteral : public LeftHandSideExpression
647{
648public:
649 QQMLJS_DECLARE_AST_NODE(StringLiteral)
650
651 StringLiteral(QStringView v):
652 value (v) { kind = K; }
653
654 void accept0(BaseVisitor *visitor) override;
655
656 SourceLocation firstSourceLocation() const override
657 { return literalToken; }
658
659 SourceLocation lastSourceLocation() const override
660 { return literalToken; }
661
662// attributes:
663 QStringView value;
664 SourceLocation literalToken;
665};
666
667class QML_PARSER_EXPORT TemplateLiteral : public LeftHandSideExpression
668{
669public:
670 QQMLJS_DECLARE_AST_NODE(TemplateLiteral)
671
672 TemplateLiteral(QStringView str, QStringView raw, ExpressionNode *e)
673 : value(str), rawValue(raw), expression(e), next(nullptr)
674 { kind = K; }
675
676 SourceLocation firstSourceLocation() const override
677 { return literalToken; }
678
679 SourceLocation lastSourceLocation() const override
680 {
681 auto last = lastListElement(head: this);
682 return (last->expression ? last->expression->lastSourceLocation() : last->literalToken);
683 }
684
685 void accept0(BaseVisitor *visitor) override;
686
687 bool hasNoSubstitution = false;
688 QStringView value;
689 QStringView rawValue;
690 ExpressionNode *expression;
691 TemplateLiteral *next;
692 SourceLocation literalToken;
693};
694
695class QML_PARSER_EXPORT RegExpLiteral: public LeftHandSideExpression
696{
697public:
698 QQMLJS_DECLARE_AST_NODE(RegExpLiteral)
699
700 RegExpLiteral(QStringView p, int f):
701 pattern (p), flags (f) { kind = K; }
702
703 void accept0(BaseVisitor *visitor) override;
704
705 SourceLocation firstSourceLocation() const override
706 { return literalToken; }
707
708 SourceLocation lastSourceLocation() const override
709 { return literalToken; }
710
711// attributes:
712 QStringView pattern;
713 int flags;
714 SourceLocation literalToken;
715};
716
717class QML_PARSER_EXPORT Pattern : public LeftHandSideExpression
718{
719public:
720 enum ParseMode {
721 Literal,
722 Binding
723 };
724 Pattern *patternCast() override;
725 virtual bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage) = 0;
726 ParseMode parseMode = Literal;
727};
728
729class QML_PARSER_EXPORT ArrayPattern : public Pattern
730{
731public:
732 QQMLJS_DECLARE_AST_NODE(ArrayPattern)
733
734 ArrayPattern(PatternElementList *elts)
735 : elements(elts)
736 { kind = K; }
737
738 void accept0(BaseVisitor *visitor) override;
739
740 SourceLocation firstSourceLocation() const override
741 { return lbracketToken; }
742
743 SourceLocation lastSourceLocation() const override
744 { return rbracketToken; }
745
746 bool isValidArrayLiteral(SourceLocation *errorLocation = nullptr) const;
747
748 bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage) override;
749
750// attributes
751 PatternElementList *elements = nullptr;
752 SourceLocation lbracketToken;
753 SourceLocation commaToken;
754 SourceLocation rbracketToken;
755};
756
757class QML_PARSER_EXPORT ObjectPattern : public Pattern
758{
759public:
760 QQMLJS_DECLARE_AST_NODE(ObjectPattern)
761
762 ObjectPattern()
763 { kind = K; }
764
765 ObjectPattern(PatternPropertyList *plist)
766 : properties(plist)
767 { kind = K; }
768
769 void accept0(BaseVisitor *visitor) override;
770
771 SourceLocation firstSourceLocation() const override
772 { return lbraceToken; }
773
774 SourceLocation lastSourceLocation() const override
775 { return rbraceToken; }
776
777 bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage) override;
778
779// attributes
780 PatternPropertyList *properties = nullptr;
781 SourceLocation lbraceToken;
782 SourceLocation rbraceToken;
783};
784
785class QML_PARSER_EXPORT Elision: public Node
786{
787public:
788 QQMLJS_DECLARE_AST_NODE(Elision)
789
790 Elision():
791 next (this) { kind = K; }
792
793 Elision(Elision *previous)
794 {
795 kind = K;
796 next = previous->next;
797 previous->next = this;
798 }
799
800 void accept0(BaseVisitor *visitor) override;
801
802 SourceLocation firstSourceLocation() const override
803 { return commaToken; }
804
805 SourceLocation lastSourceLocation() const override
806 { return lastListElement(head: this)->commaToken; }
807
808 inline Elision *finish ()
809 {
810 Elision *front = next;
811 next = nullptr;
812 return front;
813 }
814
815// attributes
816 Elision *next;
817 SourceLocation commaToken;
818};
819
820class QML_PARSER_EXPORT PropertyName: public Node
821{
822public:
823 QQMLJS_DECLARE_AST_NODE(PropertyName)
824
825 PropertyName() { kind = K; }
826
827 SourceLocation firstSourceLocation() const override
828 { return propertyNameToken; }
829
830 SourceLocation lastSourceLocation() const override
831 { return propertyNameToken; }
832
833 virtual QString asString() const = 0;
834
835// attributes
836 SourceLocation propertyNameToken;
837};
838
839struct QML_PARSER_EXPORT BoundName
840{
841 enum Type {
842 Declared,
843 Injected,
844 };
845
846 QString id;
847 QTaggedPointer<TypeAnnotation, Type> typeAnnotation;
848 BoundName(const QString &id, TypeAnnotation *typeAnnotation, Type type = Declared)
849 : id(id), typeAnnotation(typeAnnotation, type)
850 {}
851 BoundName() = default;
852
853 bool isInjected() const { return typeAnnotation.tag() == Injected; }
854};
855
856struct BoundNames : public QVector<BoundName>
857{
858 int indexOf(const QString &name, int from = 0) const
859 {
860 auto found = std::find_if(first: constBegin() + from, last: constEnd(),
861 pred: [name](const BoundName &it) { return it.id == name; });
862 if (found == constEnd())
863 return -1;
864 return found - constBegin();
865 }
866
867 bool contains(const QString &name) const
868 {
869 return indexOf(name) != -1;
870 }
871};
872
873class QML_PARSER_EXPORT PatternElement : public Node
874{
875public:
876 QQMLJS_DECLARE_AST_NODE(PatternElement)
877
878 enum Type {
879 // object literal types
880 Literal,
881 Method,
882 Getter,
883 Setter,
884
885 // used by both bindings and literals
886 SpreadElement,
887 RestElement = SpreadElement,
888
889 // binding types
890 Binding,
891 };
892
893 PatternElement(ExpressionNode *i = nullptr, Type t = Literal)
894 : initializer(i), type(t)
895 { kind = K; }
896
897 PatternElement(QStringView n, TypeAnnotation *typeAnnotation = nullptr, ExpressionNode *i = nullptr, Type t = Binding)
898 : bindingIdentifier(n), initializer(i), type(t)
899 , typeAnnotation(typeAnnotation)
900 {
901 Q_ASSERT(t >= RestElement);
902 kind = K;
903 }
904
905 PatternElement(Pattern *pattern, ExpressionNode *i = nullptr, Type t = Binding)
906 : bindingTarget(pattern), initializer(i), type(t)
907 {
908 Q_ASSERT(t >= RestElement);
909 kind = K;
910 }
911
912 void accept0(BaseVisitor *visitor) override;
913 virtual bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage);
914
915 SourceLocation firstSourceLocation() const override
916 { return identifierToken.isValid() ? identifierToken : (bindingTarget ? bindingTarget->firstSourceLocation() : initializer->firstSourceLocation()); }
917
918 SourceLocation lastSourceLocation() const override
919 { return initializer ? initializer->lastSourceLocation() : (bindingTarget ? bindingTarget->lastSourceLocation() : (typeAnnotation ? typeAnnotation->lastSourceLocation() : identifierToken)); }
920
921 ExpressionNode *destructuringTarget() const { return bindingTarget; }
922 Pattern *destructuringPattern() const { return bindingTarget ? bindingTarget->patternCast() : nullptr; }
923 PatternElementList *elementList() const { ArrayPattern *a = cast<ArrayPattern *>(ast: bindingTarget); return a ? a->elements : nullptr; }
924 PatternPropertyList *propertyList() const { ObjectPattern *o = cast<ObjectPattern *>(ast: bindingTarget); return o ? o->properties : nullptr; }
925
926 bool isVariableDeclaration() const { return scope != VariableScope::NoScope; }
927 bool isLexicallyScoped() const { return scope == VariableScope::Let || scope == VariableScope::Const; }
928
929 virtual void boundNames(BoundNames *names);
930
931// attributes
932 SourceLocation identifierToken;
933 QStringView bindingIdentifier;
934 ExpressionNode *bindingTarget = nullptr;
935 ExpressionNode *initializer = nullptr;
936 Type type = Literal;
937 TypeAnnotation *typeAnnotation = nullptr;
938 // when used in a VariableDeclarationList
939 VariableScope scope = VariableScope::NoScope;
940 bool isForDeclaration = false;
941 bool isInjectedSignalParameter = false;
942};
943
944class QML_PARSER_EXPORT PatternElementList : public Node
945{
946public:
947 QQMLJS_DECLARE_AST_NODE(PatternElementList)
948
949 PatternElementList(Elision *elision, PatternElement *element)
950 : elision(elision), element(element), next(this)
951 { kind = K; }
952
953 PatternElementList *append(PatternElementList *n) {
954 n->next = next;
955 next = n;
956 return n;
957 }
958
959 inline PatternElementList *finish ()
960 {
961 PatternElementList *front = next;
962 next = 0;
963 return front;
964 }
965
966 void accept0(BaseVisitor *visitor) override;
967
968 void boundNames(BoundNames *names);
969
970 SourceLocation firstSourceLocation() const override
971 { return elision ? elision->firstSourceLocation() : element->firstSourceLocation(); }
972
973 SourceLocation lastSourceLocation() const override
974 {
975 auto last = lastListElement(head: this);
976 return last->element ? last->element->lastSourceLocation() : last->elision->lastSourceLocation();
977 }
978
979 Elision *elision = nullptr;
980 PatternElement *element = nullptr;
981 PatternElementList *next;
982};
983
984class QML_PARSER_EXPORT PatternProperty : public PatternElement
985{
986public:
987 QQMLJS_DECLARE_AST_NODE(PatternProperty)
988
989 PatternProperty(PropertyName *name, ExpressionNode *i = nullptr, Type t = Literal)
990 : PatternElement(i, t), name(name)
991 { kind = K; }
992
993 PatternProperty(PropertyName *name, QStringView n, ExpressionNode *i = nullptr)
994 : PatternElement(n, /*type annotation*/nullptr, i), name(name)
995 { kind = K; }
996
997 PatternProperty(PropertyName *name, Pattern *pattern, ExpressionNode *i = nullptr)
998 : PatternElement(pattern, i), name(name)
999 { kind = K; }
1000
1001 void accept0(BaseVisitor *visitor) override;
1002
1003 SourceLocation firstSourceLocation() const override
1004 { return name->firstSourceLocation(); }
1005 SourceLocation lastSourceLocation() const override
1006 {
1007 SourceLocation loc = PatternElement::lastSourceLocation();
1008 return loc.isValid() ? loc : name->lastSourceLocation();
1009 }
1010
1011 void boundNames(BoundNames *names) override;
1012 bool convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage) override;
1013
1014// attributes
1015 PropertyName *name;
1016 SourceLocation colonToken;
1017};
1018
1019
1020class QML_PARSER_EXPORT PatternPropertyList : public Node
1021{
1022public:
1023 QQMLJS_DECLARE_AST_NODE(PatternPropertyList)
1024
1025 PatternPropertyList(PatternProperty *property)
1026 : property(property), next(this)
1027 { kind = K; }
1028
1029 PatternPropertyList(PatternPropertyList *previous, PatternProperty *property)
1030 : property(property), next(this)
1031 {
1032 kind = K;
1033 next = previous->next;
1034 previous->next = this;
1035 }
1036
1037 void accept0(BaseVisitor *visitor) override;
1038
1039 void boundNames(BoundNames *names);
1040
1041 inline PatternPropertyList *finish ()
1042 {
1043 PatternPropertyList *front = next;
1044 next = 0;
1045 return front;
1046 }
1047
1048 SourceLocation firstSourceLocation() const override
1049 { return property->firstSourceLocation(); }
1050
1051 SourceLocation lastSourceLocation() const override
1052 { return lastListElement(head: this)->property->lastSourceLocation(); }
1053
1054 PatternProperty *property;
1055 PatternPropertyList *next;
1056};
1057
1058class QML_PARSER_EXPORT IdentifierPropertyName: public PropertyName
1059{
1060public:
1061 QQMLJS_DECLARE_AST_NODE(IdentifierPropertyName)
1062
1063 IdentifierPropertyName(QStringView n):
1064 id (n) { kind = K; }
1065
1066 void accept0(BaseVisitor *visitor) override;
1067
1068 QString asString() const override { return id.toString(); }
1069
1070// attributes
1071 QStringView id;
1072};
1073
1074class QML_PARSER_EXPORT StringLiteralPropertyName: public PropertyName
1075{
1076public:
1077 QQMLJS_DECLARE_AST_NODE(StringLiteralPropertyName)
1078
1079 StringLiteralPropertyName(QStringView n):
1080 id (n) { kind = K; }
1081
1082 void accept0(BaseVisitor *visitor) override;
1083
1084 QString asString() const override { return id.toString(); }
1085
1086// attributes
1087 QStringView id;
1088};
1089
1090class QML_PARSER_EXPORT NumericLiteralPropertyName: public PropertyName
1091{
1092public:
1093 QQMLJS_DECLARE_AST_NODE(NumericLiteralPropertyName)
1094
1095 NumericLiteralPropertyName(double n):
1096 id (n) { kind = K; }
1097
1098 void accept0(BaseVisitor *visitor) override;
1099
1100 QString asString() const override;
1101
1102// attributes
1103 double id;
1104};
1105
1106class QML_PARSER_EXPORT ComputedPropertyName : public PropertyName
1107{
1108public:
1109 QQMLJS_DECLARE_AST_NODE(ComputedPropertyName)
1110
1111 ComputedPropertyName(ExpressionNode *expression)
1112 : expression(expression)
1113 { kind = K; }
1114
1115 void accept0(BaseVisitor *visitor) override;
1116
1117 QString asString() const override { return QString(); }
1118
1119 SourceLocation firstSourceLocation() const override
1120 { return expression->firstSourceLocation(); }
1121
1122 SourceLocation lastSourceLocation() const override
1123 { return expression->lastSourceLocation(); }
1124
1125// attributes
1126 ExpressionNode *expression;
1127};
1128
1129
1130class QML_PARSER_EXPORT ArrayMemberExpression: public LeftHandSideExpression
1131{
1132public:
1133 QQMLJS_DECLARE_AST_NODE(ArrayMemberExpression)
1134
1135 ArrayMemberExpression(ExpressionNode *b, ExpressionNode *e):
1136 base (b), expression (e)
1137 { kind = K; }
1138
1139 void accept0(BaseVisitor *visitor) override;
1140
1141 SourceLocation firstSourceLocation() const override
1142 { return base->firstSourceLocation(); }
1143
1144 SourceLocation lastSourceLocation() const override
1145 { return rbracketToken; }
1146
1147// attributes
1148 ExpressionNode *base;
1149 ExpressionNode *expression;
1150 SourceLocation lbracketToken;
1151 SourceLocation rbracketToken;
1152 bool isOptional = false;
1153};
1154
1155class QML_PARSER_EXPORT FieldMemberExpression: public LeftHandSideExpression
1156{
1157public:
1158 QQMLJS_DECLARE_AST_NODE(FieldMemberExpression)
1159
1160 FieldMemberExpression(ExpressionNode *b, QStringView n):
1161 base (b), name (n)
1162 { kind = K; }
1163
1164 void accept0(BaseVisitor *visitor) override;
1165
1166 SourceLocation firstSourceLocation() const override
1167 { return base->firstSourceLocation(); }
1168
1169 SourceLocation lastSourceLocation() const override
1170 { return identifierToken; }
1171
1172 // attributes
1173 ExpressionNode *base;
1174 QStringView name;
1175 SourceLocation dotToken;
1176 SourceLocation identifierToken;
1177 bool isOptional = false;
1178};
1179
1180class QML_PARSER_EXPORT TaggedTemplate : public LeftHandSideExpression
1181{
1182public:
1183 QQMLJS_DECLARE_AST_NODE(TaggedTemplate)
1184
1185 TaggedTemplate(ExpressionNode *b, TemplateLiteral *t)
1186 : base (b), templateLiteral(t)
1187 { kind = K; }
1188
1189 void accept0(BaseVisitor *visitor) override;
1190
1191 SourceLocation firstSourceLocation() const override
1192 { return base->firstSourceLocation(); }
1193
1194 SourceLocation lastSourceLocation() const override
1195 { return templateLiteral->lastSourceLocation(); }
1196
1197 // attributes
1198 ExpressionNode *base;
1199 TemplateLiteral *templateLiteral;
1200};
1201
1202class QML_PARSER_EXPORT NewMemberExpression: public LeftHandSideExpression
1203{
1204public:
1205 QQMLJS_DECLARE_AST_NODE(NewMemberExpression)
1206
1207 NewMemberExpression(ExpressionNode *b, ArgumentList *a):
1208 base (b), arguments (a)
1209 { kind = K; }
1210
1211 void accept0(BaseVisitor *visitor) override;
1212
1213 SourceLocation firstSourceLocation() const override
1214 { return newToken; }
1215
1216 SourceLocation lastSourceLocation() const override
1217 { return rparenToken; }
1218
1219 // attributes
1220 ExpressionNode *base;
1221 ArgumentList *arguments;
1222 SourceLocation newToken;
1223 SourceLocation lparenToken;
1224 SourceLocation rparenToken;
1225};
1226
1227class QML_PARSER_EXPORT NewExpression: public LeftHandSideExpression
1228{
1229public:
1230 QQMLJS_DECLARE_AST_NODE(NewExpression)
1231
1232 NewExpression(ExpressionNode *e):
1233 expression (e) { kind = K; }
1234
1235 void accept0(BaseVisitor *visitor) override;
1236
1237 SourceLocation firstSourceLocation() const override
1238 { return newToken; }
1239
1240 SourceLocation lastSourceLocation() const override
1241 { return expression->lastSourceLocation(); }
1242
1243// attributes
1244 ExpressionNode *expression;
1245 SourceLocation newToken;
1246};
1247
1248class QML_PARSER_EXPORT CallExpression: public LeftHandSideExpression
1249{
1250public:
1251 QQMLJS_DECLARE_AST_NODE(CallExpression)
1252
1253 CallExpression(ExpressionNode *b, ArgumentList *a):
1254 base (b), arguments (a)
1255 { kind = K; }
1256
1257 void accept0(BaseVisitor *visitor) override;
1258
1259 SourceLocation firstSourceLocation() const override
1260 { return base->firstSourceLocation(); }
1261
1262 SourceLocation lastSourceLocation() const override
1263 { return rparenToken; }
1264
1265// attributes
1266 ExpressionNode *base;
1267 ArgumentList *arguments;
1268 SourceLocation lparenToken;
1269 SourceLocation rparenToken;
1270 bool isOptional = false;
1271};
1272
1273class QML_PARSER_EXPORT ArgumentList: public Node
1274{
1275public:
1276 QQMLJS_DECLARE_AST_NODE(ArgumentList)
1277
1278 ArgumentList(ExpressionNode *e):
1279 expression (e), next (this)
1280 { kind = K; }
1281
1282 ArgumentList(ArgumentList *previous, ExpressionNode *e):
1283 expression (e)
1284 {
1285 kind = K;
1286 next = previous->next;
1287 previous->next = this;
1288 }
1289
1290 void accept0(BaseVisitor *visitor) override;
1291
1292 SourceLocation firstSourceLocation() const override
1293 { return expression->firstSourceLocation(); }
1294
1295 SourceLocation lastSourceLocation() const override
1296 {
1297 if (next)
1298 return next->lastSourceLocation();
1299 return expression->lastSourceLocation();
1300 }
1301
1302 inline ArgumentList *finish ()
1303 {
1304 ArgumentList *front = next;
1305 next = nullptr;
1306 return front;
1307 }
1308
1309// attributes
1310 ExpressionNode *expression;
1311 ArgumentList *next;
1312 SourceLocation commaToken;
1313 bool isSpreadElement = false;
1314};
1315
1316class QML_PARSER_EXPORT PostIncrementExpression: public ExpressionNode
1317{
1318public:
1319 QQMLJS_DECLARE_AST_NODE(PostIncrementExpression)
1320
1321 PostIncrementExpression(ExpressionNode *b):
1322 base (b) { kind = K; }
1323
1324 void accept0(BaseVisitor *visitor) override;
1325
1326 SourceLocation firstSourceLocation() const override
1327 { return base->firstSourceLocation(); }
1328
1329 SourceLocation lastSourceLocation() const override
1330 { return incrementToken; }
1331
1332// attributes
1333 ExpressionNode *base;
1334 SourceLocation incrementToken;
1335};
1336
1337class QML_PARSER_EXPORT PostDecrementExpression: public ExpressionNode
1338{
1339public:
1340 QQMLJS_DECLARE_AST_NODE(PostDecrementExpression)
1341
1342 PostDecrementExpression(ExpressionNode *b):
1343 base (b) { kind = K; }
1344
1345 void accept0(BaseVisitor *visitor) override;
1346
1347 SourceLocation firstSourceLocation() const override
1348 { return base->firstSourceLocation(); }
1349
1350 SourceLocation lastSourceLocation() const override
1351 { return decrementToken; }
1352
1353// attributes
1354 ExpressionNode *base;
1355 SourceLocation decrementToken;
1356};
1357
1358class QML_PARSER_EXPORT DeleteExpression: public ExpressionNode
1359{
1360public:
1361 QQMLJS_DECLARE_AST_NODE(DeleteExpression)
1362
1363 DeleteExpression(ExpressionNode *e):
1364 expression (e) { kind = K; }
1365
1366 void accept0(BaseVisitor *visitor) override;
1367
1368 SourceLocation firstSourceLocation() const override
1369 { return deleteToken; }
1370
1371 SourceLocation lastSourceLocation() const override
1372 { return expression->lastSourceLocation(); }
1373
1374// attributes
1375 ExpressionNode *expression;
1376 SourceLocation deleteToken;
1377};
1378
1379class QML_PARSER_EXPORT VoidExpression: public ExpressionNode
1380{
1381public:
1382 QQMLJS_DECLARE_AST_NODE(VoidExpression)
1383
1384 VoidExpression(ExpressionNode *e):
1385 expression (e) { kind = K; }
1386
1387 void accept0(BaseVisitor *visitor) override;
1388
1389 SourceLocation firstSourceLocation() const override
1390 { return voidToken; }
1391
1392 SourceLocation lastSourceLocation() const override
1393 { return expression->lastSourceLocation(); }
1394
1395// attributes
1396 ExpressionNode *expression;
1397 SourceLocation voidToken;
1398};
1399
1400class QML_PARSER_EXPORT TypeOfExpression: public ExpressionNode
1401{
1402public:
1403 QQMLJS_DECLARE_AST_NODE(TypeOfExpression)
1404
1405 TypeOfExpression(ExpressionNode *e):
1406 expression (e) { kind = K; }
1407
1408 void accept0(BaseVisitor *visitor) override;
1409
1410 SourceLocation firstSourceLocation() const override
1411 { return typeofToken; }
1412
1413 SourceLocation lastSourceLocation() const override
1414 { return expression->lastSourceLocation(); }
1415
1416// attributes
1417 ExpressionNode *expression;
1418 SourceLocation typeofToken;
1419};
1420
1421class QML_PARSER_EXPORT PreIncrementExpression: public ExpressionNode
1422{
1423public:
1424 QQMLJS_DECLARE_AST_NODE(PreIncrementExpression)
1425
1426 PreIncrementExpression(ExpressionNode *e):
1427 expression (e) { kind = K; }
1428
1429 void accept0(BaseVisitor *visitor) override;
1430
1431 SourceLocation firstSourceLocation() const override
1432 { return incrementToken; }
1433
1434 SourceLocation lastSourceLocation() const override
1435 { return expression->lastSourceLocation(); }
1436
1437// attributes
1438 ExpressionNode *expression;
1439 SourceLocation incrementToken;
1440};
1441
1442class QML_PARSER_EXPORT PreDecrementExpression: public ExpressionNode
1443{
1444public:
1445 QQMLJS_DECLARE_AST_NODE(PreDecrementExpression)
1446
1447 PreDecrementExpression(ExpressionNode *e):
1448 expression (e) { kind = K; }
1449
1450 void accept0(BaseVisitor *visitor) override;
1451
1452 SourceLocation firstSourceLocation() const override
1453 { return decrementToken; }
1454
1455 SourceLocation lastSourceLocation() const override
1456 { return expression->lastSourceLocation(); }
1457
1458// attributes
1459 ExpressionNode *expression;
1460 SourceLocation decrementToken;
1461};
1462
1463class QML_PARSER_EXPORT UnaryPlusExpression: public ExpressionNode
1464{
1465public:
1466 QQMLJS_DECLARE_AST_NODE(UnaryPlusExpression)
1467
1468 UnaryPlusExpression(ExpressionNode *e):
1469 expression (e) { kind = K; }
1470
1471 void accept0(BaseVisitor *visitor) override;
1472
1473 SourceLocation firstSourceLocation() const override
1474 { return plusToken; }
1475
1476 SourceLocation lastSourceLocation() const override
1477 { return expression->lastSourceLocation(); }
1478
1479// attributes
1480 ExpressionNode *expression;
1481 SourceLocation plusToken;
1482};
1483
1484class QML_PARSER_EXPORT UnaryMinusExpression: public ExpressionNode
1485{
1486public:
1487 QQMLJS_DECLARE_AST_NODE(UnaryMinusExpression)
1488
1489 UnaryMinusExpression(ExpressionNode *e):
1490 expression (e) { kind = K; }
1491
1492 void accept0(BaseVisitor *visitor) override;
1493
1494 SourceLocation firstSourceLocation() const override
1495 { return minusToken; }
1496
1497 SourceLocation lastSourceLocation() const override
1498 { return expression->lastSourceLocation(); }
1499
1500// attributes
1501 ExpressionNode *expression;
1502 SourceLocation minusToken;
1503};
1504
1505class QML_PARSER_EXPORT TildeExpression: public ExpressionNode
1506{
1507public:
1508 QQMLJS_DECLARE_AST_NODE(TildeExpression)
1509
1510 TildeExpression(ExpressionNode *e):
1511 expression (e) { kind = K; }
1512
1513 void accept0(BaseVisitor *visitor) override;
1514
1515 SourceLocation firstSourceLocation() const override
1516 { return tildeToken; }
1517
1518 SourceLocation lastSourceLocation() const override
1519 { return expression->lastSourceLocation(); }
1520
1521// attributes
1522 ExpressionNode *expression;
1523 SourceLocation tildeToken;
1524};
1525
1526class QML_PARSER_EXPORT NotExpression: public ExpressionNode
1527{
1528public:
1529 QQMLJS_DECLARE_AST_NODE(NotExpression)
1530
1531 NotExpression(ExpressionNode *e):
1532 expression (e) { kind = K; }
1533
1534 void accept0(BaseVisitor *visitor) override;
1535
1536 SourceLocation firstSourceLocation() const override
1537 { return notToken; }
1538
1539 SourceLocation lastSourceLocation() const override
1540 { return expression->lastSourceLocation(); }
1541
1542// attributes
1543 ExpressionNode *expression;
1544 SourceLocation notToken;
1545};
1546
1547class QML_PARSER_EXPORT BinaryExpression: public ExpressionNode
1548{
1549public:
1550 QQMLJS_DECLARE_AST_NODE(BinaryExpression)
1551
1552 BinaryExpression(ExpressionNode *l, int o, ExpressionNode *r):
1553 left (l), op (o), right (r)
1554 { kind = K; }
1555
1556 BinaryExpression *binaryExpressionCast() override;
1557
1558 void accept0(BaseVisitor *visitor) override;
1559
1560 SourceLocation firstSourceLocation() const override
1561 { return left->firstSourceLocation(); }
1562
1563 SourceLocation lastSourceLocation() const override
1564 { return right->lastSourceLocation(); }
1565
1566// attributes
1567 ExpressionNode *left;
1568 int op;
1569 ExpressionNode *right;
1570 SourceLocation operatorToken;
1571};
1572
1573class QML_PARSER_EXPORT ConditionalExpression: public ExpressionNode
1574{
1575public:
1576 QQMLJS_DECLARE_AST_NODE(ConditionalExpression)
1577
1578 ConditionalExpression(ExpressionNode *e, ExpressionNode *t, ExpressionNode *f):
1579 expression (e), ok (t), ko (f)
1580 { kind = K; }
1581
1582 void accept0(BaseVisitor *visitor) override;
1583
1584 SourceLocation firstSourceLocation() const override
1585 { return expression->firstSourceLocation(); }
1586
1587 SourceLocation lastSourceLocation() const override
1588 { return ko->lastSourceLocation(); }
1589
1590// attributes
1591 ExpressionNode *expression;
1592 ExpressionNode *ok;
1593 ExpressionNode *ko;
1594 SourceLocation questionToken;
1595 SourceLocation colonToken;
1596};
1597
1598class QML_PARSER_EXPORT Expression: public ExpressionNode // ### rename
1599{
1600public:
1601 QQMLJS_DECLARE_AST_NODE(Expression)
1602
1603 Expression(ExpressionNode *l, ExpressionNode *r):
1604 left (l), right (r) { kind = K; }
1605
1606 void accept0(BaseVisitor *visitor) override;
1607
1608 SourceLocation firstSourceLocation() const override
1609 { return left->firstSourceLocation(); }
1610
1611 SourceLocation lastSourceLocation() const override
1612 { return right->lastSourceLocation(); }
1613
1614// attributes
1615 ExpressionNode *left;
1616 ExpressionNode *right;
1617 SourceLocation commaToken;
1618};
1619
1620class QML_PARSER_EXPORT Block: public Statement
1621{
1622public:
1623 QQMLJS_DECLARE_AST_NODE(Block)
1624
1625 Block(StatementList *slist):
1626 statements (slist) { kind = K; }
1627
1628 void accept0(BaseVisitor *visitor) override;
1629
1630 SourceLocation firstSourceLocation() const override
1631 { return lbraceToken; }
1632
1633 SourceLocation lastSourceLocation() const override
1634 { return rbraceToken; }
1635
1636 // attributes
1637 StatementList *statements;
1638 SourceLocation lbraceToken;
1639 SourceLocation rbraceToken;
1640};
1641
1642class QML_PARSER_EXPORT StatementList: public Node
1643{
1644public:
1645 QQMLJS_DECLARE_AST_NODE(StatementList)
1646
1647 // ### This should be a Statement, but FunctionDeclaration currently doesn't inherit it.
1648 StatementList(Node *stmt)
1649 : statement(stmt), next (this)
1650 { kind = K; }
1651
1652 StatementList *append(StatementList *n) {
1653 n->next = next;
1654 next = n;
1655 return n;
1656 }
1657
1658 void accept0(BaseVisitor *visitor) override;
1659
1660 SourceLocation firstSourceLocation() const override
1661 { return statement->firstSourceLocation(); }
1662
1663 SourceLocation lastSourceLocation() const override
1664 {
1665 return lastListElement(head: this)->statement->lastSourceLocation();
1666 }
1667
1668 inline StatementList *finish ()
1669 {
1670 StatementList *front = next;
1671 next = nullptr;
1672 return front;
1673 }
1674
1675// attributes
1676 Node *statement = nullptr;
1677 StatementList *next;
1678};
1679
1680class QML_PARSER_EXPORT VariableDeclarationList: public Node
1681{
1682public:
1683 QQMLJS_DECLARE_AST_NODE(VariableDeclarationList)
1684
1685 VariableDeclarationList(PatternElement *decl)
1686 : declaration(decl), next(this)
1687 { kind = K; }
1688
1689 VariableDeclarationList(VariableDeclarationList *previous, PatternElement *decl)
1690 : declaration(decl)
1691 {
1692 kind = K;
1693 next = previous->next;
1694 previous->next = this;
1695 }
1696
1697 void accept0(BaseVisitor *visitor) override;
1698
1699 SourceLocation firstSourceLocation() const override
1700 { return declaration->firstSourceLocation(); }
1701
1702 SourceLocation lastSourceLocation() const override
1703 {
1704 if (next)
1705 return next->lastSourceLocation();
1706 return declaration->lastSourceLocation();
1707 }
1708
1709 inline VariableDeclarationList *finish(VariableScope s)
1710 {
1711 VariableDeclarationList *front = next;
1712 next = nullptr;
1713 VariableDeclarationList *vdl;
1714 for (vdl = front; vdl != nullptr; vdl = vdl->next) {
1715 vdl->declaration->scope = s;
1716 }
1717 return front;
1718 }
1719
1720// attributes
1721 PatternElement *declaration;
1722 VariableDeclarationList *next;
1723 SourceLocation commaToken;
1724};
1725
1726class QML_PARSER_EXPORT VariableStatement: public Statement
1727{
1728public:
1729 QQMLJS_DECLARE_AST_NODE(VariableStatement)
1730
1731 VariableStatement(VariableDeclarationList *vlist):
1732 declarations (vlist)
1733 { kind = K; }
1734
1735 void accept0(BaseVisitor *visitor) override;
1736
1737 SourceLocation firstSourceLocation() const override
1738 { return declarationKindToken; }
1739
1740 SourceLocation lastSourceLocation() const override
1741 { return declarations->lastSourceLocation(); }
1742
1743// attributes
1744 VariableDeclarationList *declarations;
1745 SourceLocation declarationKindToken;
1746};
1747
1748class QML_PARSER_EXPORT EmptyStatement: public Statement
1749{
1750public:
1751 QQMLJS_DECLARE_AST_NODE(EmptyStatement)
1752
1753 EmptyStatement() { kind = K; }
1754
1755 void accept0(BaseVisitor *visitor) override;
1756
1757 SourceLocation firstSourceLocation() const override
1758 { return semicolonToken; }
1759
1760 SourceLocation lastSourceLocation() const override
1761 { return semicolonToken; }
1762
1763// attributes
1764 SourceLocation semicolonToken;
1765};
1766
1767class QML_PARSER_EXPORT ExpressionStatement: public Statement
1768{
1769public:
1770 QQMLJS_DECLARE_AST_NODE(ExpressionStatement)
1771
1772 ExpressionStatement(ExpressionNode *e):
1773 expression (e) { kind = K; }
1774
1775 void accept0(BaseVisitor *visitor) override;
1776
1777 SourceLocation firstSourceLocation() const override
1778 { return expression->firstSourceLocation(); }
1779
1780 SourceLocation lastSourceLocation() const override
1781 { return semicolonToken; }
1782
1783// attributes
1784 ExpressionNode *expression;
1785 SourceLocation semicolonToken;
1786};
1787
1788class QML_PARSER_EXPORT IfStatement: public Statement
1789{
1790public:
1791 QQMLJS_DECLARE_AST_NODE(IfStatement)
1792
1793 IfStatement(ExpressionNode *e, Statement *t, Statement *f = nullptr):
1794 expression (e), ok (t), ko (f)
1795 { kind = K; }
1796
1797 void accept0(BaseVisitor *visitor) override;
1798
1799 SourceLocation firstSourceLocation() const override
1800 { return ifToken; }
1801
1802 SourceLocation lastSourceLocation() const override
1803 {
1804 if (ko)
1805 return ko->lastSourceLocation();
1806
1807 return ok->lastSourceLocation();
1808 }
1809
1810// attributes
1811 ExpressionNode *expression;
1812 Statement *ok;
1813 Statement *ko;
1814 SourceLocation ifToken;
1815 SourceLocation lparenToken;
1816 SourceLocation rparenToken;
1817 SourceLocation elseToken;
1818};
1819
1820class QML_PARSER_EXPORT DoWhileStatement: public Statement
1821{
1822public:
1823 QQMLJS_DECLARE_AST_NODE(DoWhileStatement)
1824
1825 DoWhileStatement(Statement *stmt, ExpressionNode *e):
1826 statement (stmt), expression (e)
1827 { kind = K; }
1828
1829 void accept0(BaseVisitor *visitor) override;
1830
1831 SourceLocation firstSourceLocation() const override
1832 { return doToken; }
1833
1834 SourceLocation lastSourceLocation() const override
1835 { return semicolonToken; }
1836
1837// attributes
1838 Statement *statement;
1839 ExpressionNode *expression;
1840 SourceLocation doToken;
1841 SourceLocation whileToken;
1842 SourceLocation lparenToken;
1843 SourceLocation rparenToken;
1844 SourceLocation semicolonToken;
1845};
1846
1847class QML_PARSER_EXPORT WhileStatement: public Statement
1848{
1849public:
1850 QQMLJS_DECLARE_AST_NODE(WhileStatement)
1851
1852 WhileStatement(ExpressionNode *e, Statement *stmt):
1853 expression (e), statement (stmt)
1854 { kind = K; }
1855
1856 void accept0(BaseVisitor *visitor) override;
1857
1858 SourceLocation firstSourceLocation() const override
1859 { return whileToken; }
1860
1861 SourceLocation lastSourceLocation() const override
1862 { return statement->lastSourceLocation(); }
1863
1864// attributes
1865 ExpressionNode *expression;
1866 Statement *statement;
1867 SourceLocation whileToken;
1868 SourceLocation lparenToken;
1869 SourceLocation rparenToken;
1870};
1871
1872class QML_PARSER_EXPORT ForStatement: public Statement
1873{
1874public:
1875 QQMLJS_DECLARE_AST_NODE(ForStatement)
1876
1877 ForStatement(ExpressionNode *i, ExpressionNode *c, ExpressionNode *e, Statement *stmt):
1878 initialiser (i), condition (c), expression (e), statement (stmt)
1879 { kind = K; }
1880
1881 ForStatement(VariableDeclarationList *vlist, ExpressionNode *c, ExpressionNode *e, Statement *stmt):
1882 declarations (vlist), condition (c), expression (e), statement (stmt)
1883 { kind = K; }
1884
1885
1886 void accept0(BaseVisitor *visitor) override;
1887
1888 SourceLocation firstSourceLocation() const override
1889 { return forToken; }
1890
1891 SourceLocation lastSourceLocation() const override
1892 { return statement->lastSourceLocation(); }
1893
1894// attributes
1895 ExpressionNode *initialiser = nullptr;
1896 VariableDeclarationList *declarations = nullptr;
1897 ExpressionNode *condition;
1898 ExpressionNode *expression;
1899 Statement *statement;
1900 SourceLocation forToken;
1901 SourceLocation lparenToken;
1902 SourceLocation firstSemicolonToken;
1903 SourceLocation secondSemicolonToken;
1904 SourceLocation rparenToken;
1905};
1906
1907enum class ForEachType {
1908 In,
1909 Of
1910};
1911
1912class QML_PARSER_EXPORT ForEachStatement: public Statement
1913{
1914public:
1915 QQMLJS_DECLARE_AST_NODE(ForEachStatement)
1916
1917 ForEachStatement(ExpressionNode *i, ExpressionNode *e, Statement *stmt)
1918 : lhs(i), expression(e), statement(stmt)
1919 { kind = K; }
1920 ForEachStatement(PatternElement *v, ExpressionNode *e, Statement *stmt)
1921 : lhs(v), expression(e), statement(stmt)
1922 { kind = K; }
1923
1924 void accept0(BaseVisitor *visitor) override;
1925
1926 SourceLocation firstSourceLocation() const override
1927 { return forToken; }
1928
1929 SourceLocation lastSourceLocation() const override
1930 { return statement->lastSourceLocation(); }
1931
1932 PatternElement *declaration() const {
1933 return AST::cast<PatternElement *>(ast: lhs);
1934 }
1935
1936// attributes
1937 Node *lhs;
1938 ExpressionNode *expression;
1939 Statement *statement;
1940 SourceLocation forToken;
1941 SourceLocation lparenToken;
1942 SourceLocation inOfToken;
1943 SourceLocation rparenToken;
1944 ForEachType type;
1945};
1946
1947class QML_PARSER_EXPORT ContinueStatement: public Statement
1948{
1949public:
1950 QQMLJS_DECLARE_AST_NODE(ContinueStatement)
1951
1952 ContinueStatement(QStringView l = QStringView()):
1953 label (l) { kind = K; }
1954
1955 void accept0(BaseVisitor *visitor) override;
1956
1957 SourceLocation firstSourceLocation() const override
1958 { return continueToken; }
1959
1960 SourceLocation lastSourceLocation() const override
1961 { return semicolonToken; }
1962
1963// attributes
1964 QStringView label;
1965 SourceLocation continueToken;
1966 SourceLocation identifierToken;
1967 SourceLocation semicolonToken;
1968};
1969
1970class QML_PARSER_EXPORT BreakStatement: public Statement
1971{
1972public:
1973 QQMLJS_DECLARE_AST_NODE(BreakStatement)
1974
1975 BreakStatement(QStringView l):
1976 label (l) { kind = K; }
1977
1978 void accept0(BaseVisitor *visitor) override;
1979
1980 SourceLocation firstSourceLocation() const override
1981 { return breakToken; }
1982
1983 SourceLocation lastSourceLocation() const override
1984 { return semicolonToken; }
1985
1986 // attributes
1987 QStringView label;
1988 SourceLocation breakToken;
1989 SourceLocation identifierToken;
1990 SourceLocation semicolonToken;
1991};
1992
1993class QML_PARSER_EXPORT ReturnStatement: public Statement
1994{
1995public:
1996 QQMLJS_DECLARE_AST_NODE(ReturnStatement)
1997
1998 ReturnStatement(ExpressionNode *e):
1999 expression (e) { kind = K; }
2000
2001 void accept0(BaseVisitor *visitor) override;
2002
2003 SourceLocation firstSourceLocation() const override
2004 { return returnToken; }
2005
2006 SourceLocation lastSourceLocation() const override
2007 { return semicolonToken; }
2008
2009// attributes
2010 ExpressionNode *expression;
2011 SourceLocation returnToken;
2012 SourceLocation semicolonToken;
2013};
2014
2015class QML_PARSER_EXPORT YieldExpression: public ExpressionNode
2016{
2017public:
2018 QQMLJS_DECLARE_AST_NODE(YieldExpression)
2019
2020 YieldExpression(ExpressionNode *e = nullptr):
2021 expression (e) { kind = K; }
2022
2023 void accept0(BaseVisitor *visitor) override;
2024
2025 SourceLocation firstSourceLocation() const override
2026 { return yieldToken; }
2027
2028 SourceLocation lastSourceLocation() const override
2029 { return expression ? expression->lastSourceLocation() : yieldToken; }
2030
2031// attributes
2032 ExpressionNode *expression;
2033 bool isYieldStar = false;
2034 SourceLocation yieldToken;
2035};
2036
2037class QML_PARSER_EXPORT WithStatement: public Statement
2038{
2039public:
2040 QQMLJS_DECLARE_AST_NODE(WithStatement)
2041
2042 WithStatement(ExpressionNode *e, Statement *stmt):
2043 expression (e), statement (stmt)
2044 { kind = K; }
2045
2046 void accept0(BaseVisitor *visitor) override;
2047
2048 SourceLocation firstSourceLocation() const override
2049 { return withToken; }
2050
2051 SourceLocation lastSourceLocation() const override
2052 { return statement->lastSourceLocation(); }
2053
2054// attributes
2055 ExpressionNode *expression;
2056 Statement *statement;
2057 SourceLocation withToken;
2058 SourceLocation lparenToken;
2059 SourceLocation rparenToken;
2060};
2061
2062class QML_PARSER_EXPORT CaseBlock: public Node
2063{
2064public:
2065 QQMLJS_DECLARE_AST_NODE(CaseBlock)
2066
2067 CaseBlock(CaseClauses *c, DefaultClause *d = nullptr, CaseClauses *r = nullptr):
2068 clauses (c), defaultClause (d), moreClauses (r)
2069 { kind = K; }
2070
2071 void accept0(BaseVisitor *visitor) override;
2072
2073 SourceLocation firstSourceLocation() const override
2074 { return lbraceToken; }
2075
2076 SourceLocation lastSourceLocation() const override
2077 { return rbraceToken; }
2078
2079// attributes
2080 CaseClauses *clauses;
2081 DefaultClause *defaultClause;
2082 CaseClauses *moreClauses;
2083 SourceLocation lbraceToken;
2084 SourceLocation rbraceToken;
2085};
2086
2087class QML_PARSER_EXPORT SwitchStatement: public Statement
2088{
2089public:
2090 QQMLJS_DECLARE_AST_NODE(SwitchStatement)
2091
2092 SwitchStatement(ExpressionNode *e, CaseBlock *b):
2093 expression (e), block (b)
2094 { kind = K; }
2095
2096 void accept0(BaseVisitor *visitor) override;
2097
2098 SourceLocation firstSourceLocation() const override
2099 { return switchToken; }
2100
2101 SourceLocation lastSourceLocation() const override
2102 { return block->rbraceToken; }
2103
2104// attributes
2105 ExpressionNode *expression;
2106 CaseBlock *block;
2107 SourceLocation switchToken;
2108 SourceLocation lparenToken;
2109 SourceLocation rparenToken;
2110};
2111
2112class QML_PARSER_EXPORT CaseClause: public Node
2113{
2114public:
2115 QQMLJS_DECLARE_AST_NODE(CaseClause)
2116
2117 CaseClause(ExpressionNode *e, StatementList *slist):
2118 expression (e), statements (slist)
2119 { kind = K; }
2120
2121 void accept0(BaseVisitor *visitor) override;
2122
2123 SourceLocation firstSourceLocation() const override
2124 { return caseToken; }
2125
2126 SourceLocation lastSourceLocation() const override
2127 { return statements ? statements->lastSourceLocation() : colonToken; }
2128
2129// attributes
2130 ExpressionNode *expression;
2131 StatementList *statements;
2132 SourceLocation caseToken;
2133 SourceLocation colonToken;
2134};
2135
2136class QML_PARSER_EXPORT CaseClauses: public Node
2137{
2138public:
2139 QQMLJS_DECLARE_AST_NODE(CaseClauses)
2140
2141 CaseClauses(CaseClause *c):
2142 clause (c), next (this)
2143 { kind = K; }
2144
2145 CaseClauses(CaseClauses *previous, CaseClause *c):
2146 clause (c)
2147 {
2148 kind = K;
2149 next = previous->next;
2150 previous->next = this;
2151 }
2152
2153 void accept0(BaseVisitor *visitor) override;
2154
2155 SourceLocation firstSourceLocation() const override
2156 { return clause->firstSourceLocation(); }
2157
2158 SourceLocation lastSourceLocation() const override
2159 {
2160 return lastListElement(head: this)->clause->lastSourceLocation();
2161 }
2162
2163 inline CaseClauses *finish ()
2164 {
2165 CaseClauses *front = next;
2166 next = nullptr;
2167 return front;
2168 }
2169
2170//attributes
2171 CaseClause *clause;
2172 CaseClauses *next;
2173};
2174
2175class QML_PARSER_EXPORT DefaultClause: public Node
2176{
2177public:
2178 QQMLJS_DECLARE_AST_NODE(DefaultClause)
2179
2180 DefaultClause(StatementList *slist):
2181 statements (slist)
2182 { kind = K; }
2183
2184 void accept0(BaseVisitor *visitor) override;
2185
2186 SourceLocation firstSourceLocation() const override
2187 { return defaultToken; }
2188
2189 SourceLocation lastSourceLocation() const override
2190 { return statements ? statements->lastSourceLocation() : colonToken; }
2191
2192// attributes
2193 StatementList *statements;
2194 SourceLocation defaultToken;
2195 SourceLocation colonToken;
2196};
2197
2198class QML_PARSER_EXPORT LabelledStatement: public Statement
2199{
2200public:
2201 QQMLJS_DECLARE_AST_NODE(LabelledStatement)
2202
2203 LabelledStatement(QStringView l, Statement *stmt):
2204 label (l), statement (stmt)
2205 { kind = K; }
2206
2207 void accept0(BaseVisitor *visitor) override;
2208
2209 SourceLocation firstSourceLocation() const override
2210 { return identifierToken; }
2211
2212 SourceLocation lastSourceLocation() const override
2213 { return statement->lastSourceLocation(); }
2214
2215// attributes
2216 QStringView label;
2217 Statement *statement;
2218 SourceLocation identifierToken;
2219 SourceLocation colonToken;
2220};
2221
2222class QML_PARSER_EXPORT ThrowStatement: public Statement
2223{
2224public:
2225 QQMLJS_DECLARE_AST_NODE(ThrowStatement)
2226
2227 ThrowStatement(ExpressionNode *e):
2228 expression (e) { kind = K; }
2229
2230 void accept0(BaseVisitor *visitor) override;
2231
2232 SourceLocation firstSourceLocation() const override
2233 { return throwToken; }
2234
2235 SourceLocation lastSourceLocation() const override
2236 { return semicolonToken; }
2237
2238 // attributes
2239 ExpressionNode *expression;
2240 SourceLocation throwToken;
2241 SourceLocation semicolonToken;
2242};
2243
2244class QML_PARSER_EXPORT Catch: public Node
2245{
2246public:
2247 QQMLJS_DECLARE_AST_NODE(Catch)
2248
2249 Catch(PatternElement *p, Block *stmt)
2250 : patternElement(p), statement(stmt)
2251 { kind = K; }
2252
2253 void accept0(BaseVisitor *visitor) override;
2254
2255 SourceLocation firstSourceLocation() const override
2256 { return catchToken; }
2257
2258 SourceLocation lastSourceLocation() const override
2259 { return statement->lastSourceLocation(); }
2260
2261// attributes
2262 PatternElement *patternElement;
2263 Block *statement;
2264 SourceLocation catchToken;
2265 SourceLocation lparenToken;
2266 SourceLocation identifierToken;
2267 SourceLocation rparenToken;
2268};
2269
2270class QML_PARSER_EXPORT Finally: public Node
2271{
2272public:
2273 QQMLJS_DECLARE_AST_NODE(Finally)
2274
2275 Finally(Block *stmt):
2276 statement (stmt)
2277 { kind = K; }
2278
2279 void accept0(BaseVisitor *visitor) override;
2280
2281 SourceLocation firstSourceLocation() const override
2282 { return finallyToken; }
2283
2284 SourceLocation lastSourceLocation() const override
2285 { return statement ? statement->lastSourceLocation() : finallyToken; }
2286
2287// attributes
2288 Block *statement;
2289 SourceLocation finallyToken;
2290};
2291
2292class QML_PARSER_EXPORT TryStatement: public Statement
2293{
2294public:
2295 QQMLJS_DECLARE_AST_NODE(TryStatement)
2296
2297 TryStatement(Statement *stmt, Catch *c, Finally *f):
2298 statement (stmt), catchExpression (c), finallyExpression (f)
2299 { kind = K; }
2300
2301 TryStatement(Statement *stmt, Finally *f):
2302 statement (stmt), catchExpression (nullptr), finallyExpression (f)
2303 { kind = K; }
2304
2305 TryStatement(Statement *stmt, Catch *c):
2306 statement (stmt), catchExpression (c), finallyExpression (nullptr)
2307 { kind = K; }
2308
2309 void accept0(BaseVisitor *visitor) override;
2310
2311 SourceLocation firstSourceLocation() const override
2312 { return tryToken; }
2313
2314 SourceLocation lastSourceLocation() const override
2315 {
2316 if (finallyExpression)
2317 return finallyExpression->statement->rbraceToken;
2318 else if (catchExpression)
2319 return catchExpression->statement->rbraceToken;
2320
2321 return statement->lastSourceLocation();
2322 }
2323
2324// attributes
2325 Statement *statement;
2326 Catch *catchExpression;
2327 Finally *finallyExpression;
2328 SourceLocation tryToken;
2329};
2330
2331class QML_PARSER_EXPORT FunctionExpression: public ExpressionNode
2332{
2333public:
2334 QQMLJS_DECLARE_AST_NODE(FunctionExpression)
2335
2336 FunctionExpression(QStringView n, FormalParameterList *f, StatementList *b, TypeAnnotation *typeAnnotation = nullptr):
2337 name (n), formals (f), body (b),
2338 typeAnnotation(typeAnnotation)
2339 { kind = K; }
2340
2341 void accept0(BaseVisitor *visitor) override;
2342
2343 SourceLocation firstSourceLocation() const override
2344 { return functionToken; }
2345
2346 SourceLocation lastSourceLocation() const override
2347 { return rbraceToken; }
2348
2349 FunctionExpression *asFunctionDefinition() override;
2350
2351// attributes
2352 QStringView name;
2353 bool isArrowFunction = false;
2354 bool isGenerator = false;
2355 FormalParameterList *formals;
2356 StatementList *body;
2357 TypeAnnotation *typeAnnotation;
2358 SourceLocation functionToken;
2359 SourceLocation identifierToken;
2360 SourceLocation lparenToken;
2361 SourceLocation rparenToken;
2362 SourceLocation lbraceToken;
2363 SourceLocation rbraceToken;
2364};
2365
2366class QML_PARSER_EXPORT FunctionDeclaration: public FunctionExpression
2367{
2368public:
2369 QQMLJS_DECLARE_AST_NODE(FunctionDeclaration)
2370
2371 FunctionDeclaration(QStringView n, FormalParameterList *f, StatementList *b, TypeAnnotation *typeAnnotation = nullptr):
2372 FunctionExpression(n, f, b, typeAnnotation)
2373 { kind = K; }
2374
2375 void accept0(BaseVisitor *visitor) override;
2376};
2377
2378class QML_PARSER_EXPORT FormalParameterList: public Node
2379{
2380public:
2381 QQMLJS_DECLARE_AST_NODE(FormalParameterList)
2382
2383 FormalParameterList(FormalParameterList *previous, PatternElement *e)
2384 : element(e)
2385 {
2386 kind = K;
2387 if (previous) {
2388 next = previous->next;
2389 previous->next = this;
2390 } else {
2391 next = this;
2392 }
2393 }
2394
2395 FormalParameterList *append(FormalParameterList *n) {
2396 n->next = next;
2397 next = n;
2398 return n;
2399 }
2400
2401 bool isSimpleParameterList()
2402 {
2403 AST::FormalParameterList *formals = this;
2404 while (formals) {
2405 PatternElement *e = formals->element;
2406 if (e && e->type == PatternElement::RestElement)
2407 return false;
2408 if (e && (e->initializer || e->bindingTarget))
2409 return false;
2410 formals = formals->next;
2411 }
2412 return true;
2413 }
2414
2415 int length()
2416 {
2417 // the length property of Function objects
2418 int l = 0;
2419 AST::FormalParameterList *formals = this;
2420 while (formals) {
2421 PatternElement *e = formals->element;
2422 if (!e || e->initializer)
2423 break;
2424 if (e->type == PatternElement::RestElement)
2425 break;
2426 ++l;
2427 formals = formals->next;
2428 }
2429 return l;
2430 }
2431
2432 bool containsName(const QString &name) const {
2433 for (const FormalParameterList *it = this; it; it = it->next) {
2434 PatternElement *b = it->element;
2435 // ### handle binding patterns
2436 if (b && b->bindingIdentifier == name)
2437 return true;
2438 }
2439 return false;
2440 }
2441
2442 BoundNames formals() const;
2443
2444 BoundNames boundNames() const;
2445
2446 void accept0(BaseVisitor *visitor) override;
2447
2448 SourceLocation firstSourceLocation() const override
2449 { return element->firstSourceLocation(); }
2450
2451 SourceLocation lastSourceLocation() const override
2452 {
2453 return lastListElement(head: this)->element->lastSourceLocation();
2454 }
2455
2456 FormalParameterList *finish(MemoryPool *pool);
2457
2458// attributes
2459 PatternElement *element = nullptr;
2460 FormalParameterList *next;
2461};
2462
2463class QML_PARSER_EXPORT ClassExpression : public ExpressionNode
2464{
2465public:
2466 QQMLJS_DECLARE_AST_NODE(ClassExpression)
2467
2468 ClassExpression(QStringView n, ExpressionNode *heritage, ClassElementList *elements)
2469 : name(n), heritage(heritage), elements(elements)
2470 { kind = K; }
2471
2472 void accept0(BaseVisitor *visitor) override;
2473
2474 SourceLocation firstSourceLocation() const override
2475 { return classToken; }
2476
2477 SourceLocation lastSourceLocation() const override
2478 { return rbraceToken; }
2479
2480 ClassExpression *asClassDefinition() override;
2481
2482// attributes
2483 QStringView name;
2484 ExpressionNode *heritage;
2485 ClassElementList *elements;
2486 SourceLocation classToken;
2487 SourceLocation identifierToken;
2488 SourceLocation lbraceToken;
2489 SourceLocation rbraceToken;
2490};
2491
2492class QML_PARSER_EXPORT ClassDeclaration: public ClassExpression
2493{
2494public:
2495 QQMLJS_DECLARE_AST_NODE(ClassDeclaration)
2496
2497 ClassDeclaration(QStringView n, ExpressionNode *heritage, ClassElementList *elements)
2498 : ClassExpression(n, heritage, elements)
2499 { kind = K; }
2500
2501 void accept0(BaseVisitor *visitor) override;
2502};
2503
2504
2505class QML_PARSER_EXPORT ClassElementList : public Node
2506{
2507public:
2508 QQMLJS_DECLARE_AST_NODE(ClassElementList)
2509
2510 ClassElementList(PatternProperty *property, bool isStatic)
2511 : isStatic(isStatic), property(property)
2512 {
2513 kind = K;
2514 next = this;
2515 }
2516
2517 ClassElementList *append(ClassElementList *n) {
2518 n->next = next;
2519 next = n;
2520 return n;
2521 }
2522
2523 void accept0(BaseVisitor *visitor) override;
2524
2525 SourceLocation firstSourceLocation() const override
2526 { return property->firstSourceLocation(); }
2527
2528 SourceLocation lastSourceLocation() const override
2529 {
2530 if (next)
2531 return next->lastSourceLocation();
2532 return property->lastSourceLocation();
2533 }
2534
2535 ClassElementList *finish();
2536
2537 bool isStatic;
2538 ClassElementList *next;
2539 PatternProperty *property;
2540};
2541
2542class QML_PARSER_EXPORT Program: public Node
2543{
2544public:
2545 QQMLJS_DECLARE_AST_NODE(Program)
2546
2547 Program(StatementList *statements)
2548 : statements(statements)
2549 { kind = K; }
2550
2551 void accept0(BaseVisitor *visitor) override;
2552
2553 SourceLocation firstSourceLocation() const override
2554 { return statements ? statements->firstSourceLocation() : SourceLocation(); }
2555
2556 SourceLocation lastSourceLocation() const override
2557 { return statements ? statements->lastSourceLocation() : SourceLocation(); }
2558
2559// attributes
2560 StatementList *statements;
2561};
2562
2563class QML_PARSER_EXPORT ImportSpecifier: public Node
2564{
2565public:
2566 QQMLJS_DECLARE_AST_NODE(ImportSpecifier)
2567
2568 ImportSpecifier(QStringView importedBinding)
2569 : importedBinding(importedBinding)
2570 {
2571 kind = K;
2572 }
2573
2574 ImportSpecifier(QStringView identifier, QStringView importedBinding)
2575 : identifier(identifier), importedBinding(importedBinding)
2576 {
2577 kind = K;
2578 }
2579
2580 void accept0(BaseVisitor *visitor) override;
2581
2582 SourceLocation firstSourceLocation() const override
2583 { return identifier.isNull() ? importedBindingToken : identifierToken; }
2584 SourceLocation lastSourceLocation() const override
2585 { return importedBindingToken; }
2586
2587// attributes
2588 SourceLocation identifierToken;
2589 SourceLocation importedBindingToken;
2590 QStringView identifier;
2591 QStringView importedBinding;
2592};
2593
2594class QML_PARSER_EXPORT ImportsList: public Node
2595{
2596public:
2597 QQMLJS_DECLARE_AST_NODE(ImportsList)
2598
2599 ImportsList(ImportSpecifier *importSpecifier)
2600 : importSpecifier(importSpecifier)
2601 {
2602 kind = K;
2603 next = this;
2604 }
2605
2606 ImportsList(ImportsList *previous, ImportSpecifier *importSpecifier)
2607 : importSpecifier(importSpecifier)
2608 {
2609 kind = K;
2610 if (previous) {
2611 next = previous->next;
2612 previous->next = this;
2613 } else {
2614 next = this;
2615 }
2616 }
2617
2618 ImportsList *finish()
2619 {
2620 ImportsList *head = next;
2621 next = nullptr;
2622 return head;
2623 }
2624
2625 void accept0(BaseVisitor *visitor) override;
2626
2627 SourceLocation firstSourceLocation() const override
2628 { return importSpecifierToken; }
2629
2630 SourceLocation lastSourceLocation() const override
2631 {
2632 return lastListElement(head: this)->importSpecifierToken;
2633 }
2634
2635// attributes
2636 SourceLocation importSpecifierToken;
2637 ImportSpecifier *importSpecifier;
2638 ImportsList *next = this;
2639};
2640
2641class QML_PARSER_EXPORT NamedImports: public Node
2642{
2643public:
2644 QQMLJS_DECLARE_AST_NODE(NamedImports)
2645
2646 NamedImports()
2647 {
2648 kind = K;
2649 }
2650
2651 NamedImports(ImportsList *importsList)
2652 : importsList(importsList)
2653 {
2654 kind = K;
2655 }
2656
2657 void accept0(BaseVisitor *visitor) override;
2658
2659 SourceLocation firstSourceLocation() const override
2660 { return leftBraceToken; }
2661 SourceLocation lastSourceLocation() const override
2662 { return rightBraceToken; }
2663
2664// attributes
2665 SourceLocation leftBraceToken;
2666 SourceLocation rightBraceToken;
2667 ImportsList *importsList = nullptr;
2668};
2669
2670class QML_PARSER_EXPORT NameSpaceImport: public Node
2671{
2672public:
2673 QQMLJS_DECLARE_AST_NODE(NameSpaceImport)
2674
2675 NameSpaceImport(QStringView importedBinding)
2676 : importedBinding(importedBinding)
2677 {
2678 kind = K;
2679 }
2680
2681 void accept0(BaseVisitor *visitor) override;
2682
2683 virtual SourceLocation firstSourceLocation() const override
2684 { return starToken; }
2685 virtual SourceLocation lastSourceLocation() const override
2686 { return importedBindingToken; }
2687
2688// attributes
2689 SourceLocation starToken;
2690 SourceLocation importedBindingToken;
2691 QStringView importedBinding;
2692};
2693
2694class QML_PARSER_EXPORT ImportClause: public Node
2695{
2696public:
2697 QQMLJS_DECLARE_AST_NODE(ImportClause)
2698
2699 ImportClause(QStringView importedDefaultBinding)
2700 : importedDefaultBinding(importedDefaultBinding)
2701 {
2702 kind = K;
2703 }
2704
2705 ImportClause(NameSpaceImport *nameSpaceImport)
2706 : nameSpaceImport(nameSpaceImport)
2707 {
2708 kind = K;
2709 }
2710
2711 ImportClause(NamedImports *namedImports)
2712 : namedImports(namedImports)
2713 {
2714 kind = K;
2715 }
2716
2717 ImportClause(QStringView importedDefaultBinding, NameSpaceImport *nameSpaceImport)
2718 : importedDefaultBinding(importedDefaultBinding)
2719 , nameSpaceImport(nameSpaceImport)
2720 {
2721 kind = K;
2722 }
2723
2724 ImportClause(QStringView importedDefaultBinding, NamedImports *namedImports)
2725 : importedDefaultBinding(importedDefaultBinding)
2726 , namedImports(namedImports)
2727 {
2728 kind = K;
2729 }
2730
2731 void accept0(BaseVisitor *visitor) override;
2732
2733 virtual SourceLocation firstSourceLocation() const override
2734 { return importedDefaultBinding.isNull() ? (nameSpaceImport ? nameSpaceImport->firstSourceLocation() : namedImports->firstSourceLocation()) : importedDefaultBindingToken; }
2735 virtual SourceLocation lastSourceLocation() const override
2736 { return importedDefaultBinding.isNull() ? (nameSpaceImport ? nameSpaceImport->lastSourceLocation() : namedImports->lastSourceLocation()) : importedDefaultBindingToken; }
2737
2738// attributes
2739 SourceLocation importedDefaultBindingToken;
2740 QStringView importedDefaultBinding;
2741 NameSpaceImport *nameSpaceImport = nullptr;
2742 NamedImports *namedImports = nullptr;
2743};
2744
2745class QML_PARSER_EXPORT FromClause: public Node
2746{
2747public:
2748 QQMLJS_DECLARE_AST_NODE(FromClause)
2749
2750 FromClause(QStringView moduleSpecifier)
2751 : moduleSpecifier(moduleSpecifier)
2752 {
2753 kind = K;
2754 }
2755
2756 void accept0(BaseVisitor *visitor) override;
2757
2758 SourceLocation firstSourceLocation() const override
2759 { return fromToken; }
2760
2761 SourceLocation lastSourceLocation() const override
2762 { return moduleSpecifierToken; }
2763
2764// attributes
2765 SourceLocation fromToken;
2766 SourceLocation moduleSpecifierToken;
2767 QStringView moduleSpecifier;
2768};
2769
2770class QML_PARSER_EXPORT ImportDeclaration: public Statement
2771{
2772public:
2773 QQMLJS_DECLARE_AST_NODE(ImportDeclaration)
2774
2775 ImportDeclaration(ImportClause *importClause, FromClause *fromClause)
2776 : importClause(importClause), fromClause(fromClause)
2777 {
2778 kind = K;
2779 }
2780
2781 ImportDeclaration(QStringView moduleSpecifier)
2782 : moduleSpecifier(moduleSpecifier)
2783 {
2784 kind = K;
2785 }
2786
2787 void accept0(BaseVisitor *visitor) override;
2788
2789 SourceLocation firstSourceLocation() const override
2790 { return importToken; }
2791
2792 SourceLocation lastSourceLocation() const override
2793 { return moduleSpecifier.isNull() ? fromClause->lastSourceLocation() : moduleSpecifierToken; }
2794
2795// attributes
2796 SourceLocation importToken;
2797 SourceLocation moduleSpecifierToken;
2798 QStringView moduleSpecifier;
2799 ImportClause *importClause = nullptr;
2800 FromClause *fromClause = nullptr;
2801};
2802
2803class QML_PARSER_EXPORT ExportSpecifier: public Node
2804{
2805public:
2806 QQMLJS_DECLARE_AST_NODE(ExportSpecifier)
2807
2808 ExportSpecifier(QStringView identifier)
2809 : identifier(identifier), exportedIdentifier(identifier)
2810 {
2811 kind = K;
2812 }
2813
2814 ExportSpecifier(QStringView identifier, QStringView exportedIdentifier)
2815 : identifier(identifier), exportedIdentifier(exportedIdentifier)
2816 {
2817 kind = K;
2818 }
2819
2820 void accept0(BaseVisitor *visitor) override;
2821
2822 SourceLocation firstSourceLocation() const override
2823 { return identifierToken; }
2824 SourceLocation lastSourceLocation() const override
2825 { return exportedIdentifierToken.isValid() ? exportedIdentifierToken : identifierToken; }
2826
2827// attributes
2828 SourceLocation identifierToken;
2829 SourceLocation exportedIdentifierToken;
2830 QStringView identifier;
2831 QStringView exportedIdentifier;
2832};
2833
2834class QML_PARSER_EXPORT ExportsList: public Node
2835{
2836public:
2837 QQMLJS_DECLARE_AST_NODE(ExportsList)
2838
2839 ExportsList(ExportSpecifier *exportSpecifier)
2840 : exportSpecifier(exportSpecifier)
2841 {
2842 kind = K;
2843 next = this;
2844 }
2845
2846 ExportsList(ExportsList *previous, ExportSpecifier *exportSpecifier)
2847 : exportSpecifier(exportSpecifier)
2848 {
2849 kind = K;
2850 if (previous) {
2851 next = previous->next;
2852 previous->next = this;
2853 } else {
2854 next = this;
2855 }
2856 }
2857
2858 ExportsList *finish()
2859 {
2860 ExportsList *head = next;
2861 next = nullptr;
2862 return head;
2863 }
2864
2865 void accept0(BaseVisitor *visitor) override;
2866
2867 SourceLocation firstSourceLocation() const override
2868 { return exportSpecifier->firstSourceLocation(); }
2869 SourceLocation lastSourceLocation() const override
2870 { return lastListElement(head: this)->exportSpecifier->lastSourceLocation(); }
2871
2872// attributes
2873 ExportSpecifier *exportSpecifier;
2874 ExportsList *next;
2875};
2876
2877class QML_PARSER_EXPORT ExportClause: public Node
2878{
2879public:
2880 QQMLJS_DECLARE_AST_NODE(ExportClause)
2881
2882 ExportClause()
2883 {
2884 kind = K;
2885 }
2886
2887 ExportClause(ExportsList *exportsList)
2888 : exportsList(exportsList)
2889 {
2890 kind = K;
2891 }
2892
2893 void accept0(BaseVisitor *visitor) override;
2894
2895 SourceLocation firstSourceLocation() const override
2896 { return leftBraceToken; }
2897 SourceLocation lastSourceLocation() const override
2898 { return rightBraceToken; }
2899
2900// attributes
2901 SourceLocation leftBraceToken;
2902 SourceLocation rightBraceToken;
2903 ExportsList *exportsList = nullptr;
2904};
2905
2906class QML_PARSER_EXPORT ExportDeclaration: public Statement
2907{
2908public:
2909 QQMLJS_DECLARE_AST_NODE(ExportDeclaration)
2910
2911 ExportDeclaration(FromClause *fromClause)
2912 : fromClause(fromClause)
2913 {
2914 kind = K;
2915 }
2916
2917 ExportDeclaration(ExportClause *exportClause, FromClause *fromClause)
2918 : exportClause(exportClause), fromClause(fromClause)
2919 {
2920 kind = K;
2921 }
2922
2923 ExportDeclaration(ExportClause *exportClause)
2924 : exportClause(exportClause)
2925 {
2926 kind = K;
2927 }
2928
2929 ExportDeclaration(bool exportDefault, Node *variableStatementOrDeclaration)
2930 : variableStatementOrDeclaration(variableStatementOrDeclaration)
2931 , exportDefault(exportDefault)
2932 {
2933 kind = K;
2934 }
2935
2936 bool exportsAll() const
2937 {
2938 return fromClause && !exportClause;
2939 }
2940
2941 void accept0(BaseVisitor *visitor) override;
2942
2943 SourceLocation firstSourceLocation() const override
2944 { return exportToken; }
2945 SourceLocation lastSourceLocation() const override
2946 { return fromClause ? fromClause->lastSourceLocation() : (exportClause ? exportClause->lastSourceLocation() : variableStatementOrDeclaration->lastSourceLocation()); }
2947
2948// attributes
2949 SourceLocation exportToken;
2950 ExportClause *exportClause = nullptr;
2951 FromClause *fromClause = nullptr;
2952 Node *variableStatementOrDeclaration = nullptr;
2953 bool exportDefault = false;
2954};
2955
2956class QML_PARSER_EXPORT ESModule: public Node
2957{
2958public:
2959 QQMLJS_DECLARE_AST_NODE(Module)
2960
2961 ESModule(StatementList *body)
2962 : body(body)
2963 {
2964 kind = K;
2965 }
2966
2967 void accept0(BaseVisitor *visitor) override;
2968
2969 SourceLocation firstSourceLocation() const override
2970 { return body ? body->firstSourceLocation() : SourceLocation(); }
2971
2972 SourceLocation lastSourceLocation() const override
2973 { return body ? body->lastSourceLocation() : SourceLocation(); }
2974
2975// attributes
2976 StatementList *body;
2977};
2978
2979class QML_PARSER_EXPORT DebuggerStatement: public Statement
2980{
2981public:
2982 QQMLJS_DECLARE_AST_NODE(DebuggerStatement)
2983
2984 DebuggerStatement()
2985 { kind = K; }
2986
2987 void accept0(BaseVisitor *visitor) override;
2988
2989 SourceLocation firstSourceLocation() const override
2990 { return debuggerToken; }
2991
2992 SourceLocation lastSourceLocation() const override
2993 { return semicolonToken; }
2994
2995// attributes
2996 SourceLocation debuggerToken;
2997 SourceLocation semicolonToken;
2998};
2999
3000class QML_PARSER_EXPORT UiImport: public Node
3001{
3002public:
3003 QQMLJS_DECLARE_AST_NODE(UiImport)
3004
3005 UiImport(QStringView fileName)
3006 : fileName(fileName), importUri(nullptr)
3007 { kind = K; }
3008
3009 UiImport(UiQualifiedId *uri)
3010 : importUri(uri)
3011 { kind = K; }
3012
3013 void accept0(BaseVisitor *visitor) override;
3014
3015 SourceLocation firstSourceLocation() const override
3016 { return importToken; }
3017
3018 SourceLocation lastSourceLocation() const override
3019 { return semicolonToken; }
3020
3021// attributes
3022 QStringView fileName;
3023 UiQualifiedId *importUri;
3024 QStringView importId;
3025 SourceLocation importToken;
3026 SourceLocation fileNameToken;
3027 SourceLocation asToken;
3028 SourceLocation importIdToken;
3029 SourceLocation semicolonToken;
3030 UiVersionSpecifier *version = nullptr;
3031};
3032
3033class QML_PARSER_EXPORT UiObjectMember: public Node
3034{
3035public:
3036 SourceLocation firstSourceLocation() const override = 0;
3037 SourceLocation lastSourceLocation() const override = 0;
3038
3039 UiObjectMember *uiObjectMemberCast() override;
3040
3041// attributes
3042 UiAnnotationList *annotations = nullptr;
3043};
3044
3045class QML_PARSER_EXPORT UiObjectMemberList: public Node
3046{
3047public:
3048 QQMLJS_DECLARE_AST_NODE(UiObjectMemberList)
3049
3050 UiObjectMemberList(UiObjectMember *member)
3051 : next(this), member(member)
3052 { kind = K; }
3053
3054 UiObjectMemberList(UiObjectMemberList *previous, UiObjectMember *member)
3055 : member(member)
3056 {
3057 kind = K;
3058 next = previous->next;
3059 previous->next = this;
3060 }
3061
3062 void accept0(BaseVisitor *visitor) override;
3063
3064 SourceLocation firstSourceLocation() const override
3065 { return member->firstSourceLocation(); }
3066
3067 SourceLocation lastSourceLocation() const override
3068 { return lastListElement(head: this)->member->lastSourceLocation(); }
3069
3070 UiObjectMemberList *finish()
3071 {
3072 UiObjectMemberList *head = next;
3073 next = nullptr;
3074 return head;
3075 }
3076
3077// attributes
3078 UiObjectMemberList *next;
3079 UiObjectMember *member;
3080};
3081
3082class QML_PARSER_EXPORT UiPragmaValueList: public Node
3083{
3084public:
3085 QQMLJS_DECLARE_AST_NODE(UiPragmaValueList)
3086
3087 UiPragmaValueList(QStringView value)
3088 : value(value)
3089 , next(this)
3090 {
3091 kind = K;
3092 }
3093
3094 UiPragmaValueList(UiPragmaValueList *previous, QStringView value)
3095 : value(value)
3096 {
3097 kind = K;
3098 next = previous->next;
3099 previous->next = this;
3100 }
3101
3102 void accept0(BaseVisitor *visitor) override;
3103
3104 SourceLocation firstSourceLocation() const override
3105 { return location; }
3106
3107 SourceLocation lastSourceLocation() const override
3108 { return lastListElement(head: this)->location; }
3109
3110 UiPragmaValueList *finish()
3111 {
3112 UiPragmaValueList *head = next;
3113 next = nullptr;
3114 return head;
3115 }
3116
3117 QStringView value;
3118 UiPragmaValueList *next;
3119 SourceLocation location;
3120};
3121
3122class QML_PARSER_EXPORT UiPragma: public Node
3123{
3124public:
3125 QQMLJS_DECLARE_AST_NODE(UiPragma)
3126
3127 UiPragma(QStringView name, UiPragmaValueList *values = nullptr)
3128 : name(name), values(values)
3129 { kind = K; }
3130
3131 void accept0(BaseVisitor *visitor) override;
3132
3133 SourceLocation firstSourceLocation() const override
3134 { return pragmaToken; }
3135
3136 SourceLocation lastSourceLocation() const override
3137 { return semicolonToken; }
3138
3139// attributes
3140 QStringView name;
3141 UiPragmaValueList *values;
3142 SourceLocation pragmaToken;
3143 SourceLocation semicolonToken;
3144};
3145
3146class QML_PARSER_EXPORT UiRequired: public Node
3147{
3148public:
3149 QQMLJS_DECLARE_AST_NODE(UiRequired)
3150
3151 UiRequired(QStringView name)
3152 :name(name)
3153 { kind = K; }
3154
3155 void accept0(BaseVisitor *visitor) override;
3156
3157 SourceLocation firstSourceLocation() const override
3158 { return requiredToken; }
3159
3160 SourceLocation lastSourceLocation() const override
3161 { return semicolonToken; }
3162
3163 QStringView name;
3164 SourceLocation requiredToken;
3165 SourceLocation semicolonToken;
3166};
3167
3168class QML_PARSER_EXPORT UiHeaderItemList: public Node
3169{
3170public:
3171 QQMLJS_DECLARE_AST_NODE(UiHeaderItemList)
3172
3173 UiHeaderItemList(UiImport *import)
3174 : headerItem(import), next(this)
3175 { kind = K; }
3176
3177 UiHeaderItemList(UiPragma *pragma)
3178 : headerItem(pragma), next(this)
3179 { kind = K; }
3180
3181 UiHeaderItemList(UiHeaderItemList *previous, UiImport *import)
3182 : headerItem(import)
3183 {
3184 kind = K;
3185 next = previous->next;
3186 previous->next = this;
3187 }
3188
3189 UiHeaderItemList(UiHeaderItemList *previous, UiPragma *pragma)
3190 : headerItem(pragma)
3191 {
3192 kind = K;
3193 next = previous->next;
3194 previous->next = this;
3195 }
3196
3197 UiHeaderItemList *finish()
3198 {
3199 UiHeaderItemList *head = next;
3200 next = nullptr;
3201 return head;
3202 }
3203
3204 void accept0(BaseVisitor *visitor) override;
3205
3206 SourceLocation firstSourceLocation() const override
3207 { return headerItem->firstSourceLocation(); }
3208
3209 SourceLocation lastSourceLocation() const override
3210 { return lastListElement(head: this)->headerItem->lastSourceLocation(); }
3211
3212// attributes
3213 Node *headerItem;
3214 UiHeaderItemList *next;
3215};
3216
3217class QML_PARSER_EXPORT UiProgram: public Node
3218{
3219public:
3220 QQMLJS_DECLARE_AST_NODE(UiProgram)
3221
3222 UiProgram(UiHeaderItemList *headers, UiObjectMemberList *members)
3223 : headers(headers), members(members)
3224 { kind = K; }
3225
3226 void accept0(BaseVisitor *visitor) override;
3227
3228 SourceLocation firstSourceLocation() const override
3229 {
3230 if (headers)
3231 return headers->firstSourceLocation();
3232 else if (members)
3233 return members->firstSourceLocation();
3234 return SourceLocation();
3235 }
3236
3237 SourceLocation lastSourceLocation() const override
3238 {
3239 if (members)
3240 return members->lastSourceLocation();
3241 else if (headers)
3242 return headers->lastSourceLocation();
3243 return SourceLocation();
3244 }
3245
3246// attributes
3247 UiHeaderItemList *headers;
3248 UiObjectMemberList *members;
3249};
3250
3251class QML_PARSER_EXPORT UiArrayMemberList: public Node
3252{
3253public:
3254 QQMLJS_DECLARE_AST_NODE(UiArrayMemberList)
3255
3256 UiArrayMemberList(UiObjectMember *member)
3257 : next(this), member(member)
3258 { kind = K; }
3259
3260 UiArrayMemberList(UiArrayMemberList *previous, UiObjectMember *member)
3261 : member(member)
3262 {
3263 kind = K;
3264 next = previous->next;
3265 previous->next = this;
3266 }
3267
3268 void accept0(BaseVisitor *visitor) override;
3269
3270 SourceLocation firstSourceLocation() const override
3271 { return member->firstSourceLocation(); }
3272
3273 SourceLocation lastSourceLocation() const override
3274 { return lastListElement(head: this)->member->lastSourceLocation(); }
3275
3276 UiArrayMemberList *finish()
3277 {
3278 UiArrayMemberList *head = next;
3279 next = nullptr;
3280 return head;
3281 }
3282
3283// attributes
3284 UiArrayMemberList *next;
3285 UiObjectMember *member;
3286 SourceLocation commaToken;
3287};
3288
3289class QML_PARSER_EXPORT UiObjectInitializer: public Node
3290{
3291public:
3292 QQMLJS_DECLARE_AST_NODE(UiObjectInitializer)
3293
3294 UiObjectInitializer(UiObjectMemberList *members)
3295 : members(members)
3296 { kind = K; }
3297
3298 void accept0(BaseVisitor *visitor) override;
3299
3300 SourceLocation firstSourceLocation() const override
3301 { return lbraceToken; }
3302
3303 SourceLocation lastSourceLocation() const override
3304 { return rbraceToken; }
3305
3306// attributes
3307 SourceLocation lbraceToken;
3308 UiObjectMemberList *members;
3309 SourceLocation rbraceToken;
3310};
3311
3312class QML_PARSER_EXPORT UiParameterList: public Node
3313{
3314public:
3315 QQMLJS_DECLARE_AST_NODE(UiParameterList)
3316
3317 UiParameterList(Type *t, QStringView n):
3318 type (t), name (n), next (this)
3319 { kind = K; }
3320
3321 UiParameterList(UiParameterList *previous, Type *t, QStringView n):
3322 type (t), name (n)
3323 {
3324 kind = K;
3325 next = previous->next;
3326 previous->next = this;
3327 }
3328
3329 void accept0(BaseVisitor *) override;
3330
3331 SourceLocation firstSourceLocation() const override
3332 { return colonToken.isValid() ? identifierToken : propertyTypeToken; }
3333
3334 SourceLocation lastSourceLocation() const override
3335 {
3336 auto last = lastListElement(head: this);
3337 return last->lastOwnSourceLocation();
3338 }
3339
3340 SourceLocation lastOwnSourceLocation() const
3341 {
3342 return (colonToken.isValid() ? propertyTypeToken : identifierToken);
3343 }
3344
3345 inline UiParameterList *finish ()
3346 {
3347 UiParameterList *front = next;
3348 next = nullptr;
3349 return front;
3350 }
3351
3352// attributes
3353 Type *type;
3354 QStringView name;
3355 UiParameterList *next;
3356 SourceLocation commaToken;
3357 SourceLocation propertyTypeToken;
3358 SourceLocation identifierToken;
3359 SourceLocation colonToken;
3360};
3361
3362class QML_PARSER_EXPORT UiPropertyAttributes : public Node
3363{
3364 QQMLJS_DECLARE_AST_NODE(UiPropertyAttributes)
3365public:
3366 UiPropertyAttributes() { kind = K; }
3367
3368 SourceLocation defaultToken() const { return m_defaultToken; }
3369 bool isDefaultMember() const { return defaultToken().isValid(); }
3370 SourceLocation requiredToken() const { return m_requiredToken; }
3371 bool isRequired() const { return requiredToken().isValid(); }
3372 SourceLocation readonlyToken() const { return m_readonlyToken; }
3373 bool isReadonly() const { return readonlyToken().isValid(); }
3374
3375 SourceLocation propertyToken() const { return m_propertyToken; }
3376
3377 template <bool InvalidIsLargest = true>
3378 static bool compareLocationsByBegin(const SourceLocation *& lhs, const SourceLocation *& rhs)
3379 {
3380 if (lhs->isValid() && rhs->isValid())
3381 return lhs->begin() < rhs->begin();
3382 else if (lhs->isValid())
3383 return InvalidIsLargest;
3384 else
3385 return !InvalidIsLargest;
3386 }
3387
3388 void accept0(BaseVisitor *) override {} // intentionally do nothing
3389
3390 SourceLocation firstSourceLocation() const override;
3391
3392 SourceLocation lastSourceLocation() const override;
3393
3394private:
3395 friend class QQmlJS::Parser;
3396 SourceLocation m_defaultToken;
3397 SourceLocation m_readonlyToken;
3398 SourceLocation m_requiredToken;
3399 SourceLocation m_propertyToken;
3400};
3401
3402class QML_PARSER_EXPORT UiPublicMember: public UiObjectMember
3403{
3404public:
3405 QQMLJS_DECLARE_AST_NODE(UiPublicMember)
3406
3407 UiPublicMember(UiQualifiedId *memberType,
3408 QStringView name)
3409 : type(Property), memberType(memberType), name(name), statement(nullptr), binding(nullptr), parameters(nullptr)
3410 { kind = K; }
3411
3412 UiPublicMember(UiQualifiedId *memberType,
3413 QStringView name,
3414 Statement *statement)
3415 : type(Property), memberType(memberType), name(name), statement(statement), binding(nullptr), parameters(nullptr)
3416 { kind = K; }
3417
3418 void accept0(BaseVisitor *visitor) override;
3419
3420 SourceLocation firstSourceLocation() const override
3421 {
3422 if (hasAttributes)
3423 return m_attributes->firstSourceLocation();
3424 else
3425 return m_propertyToken;
3426 }
3427
3428 SourceLocation lastSourceLocation() const override
3429 {
3430 if (binding)
3431 return binding->lastSourceLocation();
3432 if (statement)
3433 return statement->lastSourceLocation();
3434
3435 return semicolonToken;
3436 }
3437
3438 SourceLocation defaultToken() const
3439 {
3440 return hasAttributes ? m_attributes->defaultToken() : SourceLocation {};
3441 }
3442 bool isDefaultMember() const { return defaultToken().isValid(); }
3443
3444 SourceLocation requiredToken() const
3445 {
3446 return hasAttributes ? m_attributes->requiredToken() : SourceLocation {};
3447 }
3448 bool isRequired() const { return requiredToken().isValid(); }
3449
3450 SourceLocation readonlyToken() const
3451 {
3452 return hasAttributes ? m_attributes->readonlyToken() : SourceLocation {};
3453 }
3454 bool isReadonly() const { return readonlyToken().isValid(); }
3455
3456 void setAttributes(UiPropertyAttributes *attributes)
3457 {
3458 m_attributes = attributes;
3459 hasAttributes = true;
3460 }
3461
3462 SourceLocation propertyToken() const
3463 {
3464 return hasAttributes ? m_attributes->propertyToken() : m_propertyToken;
3465 }
3466
3467 void setPropertyToken(SourceLocation token)
3468 {
3469 m_propertyToken = token;
3470 hasAttributes = false;
3471 }
3472
3473// attributes
3474 enum : bool { Signal, Property } type;
3475 bool hasAttributes = false;
3476 QStringView typeModifier;
3477 UiQualifiedId *memberType;
3478 QStringView name;
3479 Statement *statement; // initialized with a JS expression
3480 UiObjectMember *binding; // initialized with a QML object or array.
3481 UiParameterList *parameters;
3482 // TODO: merge source locations
3483 SourceLocation typeModifierToken;
3484 SourceLocation typeToken;
3485 SourceLocation identifierToken;
3486 SourceLocation colonToken;
3487 SourceLocation semicolonToken;
3488private:
3489 union {
3490 SourceLocation m_propertyToken = SourceLocation {};
3491 UiPropertyAttributes *m_attributes;
3492 };
3493};
3494
3495class QML_PARSER_EXPORT UiObjectDefinition: public UiObjectMember
3496{
3497public:
3498 QQMLJS_DECLARE_AST_NODE(UiObjectDefinition)
3499
3500 UiObjectDefinition(UiQualifiedId *qualifiedTypeNameId,
3501 UiObjectInitializer *initializer)
3502 : qualifiedTypeNameId(qualifiedTypeNameId), initializer(initializer)
3503 { kind = K; }
3504
3505 void accept0(BaseVisitor *visitor) override;
3506
3507 SourceLocation firstSourceLocation() const override
3508 { return qualifiedTypeNameId->identifierToken; }
3509
3510 SourceLocation lastSourceLocation() const override
3511 { return initializer->rbraceToken; }
3512
3513// attributes
3514 UiQualifiedId *qualifiedTypeNameId;
3515 UiObjectInitializer *initializer;
3516};
3517
3518class QML_PARSER_EXPORT UiInlineComponent: public UiObjectMember
3519{
3520public:
3521 QQMLJS_DECLARE_AST_NODE(UiInlineComponent)
3522
3523 UiInlineComponent(QStringView inlineComponentName, UiObjectDefinition* inlineComponent)
3524 : name(inlineComponentName), component(inlineComponent)
3525 { kind = K; }
3526
3527 SourceLocation lastSourceLocation() const override
3528 {return component->lastSourceLocation();}
3529
3530 SourceLocation firstSourceLocation() const override
3531 {return componentToken;}
3532
3533 void accept0(BaseVisitor *visitor) override;
3534
3535 // attributes
3536 QStringView name;
3537 UiObjectDefinition* component;
3538 SourceLocation componentToken;
3539};
3540
3541class QML_PARSER_EXPORT UiSourceElement: public UiObjectMember
3542{
3543public:
3544 QQMLJS_DECLARE_AST_NODE(UiSourceElement)
3545
3546 UiSourceElement(Node *sourceElement)
3547 : sourceElement(sourceElement)
3548 { kind = K; }
3549
3550 SourceLocation firstSourceLocation() const override
3551 {
3552 if (FunctionExpression *funDecl = sourceElement->asFunctionDefinition())
3553 return funDecl->firstSourceLocation();
3554 else if (VariableStatement *varStmt = cast<VariableStatement *>(ast: sourceElement))
3555 return varStmt->firstSourceLocation();
3556
3557 return SourceLocation();
3558 }
3559
3560 SourceLocation lastSourceLocation() const override
3561 {
3562 if (FunctionExpression *funDecl = sourceElement->asFunctionDefinition())
3563 return funDecl->lastSourceLocation();
3564 else if (VariableStatement *varStmt = cast<VariableStatement *>(ast: sourceElement))
3565 return varStmt->lastSourceLocation();
3566
3567 return SourceLocation();
3568 }
3569
3570 void accept0(BaseVisitor *visitor) override;
3571
3572
3573// attributes
3574 Node *sourceElement;
3575};
3576
3577class QML_PARSER_EXPORT UiObjectBinding: public UiObjectMember
3578{
3579public:
3580 QQMLJS_DECLARE_AST_NODE(UiObjectBinding)
3581
3582 UiObjectBinding(UiQualifiedId *qualifiedId,
3583 UiQualifiedId *qualifiedTypeNameId,
3584 UiObjectInitializer *initializer)
3585 : qualifiedId(qualifiedId),
3586 qualifiedTypeNameId(qualifiedTypeNameId),
3587 initializer(initializer),
3588 hasOnToken(false)
3589 { kind = K; }
3590
3591 SourceLocation firstSourceLocation() const override
3592 {
3593 if (hasOnToken && qualifiedTypeNameId)
3594 return qualifiedTypeNameId->identifierToken;
3595
3596 return qualifiedId->identifierToken;
3597 }
3598
3599 SourceLocation lastSourceLocation() const override
3600 { return initializer->rbraceToken; }
3601
3602 void accept0(BaseVisitor *visitor) override;
3603
3604
3605// attributes
3606 UiQualifiedId *qualifiedId;
3607 UiQualifiedId *qualifiedTypeNameId;
3608 UiObjectInitializer *initializer;
3609 SourceLocation colonToken;
3610 bool hasOnToken;
3611};
3612
3613class QML_PARSER_EXPORT UiScriptBinding: public UiObjectMember
3614{
3615public:
3616 QQMLJS_DECLARE_AST_NODE(UiScriptBinding)
3617
3618 UiScriptBinding(UiQualifiedId *qualifiedId,
3619 Statement *statement)
3620 : qualifiedId(qualifiedId),
3621 statement(statement)
3622 { kind = K; }
3623
3624 SourceLocation firstSourceLocation() const override
3625 { return qualifiedId->identifierToken; }
3626
3627 SourceLocation lastSourceLocation() const override
3628 { return statement->lastSourceLocation(); }
3629
3630 void accept0(BaseVisitor *visitor) override;
3631
3632// attributes
3633 UiQualifiedId *qualifiedId;
3634 Statement *statement;
3635 SourceLocation colonToken;
3636};
3637
3638class QML_PARSER_EXPORT UiArrayBinding: public UiObjectMember
3639{
3640public:
3641 QQMLJS_DECLARE_AST_NODE(UiArrayBinding)
3642
3643 UiArrayBinding(UiQualifiedId *qualifiedId,
3644 UiArrayMemberList *members)
3645 : qualifiedId(qualifiedId),
3646 members(members)
3647 { kind = K; }
3648
3649 SourceLocation firstSourceLocation() const override
3650 { Q_ASSERT(qualifiedId); return qualifiedId->identifierToken; }
3651
3652 SourceLocation lastSourceLocation() const override
3653 { return rbracketToken; }
3654
3655 void accept0(BaseVisitor *visitor) override;
3656
3657// attributes
3658 UiQualifiedId *qualifiedId;
3659 UiArrayMemberList *members;
3660 SourceLocation colonToken;
3661 SourceLocation lbracketToken;
3662 SourceLocation rbracketToken;
3663};
3664
3665class QML_PARSER_EXPORT UiEnumMemberList: public Node
3666{
3667 QQMLJS_DECLARE_AST_NODE(UiEnumMemberList)
3668public:
3669 UiEnumMemberList(QStringView member, double v = 0.0)
3670 : next(this), member(member), value(v)
3671 { kind = K; }
3672
3673 UiEnumMemberList(UiEnumMemberList *previous, QStringView member)
3674 : member(member)
3675 {
3676 kind = K;
3677 next = previous->next;
3678 previous->next = this;
3679 value = previous->value + 1;
3680 }
3681
3682 UiEnumMemberList(UiEnumMemberList *previous, QStringView member, double v)
3683 : member(member), value(v)
3684 {
3685 kind = K;
3686 next = previous->next;
3687 previous->next = this;
3688 }
3689
3690 SourceLocation firstSourceLocation() const override
3691 { return memberToken; }
3692
3693 SourceLocation lastSourceLocation() const override
3694 {
3695 auto last = lastListElement(head: this);
3696 return last->valueToken.isValid() ? last->valueToken : last->memberToken;
3697 }
3698
3699 void accept0(BaseVisitor *visitor) override;
3700
3701 UiEnumMemberList *finish()
3702 {
3703 UiEnumMemberList *head = next;
3704 next = nullptr;
3705 return head;
3706 }
3707
3708// attributes
3709 UiEnumMemberList *next;
3710 QStringView member;
3711 double value;
3712 SourceLocation memberToken;
3713 SourceLocation valueToken;
3714};
3715
3716class QML_PARSER_EXPORT UiEnumDeclaration: public UiObjectMember
3717{
3718public:
3719 QQMLJS_DECLARE_AST_NODE(UiEnumDeclaration)
3720
3721 UiEnumDeclaration(QStringView name,
3722 UiEnumMemberList *members)
3723 : name(name)
3724 , members(members)
3725 { kind = K; }
3726
3727 SourceLocation firstSourceLocation() const override
3728 { return enumToken; }
3729
3730 SourceLocation lastSourceLocation() const override
3731 { return rbraceToken; }
3732
3733 void accept0(BaseVisitor *visitor) override;
3734
3735// attributes
3736 SourceLocation enumToken;
3737 SourceLocation identifierToken;
3738 SourceLocation lbraceToken;
3739 SourceLocation rbraceToken;
3740 QStringView name;
3741 UiEnumMemberList *members;
3742};
3743
3744class QML_PARSER_EXPORT UiAnnotation: public Node
3745{
3746public:
3747 QQMLJS_DECLARE_AST_NODE(UiAnnotation)
3748
3749 UiAnnotation(UiQualifiedId *qualifiedTypeNameId,
3750 UiObjectInitializer *initializer)
3751 : qualifiedTypeNameId(qualifiedTypeNameId), initializer(initializer)
3752 { kind = K; }
3753
3754 void accept0(BaseVisitor *visitor) override;
3755
3756 SourceLocation firstSourceLocation() const override
3757 { return qualifiedTypeNameId->identifierToken; }
3758
3759 SourceLocation lastSourceLocation() const override
3760 { return initializer->rbraceToken; }
3761
3762// attributes
3763 UiQualifiedId *qualifiedTypeNameId;
3764 UiObjectInitializer *initializer;
3765};
3766
3767class QML_PARSER_EXPORT UiAnnotationList: public Node
3768{
3769public:
3770 QQMLJS_DECLARE_AST_NODE(UiAnnotationList)
3771
3772 UiAnnotationList(UiAnnotation *annotation)
3773 : next(this), annotation(annotation)
3774 { kind = K; }
3775
3776 UiAnnotationList(UiAnnotationList *previous, UiAnnotation *annotation)
3777 : annotation(annotation)
3778 {
3779 kind = K;
3780 next = previous->next;
3781 previous->next = this;
3782 }
3783
3784 void accept0(BaseVisitor *visitor) override;
3785
3786 SourceLocation firstSourceLocation() const override
3787 { return annotation->firstSourceLocation(); }
3788
3789 SourceLocation lastSourceLocation() const override
3790 { return lastListElement(head: this)->annotation->lastSourceLocation(); }
3791
3792 UiAnnotationList *finish()
3793 {
3794 UiAnnotationList *head = next;
3795 next = nullptr;
3796 return head;
3797 }
3798
3799// attributes
3800 UiAnnotationList *next;
3801 UiAnnotation *annotation;
3802};
3803
3804} } // namespace AST
3805
3806
3807QT_END_NAMESPACE
3808
3809#endif
3810

source code of qtdeclarative/src/qml/parser/qqmljsast_p.h