[clang] [clang][Interp] Handle delegating constructors (PR #67823)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 09:01:09 PDT 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/67823

None

>From d858d4b61c6d0e469debb9c8b865b4b5d4c7244b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 29 Sep 2023 18:00:08 +0200
Subject: [PATCH] [clang][Interp] Handle delegating constructors

---
 clang/lib/AST/Interp/ByteCodeStmtGen.cpp      |  8 +++++++
 clang/test/AST/Interp/records.cpp             | 23 +++++++++++++++++++
 .../SemaCXX/constant-expression-cxx1z.cpp     |  1 +
 3 files changed, 32 insertions(+)

diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index 15eae8e20b3a678..838e7bb67d0c076 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -193,6 +193,14 @@ bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
           return false;
         if (!this->emitPopPtr(InitExpr))
           return false;
+      } else {
+        assert(Init->isDelegatingInitializer());
+        if (!this->emitThis(InitExpr))
+          return false;
+        if (!this->visitInitializer(Init->getInit()))
+          return false;
+        if (!this->emitPopPtr(InitExpr))
+          return false;
       }
     }
   }
diff --git a/clang/test/AST/Interp/records.cpp b/clang/test/AST/Interp/records.cpp
index bcc84087fc54020..3c866825d1f077c 100644
--- a/clang/test/AST/Interp/records.cpp
+++ b/clang/test/AST/Interp/records.cpp
@@ -1066,3 +1066,26 @@ namespace ParenInit {
   constexpr B b(A(1),2);
 }
 #endif
+
+namespace DelegatingConstructors {
+  struct S {
+    int a;
+    constexpr S() : S(10) {}
+    constexpr S(int a) : a(a) {}
+  };
+  constexpr S s = {};
+  static_assert(s.a == 10, "");
+
+  struct B {
+    int a;
+    int b;
+
+    constexpr B(int a) : a(a), b(a + 2) {}
+  };
+  struct A : B {
+    constexpr A() : B(10) {};
+  };
+  constexpr A d4 = {};
+  static_assert(d4.a == 10, "");
+  static_assert(d4.b == 12, "");
+}
diff --git a/clang/test/SemaCXX/constant-expression-cxx1z.cpp b/clang/test/SemaCXX/constant-expression-cxx1z.cpp
index 9335626a5c90a4f..8b5fcf439a7da26 100644
--- a/clang/test/SemaCXX/constant-expression-cxx1z.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx1z.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c++1z -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
+// RUN: %clang_cc1 -std=c++1z -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu -fexperimental-new-constant-interpreter
 
 namespace BaseClassAggregateInit {
   struct A {



More information about the cfe-commits mailing list