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
|
@ -35,25 +35,39 @@ namespace tolk {
|
|||
|
||||
class ASTReplacer {
|
||||
protected:
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE static AnyV replace_children(const ASTNodeLeaf* v) {
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE static AnyExprV replace_children(const ASTExprLeaf* v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyV replace_children(const ASTNodeUnary* v) {
|
||||
auto* v_mutable = const_cast<ASTNodeUnary*>(v);
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyExprV replace_children(const ASTExprUnary* v) {
|
||||
auto* v_mutable = const_cast<ASTExprUnary*>(v);
|
||||
v_mutable->child = replace(v_mutable->child);
|
||||
return v_mutable;
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyV replace_children(const ASTNodeBinary* v) {
|
||||
auto* v_mutable = const_cast<ASTNodeBinary*>(v);
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyExprV replace_children(const ASTExprBinary* v) {
|
||||
auto* v_mutable = const_cast<ASTExprBinary*>(v);
|
||||
v_mutable->lhs = replace(v->lhs);
|
||||
v_mutable->rhs = replace(v->rhs);
|
||||
return v_mutable;
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyV replace_children(const ASTNodeVararg* v) {
|
||||
auto* v_mutable = const_cast<ASTNodeVararg*>(v);
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyExprV replace_children(const ASTExprVararg* v) {
|
||||
auto* v_mutable = const_cast<ASTExprVararg*>(v);
|
||||
for (AnyExprV& child : v_mutable->children) {
|
||||
child = replace(child);
|
||||
}
|
||||
return v_mutable;
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyV replace_children(const ASTStatementUnary* v) {
|
||||
auto* v_mutable = const_cast<ASTStatementUnary*>(v);
|
||||
v_mutable->child = replace(v_mutable->child);
|
||||
return v_mutable;
|
||||
}
|
||||
|
||||
GNU_ATTRIBUTE_ALWAYS_INLINE AnyV replace_children(const ASTStatementVararg* v) {
|
||||
auto* v_mutable = const_cast<ASTStatementVararg*>(v);
|
||||
for (AnyV& child : v_mutable->children) {
|
||||
child = replace(child);
|
||||
}
|
||||
|
@ -64,44 +78,50 @@ public:
|
|||
virtual ~ASTReplacer() = default;
|
||||
|
||||
virtual AnyV replace(AnyV v) = 0;
|
||||
virtual AnyExprV replace(AnyExprV v) = 0;
|
||||
};
|
||||
|
||||
class ASTReplacerInFunctionBody : public ASTReplacer {
|
||||
protected:
|
||||
using parent = ASTReplacerInFunctionBody;
|
||||
|
||||
virtual AnyV replace(V<ast_empty> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_parenthesized_expr> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_tensor> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_tensor_square> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_identifier> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_int_const> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_string_const> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_bool_const> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_null_keyword> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_function_call> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_dot_method_call> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_underscore> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_unary_operator> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_binary_operator> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_ternary_operator> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_return_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_sequence> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_repeat_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_while_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_do_while_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_throw_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_assert_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_try_catch_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_if_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_local_var> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_local_vars_declaration> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_asm_body> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_empty_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_return_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_sequence> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_repeat_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_while_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_do_while_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_throw_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_assert_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_try_catch_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_if_statement> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_local_vars_declaration> v) { return replace_children(v); }
|
||||
virtual AnyV replace(V<ast_asm_body> v) { return replace_children(v); }
|
||||
|
||||
AnyV replace(AnyV v) final {
|
||||
virtual AnyExprV replace(V<ast_empty_expression> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_parenthesized_expression> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_tensor> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_tensor_square> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_identifier> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_int_const> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_string_const> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_bool_const> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_null_keyword> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_self_keyword> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_argument> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_argument_list> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_function_call> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_dot_method_call> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_underscore> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_unary_operator> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_binary_operator> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_ternary_operator> v) { return replace_children(v); }
|
||||
virtual AnyExprV replace(V<ast_local_var> v) { return replace_children(v); }
|
||||
|
||||
AnyExprV replace(AnyExprV v) final {
|
||||
switch (v->type) {
|
||||
case ast_empty: return replace(v->as<ast_empty>());
|
||||
case ast_parenthesized_expr: return replace(v->as<ast_parenthesized_expr>());
|
||||
case ast_empty_expression: return replace(v->as<ast_empty_expression>());
|
||||
case ast_parenthesized_expression: return replace(v->as<ast_parenthesized_expression>());
|
||||
case ast_tensor: return replace(v->as<ast_tensor>());
|
||||
case ast_tensor_square: return replace(v->as<ast_tensor_square>());
|
||||
case ast_identifier: return replace(v->as<ast_identifier>());
|
||||
|
@ -110,12 +130,23 @@ protected:
|
|||
case ast_bool_const: return replace(v->as<ast_bool_const>());
|
||||
case ast_null_keyword: return replace(v->as<ast_null_keyword>());
|
||||
case ast_self_keyword: return replace(v->as<ast_self_keyword>());
|
||||
case ast_argument: return replace(v->as<ast_argument>());
|
||||
case ast_argument_list: return replace(v->as<ast_argument_list>());
|
||||
case ast_function_call: return replace(v->as<ast_function_call>());
|
||||
case ast_dot_method_call: return replace(v->as<ast_dot_method_call>());
|
||||
case ast_underscore: return replace(v->as<ast_underscore>());
|
||||
case ast_unary_operator: return replace(v->as<ast_unary_operator>());
|
||||
case ast_binary_operator: return replace(v->as<ast_binary_operator>());
|
||||
case ast_ternary_operator: return replace(v->as<ast_ternary_operator>());
|
||||
case ast_local_var: return replace(v->as<ast_local_var>());
|
||||
default:
|
||||
throw UnexpectedASTNodeType(v, "ASTReplacerInFunctionBody::replace");
|
||||
}
|
||||
}
|
||||
|
||||
AnyV replace(AnyV v) final {
|
||||
switch (v->type) {
|
||||
case ast_empty_statement: return replace(v->as<ast_empty_statement>());
|
||||
case ast_return_statement: return replace(v->as<ast_return_statement>());
|
||||
case ast_sequence: return replace(v->as<ast_sequence>());
|
||||
case ast_repeat_statement: return replace(v->as<ast_repeat_statement>());
|
||||
|
@ -125,11 +156,13 @@ protected:
|
|||
case ast_assert_statement: return replace(v->as<ast_assert_statement>());
|
||||
case ast_try_catch_statement: return replace(v->as<ast_try_catch_statement>());
|
||||
case ast_if_statement: return replace(v->as<ast_if_statement>());
|
||||
case ast_local_var: return replace(v->as<ast_local_var>());
|
||||
case ast_local_vars_declaration: return replace(v->as<ast_local_vars_declaration>());
|
||||
case ast_asm_body: return replace(v->as<ast_asm_body>());
|
||||
default:
|
||||
throw UnexpectedASTNodeType(v, "ASTReplacerInFunctionBody::visit");
|
||||
default: {
|
||||
// be very careful, don't forget to handle all statements (not expressions) above!
|
||||
AnyExprV as_expr = reinterpret_cast<const ASTNodeExpressionBase*>(v);
|
||||
return replace(as_expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,22 +172,18 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class ASTReplacerAllFunctionsInFile : public ASTReplacerInFunctionBody {
|
||||
protected:
|
||||
using parent = ASTReplacerAllFunctionsInFile;
|
||||
|
||||
virtual bool should_enter_function(V<ast_function_declaration> v) = 0;
|
||||
|
||||
public:
|
||||
void start_replacing_in_file(V<ast_tolk_file> v_file) {
|
||||
for (AnyV v : v_file->get_toplevel_declarations()) {
|
||||
if (auto v_function = v->try_as<ast_function_declaration>()) {
|
||||
if (should_enter_function(v_function)) {
|
||||
replace(v_function->get_body());
|
||||
template<class BodyReplacerT>
|
||||
void replace_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 (v_func->is_regular_function()) {
|
||||
BodyReplacerT visitor;
|
||||
visitor.start_replacing_in_function(v_func);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace tolk
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue