1
0
Fork 0
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:
tolk-vm 2024-12-30 22:31:27 +07:00
parent 3540424aa1
commit 799e2d1265
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
101 changed files with 5402 additions and 2713 deletions

View file

@ -17,28 +17,48 @@
#include "symtable.h"
#include "compiler-state.h"
#include "platform-utils.h"
#include <sstream>
#include <cassert>
#include "generics-helpers.h"
namespace tolk {
std::string FunctionData::as_human_readable() const {
if (!genericTs) {
return name; // if it's generic instantiation like `f<int>`, its name is "f<int>", not "f"
}
return name + genericTs->as_human_readable();
}
bool FunctionData::does_need_codegen() const {
// when a function is declared, but not referenced from code in any way, don't generate its body
if (!is_really_used() && G.settings.remove_unused_functions) {
return false;
}
// functions with asm body don't need code generation
// (even if used as non-call: `var a = beginCell;` inserts TVM continuation inline)
if (is_asm_function() || is_builtin_function()) {
return false;
}
// when a function is referenced like `var a = some_fn;` (or in some other non-call way), its continuation should exist
if (is_used_as_noncall()) {
return true;
}
// generic functions also don't need code generation, only generic instantiations do
if (is_generic_function()) {
return false;
}
// currently, there is no inlining, all functions are codegenerated
// (but actually, unused ones are later removed by Fift)
// in the future, we may want to implement a true AST inlining for "simple" functions
return true;
}
void FunctionData::assign_is_really_used() {
this->flags |= flagReallyUsed;
void FunctionData::assign_resolved_type(TypePtr declared_return_type) {
this->declared_return_type = declared_return_type;
}
void FunctionData::assign_inferred_type(TypePtr inferred_return_type, TypePtr inferred_full_type) {
this->inferred_return_type = inferred_return_type;
this->inferred_full_type = inferred_full_type;
}
void FunctionData::assign_is_used_as_noncall() {
@ -49,14 +69,45 @@ void FunctionData::assign_is_implicit_return() {
this->flags |= flagImplicitReturn;
}
void FunctionData::assign_is_type_inferring_done() {
this->flags |= flagTypeInferringDone;
}
void FunctionData::assign_is_really_used() {
this->flags |= flagReallyUsed;
}
void FunctionData::assign_arg_order(std::vector<int>&& arg_order) {
this->arg_order = std::move(arg_order);
}
void GlobalVarData::assign_resolved_type(TypePtr declared_type) {
this->declared_type = declared_type;
}
void GlobalVarData::assign_is_really_used() {
this->flags |= flagReallyUsed;
}
void GlobalConstData::assign_resolved_type(TypePtr declared_type) {
this->declared_type = declared_type;
}
void LocalVarData::assign_idx(int idx) {
this->idx = idx;
}
void LocalVarData::assign_resolved_type(TypePtr declared_type) {
this->declared_type = declared_type;
}
void LocalVarData::assign_inferred_type(TypePtr inferred_type) {
#ifdef TOLK_DEBUG
assert(this->declared_type == nullptr); // called when type declaration omitted, inferred from assigned value
#endif
this->declared_type = inferred_type;
}
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
static void fire_error_redefinition_of_symbol(SrcLocation loc, const Symbol* previous) {
SrcLocation prev_loc = previous->loc;