1 #ifndef __TAN_INCLUDE_AST_AST_VISITOR_H__
2 #define __TAN_INCLUDE_AST_AST_VISITOR_H__
5 #include "ast/ast_base.h"
34 class NullPointerLiteral;
41 #ifndef DEFINE_AST_VISITOR_INTERFACE
42 #define DEFINE_AST_VISITOR_INTERFACE(AST_NAME) \
44 void Visit##AST_NAME(AST_NAME *p) { \
45 constexpr bool has_func = requires(Derived t) { t.Visit##AST_NAME##Impl(p); }; \
46 if constexpr (has_func) { \
47 ((Derived *)this)->Visit##AST_NAME##Impl(p); \
49 ((Derived *)this)->default_visit(p); \
54 #ifndef CALL_AST_VISITOR
55 #define CALL_AST_VISITOR(AST_NAME, NODE) Visit##AST_NAME(pcast<AST_NAME>(NODE))
64 DEFINE_AST_VISITOR_INTERFACE(
If)
72 DEFINE_AST_VISITOR_INTERFACE(
Cast)
87 DEFINE_AST_VISITOR_INTERFACE(
Loop)
95 switch (p->get_node_type()) {
96 case ASTNodeType::PROGRAM:
99 case ASTNodeType::PACKAGE:
102 case ASTNodeType::COMPOUND_STATEMENT:
105 case ASTNodeType::RET:
106 CALL_AST_VISITOR(
Return, p);
108 case ASTNodeType::IF:
109 CALL_AST_VISITOR(
If, p);
111 case ASTNodeType::LOOP:
112 CALL_AST_VISITOR(
Loop, p);
114 case ASTNodeType::BREAK:
115 case ASTNodeType::CONTINUE:
118 case ASTNodeType::IMPORT:
119 CALL_AST_VISITOR(
Import, p);
122 case ASTNodeType::ASSIGN:
125 case ASTNodeType::CAST:
126 CALL_AST_VISITOR(
Cast, p);
128 case ASTNodeType::BOP:
131 case ASTNodeType::UOP:
134 case ASTNodeType::BOP_OR_UOP:
137 case ASTNodeType::ID:
140 case ASTNodeType::STRING_LITERAL:
143 case ASTNodeType::CHAR_LITERAL:
146 case ASTNodeType::BOOL_LITERAL:
149 case ASTNodeType::INTEGER_LITERAL:
152 case ASTNodeType::FLOAT_LITERAL:
155 case ASTNodeType::ARRAY_LITERAL:
158 case ASTNodeType::NULLPTR_LITERAL:
161 case ASTNodeType::INTRINSIC:
164 case ASTNodeType::PARENTHESIS:
167 case ASTNodeType::FUNC_CALL:
170 case ASTNodeType::FUNC_DECL:
173 case ASTNodeType::ARG_DECL:
176 case ASTNodeType::VAR_DECL:
179 case ASTNodeType::STRUCT_DECL:
182 case ASTNodeType::VAR_REF:
183 CALL_AST_VISITOR(
VarRef, p);
185 case ASTNodeType::PACKAGE_DECL:
194 virtual void default_visit(
ASTBase *) {}
197 #undef DEFINE_AST_VISITOR_INTERFACE
199 #ifndef DEFINE_AST_VISITOR_IMPL
200 #define DEFINE_AST_VISITOR_IMPL(CLASS, AST_NAME) void CLASS::Visit##AST_NAME##Impl(AST_NAME *p)
203 #ifndef DECLARE_AST_VISITOR_IMPL
204 #define DECLARE_AST_VISITOR_IMPL(AST_NAME) void Visit##AST_NAME##Impl(AST_NAME *p)
Represent if-[else] or if-elif-[else] statements.
A generic representation of Intrinsic variables/functions.