sammine-lang
Loading...
Searching...
No Matches
AstBase.h
Go to the documentation of this file.
1//
2// Created by jjasmine on 3/9/24.
3//
4
5#pragma once
6#include "ast/AstDecl.h"
7#include "ast/ASTProperties.h"
8#include "lex/Lexer.h"
9#include "lex/Token.h"
10#include "typecheck/Types.h"
11#include "util/LexicalContext.h"
12#include "util/Utilities.h"
13#include "llvm/Support/Casting.h"
14#include <atomic>
15#include <stack>
16
19namespace llvm {
20class Value;
21
22class Function;
23} // namespace llvm
24
25namespace sammine_lang {
26namespace AST {
27
28enum class NodeKind {
29 // Direct AstBase children (not ExprAST, not DefinitionAST)
34
35 // ExprAST subclasses [FirstExpr..LastExpr]
36 FirstExpr,
37 VarDefAST = FirstExpr,
61 LastExpr = TupleLiteralExprAST,
62
63 // DefinitionAST subclasses [FirstDef..LastDef]
64 FirstDef,
65 FuncDefAST = FirstDef,
72 LastDef = TypeClassInstanceAST,
73};
74
75class Visitable;
76class AstBase;
77class ASTVisitor;
78class ASTProperties;
79
80struct ASTPrinter {
81 static void print(AstBase *t, const ASTProperties &props);
82 static void print(ProgramAST *t, const ASTProperties &props);
83 // Convenience overloads for debug/error paths that don't have props available
84 static void print(AstBase *t);
85 static void print(ProgramAST *t);
86};
88protected:
89 ProgramAST *top_level_ast = nullptr;
90
91public:
92 [[noreturn]] virtual void
93 abort(const std::string &msg = "<NO MESSAGE>") override final;
94
95 virtual void visit(ProgramAST *ast);
96 virtual void preorder_walk(ProgramAST *ast) = 0;
97 virtual void postorder_walk(ProgramAST *ast) = 0;
98
99 virtual void visit(VarDefAST *ast);
100 virtual void preorder_walk(VarDefAST *ast) = 0;
101 virtual void postorder_walk(VarDefAST *ast) = 0;
102
103 virtual void visit(ExternAST *ast);
104 virtual void preorder_walk(ExternAST *ast) = 0;
105 virtual void postorder_walk(ExternAST *ast) = 0;
106
107 virtual void visit(FuncDefAST *ast);
108 virtual void preorder_walk(FuncDefAST *ast) = 0;
109 virtual void postorder_walk(FuncDefAST *ast) = 0;
110
111 virtual void visit(StructDefAST *ast);
112 virtual void preorder_walk(StructDefAST *ast) = 0;
113 virtual void postorder_walk(StructDefAST *ast) = 0;
114
115 virtual void visit(EnumDefAST *ast);
116 virtual void preorder_walk(EnumDefAST *ast) = 0;
117 virtual void postorder_walk(EnumDefAST *ast) = 0;
118
119 virtual void visit(TypeAliasDefAST *ast);
120 virtual void preorder_walk(TypeAliasDefAST *ast) = 0;
121 virtual void postorder_walk(TypeAliasDefAST *ast) = 0;
122
123 virtual void visit(PrototypeAST *ast);
124 virtual void preorder_walk(PrototypeAST *ast) = 0;
125 virtual void postorder_walk(PrototypeAST *ast) = 0;
126
127 virtual void visit(CallExprAST *ast);
128 virtual void preorder_walk(CallExprAST *ast) = 0;
129 virtual void postorder_walk(CallExprAST *ast) = 0;
130
131 virtual void visit(ReturnExprAST *ast);
132 virtual void preorder_walk(ReturnExprAST *ast) = 0;
133 virtual void postorder_walk(ReturnExprAST *ast) = 0;
134
135 virtual void visit(BinaryExprAST *ast);
136 virtual void preorder_walk(BinaryExprAST *ast) = 0;
137 virtual void postorder_walk(BinaryExprAST *ast) = 0;
138
139 virtual void visit(NumberExprAST *ast);
140 virtual void preorder_walk(NumberExprAST *ast) = 0;
141 virtual void postorder_walk(NumberExprAST *ast) = 0;
142
143 virtual void visit(StringExprAST *ast);
144 virtual void preorder_walk(StringExprAST *ast) = 0;
145 virtual void postorder_walk(StringExprAST *ast) = 0;
146
147 virtual void visit(BoolExprAST *ast);
148 virtual void preorder_walk(BoolExprAST *ast) = 0;
149 virtual void postorder_walk(BoolExprAST *ast) = 0;
150
151 virtual void visit(CharExprAST *ast);
152 virtual void preorder_walk(CharExprAST *ast) = 0;
153 virtual void postorder_walk(CharExprAST *ast) = 0;
154
155 virtual void visit(UnitExprAST *ast);
156 virtual void preorder_walk(UnitExprAST *ast) = 0;
157 virtual void postorder_walk(UnitExprAST *ast) = 0;
158
159 virtual void visit(VariableExprAST *ast);
160 virtual void preorder_walk(VariableExprAST *ast) = 0;
161 virtual void postorder_walk(VariableExprAST *ast) = 0;
162
163 virtual void visit(BlockAST *ast);
164 virtual void preorder_walk(BlockAST *ast) = 0;
165 virtual void postorder_walk(BlockAST *ast) = 0;
166
167 virtual void visit(IfExprAST *ast);
168 virtual void preorder_walk(IfExprAST *ast) = 0;
169 virtual void postorder_walk(IfExprAST *ast) = 0;
170
171 virtual void visit(TypedVarAST *ast);
172 virtual void preorder_walk(TypedVarAST *ast) = 0;
173 virtual void postorder_walk(TypedVarAST *ast) = 0;
174
175 virtual void visit(DerefExprAST *ast);
176 virtual void preorder_walk(DerefExprAST *ast) = 0;
177 virtual void postorder_walk(DerefExprAST *ast) = 0;
178
179 virtual void visit(AddrOfExprAST *ast);
180 virtual void preorder_walk(AddrOfExprAST *ast) = 0;
181 virtual void postorder_walk(AddrOfExprAST *ast) = 0;
182
183 virtual void visit(AllocExprAST *ast);
184 virtual void preorder_walk(AllocExprAST *ast) = 0;
185 virtual void postorder_walk(AllocExprAST *ast) = 0;
186
187 virtual void visit(FreeExprAST *ast);
188 virtual void preorder_walk(FreeExprAST *ast) = 0;
189 virtual void postorder_walk(FreeExprAST *ast) = 0;
190
191 virtual void visit(ArrayLiteralExprAST *ast);
192 virtual void preorder_walk(ArrayLiteralExprAST *ast) = 0;
193 virtual void postorder_walk(ArrayLiteralExprAST *ast) = 0;
194
195 virtual void visit(IndexExprAST *ast);
196 virtual void preorder_walk(IndexExprAST *ast) = 0;
197 virtual void postorder_walk(IndexExprAST *ast) = 0;
198
199 virtual void visit(LenExprAST *ast);
200 virtual void preorder_walk(LenExprAST *ast) = 0;
201 virtual void postorder_walk(LenExprAST *ast) = 0;
202
203 virtual void visit(UnaryNegExprAST *ast);
204 virtual void preorder_walk(UnaryNegExprAST *ast) = 0;
205 virtual void postorder_walk(UnaryNegExprAST *ast) = 0;
206
207 virtual void visit(StructLiteralExprAST *ast);
208 virtual void preorder_walk(StructLiteralExprAST *ast) = 0;
209 virtual void postorder_walk(StructLiteralExprAST *ast) = 0;
210
211 virtual void visit(FieldAccessExprAST *ast);
212 virtual void preorder_walk(FieldAccessExprAST *ast) = 0;
213 virtual void postorder_walk(FieldAccessExprAST *ast) = 0;
214
215 virtual void visit(CaseExprAST *ast);
216 virtual void preorder_walk(CaseExprAST *ast) = 0;
217 virtual void postorder_walk(CaseExprAST *ast) = 0;
218
219 virtual void visit(WhileExprAST *ast);
220 virtual void preorder_walk(WhileExprAST *ast) = 0;
221 virtual void postorder_walk(WhileExprAST *ast) = 0;
222
223 virtual void visit(TupleLiteralExprAST *ast);
224 virtual void preorder_walk(TupleLiteralExprAST *ast) = 0;
225 virtual void postorder_walk(TupleLiteralExprAST *ast) = 0;
226
227 virtual void visit(TypeClassDeclAST *ast);
228 virtual void preorder_walk(TypeClassDeclAST *ast) = 0;
229 virtual void postorder_walk(TypeClassDeclAST *ast) = 0;
230
231 virtual void visit(TypeClassInstanceAST *ast);
232 virtual void preorder_walk(TypeClassInstanceAST *ast) = 0;
233 virtual void postorder_walk(TypeClassInstanceAST *ast) = 0;
234
235 virtual ~ASTVisitor() = 0;
236};
237
238template <class T, class S>
239class LexicalStack : public std::stack<LexicalContext<T, S>> {
240public:
241 void push_context() {
242 if (this->empty())
243 this->push(LexicalContext<T, S>());
244 else
245 this->push(LexicalContext<T, S>(&this->top()));
246 }
247 void pop_context() {
248 if (this->empty())
249 sammine_util::abort("ICE: You are popping an empty lexical stack");
250 this->pop();
251 }
252
253 void registerNameT(const std::string &name, T l) {
254 return this->top().registerNameT(name, l);
255 }
256 NameQueryResult recursiveQueryName(const std::string &name) const {
257 return this->top().recursiveQueryName(name);
258 }
259
260 T get_from_name(const std::string &name) const {
261 return this->top().get_from_name(name);
262 }
263 NameQueryResult queryName(const std::string &name) const {
264 return this->top().queryName(name);
265 }
266
267 T recursive_get_from_name(const std::string &name) const {
268 return this->top().recursive_get_from_name(name);
269 }
270
271 const LexicalContext<T, S> *parent_scope() const {
272 return this->top().parent_scope;
273 }
274 LexicalContext<T, S> *parent_scope() { return this->top().parent_scope; }
275};
276
278public:
279 virtual void enter_new_scope() = 0;
280 virtual void exit_new_scope() = 0;
281
285 virtual void visit(FuncDefAST *ast);
286
287 virtual ~ScopedASTVisitor() = 0;
288};
289
291public:
292 virtual Type synthesize(ProgramAST *ast) = 0;
293 virtual Type synthesize(VarDefAST *ast) = 0;
294 virtual Type synthesize(ExternAST *ast) = 0;
295 virtual Type synthesize(FuncDefAST *ast) = 0;
296 virtual Type synthesize(StructDefAST *ast) = 0;
297 virtual Type synthesize(EnumDefAST *ast) = 0;
298 virtual Type synthesize(TypeAliasDefAST *ast) = 0;
299 virtual Type synthesize(PrototypeAST *ast) = 0;
300 virtual Type synthesize(CallExprAST *ast) = 0;
301 virtual Type synthesize(ReturnExprAST *ast) = 0;
302 virtual Type synthesize(BinaryExprAST *ast) = 0;
303 virtual Type synthesize(NumberExprAST *ast) = 0;
304 virtual Type synthesize(StringExprAST *ast) = 0;
305 virtual Type synthesize(UnitExprAST *ast) = 0;
306 virtual Type synthesize(BoolExprAST *ast) = 0;
307 virtual Type synthesize(CharExprAST *ast) = 0;
308 virtual Type synthesize(VariableExprAST *ast) = 0;
309 virtual Type synthesize(BlockAST *ast) = 0;
310 virtual Type synthesize(IfExprAST *ast) = 0;
311 virtual Type synthesize(TypedVarAST *ast) = 0;
312 virtual Type synthesize(DerefExprAST *ast) = 0;
313 virtual Type synthesize(AddrOfExprAST *ast) = 0;
314 virtual Type synthesize(AllocExprAST *ast) = 0;
315 virtual Type synthesize(FreeExprAST *ast) = 0;
316 virtual Type synthesize(ArrayLiteralExprAST *ast) = 0;
317 virtual Type synthesize(IndexExprAST *ast) = 0;
318 virtual Type synthesize(LenExprAST *ast) = 0;
319 virtual Type synthesize(UnaryNegExprAST *ast) = 0;
320 virtual Type synthesize(StructLiteralExprAST *ast) = 0;
321 virtual Type synthesize(FieldAccessExprAST *ast) = 0;
322 virtual Type synthesize(CaseExprAST *ast) = 0;
323 virtual Type synthesize(WhileExprAST *ast) = 0;
324 virtual Type synthesize(TupleLiteralExprAST *ast) = 0;
325 virtual Type synthesize(TypeClassDeclAST *ast) = 0;
326 virtual Type synthesize(TypeClassInstanceAST *ast) = 0;
327
328 virtual ~TypeCheckerVisitor() = 0;
329};
330
332public:
333 virtual ~Visitable() = default;
334 virtual void accept_vis(ASTVisitor *visitor) = 0;
335 virtual void walk_with_preorder(ASTVisitor *visitor) = 0;
336 virtual void walk_with_postorder(ASTVisitor *visitor) = 0;
337 virtual Type accept_synthesis(TypeCheckerVisitor *visitor) = 0;
338 virtual std::string getTreeName() const = 0;
339};
340
341class AstBase : public Visitable {
342 NodeKind kind;
343 NodeId node_id_;
344
345 static inline std::atomic<NodeId> next_id_{0};
346 static inline ASTProperties *current_props_ = nullptr;
347
348 void change_location(sammine_util::Location loc) {
349 if (first_location) {
350 this->location = loc;
351 first_location = false;
352 } else
353 this->location |= loc;
354 }
355
356 bool first_location = true;
357
358protected:
359 sammine_util::Location location;
360
361public:
362 AstBase(NodeKind kind) : kind(kind), node_id_(next_id_++) {}
363 NodeId id() const { return node_id_; }
364 static void reset_id_counter() { next_id_ = 0; }
365 static void set_properties(ASTProperties *p) { current_props_ = p; }
366 NodeKind getKind() const { return kind; }
367 // INFO: Parser error
368 bool pe = false;
369 llvm::Value *val;
370 AstBase *join_location(AstBase *ast) {
371 if (!ast)
372 pe = true;
373 else
374 change_location(ast->get_location());
375
376 return this;
377 }
378
379 AstBase *join_location(std::shared_ptr<Token> tok) {
380
381 if (!tok)
382 pe = true;
383 else
384 change_location(tok->get_location());
385
386 return this;
387 }
388
389 AstBase *join_location(sammine_util::Location location) {
390 if (location.source_start <= 0 && location.source_end <= 0)
391 return this;
392
393 change_location(location);
394 return this;
395 }
396 sammine_util::Location get_location() const { return this->location; }
397 void set_location(sammine_util::Location loc) {
398 this->location = loc;
399 first_location = false;
400 }
401 bool synthesized() const { return get_type().synthesized(); }
402 Type get_type() const {
403 if (current_props_)
404 return current_props_->get_type(node_id_);
405 return Type::NonExistent();
406 }
407 Type set_type(const Type &t) {
408 if (current_props_)
409 current_props_->set_type(node_id_, t);
410 return t;
411 }
412};
413} // namespace AST
414} // namespace sammine_lang
Holds declaration for all the AST Nodes.
Houses the lexer and the declaration of its method.
A simple scoping class, doesn't differentiate between different names, like variable name,...
NameQueryResult
NameQueryResult enum.
Definition LexicalContext.h:13
Defines the token structure (TokenType, TokStream, TokenMap).
Defines the core Type system for Sammine.
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
LexicalContext class.
Definition LexicalContext.h:19
Definition ASTProperties.h:40
Definition AstBase.h:87
Definition AstBase.h:341
An AST to simulate a { } code block.
Definition Ast.h:317
Definition Ast.h:485
Definition Ast.h:548
Definition Ast.h:798
Definition Ast.h:495
Definition Ast.h:378
A Function Definition that has the prototype and definition in terms of a block.
Definition Ast.h:291
Definition Ast.h:667
Definition Ast.h:326
Definition Ast.h:598
Definition Ast.h:707
Definition AstBase.h:239
Definition Ast.h:197
A prototype to present "func func_name(...) -> type;".
Definition Ast.h:236
Definition AstBase.h:277
virtual void visit(FuncDefAST *ast)
Definition Ast.cpp:243
Definition Ast.h:205
Definition Ast.h:581
A variable definition: "var x = expression;" or "let (a, b) = expr;".
Definition Ast.h:420
Definition AstBase.h:331
Definition Utilities.h:70
Definition Utilities.h:130
Definition Types.h:139
Definition AstBase.h:80