mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner. This is a common approach for functional languages, where types are inferred from usage through unification. As a result, type declarations are not necessary: () f(a,b) { return a+b; } // a and b now int, since `+` (int, int) While this approach works for now, problems arise with the introduction of new types like bool, where `!x` must handle both int and bool. It will also become incompatible with int32 and other strict integers. This will clash with structure methods, struggle with proper generics, and become entirely impractical for union types. This PR completely rewrites the type system targeting the future. 1) type of any expression is inferred and never changed 2) this is available because dependent expressions already inferred 3) forall completely removed, generic functions introduced (they work like template functions actually, instantiated while inferring) 4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)` 5) `as` keyword, for example `t.tupleAt(0) as int` 6) methods binding is done along with type inferring, not before ("before", as worked previously, was always a wrong approach)
This commit is contained in:
parent
3540424aa1
commit
799e2d1265
101 changed files with 5402 additions and 2713 deletions
|
@ -40,20 +40,15 @@ void FunctionBodyAsm::set_code(std::vector<AsmOp>&& code) {
|
|||
|
||||
|
||||
static void generate_output_func(const FunctionData* fun_ref) {
|
||||
tolk_assert(fun_ref->is_regular_function());
|
||||
tolk_assert(fun_ref->is_code_function());
|
||||
if (G.is_verbosity(2)) {
|
||||
std::cerr << "\n\n=========================\nfunction " << fun_ref->name << " : " << fun_ref->full_type << std::endl;
|
||||
std::cerr << "\n\n=========================\nfunction " << fun_ref->name << " : " << fun_ref->inferred_return_type << std::endl;
|
||||
}
|
||||
|
||||
CodeBlob* code = std::get<FunctionBodyCode*>(fun_ref->body)->code;
|
||||
if (G.is_verbosity(3)) {
|
||||
code->print(std::cerr, 9);
|
||||
}
|
||||
code->simplify_var_types();
|
||||
if (G.is_verbosity(5)) {
|
||||
std::cerr << "after simplify_var_types: \n";
|
||||
code->print(std::cerr, 0);
|
||||
}
|
||||
code->prune_unreachable_code();
|
||||
if (G.is_verbosity(5)) {
|
||||
std::cerr << "after prune_unreachable: \n";
|
||||
|
@ -112,11 +107,11 @@ static void generate_output_func(const FunctionData* fun_ref) {
|
|||
}
|
||||
}
|
||||
|
||||
void pipeline_generate_fif_output_to_std_cout(const AllSrcFiles& all_src_files) {
|
||||
void pipeline_generate_fif_output_to_std_cout() {
|
||||
std::cout << "\"Asm.fif\" include\n";
|
||||
std::cout << "// automatically generated from ";
|
||||
bool need_comma = false;
|
||||
for (const SrcFile* file : all_src_files) {
|
||||
for (const SrcFile* file : G.all_src_files) {
|
||||
if (!file->is_stdlib_file()) {
|
||||
if (need_comma) {
|
||||
std::cout << ", ";
|
||||
|
@ -129,9 +124,9 @@ void pipeline_generate_fif_output_to_std_cout(const AllSrcFiles& all_src_files)
|
|||
std::cout << "PROGRAM{\n";
|
||||
|
||||
bool has_main_procedure = false;
|
||||
for (const FunctionData* fun_ref : G.all_code_functions) {
|
||||
for (const FunctionData* fun_ref : G.all_functions) {
|
||||
if (!fun_ref->does_need_codegen()) {
|
||||
if (G.is_verbosity(2)) {
|
||||
if (G.is_verbosity(2) && fun_ref->is_code_function()) {
|
||||
std::cerr << fun_ref->name << ": code not generated, function does not need codegen\n";
|
||||
}
|
||||
continue;
|
||||
|
@ -164,7 +159,7 @@ void pipeline_generate_fif_output_to_std_cout(const AllSrcFiles& all_src_files)
|
|||
std::cout << std::string(2, ' ') << "DECLGLOBVAR " << var_ref->name << "\n";
|
||||
}
|
||||
|
||||
for (const FunctionData* fun_ref : G.all_code_functions) {
|
||||
for (const FunctionData* fun_ref : G.all_functions) {
|
||||
if (!fun_ref->does_need_codegen()) {
|
||||
continue;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue