diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-01-25 19:45:36 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-01-29 14:27:20 +0000 |
commit | a065f205c3171e63b4efee0b9d890782e95a691b (patch) | |
tree | 579891f4bd5354d72a093fd9aab8dd83407f2620 /gcc/rust/resolve/rust-ast-resolve.cc | |
parent | 63686e099bfd0e3e89789942f20a05a750541d6b (diff) | |
download | gcc-a065f205c3171e63b4efee0b9d890782e95a691b.zip gcc-a065f205c3171e63b4efee0b9d890782e95a691b.tar.gz gcc-a065f205c3171e63b4efee0b9d890782e95a691b.tar.bz2 |
This updates our handling of canonical paths to contain the crate-name
In the name resolver there are two types of canonical-path object.
1. The relative canonical path to a type for name resolution
2. The full canonical-path including the crate-name (this-was-missing)
The lack of the crate-name being present in the canonical-path meant the
symbol mangling system was required to append it where apropriate but this
was going to be too messy to handle all cases. Such as module blocks
containing impl blocks requires a prefix::<impl crate::path>::item and
similarly for trait impl blocks.
This patch updates the name-resolution system to build up the
canonical-path of items along side the relative type-paths at the same time
this needs to be done as it is not possible to resolve the canonical path
in the toplevel scan for all names within the crate when it comes to
impl/trait-impl blocks as they may be declared after the block so this
needs to be done at the same time as the normal name resolution mechanisms.
The patch here means the name-manglers no longer need to care about the
crate names of any item which is key for when we need to call functions
in other crates.
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve.cc')
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve.cc | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc index a34e631..5e29e25 100644 --- a/gcc/rust/resolve/rust-ast-resolve.cc +++ b/gcc/rust/resolve/rust-ast-resolve.cc @@ -313,6 +313,13 @@ NameResolution::Resolve (AST::Crate &crate) void NameResolution::go (AST::Crate &crate) { + // lookup current crate name + std::string crate_name; + bool ok + = mappings->get_crate_name (mappings->get_current_crate (), crate_name); + rust_assert (ok); + + // setup the ribs NodeId scope_node_id = crate.get_node_id (); resolver->get_name_scope ().push (scope_node_id); resolver->get_type_scope ().push (scope_node_id); @@ -321,16 +328,24 @@ NameResolution::go (AST::Crate &crate) resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); - // first gather the top-level namespace names then we drill down + // get the root segment + CanonicalPath crate_prefix + = CanonicalPath::new_seg (scope_node_id, crate_name); + + // first gather the top-level namespace names then we drill down so this + // allows for resolving forward declarations since an impl block might have a + // Self type Foo which is defined after the impl block for example. for (auto it = crate.items.begin (); it != crate.items.end (); it++) - ResolveTopLevel::go (it->get ()); + ResolveTopLevel::go (it->get (), CanonicalPath::create_empty (), + crate_prefix); + // FIXME remove this if (saw_errors ()) return; // next we can drill down into the items and their scopes for (auto it = crate.items.begin (); it != crate.items.end (); it++) - ResolveItem::go (it->get ()); + ResolveItem::go (it->get (), CanonicalPath::create_empty (), crate_prefix); } // rust-ast-resolve-expr.h @@ -349,17 +364,19 @@ ResolveExpr::visit (AST::BlockExpr &expr) for (auto &s : expr.get_statements ()) { if (s->is_item ()) - ResolveStmt::go (s.get (), s->get_node_id ()); + ResolveStmt::go (s.get (), s->get_node_id (), prefix, canonical_prefix, + CanonicalPath::create_empty ()); } for (auto &s : expr.get_statements ()) { if (!s->is_item ()) - ResolveStmt::go (s.get (), s->get_node_id ()); + ResolveStmt::go (s.get (), s->get_node_id (), prefix, canonical_prefix, + CanonicalPath::create_empty ()); } if (expr.has_tail_expr ()) - ResolveExpr::go (expr.get_tail_expr ().get (), expr.get_node_id ()); + resolve_expr (expr.get_tail_expr ().get (), expr.get_node_id ()); resolver->get_name_scope ().pop (); resolver->get_type_scope ().pop (); @@ -371,13 +388,15 @@ ResolveExpr::visit (AST::BlockExpr &expr) void ResolveStructExprField::visit (AST::StructExprFieldIdentifierValue &field) { - ResolveExpr::go (field.get_value ().get (), field.get_node_id ()); + ResolveExpr::go (field.get_value ().get (), field.get_node_id (), prefix, + canonical_prefix); } void ResolveStructExprField::visit (AST::StructExprFieldIndexValue &field) { - ResolveExpr::go (field.get_value ().get (), field.get_node_id ()); + ResolveExpr::go (field.get_value ().get (), field.get_node_id (), prefix, + canonical_prefix); } void @@ -386,7 +405,7 @@ ResolveStructExprField::visit (AST::StructExprFieldIdentifier &field) AST::IdentifierExpr expr (field.get_field_name (), {}, field.get_locus ()); expr.set_node_id (field.get_node_id ()); - ResolveExpr::go (&expr, field.get_node_id ()); + ResolveExpr::go (&expr, field.get_node_id (), prefix, canonical_prefix); } // rust-ast-resolve-type.h @@ -746,7 +765,12 @@ void ResolveType::visit (AST::ArrayType &type) { type.get_elem_type ()->accept_vis (*this); - ResolveExpr::go (type.get_size_expr ().get (), type.get_node_id ()); + // FIXME + // the capacity expr can contain block-expr with functions but these should be + // folded via constexpr code + ResolveExpr::go (type.get_size_expr ().get (), type.get_node_id (), + CanonicalPath::create_empty (), + CanonicalPath::create_empty ()); } void @@ -771,15 +795,19 @@ ResolveType::visit (AST::TraitObjectType &type) // rust-ast-resolve-item.h void -ResolveItem::resolve_impl_item (AST::TraitImplItem *item) +ResolveItem::resolve_impl_item (AST::TraitImplItem *item, + const CanonicalPath &prefix, + const CanonicalPath &canonical_prefix) { - ResolveImplItems::go (item); + ResolveImplItems::go (item, prefix, canonical_prefix); } void -ResolveItem::resolve_impl_item (AST::InherentImplItem *item) +ResolveItem::resolve_impl_item (AST::InherentImplItem *item, + const CanonicalPath &prefix, + const CanonicalPath &canonical_prefix) { - ResolveImplItems::go (item); + ResolveImplItems::go (item, prefix, canonical_prefix); } void |