Skip to content

Commit

Permalink
Add name-resolution helpers for looking up macros
Browse files Browse the repository at this point in the history
  • Loading branch information
philberty committed Feb 17, 2022
1 parent a026c16 commit ef62630
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
40 changes: 40 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Resolver::Resolver ()
name_scope (Scope (mappings->get_current_crate ())),
type_scope (Scope (mappings->get_current_crate ())),
label_scope (Scope (mappings->get_current_crate ())),
macro_scope (Scope (mappings->get_current_crate ())),
global_type_node_id (UNKNOWN_NODEID), unit_ty_node_id (UNKNOWN_NODEID)
{
generate_builtins ();
Expand Down Expand Up @@ -93,6 +94,13 @@ Resolver::push_new_label_rib (Rib *r)
label_ribs[r->get_node_id ()] = r;
}

void
Resolver::push_new_macro_rib (Rib *r)
{
rust_assert (label_ribs.find (r->get_node_id ()) == label_ribs.end ());
macro_ribs[r->get_node_id ()] = r;
}

bool
Resolver::find_name_rib (NodeId id, Rib **rib)
{
Expand All @@ -115,6 +123,17 @@ Resolver::find_type_rib (NodeId id, Rib **rib)
return true;
}

bool
Resolver::find_macro_rib (NodeId id, Rib **rib)
{
auto it = macro_ribs.find (id);
if (it == macro_ribs.end ())
return false;

*rib = it->second;
return true;
}

void
Resolver::insert_builtin_types (Rib *r)
{
Expand Down Expand Up @@ -281,6 +300,27 @@ Resolver::lookup_resolved_label (NodeId refId, NodeId *defId)
return true;
}

void
Resolver::insert_resolved_macro (NodeId refId, NodeId defId)
{
auto it = resolved_macros.find (refId);
rust_assert (it == resolved_macros.end ());

resolved_labels[refId] = defId;
get_label_scope ().append_reference_for_def (refId, defId);
}

bool
Resolver::lookup_resolved_macro (NodeId refId, NodeId *defId)
{
auto it = resolved_macros.find (refId);
if (it == resolved_macros.end ())
return false;

*defId = it->second;
return true;
}

// NameResolution

NameResolution *
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/resolve/rust-name-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ class Resolver
void push_new_name_rib (Rib *r);
void push_new_type_rib (Rib *r);
void push_new_label_rib (Rib *r);
void push_new_macro_rib (Rib *r);

bool find_name_rib (NodeId id, Rib **rib);
bool find_type_rib (NodeId id, Rib **rib);
bool find_label_rib (NodeId id, Rib **rib);
bool find_macro_rib (NodeId id, Rib **rib);

void insert_new_definition (NodeId id, Definition def);
bool lookup_definition (NodeId id, Definition *def);
Expand All @@ -291,10 +293,14 @@ class Resolver
void insert_resolved_label (NodeId refId, NodeId defId);
bool lookup_resolved_label (NodeId refId, NodeId *defId);

void insert_resolved_macro (NodeId refId, NodeId defId);
bool lookup_resolved_macro (NodeId refId, NodeId *defId);

// proxy for scoping
Scope &get_name_scope () { return name_scope; }
Scope &get_type_scope () { return type_scope; }
Scope &get_label_scope () { return label_scope; }
Scope &get_macro_scope () { return macro_scope; }

NodeId get_global_type_node_id () { return global_type_node_id; }

Expand Down Expand Up @@ -371,6 +377,7 @@ class Resolver
Scope name_scope;
Scope type_scope;
Scope label_scope;
Scope macro_scope;

NodeId global_type_node_id;
NodeId unit_ty_node_id;
Expand All @@ -379,6 +386,7 @@ class Resolver
std::map<NodeId, Rib *> name_ribs;
std::map<NodeId, Rib *> type_ribs;
std::map<NodeId, Rib *> label_ribs;
std::map<NodeId, Rib *> macro_ribs;

// map any Node to its Definition
// ie any name or type usage
Expand All @@ -395,6 +403,7 @@ class Resolver
std::map<NodeId, NodeId> resolved_names;
std::map<NodeId, NodeId> resolved_types;
std::map<NodeId, NodeId> resolved_labels;
std::map<NodeId, NodeId> resolved_macros;

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

0 comments on commit ef62630

Please sign in to comment.