[clang] 12e425d - [clang][Interp] Support __real/__imag on primitives (#75485)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 07:08:27 PST 2024


Author: Timm Baeder
Date: 2024-01-15T16:08:22+01:00
New Revision: 12e425d0cf9bca072c7b2138e50acbc5f1cd818c

URL: https://github.com/llvm/llvm-project/commit/12e425d0cf9bca072c7b2138e50acbc5f1cd818c
DIFF: https://github.com/llvm/llvm-project/commit/12e425d0cf9bca072c7b2138e50acbc5f1cd818c.diff

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

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 8863c5f89027e4..5839123a5b95f2 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2768,7 +2768,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))
@@ -2783,7 +2784,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