[clang] [clang][Interp] Support __real/__imag on primitives (PR #75485)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 18 06:16:24 PST 2023


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/75485

>From e624652182dc0d76712f83a1c876ec856a8f38ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 14 Dec 2023 16:50:22 +0100
Subject: [PATCH] [clang][Interp] Support __real/__imag on primitives

---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp | 9 +++++++--
 clang/test/AST/Interp/complex.cpp        | 6 ++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index d0980882f402b9..eb70e10747ea8b 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2761,7 +2761,8 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
       return false;
     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
   case UO_Real: { // __real x
-    assert(!T);
+    if (T)
+      return this->delegate(SubExpr);
     if (!this->visit(SubExpr))
       return false;
     if (!this->emitConstUint8(0, E))
@@ -2776,7 +2777,11 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
     return true;
   }
   case UO_Imag: { // __imag x
-    assert(!T);
+    if (T) {
+      if (!this->discard(SubExpr))
+        return false;
+      return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
+    }
     if (!this->visit(SubExpr))
       return false;
     if (!this->emitConstUint8(1, E))
diff --git a/clang/test/AST/Interp/complex.cpp b/clang/test/AST/Interp/complex.cpp
index 66490e973988bb..1e97cc61849524 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -37,6 +37,12 @@ constexpr _Complex int I2 = {};
 static_assert(__real(I2) == 0, "");
 static_assert(__imag(I2) == 0, "");
 
+static_assert(__real(4.0) == 4.0, "");
+static_assert(__real(12u) == 12u, "");
+static_assert(__imag(4.0) == 0.0, "");
+static_assert(__imag(13) == 0, "");
+
+
 
 /// Standalone complex expressions.
 static_assert(__real((_Complex float){1.0, 3.0}) == 1.0, "");



More information about the cfe-commits mailing list