public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] const folding in gccrs: remove ConstCtx class.
@ 2022-06-08 12:50 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:50 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:1a04c501e41ec0dee74d520a7ca4373cb7df7a48

commit 1a04c501e41ec0dee74d520a7ca4373cb7df7a48
Author: Faisal Abbas <90.abbasfaisal@gmail.com>
Date:   Thu May 26 09:07:22 2022 +0100

    const folding in gccrs: remove ConstCtx class.
    
    This class has potential to hinder porting further const folding code from C++.
    So this edit makes it easy to copy code from constexpr.cc to rust-constexpr.cc
    and so on.
    
    Signed-off-by: Faisal Abbas <90.abbasfaisal@gmail.com>

Diff:
---
 gcc/rust/backend/rust-compile-base.cc         |  4 +-
 gcc/rust/backend/rust-compile-expr.cc         |  2 +-
 gcc/rust/backend/rust-compile-expr.h          |  2 +-
 gcc/rust/backend/rust-compile-pattern.cc      |  6 +-
 gcc/rust/backend/rust-compile-resolve-path.cc |  2 +-
 gcc/rust/backend/rust-compile-type.cc         |  2 +-
 gcc/rust/backend/rust-constexpr.cc            | 86 ++++++++++++++++++---------
 gcc/rust/backend/rust-constexpr.h             | 17 +-----
 8 files changed, 67 insertions(+), 54 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 7b0a375439c..3de80d99532 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -555,7 +555,7 @@ HIRCompileBase::compile_constant_item (
   if (!is_block_expr)
     {
       tree value = CompileExpr::Compile (const_value_expr, ctx);
-      folded_expr = ConstCtx::fold (value);
+      folded_expr = fold_expr (value);
     }
   else
     {
@@ -605,7 +605,7 @@ HIRCompileBase::compile_constant_item (
       // lets fold it into a call expr
       tree call = build_call_array_loc (locus.gcc_location (), const_type,
 					fndecl, 0, NULL);
-      folded_expr = ConstCtx::fold (call);
+      folded_expr = fold_expr (call);
     }
 
   return named_constant_expression (const_type, ident, folded_expr, locus);
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index b176ed2cf36..77819cf40bc 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -449,7 +449,7 @@ CompileExpr::visit (HIR::CallExpr &expr)
 	{
 	  HIR::Expr *discrim_expr = variant->get_discriminant ();
 	  tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
-	  tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node);
+	  tree folded_discrim_expr = fold_expr (discrim_expr_node);
 	  tree qualifier = folded_discrim_expr;
 
 	  ctor_arguments.push_back (qualifier);
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 655ffbb263e..593e1f978fe 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -492,7 +492,7 @@ public:
       {
 	HIR::Expr *discrim_expr = variant->get_discriminant ();
 	tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
-	tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node);
+	tree folded_discrim_expr = fold_expr (discrim_expr_node);
 	tree qualifier = folded_discrim_expr;
 
 	ctor_arguments.push_back (qualifier);
diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc
index aefa4eb08bb..2f3449a1eb7 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -52,7 +52,7 @@ CompilePatternCaseLabelExpr::visit (HIR::PathInExpression &pattern)
 
   HIR::Expr *discrim_expr = variant->get_discriminant ();
   tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
-  tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node);
+  tree folded_discrim_expr = fold_expr (discrim_expr_node);
   tree case_low = folded_discrim_expr;
 
   case_label_expr
@@ -132,7 +132,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound,
 	result = ResolvePathRef::Compile (ref.get_path (), ctx);
 
 	// If the path resolves to a const expression, fold it.
-	result = ConstCtx::fold (result);
+	result = fold_expr (result);
       }
       break;
 
@@ -143,7 +143,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound,
 	result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx);
 
 	// If the path resolves to a const expression, fold it.
-	result = ConstCtx::fold (result);
+	result = fold_expr (result);
       }
     }
 
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index ca96a277ea8..44239122379 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -99,7 +99,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
       // make the ctor for the union
       HIR::Expr *discrim_expr = variant->get_discriminant ();
       tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
