5 #ifndef V8_TORQUE_AST_H_ 6 #define V8_TORQUE_AST_H_ 13 #include "src/base/optional.h" 14 #include "src/torque/source-positions.h" 20 #define AST_EXPRESSION_NODE_KIND_LIST(V) \ 22 V(IntrinsicCallExpression) \ 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) \ 38 #define AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \ 39 V(BasicTypeExpression) \ 40 V(FunctionTypeExpression) \ 41 V(UnionTypeExpression) 43 #define AST_STATEMENT_NODE_KIND_LIST(V) \ 45 V(ExpressionStatement) \ 49 V(ForOfLoopStatement) \ 51 V(ContinueStatement) \ 55 V(TailCallStatement) \ 56 V(VarDeclarationStatement) \ 59 #define AST_DECLARATION_NODE_KIND_LIST(V) \ 61 V(TypeAliasDeclaration) \ 62 V(StandardDeclaration) \ 63 V(GenericDeclaration) \ 64 V(SpecializationDeclaration) \ 65 V(ExternConstDeclaration) \ 66 V(StructDeclaration) \ 67 V(NamespaceDeclaration) \ 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) 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) \ 89 #define ENUM_ITEM(name) k##name, 90 AST_NODE_KIND_LIST(ENUM_ITEM)
103 static bool IsInstanceOf(
AstNode* node);
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); \ 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); \ 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); \ 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); \ 153 DEFINE_AST_NODE_INNER_BOILERPLATE(
Statement)
161 std::vector<Declaration*> declarations)
163 declarations(std::move(declarations)),
165 std::vector<Declaration*> declarations;
173 std::vector<Declaration*>& declarations() {
return declarations_; }
174 const std::vector<Declaration*>& declarations()
const {
175 return declarations_;
178 T* AddNode(std::unique_ptr<T> node) {
179 T* result = node.get();
180 nodes_.push_back(std::move(node));
185 std::vector<Declaration*> declarations_;
186 std::vector<std::unique_ptr<AstNode>> nodes_;
192 std::vector<std::string> namespace_qualification,
193 std::string name, std::vector<TypeExpression*> args = {})
195 namespace_qualification(std::move(namespace_qualification)),
196 name(std::move(name)),
197 generic_arguments(std::move(args)) {}
199 std::vector<TypeExpression*> args = {})
201 std::vector<std::string> namespace_qualification;
203 std::vector<TypeExpression*> generic_arguments;
209 std::vector<TypeExpression*> generic_arguments,
210 std::vector<Expression*> arguments)
212 name(std::move(name)),
213 generic_arguments(std::move(generic_arguments)),
214 arguments(std::move(arguments)) {}
216 std::vector<TypeExpression*> generic_arguments;
217 std::vector<Expression*> arguments;
223 std::vector<Expression*> arguments,
224 std::vector<std::string> labels)
227 arguments(std::move(arguments)),
228 labels(std::move(labels)) {}
230 std::vector<Expression*> arguments;
231 std::vector<std::string> labels;
237 std::vector<std::string> namespace_qualification,
238 std::string name, std::vector<Expression*> expressions)
240 namespace_qualification(std::move(namespace_qualification)),
241 name(std::move(name)),
242 expressions(std::move(expressions)) {}
243 std::vector<std::string> namespace_qualification;
245 std::vector<Expression*> expressions;
251 :
Expression(kKind, pos), left(left), right(right) {}
259 :
Expression(kKind, pos), left(left), right(right) {}
269 condition(condition),
271 if_false(if_false) {}
280 :
Expression(kKind, pos), literal(std::move(literal)) {}
287 :
Expression(kKind, pos), number(std::move(name)) {}
306 field(std::move(field)) {}
324 enum class IncrementDecrementOperator { kIncrement, kDecrement };
329 IncrementDecrementOperator op,
bool postfix)
330 :
Expression(kKind, pos), location(location), op(op), postfix(postfix) {}
332 IncrementDecrementOperator op;
346 excluded_type(excluded_type),
347 expression(expression) {}
353 std::vector<std::string> names;
354 std::vector<TypeExpression*> types;
355 size_t implicit_count;
357 std::string arguments_variable;
360 std::vector<TypeExpression*> GetImplicitTypes() {
361 return std::vector<TypeExpression*>(types.begin(),
362 types.begin() + implicit_count);
364 std::vector<TypeExpression*> GetExplicitTypes() {
365 return std::vector<TypeExpression*>(types.begin() + implicit_count,
373 std::vector<std::string> namespace_qualification,
374 bool is_constexpr, std::string name)
376 namespace_qualification(std::move(namespace_qualification)),
377 is_constexpr(is_constexpr),
378 name(std::move(name)) {}
379 std::vector<std::string> namespace_qualification;
387 std::vector<TypeExpression*> parameters,
390 parameters(std::move(parameters)),
391 return_type(return_type) {}
392 std::vector<TypeExpression*> parameters;
407 :
Statement(kKind, pos), expression(expression) {}
416 condition(condition),
417 is_constexpr(is_constexpr),
419 if_false(if_false) {}
429 :
Statement(kKind, pos), condition(condition), body(body) {}
444 bool never_continues)
447 never_continues(never_continues) {}
449 bool never_continues;
457 debug_only(debug_only),
458 expression(expression),
459 source(std::move(source)) {}
479 const_qualified(const_qualified),
480 name(std::move(name)),
482 initializer(initializer) {}
483 bool const_qualified;
502 const std::vector<Expression*>& arguments)
504 label(std::move(label)),
505 arguments(std::move(arguments)) {}
507 std::vector<Expression*> arguments;
517 test(std::move(test)),
518 action(std::move(action)),
519 body(std::move(body)) {
521 var_declaration = VarDeclarationStatement::cast(*declaration);
539 var_declaration(VarDeclarationStatement::cast(decl)),
543 begin = range->begin;
559 label(std::move(label)),
560 parameters(parameters),
561 body(std::move(body)) {}
570 :
Expression(kKind, pos), statement(statement) {}
579 catch_exceptions(catch_exceptions),
580 try_expression(try_expression),
581 label_block(label_block) {}
582 bool catch_exceptions;
590 std::vector<Statement*> statements = {})
593 statements(std::move(statements)) {}
595 std::vector<Statement*> statements;
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)) {}
633 std::vector<TypeExpression*> types;
636 typedef std::vector<LabelAndTypes> LabelAndTypesVector;
641 LabelAndTypesVector labels;
649 transitioning(transitioning),
650 name(std::move(name)),
655 std::unique_ptr<CallableNodeSignature> signature;
663 const LabelAndTypesVector& labels)
664 :
CallableNode(kind, pos, transitioning, std::move(name),
665 std::move(parameters), return_type, labels),
673 std::string external_assembler_name,
677 const LabelAndTypesVector& labels)
679 std::move(op), std::move(parameters), return_type,
681 external_assembler_name(std::move(external_assembler_name)) {}
682 std::string external_assembler_name;
689 :
CallableNode(kKind, pos,
false, std::move(name), std::move(parameters),
698 const LabelAndTypesVector& labels)
700 std::move(op), std::move(parameters), return_type,
707 bool javascript_linkage,
bool transitioning,
710 :
CallableNode(kind, pos, transitioning, std::move(name),
711 std::move(parameters), return_type, {}),
712 javascript_linkage(javascript_linkage) {}
713 bool javascript_linkage;
719 bool javascript_linkage, std::string name,
723 std::move(name), std::move(parameters),
730 bool javascript_linkage, std::string name,
734 std::move(name), std::move(parameters),
743 :
CallableNode(kKind, pos, transitioning, name, parameters, return_type,
752 name(std::move(name)),
754 expression(expression) {}
764 :
Declaration(kKind, pos), callable(callable), body(body) {}
772 std::vector<std::string> generic_parameters,
776 generic_parameters(std::move(generic_parameters)),
779 std::vector<std::string> generic_parameters;
786 std::vector<TypeExpression*> generic_parameters,
789 LabelAndTypesVector labels,
Statement* b)
791 name(std::move(name)),
793 generic_parameters(std::move(generic_parameters)),
799 std::vector<TypeExpression*> generic_parameters;
800 std::unique_ptr<CallableNodeSignature> signature;
809 name(std::move(name)),
811 literal(std::move(literal)) {}
820 std::vector<NameAndTypeExpression> fields)
822 name(std::move(name)),
823 fields(std::move(fields)) {}
825 std::vector<NameAndTypeExpression> fields;
828 #define ENUM_ITEM(name) \ 829 case AstNode::Kind::k##name: \ 830 return std::is_base_of<T, name>::value; \ 834 bool AstNodeClassCheck::IsInstanceOf(
AstNode* node) {
835 switch (node->kind) {
836 AST_NODE_KIND_LIST(ENUM_ITEM)
845 inline bool IsDeferred(Statement* stmt) {
846 if (
auto* block = BlockStatement::DynamicCast(stmt)) {
847 return block->deferred;
856 #endif // V8_TORQUE_AST_H_