-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathrust-late-name-resolver-2.0.cc
434 lines (365 loc) · 12.5 KB
/
rust-late-name-resolver-2.0.cc
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
428
429
430
431
432
433
434
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC 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 General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "optional.h"
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-hir-map.h"
#include "rust-late-name-resolver-2.0.h"
#include "rust-default-resolver.h"
#include "rust-name-resolution-context.h"
#include "rust-path.h"
#include "rust-system.h"
#include "rust-tyty.h"
#include "rust-hir-type-check.h"
#include "rust-ice-finalizer.h"
#include "rust-ast.h"
namespace Rust {
namespace Resolver2_0 {
Late::Late (NameResolutionContext &ctx) : DefaultResolver (ctx) {}
static NodeId
next_node_id ()
{
return Analysis::Mappings::get ().get_next_node_id ();
};
static HirId
next_hir_id ()
{
return Analysis::Mappings::get ().get_next_hir_id ();
};
void
Late::setup_builtin_types ()
{
// access the global type context to setup the TyTys
auto &ty_ctx = *Resolver::TypeCheckContext::get ();
// Late builtin type struct helper
struct LType
{
std::string name;
NodeId node_id;
NodeId hir_id;
TyTy::BaseType *type;
explicit LType (std::string name, TyTy::BaseType *type)
: name (name), node_id (next_node_id ()), hir_id (type->get_ref ()),
type (type)
{}
};
static const LType builtins[] = {
{LType ("bool", new TyTy::BoolType (next_hir_id ()))},
{LType ("u8", new TyTy::UintType (next_hir_id (), TyTy::UintType::U8))},
{LType ("u16", new TyTy::UintType (next_hir_id (), TyTy::UintType::U16))},
{LType ("u32", new TyTy::UintType (next_hir_id (), TyTy::UintType::U32))},
{LType ("u64", new TyTy::UintType (next_hir_id (), TyTy::UintType::U64))},
{LType ("u128", new TyTy::UintType (next_hir_id (), TyTy::UintType::U128))},
{LType ("i8", new TyTy::IntType (next_hir_id (), TyTy::IntType::I8))},
{LType ("i16", new TyTy::IntType (next_hir_id (), TyTy::IntType::I16))},
{LType ("i32", new TyTy::IntType (next_hir_id (), TyTy::IntType::I32))},
{LType ("i64", new TyTy::IntType (next_hir_id (), TyTy::IntType::I64))},
{LType ("i128", new TyTy::IntType (next_hir_id (), TyTy::IntType::I128))},
{LType ("f32", new TyTy::FloatType (next_hir_id (), TyTy::FloatType::F32))},
{LType ("f64", new TyTy::FloatType (next_hir_id (), TyTy::FloatType::F64))},
{LType ("usize", new TyTy::USizeType (next_hir_id ()))},
{LType ("isize", new TyTy::ISizeType (next_hir_id ()))},
{LType ("char", new TyTy::CharType (next_hir_id ()))},
{LType ("str", new TyTy::StrType (next_hir_id ()))},
{LType ("!", new TyTy::NeverType (next_hir_id ()))},
// the unit type `()` does not play a part in name-resolution - so we only
// insert it in the type context...
};
// There's a special Rib for putting prelude items, since prelude items need
// to satisfy certain special rules.
ctx.scoped (Rib::Kind::Prelude, 0, [this, &ty_ctx] (void) -> void {
for (const auto &builtin : builtins)
{
auto ok = ctx.types.insert (builtin.name, builtin.node_id);
rust_assert (ok);
ctx.mappings.insert_node_to_hir (builtin.node_id, builtin.hir_id);
ty_ctx.insert_builtin (builtin.hir_id, builtin.node_id, builtin.type);
}
});
// ...here!
auto *unit_type = TyTy::TupleType::get_unit_type ();
ty_ctx.insert_builtin (unit_type->get_ref (), next_node_id (), unit_type);
}
void
Late::go (AST::Crate &crate)
{
setup_builtin_types ();
for (auto &item : crate.items)
item->accept_vis (*this);
}
void
Late::new_label (Identifier name, NodeId id)
{
// labels can always shadow, so `insert` should never fail. if it does, we're
// in big trouble!
auto ok = ctx.labels.insert (name, id);
rust_assert (ok);
}
void
Late::visit (AST::LetStmt &let)
{
DefaultASTVisitor::visit_outer_attrs (let);
if (let.has_type ())
visit (let.get_type ());
// visit expression before pattern
// this makes variable shadowing work properly
if (let.has_init_expr ())
visit (let.get_init_expr ());
visit (let.get_pattern ());
// how do we deal with the fact that `let a = blipbloup` should look for a
// label and cannot go through function ribs, but `let a = blipbloup()` can?
// how do we insert ribs here, and only pop them when we exit the current
// function?
// keep a list of ribs to pop when a scope exits? so only for blocks?
// how do we pop ribs that need to be popped not in order?
// I think it's not important if we have shadowing, correct?
// if we have shadowing, it should work! we'll see
// ctx.insert(Identifier name, NodeId id, Namespace ns)
// ctx.scoped (Rib::Kind::Normal /* FIXME: Is that valid? */,
// Namespace::Labels,
// let.get_node_id (), [] () {});
}
void
Late::visit (AST::IdentifierPattern &identifier)
{
// do we insert in labels or in values
// but values does not allow shadowing... since functions cannot shadow
// do we insert functions in labels as well?
// We do want to ignore duplicated data because some situations rely on it.
std::ignore = ctx.values.insert_shadowable (identifier.get_ident (),
identifier.get_node_id ());
}
void
Late::visit (AST::SelfParam ¶m)
{
// handle similar to AST::IdentifierPattern
DefaultResolver::visit (param);
// FIXME: this location should be a bit off
// ex: would point to the begining of "mut self" instead of the "self"
std::ignore = ctx.values.insert (Identifier ("self", param.get_locus ()),
param.get_node_id ());
}
void
Late::visit (AST::BreakExpr &expr)
{
if (expr.has_break_expr ())
{
auto &break_expr = expr.get_break_expr ();
if (break_expr.get_expr_kind () == AST::Expr::Kind::Identifier)
{
/* This is a break with an expression, and the expression is
just a single identifier. See if the identifier is either
"rust" or "gcc", in which case we have "break rust" or "break
gcc", and so may need to emit our funny error. We cannot yet
emit the error here though, because the identifier may still
be in scope, and ICE'ing on valid programs would not be very
funny. */
std::string ident
= static_cast<AST::IdentifierExpr &> (expr.get_break_expr ())
.as_string ();
if (ident == "rust" || ident == "gcc")
funny_error = true;
}
}
DefaultResolver::visit (expr);
funny_error = false;
}
void
Late::visit (AST::IdentifierExpr &expr)
{
// TODO: same thing as visit(PathInExpression) here?
tl::optional<Rib::Definition> resolved = tl::nullopt;
if (auto value = ctx.values.get (expr.get_ident ()))
{
resolved = value;
}
else if (auto type = ctx.types.get (expr.get_ident ()))
{
resolved = type;
}
else if (funny_error)
{
diagnostic_finalizer (global_dc) = Resolver::funny_ice_finalizer;
emit_diagnostic (DK_ICE_NOBT, expr.get_locus (), -1,
"are you trying to break %s? how dare you?",
expr.as_string ().c_str ());
}
else
{
if (auto typ = ctx.types.get_prelude (expr.get_ident ()))
{
resolved = typ;
}
else
{
rust_error_at (expr.get_locus (),
"could not resolve identifier expression: %qs",
expr.get_ident ().as_string ().c_str ());
}
}
ctx.map_usage (Usage (expr.get_node_id ()),
Definition (resolved->get_node_id ()));
// in the old resolver, resolutions are kept in the resolver, not the mappings
// :/ how do we deal with that?
// ctx.mappings.insert_resolved_name(expr, resolved);
// For empty types, do we perform a lookup in ctx.types or should the
// toplevel instead insert a name in ctx.values? (like it currently does)
}
void
Late::visit (AST::PathInExpression &expr)
{
// TODO: How do we have a nice error with `can't capture dynamic environment
// in a function item` error here?
// do we emit it in `get<Namespace::Labels>`?
if (expr.is_lang_item ())
{
ctx.map_usage (Usage (expr.get_node_id ()),
Definition (Analysis::Mappings::get ().get_lang_item_node (
expr.get_lang_item ())));
return;
}
auto resolved = ctx.resolve_path (expr.get_segments (), Namespace::Values,
Namespace::Types);
if (!resolved)
{
if (!ctx.lookup (expr.get_segments ().front ().get_node_id ()))
rust_error_at (expr.get_locus (),
"could not resolve path expression: %qs",
expr.as_simple_path ().as_string ().c_str ());
return;
}
if (resolved->is_ambiguous ())
{
rust_error_at (expr.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
expr.as_string ().c_str ());
return;
}
ctx.map_usage (Usage (expr.get_node_id ()),
Definition (resolved->get_node_id ()));
}
void
Late::visit (AST::TypePath &type)
{
// should we add type path resolution in `ForeverStack` directly? Since it's
// quite more complicated.
// maybe we can overload `resolve_path<Namespace::Types>` to only do
// typepath-like path resolution? that sounds good
// take care of only simple cases
// TODO: remove this?
rust_assert (!type.has_opening_scope_resolution_op ());
// this *should* mostly work
// TODO: make sure typepath-like path resolution (?) is working
auto resolved = ctx.resolve_path (type.get_segments (), Namespace::Types);
if (resolved.has_value ())
ctx.map_usage (Usage (type.get_node_id ()),
Definition (resolved->get_node_id ()));
else
rust_error_at (type.get_locus (), "could not resolve type path %qs",
type.as_string ().c_str ());
DefaultResolver::visit (type);
}
void
Late::visit (AST::Trait &trait)
{
// kind of weird how this is done
// names are resolved to the node id of trait.get_implicit_self ()
// which is then resolved to the node id of trait
// we set up the latter mapping here
ctx.map_usage (Usage (trait.get_implicit_self ().get_node_id ()),
Definition (trait.get_node_id ()));
DefaultResolver::visit (trait);
}
void
Late::visit (AST::StructStruct &s)
{
auto s_vis = [this, &s] () { AST::DefaultASTVisitor::visit (s); };
ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
}
void
Late::visit (AST::StructExprStruct &s)
{
auto resolved
= ctx.resolve_path (s.get_struct_name ().get_segments (), Namespace::Types);
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
Definition (resolved->get_node_id ()));
}
void
Late::visit (AST::StructExprStructBase &s)
{
auto resolved
= ctx.resolve_path (s.get_struct_name ().get_segments (), Namespace::Types);
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
Definition (resolved->get_node_id ()));
DefaultResolver::visit (s);
}
void
Late::visit (AST::StructExprStructFields &s)
{
auto resolved
= ctx.resolve_path (s.get_struct_name ().get_segments (), Namespace::Types);
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
Definition (resolved->get_node_id ()));
DefaultResolver::visit (s);
}
// needed because Late::visit (AST::GenericArg &) is non-virtual
void
Late::visit (AST::GenericArgs &args)
{
for (auto &lifetime : args.get_lifetime_args ())
visit (lifetime);
for (auto &generic : args.get_generic_args ())
visit (generic);
for (auto &binding : args.get_binding_args ())
visit (binding);
}
void
Late::visit (AST::GenericArg &arg)
{
if (arg.get_kind () == AST::GenericArg::Kind::Either)
{
// prefer type parameter to const parameter on ambiguity
auto type = ctx.types.get (arg.get_path ());
auto value = ctx.values.get (arg.get_path ());
if (!type.has_value () && value.has_value ())
arg = arg.disambiguate_to_const ();
else
arg = arg.disambiguate_to_type ();
}
DefaultResolver::visit (arg);
}
template <class Closure>
static void
add_captures (Closure &closure, NameResolutionContext &ctx)
{
auto vals = ctx.values.peek ().get_values ();
for (auto &val : vals)
{
ctx.mappings.add_capture (closure.get_node_id (),
val.second.get_node_id ());
}
}
void
Late::visit (AST::ClosureExprInner &closure)
{
add_captures (closure, ctx);
DefaultResolver::visit (closure);
}
void
Late::visit (AST::ClosureExprInnerTyped &closure)
{
add_captures (closure, ctx);
DefaultResolver::visit (closure);
}
} // namespace Resolver2_0
} // namespace Rust