diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-06-16 15:17:07 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-06-16 15:17:07 +0100 |
commit | 9d9ba03360721158e2000f19f6f65896f5af88b0 (patch) | |
tree | eb920e6388e9d782bab2efc195f9c15e6edab20b | |
parent | 730a2174b933162c42843af43d85851b85d64144 (diff) | |
download | gcc-9d9ba03360721158e2000f19f6f65896f5af88b0.zip gcc-9d9ba03360721158e2000f19f6f65896f5af88b0.tar.gz gcc-9d9ba03360721158e2000f19f6f65896f5af88b0.tar.bz2 |
Impl blocks can have a trait reference
This refactors Type HIR lowering to extract TypePath AST -> HIR lowering
function, as Trait References are simply TypePaths.
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 5 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-type.h | 131 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 28 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 27 |
4 files changed, 113 insertions, 78 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 9c0caf2..bb9f764 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -387,7 +387,7 @@ public: HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (mapping, std::move (impl_items), std::move (generic_params), - std::unique_ptr<HIR::Type> (impl_type), + std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (), impl_block.get_locus ()); @@ -507,6 +507,8 @@ public: HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ().get ()); + HIR::TypePath *trait_ref + = ASTLowerTypePath::translate (impl_block.get_trait_path ()); auto crate_num = mappings->get_current_crate (); Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (), @@ -528,6 +530,7 @@ public: = new HIR::ImplBlock (mapping, std::move (impl_items), std::move (generic_params), std::unique_ptr<HIR::Type> (impl_type), + std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (), impl_block.get_locus ()); diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h index 635c018..86e802e 100644 --- a/gcc/rust/hir/rust-ast-lower-type.h +++ b/gcc/rust/hir/rust-ast-lower-type.h @@ -26,6 +26,68 @@ namespace Rust { namespace HIR { +class ASTLowerTypePath : public ASTLoweringBase +{ + using Rust::HIR::ASTLoweringBase::visit; + +public: + static HIR::TypePath *translate (AST::TypePath &type) + { + ASTLowerTypePath resolver; + type.accept_vis (resolver); + + rust_assert (resolver.translated != nullptr); + + return resolver.translated; + } + + void visit (AST::TypePathSegment &segment) override + { + HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ()); + translated_segment + = new HIR::TypePathSegment (ident, + segment.get_separating_scope_resolution (), + segment.get_locus ()); + } + + void visit (AST::TypePathSegmentGeneric &segment) override; + + void visit (AST::TypePath &path) override + { + std::vector<std::unique_ptr<HIR::TypePathSegment> > translated_segments; + + path.iterate_segments ([&] (AST::TypePathSegment *seg) mutable -> bool { + translated_segment = nullptr; + seg->accept_vis (*this); + if (translated_segment == nullptr) + { + rust_fatal_error (seg->get_locus (), + "failed to translate AST TypePathSegment"); + return false; + } + + translated_segments.push_back ( + std::unique_ptr<HIR::TypePathSegment> (translated_segment)); + return true; + }); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, path.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + translated + = new HIR::TypePath (std::move (mapping), std::move (translated_segments), + path.get_locus (), + path.has_opening_scope_resolution_op ()); + mappings->insert_hir_type (mapping.get_crate_num (), mapping.get_hirid (), + translated); + } + +private: + HIR::TypePath *translated; + HIR::TypePathSegment *translated_segment; +}; + class ASTLoweringType : public ASTLoweringBase { using Rust::HIR::ASTLoweringBase::visit; @@ -116,71 +178,9 @@ public: tuple.get_locus ()); } - void visit (AST::TypePathSegment &segment) override - { - HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ()); - translated_segment - = new HIR::TypePathSegment (ident, - segment.get_separating_scope_resolution (), - segment.get_locus ()); - } - - void visit (AST::TypePathSegmentGeneric &segment) override - { - std::vector<HIR::GenericArgsBinding> binding_args; // TODO - - std::string segment_name = segment.get_ident_segment ().as_string (); - bool has_separating_scope_resolution - = segment.get_separating_scope_resolution (); - - std::vector<HIR::Lifetime> lifetime_args; - for (auto &lifetime : segment.get_generic_args ().get_lifetime_args ()) - { - HIR::Lifetime l = lower_lifetime (lifetime); - lifetime_args.push_back (std::move (l)); - } - - std::vector<std::unique_ptr<HIR::Type> > type_args; - for (auto &type : segment.get_generic_args ().get_type_args ()) - { - HIR::Type *t = ASTLoweringType::translate (type.get ()); - type_args.push_back (std::unique_ptr<HIR::Type> (t)); - } - - translated_segment = new HIR::TypePathSegmentGeneric ( - segment_name, has_separating_scope_resolution, std::move (lifetime_args), - std::move (type_args), std::move (binding_args), segment.get_locus ()); - } - void visit (AST::TypePath &path) override { - std::vector<std::unique_ptr<HIR::TypePathSegment> > translated_segments; - - path.iterate_segments ([&] (AST::TypePathSegment *seg) mutable -> bool { - translated_segment = nullptr; - seg->accept_vis (*this); - if (translated_segment == nullptr) - { - rust_fatal_error (seg->get_locus (), - "failed to translate AST TypePathSegment"); - return false; - } - - translated_segments.push_back ( - std::unique_ptr<HIR::TypePathSegment> (translated_segment)); - return true; - }); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, path.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - translated - = new HIR::TypePath (std::move (mapping), std::move (translated_segments), - path.get_locus (), - path.has_opening_scope_resolution_op ()); - mappings->insert_hir_type (mapping.get_crate_num (), mapping.get_hirid (), - translated); + translated = ASTLowerTypePath::translate (path); } void visit (AST::ArrayType &type) override @@ -238,12 +238,9 @@ public: } private: - ASTLoweringType () - : ASTLoweringBase (), translated (nullptr), translated_segment (nullptr) - {} + ASTLoweringType () : ASTLoweringBase (), translated (nullptr) {} HIR::Type *translated; - HIR::TypePathSegment *translated_segment; }; class ASTLowerGenericParam : public ASTLoweringBase diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index 1abdd3c..c7222e2 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -363,5 +363,33 @@ ASTLoweringBase::lower_self (AST::SelfParam &self) self.get_is_mut (), self.get_locus ()); } +void +ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment) +{ + std::vector<HIR::GenericArgsBinding> binding_args; // TODO + + std::string segment_name = segment.get_ident_segment ().as_string (); + bool has_separating_scope_resolution + = segment.get_separating_scope_resolution (); + + std::vector<HIR::Lifetime> lifetime_args; + for (auto &lifetime : segment.get_generic_args ().get_lifetime_args ()) + { + HIR::Lifetime l = lower_lifetime (lifetime); + lifetime_args.push_back (std::move (l)); + } + + std::vector<std::unique_ptr<HIR::Type> > type_args; + for (auto &type : segment.get_generic_args ().get_type_args ()) + { + HIR::Type *t = ASTLoweringType::translate (type.get ()); + type_args.push_back (std::unique_ptr<HIR::Type> (t)); + } + + translated_segment = new HIR::TypePathSegmentGeneric ( + segment_name, has_separating_scope_resolution, std::move (lifetime_args), + std::move (type_args), std::move (binding_args), segment.get_locus ()); +} + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 3bfdf71..74969f9 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2884,33 +2884,28 @@ class ImplBlock : public VisItem { std::vector<std::unique_ptr<GenericParam> > generic_params; std::unique_ptr<Type> impl_type; + std::unique_ptr<TypePath> trait_ref; WhereClause where_clause; AST::AttrVec inner_attrs; Location locus; std::vector<std::unique_ptr<ImplItem> > impl_items; public: - std::string as_string () const override; - - // Returns whether inherent impl block has inherent impl items. - bool has_impl_items () const { return !impl_items.empty (); } - - // Mega-constructor ImplBlock (Analysis::NodeMapping mappings, std::vector<std::unique_ptr<ImplItem> > impl_items, std::vector<std::unique_ptr<GenericParam> > generic_params, - std::unique_ptr<Type> impl_type, WhereClause where_clause, + std::unique_ptr<Type> impl_type, + std::unique_ptr<TypePath> trait_ref, WhereClause where_clause, Visibility vis, AST::AttrVec inner_attrs, AST::AttrVec outer_attrs, Location locus) : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), generic_params (std::move (generic_params)), - impl_type (std::move (impl_type)), + impl_type (std::move (impl_type)), trait_ref (std::move (trait_ref)), where_clause (std::move (where_clause)), inner_attrs (std::move (inner_attrs)), locus (locus), impl_items (std::move (impl_items)) {} - // Copy constructor with vector clone ImplBlock (ImplBlock const &other) : VisItem (other), impl_type (other.impl_type->clone_type ()), where_clause (other.where_clause), inner_attrs (other.inner_attrs), @@ -2925,7 +2920,6 @@ public: impl_items.push_back (e->clone_inherent_impl_item ()); } - // Overloaded assignment operator with vector clone ImplBlock &operator= (ImplBlock const &other) { VisItem::operator= (other); @@ -2948,6 +2942,11 @@ public: ImplBlock (ImplBlock &&other) = default; ImplBlock &operator= (ImplBlock &&other) = default; + std::string as_string () const override; + + // Returns whether inherent impl block has inherent impl items. + bool has_impl_items () const { return !impl_items.empty (); } + void accept_vis (HIRVisitor &vis) override; std::vector<std::unique_ptr<ImplItem> > &get_impl_items () @@ -2978,6 +2977,14 @@ public: return generic_params; } + bool has_trait_ref () const { return trait_ref != nullptr; } + + std::unique_ptr<TypePath> &get_trait_ref () + { + rust_assert (has_trait_ref ()); + return trait_ref; + } + protected: ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); } }; |