1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_TORQUE_AST_H_
6 #define V8_TORQUE_AST_H_
7
8 #include <algorithm>
9 #include <iostream>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15
16 #include "src/base/optional.h"
17 #include "src/numbers/integer-literal.h"
18 #include "src/torque/constants.h"
19 #include "src/torque/source-positions.h"
20 #include "src/torque/utils.h"
21
22 namespace v8 {
23 namespace internal {
24 namespace torque {
25
26 #define AST_EXPRESSION_NODE_KIND_LIST(V) \
27 V(CallExpression) \
28 V(CallMethodExpression) \
29 V(IntrinsicCallExpression) \
30 V(StructExpression) \
31 V(LogicalOrExpression) \
32 V(LogicalAndExpression) \
33 V(SpreadExpression) \
34 V(ConditionalExpression) \
35 V(IdentifierExpression) \
36 V(StringLiteralExpression) \
37 V(IntegerLiteralExpression) \
38 V(FloatingPointLiteralExpression) \
39 V(FieldAccessExpression) \
40 V(ElementAccessExpression) \
41 V(DereferenceExpression) \
42 V(AssignmentExpression) \
43 V(IncrementDecrementExpression) \
44 V(NewExpression) \
45 V(AssumeTypeImpossibleExpression) \
46 V(StatementExpression) \
47 V(TryLabelExpression)
48
49 #define AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
50 V(BasicTypeExpression) \
51 V(FunctionTypeExpression) \
52 V(PrecomputedTypeExpression) \
53 V(UnionTypeExpression)
54
55 #define AST_STATEMENT_NODE_KIND_LIST(V) \
56 V(BlockStatement) \
57 V(ExpressionStatement) \
58 V(IfStatement) \
59 V(WhileStatement) \
60 V(ForLoopStatement) \
61 V(BreakStatement) \
62 V(ContinueStatement) \
63 V(ReturnStatement) \
64 V(DebugStatement) \
65 V(AssertStatement) \
66 V(TailCallStatement) \
67 V(VarDeclarationStatement) \
68 V(GotoStatement)
69
70 #define AST_TYPE_DECLARATION_NODE_KIND_LIST(V) \
71 V(AbstractTypeDeclaration) \
72 V(TypeAliasDeclaration) \
73 V(BitFieldStructDeclaration) \
74 V(ClassDeclaration) \
75 V(StructDeclaration)
76
77 #define AST_DECLARATION_NODE_KIND_LIST(V) \
78 AST_TYPE_DECLARATION_NODE_KIND_LIST(V) \
79 V(GenericCallableDeclaration) \
80 V(GenericTypeDeclaration) \
81 V(SpecializationDeclaration) \
82 V(ExternConstDeclaration) \
83 V(NamespaceDeclaration) \
84 V(ConstDeclaration) \
85 V(CppIncludeDeclaration) \
86 V(TorqueMacroDeclaration) \
87 V(TorqueBuiltinDeclaration) \
88 V(ExternalMacroDeclaration) \
89 V(ExternalBuiltinDeclaration) \
90 V(ExternalRuntimeDeclaration) \
91 V(IntrinsicDeclaration)
92
93 #define AST_NODE_KIND_LIST(V) \
94 AST_EXPRESSION_NODE_KIND_LIST(V) \
95 AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
96 AST_STATEMENT_NODE_KIND_LIST(V) \
97 AST_DECLARATION_NODE_KIND_LIST(V) \
98 V(Identifier) \
99 V(TryHandler) \
100 V(ClassBody)
101
102 struct AstNode {
103 public:
104 enum class Kind {
105 #define ENUM_ITEM(name) k##name,
106 AST_NODE_KIND_LIST(ENUM_ITEM)
107 #undef ENUM_ITEM
108 };
109
AstNodeAstNode110 AstNode(Kind kind, SourcePosition pos) : kind(kind), pos(pos) {}
111 virtual ~AstNode() = default;
112
113 const Kind kind;
114 SourcePosition pos;
115 };
116
117 struct AstNodeClassCheck {
118 template <class T>
119 static bool IsInstanceOf(AstNode* node);
120 };
121
122 // Boilerplate for most derived classes.
123 #define DEFINE_AST_NODE_LEAF_BOILERPLATE(T) \
124 static const Kind kKind = Kind::k##T; \
125 static T* cast(AstNode* node) { \
126 DCHECK_EQ(node->kind, kKind); \
127 return static_cast<T*>(node); \
128 } \
129 static T* DynamicCast(AstNode* node) { \
130 if (!node) return nullptr; \
131 if (node->kind != kKind) return nullptr; \
132 return static_cast<T*>(node); \
133 }
134
135 // Boilerplate for classes with subclasses.
136 #define DEFINE_AST_NODE_INNER_BOILERPLATE(T) \
137 static T* cast(AstNode* node) { \
138 DCHECK(AstNodeClassCheck::IsInstanceOf<T>(node)); \
139 return static_cast<T*>(node); \
140 } \
141 static T* DynamicCast(AstNode* node) { \
142 if (!node) return nullptr; \
143 if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
144 return static_cast<T*>(node); \
145 }
146
147 struct Expression : AstNode {
ExpressionExpression148 Expression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
149 DEFINE_AST_NODE_INNER_BOILERPLATE(Expression)
150
151 using VisitCallback = std::function<void(Expression*)>;
VisitAllSubExpressionsExpression152 virtual void VisitAllSubExpressions(VisitCallback callback) {
153 // TODO(szuend): Hoist this up to AstNode and make it a
154 // general Ast visitor.
155 }
156 };
157
158 struct LocationExpression : Expression {
LocationExpressionLocationExpression159 LocationExpression(Kind kind, SourcePosition pos) : Expression(kind, pos) {}
160 DEFINE_AST_NODE_INNER_BOILERPLATE(LocationExpression)
161 };
162
163 struct TypeExpression : AstNode {
TypeExpressionTypeExpression164 TypeExpression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
165 DEFINE_AST_NODE_INNER_BOILERPLATE(TypeExpression)
166 };
167
168 struct Declaration : AstNode {
DeclarationDeclaration169 Declaration(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
170 DEFINE_AST_NODE_INNER_BOILERPLATE(Declaration)
171 };
172
173 struct Statement : AstNode {
StatementStatement174 Statement(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
175 DEFINE_AST_NODE_INNER_BOILERPLATE(Statement)
176 };
177
178 class Namespace;
179
180 struct NamespaceDeclaration : Declaration {
181 DEFINE_AST_NODE_LEAF_BOILERPLATE(NamespaceDeclaration)
NamespaceDeclarationNamespaceDeclaration182 NamespaceDeclaration(SourcePosition pos, std::string name,
183 std::vector<Declaration*> declarations)
184 : Declaration(kKind, pos),
185 declarations(std::move(declarations)),
186 name(name) {}
187 std::vector<Declaration*> declarations;
188 std::string name;
189 };
190
191 struct EnumDescription {
192 SourcePosition pos;
193 std::string name;
194 std::string constexpr_generates;
195 bool is_open;
196 std::vector<std::string> entries;
197
198 EnumDescription(SourcePosition pos, std::string name,
199 std::string constexpr_generates, bool is_open,
200 std::vector<std::string> entries = {})
posEnumDescription201 : pos(std::move(pos)),
202 name(std::move(name)),
203 constexpr_generates(std::move(constexpr_generates)),
204 is_open(is_open),
205 entries(std::move(entries)) {}
206 };
207
208 class Ast {
209 public:
210 Ast() = default;
211
declarations()212 std::vector<Declaration*>& declarations() { return declarations_; }
declarations()213 const std::vector<Declaration*>& declarations() const {
214 return declarations_;
215 }
216 template <class T>
AddNode(std::unique_ptr<T> node)217 T* AddNode(std::unique_ptr<T> node) {
218 T* result = node.get();
219 nodes_.push_back(std::move(node));
220 return result;
221 }
222
DeclareImportForCurrentFile(SourceId import_id)223 void DeclareImportForCurrentFile(SourceId import_id) {
224 declared_imports_[CurrentSourcePosition::Get().source].insert(import_id);
225 }
226
AddEnumDescription(EnumDescription description)227 void AddEnumDescription(EnumDescription description) {
228 std::string name = description.name;
229 DCHECK(!name.empty());
230 auto f = [&](const auto& d) { return d.name == name; };
231 USE(f); // Suppress unused in release.
232 DCHECK_EQ(
233 std::find_if(enum_descriptions_.begin(), enum_descriptions_.end(), f),
234 enum_descriptions_.end());
235 enum_descriptions_.push_back(std::move(description));
236 }
237
EnumDescriptions()238 std::vector<EnumDescription>& EnumDescriptions() {
239 return enum_descriptions_;
240 }
241
242 private:
243 std::vector<Declaration*> declarations_;
244 std::vector<std::unique_ptr<AstNode>> nodes_;
245 std::map<SourceId, std::set<SourceId>> declared_imports_;
246 std::vector<EnumDescription> enum_descriptions_;
247 };
248
249 static const char* const kThisParameterName = "this";
250
251 // A Identifier is a string with a SourcePosition attached.
252 struct Identifier : AstNode {
253 DEFINE_AST_NODE_LEAF_BOILERPLATE(Identifier)
IdentifierIdentifier254 Identifier(SourcePosition pos, std::string identifier)
255 : AstNode(kKind, pos), value(std::move(identifier)) {}
256 std::string value;
257 };
258
259 inline std::ostream& operator<<(std::ostream& os, Identifier* id) {
260 return os << id->value;
261 }
262
263 struct IdentifierPtrValueEq {
operatorIdentifierPtrValueEq264 bool operator()(const Identifier* a, const Identifier* b) {
265 return a->value < b->value;
266 }
267 };
268
269 struct IdentifierExpression : LocationExpression {
270 DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
271 IdentifierExpression(SourcePosition pos,
272 std::vector<std::string> namespace_qualification,
273 Identifier* name, std::vector<TypeExpression*> args = {})
LocationExpressionIdentifierExpression274 : LocationExpression(kKind, pos),
275 namespace_qualification(std::move(namespace_qualification)),
276 name(name),
277 generic_arguments(std::move(args)) {}
278 IdentifierExpression(SourcePosition pos, Identifier* name,
279 std::vector<TypeExpression*> args = {})
280 : IdentifierExpression(pos, {}, name, std::move(args)) {}
IsThisIdentifierExpression281 bool IsThis() const { return name->value == kThisParameterName; }
282
VisitAllSubExpressionsIdentifierExpression283 void VisitAllSubExpressions(VisitCallback callback) override {
284 callback(this);
285 }
286
287 std::vector<std::string> namespace_qualification;
288 Identifier* name;
289 std::vector<TypeExpression*> generic_arguments;
290 };
291
292 struct IntrinsicCallExpression : Expression {
293 DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicCallExpression)
IntrinsicCallExpressionIntrinsicCallExpression294 IntrinsicCallExpression(SourcePosition pos, Identifier* name,
295 std::vector<TypeExpression*> generic_arguments,
296 std::vector<Expression*> arguments)
297 : Expression(kKind, pos),
298 name(name),
299 generic_arguments(std::move(generic_arguments)),
300 arguments(std::move(arguments)) {}
301
VisitAllSubExpressionsIntrinsicCallExpression302 void VisitAllSubExpressions(VisitCallback callback) override {
303 for (auto argument : arguments) {
304 argument->VisitAllSubExpressions(callback);
305 }
306 callback(this);
307 }
308
309 Identifier* name;
310 std::vector<TypeExpression*> generic_arguments;
311 std::vector<Expression*> arguments;
312 };
313
314 struct CallMethodExpression : Expression {
315 DEFINE_AST_NODE_LEAF_BOILERPLATE(CallMethodExpression)
CallMethodExpressionCallMethodExpression316 CallMethodExpression(SourcePosition pos, Expression* target,
317 IdentifierExpression* method,
318 std::vector<Expression*> arguments,
319 std::vector<Identifier*> labels)
320 : Expression(kKind, pos),
321 target(target),
322 method(method),
323 arguments(std::move(arguments)),
324 labels(std::move(labels)) {}
325
VisitAllSubExpressionsCallMethodExpression326 void VisitAllSubExpressions(VisitCallback callback) override {
327 target->VisitAllSubExpressions(callback);
328 method->VisitAllSubExpressions(callback);
329 for (auto argument : arguments) {
330 argument->VisitAllSubExpressions(callback);
331 }
332 callback(this);
333 }
334
335 Expression* target;
336 IdentifierExpression* method;
337 std::vector<Expression*> arguments;
338 std::vector<Identifier*> labels;
339 };
340
341 struct CallExpression : Expression {
342 DEFINE_AST_NODE_LEAF_BOILERPLATE(CallExpression)
CallExpressionCallExpression343 CallExpression(SourcePosition pos, IdentifierExpression* callee,
344 std::vector<Expression*> arguments,
345 std::vector<Identifier*> labels)
346 : Expression(kKind, pos),
347 callee(callee),
348 arguments(std::move(arguments)),
349 labels(std::move(labels)) {}
350
VisitAllSubExpressionsCallExpression351 void VisitAllSubExpressions(VisitCallback callback) override {
352 callee->VisitAllSubExpressions(callback);
353 for (auto argument : arguments) {
354 argument->VisitAllSubExpressions(callback);
355 }
356 callback(this);
357 }
358
359 IdentifierExpression* callee;
360 std::vector<Expression*> arguments;
361 std::vector<Identifier*> labels;
362 };
363
364 struct NameAndExpression {
365 Identifier* name;
366 Expression* expression;
367 };
368
369 struct StructExpression : Expression {
370 DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
StructExpressionStructExpression371 StructExpression(SourcePosition pos, TypeExpression* type,
372 std::vector<NameAndExpression> initializers)
373 : Expression(kKind, pos),
374 type(type),
375 initializers(std::move(initializers)) {}
376
VisitAllSubExpressionsStructExpression377 void VisitAllSubExpressions(VisitCallback callback) override {
378 for (auto& initializer : initializers) {
379 initializer.expression->VisitAllSubExpressions(callback);
380 }
381 callback(this);
382 }
383
384 TypeExpression* type;
385 std::vector<NameAndExpression> initializers;
386 };
387
388 struct LogicalOrExpression : Expression {
389 DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalOrExpression)
LogicalOrExpressionLogicalOrExpression390 LogicalOrExpression(SourcePosition pos, Expression* left, Expression* right)
391 : Expression(kKind, pos), left(left), right(right) {}
392
VisitAllSubExpressionsLogicalOrExpression393 void VisitAllSubExpressions(VisitCallback callback) override {
394 left->VisitAllSubExpressions(callback);
395 right->VisitAllSubExpressions(callback);
396 callback(this);
397 }
398
399 Expression* left;
400 Expression* right;
401 };
402
403 struct LogicalAndExpression : Expression {
404 DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalAndExpression)
LogicalAndExpressionLogicalAndExpression405 LogicalAndExpression(SourcePosition pos, Expression* left, Expression* right)
406 : Expression(kKind, pos), left(left), right(right) {}
407
VisitAllSubExpressionsLogicalAndExpression408 void VisitAllSubExpressions(VisitCallback callback) override {
409 left->VisitAllSubExpressions(callback);
410 right->VisitAllSubExpressions(callback);
411 callback(this);
412 }
413
414 Expression* left;
415 Expression* right;
416 };
417
418 struct SpreadExpression : Expression {
419 DEFINE_AST_NODE_LEAF_BOILERPLATE(SpreadExpression)
SpreadExpressionSpreadExpression420 SpreadExpression(SourcePosition pos, Expression* spreadee)
421 : Expression(kKind, pos), spreadee(spreadee) {}
422
VisitAllSubExpressionsSpreadExpression423 void VisitAllSubExpressions(VisitCallback callback) override {
424 spreadee->VisitAllSubExpressions(callback);
425 callback(this);
426 }
427
428 Expression* spreadee;
429 };
430
431 struct ConditionalExpression : Expression {
432 DEFINE_AST_NODE_LEAF_BOILERPLATE(ConditionalExpression)
ConditionalExpressionConditionalExpression433 ConditionalExpression(SourcePosition pos, Expression* condition,
434 Expression* if_true, Expression* if_false)
435 : Expression(kKind, pos),
436 condition(condition),
437 if_true(if_true),
438 if_false(if_false) {}
439
VisitAllSubExpressionsConditionalExpression440 void VisitAllSubExpressions(VisitCallback callback) override {
441 condition->VisitAllSubExpressions(callback);
442 if_true->VisitAllSubExpressions(callback);
443 if_false->VisitAllSubExpressions(callback);
444 callback(this);
445 }
446
447 Expression* condition;
448 Expression* if_true;
449 Expression* if_false;
450 };
451
452 struct StringLiteralExpression : Expression {
453 DEFINE_AST_NODE_LEAF_BOILERPLATE(StringLiteralExpression)
StringLiteralExpressionStringLiteralExpression454 StringLiteralExpression(SourcePosition pos, std::string literal)
455 : Expression(kKind, pos), literal(std::move(literal)) {}
456
VisitAllSubExpressionsStringLiteralExpression457 void VisitAllSubExpressions(VisitCallback callback) override {
458 callback(this);
459 }
460
461 std::string literal;
462 };
463
464 struct IntegerLiteralExpression : Expression {
465 DEFINE_AST_NODE_LEAF_BOILERPLATE(IntegerLiteralExpression)
IntegerLiteralExpressionIntegerLiteralExpression466 IntegerLiteralExpression(SourcePosition pos, IntegerLiteral value)
467 : Expression(kKind, pos), value(std::move(value)) {}
468
VisitAllSubExpressionsIntegerLiteralExpression469 void VisitAllSubExpressions(VisitCallback callback) override {
470 callback(this);
471 }
472
473 IntegerLiteral value;
474 };
475
476 struct FloatingPointLiteralExpression : Expression {
477 DEFINE_AST_NODE_LEAF_BOILERPLATE(FloatingPointLiteralExpression)
FloatingPointLiteralExpressionFloatingPointLiteralExpression478 FloatingPointLiteralExpression(SourcePosition pos, double value)
479 : Expression(kKind, pos), value(value) {}
480
VisitAllSubExpressionsFloatingPointLiteralExpression481 void VisitAllSubExpressions(VisitCallback callback) override {
482 callback(this);
483 }
484
485 double value;
486 };
487
488 struct ElementAccessExpression : LocationExpression {
489 DEFINE_AST_NODE_LEAF_BOILERPLATE(ElementAccessExpression)
ElementAccessExpressionElementAccessExpression490 ElementAccessExpression(SourcePosition pos, Expression* array,
491 Expression* index)
492 : LocationExpression(kKind, pos), array(array), index(index) {}
493
VisitAllSubExpressionsElementAccessExpression494 void VisitAllSubExpressions(VisitCallback callback) override {
495 array->VisitAllSubExpressions(callback);
496 index->VisitAllSubExpressions(callback);
497 callback(this);
498 }
499
500 Expression* array;
501 Expression* index;
502 };
503
504 struct FieldAccessExpression : LocationExpression {
505 DEFINE_AST_NODE_LEAF_BOILERPLATE(FieldAccessExpression)
FieldAccessExpressionFieldAccessExpression506 FieldAccessExpression(SourcePosition pos, Expression* object,
507 Identifier* field)
508 : LocationExpression(kKind, pos), object(object), field(field) {}
509
VisitAllSubExpressionsFieldAccessExpression510 void VisitAllSubExpressions(VisitCallback callback) override {
511 object->VisitAllSubExpressions(callback);
512 callback(this);
513 }
514
515 Expression* object;
516 Identifier* field;
517 };
518
519 struct DereferenceExpression : LocationExpression {
520 DEFINE_AST_NODE_LEAF_BOILERPLATE(DereferenceExpression)
DereferenceExpressionDereferenceExpression521 DereferenceExpression(SourcePosition pos, Expression* reference)
522 : LocationExpression(kKind, pos), reference(reference) {}
523
VisitAllSubExpressionsDereferenceExpression524 void VisitAllSubExpressions(VisitCallback callback) override {
525 reference->VisitAllSubExpressions(callback);
526 callback(this);
527 }
528
529 Expression* reference;
530 };
531
532 struct AssignmentExpression : Expression {
533 DEFINE_AST_NODE_LEAF_BOILERPLATE(AssignmentExpression)
AssignmentExpressionAssignmentExpression534 AssignmentExpression(SourcePosition pos, Expression* location,
535 Expression* value)
536 : AssignmentExpression(pos, location, base::nullopt, value) {}
AssignmentExpressionAssignmentExpression537 AssignmentExpression(SourcePosition pos, Expression* location,
538 base::Optional<std::string> op, Expression* value)
539 : Expression(kKind, pos),
540 location(location),
541 op(std::move(op)),
542 value(value) {}
543
VisitAllSubExpressionsAssignmentExpression544 void VisitAllSubExpressions(VisitCallback callback) override {
545 location->VisitAllSubExpressions(callback);
546 value->VisitAllSubExpressions(callback);
547 callback(this);
548 }
549
550 Expression* location;
551 base::Optional<std::string> op;
552 Expression* value;
553 };
554
555 enum class IncrementDecrementOperator { kIncrement, kDecrement };
556
557 struct IncrementDecrementExpression : Expression {
558 DEFINE_AST_NODE_LEAF_BOILERPLATE(IncrementDecrementExpression)
IncrementDecrementExpressionIncrementDecrementExpression559 IncrementDecrementExpression(SourcePosition pos, Expression* location,
560 IncrementDecrementOperator op, bool postfix)
561 : Expression(kKind, pos), location(location), op(op), postfix(postfix) {}
562
VisitAllSubExpressionsIncrementDecrementExpression563 void VisitAllSubExpressions(VisitCallback callback) override {
564 location->VisitAllSubExpressions(callback);
565 callback(this);
566 }
567
568 Expression* location;
569 IncrementDecrementOperator op;
570 bool postfix;
571 };
572
573 // This expression is only used in the desugaring of typeswitch, and it allows
574 // to bake in the static information that certain types are impossible at a
575 // certain position in the control flow.
576 // The result type is the type of {expression} minus the provided type.
577 struct AssumeTypeImpossibleExpression : Expression {
578 DEFINE_AST_NODE_LEAF_BOILERPLATE(AssumeTypeImpossibleExpression)
AssumeTypeImpossibleExpressionAssumeTypeImpossibleExpression579 AssumeTypeImpossibleExpression(SourcePosition pos,
580 TypeExpression* excluded_type,
581 Expression* expression)
582 : Expression(kKind, pos),
583 excluded_type(excluded_type),
584 expression(expression) {}
585
VisitAllSubExpressionsAssumeTypeImpossibleExpression586 void VisitAllSubExpressions(VisitCallback callback) override {
587 expression->VisitAllSubExpressions(callback);
588 callback(this);
589 }
590
591 TypeExpression* excluded_type;
592 Expression* expression;
593 };
594
595 struct NewExpression : Expression {
596 DEFINE_AST_NODE_LEAF_BOILERPLATE(NewExpression)
NewExpressionNewExpression597 NewExpression(SourcePosition pos, TypeExpression* type,
598 std::vector<NameAndExpression> initializers, bool pretenured)
599 : Expression(kKind, pos),
600 type(type),
601 initializers(std::move(initializers)),
602 pretenured(pretenured) {}
603
VisitAllSubExpressionsNewExpression604 void VisitAllSubExpressions(VisitCallback callback) override {
605 for (auto& initializer : initializers) {
606 initializer.expression->VisitAllSubExpressions(callback);
607 }
608 callback(this);
609 }
610
611 TypeExpression* type;
612 std::vector<NameAndExpression> initializers;
613 bool pretenured;
614 };
615
616 enum class ImplicitKind { kNoImplicit, kJSImplicit, kImplicit };
617
618 struct ParameterList {
619 std::vector<Identifier*> names;
620 std::vector<TypeExpression*> types;
621 ImplicitKind implicit_kind = ImplicitKind::kNoImplicit;
622 SourcePosition implicit_kind_pos = SourcePosition::Invalid();
623 size_t implicit_count = 0;
624 bool has_varargs = false;
625 std::string arguments_variable = "";
626
EmptyParameterList627 static ParameterList Empty() { return {}; }
GetImplicitTypesParameterList628 std::vector<TypeExpression*> GetImplicitTypes() {
629 return std::vector<TypeExpression*>(types.begin(),
630 types.begin() + implicit_count);
631 }
GetExplicitTypesParameterList632 std::vector<TypeExpression*> GetExplicitTypes() {
633 return std::vector<TypeExpression*>(types.begin() + implicit_count,
634 types.end());
635 }
636 };
637
638 struct BasicTypeExpression : TypeExpression {
639 DEFINE_AST_NODE_LEAF_BOILERPLATE(BasicTypeExpression)
BasicTypeExpressionBasicTypeExpression640 BasicTypeExpression(SourcePosition pos,
641 std::vector<std::string> namespace_qualification,
642 Identifier* name,
643 std::vector<TypeExpression*> generic_arguments)
644 : TypeExpression(kKind, pos),
645 namespace_qualification(std::move(namespace_qualification)),
646 is_constexpr(IsConstexprName(name->value)),
647 name(name),
648 generic_arguments(std::move(generic_arguments)) {}
BasicTypeExpressionBasicTypeExpression649 BasicTypeExpression(SourcePosition pos, Identifier* name)
650 : BasicTypeExpression(pos, {}, name, {}) {}
651 std::vector<std::string> namespace_qualification;
652 bool is_constexpr;
653 Identifier* name;
654 std::vector<TypeExpression*> generic_arguments;
655 };
656
657 struct FunctionTypeExpression : TypeExpression {
658 DEFINE_AST_NODE_LEAF_BOILERPLATE(FunctionTypeExpression)
FunctionTypeExpressionFunctionTypeExpression659 FunctionTypeExpression(SourcePosition pos,
660 std::vector<TypeExpression*> parameters,
661 TypeExpression* return_type)
662 : TypeExpression(kKind, pos),
663 parameters(std::move(parameters)),
664 return_type(return_type) {}
665 std::vector<TypeExpression*> parameters;
666 TypeExpression* return_type;
667 };
668
669 // A PrecomputedTypeExpression is never created directly by the parser. Later
670 // stages can use this to insert AST snippets where the type has already been
671 // resolved.
672 class Type;
673 struct PrecomputedTypeExpression : TypeExpression {
674 DEFINE_AST_NODE_LEAF_BOILERPLATE(PrecomputedTypeExpression)
PrecomputedTypeExpressionPrecomputedTypeExpression675 PrecomputedTypeExpression(SourcePosition pos, const Type* type)
676 : TypeExpression(kKind, pos), type(type) {}
677 const Type* type;
678 };
679
680 struct UnionTypeExpression : TypeExpression {
681 DEFINE_AST_NODE_LEAF_BOILERPLATE(UnionTypeExpression)
UnionTypeExpressionUnionTypeExpression682 UnionTypeExpression(SourcePosition pos, TypeExpression* a, TypeExpression* b)
683 : TypeExpression(kKind, pos), a(a), b(b) {}
684 TypeExpression* a;
685 TypeExpression* b;
686 };
687
688 struct ExpressionStatement : Statement {
689 DEFINE_AST_NODE_LEAF_BOILERPLATE(ExpressionStatement)
ExpressionStatementExpressionStatement690 ExpressionStatement(SourcePosition pos, Expression* expression)
691 : Statement(kKind, pos), expression(expression) {}
692 Expression* expression;
693 };
694
695 struct IfStatement : Statement {
696 DEFINE_AST_NODE_LEAF_BOILERPLATE(IfStatement)
IfStatementIfStatement697 IfStatement(SourcePosition pos, bool is_constexpr, Expression* condition,
698 Statement* if_true, base::Optional<Statement*> if_false)
699 : Statement(kKind, pos),
700 condition(condition),
701 is_constexpr(is_constexpr),
702 if_true(if_true),
703 if_false(if_false) {}
704 Expression* condition;
705 bool is_constexpr;
706 Statement* if_true;
707 base::Optional<Statement*> if_false;
708 };
709
710 struct WhileStatement : Statement {
711 DEFINE_AST_NODE_LEAF_BOILERPLATE(WhileStatement)
WhileStatementWhileStatement712 WhileStatement(SourcePosition pos, Expression* condition, Statement* body)
713 : Statement(kKind, pos), condition(condition), body(body) {}
714 Expression* condition;
715 Statement* body;
716 };
717
718 struct ReturnStatement : Statement {
719 DEFINE_AST_NODE_LEAF_BOILERPLATE(ReturnStatement)
ReturnStatementReturnStatement720 ReturnStatement(SourcePosition pos, base::Optional<Expression*> value)
721 : Statement(kKind, pos), value(value) {}
722 base::Optional<Expression*> value;
723 };
724
725 struct DebugStatement : Statement {
726 DEFINE_AST_NODE_LEAF_BOILERPLATE(DebugStatement)
DebugStatementDebugStatement727 DebugStatement(SourcePosition pos, const std::string& reason,
728 bool never_continues)
729 : Statement(kKind, pos),
730 reason(reason),
731 never_continues(never_continues) {}
732 std::string reason;
733 bool never_continues;
734 };
735
736 struct AssertStatement : Statement {
737 DEFINE_AST_NODE_LEAF_BOILERPLATE(AssertStatement)
738 enum class AssertKind { kDcheck, kCheck, kStaticAssert };
AssertStatementAssertStatement739 AssertStatement(SourcePosition pos, AssertKind kind, Expression* expression,
740 std::string source)
741 : Statement(kKind, pos),
742 kind(kind),
743 expression(expression),
744 source(std::move(source)) {}
745 AssertKind kind;
746 Expression* expression;
747 std::string source;
748 };
749
750 struct TailCallStatement : Statement {
751 DEFINE_AST_NODE_LEAF_BOILERPLATE(TailCallStatement)
TailCallStatementTailCallStatement752 TailCallStatement(SourcePosition pos, CallExpression* call)
753 : Statement(kKind, pos), call(call) {}
754 CallExpression* call;
755 };
756
757 struct VarDeclarationStatement : Statement {
758 DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
759 VarDeclarationStatement(
760 SourcePosition pos, bool const_qualified, Identifier* name,
761 base::Optional<TypeExpression*> type,
762 base::Optional<Expression*> initializer = base::nullopt)
StatementVarDeclarationStatement763 : Statement(kKind, pos),
764 const_qualified(const_qualified),
765 name(name),
766 type(type),
767 initializer(initializer) {}
768 bool const_qualified;
769 Identifier* name;
770 base::Optional<TypeExpression*> type;
771 base::Optional<Expression*> initializer;
772 };
773
774 struct BreakStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEBreakStatement775 DEFINE_AST_NODE_LEAF_BOILERPLATE(BreakStatement)
776 explicit BreakStatement(SourcePosition pos) : Statement(kKind, pos) {}
777 };
778
779 struct ContinueStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEContinueStatement780 DEFINE_AST_NODE_LEAF_BOILERPLATE(ContinueStatement)
781 explicit ContinueStatement(SourcePosition pos) : Statement(kKind, pos) {}
782 };
783
784 struct GotoStatement : Statement {
785 DEFINE_AST_NODE_LEAF_BOILERPLATE(GotoStatement)
GotoStatementGotoStatement786 GotoStatement(SourcePosition pos, Identifier* label,
787 const std::vector<Expression*>& arguments)
788 : Statement(kKind, pos), label(label), arguments(std::move(arguments)) {}
789 Identifier* label;
790 std::vector<Expression*> arguments;
791 };
792
793 struct ForLoopStatement : Statement {
794 DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
ForLoopStatementForLoopStatement795 ForLoopStatement(SourcePosition pos, base::Optional<Statement*> declaration,
796 base::Optional<Expression*> test,
797 base::Optional<Statement*> action, Statement* body)
798 : Statement(kKind, pos),
799 var_declaration(),
800 test(std::move(test)),
801 action(std::move(action)),
802 body(std::move(body)) {
803 if (declaration)
804 var_declaration = VarDeclarationStatement::cast(*declaration);
805 }
806 base::Optional<VarDeclarationStatement*> var_declaration;
807 base::Optional<Expression*> test;
808 base::Optional<Statement*> action;
809 Statement* body;
810 };
811
812 struct TryHandler : AstNode {
813 DEFINE_AST_NODE_LEAF_BOILERPLATE(TryHandler)
814 enum class HandlerKind { kCatch, kLabel };
TryHandlerTryHandler815 TryHandler(SourcePosition pos, HandlerKind handler_kind, Identifier* label,
816 const ParameterList& parameters, Statement* body)
817 : AstNode(kKind, pos),
818 handler_kind(handler_kind),
819 label(label),
820 parameters(parameters),
821 body(std::move(body)) {}
822 HandlerKind handler_kind;
823 Identifier* label;
824 ParameterList parameters;
825 Statement* body;
826 };
827
828 struct StatementExpression : Expression {
829 DEFINE_AST_NODE_LEAF_BOILERPLATE(StatementExpression)
StatementExpressionStatementExpression830 StatementExpression(SourcePosition pos, Statement* statement)
831 : Expression(kKind, pos), statement(statement) {}
832 Statement* statement;
833 };
834
835 struct TryLabelExpression : Expression {
836 DEFINE_AST_NODE_LEAF_BOILERPLATE(TryLabelExpression)
TryLabelExpressionTryLabelExpression837 TryLabelExpression(SourcePosition pos, Expression* try_expression,
838 TryHandler* label_block)
839 : Expression(kKind, pos),
840 try_expression(try_expression),
841 label_block(label_block) {}
842 Expression* try_expression;
843 TryHandler* label_block;
844 };
845
846 struct BlockStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEBlockStatement847 DEFINE_AST_NODE_LEAF_BOILERPLATE(BlockStatement)
848 explicit BlockStatement(SourcePosition pos, bool deferred = false,
849 std::vector<Statement*> statements = {})
850 : Statement(kKind, pos),
851 deferred(deferred),
852 statements(std::move(statements)) {}
853 bool deferred;
854 std::vector<Statement*> statements;
855 };
856
857 struct TypeDeclaration : Declaration {
858 DEFINE_AST_NODE_INNER_BOILERPLATE(TypeDeclaration)
TypeDeclarationTypeDeclaration859 TypeDeclaration(Kind kKind, SourcePosition pos, Identifier* name)
860 : Declaration(kKind, pos), name(name) {}
861 Identifier* name;
862 };
863
864 struct InstanceTypeConstraints {
InstanceTypeConstraintsInstanceTypeConstraints865 InstanceTypeConstraints() : value(-1), num_flags_bits(-1) {}
866 int value;
867 int num_flags_bits;
868 };
869
870 struct AbstractTypeDeclaration : TypeDeclaration {
871 DEFINE_AST_NODE_LEAF_BOILERPLATE(AbstractTypeDeclaration)
AbstractTypeDeclarationAbstractTypeDeclaration872 AbstractTypeDeclaration(SourcePosition pos, Identifier* name,
873 AbstractTypeFlags flags,
874 base::Optional<TypeExpression*> extends,
875 base::Optional<std::string> generates)
876 : TypeDeclaration(kKind, pos, name),
877 flags(flags),
878 extends(extends),
879 generates(std::move(generates)) {
880 CHECK_EQ(IsConstexprName(name->value),
881 !!(flags & AbstractTypeFlag::kConstexpr));
882 }
883
IsConstexprAbstractTypeDeclaration884 bool IsConstexpr() const { return flags & AbstractTypeFlag::kConstexpr; }
IsTransientAbstractTypeDeclaration885 bool IsTransient() const { return flags & AbstractTypeFlag::kTransient; }
886
887 AbstractTypeFlags flags;
888 base::Optional<TypeExpression*> extends;
889 base::Optional<std::string> generates;
890 };
891
892 struct TypeAliasDeclaration : TypeDeclaration {
893 DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
TypeAliasDeclarationTypeAliasDeclaration894 TypeAliasDeclaration(SourcePosition pos, Identifier* name,
895 TypeExpression* type)
896 : TypeDeclaration(kKind, pos, name), type(type) {}
897 TypeExpression* type;
898 };
899
900 struct NameAndTypeExpression {
901 Identifier* name;
902 TypeExpression* type;
903 };
904
905 struct ImplicitParameters {
906 Identifier* kind;
907 std::vector<NameAndTypeExpression> parameters;
908 };
909
910 struct StructFieldExpression {
911 NameAndTypeExpression name_and_type;
912 bool const_qualified;
913 };
914
915 struct BitFieldDeclaration {
916 NameAndTypeExpression name_and_type;
917 int num_bits;
918 };
919
920 enum class ConditionalAnnotationType {
921 kPositive,
922 kNegative,
923 };
924
925 struct ConditionalAnnotation {
926 std::string condition;
927 ConditionalAnnotationType type;
928 };
929
930 struct AnnotationParameter {
931 std::string string_value;
932 int int_value;
933 bool is_int;
934 };
935
936 struct Annotation {
937 Identifier* name;
938 base::Optional<AnnotationParameter> param;
939 };
940
941 struct ClassFieldIndexInfo {
942 // The expression that can compute how many items are in the indexed field.
943 Expression* expr;
944
945 // Whether the field was declared as optional, meaning it can only hold zero
946 // or one values, and thus should not require an index expression to access.
947 bool optional;
948 };
949
950 struct ClassFieldExpression {
951 NameAndTypeExpression name_and_type;
952 base::Optional<ClassFieldIndexInfo> index;
953 std::vector<ConditionalAnnotation> conditions;
954 bool custom_weak_marking;
955 bool const_qualified;
956 FieldSynchronization read_synchronization;
957 FieldSynchronization write_synchronization;
958 };
959
960 struct LabelAndTypes {
961 Identifier* name;
962 std::vector<TypeExpression*> types;
963 };
964
965 using LabelAndTypesVector = std::vector<LabelAndTypes>;
966
967 struct CallableDeclaration : Declaration {
CallableDeclarationCallableDeclaration968 CallableDeclaration(AstNode::Kind kind, SourcePosition pos,
969 bool transitioning, Identifier* name,
970 ParameterList parameters, TypeExpression* return_type,
971 LabelAndTypesVector labels)
972 : Declaration(kind, pos),
973 transitioning(transitioning),
974 name(name),
975 parameters(std::move(parameters)),
976 return_type(return_type),
977 labels(std::move(labels)) {}
978 DEFINE_AST_NODE_INNER_BOILERPLATE(CallableDeclaration)
979 bool transitioning;
980 Identifier* name;
981 ParameterList parameters;
982 TypeExpression* return_type;
983 LabelAndTypesVector labels;
984 };
985
986 struct MacroDeclaration : CallableDeclaration {
987 DEFINE_AST_NODE_INNER_BOILERPLATE(MacroDeclaration)
MacroDeclarationMacroDeclaration988 MacroDeclaration(AstNode::Kind kind, SourcePosition pos, bool transitioning,
989 Identifier* name, base::Optional<std::string> op,
990 ParameterList parameters, TypeExpression* return_type,
991 const LabelAndTypesVector& labels)
992 : CallableDeclaration(kind, pos, transitioning, name,
993 std::move(parameters), return_type, labels),
994 op(std::move(op)) {
995 if (parameters.implicit_kind == ImplicitKind::kJSImplicit) {
996 Error("Cannot use \"js-implicit\" with macros, use \"implicit\" instead.")
997 .Position(parameters.implicit_kind_pos);
998 }
999 }
1000 base::Optional<std::string> op;
1001 };
1002
1003 struct ExternalMacroDeclaration : MacroDeclaration {
1004 DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalMacroDeclaration)
ExternalMacroDeclarationExternalMacroDeclaration1005 ExternalMacroDeclaration(SourcePosition pos, bool transitioning,
1006 std::string external_assembler_name,
1007 Identifier* name, base::Optional<std::string> op,
1008 ParameterList parameters,
1009 TypeExpression* return_type,
1010 const LabelAndTypesVector& labels)
1011 : MacroDeclaration(kKind, pos, transitioning, name, std::move(op),
1012 std::move(parameters), return_type, labels),
1013 external_assembler_name(std::move(external_assembler_name)) {}
1014 std::string external_assembler_name;
1015 };
1016
1017 struct IntrinsicDeclaration : CallableDeclaration {
1018 DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicDeclaration)
IntrinsicDeclarationIntrinsicDeclaration1019 IntrinsicDeclaration(SourcePosition pos, Identifier* name,
1020 ParameterList parameters, TypeExpression* return_type)
1021 : CallableDeclaration(kKind, pos, false, name, std::move(parameters),
1022 return_type, {}) {
1023 if (parameters.implicit_kind != ImplicitKind::kNoImplicit) {
1024 Error("Intinsics cannot have implicit parameters.");
1025 }
1026 }
1027 };
1028
1029 struct TorqueMacroDeclaration : MacroDeclaration {
1030 DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueMacroDeclaration)
TorqueMacroDeclarationTorqueMacroDeclaration1031 TorqueMacroDeclaration(SourcePosition pos, bool transitioning,
1032 Identifier* name, base::Optional<std::string> op,
1033 ParameterList parameters, TypeExpression* return_type,
1034 const LabelAndTypesVector& labels, bool export_to_csa,
1035 base::Optional<Statement*> body)
1036 : MacroDeclaration(kKind, pos, transitioning, name, std::move(op),
1037 std::move(parameters), return_type, labels),
1038 export_to_csa(export_to_csa),
1039 body(body) {}
1040 bool export_to_csa;
1041 base::Optional<Statement*> body;
1042 };
1043
1044 struct BuiltinDeclaration : CallableDeclaration {
1045 DEFINE_AST_NODE_INNER_BOILERPLATE(BuiltinDeclaration)
BuiltinDeclarationBuiltinDeclaration1046 BuiltinDeclaration(AstNode::Kind kind, SourcePosition pos,
1047 bool javascript_linkage, bool transitioning,
1048 Identifier* name, ParameterList parameters,
1049 TypeExpression* return_type)
1050 : CallableDeclaration(kind, pos, transitioning, name,
1051 std::move(parameters), return_type, {}),
1052 javascript_linkage(javascript_linkage) {
1053 if (parameters.implicit_kind == ImplicitKind::kJSImplicit &&
1054 !javascript_linkage) {
1055 Error(
1056 "\"js-implicit\" is for implicit parameters passed according to the "
1057 "JavaScript calling convention. Use \"implicit\" instead.");
1058 }
1059 if (parameters.implicit_kind == ImplicitKind::kImplicit &&
1060 javascript_linkage) {
1061 Error(
1062 "The JavaScript calling convention implicitly passes a fixed set of "
1063 "values. Use \"js-implicit\" to refer to those.")
1064 .Position(parameters.implicit_kind_pos);
1065 }
1066 }
1067 bool javascript_linkage;
1068 };
1069
1070 struct ExternalBuiltinDeclaration : BuiltinDeclaration {
1071 DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalBuiltinDeclaration)
ExternalBuiltinDeclarationExternalBuiltinDeclaration1072 ExternalBuiltinDeclaration(SourcePosition pos, bool transitioning,
1073 bool javascript_linkage, Identifier* name,
1074 ParameterList parameters,
1075 TypeExpression* return_type)
1076 : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning, name,
1077 std::move(parameters), return_type) {}
1078 };
1079
1080 struct TorqueBuiltinDeclaration : BuiltinDeclaration {
1081 DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueBuiltinDeclaration)
TorqueBuiltinDeclarationTorqueBuiltinDeclaration1082 TorqueBuiltinDeclaration(SourcePosition pos, bool transitioning,
1083 bool javascript_linkage, Identifier* name,
1084 ParameterList parameters,
1085 TypeExpression* return_type,
1086 base::Optional<Statement*> body)
1087 : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning, name,
1088 std::move(parameters), return_type),
1089 body(body) {}
1090 base::Optional<Statement*> body;
1091 };
1092
1093 struct ExternalRuntimeDeclaration : CallableDeclaration {
1094 DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalRuntimeDeclaration)
ExternalRuntimeDeclarationExternalRuntimeDeclaration1095 ExternalRuntimeDeclaration(SourcePosition pos, bool transitioning,
1096 Identifier* name, ParameterList parameters,
1097 TypeExpression* return_type)
1098 : CallableDeclaration(kKind, pos, transitioning, name, parameters,
1099 return_type, {}) {}
1100 };
1101
1102 struct ConstDeclaration : Declaration {
1103 DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
ConstDeclarationConstDeclaration1104 ConstDeclaration(SourcePosition pos, Identifier* name, TypeExpression* type,
1105 Expression* expression)
1106 : Declaration(kKind, pos),
1107 name(name),
1108 type(type),
1109 expression(expression) {}
1110 Identifier* name;
1111 TypeExpression* type;
1112 Expression* expression;
1113 };
1114
1115 struct GenericParameter {
1116 Identifier* name;
1117 base::Optional<TypeExpression*> constraint;
1118 };
1119
1120 using GenericParameters = std::vector<GenericParameter>;
1121
1122 // The AST re-shuffles generics from the concrete syntax:
1123 // Instead of the generic parameters being part of a normal declaration,
1124 // a declaration with generic parameters gets wrapped in a generic declaration,
1125 // which holds the generic parameters. This corresponds to how you write
1126 // templates in C++, with the template parameters coming before the declaration.
1127
1128 struct GenericCallableDeclaration : Declaration {
1129 DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericCallableDeclaration)
GenericCallableDeclarationGenericCallableDeclaration1130 GenericCallableDeclaration(SourcePosition pos,
1131 GenericParameters generic_parameters,
1132 CallableDeclaration* declaration)
1133 : Declaration(kKind, pos),
1134 generic_parameters(std::move(generic_parameters)),
1135 declaration(declaration) {}
1136
1137 GenericParameters generic_parameters;
1138 CallableDeclaration* declaration;
1139 };
1140
1141 struct GenericTypeDeclaration : Declaration {
1142 DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericTypeDeclaration)
GenericTypeDeclarationGenericTypeDeclaration1143 GenericTypeDeclaration(SourcePosition pos,
1144 GenericParameters generic_parameters,
1145 TypeDeclaration* declaration)
1146 : Declaration(kKind, pos),
1147 generic_parameters(std::move(generic_parameters)),
1148 declaration(declaration) {}
1149
1150 GenericParameters generic_parameters;
1151 TypeDeclaration* declaration;
1152 };
1153
1154 struct SpecializationDeclaration : CallableDeclaration {
1155 DEFINE_AST_NODE_LEAF_BOILERPLATE(SpecializationDeclaration)
SpecializationDeclarationSpecializationDeclaration1156 SpecializationDeclaration(SourcePosition pos, bool transitioning,
1157 Identifier* name,
1158 std::vector<TypeExpression*> generic_parameters,
1159 ParameterList parameters,
1160 TypeExpression* return_type,
1161 LabelAndTypesVector labels, Statement* body)
1162 : CallableDeclaration(kKind, pos, transitioning, name,
1163 std::move(parameters), return_type,
1164 std::move(labels)),
1165 generic_parameters(std::move(generic_parameters)),
1166 body(body) {}
1167 std::vector<TypeExpression*> generic_parameters;
1168 Statement* body;
1169 };
1170
1171 struct ExternConstDeclaration : Declaration {
1172 DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
ExternConstDeclarationExternConstDeclaration1173 ExternConstDeclaration(SourcePosition pos, Identifier* name,
1174 TypeExpression* type, std::string literal)
1175 : Declaration(kKind, pos),
1176 name(name),
1177 type(type),
1178 literal(std::move(literal)) {}
1179 Identifier* name;
1180 TypeExpression* type;
1181 std::string literal;
1182 };
1183
1184 struct StructDeclaration : TypeDeclaration {
1185 DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
StructDeclarationStructDeclaration1186 StructDeclaration(SourcePosition pos, StructFlags flags, Identifier* name,
1187 std::vector<Declaration*> methods,
1188 std::vector<StructFieldExpression> fields)
1189 : TypeDeclaration(kKind, pos, name),
1190 flags(flags),
1191 methods(std::move(methods)),
1192 fields(std::move(fields)) {}
1193 StructFlags flags;
1194 std::vector<Declaration*> methods;
1195 std::vector<StructFieldExpression> fields;
1196 };
1197
1198 struct BitFieldStructDeclaration : TypeDeclaration {
1199 DEFINE_AST_NODE_LEAF_BOILERPLATE(BitFieldStructDeclaration)
BitFieldStructDeclarationBitFieldStructDeclaration1200 BitFieldStructDeclaration(SourcePosition pos, Identifier* name,
1201 TypeExpression* parent,
1202 std::vector<BitFieldDeclaration> fields)
1203 : TypeDeclaration(kKind, pos, name),
1204 parent(parent),
1205 fields(std::move(fields)) {}
1206 TypeExpression* parent;
1207 std::vector<BitFieldDeclaration> fields;
1208 };
1209
1210 struct ClassBody : AstNode {
1211 DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassBody)
ClassBodyClassBody1212 ClassBody(SourcePosition pos, std::vector<Declaration*> methods,
1213 std::vector<ClassFieldExpression> fields)
1214 : AstNode(kKind, pos),
1215 methods(std::move(methods)),
1216 fields(std::move(fields)) {}
1217 std::vector<Declaration*> methods;
1218 std::vector<ClassFieldExpression> fields;
1219 };
1220
1221 struct ClassDeclaration : TypeDeclaration {
1222 DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration)
ClassDeclarationClassDeclaration1223 ClassDeclaration(SourcePosition pos, Identifier* name, ClassFlags flags,
1224 TypeExpression* super, base::Optional<std::string> generates,
1225 std::vector<Declaration*> methods,
1226 std::vector<ClassFieldExpression> fields,
1227 InstanceTypeConstraints instance_type_constraints)
1228 : TypeDeclaration(kKind, pos, name),
1229 flags(flags),
1230 super(super),
1231 generates(std::move(generates)),
1232 methods(std::move(methods)),
1233 fields(std::move(fields)),
1234 instance_type_constraints(std::move(instance_type_constraints)) {}
1235 ClassFlags flags;
1236 TypeExpression* super;
1237 base::Optional<std::string> generates;
1238 std::vector<Declaration*> methods;
1239 std::vector<ClassFieldExpression> fields;
1240 InstanceTypeConstraints instance_type_constraints;
1241 };
1242
1243 struct CppIncludeDeclaration : Declaration {
1244 DEFINE_AST_NODE_LEAF_BOILERPLATE(CppIncludeDeclaration)
CppIncludeDeclarationCppIncludeDeclaration1245 CppIncludeDeclaration(SourcePosition pos, std::string include_path)
1246 : Declaration(kKind, pos), include_path(std::move(include_path)) {}
1247 std::string include_path;
1248 };
1249
1250 #define ENUM_ITEM(name) \
1251 case AstNode::Kind::k##name: \
1252 return std::is_base_of<T, name>::value; \
1253 break;
1254
1255 template <class T>
IsInstanceOf(AstNode * node)1256 bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
1257 switch (node->kind) {
1258 AST_NODE_KIND_LIST(ENUM_ITEM)
1259 default:
1260 UNIMPLEMENTED();
1261 }
1262 return true;
1263 }
1264
1265 #undef ENUM_ITEM
1266
IsDeferred(Statement * stmt)1267 inline bool IsDeferred(Statement* stmt) {
1268 if (auto* block = BlockStatement::DynamicCast(stmt)) {
1269 return block->deferred;
1270 }
1271 return false;
1272 }
1273
1274 DECLARE_CONTEXTUAL_VARIABLE(CurrentAst, Ast);
1275
1276 template <class T, class... Args>
MakeNode(Args...args)1277 T* MakeNode(Args... args) {
1278 return CurrentAst::Get().AddNode(
1279 std::make_unique<T>(CurrentSourcePosition::Get(), std::move(args)...));
1280 }
1281
MakeFieldAccessExpression(Expression * object,std::string field)1282 inline FieldAccessExpression* MakeFieldAccessExpression(Expression* object,
1283 std::string field) {
1284 return MakeNode<FieldAccessExpression>(
1285 object, MakeNode<Identifier>(std::move(field)));
1286 }
1287
1288 inline IdentifierExpression* MakeIdentifierExpression(
1289 std::vector<std::string> namespace_qualification, std::string name,
1290 std::vector<TypeExpression*> args = {}) {
1291 return MakeNode<IdentifierExpression>(std::move(namespace_qualification),
1292 MakeNode<Identifier>(std::move(name)),
1293 std::move(args));
1294 }
1295
MakeIdentifierExpression(std::string name)1296 inline IdentifierExpression* MakeIdentifierExpression(std::string name) {
1297 return MakeIdentifierExpression({}, std::move(name));
1298 }
1299
1300 inline CallExpression* MakeCallExpression(
1301 IdentifierExpression* callee, std::vector<Expression*> arguments,
1302 std::vector<Identifier*> labels = {}) {
1303 return MakeNode<CallExpression>(callee, std::move(arguments),
1304 std::move(labels));
1305 }
1306
1307 inline CallExpression* MakeCallExpression(
1308 std::string callee, std::vector<Expression*> arguments,
1309 std::vector<Identifier*> labels = {}) {
1310 return MakeCallExpression(MakeIdentifierExpression(std::move(callee)),
1311 std::move(arguments), std::move(labels));
1312 }
1313
MakeConstDeclarationStatement(std::string name,Expression * initializer)1314 inline VarDeclarationStatement* MakeConstDeclarationStatement(
1315 std::string name, Expression* initializer) {
1316 return MakeNode<VarDeclarationStatement>(
1317 /*const_qualified=*/true, MakeNode<Identifier>(std::move(name)),
1318 base::Optional<TypeExpression*>{}, initializer);
1319 }
1320
1321 inline BasicTypeExpression* MakeBasicTypeExpression(
1322 std::vector<std::string> namespace_qualification, Identifier* name,
1323 std::vector<TypeExpression*> generic_arguments = {}) {
1324 return MakeNode<BasicTypeExpression>(std::move(namespace_qualification), name,
1325 std::move(generic_arguments));
1326 }
1327
MakeStructExpression(TypeExpression * type,std::vector<NameAndExpression> initializers)1328 inline StructExpression* MakeStructExpression(
1329 TypeExpression* type, std::vector<NameAndExpression> initializers) {
1330 return MakeNode<StructExpression>(type, std::move(initializers));
1331 }
1332
1333 } // namespace torque
1334 } // namespace internal
1335 } // namespace v8
1336
1337 #endif // V8_TORQUE_AST_H_
1338