[PATCH] Remove lambda iterators in various HIR classes

2021-10-08 Thread David Faust via Gcc-rust
This patch removes the lambda iterators used in various HIR objects.
These iterators make interacting with the IR for static analysis more
difficult. Instead, get_X () helpers are added for accessing elements,
and uses of the iterators replaced with for loops.

The following objects are adjusted in this patch:
- HIR::TupleExpr
- HIR::StructExprField
- HIR::StructStruct
- HIR::TupleStruct

Fixes: #704, #705, #706, #707
---
 gcc/rust/backend/rust-compile-expr.h  | 20 -
 gcc/rust/hir/tree/rust-hir-expr.h | 32 +
 gcc/rust/hir/tree/rust-hir-item.h | 18 +---
 gcc/rust/lint/rust-lint-marklive.h| 24 +-
 gcc/rust/lint/rust-lint-scan-deadcode.h   | 20 -
 gcc/rust/typecheck/rust-hir-type-check-expr.h |  9 ++--
 gcc/rust/typecheck/rust-hir-type-check-stmt.h | 44 +-
 .../typecheck/rust-hir-type-check-toplevel.h  | 45 ++-
 gcc/rust/typecheck/rust-hir-type-check.cc | 33 +++---
 gcc/rust/typecheck/rust-tycheck-dump.h| 10 ++---
 10 files changed, 107 insertions(+), 148 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.h 
b/gcc/rust/backend/rust-compile-expr.h
index eb245dce5be..7c4046680e9 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -412,11 +412,11 @@ public:
 
   void visit (HIR::ArrayElemsValues &elems) override
   {
-elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
-  Bexpression *translated_expr = CompileExpr::Compile (e, ctx);
-  constructor.push_back (translated_expr);
-  return true;
-});
+for (auto &elem : elems.get_values ())
+  {
+   Bexpression *translated_expr = CompileExpr::Compile (elem.get (), ctx);
+   constructor.push_back (translated_expr);
+  }
   }
 
   void visit (HIR::ArrayElemsCopied &elems) override
@@ -646,11 +646,11 @@ public:
 // this assumes all fields are in order from type resolution and if a base
 // struct was specified those fields are filed via accesors
 std::vector vals;
-struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
-  Bexpression *expr = CompileStructExprField::Compile (field, ctx);
-  vals.push_back (expr);
-  return true;
-});
+for (auto &field : struct_expr.get_fields ())
+  {
+   Bexpression *expr = CompileStructExprField::Compile (field.get (), ctx);
+   vals.push_back (expr);
+  }
 
 translated
   = ctx->get_backend ()->constructor_expression (type, vals,
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index 05bc1f9f055..d9958a153be 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -796,14 +796,7 @@ public:
 
   size_t get_num_elements () const { return values.size (); }
 
-  void iterate (std::function cb)
-  {
-for (auto it = values.begin (); it != values.end (); it++)
-  {
-   if (!cb ((*it).get ()))
- return;
-  }
-  }
+  std::vector>& get_values () { return values; }
 
 protected:
   ArrayElemsValues *clone_array_elems_impl () const override
@@ -1070,15 +1063,6 @@ public:
 
   bool is_unit () const { return tuple_elems.size () == 0; }
 
-  void iterate (std::function cb)
-  {
-for (auto &tuple_elem : tuple_elems)
-  {
-   if (!cb (tuple_elem.get ()))
- return;
-  }
-  }
-
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -1491,15 +1475,6 @@ public:
 
   void accept_vis (HIRVisitor &vis) override;
 
-  void iterate (std::function cb)
-  {
-for (auto &field : fields)
-  {
-   if (!cb (field.get ()))
- return;
-  }
-  }
-
   std::vector > &get_fields ()
   {
 return fields;
@@ -1510,11 +1485,6 @@ public:
 return fields;
   };
 
-  std::vector > get_fields_as_owner ()
-  {
-return std::move (fields);
-  };
-
   void set_fields_as_owner (
 std::vector > new_fields)
   {
diff --git a/gcc/rust/hir/tree/rust-hir-item.h 
b/gcc/rust/hir/tree/rust-hir-item.h
index 8cd7a01b2e8..f5119c3dc77 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -1492,14 +1492,7 @@ public:
 
   void accept_vis (HIRVisitor &vis) override;
 
-  void iterate (std::function cb)
-  {
-for (auto &field : fields)
-  {
-   if (!cb (field))
- return;
-  }
-  }
+  std::vector& get_fields() { return fields; }
 
 protected:
   /* Use covariance to implement clone function as returning this object
@@ -1610,15 +1603,6 @@ public:
   std::vector &get_fields () { return fields; }
   const std::vector &get_fields () const { return fields; }
 
-  void iterate (std::function cb)
-  {
-for (auto &field : fields)
-  {
-   if (!cb (field))
- return;
-  }
-  }
-
 protected:
   /* Use covariance to implement clone function as returning this object
* rather than base */
diff --git a/gcc/rust/li

Re: [PATCH] Remove lambda iterators in various HIR classes

2021-10-08 Thread David Faust via Gcc-rust




On 10/8/21 10:45, David Faust via Gcc-rust wrote:

This patch removes the lambda iterators used in various HIR objects.
These iterators make interacting with the IR for static analysis more
difficult. Instead, get_X () helpers are added for accessing elements,
and uses of the iterators replaced with for loops.

The following objects are adjusted in this patch:
- HIR::TupleExpr
- HIR::StructExprField
- HIR::StructStruct
- HIR::TupleStruct

Fixes: #704, #705, #706, #707


This also adjusts HIR::ArrayElemsValues, fixing #703.
Must have lost those lines in the patch prep, sorry.


---
  gcc/rust/backend/rust-compile-expr.h  | 20 -
  gcc/rust/hir/tree/rust-hir-expr.h | 32 +
  gcc/rust/hir/tree/rust-hir-item.h | 18 +---
  gcc/rust/lint/rust-lint-marklive.h| 24 +-
  gcc/rust/lint/rust-lint-scan-deadcode.h   | 20 -
  gcc/rust/typecheck/rust-hir-type-check-expr.h |  9 ++--
  gcc/rust/typecheck/rust-hir-type-check-stmt.h | 44 +-
  .../typecheck/rust-hir-type-check-toplevel.h  | 45 ++-
  gcc/rust/typecheck/rust-hir-type-check.cc | 33 +++---
  gcc/rust/typecheck/rust-tycheck-dump.h| 10 ++---
  10 files changed, 107 insertions(+), 148 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.h 
b/gcc/rust/backend/rust-compile-expr.h
index eb245dce5be..7c4046680e9 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -412,11 +412,11 @@ public:
  
void visit (HIR::ArrayElemsValues &elems) override

{
-elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
-  Bexpression *translated_expr = CompileExpr::Compile (e, ctx);
-  constructor.push_back (translated_expr);
-  return true;
-});
+for (auto &elem : elems.get_values ())
+  {
+   Bexpression *translated_expr = CompileExpr::Compile (elem.get (), ctx);
+   constructor.push_back (translated_expr);
+  }
}
  
