tan  0.0.1
ast_visitor.h
1 #ifndef __TAN_INCLUDE_AST_AST_VISITOR_H__
2 #define __TAN_INCLUDE_AST_AST_VISITOR_H__
3 
4 #include "base.h"
5 #include "ast/ast_base.h"
6 
7 namespace tanlang {
8 
9 class Program;
10 class Package;
11 class Identifier;
12 class VarRef;
13 class Parenthesis;
14 class If;
15 class VarDecl;
16 class ArgDecl;
17 class Return;
18 class CompoundStmt;
19 class BinaryOrUnary;
20 class BinaryOperator;
21 class UnaryOperator;
22 class Cast;
23 class Assignment;
24 class FunctionCall;
25 class FunctionDecl;
26 class Import;
27 class Intrinsic;
28 class ArrayLiteral;
29 class CharLiteral;
30 class BoolLiteral;
31 class IntegerLiteral;
32 class FloatLiteral;
33 class StringLiteral;
34 class NullPointerLiteral;
35 class MemberAccess;
36 class StructDecl;
37 class Loop;
38 class BreakContinue;
39 class PackageDecl;
40 
41 #ifndef DEFINE_AST_VISITOR_INTERFACE
42 #define DEFINE_AST_VISITOR_INTERFACE(AST_NAME) \
43  /** The interface VisitXX calls this->VisitXXImpl() if defined by Derived */ \
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); \
48  } else { \
49  ((Derived *)this)->default_visit(p); \
50  } \
51  }
52 #endif
53 
54 #ifndef CALL_AST_VISITOR
55 #define CALL_AST_VISITOR(AST_NAME, NODE) Visit##AST_NAME(pcast<AST_NAME>(NODE))
56 #endif
57 
58 template <typename Derived> class ASTVisitor {
59 public:
60  DEFINE_AST_VISITOR_INTERFACE(Program)
61  DEFINE_AST_VISITOR_INTERFACE(Package)
62  DEFINE_AST_VISITOR_INTERFACE(Identifier)
63  DEFINE_AST_VISITOR_INTERFACE(Parenthesis)
64  DEFINE_AST_VISITOR_INTERFACE(If)
65  DEFINE_AST_VISITOR_INTERFACE(VarDecl)
66  DEFINE_AST_VISITOR_INTERFACE(ArgDecl)
67  DEFINE_AST_VISITOR_INTERFACE(Return)
68  DEFINE_AST_VISITOR_INTERFACE(CompoundStmt)
69  DEFINE_AST_VISITOR_INTERFACE(BinaryOrUnary)
70  DEFINE_AST_VISITOR_INTERFACE(BinaryOperator)
71  DEFINE_AST_VISITOR_INTERFACE(UnaryOperator)
72  DEFINE_AST_VISITOR_INTERFACE(Cast)
73  DEFINE_AST_VISITOR_INTERFACE(Assignment)
74  DEFINE_AST_VISITOR_INTERFACE(FunctionCall)
75  DEFINE_AST_VISITOR_INTERFACE(FunctionDecl)
76  DEFINE_AST_VISITOR_INTERFACE(Import)
77  DEFINE_AST_VISITOR_INTERFACE(Intrinsic)
78  DEFINE_AST_VISITOR_INTERFACE(ArrayLiteral)
79  DEFINE_AST_VISITOR_INTERFACE(CharLiteral)
80  DEFINE_AST_VISITOR_INTERFACE(BoolLiteral)
81  DEFINE_AST_VISITOR_INTERFACE(IntegerLiteral)
82  DEFINE_AST_VISITOR_INTERFACE(FloatLiteral)
83  DEFINE_AST_VISITOR_INTERFACE(StringLiteral)
84  DEFINE_AST_VISITOR_INTERFACE(NullPointerLiteral)
85  DEFINE_AST_VISITOR_INTERFACE(MemberAccess)
86  DEFINE_AST_VISITOR_INTERFACE(StructDecl)
87  DEFINE_AST_VISITOR_INTERFACE(Loop)
88  DEFINE_AST_VISITOR_INTERFACE(BreakContinue)
89  DEFINE_AST_VISITOR_INTERFACE(VarRef)
90  DEFINE_AST_VISITOR_INTERFACE(PackageDecl)
91 
92  void visit(ASTBase *p) {
93  TAN_ASSERT(p);
94 
95  switch (p->get_node_type()) {
96  case ASTNodeType::PROGRAM:
97  CALL_AST_VISITOR(Program, p);
98  break;
99  case ASTNodeType::PACKAGE:
100  CALL_AST_VISITOR(Package, p);
101  break;
102  case ASTNodeType::COMPOUND_STATEMENT:
103  CALL_AST_VISITOR(CompoundStmt, p);
104  break;
105  case ASTNodeType::RET:
106  CALL_AST_VISITOR(Return, p);
107  break;
108  case ASTNodeType::IF:
109  CALL_AST_VISITOR(If, p);
110  break;
111  case ASTNodeType::LOOP:
112  CALL_AST_VISITOR(Loop, p);
113  break;
114  case ASTNodeType::BREAK:
115  case ASTNodeType::CONTINUE:
116  CALL_AST_VISITOR(BreakContinue, p);
117  break;
118  case ASTNodeType::IMPORT:
119  CALL_AST_VISITOR(Import, p);
120  break;
121  /// expressions
122  case ASTNodeType::ASSIGN:
123  CALL_AST_VISITOR(Assignment, p);
124  break;
125  case ASTNodeType::CAST:
126  CALL_AST_VISITOR(Cast, p);
127  break;
128  case ASTNodeType::BOP:
129  CALL_AST_VISITOR(BinaryOperator, p);
130  break;
131  case ASTNodeType::UOP:
132  CALL_AST_VISITOR(UnaryOperator, p);
133  break;
134  case ASTNodeType::BOP_OR_UOP:
135  CALL_AST_VISITOR(BinaryOrUnary, p);
136  break;
137  case ASTNodeType::ID:
138  CALL_AST_VISITOR(Identifier, p);
139  break;
140  case ASTNodeType::STRING_LITERAL:
141  CALL_AST_VISITOR(StringLiteral, p);
142  break;
143  case ASTNodeType::CHAR_LITERAL:
144  CALL_AST_VISITOR(CharLiteral, p);
145  break;
146  case ASTNodeType::BOOL_LITERAL:
147  CALL_AST_VISITOR(BoolLiteral, p);
148  break;
149  case ASTNodeType::INTEGER_LITERAL:
150  CALL_AST_VISITOR(IntegerLiteral, p);
151  break;
152  case ASTNodeType::FLOAT_LITERAL:
153  CALL_AST_VISITOR(FloatLiteral, p);
154  break;
155  case ASTNodeType::ARRAY_LITERAL:
156  CALL_AST_VISITOR(ArrayLiteral, p);
157  break;
158  case ASTNodeType::NULLPTR_LITERAL:
159  CALL_AST_VISITOR(NullPointerLiteral, p);
160  break;
161  case ASTNodeType::INTRINSIC:
162  CALL_AST_VISITOR(Intrinsic, p);
163  break;
164  case ASTNodeType::PARENTHESIS:
165  CALL_AST_VISITOR(Parenthesis, p);
166  break;
167  case ASTNodeType::FUNC_CALL:
168  CALL_AST_VISITOR(FunctionCall, p);
169  break;
170  case ASTNodeType::FUNC_DECL:
171  CALL_AST_VISITOR(FunctionDecl, p);
172  break;
173  case ASTNodeType::ARG_DECL:
174  CALL_AST_VISITOR(ArgDecl, p);
175  break;
176  case ASTNodeType::VAR_DECL:
177  CALL_AST_VISITOR(VarDecl, p);
178  break;
179  case ASTNodeType::STRUCT_DECL:
180  CALL_AST_VISITOR(StructDecl, p);
181  break;
182  case ASTNodeType::VAR_REF:
183  CALL_AST_VISITOR(VarRef, p);
184  break;
185  case ASTNodeType::PACKAGE_DECL:
186  CALL_AST_VISITOR(PackageDecl, p);
187  break;
188  default:
189  TAN_ASSERT(false);
190  }
191  }
192 
193  ~ASTVisitor() = default;
194  virtual void default_visit(ASTBase *) {}
195 };
196 
197 #undef DEFINE_AST_VISITOR_INTERFACE
198 
199 #ifndef DEFINE_AST_VISITOR_IMPL
200 #define DEFINE_AST_VISITOR_IMPL(CLASS, AST_NAME) void CLASS::Visit##AST_NAME##Impl(AST_NAME *p)
201 #endif
202 
203 #ifndef DECLARE_AST_VISITOR_IMPL
204 #define DECLARE_AST_VISITOR_IMPL(AST_NAME) void Visit##AST_NAME##Impl(AST_NAME *p)
205 #endif
206 
207 } // namespace tanlang
208 
209 #endif //__TAN_INCLUDE_AST_AST_VISITOR_H__
Represent if-[else] or if-elif-[else] statements.
Definition: stmt.h:144
A generic representation of Intrinsic variables/functions.
Definition: intrinsic.h:55