[PATCH] D136920: [clang][Interp] Array initialization via CXXConstructExpr

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 28 00:26:19 PDT 2022


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136920

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/arrays.cpp


Index: clang/test/AST/Interp/arrays.cpp
===================================================================
--- clang/test/AST/Interp/arrays.cpp
+++ clang/test/AST/Interp/arrays.cpp
@@ -181,6 +181,18 @@
   }
 };
 
+class A {
+public:
+  int a;
+  constexpr A() : a(10) {}
+};
+class B {
+public:
+  A a[2];
+  constexpr B() {}
+};
+constexpr B b;
+
 namespace IncDec {
   // FIXME: Pointer arithmethic needs to be supported in inc/dec
   //   unary operators
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1007,6 +1007,30 @@
       assert(false && "default initializer for non-primitive type");
     }
 
+    return true;
+  } else if (const auto *Ctor = dyn_cast<CXXConstructExpr>(Initializer)) {
+    const ArrayType *AT = Ctor->getType()->getAsArrayTypeUnsafe();
+    const auto *CAT = cast<ConstantArrayType>(AT);
+    size_t NumElems = CAT->getSize().getZExtValue();
+    const Function *Func = getFunction(Ctor->getConstructor());
+    if (!Func || !Func->isConstexpr())
+      return false;
+
+    assert(Ctor->getNumArgs() == 0);
+    for (size_t I = 0; I != NumElems; ++I) {
+      if (!this->emitDupPtr(Initializer))
+        return false;
+      if (!this->emitConstUint64(I, Initializer))
+        return false;
+      if (!this->emitAddOffsetUint64(Initializer))
+        return false;
+      if (!this->emitNarrowPtr(Initializer))
+        return false;
+
+      // Actually call the constructor
+      if (!this->emitCall(Func, Ctor))
+        return false;
+    }
     return true;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136920.471421.patch
Type: text/x-patch
Size: 1668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221028/4b2054b4/attachment-0001.bin>


More information about the cfe-commits mailing list