Skip to content

Commit 723833d

Browse files
philbertydkm
authored andcommitted
Refactor name resolver to take a Rib::ItemType
This allows us to track the type of declaration that is stored within a Rib.
1 parent a3f3066 commit 723833d

10 files changed

+108
-41
lines changed

gcc/rust/resolve/rust-ast-resolve-expr.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ ResolveExpr::visit (AST::IfLetExpr &expr)
209209

210210
for (auto &pattern : expr.get_patterns ())
211211
{
212-
PatternDeclaration::go (pattern.get ());
212+
PatternDeclaration::go (pattern.get (), Rib::ItemType::Var);
213213
}
214214

215215
ResolveExpr::go (expr.get_if_block ().get (), prefix, canonical_prefix);
@@ -343,7 +343,7 @@ ResolveExpr::visit (AST::LoopExpr &expr)
343343
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
344344
resolver->get_label_scope ().insert (
345345
CanonicalPath::new_seg (expr.get_node_id (), label_name),
346-
label_lifetime_node_id, label.get_locus (), false,
346+
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
347347
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
348348
rust_error_at (label.get_locus (), "label redefined multiple times");
349349
rust_error_at (locus, "was defined here");
@@ -400,7 +400,7 @@ ResolveExpr::visit (AST::WhileLoopExpr &expr)
400400
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
401401
resolver->get_label_scope ().insert (
402402
CanonicalPath::new_seg (label.get_node_id (), label_name),
403-
label_lifetime_node_id, label.get_locus (), false,
403+
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
404404
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
405405
rust_error_at (label.get_locus (), "label redefined multiple times");
406406
rust_error_at (locus, "was defined here");
@@ -429,7 +429,7 @@ ResolveExpr::visit (AST::ForLoopExpr &expr)
429429
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
430430
resolver->get_label_scope ().insert (
431431
CanonicalPath::new_seg (label.get_node_id (), label_name),
432-
label_lifetime_node_id, label.get_locus (), false,
432+
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
433433
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
434434
rust_error_at (label.get_locus (), "label redefined multiple times");
435435
rust_error_at (locus, "was defined here");
@@ -446,7 +446,7 @@ ResolveExpr::visit (AST::ForLoopExpr &expr)
446446
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
447447

448448
// resolve the expression
449-
PatternDeclaration::go (expr.get_pattern ().get ());
449+
PatternDeclaration::go (expr.get_pattern ().get (), Rib::ItemType::Var);
450450
ResolveExpr::go (expr.get_iterator_expr ().get (), prefix, canonical_prefix);
451451
ResolveExpr::go (expr.get_loop_block ().get (), prefix, canonical_prefix);
452452

@@ -520,7 +520,7 @@ ResolveExpr::visit (AST::MatchExpr &expr)
520520
// insert any possible new patterns
521521
for (auto &pattern : arm.get_patterns ())
522522
{
523-
PatternDeclaration::go (pattern.get ());
523+
PatternDeclaration::go (pattern.get (), Rib::ItemType::Var);
524524
}
525525

526526
// resolve the body
@@ -617,7 +617,7 @@ ResolveExpr::visit (AST::ClosureExprInnerTyped &expr)
617617
void
618618
ResolveExpr::resolve_closure_param (AST::ClosureParam &param)
619619
{
620-
PatternDeclaration::go (param.get_pattern ().get ());
620+
PatternDeclaration::go (param.get_pattern ().get (), Rib::ItemType::Param);
621621

622622
if (param.has_type_given ())
623623
ResolveType::go (param.get_type ().get ());

gcc/rust/resolve/rust-ast-resolve-implitem.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ResolveToplevelImplItem : public ResolverBase
5656
auto path = prefix.append (decl);
5757

5858
resolver->get_type_scope ().insert (
59-
path, type.get_node_id (), type.get_locus (), false,
59+
path, type.get_node_id (), type.get_locus (), false, Rib::ItemType::Type,
6060
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
6161
RichLocation r (type.get_locus ());
6262
r.add_range (locus);
@@ -72,6 +72,7 @@ class ResolveToplevelImplItem : public ResolverBase
7272

7373
resolver->get_name_scope ().insert (
7474
path, constant.get_node_id (), constant.get_locus (), false,
75+
Rib::ItemType::Const,
7576
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
7677
RichLocation r (constant.get_locus ());
7778
r.add_range (locus);
@@ -87,6 +88,7 @@ class ResolveToplevelImplItem : public ResolverBase
8788

8889
resolver->get_name_scope ().insert (
8990
path, function.get_node_id (), function.get_locus (), false,
91+
Rib::ItemType::Function,
9092
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
9193
RichLocation r (function.get_locus ());
9294
r.add_range (locus);
@@ -102,6 +104,7 @@ class ResolveToplevelImplItem : public ResolverBase
102104

103105
resolver->get_name_scope ().insert (
104106
path, method.get_node_id (), method.get_locus (), false,
107+
Rib::ItemType::Function,
105108
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
106109
RichLocation r (method.get_locus ());
107110
r.add_range (locus);
@@ -141,6 +144,7 @@ class ResolveTopLevelTraitItems : public ResolverBase
141144

142145
resolver->get_name_scope ().insert (
143146
path, function.get_node_id (), function.get_locus (), false,
147+
Rib::ItemType::Function,
144148
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
145149
RichLocation r (function.get_locus ());
146150
r.add_range (locus);
@@ -159,6 +163,7 @@ class ResolveTopLevelTraitItems : public ResolverBase
159163

160164
resolver->get_name_scope ().insert (
161165
path, method.get_node_id (), method.get_locus (), false,
166+
Rib::ItemType::Function,
162167
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
163168
RichLocation r (method.get_locus ());
164169
r.add_range (locus);
@@ -177,6 +182,7 @@ class ResolveTopLevelTraitItems : public ResolverBase
177182

178183
resolver->get_name_scope ().insert (
179184
path, constant.get_node_id (), constant.get_locus (), false,
185+
Rib::ItemType::Const,
180186
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
181187
RichLocation r (constant.get_locus ());
182188
r.add_range (locus);
@@ -194,7 +200,7 @@ class ResolveTopLevelTraitItems : public ResolverBase
194200
auto cpath = canonical_prefix.append (decl);
195201

196202
resolver->get_type_scope ().insert (
197-
path, type.get_node_id (), type.get_locus (), false,
203+
path, type.get_node_id (), type.get_locus (), false, Rib::ItemType::Type,
198204
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
199205
RichLocation r (type.get_locus ());
200206
r.add_range (locus);
@@ -233,6 +239,7 @@ class ResolveToplevelExternItem : public ResolverBase
233239

234240
resolver->get_name_scope ().insert (
235241
path, function.get_node_id (), function.get_locus (), false,
242+
Rib::ItemType::Function,
236243
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
237244
RichLocation r (function.get_locus ());
238245
r.add_range (locus);
@@ -251,6 +258,7 @@ class ResolveToplevelExternItem : public ResolverBase
251258

252259
resolver->get_name_scope ().insert (
253260
path, item.get_node_id (), item.get_locus (), false,
261+
Rib::ItemType::Static,
254262
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
255263
RichLocation r (item.get_locus ());
256264
r.add_range (locus);

gcc/rust/resolve/rust-ast-resolve-item.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ ResolveTraitItems::visit (AST::TraitItemFunc &func)
8282
for (auto &param : function.get_function_params ())
8383
{
8484
ResolveType::go (param.get_type ().get ());
85-
PatternDeclaration::go (param.get_pattern ().get ());
85+
PatternDeclaration::go (param.get_pattern ().get (),
86+
Rib::ItemType::Param);
8687
}
8788

8889
if (function.has_where_clause ())
@@ -138,14 +139,15 @@ ResolveTraitItems::visit (AST::TraitItemMethod &func)
138139
AST::TypePath self_type_path (std::move (segments), self_param.get_locus ());
139140

140141
ResolveType::go (&self_type_path);
141-
PatternDeclaration::go (&self_pattern);
142+
PatternDeclaration::go (&self_pattern, Rib::ItemType::Param);
142143

143144
// we make a new scope so the names of parameters are resolved and shadowed
144145
// correctly
145146
for (auto &param : function.get_function_params ())
146147
{
147148
ResolveType::go (param.get_type ().get ());
148-
PatternDeclaration::go (param.get_pattern ().get ());
149+
PatternDeclaration::go (param.get_pattern ().get (),
150+
Rib::ItemType::Param);
149151
}
150152

151153
if (function.has_where_clause ())
@@ -499,10 +501,8 @@ ResolveItem::visit (AST::Function &function)
499501
for (auto &param : function.get_function_params ())
500502
{
501503
ResolveType::go (param.get_type ().get ());
502-
PatternDeclaration::go (param.get_pattern ().get ());
503-
504-
// the mutability checker needs to verify for immutable decls the number
505-
// of assignments are <1. This marks an implicit assignment
504+
PatternDeclaration::go (param.get_pattern ().get (),
505+
Rib::ItemType::Param);
506506
}
507507

508508
// resolve the function body
@@ -631,14 +631,15 @@ ResolveItem::visit (AST::Method &method)
631631
AST::TypePath self_type_path (std::move (segments), self_param.get_locus ());
632632

633633
ResolveType::go (&self_type_path);
634-
PatternDeclaration::go (&self_pattern);
634+
PatternDeclaration::go (&self_pattern, Rib::ItemType::Param);
635635

636636
// we make a new scope so the names of parameters are resolved and shadowed
637637
// correctly
638638
for (auto &param : method.get_function_params ())
639639
{
640640
ResolveType::go (param.get_type ().get ());
641-
PatternDeclaration::go (param.get_pattern ().get ());
641+
PatternDeclaration::go (param.get_pattern ().get (),
642+
Rib::ItemType::Param);
642643
}
643644

644645
// resolve any where clause items

gcc/rust/resolve/rust-ast-resolve-pattern.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PatternDeclaration::visit (AST::TupleStructPattern &pattern)
4949

5050
for (auto &inner_pattern : items_no_range.get_patterns ())
5151
{
52-
PatternDeclaration::go (inner_pattern.get ());
52+
PatternDeclaration::go (inner_pattern.get (), type);
5353
}
5454
}
5555
break;

gcc/rust/resolve/rust-ast-resolve-pattern.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class PatternDeclaration : public ResolverBase
5555
using Rust::Resolver::ResolverBase::visit;
5656

5757
public:
58-
static void go (AST::Pattern *pattern)
58+
static void go (AST::Pattern *pattern, Rib::ItemType type)
5959
{
60-
PatternDeclaration resolver;
60+
PatternDeclaration resolver (type);
6161
pattern->accept_vis (resolver);
6262
};
6363

@@ -67,14 +67,14 @@ class PatternDeclaration : public ResolverBase
6767
// as new refs to this decl will match back here so it is ok to overwrite
6868
resolver->get_name_scope ().insert (
6969
CanonicalPath::new_seg (pattern.get_node_id (), pattern.get_ident ()),
70-
pattern.get_node_id (), pattern.get_locus ());
70+
pattern.get_node_id (), pattern.get_locus (), type);
7171
}
7272

7373
void visit (AST::WildcardPattern &pattern) override
7474
{
7575
resolver->get_name_scope ().insert (
7676
CanonicalPath::new_seg (pattern.get_node_id (), "_"),
77-
pattern.get_node_id (), pattern.get_locus ());
77+
pattern.get_node_id (), pattern.get_locus (), type);
7878
}
7979

8080
// cases in a match expression
@@ -89,7 +89,9 @@ class PatternDeclaration : public ResolverBase
8989
void visit (AST::RangePattern &pattern) override;
9090

9191
private:
92-
PatternDeclaration () : ResolverBase () {}
92+
PatternDeclaration (Rib::ItemType type) : ResolverBase (), type (type) {}
93+
94+
Rib::ItemType type;
9395
};
9496

9597
} // namespace Resolver

gcc/rust/resolve/rust-ast-resolve-stmt.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class ResolveStmt : public ResolverBase
6464

6565
resolver->get_name_scope ().insert (
6666
path, constant.get_node_id (), constant.get_locus (), false,
67+
Rib::ItemType::Const,
6768
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
6869
RichLocation r (constant.get_locus ());
6970
r.add_range (locus);
@@ -82,7 +83,7 @@ class ResolveStmt : public ResolverBase
8283
canonical_prefix);
8384
}
8485

85-
PatternDeclaration::go (stmt.get_pattern ().get ());
86+
PatternDeclaration::go (stmt.get_pattern ().get (), Rib::ItemType::Var);
8687
if (stmt.has_type ())
8788
ResolveType::go (stmt.get_type ().get ());
8889
}
@@ -97,6 +98,7 @@ class ResolveStmt : public ResolverBase
9798

9899
resolver->get_type_scope ().insert (
99100
path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
101+
Rib::ItemType::Type,
100102
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
101103
RichLocation r (struct_decl.get_locus ());
102104
r.add_range (locus);
@@ -128,6 +130,7 @@ class ResolveStmt : public ResolverBase
128130

129131
resolver->get_type_scope ().insert (
130132
path, enum_decl.get_node_id (), enum_decl.get_locus (), false,
133+
Rib::ItemType::Type,
131134
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
132135
RichLocation r (enum_decl.get_locus ());
133136
r.add_range (locus);
@@ -158,7 +161,7 @@ class ResolveStmt : public ResolverBase
158161
mappings->insert_canonical_path (item.get_node_id (), cpath);
159162

160163
resolver->get_type_scope ().insert (
161-
path, item.get_node_id (), item.get_locus (), false,
164+
path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
162165
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
163166
RichLocation r (item.get_locus ());
164167
r.add_range (locus);
@@ -177,7 +180,7 @@ class ResolveStmt : public ResolverBase
177180
mappings->insert_canonical_path (item.get_node_id (), cpath);
178181

179182
resolver->get_type_scope ().insert (
180-
path, item.get_node_id (), item.get_locus (), false,
183+
path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
181184
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
182185
RichLocation r (item.get_locus ());
183186
r.add_range (locus);
@@ -202,7 +205,7 @@ class ResolveStmt : public ResolverBase
202205
mappings->insert_canonical_path (item.get_node_id (), cpath);
203206

204207
resolver->get_type_scope ().insert (
205-
path, item.get_node_id (), item.get_locus (), false,
208+
path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
206209
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
207210
RichLocation r (item.get_locus ());
208211
r.add_range (locus);
@@ -227,7 +230,7 @@ class ResolveStmt : public ResolverBase
227230
mappings->insert_canonical_path (item.get_node_id (), cpath);
228231

229232
resolver->get_type_scope ().insert (
230-
path, item.get_node_id (), item.get_locus (), false,
233+
path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
231234
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
232235
RichLocation r (item.get_locus ());
233236
r.add_range (locus);
@@ -247,6 +250,7 @@ class ResolveStmt : public ResolverBase
247250

248251
resolver->get_type_scope ().insert (
249252
path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
253+
Rib::ItemType::Type,
250254
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
251255
RichLocation r (struct_decl.get_locus ());
252256
r.add_range (locus);
@@ -283,6 +287,7 @@ class ResolveStmt : public ResolverBase
283287

284288
resolver->get_type_scope ().insert (
285289
path, union_decl.get_node_id (), union_decl.get_locus (), false,
290+
Rib::ItemType::Type,
286291
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
287292
RichLocation r (union_decl.get_locus ());
288293
r.add_range (locus);
@@ -317,6 +322,7 @@ class ResolveStmt : public ResolverBase
317322

318323
resolver->get_name_scope ().insert (
319324
path, function.get_node_id (), function.get_locus (), false,
325+
Rib::ItemType::Function,
320326
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
321327
RichLocation r (function.get_locus ());
322328
r.add_range (locus);
@@ -343,7 +349,8 @@ class ResolveStmt : public ResolverBase
343349
for (auto &param : function.get_function_params ())
344350
{
345351
ResolveType::go (param.get_type ().get ());
346-
PatternDeclaration::go (param.get_pattern ().get ());
352+
PatternDeclaration::go (param.get_pattern ().get (),
353+
Rib::ItemType::Param);
347354
}
348355

349356
// resolve the function body

0 commit comments

Comments
 (0)