[clang] [CIR] Add support for constructors with VTT parameters (PR #156521)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 3 03:53:59 PDT 2025


================
@@ -723,6 +811,52 @@ void CIRGenFunction::emitCXXDestructorCall(const CXXDestructorDecl *dd,
                                      delegating, thisAddr, thisTy);
 }
 
+mlir::Value CIRGenFunction::getVTTParameter(GlobalDecl gd, bool forVirtualBase,
+                                            bool delegating) {
+  if (!cgm.getCXXABI().needsVTTParameter(gd))
+    return nullptr;
+
+  const CXXRecordDecl *rd = cast<CXXMethodDecl>(curFuncDecl)->getParent();
+  const CXXRecordDecl *base = cast<CXXMethodDecl>(gd.getDecl())->getParent();
+
+  uint64_t subVTTIndex;
+
+  if (delegating) {
+    cgm.errorNYI(rd->getSourceRange(),
+                 "getVTTParameter: delegating constructor");
+    return {};
+  } else if (rd == base) {
+    // If the record matches the base, this is the complete ctor/dtor
+    // variant calling the base variant in a class with virtual bases.
+    assert(!cgm.getCXXABI().needsVTTParameter(curGD) &&
+           "doing no-op VTT offset in base dtor/ctor?");
+    assert(!forVirtualBase && "Can't have same class as virtual base!");
+    subVTTIndex = 0;
+  } else {
+    const ASTRecordLayout &layout = getContext().getASTRecordLayout(rd);
+    CharUnits baseOffset = forVirtualBase ? layout.getVBaseClassOffset(base)
+                                          : layout.getBaseClassOffset(base);
+
+    subVTTIndex =
+        cgm.getVTables().getSubVTTIndex(rd, BaseSubobject(base, baseOffset));
+    assert(subVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
+  }
+
+  mlir::Location loc = cgm.getLoc(rd->getBeginLoc());
+  if (cgm.getCXXABI().needsVTTParameter(curGD)) {
+    // A VTT parameter was passed to the constructor, use it.
+    mlir::Value vtt = loadCXXVTT();
+    return cgm.getBuilder().createVTTAddrPoint(loc, vtt.getType(), vtt,
----------------
xlauko wrote:

```suggestion
    return builder.createVTTAddrPoint(loc, vtt.getType(), vtt,
```

https://github.com/llvm/llvm-project/pull/156521


More information about the cfe-commits mailing list