Skip to content

Commit ef62630

Browse files
committed
Add name-resolution helpers for looking up macros
1 parent a026c16 commit ef62630

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Resolver::Resolver ()
5454
name_scope (Scope (mappings->get_current_crate ())),
5555
type_scope (Scope (mappings->get_current_crate ())),
5656
label_scope (Scope (mappings->get_current_crate ())),
57+
macro_scope (Scope (mappings->get_current_crate ())),
5758
global_type_node_id (UNKNOWN_NODEID), unit_ty_node_id (UNKNOWN_NODEID)
5859
{
5960
generate_builtins ();
@@ -93,6 +94,13 @@ Resolver::push_new_label_rib (Rib *r)
9394
label_ribs[r->get_node_id ()] = r;
9495
}
9596

97+
void
98+
Resolver::push_new_macro_rib (Rib *r)
99+
{
100+
rust_assert (label_ribs.find (r->get_node_id ()) == label_ribs.end ());
101+
macro_ribs[r->get_node_id ()] = r;
102+
}
103+
96104
bool
97105
Resolver::find_name_rib (NodeId id, Rib **rib)
98106
{
@@ -115,6 +123,17 @@ Resolver::find_type_rib (NodeId id, Rib **rib)
115123
return true;
116124
}
117125

126+
bool
127+
Resolver::find_macro_rib (NodeId id, Rib **rib)
128+
{
129+
auto it = macro_ribs.find (id);
130+
if (it == macro_ribs.end ())
131+
return false;
132+
133+
*rib = it->second;
134+
return true;
135+
}
136+
118137
void
119138
Resolver::insert_builtin_types (Rib *r)
120139
{
@@ -281,6 +300,27 @@ Resolver::lookup_resolved_label (NodeId refId, NodeId *defId)
281300
return true;
282301
}
283302

303+
void
304+
Resolver::insert_resolved_macro (NodeId refId, NodeId defId)
305+
{
306+
auto it = resolved_macros.find (refId);
307+
rust_assert (it == resolved_macros.end ());
308+
309+
resolved_labels[refId] = defId;
310+
get_label_scope ().append_reference_for_def (refId, defId);
311+
}
312+
313+
bool
314+
Resolver::lookup_resolved_macro (NodeId refId, NodeId *defId)
315+
{
316+
auto it = resolved_macros.find (refId);
317+
if (it == resolved_macros.end ())
318+
return false;
319+
320+
*defId = it->second;
321+
return true;
322+
}
323+
284324
// NameResolution
285325

286326
NameResolution *

gcc/rust/resolve/rust-name-resolver.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ class Resolver
274274
void push_new_name_rib (Rib *r);
275275
void push_new_type_rib (Rib *r);
276276
void push_new_label_rib (Rib *r);
277+
void push_new_macro_rib (Rib *r);
277278

278279
bool find_name_rib (NodeId id, Rib **rib);
279280
bool find_type_rib (NodeId id, Rib **rib);
280281
bool find_label_rib (NodeId id, Rib **rib);
282+
bool find_macro_rib (NodeId id, Rib **rib);
281283

282284
void insert_new_definition (NodeId id, Definition def);
283285
bool lookup_definition (NodeId id, Definition *def);
@@ -291,10 +293,14 @@ class Resolver
291293
void insert_resolved_label (NodeId refId, NodeId defId);
292294
bool lookup_resolved_label (NodeId refId, NodeId *defId);
293295

296+
void insert_resolved_macro (NodeId refId, NodeId defId);
297+
bool lookup_resolved_macro (NodeId refId, NodeId *defId);
298+
294299
// proxy for scoping
295300
Scope &get_name_scope () { return name_scope; }
296301
Scope &get_type_scope () { return type_scope; }
297302
Scope &get_label_scope () { return label_scope; }
303+
Scope &get_macro_scope () { return macro_scope; }
298304

299305
NodeId get_global_type_node_id () { return global_type_node_id; }
300306

@@ -371,6 +377,7 @@ class Resolver
371377
Scope name_scope;
372378
Scope type_scope;
373379
Scope label_scope;
380+
Scope macro_scope;
374381

375382
NodeId global_type_node_id;
376383
NodeId unit_ty_node_id;
@@ -379,6 +386,7 @@ class Resolver
379386
std::map<NodeId, Rib *> name_ribs;
380387
std::map<NodeId, Rib *> type_ribs;
381388
std::map<NodeId, Rib *> label_ribs;
389+
std::map<NodeId, Rib *> macro_ribs;
382390

383391
// map any Node to its Definition
384392
// ie any name or type usage
@@ -395,6 +403,7 @@ class Resolver
395403
std::map<NodeId, NodeId> resolved_names;
396404
std::map<NodeId, NodeId> resolved_types;
397405
std::map<NodeId, NodeId> resolved_labels;
406+
std::map<NodeId, NodeId> resolved_macros;
398407

399408
// map of resolved names mutability flag
400409
std::map<NodeId, bool> decl_mutability;

0 commit comments

Comments
 (0)