public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] rust-constexpr.cc: port over cxx_eval_unary_expression
@ 2022-08-29 15:33 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-08-29 15:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:23bde7d7c663302c803f0a7e4324630eae825008

commit 23bde7d7c663302c803f0a7e4324630eae825008
Author: Faisal Abbas <90.abbasfaisal@gmail.com>
Date:   Fri Jul 29 05:53:09 2022 +0100

    rust-constexpr.cc: port over cxx_eval_unary_expression

Diff:
---
 gcc/rust/backend/rust-constexpr.cc | 62 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc
index 127451e5966..6ef601d66c2 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -501,6 +501,10 @@ static tree
 eval_switch_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
 		  bool *overflow_p, tree *jump_target);
 
+static tree
+eval_unary_expression (const constexpr_ctx *ctx, tree t, bool /*lval*/,
+		       bool *non_constant_p, bool *overflow_p);
+
 /* Variables and functions to manage constexpr call expansion context.
    These do not need to be marked for PCH or GC.  */
 
@@ -739,6 +743,33 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval,
 	}
       break;
 
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+      if (lval)
+	{
+	  r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
+					non_constant_p, overflow_p);
+	  if (r == error_mark_node)
+	    ;
+	  else if (r == TREE_OPERAND (t, 0))
+	    r = t;
+	  else
+	    r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
+	  break;
+	}
+      /* FALLTHRU */
+    case CONJ_EXPR:
+    case FIX_TRUNC_EXPR:
+    case FLOAT_EXPR:
+    case NEGATE_EXPR:
+    case ABS_EXPR:
+    case ABSU_EXPR:
+    case BIT_NOT_EXPR:
+    case TRUTH_NOT_EXPR:
+    case FIXED_CONVERT_EXPR:
+      r = eval_unary_expression (ctx, t, lval, non_constant_p, overflow_p);
+      break;
+
     case LOOP_EXPR:
     case WHILE_STMT:
     case FOR_STMT:
@@ -2970,6 +3001,37 @@ eval_switch_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
   return NULL_TREE;
 }
 
+// forked from gcc/cp/constexpr.cc eval_unary_expression
+
+/* Subroutine of cxx_eval_constant_expression.
+   Attempt to reduce the unary expression tree T to a compile time value.
+   If successful, return the value.  Otherwise issue a diagnostic
+   and return error_mark_node.  */
+
+static tree
+eval_unary_expression (const constexpr_ctx *ctx, tree t, bool /*lval*/,
+		       bool *non_constant_p, bool *overflow_p)
+{
+  tree r;
+  tree orig_arg = TREE_OPERAND (t, 0);
+  tree arg = eval_constant_expression (ctx, orig_arg, /*lval*/ false,
+				       non_constant_p, overflow_p);
+  VERIFY_CONSTANT (arg);
+  location_t loc = EXPR_LOCATION (t);
+  enum tree_code code = TREE_CODE (t);
+  tree type = TREE_TYPE (t);
+  r = fold_unary_loc (loc, code, type, arg);
+  if (r == NULL_TREE)
+    {
+      if (arg == orig_arg)
+	r = t;
+      else
+	r = build1_loc (loc, code, type, arg);
+    }
+  VERIFY_CONSTANT (r);
+  return r;
+}
+
 // #include "gt-rust-rust-constexpr.h"
 
 } // namespace Compile

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

only message in thread, other threads:[~2022-08-29 15:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-29 15:33 [gcc/devel/rust/master] rust-constexpr.cc: port over cxx_eval_unary_expression 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).