3 #include "diagnostics/Location.h"
4 #include "parsetree/ParseTree.h"
5 #include "semantic/Semantic.h"
6 #include <utils/Error.h>
12 ParseTreeException(
Node* where,
const std::string& what)
13 : msg{what}, where{where} {}
15 const char* what()
const noexcept override {
return msg.c_str(); }
17 Node* get_where()
const {
return where; }
25 using pty =
Node::Type;
28 ParseTreeVisitor(ast::
Semantic& sem) : sem{sem}, alloc{sem.allocator()} {}
29 ParseTreeVisitor(ast::
Semantic& sem, BumpAllocator& alloc)
30 : sem{sem}, alloc{alloc} {}
35 static inline void check_node_type(
Node* node,
Node::Type type) {
38 "Called on a node that is not the correct type!"
45 static inline void check_num_children(
Node* node, size_t min, size_t max) {
48 "Node has incorrect number of children!"
74 template <parsetree::
Node::Type N,
typename T>
92 template <parsetree::
Node::Type N,
typename T,
bool nullable =
false>
93 void visitListPattern(
Node* node, ast::array_ref<T> list) {
94 if(nullable && node ==
nullptr)
return;
95 if(!nullable && node ==
nullptr)
96 throw utils::FatalError(
"Visited a null node!");
97 check_node_type(node, N);
98 check_num_children(node, 1, 2);
100 list.push_back(visit<N, T>(node->child(0)));
102 visitListPattern<N, T, nullable>(node->child(0), list);
103 list.push_back(visit<N, T>(node->child(1)));
110 ast::CompilationUnit* visitCompilationUnit(
Node* node);
117 ast::ClassDecl* visitClassDeclaration(
Node* node);
118 ast::InterfaceDecl* visitInterfaceDeclaration(
Node* node);
120 ast::FieldDecl* visitFieldDeclaration(
Node* node);
121 ast::MethodDecl* visitMethodDeclaration(
Node* node);
122 ast::MethodDecl* visitConstructorDeclaration(
Node* node);
123 ast::MethodDecl* visitAbstractMethodDeclaration(
Node* node);
126 ast::
Decl* visit<pty::ClassBodyDeclarationList>(
Node* node);
128 ast::
VarDecl* visit<pty::FormalParameterList>(
Node* node);
130 ast::
Decl* visit<pty::InterfaceMemberDeclarationList>(
Node* node);
137 std::string_view name;
142 ast::DeclStmt* visitLocalVariableDeclarationStatement(
Node* node);
144 ast::BlockStatement* visitBlock(
Node* node);
145 ast::
Stmt* visitStatement(
Node* node);
146 ast::IfStmt* visitIfThenStatement(
Node* node);
147 ast::WhileStmt* visitWhileStatement(
Node* node);
148 ast::ForStmt* visitForStatement(
Node* node);
149 ast::ReturnStmt* visitReturnStatement(
Node* node);
150 ast::ExprStmt* visitExpressionStatement(
Node* node);
155 ast::ExprNodeList visitExprChild(
Node* node);
156 ast::ExprNodeList visitExprNode(
Node* node);
157 ast::ExprNodeList visitMethodInvocation(
Node* node);
158 ast::ExprNodeList visitQualifiedIdentifierInExpr(
Node* node,
bool isMethodInvocation =
false);
159 ast::ExprNodeList visitFieldAccess(
Node* node);
160 ast::ExprNodeList visitClassCreation(
Node* node);
161 ast::ExprNodeList visitArrayAccess(
Node* node);
162 ast::ExprNodeList visitArrayCreation(
Node* node);
163 ast::ExprNodeList visitCastExpression(
Node* node);
167 ast::exprnode::LiteralNode* visitLiteral(
Node* node);
171 int visitArgumentList(
Node* node, ast::ExprNodeList& ops);
174 ast::
Stmt* visit<pty::BlockStatementList>(
Node* node);
178 ast::UnresolvedType* visitReferenceType(
179 Node* node, ast::UnresolvedType* ast_node =
nullptr);
180 std::string_view visitIdentifier(
Node* node);
188 BumpAllocator& alloc;