void visit (HIR::ArrayElemsCopied &elems) override

@@ -646,11 +646,11 @@ public:
  // this assumes all fields are in order from type resolution and if a base
  // struct was specified those fields are filed via accesors
  std::vector vals;
-struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
-  Bexpression *expr = CompileStructExprField::Compile (field, ctx);
-  vals.push_back (expr);
-  return true;
-});
+for (auto &field : struct_expr.get_fields ())
+  {
+   Bexpression *expr = CompileStructExprField::Compile (field.get (), ctx);
+   vals.push_back (expr);
+  }
  
  translated

= ctx->get_backend ()->constructor_expression (type, vals,
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index 05bc1f9f055..d9958a153be 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -796,14 +796,7 @@ public:
  
size_t get_num_elements () const { return values.size (); }
  
-  void iterate (std::function cb)

-  {
-for (auto it = values.begin (); it != values.end (); it++)
-  {
-   if (!cb ((*it).get ()))
- return;
-  }
-  }
+  std::vector>& get_values () { return values; }
  
  protected:

ArrayElemsValues *clone_array_elems_impl () const override
@@ -1070,15 +1063,6 @@ public:
  
bool is_unit () const { return tuple_elems.size () == 0; }
  
-  void iterate (std::function cb)

-  {
-for (auto &tuple_elem : tuple_elems)
-  {
-   if (!cb (tuple_elem.get ()))
- return;
-  }
-  }
-
  protected:
/* Use covariance to implement clone function as returning this object 
rather
 * than base */
@@ -1491,15 +1475,6 @@ public:
  
void accept_vis (HIRVisitor &vis) override;
  
-  void iterate (std::function cb)

-  {
-for (auto &field : fields)
-  {
-   if (!cb (field.get ()))
- return;
-  }
-  }
-
std::vector > &get_fields ()
{
  return fields;
@@ -1510,11 +1485,6 @@ public:
  return fields;
};
  
-  std::vector > get_fields_as_owner ()

-  {
-return std::move (fields);
-  };
-
void set_fields_as_owner (
  std::vector > new_fields)
{
diff --git a/gcc/rust/hir/tree/rust-hir-item.h 
b/gcc/rust/hir/tree/rust-hir-item.h
index 8cd7a01b2e8..f5119c3dc77 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -1492,14 +1492,7 @@ public:
  
void accept_vis (HIRVisitor &vis) override;
  
-  void iterate (std::function cb)

-  {
-for (auto &field : fields)
-  {
-   if (!cb (field))
- return;
-  }
-  }
+  std::vector& get_fields() { return fields; }
  
  protected:

/* Use covariance to implement clone function as returning this object
@@ -1610,15 +1603,6 @@ public:
std::vector &get_fields () { return fields; }
const std: