• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "src/base/optional.h"
14 #include "src/torque/source-positions.h"
15 
16 namespace v8 {
17 namespace internal {
18 namespace torque {
19 
20 #define AST_EXPRESSION_NODE_KIND_LIST(V) \
21   V(CallExpression)                      \
22   V(StructExpression)                    \
23   V(LogicalOrExpression)                 \
24   V(LogicalAndExpression)                \
25   V(ConditionalExpression)               \
26   V(IdentifierExpression)                \
27   V(StringLiteralExpression)             \
28   V(NumberLiteralExpression)             \
29   V(FieldAccessExpression)               \
30   V(ElementAccessExpression)             \
31   V(AssignmentExpression)                \
32   V(IncrementDecrementExpression)        \
33   V(AssumeTypeImpossibleExpression)
34 
35 #define AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
36   V(BasicTypeExpression)                      \
37   V(FunctionTypeExpression)                   \
38   V(UnionTypeExpression)
39 
40 #define AST_STATEMENT_NODE_KIND_LIST(V) \
41   V(BlockStatement)                     \
42   V(ExpressionStatement)                \
43   V(IfStatement)                        \
44   V(WhileStatement)                     \
45   V(ForLoopStatement)                   \
46   V(ForOfLoopStatement)                 \
47   V(BreakStatement)                     \
48   V(ContinueStatement)                  \
49   V(ReturnStatement)                    \
50   V(DebugStatement)                     \
51   V(AssertStatement)                    \
52   V(TailCallStatement)                  \
53   V(VarDeclarationStatement)            \
54   V(GotoStatement)                      \
55   V(TryLabelStatement)
56 
57 #define AST_DECLARATION_NODE_KIND_LIST(V) \
58   V(TypeDeclaration)                      \
59   V(TypeAliasDeclaration)                 \
60   V(StandardDeclaration)                  \
61   V(GenericDeclaration)                   \
62   V(SpecializationDeclaration)            \
63   V(ExternConstDeclaration)               \
64   V(StructDeclaration)                    \
65   V(DefaultModuleDeclaration)             \
66   V(ExplicitModuleDeclaration)            \
67   V(ConstDeclaration)
68 
69 #define AST_CALLABLE_NODE_KIND_LIST(V) \
70   V(TorqueMacroDeclaration)            \
71   V(TorqueBuiltinDeclaration)          \
72   V(ExternalMacroDeclaration)          \
73   V(ExternalBuiltinDeclaration)        \
74   V(ExternalRuntimeDeclaration)
75 
76 #define AST_NODE_KIND_LIST(V)           \
77   AST_EXPRESSION_NODE_KIND_LIST(V)      \
78   AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
79   AST_STATEMENT_NODE_KIND_LIST(V)       \
80   AST_DECLARATION_NODE_KIND_LIST(V)     \
81   AST_CALLABLE_NODE_KIND_LIST(V)        \
82   V(LabelBlock)
83 
84 struct AstNode {
85  public:
86   enum class Kind {
87 #define ENUM_ITEM(name) k##name,
88     AST_NODE_KIND_LIST(ENUM_ITEM)
89 #undef ENUM_ITEM
90   };
91 
AstNodeAstNode92   AstNode(Kind kind, SourcePosition pos) : kind(kind), pos(pos) {}
~AstNodeAstNode93   virtual ~AstNode() {}
94 
95   const Kind kind;
96   SourcePosition pos;
97 };
98 
99 struct AstNodeClassCheck {
100   template <class T>
101   static bool IsInstanceOf(AstNode* node);
102 };
103 
104 // Boilerplate for most derived classes.
105 #define DEFINE_AST_NODE_LEAF_BOILERPLATE(T)                        \
106   static const Kind kKind = Kind::k##T;                            \
107   static T* cast(AstNode* node) {                                  \
108     if (node->kind != kKind) return nullptr;                       \
109     return static_cast<T*>(node);                                  \
110   }                                                                \
111   static T* DynamicCast(AstNode* node) {                           \
112     if (!node) return nullptr;                                     \
113     if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
114     return static_cast<T*>(node);                                  \
115   }
116 
117 // Boilerplate for classes with subclasses.
118 #define DEFINE_AST_NODE_INNER_BOILERPLATE(T)                       \
119   static T* cast(AstNode* node) {                                  \
120     DCHECK(AstNodeClassCheck::IsInstanceOf<T>(node));              \
121     return static_cast<T*>(node);                                  \
122   }                                                                \
123   static T* DynamicCast(AstNode* node) {                           \
124     if (!node) return nullptr;                                     \
125     if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
126     return static_cast<T*>(node);                                  \
127   }
128 
129 struct Expression : AstNode {
ExpressionExpression130   Expression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
131   DEFINE_AST_NODE_INNER_BOILERPLATE(Expression)
132 };
133 
134 struct LocationExpression : Expression {
LocationExpressionLocationExpression135   LocationExpression(Kind kind, SourcePosition pos) : Expression(kind, pos) {}
136   DEFINE_AST_NODE_INNER_BOILERPLATE(LocationExpression)
137 };
138 
139 struct TypeExpression : AstNode {
TypeExpressionTypeExpression140   TypeExpression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
141   DEFINE_AST_NODE_INNER_BOILERPLATE(TypeExpression)
142 };
143 
144 struct Declaration : AstNode {
DeclarationDeclaration145   Declaration(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
146   DEFINE_AST_NODE_INNER_BOILERPLATE(Declaration)
147 };
148 
149 struct Statement : AstNode {
StatementStatement150   Statement(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
151   DEFINE_AST_NODE_INNER_BOILERPLATE(Statement)
152 };
153 
154 class Module;
155 
156 struct ModuleDeclaration : Declaration {
ModuleDeclarationModuleDeclaration157   ModuleDeclaration(AstNode::Kind kind, SourcePosition pos,
158                     std::vector<Declaration*> declarations)
159       : Declaration(kind, pos),
160         module(nullptr),
161         declarations(std::move(declarations)) {}
162   virtual bool IsDefault() const = 0;
163   //  virtual std::string GetName() const = 0;
SetModuleModuleDeclaration164   void SetModule(Module* m) { module = m; }
GetModuleModuleDeclaration165   Module* GetModule() const { return module; }
166   Module* module;
167   std::vector<Declaration*> declarations;
168 };
169 
170 struct DefaultModuleDeclaration : ModuleDeclaration {
171   DEFINE_AST_NODE_LEAF_BOILERPLATE(DefaultModuleDeclaration)
DefaultModuleDeclarationDefaultModuleDeclaration172   DefaultModuleDeclaration(SourcePosition pos,
173                            std::vector<Declaration*> declarations)
174       : ModuleDeclaration(kKind, pos, declarations) {}
IsDefaultDefaultModuleDeclaration175   bool IsDefault() const override { return true; }
176 };
177 
178 struct ExplicitModuleDeclaration : ModuleDeclaration {
179   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExplicitModuleDeclaration)
ExplicitModuleDeclarationExplicitModuleDeclaration180   ExplicitModuleDeclaration(SourcePosition pos, std::string name,
181                             std::vector<Declaration*> declarations)
182       : ModuleDeclaration(kKind, pos, declarations), name(std::move(name)) {}
IsDefaultExplicitModuleDeclaration183   bool IsDefault() const override { return false; }
184   std::string name;
185 };
186 
187 class Ast {
188  public:
Ast()189   Ast() : default_module_{SourcePosition{CurrentSourceFile::Get(), 0, 0}, {}} {}
190 
declarations()191   std::vector<Declaration*>& declarations() {
192     return default_module_.declarations;
193   }
declarations()194   const std::vector<Declaration*>& declarations() const {
195     return default_module_.declarations;
196   }
197   template <class T>
AddNode(std::unique_ptr<T> node)198   T* AddNode(std::unique_ptr<T> node) {
199     T* result = node.get();
200     nodes_.push_back(std::move(node));
201     return result;
202   }
default_module()203   DefaultModuleDeclaration* default_module() { return &default_module_; }
204 
205  private:
206   DefaultModuleDeclaration default_module_;
207   std::vector<std::unique_ptr<AstNode>> nodes_;
208 };
209 
210 struct IdentifierExpression : LocationExpression {
211   DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
212   IdentifierExpression(SourcePosition pos, std::string name,
213                        std::vector<TypeExpression*> args = {})
LocationExpressionIdentifierExpression214       : LocationExpression(kKind, pos),
215         name(std::move(name)),
216         generic_arguments(std::move(args)) {}
217   std::string name;
218   std::vector<TypeExpression*> generic_arguments;
219 };
220 
221 struct CallExpression : Expression {
222   DEFINE_AST_NODE_LEAF_BOILERPLATE(CallExpression)
CallExpressionCallExpression223   CallExpression(SourcePosition pos, std::string callee, bool is_operator,
224                  std::vector<TypeExpression*> generic_arguments,
225                  std::vector<Expression*> arguments,
226                  std::vector<std::string> labels)
227       : Expression(kKind, pos),
228         callee(pos, std::move(callee), std::move(generic_arguments)),
229         is_operator(is_operator),
230         arguments(std::move(arguments)),
231         labels(labels) {}
232   IdentifierExpression callee;
233   bool is_operator;
234   std::vector<Expression*> arguments;
235   std::vector<std::string> labels;
236 };
237 
238 struct StructExpression : Expression {
239   DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
StructExpressionStructExpression240   StructExpression(SourcePosition pos, std::string name,
241                    std::vector<Expression*> expressions)
242       : Expression(kKind, pos),
243         name(name),
244         expressions(std::move(expressions)) {}
245   std::string name;
246   std::vector<Expression*> expressions;
247 };
248 
249 struct LogicalOrExpression : Expression {
250   DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalOrExpression)
LogicalOrExpressionLogicalOrExpression251   LogicalOrExpression(SourcePosition pos, Expression* left, Expression* right)
252       : Expression(kKind, pos), left(left), right(right) {}
253   Expression* left;
254   Expression* right;
255 };
256 
257 struct LogicalAndExpression : Expression {
258   DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalAndExpression)
LogicalAndExpressionLogicalAndExpression259   LogicalAndExpression(SourcePosition pos, Expression* left, Expression* right)
260       : Expression(kKind, pos), left(left), right(right) {}
261   Expression* left;
262   Expression* right;
263 };
264 
265 struct ConditionalExpression : Expression {
266   DEFINE_AST_NODE_LEAF_BOILERPLATE(ConditionalExpression)
ConditionalExpressionConditionalExpression267   ConditionalExpression(SourcePosition pos, Expression* condition,
268                         Expression* if_true, Expression* if_false)
269       : Expression(kKind, pos),
270         condition(condition),
271         if_true(if_true),
272         if_false(if_false) {}
273   Expression* condition;
274   Expression* if_true;
275   Expression* if_false;
276 };
277 
278 struct StringLiteralExpression : Expression {
279   DEFINE_AST_NODE_LEAF_BOILERPLATE(StringLiteralExpression)
StringLiteralExpressionStringLiteralExpression280   StringLiteralExpression(SourcePosition pos, std::string literal)
281       : Expression(kKind, pos), literal(std::move(literal)) {}
282   std::string literal;
283 };
284 
285 struct NumberLiteralExpression : Expression {
286   DEFINE_AST_NODE_LEAF_BOILERPLATE(NumberLiteralExpression)
NumberLiteralExpressionNumberLiteralExpression287   NumberLiteralExpression(SourcePosition pos, std::string name)
288       : Expression(kKind, pos), number(std::move(name)) {}
289   std::string number;
290 };
291 
292 struct ElementAccessExpression : LocationExpression {
293   DEFINE_AST_NODE_LEAF_BOILERPLATE(ElementAccessExpression)
ElementAccessExpressionElementAccessExpression294   ElementAccessExpression(SourcePosition pos, Expression* array,
295                           Expression* index)
296       : LocationExpression(kKind, pos), array(array), index(index) {}
297   Expression* array;
298   Expression* index;
299 };
300 
301 struct FieldAccessExpression : LocationExpression {
302   DEFINE_AST_NODE_LEAF_BOILERPLATE(FieldAccessExpression)
FieldAccessExpressionFieldAccessExpression303   FieldAccessExpression(SourcePosition pos, Expression* object,
304                         std::string field)
305       : LocationExpression(kKind, pos),
306         object(object),
307         field(std::move(field)) {}
308   Expression* object;
309   std::string field;
310 };
311 
312 struct AssignmentExpression : Expression {
313   DEFINE_AST_NODE_LEAF_BOILERPLATE(AssignmentExpression)
AssignmentExpressionAssignmentExpression314   AssignmentExpression(SourcePosition pos, LocationExpression* location,
315                        base::Optional<std::string> op, Expression* value)
316       : Expression(kKind, pos),
317         location(location),
318         op(std::move(op)),
319         value(value) {}
320   LocationExpression* location;
321   base::Optional<std::string> op;
322   Expression* value;
323 };
324 
325 enum class IncrementDecrementOperator { kIncrement, kDecrement };
326 
327 struct IncrementDecrementExpression : Expression {
328   DEFINE_AST_NODE_LEAF_BOILERPLATE(IncrementDecrementExpression)
IncrementDecrementExpressionIncrementDecrementExpression329   IncrementDecrementExpression(SourcePosition pos, LocationExpression* location,
330                                IncrementDecrementOperator op, bool postfix)
331       : Expression(kKind, pos), location(location), op(op), postfix(postfix) {}
332   LocationExpression* location;
333   IncrementDecrementOperator op;
334   bool postfix;
335 };
336 
337 // This expression is only used in the desugaring of typeswitch, and it allows
338 // to bake in the static information that certain types are impossible at a
339 // certain position in the control flow.
340 // The result type is the type of {expression} minus the provided type.
341 struct AssumeTypeImpossibleExpression : Expression {
342   DEFINE_AST_NODE_LEAF_BOILERPLATE(AssumeTypeImpossibleExpression)
AssumeTypeImpossibleExpressionAssumeTypeImpossibleExpression343   AssumeTypeImpossibleExpression(SourcePosition pos,
344                                  TypeExpression* excluded_type,
345                                  Expression* expression)
346       : Expression(kKind, pos),
347         excluded_type(excluded_type),
348         expression(expression) {}
349   TypeExpression* excluded_type;
350   Expression* expression;
351 };
352 
353 struct ParameterList {
354   std::vector<std::string> names;
355   std::vector<TypeExpression*> types;
356   bool has_varargs;
357   std::string arguments_variable;
358 
EmptyParameterList359   static ParameterList Empty() { return ParameterList{{}, {}, false, ""}; }
360 };
361 
362 struct BasicTypeExpression : TypeExpression {
363   DEFINE_AST_NODE_LEAF_BOILERPLATE(BasicTypeExpression)
BasicTypeExpressionBasicTypeExpression364   BasicTypeExpression(SourcePosition pos, bool is_constexpr, std::string name)
365       : TypeExpression(kKind, pos), is_constexpr(is_constexpr), name(name) {}
366   bool is_constexpr;
367   std::string name;
368 };
369 
370 struct FunctionTypeExpression : TypeExpression {
371   DEFINE_AST_NODE_LEAF_BOILERPLATE(FunctionTypeExpression)
FunctionTypeExpressionFunctionTypeExpression372   FunctionTypeExpression(SourcePosition pos,
373                          std::vector<TypeExpression*> parameters,
374                          TypeExpression* return_type)
375       : TypeExpression(kKind, pos),
376         parameters(parameters),
377         return_type(return_type) {}
378   std::vector<TypeExpression*> parameters;
379   TypeExpression* return_type;
380 };
381 
382 struct UnionTypeExpression : TypeExpression {
383   DEFINE_AST_NODE_LEAF_BOILERPLATE(UnionTypeExpression)
UnionTypeExpressionUnionTypeExpression384   UnionTypeExpression(SourcePosition pos, TypeExpression* a, TypeExpression* b)
385       : TypeExpression(kKind, pos), a(a), b(b) {}
386   TypeExpression* a;
387   TypeExpression* b;
388 };
389 
390 struct ExpressionStatement : Statement {
391   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExpressionStatement)
ExpressionStatementExpressionStatement392   ExpressionStatement(SourcePosition pos, Expression* expression)
393       : Statement(kKind, pos), expression(expression) {}
394   Expression* expression;
395 };
396 
397 struct IfStatement : Statement {
398   DEFINE_AST_NODE_LEAF_BOILERPLATE(IfStatement)
IfStatementIfStatement399   IfStatement(SourcePosition pos, bool is_constexpr, Expression* condition,
400               Statement* if_true, base::Optional<Statement*> if_false)
401       : Statement(kKind, pos),
402         condition(condition),
403         is_constexpr(is_constexpr),
404         if_true(if_true),
405         if_false(if_false) {}
406   Expression* condition;
407   bool is_constexpr;
408   Statement* if_true;
409   base::Optional<Statement*> if_false;
410 };
411 
412 struct WhileStatement : Statement {
413   DEFINE_AST_NODE_LEAF_BOILERPLATE(WhileStatement)
WhileStatementWhileStatement414   WhileStatement(SourcePosition pos, Expression* condition, Statement* body)
415       : Statement(kKind, pos), condition(condition), body(body) {}
416   Expression* condition;
417   Statement* body;
418 };
419 
420 struct ReturnStatement : Statement {
421   DEFINE_AST_NODE_LEAF_BOILERPLATE(ReturnStatement)
ReturnStatementReturnStatement422   ReturnStatement(SourcePosition pos, base::Optional<Expression*> value)
423       : Statement(kKind, pos), value(value) {}
424   base::Optional<Expression*> value;
425 };
426 
427 struct DebugStatement : Statement {
428   DEFINE_AST_NODE_LEAF_BOILERPLATE(DebugStatement)
DebugStatementDebugStatement429   DebugStatement(SourcePosition pos, const std::string& reason,
430                  bool never_continues)
431       : Statement(kKind, pos),
432         reason(reason),
433         never_continues(never_continues) {}
434   std::string reason;
435   bool never_continues;
436 };
437 
438 struct AssertStatement : Statement {
439   DEFINE_AST_NODE_LEAF_BOILERPLATE(AssertStatement)
AssertStatementAssertStatement440   AssertStatement(SourcePosition pos, bool debug_only, Expression* expression,
441                   std::string source)
442       : Statement(kKind, pos),
443         debug_only(debug_only),
444         expression(expression),
445         source(std::move(source)) {}
446   bool debug_only;
447   Expression* expression;
448   std::string source;
449 };
450 
451 struct TailCallStatement : Statement {
452   DEFINE_AST_NODE_LEAF_BOILERPLATE(TailCallStatement)
TailCallStatementTailCallStatement453   TailCallStatement(SourcePosition pos, CallExpression* call)
454       : Statement(kKind, pos), call(call) {}
455   CallExpression* call;
456 };
457 
458 struct VarDeclarationStatement : Statement {
459   DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
460   VarDeclarationStatement(
461       SourcePosition pos, bool const_qualified, std::string name,
462       base::Optional<TypeExpression*> type,
463       base::Optional<Expression*> initializer = base::nullopt)
StatementVarDeclarationStatement464       : Statement(kKind, pos),
465         const_qualified(const_qualified),
466         name(std::move(name)),
467         type(type),
468         initializer(initializer) {}
469   bool const_qualified;
470   std::string name;
471   base::Optional<TypeExpression*> type;
472   base::Optional<Expression*> initializer;
473 };
474 
475 struct BreakStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEBreakStatement476   DEFINE_AST_NODE_LEAF_BOILERPLATE(BreakStatement)
477   explicit BreakStatement(SourcePosition pos) : Statement(kKind, pos) {}
478 };
479 
480 struct ContinueStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEContinueStatement481   DEFINE_AST_NODE_LEAF_BOILERPLATE(ContinueStatement)
482   explicit ContinueStatement(SourcePosition pos) : Statement(kKind, pos) {}
483 };
484 
485 struct GotoStatement : Statement {
486   DEFINE_AST_NODE_LEAF_BOILERPLATE(GotoStatement)
GotoStatementGotoStatement487   GotoStatement(SourcePosition pos, std::string label,
488                 const std::vector<Expression*>& arguments)
489       : Statement(kKind, pos),
490         label(std::move(label)),
491         arguments(std::move(arguments)) {}
492   std::string label;
493   std::vector<Expression*> arguments;
494 };
495 
496 struct ForLoopStatement : Statement {
497   DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
ForLoopStatementForLoopStatement498   ForLoopStatement(SourcePosition pos, base::Optional<Statement*> declaration,
499                    base::Optional<Expression*> test,
500                    base::Optional<Expression*> action, Statement* body)
501       : Statement(kKind, pos),
502         var_declaration(),
503         test(std::move(test)),
504         action(std::move(action)),
505         body(std::move(body)) {
506     if (declaration)
507       var_declaration = VarDeclarationStatement::cast(*declaration);
508   }
509   base::Optional<VarDeclarationStatement*> var_declaration;
510   base::Optional<Expression*> test;
511   base::Optional<Expression*> action;
512   Statement* body;
513 };
514 
515 struct RangeExpression {
516   base::Optional<Expression*> begin;
517   base::Optional<Expression*> end;
518 };
519 
520 struct ForOfLoopStatement : Statement {
521   DEFINE_AST_NODE_LEAF_BOILERPLATE(ForOfLoopStatement)
ForOfLoopStatementForOfLoopStatement522   ForOfLoopStatement(SourcePosition pos, Statement* decl, Expression* iterable,
523                      base::Optional<RangeExpression> range, Statement* body)
524       : Statement(kKind, pos),
525         var_declaration(VarDeclarationStatement::cast(decl)),
526         iterable(iterable),
527         body(body) {
528     if (range) {
529       begin = range->begin;
530       end = range->end;
531     }
532   }
533   VarDeclarationStatement* var_declaration;
534   Expression* iterable;
535   base::Optional<Expression*> begin;
536   base::Optional<Expression*> end;
537   Statement* body;
538 };
539 
540 struct LabelBlock : AstNode {
541   DEFINE_AST_NODE_LEAF_BOILERPLATE(LabelBlock)
LabelBlockLabelBlock542   LabelBlock(SourcePosition pos, const std::string& label,
543              const ParameterList& parameters, Statement* body)
544       : AstNode(kKind, pos),
545         label(std::move(label)),
546         parameters(parameters),
547         body(std::move(body)) {}
548   std::string label;
549   ParameterList parameters;
550   Statement* body;
551 };
552 
553 struct TryLabelStatement : Statement {
554   DEFINE_AST_NODE_LEAF_BOILERPLATE(TryLabelStatement)
TryLabelStatementTryLabelStatement555   TryLabelStatement(SourcePosition pos, Statement* try_block,
556                     std::vector<LabelBlock*> label_blocks)
557       : Statement(kKind, pos),
558         try_block(try_block),
559         label_blocks(std::move(label_blocks)) {}
560   Statement* try_block;
561   std::vector<LabelBlock*> label_blocks;
562 };
563 
564 struct BlockStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATEBlockStatement565   DEFINE_AST_NODE_LEAF_BOILERPLATE(BlockStatement)
566   explicit BlockStatement(SourcePosition pos, bool deferred = false,
567                           std::vector<Statement*> statements = {})
568       : Statement(kKind, pos),
569         deferred(deferred),
570         statements(std::move(statements)) {}
571   bool deferred;
572   std::vector<Statement*> statements;
573 };
574 
575 struct TypeDeclaration : Declaration {
576   DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeDeclaration)
TypeDeclarationTypeDeclaration577   TypeDeclaration(SourcePosition pos, std::string name,
578                   base::Optional<std::string> extends,
579                   base::Optional<std::string> generates,
580                   base::Optional<std::string> constexpr_generates)
581       : Declaration(kKind, pos),
582         name(std::move(name)),
583         extends(std::move(extends)),
584         generates(std::move(generates)),
585         constexpr_generates(std::move(constexpr_generates)) {}
586   std::string name;
587   base::Optional<std::string> extends;
588   base::Optional<std::string> generates;
589   base::Optional<std::string> constexpr_generates;
590 };
591 
592 struct TypeAliasDeclaration : Declaration {
593   DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
TypeAliasDeclarationTypeAliasDeclaration594   TypeAliasDeclaration(SourcePosition pos, std::string name,
595                        TypeExpression* type)
596       : Declaration(kKind, pos), name(std::move(name)), type(type) {}
597   std::string name;
598   TypeExpression* type;
599 };
600 
601 struct NameAndTypeExpression {
602   std::string name;
603   TypeExpression* type;
604 };
605 
606 struct LabelAndTypes {
607   std::string name;
608   std::vector<TypeExpression*> types;
609 };
610 
611 typedef std::vector<LabelAndTypes> LabelAndTypesVector;
612 
613 struct CallableNodeSignature {
614   ParameterList parameters;
615   TypeExpression* return_type;
616   LabelAndTypesVector labels;
617 };
618 
619 struct CallableNode : AstNode {
CallableNodeCallableNode620   CallableNode(AstNode::Kind kind, SourcePosition pos, std::string name,
621                ParameterList parameters, TypeExpression* return_type,
622                const LabelAndTypesVector& labels)
623       : AstNode(kind, pos),
624         name(std::move(name)),
625         signature(new CallableNodeSignature{parameters, return_type, labels}) {}
626   DEFINE_AST_NODE_INNER_BOILERPLATE(CallableNode)
627   std::string name;
628   std::unique_ptr<CallableNodeSignature> signature;
629 };
630 
631 struct MacroDeclaration : CallableNode {
632   DEFINE_AST_NODE_INNER_BOILERPLATE(MacroDeclaration)
MacroDeclarationMacroDeclaration633   MacroDeclaration(AstNode::Kind kind, SourcePosition pos, std::string name,
634                    base::Optional<std::string> op, ParameterList parameters,
635                    TypeExpression* return_type,
636                    const LabelAndTypesVector& labels)
637       : CallableNode(kind, pos, name, parameters, return_type, labels),
638         op(std::move(op)) {}
639   base::Optional<std::string> op;
640 };
641 
642 struct ExternalMacroDeclaration : MacroDeclaration {
643   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalMacroDeclaration)
ExternalMacroDeclarationExternalMacroDeclaration644   ExternalMacroDeclaration(SourcePosition pos, std::string name,
645                            base::Optional<std::string> op,
646                            ParameterList parameters,
647                            TypeExpression* return_type,
648                            const LabelAndTypesVector& labels)
649       : MacroDeclaration(kKind, pos, name, op, parameters, return_type,
650                          labels) {}
651 };
652 
653 struct TorqueMacroDeclaration : MacroDeclaration {
654   DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueMacroDeclaration)
TorqueMacroDeclarationTorqueMacroDeclaration655   TorqueMacroDeclaration(SourcePosition pos, std::string name,
656                          base::Optional<std::string> op,
657                          ParameterList parameters, TypeExpression* return_type,
658                          const LabelAndTypesVector& labels)
659       : MacroDeclaration(kKind, pos, name, op, parameters, return_type,
660                          labels) {}
661 };
662 
663 struct BuiltinDeclaration : CallableNode {
BuiltinDeclarationBuiltinDeclaration664   BuiltinDeclaration(AstNode::Kind kind, SourcePosition pos,
665                      bool javascript_linkage, std::string name,
666                      ParameterList parameters, TypeExpression* return_type)
667       : CallableNode(kind, pos, name, parameters, return_type, {}),
668         javascript_linkage(javascript_linkage) {}
669   bool javascript_linkage;
670 };
671 
672 struct ExternalBuiltinDeclaration : BuiltinDeclaration {
673   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalBuiltinDeclaration)
ExternalBuiltinDeclarationExternalBuiltinDeclaration674   ExternalBuiltinDeclaration(SourcePosition pos, bool javascript_linkage,
675                              std::string name, ParameterList parameters,
676                              TypeExpression* return_type)
677       : BuiltinDeclaration(kKind, pos, javascript_linkage, name, parameters,
678                            return_type) {}
679 };
680 
681 struct TorqueBuiltinDeclaration : BuiltinDeclaration {
682   DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueBuiltinDeclaration)
TorqueBuiltinDeclarationTorqueBuiltinDeclaration683   TorqueBuiltinDeclaration(SourcePosition pos, bool javascript_linkage,
684                            std::string name, ParameterList parameters,
685                            TypeExpression* return_type)
686       : BuiltinDeclaration(kKind, pos, javascript_linkage, name, parameters,
687                            return_type) {}
688 };
689 
690 struct ExternalRuntimeDeclaration : CallableNode {
691   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalRuntimeDeclaration)
ExternalRuntimeDeclarationExternalRuntimeDeclaration692   ExternalRuntimeDeclaration(SourcePosition pos, std::string name,
693                              ParameterList parameters,
694                              TypeExpression* return_type)
695       : CallableNode(kKind, pos, name, parameters, return_type, {}) {}
696 };
697 
698 struct ConstDeclaration : Declaration {
699   DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
ConstDeclarationConstDeclaration700   ConstDeclaration(SourcePosition pos, std::string name, TypeExpression* type,
701                    Expression* expression)
702       : Declaration(kKind, pos),
703         name(std::move(name)),
704         type(type),
705         expression(expression) {}
706   std::string name;
707   TypeExpression* type;
708   Expression* expression;
709 };
710 
711 struct StandardDeclaration : Declaration {
712   DEFINE_AST_NODE_LEAF_BOILERPLATE(StandardDeclaration)
StandardDeclarationStandardDeclaration713   StandardDeclaration(SourcePosition pos, CallableNode* callable,
714                       Statement* body)
715       : Declaration(kKind, pos), callable(callable), body(body) {}
716   CallableNode* callable;
717   Statement* body;
718 };
719 
720 struct GenericDeclaration : Declaration {
721   DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericDeclaration)
722   GenericDeclaration(SourcePosition pos, CallableNode* callable,
723                      std::vector<std::string> generic_parameters,
724                      base::Optional<Statement*> body = base::nullopt)
DeclarationGenericDeclaration725       : Declaration(kKind, pos),
726         callable(callable),
727         generic_parameters(std::move(generic_parameters)),
728         body(body) {}
729   CallableNode* callable;
730   std::vector<std::string> generic_parameters;
731   base::Optional<Statement*> body;
732 };
733 
734 struct SpecializationDeclaration : Declaration {
735   DEFINE_AST_NODE_LEAF_BOILERPLATE(SpecializationDeclaration)
SpecializationDeclarationSpecializationDeclaration736   SpecializationDeclaration(SourcePosition pos, std::string name,
737                             std::vector<TypeExpression*> generic_parameters,
738                             ParameterList parameters,
739                             TypeExpression* return_type,
740                             LabelAndTypesVector labels, Statement* b)
741       : Declaration(kKind, pos),
742         name(std::move(name)),
743         external(false),
744         generic_parameters(generic_parameters),
745         signature(new CallableNodeSignature{parameters, return_type, labels}),
746         body(b) {}
747   std::string name;
748   bool external;
749   std::vector<TypeExpression*> generic_parameters;
750   std::unique_ptr<CallableNodeSignature> signature;
751   Statement* body;
752 };
753 
754 struct ExternConstDeclaration : Declaration {
755   DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
ExternConstDeclarationExternConstDeclaration756   ExternConstDeclaration(SourcePosition pos, std::string name,
757                          TypeExpression* type, std::string literal)
758       : Declaration(kKind, pos),
759         name(std::move(name)),
760         type(type),
761         literal(std::move(literal)) {}
762   std::string name;
763   TypeExpression* type;
764   std::string literal;
765 };
766 
767 struct StructDeclaration : Declaration {
768   DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
StructDeclarationStructDeclaration769   StructDeclaration(SourcePosition pos, std::string name,
770                     std::vector<NameAndTypeExpression> fields)
771       : Declaration(kKind, pos),
772         name(std::move(name)),
773         fields(std::move(fields)) {}
774   std::string name;
775   std::vector<NameAndTypeExpression> fields;
776 };
777 
778 #define ENUM_ITEM(name)                     \
779   case AstNode::Kind::k##name:              \
780     return std::is_base_of<T, name>::value; \
781     break;
782 
783 template <class T>
IsInstanceOf(AstNode * node)784 bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
785   switch (node->kind) {
786     AST_NODE_KIND_LIST(ENUM_ITEM)
787     default:
788       UNIMPLEMENTED();
789   }
790   return true;
791 }
792 
793 #undef ENUM_ITEM
794 
795 }  // namespace torque
796 }  // namespace internal
797 }  // namespace v8
798 
799 #endif  // V8_TORQUE_AST_H_
800