From be717fca2f2f406166383d60741ca41aa0c514f0 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 16:08:14 +0100 Subject: Add missing node mappings to HIR::WhereClauseItems --- gcc/rust/hir/tree/rust-hir-item.h | 61 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'gcc/rust/hir') diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index ab9eab6..30170a9 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -20,6 +20,7 @@ #define RUST_HIR_ITEM_H #include "rust-ast-full-decls.h" +#include "rust-common.h" #include "rust-hir.h" #include "rust-hir-path.h" @@ -152,6 +153,8 @@ public: virtual void accept_vis (HIRVisitor &vis) = 0; + virtual Analysis::NodeMapping get_mappings () const = 0; + protected: // Clone function implementation as pure virtual method virtual WhereClauseItem *clone_where_clause_item_impl () const = 0; @@ -161,24 +164,32 @@ protected: class LifetimeWhereClauseItem : public WhereClauseItem { Lifetime lifetime; - - // LifetimeBounds lifetime_bounds; - std::vector lifetime_bounds; // inlined lifetime bounds - + std::vector lifetime_bounds; Location locus; + Analysis::NodeMapping mappings; public: - LifetimeWhereClauseItem (Lifetime lifetime, + LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime, std::vector lifetime_bounds, Location locus) : lifetime (std::move (lifetime)), - lifetime_bounds (std::move (lifetime_bounds)), locus (locus) + lifetime_bounds (std::move (lifetime_bounds)), locus (locus), + mappings (std::move (mappings)) {} std::string as_string () const override; void accept_vis (HIRVisitor &vis) override; + Lifetime &get_lifetime () { return lifetime; } + + std::vector &get_lifetime_bounds () { return lifetime_bounds; } + + Analysis::NodeMapping get_mappings () const override final + { + return mappings; + }; + protected: // Clone function implementation as (not pure) virtual method LifetimeWhereClauseItem *clone_where_clause_item_impl () const override @@ -190,18 +201,10 @@ protected: // A type bound where clause item class TypeBoundWhereClauseItem : public WhereClauseItem { - // bool has_for_lifetimes; - // LifetimeParams for_lifetimes; - std::vector for_lifetimes; // inlined - + std::vector for_lifetimes; std::unique_ptr bound_type; - - // bool has_type_param_bounds; - // TypeParamBounds type_param_bounds; - std::vector> - type_param_bounds; // inlined form - - // should this store location info? + std::vector> type_param_bounds; + Analysis::NodeMapping mappings; public: // Returns whether the item has ForLifetimes @@ -211,17 +214,19 @@ public: bool has_type_param_bounds () const { return !type_param_bounds.empty (); } TypeBoundWhereClauseItem ( - std::vector for_lifetimes, std::unique_ptr bound_type, + Analysis::NodeMapping mappings, std::vector for_lifetimes, + std::unique_ptr bound_type, std::vector> type_param_bounds) : for_lifetimes (std::move (for_lifetimes)), bound_type (std::move (bound_type)), - type_param_bounds (std::move (type_param_bounds)) + type_param_bounds (std::move (type_param_bounds)), + mappings (std::move (mappings)) {} // Copy constructor requires clone TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other) : for_lifetimes (other.for_lifetimes), - bound_type (other.bound_type->clone_type ()) + bound_type (other.bound_type->clone_type ()), mappings (other.mappings) { type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -231,9 +236,9 @@ public: // Overload assignment operator to clone TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other) { + mappings = other.mappings; for_lifetimes = other.for_lifetimes; bound_type = other.bound_type->clone_type (); - type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) type_param_bounds.push_back (e->clone_type_param_bound ()); @@ -250,6 +255,20 @@ public: void accept_vis (HIRVisitor &vis) override; + std::vector &get_for_lifetimes () { return for_lifetimes; } + + std::unique_ptr &get_bound_type () { return bound_type; } + + std::vector> &get_type_param_bounds () + { + return type_param_bounds; + } + + Analysis::NodeMapping get_mappings () const override final + { + return mappings; + }; + protected: // Clone function implementation as (not pure) virtual method TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override -- cgit v1.1 From b01e62c136b1ebf27894c68669e265e67b941300 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 17:08:55 +0100 Subject: Add WhereClauseItem::ItemType specifier to differentiate between items --- gcc/rust/hir/tree/rust-hir-item.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'gcc/rust/hir') diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 30170a9..de067e6 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -141,6 +141,12 @@ protected: class WhereClauseItem { public: + enum ItemType + { + LIFETIME, + TYPE_BOUND, + }; + virtual ~WhereClauseItem () {} // Unique pointer custom clone function @@ -155,6 +161,8 @@ public: virtual Analysis::NodeMapping get_mappings () const = 0; + virtual ItemType get_item_type () const = 0; + protected: // Clone function implementation as pure virtual method virtual WhereClauseItem *clone_where_clause_item_impl () const = 0; @@ -190,6 +198,11 @@ public: return mappings; }; + ItemType get_item_type () const override final + { + return WhereClauseItem::ItemType::LIFETIME; + } + protected: // Clone function implementation as (not pure) virtual method LifetimeWhereClauseItem *clone_where_clause_item_impl () const override @@ -269,6 +282,11 @@ public: return mappings; }; + ItemType get_item_type () const override final + { + return WhereClauseItem::ItemType::TYPE_BOUND; + } + protected: // Clone function implementation as (not pure) virtual method TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override -- cgit v1.1 From 1a9746032345e1bffeb9ff2c92473bf90f843379 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 17:34:21 +0100 Subject: Add HIR lowering for where clause items --- gcc/rust/hir/rust-ast-lower-item.h | 72 ++++++++++++++++++++++++++++++++++++-- gcc/rust/hir/rust-ast-lower-type.h | 72 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 6 deletions(-) (limited to 'gcc/rust/hir') diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 9372e94..db0425f 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -95,6 +95,14 @@ public: void visit (AST::TypeAlias &alias) override { std::vector> where_clause_items; + for (auto &item : alias.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -134,6 +142,14 @@ public: } std::vector> where_clause_items; + for (auto &item : struct_decl.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -186,6 +202,14 @@ public: } std::vector> where_clause_items; + for (auto &item : struct_decl.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -242,6 +266,14 @@ public: } std::vector> where_clause_items; + for (auto &item : enum_decl.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -282,6 +314,13 @@ public: } std::vector> where_clause_items; + for (auto &item : union_decl.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -380,8 +419,15 @@ public: void visit (AST::Function &function) override { - // ignore for now and leave empty std::vector> where_clause_items; + for (auto &item : function.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::FunctionQualifiers qualifiers ( HIR::FunctionQualifiers::AsyncConstStatus::NONE, Unsafety::Normal); @@ -466,6 +512,13 @@ public: void visit (AST::InherentImpl &impl_block) override { std::vector> where_clause_items; + for (auto &item : impl_block.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -545,8 +598,15 @@ public: void visit (AST::Trait &trait) override { std::vector> where_clause_items; - + for (auto &item : trait.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::Visibility vis = HIR::Visibility::create_public (); std::vector> generic_params; @@ -632,7 +692,13 @@ public: void visit (AST::TraitImpl &impl_block) override { std::vector> where_clause_items; - + for (auto &item : impl_block.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back ( + std::unique_ptr (i)); + } HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h index 0a71e3a..8205d07 100644 --- a/gcc/rust/hir/rust-ast-lower-type.h +++ b/gcc/rust/hir/rust-ast-lower-type.h @@ -60,7 +60,7 @@ public: void visit (AST::TypePath &path) override { - std::vector > translated_segments; + std::vector> translated_segments; path.iterate_segments ([&] (AST::TypePathSegment *seg) mutable -> bool { translated_segment = nullptr; @@ -188,7 +188,7 @@ public: void visit (AST::TupleType &tuple) override { - std::vector > elems; + std::vector> elems; for (auto &e : tuple.get_elems ()) { HIR::Type *t = ASTLoweringType::translate (e.get ()); @@ -340,7 +340,7 @@ public: void visit (AST::TypeParam ¶m) override { AST::Attribute outer_attr = AST::Attribute::create_empty (); - std::vector > type_param_bounds; + std::vector> type_param_bounds; if (param.has_type_param_bounds ()) { for (auto &bound : param.get_type_param_bounds ()) @@ -422,6 +422,72 @@ private: HIR::TypeParamBound *translated; }; +class ASTLowerWhereClauseItem : public ASTLoweringBase +{ + using Rust::HIR::ASTLoweringBase::visit; + +public: + static HIR::WhereClauseItem *translate (AST::WhereClauseItem &item) + { + ASTLowerWhereClauseItem compiler; + item.accept_vis (compiler); + rust_assert (compiler.translated != nullptr); + return compiler.translated; + } + + void visit (AST::LifetimeWhereClauseItem &item) override + { + HIR::Lifetime l = lower_lifetime (item.get_lifetime ()); + std::vector lifetime_bounds; + for (auto &lifetime_bound : item.get_lifetime_bounds ()) + { + HIR::Lifetime ll = lower_lifetime (lifetime_bound); + lifetime_bounds.push_back (std::move (ll)); + } + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, item.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + translated = new HIR::LifetimeWhereClauseItem (mapping, std::move (l), + std::move (lifetime_bounds), + item.get_locus ()); + } + + void visit (AST::TypeBoundWhereClauseItem &item) override + { + // FIXME + std::vector for_lifetimes; + + std::unique_ptr bound_type = std::unique_ptr ( + ASTLoweringType::translate (item.get_type ().get ())); + + std::vector> type_param_bounds; + for (auto &bound : item.get_type_param_bounds ()) + { + HIR::TypeParamBound *b + = ASTLoweringTypeBounds::translate (bound.get ()); + type_param_bounds.push_back (std::unique_ptr (b)); + } + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, item.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + translated + = new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes), + std::move (bound_type), + std::move (type_param_bounds)); + } + +private: + ASTLowerWhereClauseItem () : ASTLoweringBase (), translated (nullptr) {} + + HIR::WhereClauseItem *translated; +}; + } // namespace HIR } // namespace Rust -- cgit v1.1 From 1a8479839e944fa2a8abd5923b4b65ce6cc96401 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 18:15:10 +0100 Subject: Remove assertion for has_where_clause This assertion doesn't protect against bad pointers or behaviour, when we access the where clause it is simply a wrapper over a vector of where clause items so we can safely iterate even when the where clause item vector is empty. --- gcc/rust/hir/tree/rust-hir-item.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'gcc/rust/hir') diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index de067e6..4fdd764 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -340,6 +340,15 @@ public: bool is_empty () const { return where_clause_items.empty (); } std::string as_string () const; + + std::vector> &get_items () + { + return where_clause_items; + } + const std::vector> &get_items () const + { + return where_clause_items; + } }; // A self parameter in a method @@ -1205,11 +1214,7 @@ public: Identifier get_function_name () const { return function_name; } // TODO: is this better? Or is a "vis_block" better? - WhereClause &get_where_clause () - { - rust_assert (has_where_clause ()); - return where_clause; - } + WhereClause &get_where_clause () { return where_clause; } bool has_return_type () const { return return_type != nullptr; } @@ -1322,11 +1327,7 @@ public: return generic_params; } - WhereClause &get_where_clause () - { - rust_assert (has_where_clause ()); - return where_clause; - } + WhereClause &get_where_clause () { return where_clause; } std::unique_ptr &get_type_aliased () { -- cgit v1.1 From 4c873251e79980a86da81df4d7180e757c472ce9 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 18:15:33 +0100 Subject: Add support for higher ranked trait bounds This adds the type checking for the bounds which will add the relevant bounds to the associated types and perform the relevant type checking required. Fixes #442 --- gcc/rust/hir/tree/rust-hir-item.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'gcc/rust/hir') diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 4fdd764..21f0781 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -1387,6 +1387,8 @@ public: return generic_params; } + WhereClause &get_where_clause () { return where_clause; } + protected: Struct (Analysis::NodeMapping mappings, Identifier struct_name, std::vector> generic_params, @@ -1994,6 +1996,8 @@ public: } } + WhereClause &get_where_clause () { return where_clause; } + protected: /* Use covariance to implement clone function as returning this object * rather than base */ @@ -2704,6 +2708,8 @@ public: return trait_ref; } + WhereClause &get_where_clause () { return where_clause; } + protected: ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); } }; -- cgit v1.1