[clang] [CIR] Add virtual base support to getAddressOfBaseClass (PR #159162)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 23 09:01:37 PDT 2025


================
@@ -865,39 +865,55 @@ Address CIRGenFunction::getAddressOfBaseClass(
     bool nullCheckValue, SourceLocation loc) {
   assert(!path.empty() && "Base path should not be empty!");
 
+  CastExpr::path_const_iterator start = path.begin();
+  const CXXRecordDecl *vBase = nullptr;
+
   if ((*path.begin())->isVirtual()) {
-    // The implementation here is actually complete, but let's flag this
-    // as an error until the rest of the virtual base class support is in place.
-    cgm.errorNYI(loc, "getAddrOfBaseClass: virtual base");
-    return Address::invalid();
+    vBase = (*start)->getType()->castAsCXXRecordDecl();
+    ++start;
   }
 
   // Compute the static offset of the ultimate destination within its
   // allocating subobject (the virtual base, if there is one, or else
   // the "complete" object that we see).
-  CharUnits nonVirtualOffset =
-      cgm.computeNonVirtualBaseClassOffset(derived, path);
+  CharUnits nonVirtualOffset = cgm.computeNonVirtualBaseClassOffset(
+      vBase ? vBase : derived, {start, path.end()});
----------------
andykaylor wrote:

We've asserted that `path` is not empty at the beginning of the function, which is going to be true because this is only called from derived-to-base cast handling code, so when we get here `{start, path.end()}` will only be empty if `(*path.begin())->isVirtual()` is true and `vBase` will be non-null in that case. However, if something goes haywire and we do call `computeNonVirtualBaseClassOffset()` with an empty path, it will just return a zero offset.

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


More information about the cfe-commits mailing list