diff options
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve-item.cc')
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.cc | 108 |
1 files changed, 67 insertions, 41 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index 34098bc..d584961 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -61,11 +61,11 @@ ResolveTraitItems::visit (AST::Function &function) resolver->get_label_scope ().push (scope_node_id); resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); if (function.has_generics ()) - for (auto &generic : function.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (function.get_generic_params (), prefix, + canonical_prefix); if (function.has_return_type ()) ResolveType::go (function.get_return_type ()); @@ -188,8 +188,8 @@ ResolveItem::visit (AST::TypeAlias &alias) resolver->get_type_scope ().push (scope_node_id); if (alias.has_generics ()) - for (auto &generic : alias.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (alias.get_generic_params (), prefix, + canonical_prefix); if (alias.has_where_clause ()) ResolveWhereClause::Resolve (alias.get_where_clause ()); @@ -216,7 +216,7 @@ ResolveItem::visit (AST::Module &module) resolver->get_label_scope ().push (scope_node_id); resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); // FIXME: Should we reinsert a child here? Any reason we ResolveTopLevel::go // in ResolveTopLevel::visit (AST::Module) as well as here? @@ -250,8 +250,8 @@ ResolveItem::visit (AST::TupleStruct &struct_decl) resolver->get_type_scope ().push (scope_node_id); if (struct_decl.has_generics ()) - for (auto &generic : struct_decl.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (struct_decl.get_generic_params (), prefix, + canonical_prefix); if (struct_decl.has_where_clause ()) ResolveWhereClause::Resolve (struct_decl.get_where_clause ()); @@ -284,8 +284,8 @@ ResolveItem::visit (AST::Enum &enum_decl) resolver->get_type_scope ().push (scope_node_id); if (enum_decl.has_generics ()) - for (auto &generic : enum_decl.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, cpath); + ResolveGenericParams::go (enum_decl.get_generic_params (), prefix, + canonical_prefix); if (enum_decl.has_where_clause ()) ResolveWhereClause::Resolve (enum_decl.get_where_clause ()); @@ -374,8 +374,8 @@ ResolveItem::visit (AST::StructStruct &struct_decl) resolver->get_type_scope ().push (scope_node_id); if (struct_decl.has_generics ()) - for (auto &generic : struct_decl.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (struct_decl.get_generic_params (), prefix, + canonical_prefix); if (struct_decl.has_where_clause ()) ResolveWhereClause::Resolve (struct_decl.get_where_clause ()); @@ -409,8 +409,8 @@ ResolveItem::visit (AST::Union &union_decl) resolver->get_type_scope ().push (scope_node_id); if (union_decl.has_generics ()) - for (auto &generic : union_decl.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (union_decl.get_generic_params (), prefix, + canonical_prefix); if (union_decl.has_where_clause ()) ResolveWhereClause::Resolve (union_decl.get_where_clause ()); @@ -473,11 +473,11 @@ ResolveItem::visit (AST::Function &function) resolver->get_label_scope ().push (scope_node_id); resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); if (function.has_generics ()) - for (auto &generic : function.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (function.get_generic_params (), prefix, + canonical_prefix); // resolve any where clause items if (function.has_where_clause ()) @@ -567,8 +567,8 @@ ResolveItem::visit (AST::InherentImpl &impl_block) resolve_visibility (impl_block.get_visibility ()); if (impl_block.has_generics ()) - for (auto &generic : impl_block.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (impl_block.get_generic_params (), prefix, + canonical_prefix); // resolve any where clause items if (impl_block.has_where_clause ()) @@ -582,7 +582,14 @@ ResolveItem::visit (AST::InherentImpl &impl_block) // Setup paths CanonicalPath self_cpath = CanonicalPath::create_empty (); bool ok = ResolveTypeToCanonicalPath::go (impl_block.get_type (), self_cpath); - rust_assert (ok); + if (!ok) + { + resolver->get_name_scope ().pop (); + resolver->get_type_scope ().pop (); + resolver->get_label_scope ().pop (); + return; + } + rust_debug ("AST::InherentImpl resolve Self: {%s}", self_cpath.get ().c_str ()); @@ -641,11 +648,11 @@ ResolveItem::visit (AST::TraitImpl &impl_block) resolver->get_label_scope ().push (scope_node_id); resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); if (impl_block.has_generics ()) - for (auto &generic : impl_block.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (impl_block.get_generic_params (), prefix, + canonical_prefix); // resolve any where clause items if (impl_block.has_where_clause ()) @@ -671,12 +678,20 @@ ResolveItem::visit (AST::TraitImpl &impl_block) return; } - bool ok; + bool ok = true; + // setup paths CanonicalPath canonical_trait_type = CanonicalPath::create_empty (); + ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path (), canonical_trait_type); - rust_assert (ok); + if (!ok) + { + resolver->get_name_scope ().pop (); + resolver->get_type_scope ().pop (); + resolver->get_label_scope ().pop (); + return; + } rust_debug ("AST::TraitImpl resolve trait type: {%s}", canonical_trait_type.get ().c_str ()); @@ -684,7 +699,13 @@ ResolveItem::visit (AST::TraitImpl &impl_block) CanonicalPath canonical_impl_type = CanonicalPath::create_empty (); ok = ResolveTypeToCanonicalPath::go (impl_block.get_type (), canonical_impl_type); - rust_assert (ok); + if (!ok) + { + resolver->get_name_scope ().pop (); + resolver->get_type_scope ().pop (); + resolver->get_label_scope ().pop (); + return; + } rust_debug ("AST::TraitImpl resolve self: {%s}", canonical_impl_type.get ().c_str ()); @@ -755,20 +776,14 @@ ResolveItem::visit (AST::Trait &trait) resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - // we need to inject an implicit self TypeParam here - // FIXME: which location should be used for Rust::Identifier `Self`? - AST::TypeParam *implicit_self - = new AST::TypeParam ({"Self"}, trait.get_locus ()); - trait.insert_implict_self ( - std::unique_ptr<AST::GenericParam> (implicit_self)); - CanonicalPath Self = CanonicalPath::get_big_self (trait.get_node_id ()); - - for (auto &generic : trait.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go_single (trait.get_implicit_self (), prefix, + canonical_prefix); + ResolveGenericParams::go (trait.get_generic_params (), prefix, + canonical_prefix); // Self is an implicit TypeParam so lets mark it as such resolver->get_type_scope ().append_reference_for_def ( - Self.get_node_id (), implicit_self->get_node_id ()); + trait.get_node_id (), trait.get_implicit_self ().get_node_id ()); if (trait.has_type_param_bounds ()) { @@ -888,7 +903,18 @@ flatten_list (const AST::UseTreeList &list, std::vector<Import> &imports) for (auto import = imports.begin () + start_idx; import != imports.end (); import++) - import->add_prefix (prefix); + { + // avoid duplicate node ids + auto prefix_copy + = AST::SimplePath ({}, prefix.has_opening_scope_resolution (), + prefix.get_locus ()); + for (auto &seg : prefix.get_segments ()) + prefix_copy.get_segments ().push_back ( + AST::SimplePathSegment (seg.get_segment_name (), + seg.get_locus ())); + + import->add_prefix (std::move (prefix_copy)); + } } } @@ -1025,12 +1051,12 @@ ResolveExternItem::visit (AST::Function &function) resolver->get_label_scope ().push (scope_node_id); resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); - resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); // resolve the generics if (function.has_generics ()) - for (auto &generic : function.get_generic_params ()) - ResolveGenericParam::go (*generic, prefix, canonical_prefix); + ResolveGenericParams::go (function.get_generic_params (), prefix, + canonical_prefix); if (function.has_return_type ()) ResolveType::go (function.get_return_type ()); |