diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-01-29 15:00:30 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-29 15:00:30 +0000 |
commit | 631f7ae7b9dbb55ed3adce942952f93b48983a53 (patch) | |
tree | f5fc89773ab1941593f3f2230908919063679c68 /gcc/rust/resolve/rust-ast-resolve-toplevel.h | |
parent | bc7f518ac6f5c05f1c6edd7a1601f32753bab47d (diff) | |
parent | 2fbf9cb25e930e2df86d05e0f7f707e69bae2b1f (diff) | |
download | gcc-631f7ae7b9dbb55ed3adce942952f93b48983a53.zip gcc-631f7ae7b9dbb55ed3adce942952f93b48983a53.tar.gz gcc-631f7ae7b9dbb55ed3adce942952f93b48983a53.tar.bz2 |
Merge #894
894: Update name-resolution to build up canonical-path with the crate-name r=philberty a=philberty
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 appropriate 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.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve-toplevel.h')
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-toplevel.h | 167 |
1 files changed, 126 insertions, 41 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index 4993649..56962f6 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -33,17 +33,20 @@ class ResolveTopLevel : public ResolverBase using Rust::Resolver::ResolverBase::visit; public: - static void go (AST::Item *item, - const CanonicalPath &prefix = CanonicalPath::create_empty ()) + static void go (AST::Item *item, const CanonicalPath &prefix, + const CanonicalPath &canonical_prefix) { - ResolveTopLevel resolver (prefix); + ResolveTopLevel resolver (prefix, canonical_prefix); item->accept_vis (resolver); }; void visit (AST::Module &module) override { - auto path = prefix.append ( - CanonicalPath::new_seg (module.get_node_id (), module.get_name ())); + auto mod + = CanonicalPath::new_seg (module.get_node_id (), module.get_name ()); + auto path = prefix.append (mod); + auto cpath = canonical_prefix.append (mod); + resolver->get_name_scope ().insert ( path, module.get_node_id (), module.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -57,14 +60,19 @@ public: module.get_node_id ()}); for (auto &item : module.get_items ()) - ResolveTopLevel::go (item.get (), path); + ResolveTopLevel::go (item.get (), path, cpath); + + mappings->insert_canonical_path (mappings->get_current_crate (), + module.get_node_id (), cpath); } void visit (AST::TypeAlias &alias) override { - auto path - = prefix.append (CanonicalPath::new_seg (alias.get_node_id (), - alias.get_new_type_name ())); + auto talias = CanonicalPath::new_seg (alias.get_node_id (), + alias.get_new_type_name ()); + auto path = prefix.append (talias); + auto cpath = canonical_prefix.append (talias); + resolver->get_type_scope ().insert ( path, alias.get_node_id (), alias.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -72,13 +80,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + alias.get_node_id (), cpath); } void visit (AST::TupleStruct &struct_decl) override { - auto path - = prefix.append (CanonicalPath::new_seg (struct_decl.get_node_id (), - struct_decl.get_identifier ())); + auto decl = CanonicalPath::new_seg (struct_decl.get_node_id (), + struct_decl.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -86,13 +99,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + struct_decl.get_node_id (), cpath); } void visit (AST::Enum &enum_decl) override { - auto path - = prefix.append (CanonicalPath::new_seg (enum_decl.get_node_id (), - enum_decl.get_identifier ())); + auto decl = CanonicalPath::new_seg (enum_decl.get_node_id (), + enum_decl.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, enum_decl.get_node_id (), enum_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -102,13 +120,19 @@ public: }); for (auto &variant : enum_decl.get_variants ()) - ResolveTopLevel::go (variant.get (), path); + ResolveTopLevel::go (variant.get (), path, cpath); + + mappings->insert_canonical_path (mappings->get_current_crate (), + enum_decl.get_node_id (), cpath); } void visit (AST::EnumItem &item) override { - auto path = prefix.append ( - CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ())); + auto decl + = CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, item.get_node_id (), item.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -116,12 +140,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + item.get_node_id (), cpath); } void visit (AST::EnumItemTuple &item) override { - auto path = prefix.append ( - CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ())); + auto decl + = CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, item.get_node_id (), item.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -129,12 +159,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + item.get_node_id (), cpath); } void visit (AST::EnumItemStruct &item) override { - auto path = prefix.append ( - CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ())); + auto decl + = CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, item.get_node_id (), item.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -142,12 +178,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + item.get_node_id (), cpath); } void visit (AST::EnumItemDiscriminant &item) override { - auto path = prefix.append ( - CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ())); + auto decl + = CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, item.get_node_id (), item.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -155,13 +197,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + item.get_node_id (), cpath); } void visit (AST::StructStruct &struct_decl) override { - auto path - = prefix.append (CanonicalPath::new_seg (struct_decl.get_node_id (), - struct_decl.get_identifier ())); + auto decl = CanonicalPath::new_seg (struct_decl.get_node_id (), + struct_decl.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -169,13 +216,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + struct_decl.get_node_id (), cpath); } void visit (AST::Union &union_decl) override { - auto path - = prefix.append (CanonicalPath::new_seg (union_decl.get_node_id (), - union_decl.get_identifier ())); + auto decl = CanonicalPath::new_seg (union_decl.get_node_id (), + union_decl.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, union_decl.get_node_id (), union_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -183,12 +235,18 @@ public: r.add_range (locus); rust_error_at (r, "redefined multiple times"); }); + + mappings->insert_canonical_path (mappings->get_current_crate (), + union_decl.get_node_id (), cpath); } void visit (AST::StaticItem &var) override { - auto path = prefix.append ( - CanonicalPath::new_seg (var.get_node_id (), var.get_identifier ())); + auto decl + = CanonicalPath::new_seg (var.get_node_id (), var.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_name_scope ().insert ( path, var.get_node_id (), var.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -200,12 +258,17 @@ public: Definition{var.get_node_id (), var.get_node_id ()}); resolver->mark_decl_mutability (var.get_node_id (), var.is_mutable ()); + + mappings->insert_canonical_path (mappings->get_current_crate (), + var.get_node_id (), cpath); } void visit (AST::ConstantItem &constant) override { - auto path - = prefix.append (ResolveConstantItemToCanonicalPath::resolve (constant)); + auto decl = ResolveConstantItemToCanonicalPath::resolve (constant); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_name_scope ().insert ( path, constant.get_node_id (), constant.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -216,6 +279,9 @@ public: resolver->insert_new_definition (constant.get_node_id (), Definition{constant.get_node_id (), constant.get_node_id ()}); + + mappings->insert_canonical_path (mappings->get_current_crate (), + constant.get_node_id (), cpath); } void visit (AST::Function &function) override @@ -223,8 +289,10 @@ public: if (function.is_marked_for_strip ()) return; - auto path - = prefix.append (ResolveFunctionItemToCanonicalPath::resolve (function)); + auto decl = ResolveFunctionItemToCanonicalPath::resolve (function); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_name_scope ().insert ( path, function.get_node_id (), function.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -243,12 +311,16 @@ public: resolver->insert_resolved_name (function.get_node_id (), function.get_node_id ()); } + + mappings->insert_canonical_path (mappings->get_current_crate (), + function.get_node_id (), cpath); } void visit (AST::InherentImpl &impl_block) override { bool canonicalize_type_args = !impl_block.has_generics (); bool type_resolve_generic_args = false; + CanonicalPath impl_type = ResolveTypeToCanonicalPath::resolve (*impl_block.get_type ().get (), canonicalize_type_args, @@ -257,6 +329,8 @@ public: for (auto &impl_item : impl_block.get_impl_items ()) ResolveToplevelImplItem::go (impl_item.get (), impl_prefix); + + // we cannot resolve canonical paths here until later on } void visit (AST::TraitImpl &impl_block) override @@ -293,12 +367,17 @@ public: for (auto &impl_item : impl_block.get_impl_items ()) ResolveToplevelImplItem::go (impl_item.get (), impl_prefix); + + // we cannot resolve canonical paths here until later on } void visit (AST::Trait &trait) override { - CanonicalPath path = prefix.append ( - CanonicalPath::new_seg (trait.get_node_id (), trait.get_identifier ())); + auto decl + = CanonicalPath::new_seg (trait.get_node_id (), trait.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + resolver->get_type_scope ().insert ( path, trait.get_node_id (), trait.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -308,7 +387,10 @@ public: }); for (auto &item : trait.get_trait_items ()) - ResolveTopLevelTraitItems::go (item.get (), path); + ResolveTopLevelTraitItems::go (item.get (), path, cpath); + + mappings->insert_canonical_path (mappings->get_current_crate (), + trait.get_node_id (), cpath); } void visit (AST::ExternBlock &extern_block) override @@ -320,11 +402,14 @@ public: } private: - ResolveTopLevel (const CanonicalPath &prefix) - : ResolverBase (UNKNOWN_NODEID), prefix (prefix) + ResolveTopLevel (const CanonicalPath &prefix, + const CanonicalPath &canonical_prefix) + : ResolverBase (UNKNOWN_NODEID), prefix (prefix), + canonical_prefix (canonical_prefix) {} const CanonicalPath &prefix; + const CanonicalPath &canonical_prefix; }; } // namespace Resolver |