V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
ast.h
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(IntrinsicCallExpression) \
23  V(StructExpression) \
24  V(LogicalOrExpression) \
25  V(LogicalAndExpression) \
26  V(ConditionalExpression) \
27  V(IdentifierExpression) \
28  V(StringLiteralExpression) \
29  V(NumberLiteralExpression) \
30  V(FieldAccessExpression) \
31  V(ElementAccessExpression) \
32  V(AssignmentExpression) \
33  V(IncrementDecrementExpression) \
34  V(AssumeTypeImpossibleExpression) \
35  V(StatementExpression) \
36  V(TryLabelExpression)
37 
38 #define AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
39  V(BasicTypeExpression) \
40  V(FunctionTypeExpression) \
41  V(UnionTypeExpression)
42 
43 #define AST_STATEMENT_NODE_KIND_LIST(V) \
44  V(BlockStatement) \
45  V(ExpressionStatement) \
46  V(IfStatement) \
47  V(WhileStatement) \
48  V(ForLoopStatement) \
49  V(ForOfLoopStatement) \
50  V(BreakStatement) \
51  V(ContinueStatement) \
52  V(ReturnStatement) \
53  V(DebugStatement) \
54  V(AssertStatement) \
55  V(TailCallStatement) \
56  V(VarDeclarationStatement) \
57  V(GotoStatement)
58 
59 #define AST_DECLARATION_NODE_KIND_LIST(V) \
60  V(TypeDeclaration) \
61  V(TypeAliasDeclaration) \
62  V(StandardDeclaration) \
63  V(GenericDeclaration) \
64  V(SpecializationDeclaration) \
65  V(ExternConstDeclaration) \
66  V(StructDeclaration) \
67  V(NamespaceDeclaration) \
68  V(ConstDeclaration)
69 
70 #define AST_CALLABLE_NODE_KIND_LIST(V) \
71  V(TorqueMacroDeclaration) \
72  V(TorqueBuiltinDeclaration) \
73  V(ExternalMacroDeclaration) \
74  V(ExternalBuiltinDeclaration) \
75  V(ExternalRuntimeDeclaration) \
76  V(IntrinsicDeclaration)
77 
78 #define AST_NODE_KIND_LIST(V) \
79  AST_EXPRESSION_NODE_KIND_LIST(V) \
80  AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
81  AST_STATEMENT_NODE_KIND_LIST(V) \
82  AST_DECLARATION_NODE_KIND_LIST(V) \
83  AST_CALLABLE_NODE_KIND_LIST(V) \
84  V(LabelBlock)
85 
86 struct AstNode {
87  public:
88  enum class Kind {
89 #define ENUM_ITEM(name) k##name,
90  AST_NODE_KIND_LIST(ENUM_ITEM)
91 #undef ENUM_ITEM
92  };
93 
94  AstNode(Kind kind, SourcePosition pos) : kind(kind), pos(pos) {}
95  virtual ~AstNode() = default;
96 
97  const Kind kind;
98  SourcePosition pos;
99 };
100 
102  template <class T>
103  static bool IsInstanceOf(AstNode* node);
104 };
105 
106 // Boilerplate for most derived classes.
107 #define DEFINE_AST_NODE_LEAF_BOILERPLATE(T) \
108  static const Kind kKind = Kind::k##T; \
109  static T* cast(AstNode* node) { \
110  if (node->kind != kKind) return nullptr; \
111  return static_cast<T*>(node); \
112  } \
113  static T* DynamicCast(AstNode* node) { \
114  if (!node) return nullptr; \
115  if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
116  return static_cast<T*>(node); \
117  }
118 
119 // Boilerplate for classes with subclasses.
120 #define DEFINE_AST_NODE_INNER_BOILERPLATE(T) \
121  static T* cast(AstNode* node) { \
122  DCHECK(AstNodeClassCheck::IsInstanceOf<T>(node)); \
123  return static_cast<T*>(node); \
124  } \
125  static T* DynamicCast(AstNode* node) { \
126  if (!node) return nullptr; \
127  if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
128  return static_cast<T*>(node); \
129  }
130 
131 struct Expression : AstNode {
132  Expression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
133  DEFINE_AST_NODE_INNER_BOILERPLATE(Expression)
134 };
135 
137  LocationExpression(Kind kind, SourcePosition pos) : Expression(kind, pos) {}
138  DEFINE_AST_NODE_INNER_BOILERPLATE(LocationExpression)
139 };
140 
142  TypeExpression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
143  DEFINE_AST_NODE_INNER_BOILERPLATE(TypeExpression)
144 };
145 
147  Declaration(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
148  DEFINE_AST_NODE_INNER_BOILERPLATE(Declaration)
149 };
150 
151 struct Statement : AstNode {
152  Statement(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
153  DEFINE_AST_NODE_INNER_BOILERPLATE(Statement)
154 };
155 
156 class Namespace;
157 
159  DEFINE_AST_NODE_LEAF_BOILERPLATE(NamespaceDeclaration)
160  NamespaceDeclaration(SourcePosition pos, std::string name,
161  std::vector<Declaration*> declarations)
162  : Declaration(kKind, pos),
163  declarations(std::move(declarations)),
164  name(name) {}
165  std::vector<Declaration*> declarations;
166  std::string name;
167 };
168 
169 class Ast {
170  public:
171  Ast() {}
172 
173  std::vector<Declaration*>& declarations() { return declarations_; }
174  const std::vector<Declaration*>& declarations() const {
175  return declarations_;
176  }
177  template <class T>
178  T* AddNode(std::unique_ptr<T> node) {
179  T* result = node.get();
180  nodes_.push_back(std::move(node));
181  return result;
182  }
183 
184  private:
185  std::vector<Declaration*> declarations_;
186  std::vector<std::unique_ptr<AstNode>> nodes_;
187 };
188 
190  DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
192  std::vector<std::string> namespace_qualification,
193  std::string name, std::vector<TypeExpression*> args = {})
194  : LocationExpression(kKind, pos),
195  namespace_qualification(std::move(namespace_qualification)),
196  name(std::move(name)),
197  generic_arguments(std::move(args)) {}
198  IdentifierExpression(SourcePosition pos, std::string name,
199  std::vector<TypeExpression*> args = {})
200  : IdentifierExpression(pos, {}, std::move(name), std::move(args)) {}
201  std::vector<std::string> namespace_qualification;
202  std::string name;
203  std::vector<TypeExpression*> generic_arguments;
204 };
205 
207  DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicCallExpression)
208  IntrinsicCallExpression(SourcePosition pos, std::string name,
209  std::vector<TypeExpression*> generic_arguments,
210  std::vector<Expression*> arguments)
211  : Expression(kKind, pos),
212  name(std::move(name)),
213  generic_arguments(std::move(generic_arguments)),
214  arguments(std::move(arguments)) {}
215  std::string name;
216  std::vector<TypeExpression*> generic_arguments;
217  std::vector<Expression*> arguments;
218 };
219 
221  DEFINE_AST_NODE_LEAF_BOILERPLATE(CallExpression)
223  std::vector<Expression*> arguments,
224  std::vector<std::string> labels)
225  : Expression(kKind, pos),
226  callee(callee),
227  arguments(std::move(arguments)),
228  labels(std::move(labels)) {}
229  IdentifierExpression* callee;
230  std::vector<Expression*> arguments;
231  std::vector<std::string> labels;
232 };
233 
235  DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
237  std::vector<std::string> namespace_qualification,
238  std::string name, std::vector<Expression*> expressions)
239  : Expression(kKind, pos),
240  namespace_qualification(std::move(namespace_qualification)),
241  name(std::move(name)),
242  expressions(std::move(expressions)) {}
243  std::vector<std::string> namespace_qualification;
244  std::string name;
245  std::vector<Expression*> expressions;
246 };
247 
249  DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalOrExpression)
251  : Expression(kKind, pos), left(left), right(right) {}
252  Expression* left;
253  Expression* right;
254 };
255 
257  DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalAndExpression)
259  : Expression(kKind, pos), left(left), right(right) {}
260  Expression* left;
261  Expression* right;
262 };
263 
265  DEFINE_AST_NODE_LEAF_BOILERPLATE(ConditionalExpression)
267  Expression* if_true, Expression* if_false)
268  : Expression(kKind, pos),
269  condition(condition),
270  if_true(if_true),
271  if_false(if_false) {}
272  Expression* condition;
273  Expression* if_true;
274  Expression* if_false;
275 };
276 
278  DEFINE_AST_NODE_LEAF_BOILERPLATE(StringLiteralExpression)
279  StringLiteralExpression(SourcePosition pos, std::string literal)
280  : Expression(kKind, pos), literal(std::move(literal)) {}
281  std::string literal;
282 };
283 
285  DEFINE_AST_NODE_LEAF_BOILERPLATE(NumberLiteralExpression)
286  NumberLiteralExpression(SourcePosition pos, std::string name)
287  : Expression(kKind, pos), number(std::move(name)) {}
288  std::string number;
289 };
290 
292  DEFINE_AST_NODE_LEAF_BOILERPLATE(ElementAccessExpression)
294  Expression* index)
295  : LocationExpression(kKind, pos), array(array), index(index) {}
296  Expression* array;
297  Expression* index;
298 };
299 
301  DEFINE_AST_NODE_LEAF_BOILERPLATE(FieldAccessExpression)
303  std::string field)
304  : LocationExpression(kKind, pos),
305  object(object),
306  field(std::move(field)) {}
307  Expression* object;
308  std::string field;
309 };
310 
312  DEFINE_AST_NODE_LEAF_BOILERPLATE(AssignmentExpression)
315  : Expression(kKind, pos),
316  location(location),
317  op(std::move(op)),
318  value(value) {}
319  LocationExpression* location;
321  Expression* value;
322 };
323 
324 enum class IncrementDecrementOperator { kIncrement, kDecrement };
325 
327  DEFINE_AST_NODE_LEAF_BOILERPLATE(IncrementDecrementExpression)
329  IncrementDecrementOperator op, bool postfix)
330  : Expression(kKind, pos), location(location), op(op), postfix(postfix) {}
331  LocationExpression* location;
332  IncrementDecrementOperator op;
333  bool postfix;
334 };
335 
336 // This expression is only used in the desugaring of typeswitch, and it allows
337 // to bake in the static information that certain types are impossible at a
338 // certain position in the control flow.
339 // The result type is the type of {expression} minus the provided type.
341  DEFINE_AST_NODE_LEAF_BOILERPLATE(AssumeTypeImpossibleExpression)
343  TypeExpression* excluded_type,
344  Expression* expression)
345  : Expression(kKind, pos),
346  excluded_type(excluded_type),
347  expression(expression) {}
348  TypeExpression* excluded_type;
349  Expression* expression;
350 };
351 
353  std::vector<std::string> names;
354  std::vector<TypeExpression*> types;
355  size_t implicit_count;
356  bool has_varargs;
357  std::string arguments_variable;
358 
359  static ParameterList Empty() { return ParameterList{{}, {}, 0, false, ""}; }
360  std::vector<TypeExpression*> GetImplicitTypes() {
361  return std::vector<TypeExpression*>(types.begin(),
362  types.begin() + implicit_count);
363  }
364  std::vector<TypeExpression*> GetExplicitTypes() {
365  return std::vector<TypeExpression*>(types.begin() + implicit_count,
366  types.end());
367  }
368 };
369 
371  DEFINE_AST_NODE_LEAF_BOILERPLATE(BasicTypeExpression)
373  std::vector<std::string> namespace_qualification,
374  bool is_constexpr, std::string name)
375  : TypeExpression(kKind, pos),
376  namespace_qualification(std::move(namespace_qualification)),
377  is_constexpr(is_constexpr),
378  name(std::move(name)) {}
379  std::vector<std::string> namespace_qualification;
380  bool is_constexpr;
381  std::string name;
382 };
383 
385  DEFINE_AST_NODE_LEAF_BOILERPLATE(FunctionTypeExpression)
387  std::vector<TypeExpression*> parameters,
388  TypeExpression* return_type)
389  : TypeExpression(kKind, pos),
390  parameters(std::move(parameters)),
391  return_type(return_type) {}
392  std::vector<TypeExpression*> parameters;
393  TypeExpression* return_type;
394 };
395 
397  DEFINE_AST_NODE_LEAF_BOILERPLATE(UnionTypeExpression)
399  : TypeExpression(kKind, pos), a(a), b(b) {}
400  TypeExpression* a;
401  TypeExpression* b;
402 };
403 
405  DEFINE_AST_NODE_LEAF_BOILERPLATE(ExpressionStatement)
407  : Statement(kKind, pos), expression(expression) {}
408  Expression* expression;
409 };
410 
412  DEFINE_AST_NODE_LEAF_BOILERPLATE(IfStatement)
413  IfStatement(SourcePosition pos, bool is_constexpr, Expression* condition,
414  Statement* if_true, base::Optional<Statement*> if_false)
415  : Statement(kKind, pos),
416  condition(condition),
417  is_constexpr(is_constexpr),
418  if_true(if_true),
419  if_false(if_false) {}
420  Expression* condition;
421  bool is_constexpr;
422  Statement* if_true;
424 };
425 
427  DEFINE_AST_NODE_LEAF_BOILERPLATE(WhileStatement)
428  WhileStatement(SourcePosition pos, Expression* condition, Statement* body)
429  : Statement(kKind, pos), condition(condition), body(body) {}
430  Expression* condition;
431  Statement* body;
432 };
433 
435  DEFINE_AST_NODE_LEAF_BOILERPLATE(ReturnStatement)
437  : Statement(kKind, pos), value(value) {}
439 };
440 
442  DEFINE_AST_NODE_LEAF_BOILERPLATE(DebugStatement)
443  DebugStatement(SourcePosition pos, const std::string& reason,
444  bool never_continues)
445  : Statement(kKind, pos),
446  reason(reason),
447  never_continues(never_continues) {}
448  std::string reason;
449  bool never_continues;
450 };
451 
453  DEFINE_AST_NODE_LEAF_BOILERPLATE(AssertStatement)
454  AssertStatement(SourcePosition pos, bool debug_only, Expression* expression,
455  std::string source)
456  : Statement(kKind, pos),
457  debug_only(debug_only),
458  expression(expression),
459  source(std::move(source)) {}
460  bool debug_only;
461  Expression* expression;
462  std::string source;
463 };
464 
466  DEFINE_AST_NODE_LEAF_BOILERPLATE(TailCallStatement)
468  : Statement(kKind, pos), call(call) {}
469  CallExpression* call;
470 };
471 
473  DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
475  SourcePosition pos, bool const_qualified, std::string name,
477  base::Optional<Expression*> initializer = base::nullopt)
478  : Statement(kKind, pos),
479  const_qualified(const_qualified),
480  name(std::move(name)),
481  type(type),
482  initializer(initializer) {}
483  bool const_qualified;
484  std::string name;
486  base::Optional<Expression*> initializer;
487 };
488 
490  DEFINE_AST_NODE_LEAF_BOILERPLATE(BreakStatement)
491  explicit BreakStatement(SourcePosition pos) : Statement(kKind, pos) {}
492 };
493 
495  DEFINE_AST_NODE_LEAF_BOILERPLATE(ContinueStatement)
496  explicit ContinueStatement(SourcePosition pos) : Statement(kKind, pos) {}
497 };
498 
500  DEFINE_AST_NODE_LEAF_BOILERPLATE(GotoStatement)
501  GotoStatement(SourcePosition pos, std::string label,
502  const std::vector<Expression*>& arguments)
503  : Statement(kKind, pos),
504  label(std::move(label)),
505  arguments(std::move(arguments)) {}
506  std::string label;
507  std::vector<Expression*> arguments;
508 };
509 
511  DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
515  : Statement(kKind, pos),
516  var_declaration(),
517  test(std::move(test)),
518  action(std::move(action)),
519  body(std::move(body)) {
520  if (declaration)
521  var_declaration = VarDeclarationStatement::cast(*declaration);
522  }
526  Statement* body;
527 };
528 
532 };
533 
535  DEFINE_AST_NODE_LEAF_BOILERPLATE(ForOfLoopStatement)
538  : Statement(kKind, pos),
539  var_declaration(VarDeclarationStatement::cast(decl)),
540  iterable(iterable),
541  body(body) {
542  if (range) {
543  begin = range->begin;
544  end = range->end;
545  }
546  }
547  VarDeclarationStatement* var_declaration;
548  Expression* iterable;
551  Statement* body;
552 };
553 
554 struct LabelBlock : AstNode {
555  DEFINE_AST_NODE_LEAF_BOILERPLATE(LabelBlock)
556  LabelBlock(SourcePosition pos, std::string label,
557  const ParameterList& parameters, Statement* body)
558  : AstNode(kKind, pos),
559  label(std::move(label)),
560  parameters(parameters),
561  body(std::move(body)) {}
562  std::string label;
563  ParameterList parameters;
564  Statement* body;
565 };
566 
568  DEFINE_AST_NODE_LEAF_BOILERPLATE(StatementExpression)
570  : Expression(kKind, pos), statement(statement) {}
571  Statement* statement;
572 };
573 
575  DEFINE_AST_NODE_LEAF_BOILERPLATE(TryLabelExpression)
576  TryLabelExpression(SourcePosition pos, bool catch_exceptions,
577  Expression* try_expression, LabelBlock* label_block)
578  : Expression(kKind, pos),
579  catch_exceptions(catch_exceptions),
580  try_expression(try_expression),
581  label_block(label_block) {}
582  bool catch_exceptions;
583  Expression* try_expression;
584  LabelBlock* label_block;
585 };
586 
588  DEFINE_AST_NODE_LEAF_BOILERPLATE(BlockStatement)
589  explicit BlockStatement(SourcePosition pos, bool deferred = false,
590  std::vector<Statement*> statements = {})
591  : Statement(kKind, pos),
592  deferred(deferred),
593  statements(std::move(statements)) {}
594  bool deferred;
595  std::vector<Statement*> statements;
596 };
597 
599  DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeDeclaration)
600  TypeDeclaration(SourcePosition pos, std::string name, bool transient,
602  base::Optional<std::string> generates,
603  base::Optional<std::string> constexpr_generates)
604  : Declaration(kKind, pos),
605  name(std::move(name)),
606  transient(transient),
607  extends(std::move(extends)),
608  generates(std::move(generates)),
609  constexpr_generates(std::move(constexpr_generates)) {}
610  std::string name;
611  bool transient;
613  base::Optional<std::string> generates;
614  base::Optional<std::string> constexpr_generates;
615 };
616 
618  DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
619  TypeAliasDeclaration(SourcePosition pos, std::string name,
621  : Declaration(kKind, pos), name(std::move(name)), type(type) {}
622  std::string name;
624 };
625 
627  std::string name;
629 };
630 
632  std::string name;
633  std::vector<TypeExpression*> types;
634 };
635 
636 typedef std::vector<LabelAndTypes> LabelAndTypesVector;
637 
639  ParameterList parameters;
640  TypeExpression* return_type;
641  LabelAndTypesVector labels;
642 };
643 
645  CallableNode(AstNode::Kind kind, SourcePosition pos, bool transitioning,
646  std::string name, ParameterList parameters,
647  TypeExpression* return_type, const LabelAndTypesVector& labels)
648  : AstNode(kind, pos),
649  transitioning(transitioning),
650  name(std::move(name)),
651  signature(new CallableNodeSignature{parameters, return_type, labels}) {}
652  DEFINE_AST_NODE_INNER_BOILERPLATE(CallableNode)
653  bool transitioning;
654  std::string name;
655  std::unique_ptr<CallableNodeSignature> signature;
656 };
657 
659  DEFINE_AST_NODE_INNER_BOILERPLATE(MacroDeclaration)
660  MacroDeclaration(AstNode::Kind kind, SourcePosition pos, bool transitioning,
661  std::string name, base::Optional<std::string> op,
662  ParameterList parameters, TypeExpression* return_type,
663  const LabelAndTypesVector& labels)
664  : CallableNode(kind, pos, transitioning, std::move(name),
665  std::move(parameters), return_type, labels),
666  op(std::move(op)) {}
668 };
669 
671  DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalMacroDeclaration)
672  ExternalMacroDeclaration(SourcePosition pos, bool transitioning,
673  std::string external_assembler_name,
674  std::string name, base::Optional<std::string> op,
675  ParameterList parameters,
676  TypeExpression* return_type,
677  const LabelAndTypesVector& labels)
678  : MacroDeclaration(kKind, pos, transitioning, std::move(name),
679  std::move(op), std::move(parameters), return_type,
680  labels),
681  external_assembler_name(std::move(external_assembler_name)) {}
682  std::string external_assembler_name;
683 };
684 
686  DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicDeclaration)
687  IntrinsicDeclaration(SourcePosition pos, std::string name,
688  ParameterList parameters, TypeExpression* return_type)
689  : CallableNode(kKind, pos, false, std::move(name), std::move(parameters),
690  return_type, {}) {}
691 };
692 
694  DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueMacroDeclaration)
695  TorqueMacroDeclaration(SourcePosition pos, bool transitioning,
696  std::string name, base::Optional<std::string> op,
697  ParameterList parameters, TypeExpression* return_type,
698  const LabelAndTypesVector& labels)
699  : MacroDeclaration(kKind, pos, transitioning, std::move(name),
700  std::move(op), std::move(parameters), return_type,
701  labels) {}
702 };
703 
705  DEFINE_AST_NODE_INNER_BOILERPLATE(BuiltinDeclaration)
706  BuiltinDeclaration(AstNode::Kind kind, SourcePosition pos,
707  bool javascript_linkage, bool transitioning,
708  std::string name, ParameterList parameters,
709  TypeExpression* return_type)
710  : CallableNode(kind, pos, transitioning, std::move(name),
711  std::move(parameters), return_type, {}),
712  javascript_linkage(javascript_linkage) {}
713  bool javascript_linkage;
714 };
715 
717  DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalBuiltinDeclaration)
718  ExternalBuiltinDeclaration(SourcePosition pos, bool transitioning,
719  bool javascript_linkage, std::string name,
720  ParameterList parameters,
721  TypeExpression* return_type)
722  : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
723  std::move(name), std::move(parameters),
724  return_type) {}
725 };
726 
728  DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueBuiltinDeclaration)
729  TorqueBuiltinDeclaration(SourcePosition pos, bool transitioning,
730  bool javascript_linkage, std::string name,
731  ParameterList parameters,
732  TypeExpression* return_type)
733  : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
734  std::move(name), std::move(parameters),
735  return_type) {}
736 };
737 
739  DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalRuntimeDeclaration)
740  ExternalRuntimeDeclaration(SourcePosition pos, bool transitioning,
741  std::string name, ParameterList parameters,
742  TypeExpression* return_type)
743  : CallableNode(kKind, pos, transitioning, name, parameters, return_type,
744  {}) {}
745 };
746 
748  DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
749  ConstDeclaration(SourcePosition pos, std::string name, TypeExpression* type,
750  Expression* expression)
751  : Declaration(kKind, pos),
752  name(std::move(name)),
753  type(type),
754  expression(expression) {}
755  std::string name;
757  Expression* expression;
758 };
759 
761  DEFINE_AST_NODE_LEAF_BOILERPLATE(StandardDeclaration)
764  : Declaration(kKind, pos), callable(callable), body(body) {}
765  CallableNode* callable;
767 };
768 
770  DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericDeclaration)
772  std::vector<std::string> generic_parameters,
773  base::Optional<Statement*> body = base::nullopt)
774  : Declaration(kKind, pos),
775  callable(callable),
776  generic_parameters(std::move(generic_parameters)),
777  body(body) {}
778  CallableNode* callable;
779  std::vector<std::string> generic_parameters;
781 };
782 
784  DEFINE_AST_NODE_LEAF_BOILERPLATE(SpecializationDeclaration)
785  SpecializationDeclaration(SourcePosition pos, std::string name,
786  std::vector<TypeExpression*> generic_parameters,
787  ParameterList parameters,
788  TypeExpression* return_type,
789  LabelAndTypesVector labels, Statement* b)
790  : Declaration(kKind, pos),
791  name(std::move(name)),
792  external(false),
793  generic_parameters(std::move(generic_parameters)),
794  signature(new CallableNodeSignature{std::move(parameters), return_type,
795  std::move(labels)}),
796  body(b) {}
797  std::string name;
798  bool external;
799  std::vector<TypeExpression*> generic_parameters;
800  std::unique_ptr<CallableNodeSignature> signature;
801  Statement* body;
802 };
803 
805  DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
806  ExternConstDeclaration(SourcePosition pos, std::string name,
807  TypeExpression* type, std::string literal)
808  : Declaration(kKind, pos),
809  name(std::move(name)),
810  type(type),
811  literal(std::move(literal)) {}
812  std::string name;
814  std::string literal;
815 };
816 
818  DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
819  StructDeclaration(SourcePosition pos, std::string name,
820  std::vector<NameAndTypeExpression> fields)
821  : Declaration(kKind, pos),
822  name(std::move(name)),
823  fields(std::move(fields)) {}
824  std::string name;
825  std::vector<NameAndTypeExpression> fields;
826 };
827 
828 #define ENUM_ITEM(name) \
829  case AstNode::Kind::k##name: \
830  return std::is_base_of<T, name>::value; \
831  break;
832 
833 template <class T>
834 bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
835  switch (node->kind) {
836  AST_NODE_KIND_LIST(ENUM_ITEM)
837  default:
838  UNIMPLEMENTED();
839  }
840  return true;
841 }
842 
843 #undef ENUM_ITEM
844 
845 inline bool IsDeferred(Statement* stmt) {
846  if (auto* block = BlockStatement::DynamicCast(stmt)) {
847  return block->deferred;
848  }
849  return false;
850 }
851 
852 } // namespace torque
853 } // namespace internal
854 } // namespace v8
855 
856 #endif // V8_TORQUE_AST_H_
Definition: libplatform.h:13