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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Convert all paths in AST into absolute form (or to the relevant local item)
* - NOTE: This is the core of the 'resolve' pass.
*
* After complete there should be no:
* - Relative/super/self paths
* - MaybeBind patterns
*/
#include <ast/crate.hpp>
#include <ast/ast.hpp>
#include <main_bindings.hpp>
struct GenericSlot
{
enum class Level
{
Impl,
Function,
} level;
unsigned short index;
};
template<typename Val>
struct Named
{
::std::string name;
Val value;
};
struct Context
{
TAGGED_UNION(Ent, Module,
(Module, struct {
const ::AST::Module* mod;
}),
(VarBlock, struct {
// "Map" of names to function-level variable slots
::std::vector< Named< unsigned int > > variables;
}),
(Generic, struct {
// Map of names to slots
::std::vector< Named< GenericSlot > > types;
::std::vector< Named< GenericSlot > > constants;
::std::vector< Named< GenericSlot > > lifetimes;
})
);
const ::AST::Crate& m_crate;
const ::AST::Module& m_mod;
::std::vector<Ent> m_name_context;
Context(const ::AST::Crate& crate, const::AST::Module& mod):
m_crate(crate),
m_mod(mod)
{}
AST::Path lookup_type(const Span& sp, const ::std::string& name) const {
return this->lookup(sp, name, true);
}
AST::Path lookup_value(const Span& sp, const ::std::string& name) const {
return this->lookup(sp, name, false);
}
AST::Path lookup(const Span& sp, const ::std::string& name, bool is_type) const {
for(auto it = m_name_context.rbegin(); it != m_name_context.rend(); ++ it)
{
TU_MATCH(Ent, (*it), (e),
(Module,
// TODO: m_type_items/m_value_items should store the path
if( is_type ) {
auto v = e.mod->m_type_items.find(name);
if( v != e.mod->m_type_items.end() ) {
return e.mod->path() + name;
}
}
else {
auto v = e.mod->m_value_items.find(name);
if( v != e.mod->m_value_items.end() ) {
return e.mod->path() + name;
}
}
),
(VarBlock,
if( is_type ) {
// ignore
}
else {
TODO(sp, "resolve/absolute.cpp - Context::lookup - Handle VarBlock");
}
),
(Generic,
if( is_type ) {
TODO(sp, "resolve/absolute.cpp - Context::lookup - Handle Generic");
}
else {
// ignore
}
)
)
}
// Top-level module
if( is_type ) {
auto v = m_mod.m_type_items.find(name);
if( v != m_mod.m_type_items.end() ) {
return m_mod.path() + name;
}
}
else {
auto v = m_mod.m_value_items.find(name);
if( v != m_mod.m_value_items.end() ) {
return m_mod.path() + name;
}
}
ERROR(sp, E0000, "Couldn't find name '" << name << "'");
}
};
void Resolve_Absolute_Path(const Context& context, const Span& sp, bool is_type, ::AST::Path& path);
void Resolve_Absolute_Type(const Context& context, TypeRef& type);
void Resolve_Absolute_Expr(const Context& context, ::AST::Expr& expr);
void Resolve_Absolute_Expr(const Context& context, ::AST::ExprNode& node);
void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod);
void Resolve_Absolute_Path(const Context& context, const Span& sp, bool is_type, ::AST::Path& path)
{
TU_MATCH(::AST::Path::Class, (path.m_class), (e),
(Invalid,
BUG(sp, "Encountered invalid path");
),
(Local,
// Nothing to do (TODO: Check that it's valid?)
),
(Relative,
if(e.nodes.size() == 0)
BUG(sp, "Resolve_Absolute_Path - Relative path with no nodes");
if(e.nodes.size() > 1 || is_type) {
// Look up type
auto p = context.lookup_type(sp, e.nodes[0].name());
DEBUG("Found - " << p);
path = mv$(p);
}
else {
// Look up value
auto p = context.lookup_value(sp, e.nodes[0].name());
DEBUG("Found val - " << p);
path = mv$(p);
}
),
(Self,
TODO(sp, "Resolve_Absolute_Path - Self-relative paths - " << path);
),
(Super,
TODO(sp, "Resolve_Absolute_Path - Super-relative paths - " << path);
),
(Absolute,
// Nothing to do (TODO: Bind?)
),
(UFCS,
TODO(sp, "Resolve_Absolute_Path - UFCS");
)
)
}
void Resolve_Absolute_Type(const Context& context, TypeRef& type)
{
const auto& sp = type.span();
TU_MATCH(TypeData, (type.m_data), (e),
(None,
// ! type
),
(Any,
// _ type
),
(Unit,
),
(Macro,
BUG(sp, "Resolve_Absolute_Type - Encountered an unexpanded macro");
),
(Primitive,
),
(Function,
TODO(sp, "Resolve_Absolute_Type - Function - " << type);
),
(Tuple,
for(auto& t : e.inner_types)
Resolve_Absolute_Type(context, t);
),
(Borrow,
Resolve_Absolute_Type(context, *e.inner);
),
(Pointer,
Resolve_Absolute_Type(context, *e.inner);
),
(Array,
Resolve_Absolute_Type(context, *e.inner);
Resolve_Absolute_Expr(context, *e.size);
),
(Generic,
TODO(sp, "Resolve_Absolute_Type - Encountered generic");
),
(Path,
TODO(sp, "Resolve_Absolute_Type - Path");
),
(TraitObject,
TODO(sp, "Resolve_Absolute_Type - TraitObject");
)
)
}
void Resolve_Absolute_Expr(const Context& context, ::AST::Expr& expr)
{
if( expr.is_valid() )
{
Resolve_Absolute_Expr(context, expr.node());
}
}
void Resolve_Absolute_Expr(const Context& context, ::AST::ExprNode& node)
{
TRACE_FUNCTION_F("");
struct NV:
public AST::NodeVisitorDef
{
const Context& context;
NV(const Context& context):
context(context)
{
}
void visit(AST::ExprNode_Block& node) override {
if( node.m_local_mod ) {
Resolve_Absolute_Mod(this->context.m_crate, *node.m_local_mod);
//this->context.push( *node.m_local_mod );
}
AST::NodeVisitorDef::visit(node);
if( node.m_local_mod ) {
//this->context.pop();
}
}
void visit(AST::ExprNode_Match& node) override {
TODO(node.get_pos(), "Resolve_Absolute_Expr - ExprNode_Match");
}
void visit(AST::ExprNode_LetBinding& node) override {
TODO(Span(), "Resolve_Absolute_Expr - ExprNode_LetBinding");
}
void visit(AST::ExprNode_StructLiteral& node) override {
TODO(Span(), "Resolve_Absolute_Expr - ExprNode_StructLiteral");
}
void visit(AST::ExprNode_CallPath& node) override {
TODO(Span(), "Resolve_Absolute_Expr - ExprNode_CallPath");
}
void visit(AST::ExprNode_NamedValue& node) override {
Resolve_Absolute_Path(this->context, Span(node.get_pos()), false, node.m_path);
}
void visit(AST::ExprNode_Cast& node) override {
Resolve_Absolute_Type(this->context, node.m_type);
AST::NodeVisitorDef::visit(node);
}
} expr_iter(context);
node.visit( expr_iter );
}
void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod)
{
TRACE_FUNCTION_F("(mod="<<mod.path()<<")");
for( auto& i : mod.items() )
{
TU_MATCH(AST::Item, (i.data), (e),
(None,
),
(Module,
Resolve_Absolute_Mod(crate, e);
),
(Crate,
// - Nothing
),
(Enum,
TODO(Span(), "Resolve_Absolute_Mod - Enum");
),
(Trait,
TODO(Span(), "Resolve_Absolute_Mod - Trait");
),
(Type,
TODO(Span(), "Resolve_Absolute_Mod - Type");
),
(Struct,
TODO(Span(), "Resolve_Absolute_Mod - Struct");
),
(Function,
TODO(Span(), "Resolve_Absolute_Mod - Function");
),
(Static,
Resolve_Absolute_Type( Context(crate, mod), e.type() );
Resolve_Absolute_Expr( Context(crate, mod), e.value() );
)
)
}
}
void Resolve_Absolutise(AST::Crate& crate)
{
Resolve_Absolute_Mod(crate, crate.root_module());
}
|