Skip to content

Commit

Permalink
gccrs: Refactor name resolver to take a Rib::ItemType
Browse files Browse the repository at this point in the history
This allows us to track the type of declaration that is stored within a
Rib.

gcc/rust/ChangeLog:

	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Add Rib argument.
	(ResolveExpr::resolve_closure_param): Likewise.
	* resolve/rust-ast-resolve-implitem.h: Likewise.
	* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise.
	(ResolveItem::visit): Likewise.
	* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::visit): Likewise.
	* resolve/rust-ast-resolve-pattern.h: Likewise.
	* resolve/rust-ast-resolve-stmt.h: Likewise.
	* resolve/rust-ast-resolve-toplevel.h: Likewise.
	* resolve/rust-ast-resolve-type.h: Likewise.
	* resolve/rust-name-resolver.cc (Rib::lookup_decl_type): Likewise.
	(Scope::insert): Likewise.
	(Resolver::insert_builtin_types): Likewise.
	* resolve/rust-name-resolver.h: Likewise.
  • Loading branch information
philberty authored and CohenArthur committed Feb 14, 2023
1 parent 0186f20 commit c833686
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 41 deletions.
14 changes: 7 additions & 7 deletions gcc/rust/resolve/rust-ast-resolve-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ ResolveExpr::visit (AST::IfLetExpr &expr)

for (auto &pattern : expr.get_patterns ())
{
PatternDeclaration::go (pattern.get ());
PatternDeclaration::go (pattern.get (), Rib::ItemType::Var);
}

