mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals (previously forked from FunC). The goal is to convert AST directly to Op (a kind of IR representation), doing all code analysis at AST level. Noteable changes: - AST-based semantic kernel includes: registering global symbols, scope handling and resolving local/global identifiers, lvalue/rvalue calc and check, implicit return detection, mutability analysis, pure/impure validity checks, simple constant folding - values of `const` variables are calculated NOT based on CodeBlob, but via a newly-introduced AST-based constant evaluator - AST vertices are now inherited from expression/statement/other; expression vertices have common properties (TypeExpr, lvalue/rvalue) - symbol table is rewritten completely, SymDef/SymVal no longer exist, lexer now doesn't need to register identifiers - AST vertices have references to symbols, filled at different stages of pipeline - the remaining "FunC legacy part" is almost unchanged besides Expr which was fully dropped; AST is converted to Ops (IR) directly
This commit is contained in:
parent
ea0dc16163
commit
3540424aa1
71 changed files with 4270 additions and 3060 deletions
|
@ -37,20 +37,40 @@ namespace tolk {
|
|||
|
||||
class ASTVisitor {
|
||||
protected:
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE static void visit_children(const ASTNodeLeaf* v) {
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE static void visit_children(const ASTExprLeaf* v) {
|
||||
static_cast<void>(v);
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTNodeUnary* v) {
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTExprUnary* v) {
|
||||
visit(v->child);
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTNodeBinary* v) {
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTExprBinary* v) {
|
||||
visit(v->lhs);
|
||||
visit(v->rhs);
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTNodeVararg* v) {
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTExprVararg* v) {
|
||||
for (AnyExprV child : v->children) {
|
||||
visit(child);
|
||||
}
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTStatementUnary* v) {
|
||||
visit(v->child);
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTStatementVararg* v) {
|
||||
for (AnyV child : v->children) {
|
||||
visit(child);
|
||||
}
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE static void visit_children(const ASTOtherLeaf* v) {
|
||||
static_cast<void>(v);
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE void visit_children(const ASTOtherVararg* v) {
|
||||
for (AnyV child : v->children) {
|
||||
visit(child);
|
||||
}
|
||||
|
@ -66,8 +86,9 @@ class ASTVisitorFunctionBody : public ASTVisitor {
|
|||
protected:
|
||||
using parent = ASTVisitorFunctionBody;
|
||||
|
||||
virtual void visit(V<ast_empty> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_parenthesized_expr> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_empty_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_empty_expression> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_parenthesized_expression> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_tensor> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_tensor_square> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_identifier> v) { return visit_children(v); }
|
||||
|
@ -76,8 +97,10 @@ protected:
|
|||
virtual void visit(V<ast_bool_const> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_null_keyword> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_self_keyword> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_argument> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_argument_list> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_function_call> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_dot_method_call> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_dot_method_call> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_underscore> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_unary_operator> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_binary_operator> v) { return visit_children(v); }
|
||||
|
@ -87,6 +110,8 @@ protected:
|
|||
virtual void visit(V<ast_repeat_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_while_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_do_while_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_throw_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_assert_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_try_catch_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_if_statement> v) { return visit_children(v); }
|
||||
virtual void visit(V<ast_local_var> v) { return visit_children(v); }
|
||||
|
@ -95,8 +120,9 @@ protected:
|
|||
|
||||
void visit(AnyV v) final {
|
||||
switch (v->type) {
|
||||
case ast_empty: return visit(v->as<ast_empty>());
|
||||
case ast_parenthesized_expr: return visit(v->as<ast_parenthesized_expr>());
|
||||
case ast_empty_statement: return visit(v->as<ast_empty_statement>());
|
||||
case ast_empty_expression: return visit(v->as<ast_empty_expression>());
|
||||
case ast_parenthesized_expression: return visit(v->as<ast_parenthesized_expression>());
|
||||
case ast_tensor: return visit(v->as<ast_tensor>());
|
||||
case ast_tensor_square: return visit(v->as<ast_tensor_square>());
|
||||
case ast_identifier: return visit(v->as<ast_identifier>());
|
||||
|
@ -105,6 +131,8 @@ protected:
|
|||
case ast_bool_const: return visit(v->as<ast_bool_const>());
|
||||
case ast_null_keyword: return visit(v->as<ast_null_keyword>());
|
||||
case ast_self_keyword: return visit(v->as<ast_self_keyword>());
|
||||
case ast_argument: return visit(v->as<ast_argument>());
|
||||
case ast_argument_list: return visit(v->as<ast_argument_list>());
|
||||
case ast_function_call: return visit(v->as<ast_function_call>());
|
||||
case ast_dot_method_call: return visit(v->as<ast_dot_method_call>());
|
||||
case ast_underscore: return visit(v->as<ast_underscore>());
|
||||
|
@ -129,27 +157,23 @@ protected:
|
|||
}
|
||||
|
||||
public:
|
||||
void start_visiting_function(V<ast_function_declaration> v_function) {
|
||||
virtual void start_visiting_function(V<ast_function_declaration> v_function) {
|
||||
visit(v_function->get_body());
|
||||
}
|
||||
};
|
||||
|
||||
class ASTVisitorAllFunctionsInFile : public ASTVisitorFunctionBody {
|
||||
protected:
|
||||
using parent = ASTVisitorAllFunctionsInFile;
|
||||
|
||||
virtual bool should_enter_function(V<ast_function_declaration> v) = 0;
|
||||
|
||||
public:
|
||||
void start_visiting_file(V<ast_tolk_file> v_file) {
|
||||
for (AnyV v : v_file->get_toplevel_declarations()) {
|
||||
template<class BodyVisitorT>
|
||||
void visit_ast_of_all_functions(const AllSrcFiles& all_files) {
|
||||
for (const SrcFile* file : all_files) {
|
||||
for (AnyV v : file->ast->as<ast_tolk_file>()->get_toplevel_declarations()) {
|
||||
if (auto v_func = v->try_as<ast_function_declaration>()) {
|
||||
if (should_enter_function(v_func)) {
|
||||
visit(v_func->get_body());
|
||||
if (v_func->is_regular_function()) {
|
||||
BodyVisitorT visitor;
|
||||
visitor.start_visiting_function(v_func);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace tolk
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue