[clang] [clang] Add builtin_get_vtable_pointer and virtual_member_address (PR #135469)

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 15 13:24:27 PDT 2025


================
@@ -5349,6 +5350,40 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     return RValue::get(Result);
   }
 
+  case Builtin::BI__builtin_virtual_member_address: {
+    Address This = EmitLValue(E->getArg(0)).getAddress();
+    APValue ConstMemFun;
+    E->getArg(1)->isCXX11ConstantExpr(getContext(), &ConstMemFun, nullptr);
+    const CXXMethodDecl *CXXMethod =
+        cast<CXXMethodDecl>(ConstMemFun.getMemberPointerDecl());
+    const CGFunctionInfo &FInfo =
+        CGM.getTypes().arrangeCXXMethodDeclaration(CXXMethod);
+    llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(FInfo);
+    CGCallee VCallee = CGM.getCXXABI().getVirtualFunctionPointer(
+        *this, CXXMethod, This, Ty, E->getBeginLoc());
+    llvm::Value *Callee = VCallee.getFunctionPointer();
+    if (const CGPointerAuthInfo &Schema = VCallee.getPointerAuthInfo())
+      Callee = EmitPointerAuthAuth(Schema, Callee);
+    return RValue::get(Callee);
+  }
+
+  case Builtin::BI__builtin_get_vtable_pointer: {
+    const Expr *Target = E->getArg(0);
+    QualType TargetType = Target->getType();
+    QualType RecordType = TargetType;
+    if (RecordType->isPointerOrReferenceType())
+      RecordType = RecordType->getPointeeType();
+    const CXXRecordDecl *Decl = RecordType->getAsCXXRecordDecl();
+    assert(Decl);
----------------
shafik wrote:

I note you are asserting pointers here but not in `BI__builtin_virtual_member_address` case, not obvious to me why.

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


More information about the cfe-commits mailing list