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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* Absolutise and check all 'use' statements
*/
#include <main_bindings.hpp>
#include <ast/crate.hpp>
#include <ast/ast.hpp>
#include <ast/expr.hpp>
#include <hir/hir.hpp>
::AST::Path Resolve_Use_AbsolutisePath(const ::AST::Path& base_path, ::AST::Path path);
void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path path, slice< const ::AST::Module* > parent_modules={});
::AST::PathBinding Resolve_Use_GetBinding(const Span& span, const ::AST::Crate& crate, const ::AST::Path& path, slice< const ::AST::Module* > parent_modules);
void Resolve_Use(::AST::Crate& crate)
{
Resolve_Use_Mod(crate, crate.m_root_module, ::AST::Path("", {}));
}
::AST::Path Resolve_Use_AbsolutisePath(const Span& span, const ::AST::Path& base_path, ::AST::Path path)
{
TU_MATCH(::AST::Path::Class, (path.m_class), (e),
(Invalid,
// Should never happen
BUG(span, "Invalid path class encountered");
),
(Local,
// Wait, how is this already known?
BUG(span, "Local path class in use statement");
),
(UFCS,
// Wait, how is this already known?
BUG(span, "UFCS path class in use statement");
),
(Relative,
return base_path + path;
),
(Self,
// EVIL HACK: If the current module is an anon module, refer to the parent
if( base_path.nodes().back().name()[0] == '#' ) {
AST::Path np("", {});
for( unsigned int i = 0; i < base_path.nodes().size() - 1; i ++ )
np.nodes().push_back( base_path.nodes()[i] );
np += path;
return np;
}
else {
return base_path + path;
}
),
(Super,
assert(e.count >= 1);
AST::Path np("", {});
if( e.count > base_path.nodes().size() ) {
ERROR(span, E0000, "Too many `super` components");
}
for( unsigned int i = 0; i < base_path.nodes().size() - e.count; i ++ )
np.nodes().push_back( base_path.nodes()[i] );
np += path;
return np;
),
(Absolute,
// Leave as is
return path;
)
)
throw "BUG: Reached end of Resolve_Use_AbsolutisePath";
}
void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path path, slice< const ::AST::Module* > parent_modules)
{
TRACE_FUNCTION_F("path = " << path << ", mod.path() = " << mod.path());
for(auto& use_stmt : mod.imports())
{
const Span& span = use_stmt.data.sp;
use_stmt.data.path = Resolve_Use_AbsolutisePath(span, path, mv$(use_stmt.data.path));
if( !use_stmt.data.path.m_class.is_Absolute() )
BUG(span, "Use path is not absolute after absolutisation");
// TODO: Is this a valid assertion?
//if( use_stmt.data.path.crate() != "" )
// BUG(span, "Use path crate was set before resolve");
use_stmt.data.path.bind( Resolve_Use_GetBinding(span, crate, use_stmt.data.path, parent_modules) );
// - If doing a glob, ensure the item type is valid
if( use_stmt.name == "" )
{
TU_MATCH_DEF(::AST::PathBinding, (use_stmt.data.path.binding()), (e),
(
ERROR(span, E0000, "Wildcard import of invalid item type");
),
(Enum,
),
(Module,
)
)
}
}
for(auto& i : mod.items())
{
if( i.data.is_Module() )
{
Resolve_Use_Mod(crate, i.data.as_Module(), path + i.name);
}
}
/*
unsigned int idx = 0;
for(auto& mp : mod.anon_mods())
{
Resolve_Use_Mod(crate, *mp, path + FMT("#" << idx));
idx ++;
}
*/
// TODO: Handle anon modules by iterating code (allowing correct item mappings)
struct NV:
public AST::NodeVisitorDef
{
const AST::Crate& crate;
::std::vector< const AST::Module* > parent_modules;
NV(const AST::Crate& crate, const AST::Module& cur_module):
crate(crate),
parent_modules()
{
parent_modules.push_back( &cur_module );
}
void visit(AST::ExprNode_Block& node) override {
if( node.m_local_mod ) {
Resolve_Use_Mod(this->crate, *node.m_local_mod, node.m_local_mod->path(), this->parent_modules);
parent_modules.push_back(&*node.m_local_mod);
}
AST::NodeVisitorDef::visit(node);
if( node.m_local_mod ) {
parent_modules.pop_back();
}
}
} expr_iter(crate, mod);
// TODO: Check that all code blocks are covered by these two
for(auto& i : mod.items())
{
TU_MATCH_DEF( AST::Item, (i.data), (e),
(
),
(Function,
if( e.code().is_valid() ) {
e.code().node().visit( expr_iter );
}
),
(Static,
if( e.value().is_valid() ) {
e.value().node().visit( expr_iter );
}
)
)
}
for(auto& im : mod.impls())
{
for(auto& i : im.items())
{
TU_MATCH_DEF( AST::Item, (*i.data), (e),
(
),
(Function,
if( e.code().is_valid() ) {
e.code().node().visit( expr_iter );
}
),
(Static,
if( e.value().is_valid() ) {
e.value().node().visit( expr_iter );
}
)
)
}
}
}
::AST::PathBinding Resolve_Use_GetBinding_Mod(const Span& span, const ::AST::Crate& crate, const ::AST::Module& mod, const ::std::string& des_item_name, slice< const ::AST::Module* > parent_modules)
{
// HACK - Catch the possibiliy of a name clash (not sure if this is really an error)
{
bool found = false;
for( const auto& item : mod.items() )
{
if( item.data.is_None() )
continue ;
if( item.name == des_item_name ) {
if( found ) {
TODO(span, "Duplicate name found resolving use statement searching in module " << mod.path());
}
found = true;
}
}
}
if( des_item_name[0] == '#' ) {
unsigned int idx = 0;
if( ::std::sscanf(des_item_name.c_str(), "#%u", &idx) != 1 ) {
BUG(span, "Invalid anon path segment '" << des_item_name << "'");
}
if( idx >= mod.anon_mods().size() ) {
BUG(span, "Invalid anon path segment '" << des_item_name << "'");
}
return ::AST::PathBinding::make_Module({mod.anon_mods()[idx]});
}
for( const auto& item : mod.items() )
{
if( item.data.is_None() )
continue ;
if( item.name == des_item_name ) {
TU_MATCH(::AST::Item, (item.data), (e),
(None,
// IMPOSSIBLE - Handled above
),
(MacroInv,
BUG(span, "Hit MacroInv in use resolution");
),
(Crate,
return ::AST::PathBinding::make_Crate({ &crate.m_extern_crates.at(e.name) });
),
(Type,
return ::AST::PathBinding::make_TypeAlias({&e});
),
(Trait,
return ::AST::PathBinding::make_Trait({&e});
),
(Function,
return ::AST::PathBinding::make_Function({&e});
),
(Static,
return ::AST::PathBinding::make_Static({&e});
),
(Struct,
return ::AST::PathBinding::make_Struct({&e});
),
(Enum,
return ::AST::PathBinding::make_Enum({&e});
),
(Module,
return ::AST::PathBinding::make_Module({&e});
)
)
break ;
}
}
// Imports
for( const auto& imp : mod.imports() )
{
const Span& sp2 = imp.data.sp;
if( imp.name == des_item_name ) {
DEBUG("- Named import " << imp.name << " = " << imp.data);
if( imp.data.path.binding().is_Unbound() ) {
DEBUG(" > Needs resolve");
// TODO: Handle possibility of recursion
return Resolve_Use_GetBinding(sp2, crate, Resolve_Use_AbsolutisePath(sp2, mod.path(), imp.data.path), parent_modules);
}
else {
return imp.data.path.binding().clone();
}
}
if( imp.is_pub && imp.name == "" ) {
// INEFFICIENT! Resolves and throws away the result (because we can't/shouldn't mutate here)
::AST::PathBinding binding_;
const auto* binding = &imp.data.path.binding();
if( binding->is_Unbound() ) {
DEBUG("Temp resolving wildcard " << imp.data);
// TODO: Handle possibility of recursion
binding_ = Resolve_Use_GetBinding(sp2, crate, Resolve_Use_AbsolutisePath(sp2, mod.path(), imp.data.path), parent_modules);
binding = &binding_;
}
TU_MATCH_DEF(::AST::PathBinding, ((*binding)), (e),
(
BUG(sp2, "Wildcard import expanded to an invalid item class");
),
(Module,
TODO(span, "Look up wildcard in module");
),
(Enum,
TODO(span, "Look up wildcard in enum");
)
)
}
}
if( mod.path().nodes().size() > 0 && mod.path().nodes().back().name()[0] == '#' ) {
assert( parent_modules.size() > 0 );
return Resolve_Use_GetBinding_Mod(span, crate, *parent_modules.back(), des_item_name, parent_modules.subslice(0, parent_modules.size()-1));
}
else {
ERROR(span, E0000, "Could not find node '" << des_item_name << "' in module " << mod.path());
}
}
::AST::PathBinding Resolve_Use_GetBinding(const Span& span, const ::AST::Crate& crate, const ::AST::Path& path, slice< const ::AST::Module* > parent_modules)
{
const AST::Module* mod = &crate.m_root_module;
const auto& nodes = path.nodes();
for( unsigned int i = 0; i < nodes.size()-1; i ++ )
{
auto b = Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes[i].name(), parent_modules);
TU_MATCH_DEF(::AST::PathBinding, (b), (e),
(
ERROR(span, E0000, "Unexpected item type in import");
),
(Crate,
const ::HIR::Module* hmod = &e.crate_->m_hir->m_root_module;
i ++;
for(; i < nodes.size() - 1; i ++)
{
auto it = hmod->m_mod_items.find(nodes[i].name());
if( it == hmod->m_mod_items.end() ) {
// BZZT!
ERROR(span, E0000, "Unable to find path component " << nodes[i].name() << " in " << path);
}
TU_MATCH_DEF( ::HIR::TypeItem, (it->second->ent), (e),
(
ERROR(span, E0000, "Unexpected item type in import");
),
(Module,
hmod = &e;
),
(Enum,
i += 1;
if( i != nodes.size() - 1 ) {
ERROR(span, E0000, "Encountered enum at unexpected location in import");
}
const auto& name = nodes[i].name();
auto it2 = ::std::find_if( e.m_variants.begin(), e.m_variants.end(), [&](const auto& x){ return x.first == name; } );
if( it2 == e.m_variants.end() ) {
ERROR(span, E0000, "Unable to find variant " << path);
}
return ::AST::PathBinding::make_EnumVar({ nullptr, static_cast<unsigned int>(it2 - e.m_variants.begin()) });
)
)
}
auto it = hmod->m_mod_items.find(nodes[i].name());
if( it != hmod->m_mod_items.end() ) {
TU_MATCHA( (it->second->ent), (e),
(Import,
TODO(span, "Recurse to get binding for an import");
),
(Module,
return ::AST::PathBinding::make_Module({nullptr});
),
(TypeAlias,
return ::AST::PathBinding::make_TypeAlias({nullptr});
),
(Enum,
return ::AST::PathBinding::make_Enum({nullptr});
),
(Struct,
return ::AST::PathBinding::make_Struct({nullptr});
),
(Trait,
return ::AST::PathBinding::make_Trait({nullptr});
)
)
}
auto it2 = hmod->m_value_items.find(nodes[i].name());
if( it2 != hmod->m_value_items.end() ) {
TU_MATCHA( (it2->second->ent), (e),
(Import,
TODO(span, "Recurse to get binding for an import");
),
(Constant,
return ::AST::PathBinding::make_Static({ nullptr });
),
(Static,
return ::AST::PathBinding::make_Static({ nullptr });
),
(StructConstant,
return ::AST::PathBinding::make_Struct({ nullptr });
),
(Function,
return ::AST::PathBinding::make_Function({ nullptr });
),
(StructConstructor,
return ::AST::PathBinding::make_Struct({ nullptr });
)
)
}
TODO(span, "Get binding within an extern crate");
),
(Enum,
const auto& enum_ = *e.enum_;
i += 1;
if( i != nodes.size() - 1 ) {
ERROR(span, E0000, "Encountered enum at unexpected location in import");
}
const auto& node2 = nodes[i];
int variant_index = -1;
for( unsigned int j = 0; j < enum_.variants().size(); j ++ )
{
if( enum_.variants()[j].m_name == node2.name() ) {
variant_index = j;
break ;
}
}
if( variant_index < 0 ) {
ERROR(span, E0000, "Unknown enum variant '" << node2.name() << "'");
}
return ::AST::PathBinding::make_EnumVar({&enum_, static_cast<unsigned int>(variant_index)});
),
(Module,
mod = e.module_;
)
)
}
return Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes.back().name(), parent_modules);
}
|