ResolveExpr::go (expr.get_if_block ().get (), prefix, canonical_prefix);
Expand Down Expand Up @@ -343,7 +343,7 @@ ResolveExpr::visit (AST::LoopExpr &expr)
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
resolver->get_label_scope ().insert (
CanonicalPath::new_seg (expr.get_node_id (), label_name),
label_lifetime_node_id, label.get_locus (), false,
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
rust_error_at (label.get_locus (), "label redefined multiple times");
rust_error_at (locus, "was defined here");
Expand Down Expand Up @@ -400,7 +400,7 @@ ResolveExpr::visit (AST::WhileLoopExpr &expr)
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
resolver->get_label_scope ().insert (
CanonicalPath::new_seg (label.get_node_id (), label_name),
label_lifetime_node_id, label.get_locus (), false,
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
rust_error_at (label.get_locus (), "label redefined multiple times");
rust_error_at (locus, "was defined here");
Expand Down Expand Up @@ -429,7 +429,7 @@ ResolveExpr::visit (AST::ForLoopExpr &expr)
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
resolver->get_label_scope ().insert (
CanonicalPath::new_seg (label.get_node_id (), label_name),
label_lifetime_node_id, label.get_locus (), false,
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
rust_error_at (label.get_locus (), "label redefined multiple times");
rust_error_at (locus, "was defined here");
Expand All @@ -446,7 +446,7 @@ ResolveExpr::visit (AST::ForLoopExpr &expr)
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());

// resolve the expression
PatternDeclaration::go (expr.get_pattern ().get ());
PatternDeclaration::go (expr.get_pattern ().get (), Rib::ItemType::Var);
ResolveExpr::go (expr.get_iterator_expr ().get (), prefix, canonical_prefix);
ResolveExpr::go (expr.get_loop_block ().get (), prefix, canonical_prefix);

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

// resolve the body
Expand Down Expand Up @@ -617,7 +617,7 @@ ResolveExpr::visit (AST::ClosureExprInnerTyped &expr)
void
ResolveExpr::resolve_closure_param (AST::ClosureParam &param)
{
PatternDeclaration::go (param.get_pattern ().get ());
PatternDeclaration::go (param.get_pattern ().get (), Rib::ItemType::Param);

if (param.has_type_given ())
ResolveType::go (param.get_type ().get ());
Expand Down
12 changes: 10 additions & 2 deletions gcc/rust/resolve/rust-ast-resolve-implitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ResolveToplevelImplItem : public ResolverBase
auto path = prefix.append (decl);

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

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

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

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

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

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

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

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

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

resolver->get_name_scope ().insert (
path, item.get_node_id (), item.get_locus (), false,
Rib::ItemType::Static,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
RichLocation r (item.get_locus ());
r.add_range (locus);
Expand Down
19 changes: 10 additions & 9 deletions gcc/rust/resolve/rust-ast-resolve-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ ResolveTraitItems::visit (AST::TraitItemFunc &func)
for (auto &param : function.get_function_params ())
{
ResolveType::go (param.get_type ().get ());
PatternDeclaration::go (param.get_pattern ().get ());
PatternDeclaration::go (param.get_pattern ().get (),
Rib::ItemType::Param);
}

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

ResolveType::go (&self_type_path);
PatternDeclaration::go (&self_pattern);
PatternDeclaration::go (&self_pattern, Rib::ItemType::Param);

// we make a new scope so the names of parameters are resolved and shadowed
// correctly
for (auto &param : function.get_function_params ())
{
ResolveType::go (param.get_type ().get ());
PatternDeclaration::go (param.get_pattern ().get ());
PatternDeclaration::go (param.get_pattern ().get (),
Rib::ItemType::Param);
}

if (function.has_where_clause ())
Expand Down Expand Up @@ -499,10 +501,8 @@ ResolveItem::visit (AST::Function &function)
for (auto &param : function.get_function_params ())
{
ResolveType::go (param.get_type ().get ());
PatternDeclaration::go (param.get_pattern ().get ());

// the mutability checker needs to verify for immutable decls the number
// of assignments are <1. This marks an implicit assignment
PatternDeclaration::go (param.get_pattern ().get (),
Rib::ItemType::Param);
}

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

ResolveType::go (&self_type_path);
PatternDeclaration::go (&self_pattern);
PatternDeclaration::go (&self_pattern, Rib::ItemType::Param);

// we make a new scope so the names of parameters are resolved and shadowed
// correctly
for (auto &param : method.get_function_params ())
{
ResolveType::go (param.get_type ().get ());
PatternDeclaration::go (param.get_pattern ().get ());
PatternDeclaration::go (param.get_pattern ().get (),
Rib::ItemType::Param);
}

// resolve any where clause items
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PatternDeclaration::visit (AST::TupleStructPattern &pattern)

for (auto &inner_pattern : items_no_range.get_patterns ())
{
PatternDeclaration::go (inner_pattern.get ());
PatternDeclaration::go (inner_pattern.get (), type);
}
}
break;
Expand Down
12 changes: 7 additions & 5 deletions gcc/rust/resolve/rust-ast-resolve-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class PatternDeclaration : public ResolverBase
using Rust::Resolver::ResolverBase::visit;

public:
static void go (AST::Pattern *pattern)
static void go (AST::Pattern *pattern, Rib::ItemType type)
{
PatternDeclaration resolver;
PatternDeclaration resolver (type);
pattern->accept_vis (resolver);
};

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

void visit (AST::WildcardPattern &pattern) override
{
resolver->get_name_scope ().insert (
CanonicalPath::new_seg (pattern.get_node_id (), "_"),
pattern.get_node_id (), pattern.get_locus ());
pattern.get_node_id (), pattern.get_locus (), type);
}

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

private:
PatternDeclaration () : ResolverBase () {}
PatternDeclaration (Rib::ItemType type) : ResolverBase (), type (type) {}

Rib::ItemType type;
};

} // namespace Resolver
Expand Down
19 changes: 13 additions & 6 deletions gcc/rust/resolve/rust-ast-resolve-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ResolveStmt : public ResolverBase

resolver->get_name_scope ().insert (
path, constant.get_node_id (), constant.get_locus (), false,
Rib::ItemType::Const,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
RichLocation r (constant.get_locus ());
r.add_range (locus);
Expand All @@ -82,7 +83,7 @@ class ResolveStmt : public ResolverBase
canonical_prefix);
}

PatternDeclaration::go (stmt.get_pattern ().get ());
PatternDeclaration::go (stmt.get_pattern ().get (), Rib::ItemType::Var);
if (stmt.has_type ())
ResolveType::go (stmt.get_type ().get ());
}
Expand All @@ -97,6 +98,7 @@ class ResolveStmt : public ResolverBase

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

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

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

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

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

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

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

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

resolver->get_name_scope ().insert (
path, function.get_node_id (), function.get_locus (), false,
Rib::ItemType::Function,
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
RichLocation r (function.get_locus ());
r.add_range (locus);
Expand All @@ -343,7 +349,8 @@ class ResolveStmt : public ResolverBase
for (auto &param : function.get_function_params ())
{
ResolveType::go (param.get_type ().get ());
PatternDeclaration::go (param.get_pattern ().get ());
PatternDeclaration::go (param.get_pattern ().get (),
Rib::ItemType::Param);
}

// resolve the function body
Expand Down
Loading

0 comments on commit c833686

Please sign in to comment.