[PATCH] D153693: [clang][Interp] Handle InitListExprs of composite type

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 24 00:56:10 PDT 2023


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.

  They might appear freestanding, not as part of an initializer. In that
  case, we need to create a temporary variable and return a pointer to it.

The test case added doesn't work yet since passing structs by value to functions is broken right now, I will fix that in a follow-up patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153693

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


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -895,3 +895,10 @@
 #endif
 
 }
+
+#if 0
+constexpr bool BPand(BoolPair bp) {
+  return bp.first && bp.second;
+}
+static_assert(BPand(BoolPair{true, false}) == false, "");
+#endif
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -525,16 +525,27 @@
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
-  for (const Expr *Init : E->inits()) {
-    if (DiscardResult) {
-      if (!this->discard(Init))
-        return false;
-    } else {
-      if (!this->visit(Init))
-        return false;
-    }
+  if (std::optional<PrimType> T = classify(E->getType())) {
+    assert(E->getNumInits() == 1);
+    return DiscardResult ? this->discard(E->inits()[0])
+                         : this->visit(E->inits()[0]);
   }
-  return true;
+
+  if (std::optional<unsigned> LocalIndex = allocateLocal(E, false)) {
+    if (!this->emitGetPtrLocal(*LocalIndex, E))
+      return false;
+
+    if (!this->visitInitializer(E))
+      return false;
+
+    if (DiscardResult)
+      return this->emitPopPtr(E);
+    return true;
+  }
+
+  // TODO: Array, complex, etc. types might appear here as well.
+
+  return false;
 }
 
 template <class Emitter>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153693.534175.patch
Type: text/x-patch
Size: 1533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230624/468776cf/attachment-0001.bin>


More information about the cfe-commits mailing list