From b0e9cea1ae57f895063189e2e3f8a9e664d4ca35 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 25 Oct 2021 15:40:07 +0100 Subject: Add missing NodeId to where clause items --- gcc/rust/ast/rust-item.h | 50 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index d075a57..a5d9a0a 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -177,6 +177,8 @@ public: virtual void accept_vis (ASTVisitor &vis) = 0; + virtual NodeId get_node_id () const = 0; + protected: // Clone function implementation as pure virtual method virtual WhereClauseItem *clone_where_clause_item_impl () const = 0; @@ -187,23 +189,29 @@ class LifetimeWhereClauseItem : public WhereClauseItem { Lifetime lifetime; - // LifetimeBounds lifetime_bounds; - std::vector lifetime_bounds; // inlined lifetime bounds - + std::vector lifetime_bounds; Location locus; + NodeId node_id; public: LifetimeWhereClauseItem (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), + node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} std::string as_string () const override; void accept_vis (ASTVisitor &vis) override; + NodeId get_node_id () const override final { return node_id; } + + Lifetime &get_lifetime () { return lifetime; } + + std::vector &get_lifetime_bounds () { return lifetime_bounds; } + protected: // Clone function implementation as (not pure) virtual method LifetimeWhereClauseItem *clone_where_clause_item_impl () const override @@ -215,18 +223,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; + NodeId node_id; public: // Returns whether the item has ForLifetimes @@ -240,7 +240,8 @@ public: 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)), + node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} // Copy constructor requires clone @@ -248,6 +249,7 @@ public: : for_lifetimes (other.for_lifetimes), bound_type (other.bound_type->clone_type ()) { + node_id = other.node_id; 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 ()); @@ -256,9 +258,9 @@ public: // Overload assignment operator to clone TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other) { + node_id = other.node_id; 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 ()); @@ -275,7 +277,6 @@ public: void accept_vis (ASTVisitor &vis) override; - // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_type () { rust_assert (bound_type != nullptr); @@ -287,12 +288,15 @@ public: { return type_param_bounds; } + const std::vector> & get_type_param_bounds () const { return type_param_bounds; } + NodeId get_node_id () const override final { return node_id; } + protected: // Clone function implementation as (not pure) virtual method TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override @@ -306,17 +310,18 @@ struct WhereClause { private: std::vector> where_clause_items; - - // should this store location info? + NodeId node_id; public: WhereClause (std::vector> where_clause_items) - : where_clause_items (std::move (where_clause_items)) + : where_clause_items (std::move (where_clause_items)), + node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} // copy constructor with vector clone WhereClause (WhereClause const &other) { + node_id = other.node_id; where_clause_items.reserve (other.where_clause_items.size ()); for (const auto &e : other.where_clause_items) where_clause_items.push_back (e->clone_where_clause_item ()); @@ -325,6 +330,7 @@ public: // overloaded assignment operator with vector clone WhereClause &operator= (WhereClause const &other) { + node_id = other.node_id; where_clause_items.reserve (other.where_clause_items.size ()); for (const auto &e : other.where_clause_items) where_clause_items.push_back (e->clone_where_clause_item ()); @@ -347,6 +353,8 @@ public: std::string as_string () const; + NodeId get_node_id () const { return node_id; } + // TODO: this mutable getter seems kinda dodgy std::vector> &get_items () { -- 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/ast/rust-item.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index a5d9a0a..fb7cc17 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -188,7 +188,6 @@ protected: class LifetimeWhereClauseItem : public WhereClauseItem { Lifetime lifetime; - std::vector lifetime_bounds; Location locus; NodeId node_id; @@ -212,6 +211,8 @@ public: std::vector &get_lifetime_bounds () { return lifetime_bounds; } + Location get_locus () const { return locus; } + protected: // Clone function implementation as (not pure) virtual method LifetimeWhereClauseItem *clone_where_clause_item_impl () const override -- 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/ast/rust-item.h | 66 ++++++++---------------------------------------- 1 file changed, 11 insertions(+), 55 deletions(-) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index fb7cc17..323548a 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -887,11 +887,7 @@ public: } // 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; } Identifier get_method_name () const { return method_name; } @@ -1587,11 +1583,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; } // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_return_type () @@ -1719,11 +1711,7 @@ public: } // 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; } // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_type_aliased () @@ -1789,11 +1777,7 @@ public: } // 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; } Identifier get_identifier () const { return struct_name; } @@ -2410,11 +2394,7 @@ public: } // 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; } protected: /* Use covariance to implement clone function as returning this object @@ -2520,11 +2500,7 @@ public: } // 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; } Identifier get_identifier () const { return union_name; } @@ -2877,11 +2853,7 @@ public: } // 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; } }; // Actual trait item function declaration within traits @@ -3104,11 +3076,7 @@ public: } // 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; } SelfParam &get_self_param () { return self_param; } const SelfParam &get_self_param () const { return self_param; } @@ -3542,11 +3510,7 @@ public: return type_param_bounds; } - WhereClause &get_where_clause () - { - rust_assert (has_where_clause ()); - return where_clause; - } + WhereClause &get_where_clause () { return where_clause; } void insert_implict_self (std::unique_ptr &¶m) { @@ -3619,11 +3583,7 @@ public: } // 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; } // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_type () @@ -4270,11 +4230,7 @@ public: } // 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; } // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_return_type () -- cgit v1.1