diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-10-27 15:12:05 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-27 15:12:05 +0000 |
commit | e28c43f2f40cf405e89b3892aa65f6a06fa1c802 (patch) | |
tree | f7503c89dffd0fc84e1560525c5e219cefc79d78 /gcc/rust/hir | |
parent | 161a690d21044f5211ad2b55ae6b06f91bbd9106 (diff) | |
parent | 7604b6430cf3472399e5f24b7d8478a8ff89b22b (diff) | |
download | gcc-e28c43f2f40cf405e89b3892aa65f6a06fa1c802.zip gcc-e28c43f2f40cf405e89b3892aa65f6a06fa1c802.tar.gz gcc-e28c43f2f40cf405e89b3892aa65f6a06fa1c802.tar.bz2 |
Merge #771
771: Add higher ranked trait bounds r=philberty a=philberty
This adds support for where clauses and higher ranked trait bounds more test cases are needed
since where clauses are supported on associated types, impls, ADT's etc. See the individual
commits for more detail on implementation.
Fixes #773 #442
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/hir')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 72 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-type.h | 72 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 106 |
3 files changed, 213 insertions, 37 deletions
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<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -134,6 +142,14 @@ public: } std::vector<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -186,6 +202,14 @@ public: } std::vector<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -242,6 +266,14 @@ public: } std::vector<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (i)); + } + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = HIR::Visibility::create_public (); @@ -282,6 +314,13 @@ public: } std::vector<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (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<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (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<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (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<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (i)); + } HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::Visibility vis = HIR::Visibility::create_public (); std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; @@ -632,7 +692,13 @@ public: void visit (AST::TraitImpl &impl_block) override { std::vector<std::unique_ptr<HIR::WhereClauseItem>> 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<HIR::WhereClauseItem> (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<std::unique_ptr<HIR::TypePathSegment> > translated_segments; + std::vector<std::unique_ptr<HIR::TypePathSegment>> 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<std::unique_ptr<HIR::Type> > elems; + std::vector<std::unique_ptr<HIR::Type>> 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<std::unique_ptr<HIR::TypeParamBound> > type_param_bounds; + std::vector<std::unique_ptr<HIR::TypeParamBound>> 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<HIR::Lifetime> 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<HIR::LifetimeParam> for_lifetimes; + + std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> ( + ASTLoweringType::translate (item.get_type ().get ())); + + std::vector<std::unique_ptr<HIR::TypeParamBound>> 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<HIR::TypeParamBound> (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 diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index ab9eab6..21f0781 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" @@ -140,6 +141,12 @@ protected: class WhereClauseItem { public: + enum ItemType + { + LIFETIME, + TYPE_BOUND, + }; + virtual ~WhereClauseItem () {} // Unique pointer custom clone function @@ -152,6 +159,10 @@ public: virtual void accept_vis (HIRVisitor &vis) = 0; + 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; @@ -161,24 +172,37 @@ protected: class LifetimeWhereClauseItem : public WhereClauseItem { Lifetime lifetime; - - // LifetimeBounds lifetime_bounds; - std::vector<Lifetime> lifetime_bounds; // inlined lifetime bounds - + std::vector<Lifetime> lifetime_bounds; Location locus; + Analysis::NodeMapping mappings; public: - LifetimeWhereClauseItem (Lifetime lifetime, + LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime, std::vector<Lifetime> 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<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; } + + Analysis::NodeMapping get_mappings () const override final + { + 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 @@ -190,18 +214,10 @@ protected: // A type bound where clause item class TypeBoundWhereClauseItem : public WhereClauseItem { - // bool has_for_lifetimes; - // LifetimeParams for_lifetimes; - std::vector<LifetimeParam> for_lifetimes; // inlined - + std::vector<LifetimeParam> for_lifetimes; std::unique_ptr<Type> bound_type; - - // bool has_type_param_bounds; - // TypeParamBounds type_param_bounds; - std::vector<std::unique_ptr<TypeParamBound>> - type_param_bounds; // inlined form - - // should this store location info? + std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; + Analysis::NodeMapping mappings; public: // Returns whether the item has ForLifetimes @@ -211,17 +227,19 @@ public: bool has_type_param_bounds () const { return !type_param_bounds.empty (); } TypeBoundWhereClauseItem ( - std::vector<LifetimeParam> for_lifetimes, std::unique_ptr<Type> bound_type, + Analysis::NodeMapping mappings, std::vector<LifetimeParam> for_lifetimes, + std::unique_ptr<Type> bound_type, std::vector<std::unique_ptr<TypeParamBound>> 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 +249,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 +268,25 @@ public: void accept_vis (HIRVisitor &vis) override; + std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; } + + std::unique_ptr<Type> &get_bound_type () { return bound_type; } + + std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds () + { + return type_param_bounds; + } + + Analysis::NodeMapping get_mappings () const override final + { + 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 @@ -303,6 +340,15 @@ public: bool is_empty () const { return where_clause_items.empty (); } std::string as_string () const; + + std::vector<std::unique_ptr<WhereClauseItem>> &get_items () + { + return where_clause_items; + } + const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const + { + return where_clause_items; + } }; // A self parameter in a method @@ -1168,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; } @@ -1285,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<Type> &get_type_aliased () { @@ -1349,6 +1387,8 @@ public: return generic_params; } + WhereClause &get_where_clause () { return where_clause; } + protected: Struct (Analysis::NodeMapping mappings, Identifier struct_name, std::vector<std::unique_ptr<GenericParam>> generic_params, @@ -1956,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 */ @@ -2666,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); } }; |