diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-08-31 12:24:33 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-08-31 12:24:33 +0100 |
commit | bd90c95ac738852f270408ad75f31144fb854239 (patch) | |
tree | 4dfa38a5d4758d0d0e1ca9a22d3d70361a801d55 | |
parent | 2ab6f9ee9f294bcd38378905d94fe7b280524414 (diff) | |
download | gcc-bd90c95ac738852f270408ad75f31144fb854239.zip gcc-bd90c95ac738852f270408ad75f31144fb854239.tar.gz gcc-bd90c95ac738852f270408ad75f31144fb854239.tar.bz2 |
Add HIR lowering for QualifiedPathInType
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-type.h | 29 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 56 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 17 |
3 files changed, 101 insertions, 1 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h index c16025d..464045a 100644 --- a/gcc/rust/hir/rust-ast-lower-type.h +++ b/gcc/rust/hir/rust-ast-lower-type.h @@ -28,6 +28,7 @@ namespace HIR { class ASTLowerTypePath : public ASTLoweringBase { +protected: using Rust::HIR::ASTLoweringBase::visit; public: @@ -88,9 +89,30 @@ public: mappings->insert_hir_type (crate_num, hirid, translated); } +protected: + HIR::TypePathSegment *translated_segment; + private: HIR::TypePath *translated; - HIR::TypePathSegment *translated_segment; +}; + +class ASTLowerQualifiedPathInType : public ASTLowerTypePath +{ + using ASTLowerTypePath::visit; + +public: + static HIR::QualifiedPathInType *translate (AST::QualifiedPathInType &type) + { + ASTLowerQualifiedPathInType resolver; + type.accept_vis (resolver); + rust_assert (resolver.translated != nullptr); + return resolver.translated; + } + + void visit (AST::QualifiedPathInType &path) override; + +private: + HIR::QualifiedPathInType *translated; }; class ASTLoweringType : public ASTLoweringBase @@ -187,6 +209,11 @@ public: translated = ASTLowerTypePath::translate (path); } + void visit (AST::QualifiedPathInType &path) override + { + translated = ASTLowerQualifiedPathInType::translate (path); + } + void visit (AST::ArrayType &type) override { HIR::Type *translated_type diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index d5138fd..e8784b6 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -450,6 +450,62 @@ ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment) segment.get_locus ()); } +void +ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path) +{ + auto crate_num = mappings->get_current_crate (); + auto hirid = mappings->get_next_hir_id (crate_num); + Analysis::NodeMapping qual_mappings ( + crate_num, path.get_qualified_path_type ().get_node_id (), hirid, + UNKNOWN_LOCAL_DEFID); + + HIR::Type *qual_type = ASTLoweringType::translate ( + path.get_qualified_path_type ().get_type ().get ()); + HIR::TypePath *qual_trait = ASTLowerTypePath::translate ( + path.get_qualified_path_type ().get_as_type_path ()); + + HIR::QualifiedPathType qual_path_type ( + qual_mappings, std::unique_ptr<HIR::Type> (qual_type), + std::unique_ptr<HIR::TypePath> (qual_trait), + path.get_qualified_path_type ().get_locus ()); + + translated_segment = nullptr; + path.get_associated_segment ()->accept_vis (*this); + if (translated_segment == nullptr) + { + rust_fatal_error (path.get_associated_segment ()->get_locus (), + "failed to translate AST TypePathSegment"); + return; + } + std::unique_ptr<HIR::TypePathSegment> associated_segment (translated_segment); + + 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; + }); + + Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid, + mappings->get_next_localdef_id (crate_num)); + + translated = new HIR::QualifiedPathInType (std::move (mapping), + std::move (qual_path_type), + std::move (associated_segment), + std::move (translated_segments), + path.get_locus ()); + mappings->insert_hir_type (crate_num, hirid, translated); +} + // rust-ast-lower-base HIR::Type * diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 0e3c430..2a491be 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -819,6 +819,7 @@ protected: class QualifiedPathInType : public TypeNoBounds { QualifiedPathType path_type; + std::unique_ptr<TypePathSegment> associated_segment; std::vector<std::unique_ptr<TypePathSegment> > segments; Location locus; @@ -840,9 +841,11 @@ protected: public: QualifiedPathInType ( Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, + std::unique_ptr<TypePathSegment> associated_segment, std::vector<std::unique_ptr<TypePathSegment> > path_segments, Location locus = Location ()) : TypeNoBounds (mappings), path_type (std::move (qual_path_type)), + associated_segment (std::move (associated_segment)), segments (std::move (path_segments)), locus (locus) {} @@ -883,6 +886,20 @@ public: std::string as_string () const override; void accept_vis (HIRVisitor &vis) override; + + QualifiedPathType &get_path_type () { return path_type; } + + std::unique_ptr<TypePathSegment> &get_associated_segment () + { + return associated_segment; + } + + std::vector<std::unique_ptr<TypePathSegment> > &get_segments () + { + return segments; + } + + Location get_locus () { return locus; } }; } // namespace HIR } // namespace Rust |