-      tree folded_discrim_expr = ConstCtx::fold (discrim_expr_node);
+      tree folded_discrim_expr = fold_expr (discrim_expr_node);
       tree qualifier = folded_discrim_expr;
 
       return ctx->get_backend ()->constructor_expression (compiled_adt_type,
diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc
index 707b2afcbe3..84e5b9511d4 100644
--- a/gcc/rust/backend/rust-compile-type.cc
+++ b/gcc/rust/backend/rust-compile-type.cc
@@ -371,7 +371,7 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
   tree element_type
     = TyTyResolveCompile::compile (ctx, type.get_element_type ());
   tree capacity_expr = CompileExpr::Compile (&type.get_capacity_expr (), ctx);
-  tree folded_capacity_expr = ConstCtx::fold (capacity_expr);
+  tree folded_capacity_expr = fold_expr (capacity_expr);
 
   translated
     = ctx->get_backend ()->array_type (element_type, folded_capacity_expr);
diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc
index aee41e43c0a..f77fb3a8e6d 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -29,6 +29,18 @@
 namespace Rust {
 namespace Compile {
 
+struct constexpr_global_ctx
+{
+  HOST_WIDE_INT constexpr_ops_count;
+
+  constexpr_global_ctx () : constexpr_ops_count (0) {}
+};
+
+struct constexpr_ctx
+{
+  constexpr_global_ctx *global;
+};
+
 static tree
 constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
 		  bool unshare_p);
@@ -39,22 +51,39 @@ static void
 non_const_var_error (location_t loc, tree r);
 
 static tree
-get_function_named_in_call (tree t);
+constexpr_expression (const constexpr_ctx *ctx, tree);
 
-ConstCtx::ConstCtx () : constexpr_ops_count (0) {}
+static tree
+constexpr_fn_retval (const constexpr_ctx *ctx, tree r);
+
+static tree
+eval_store_expression (const constexpr_ctx *ctx, tree r);
+
+static tree
+eval_call_expression (const constexpr_ctx *ctx, tree r);
+
+static tree
+eval_binary_expression (const constexpr_ctx *ctx, tree r);
+
+static tree
+get_function_named_in_call (tree t);
 
 tree
-ConstCtx::fold (tree expr)
+fold_expr (tree expr)
 {
-  tree folded = ConstCtx ().constexpr_expression (expr);
+  constexpr_global_ctx global_ctx;
+  constexpr_ctx ctx = {&global_ctx};
+
+  tree folded = constexpr_expression (&ctx, expr);
   rust_assert (folded != NULL_TREE);
   return folded;
 }
 
-tree
-ConstCtx::constexpr_expression (tree t)
+static tree
+constexpr_expression (const constexpr_ctx *ctx, tree t)
 {
   location_t loc = EXPR_LOCATION (t);
+
   if (CONSTANT_CLASS_P (t))
     {
       if (TREE_OVERFLOW (t))
@@ -67,7 +96,7 @@ ConstCtx::constexpr_expression (tree t)
     }
 
   // Avoid excessively long constexpr evaluations
-  if (++constexpr_ops_count >= constexpr_ops_limit)
+  if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
     {
       rust_error_at (
 	Location (loc),
@@ -136,20 +165,20 @@ ConstCtx::constexpr_expression (tree t)
     case LTGT_EXPR:
     case RANGE_EXPR:
     case COMPLEX_EXPR:
-      r = eval_binary_expression (t);
+      r = eval_binary_expression (ctx, t);
       break;
 
     case CALL_EXPR:
-      r = eval_call_expression (t);
+      r = eval_call_expression (ctx, t);
       break;
 
     case RETURN_EXPR:
       rust_assert (TREE_OPERAND (t, 0) != NULL_TREE);
-      r = constexpr_expression (TREE_OPERAND (t, 0));
+      r = constexpr_expression (ctx, TREE_OPERAND (t, 0));
       break;
 
     case MODIFY_EXPR:
-      r = eval_store_expression (t);
+      r = eval_store_expression (ctx, t);
       break;
 
     default:
@@ -159,8 +188,8 @@ ConstCtx::constexpr_expression (tree t)
   return r;
 }
 
-tree
-ConstCtx::eval_store_expression (tree t)
+static tree
+eval_store_expression (const constexpr_ctx *ctx, tree t)
 {
   tree init = TREE_OPERAND (t, 1);
   if (TREE_CLOBBER_P (init))
@@ -176,7 +205,7 @@ ConstCtx::eval_store_expression (tree t)
     {
       /* Evaluate the value to be stored without knowing what object it will be
 	 stored in, so that any side-effects happen first.  */
-      init = ConstCtx::fold (init);
+      init = fold_expr (init);
     }
 
   bool evaluated = false;
@@ -190,7 +219,7 @@ ConstCtx::eval_store_expression (tree t)
 	    object = probe;
 	  else
 	    {
-	      probe = constexpr_expression (probe);
+	      probe = constexpr_expression (ctx, probe);
 	      evaluated = true;
 	    }
 	  break;
@@ -202,16 +231,15 @@ ConstCtx::eval_store_expression (tree t)
 
 /* Subroutine of cxx_eval_constant_expression.
  Like cxx_eval_unary_expression, except for binary expressions.  */
-
-tree
-ConstCtx::eval_binary_expression (tree t)
+static tree
+eval_binary_expression (const constexpr_ctx *ctx, tree t)
 {
   tree orig_lhs = TREE_OPERAND (t, 0);
   tree orig_rhs = TREE_OPERAND (t, 1);
   tree lhs, rhs;
 
-  lhs = constexpr_expression (orig_lhs);
-  rhs = constexpr_expression (orig_rhs);
+  lhs = constexpr_expression (ctx, orig_lhs);
+  rhs = constexpr_expression (ctx, orig_rhs);
 
   location_t loc = EXPR_LOCATION (t);
   enum tree_code code = TREE_CODE (t);
@@ -223,19 +251,19 @@ ConstCtx::eval_binary_expression (tree t)
 // Subroutine of cxx_eval_constant_expression.
 // Evaluate the call expression tree T in the context of OLD_CALL expression
 // evaluation.
-tree
-ConstCtx::eval_call_expression (tree t)
+static tree
+eval_call_expression (const constexpr_ctx *ctx, tree t)
 {
   tree fun = get_function_named_in_call (t);
-  return constexpr_fn_retval (DECL_SAVED_TREE (fun));
+  return constexpr_fn_retval (ctx, DECL_SAVED_TREE (fun));
 }
 
 // Subroutine of check_constexpr_fundef.  BODY is the body of a function
 // declared to be constexpr, or a sub-statement thereof.  Returns the
 // return value if suitable, error_mark_node for a statement not allowed in
 // a constexpr function, or NULL_TREE if no return value was found.
-tree
-ConstCtx::constexpr_fn_retval (tree body)
+static tree
+constexpr_fn_retval (const constexpr_ctx *ctx, tree body)
 {
   switch (TREE_CODE (body))
     {
@@ -243,7 +271,7 @@ ConstCtx::constexpr_fn_retval (tree body)
 	tree expr = NULL_TREE;
 	for (tree stmt : tsi_range (body))
 	  {
-	    tree s = constexpr_fn_retval (stmt);
+	    tree s = constexpr_fn_retval (ctx, stmt);
 	    if (s == error_mark_node)
 	      return error_mark_node;
 	    else if (s == NULL_TREE)
@@ -258,7 +286,7 @@ ConstCtx::constexpr_fn_retval (tree body)
       }
 
     case RETURN_EXPR:
-      return constexpr_expression (body);
+      return constexpr_expression (ctx, body);
 
       case DECL_EXPR: {
 	tree decl = DECL_EXPR_DECL (body);
@@ -270,11 +298,11 @@ ConstCtx::constexpr_fn_retval (tree body)
       }
 
     case CLEANUP_POINT_EXPR:
-      return constexpr_fn_retval (TREE_OPERAND (body, 0));
+      return constexpr_fn_retval (ctx, TREE_OPERAND (body, 0));
 
       case BIND_EXPR: {
 	tree b = BIND_EXPR_BODY (body);
-	return constexpr_fn_retval (b);
+	return constexpr_fn_retval (ctx, b);
       }
       break;
 
diff --git a/gcc/rust/backend/rust-constexpr.h b/gcc/rust/backend/rust-constexpr.h
index 40d9159ccd1..3cfcec817a9 100644
--- a/gcc/rust/backend/rust-constexpr.h
+++ b/gcc/rust/backend/rust-constexpr.h
@@ -23,22 +23,7 @@
 namespace Rust {
 namespace Compile {
 
-class ConstCtx
-{
-public:
-  static tree fold (tree);
-
-  tree constexpr_expression (tree);
-  tree eval_binary_expression (tree);
-  tree eval_call_expression (tree);
-  tree constexpr_fn_retval (tree);
-  tree eval_store_expression (tree);
-
-private:
-  ConstCtx ();
-
-  HOST_WIDE_INT constexpr_ops_count;
-};
+extern tree fold_expr (tree);
 
 } // namespace Compile
 } // namespace Rust


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:50 [gcc/devel/rust/master] const folding in gccrs: remove ConstCtx class Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).