-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsymtable.h
239 lines (194 loc) · 9.29 KB
/
symtable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "src-file.h"
#include "fwd-declarations.h"
#include "constant-evaluator.h"
#include "crypto/common/refint.h"
#include <unordered_map>
#include <variant>
#include <vector>
namespace tolk {
struct Symbol {
std::string name;
SrcLocation loc;
Symbol(std::string name, SrcLocation loc)
: name(std::move(name))
, loc(loc) {
}
virtual ~Symbol() = default;
template<class ConstTPtr>
ConstTPtr try_as() const {
#ifdef TOLK_DEBUG
assert(this != nullptr);
#endif
return dynamic_cast<ConstTPtr>(this);
}
};
struct LocalVarData final : Symbol {
enum {
flagMutateParameter = 1, // parameter was declared with `mutate` keyword
flagImmutable = 2, // variable was declared via `val` (not `var`)
};
TypePtr declared_type; // either at declaration `var x:int`, or if omitted, from assigned value `var x=2`
int flags;
int param_idx; // 0...N for function parameters, -1 for local vars
std::vector<int> ir_idx;
LocalVarData(std::string name, SrcLocation loc, TypePtr declared_type, int flags, int param_idx)
: Symbol(std::move(name), loc)
, declared_type(declared_type)
, flags(flags)
, param_idx(param_idx) {
}
bool is_parameter() const { return param_idx >= 0; }
bool is_immutable() const { return flags & flagImmutable; }
bool is_mutate_parameter() const { return flags & flagMutateParameter; }
LocalVarData* mutate() const { return const_cast<LocalVarData*>(this); }
void assign_ir_idx(std::vector<int>&& ir_idx);
void assign_resolved_type(TypePtr declared_type);
void assign_inferred_type(TypePtr inferred_type);
};
struct FunctionBodyCode;
struct FunctionBodyAsm;
struct FunctionBodyBuiltin;
struct GenericsDeclaration;
struct GenericsInstantiation;
typedef std::variant<
FunctionBodyCode*,
FunctionBodyAsm*,
FunctionBodyBuiltin*
> FunctionBody;
struct FunctionData final : Symbol {
static constexpr int EMPTY_METHOD_ID = -10;
enum {
flagInline = 1, // marked `@inline`
flagInlineRef = 2, // marked `@inline_ref`
flagTypeInferringDone = 4, // type inferring step of function's body (all AST nodes assigning v->inferred_type) is done
flagUsedAsNonCall = 8, // used not only as `f()`, but as a 1-st class function (assigned to var, pushed to tuple, etc.)
flagMarkedAsPure = 16, // declared as `pure`, can't call impure and access globals, unused invocations are optimized out
flagImplicitReturn = 32, // control flow reaches end of function, so it needs implicit return at the end
flagGetMethod = 64, // was declared via `get func(): T`, method_id is auto-assigned
flagIsEntrypoint = 128, // it's `main` / `onExternalMessage` / etc.
flagHasMutateParams = 256, // has parameters declared as `mutate`
flagAcceptsSelf = 512, // is a member function (has `self` first parameter)
flagReturnsSelf = 1024, // return type is `self` (returns the mutated 1st argument), calls can be chainable
flagReallyUsed = 2048, // calculated via dfs from used functions; declared but unused functions are not codegenerated
flagCompileTimeOnly = 4096, // calculated only at compile-time for constant arguments: `ton("0.05")`, `stringCrc32`, and others
};
int method_id = EMPTY_METHOD_ID;
int flags;
std::vector<LocalVarData> parameters;
std::vector<int> arg_order, ret_order;
TypePtr declared_return_type; // may be nullptr, meaning "auto infer"
TypePtr inferred_return_type = nullptr; // assigned on type inferring
TypePtr inferred_full_type = nullptr; // assigned on type inferring, it's TypeDataFunCallable(params -> return)
const GenericsDeclaration* genericTs;
const GenericsInstantiation* instantiationTs;
FunctionBody body;
AnyV ast_root; // V<ast_function_declaration> for user-defined (not builtin)
FunctionData(std::string name, SrcLocation loc, TypePtr declared_return_type, std::vector<LocalVarData> parameters, int initial_flags, const GenericsDeclaration* genericTs, const GenericsInstantiation* instantiationTs, FunctionBody body, AnyV ast_root)
: Symbol(std::move(name), loc)
, flags(initial_flags)
, parameters(std::move(parameters))
, declared_return_type(declared_return_type)
, genericTs(genericTs)
, instantiationTs(instantiationTs)
, body(body)
, ast_root(ast_root) {
}
std::string as_human_readable() const;
const std::vector<int>* get_arg_order() const {
return arg_order.empty() ? nullptr : &arg_order;
}
const std::vector<int>* get_ret_order() const {
return ret_order.empty() ? nullptr : &ret_order;
}
int get_num_params() const { return static_cast<int>(parameters.size()); }
const LocalVarData& get_param(int idx) const { return parameters[idx]; }
bool is_code_function() const { return std::holds_alternative<FunctionBodyCode*>(body); }
bool is_asm_function() const { return std::holds_alternative<FunctionBodyAsm*>(body); }
bool is_builtin_function() const { return ast_root == nullptr; }
bool is_generic_function() const { return genericTs != nullptr; }
bool is_instantiation_of_generic_function() const { return instantiationTs != nullptr; }
bool is_inline() const { return flags & flagInline; }
bool is_inline_ref() const { return flags & flagInlineRef; }
bool is_type_inferring_done() const { return flags & flagTypeInferringDone; }
bool is_used_as_noncall() const { return flags & flagUsedAsNonCall; }
bool is_marked_as_pure() const { return flags & flagMarkedAsPure; }
bool is_implicit_return() const { return flags & flagImplicitReturn; }
bool is_get_method() const { return flags & flagGetMethod; }
bool is_method_id_not_empty() const { return method_id != EMPTY_METHOD_ID; }
bool is_entrypoint() const { return flags & flagIsEntrypoint; }
bool has_mutate_params() const { return flags & flagHasMutateParams; }
bool does_accept_self() const { return flags & flagAcceptsSelf; }
bool does_return_self() const { return flags & flagReturnsSelf; }
bool does_mutate_self() const { return (flags & flagAcceptsSelf) && parameters[0].is_mutate_parameter(); }
bool is_really_used() const { return flags & flagReallyUsed; }
bool is_compile_time_only() const { return flags & flagCompileTimeOnly; }
bool does_need_codegen() const;
FunctionData* mutate() const { return const_cast<FunctionData*>(this); }
void assign_resolved_type(TypePtr declared_return_type);
void assign_inferred_type(TypePtr inferred_return_type, TypePtr inferred_full_type);
void assign_is_used_as_noncall();
void assign_is_implicit_return();
void assign_is_type_inferring_done();
void assign_is_really_used();
void assign_arg_order(std::vector<int>&& arg_order);
};
struct GlobalVarData final : Symbol {
enum {
flagReallyUsed = 1, // calculated via dfs from used functions; unused globals are not codegenerated
};
TypePtr declared_type; // always exists, declaring globals without type is prohibited
int flags = 0;
GlobalVarData(std::string name, SrcLocation loc, TypePtr declared_type)
: Symbol(std::move(name), loc)
, declared_type(declared_type) {
}
bool is_really_used() const { return flags & flagReallyUsed; }
GlobalVarData* mutate() const { return const_cast<GlobalVarData*>(this); }
void assign_resolved_type(TypePtr declared_type);
void assign_is_really_used();
};
struct GlobalConstData final : Symbol {
AnyExprV init_value;
ConstantValue value;
TypePtr declared_type; // `const a: int = ...`; nullptr for `const a = ...`
TypePtr inferred_type = nullptr; // filled at type inferring pass
GlobalConstData(std::string name, SrcLocation loc, TypePtr declared_type, AnyExprV init_value)
: Symbol(std::move(name), loc)
, init_value(init_value)
, declared_type(declared_type) {
}
GlobalConstData* mutate() const { return const_cast<GlobalConstData*>(this); }
void assign_resolved_type(TypePtr declared_type);
void assign_inferred_type(TypePtr inferred_type);
void assign_const_value(ConstantValue&& value);
};
class GlobalSymbolTable {
std::unordered_map<uint64_t, const Symbol*> entries;
static uint64_t key_hash(std::string_view name_key) {
return std::hash<std::string_view>{}(name_key);
}
public:
void add_function(FunctionPtr f_sym);
void add_global_var(GlobalVarPtr g_sym);
void add_global_const(GlobalConstPtr c_sym);
const Symbol* lookup(std::string_view name) const {
const auto it = entries.find(key_hash(name));
return it == entries.end() ? nullptr : it->second;
}
};
const Symbol* lookup_global_symbol(std::string_view name);
} // namespace tolk