1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

[Tolk] Nullable types T? and null safety

This commit introduces nullable types `T?` that are
distinct from non-nullable `T`.
Example: `int?` (int or null) and `int` are different now.
Previously, `null` could be assigned to any primitive type.
Now, it can be assigned only to `T?`.

A non-null assertion operator `!` was also introduced,
similar to `!` in TypeScript and `!!` in Kotlin.

If `int?` still occupies 1 stack slot, `(int,int)?` and
other nullable tensors occupy N+1 slots, the last for
"null precedence". `v == null` actually compares that slot.
Assigning `(int,int)` to `(int,int)?` implicitly creates
a null presence slot. Assigning `null` to `(int,int)?` widens
this null value to 3 slots. This is called "type transitioning".

All stdlib functions prototypes have been updated to reflect
whether they return/accept a nullable or a strict value.

This commit also contains refactoring from `const FunctionData*`
to `FunctionPtr` and similar.
This commit is contained in:
tolk-vm 2025-02-24 20:13:36 +03:00
parent 1389ff6789
commit f3e620f48c
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
62 changed files with 2031 additions and 702 deletions

View file

@ -45,7 +45,7 @@ typedef int const_idx_t;
struct TmpVar {
var_idx_t ir_idx; // every var in IR represents 1 stack slot
TypePtr v_type; // calc_width_on_stack() is 1
TypePtr v_type; // get_width_on_stack() is 1
std::string name; // "x" for vars originated from user sources; "x.0" for tensor components; empty for implicitly created tmp vars
SrcLocation loc; // location of var declaration in sources or where a tmp var was originated
#ifdef TOLK_DEBUG
@ -283,8 +283,8 @@ struct Op {
enum { _Disabled = 1, _NoReturn = 4, _Impure = 24 };
int flags;
std::unique_ptr<Op> next;
const FunctionData* f_sym = nullptr;
const GlobalVarData* g_sym = nullptr;
FunctionPtr f_sym = nullptr;
GlobalVarPtr g_sym = nullptr;
SrcLocation where;
VarDescrList var_info;
std::vector<VarDescr> args;
@ -313,19 +313,19 @@ struct Op {
: cl(_cl), flags(0), f_sym(nullptr), where(_where), left(std::move(_left)), right(std::move(_right)) {
}
Op(SrcLocation _where, OpKind _cl, const std::vector<var_idx_t>& _left, const std::vector<var_idx_t>& _right,
const FunctionData* _fun)
FunctionPtr _fun)
: cl(_cl), flags(0), f_sym(_fun), where(_where), left(_left), right(_right) {
}
Op(SrcLocation _where, OpKind _cl, std::vector<var_idx_t>&& _left, std::vector<var_idx_t>&& _right,
const FunctionData* _fun)
FunctionPtr _fun)
: cl(_cl), flags(0), f_sym(_fun), where(_where), left(std::move(_left)), right(std::move(_right)) {
}
Op(SrcLocation _where, OpKind _cl, const std::vector<var_idx_t>& _left, const std::vector<var_idx_t>& _right,
const GlobalVarData* _gvar)
GlobalVarPtr _gvar)
: cl(_cl), flags(0), g_sym(_gvar), where(_where), left(_left), right(_right) {
}
Op(SrcLocation _where, OpKind _cl, std::vector<var_idx_t>&& _left, std::vector<var_idx_t>&& _right,
const GlobalVarData* _gvar)
GlobalVarPtr _gvar)
: cl(_cl), flags(0), g_sym(_gvar), where(_where), left(std::move(_left)), right(std::move(_right)) {
}
@ -1083,7 +1083,7 @@ struct FunctionBodyAsm {
struct CodeBlob {
int var_cnt, in_var_cnt;
const FunctionData* fun_ref;
FunctionPtr fun_ref;
std::string name;
SrcLocation loc;
std::vector<TmpVar> vars;
@ -1094,7 +1094,7 @@ struct CodeBlob {
#endif
std::stack<std::unique_ptr<Op>*> cur_ops_stack;
bool require_callxargs = false;
CodeBlob(std::string name, SrcLocation loc, const FunctionData* fun_ref)
CodeBlob(std::string name, SrcLocation loc, FunctionPtr fun_ref)
: var_cnt(0), in_var_cnt(0), fun_ref(fun_ref), name(std::move(name)), loc(loc), cur_ops(&ops) {
}
template <typename... Args>