diff options
| author | Warren Hunt <whunt@google.com> | 2014-06-18 21:15:55 +0000 |
|---|---|---|
| committer | Warren Hunt <whunt@google.com> | 2014-06-18 21:15:55 +0000 |
| commit | 747e301ea016b61dd8bcf86c9db06de720fd914c (patch) | |
| tree | d4a1c8a375f1a5bf46bb5eb2bef8631f89ff317e /clang/lib/CodeGen | |
| parent | 1fe2c5ab277cb7a2c9de0175912d7aeabfaed6e5 (diff) | |
| download | bcm5719-llvm-747e301ea016b61dd8bcf86c9db06de720fd914c.tar.gz bcm5719-llvm-747e301ea016b61dd8bcf86c9db06de720fd914c.zip | |
[MS-ABI] Implement typeid
This patch enables clang to generate calls to __RTtypeid when lowering
typeid on win32 targets. Test cases are included.
llvm-svn: 211223
Diffstat (limited to 'clang/lib/CodeGen')
| -rw-r--r-- | clang/lib/CodeGen/CGExprCXX.cpp | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index 394ba1d8f77..0b5a415a0ea 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -1642,12 +1642,45 @@ static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF, llvm_unreachable("One of our vbases should be polymorphic."); } -static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, - const Expr *E, +static llvm::Value *emitRTtypeidCall(CodeGenFunction &CGF, + llvm::Value *Argument) { + llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; + llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( + llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), "__RTtypeid"); + llvm::Value *Args[] = {Argument}; + return CGF.EmitRuntimeCall(Function, Args); +} + +static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, llvm::Type *StdTypeInfoPtrTy) { // Get the vtable pointer. llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); + if (CGF.getTarget().getCXXABI().isMicrosoft()) { + llvm::Value *CastPtr = CGF.Builder.CreateBitCast(ThisPtr, CGF.Int8PtrTy); + const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl(); + if (CGF.getContext().getASTRecordLayout(RD).hasExtendableVFPtr()) + return CGF.Builder.CreateBitCast(emitRTtypeidCall(CGF, CastPtr), + StdTypeInfoPtrTy); + llvm::BasicBlock *EntryBlock = CGF.Builder.GetInsertBlock(); + llvm::BasicBlock *AdjustBlock = CGF.createBasicBlock("type_id.valid"); + llvm::BasicBlock *ExitBlock = CGF.createBasicBlock("type_id.call"); + CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNull(CastPtr), ExitBlock, + AdjustBlock); + // Emit the call block and code for it. + CGF.EmitBlock(AdjustBlock); + llvm::Value *AdjustedThisPtr = CGF.Builder.CreateInBoundsGEP( + CastPtr, getPolymorphicOffset(CGF, RD, CastPtr)); + // Emit the call block and the phi nodes for it. + CGF.EmitBlock(ExitBlock); + llvm::PHINode *ValuePHI = CGF.Builder.CreatePHI(CGF.Int8PtrTy, 2); + ValuePHI->addIncoming(AdjustedThisPtr, AdjustBlock); + ValuePHI->addIncoming(llvm::Constant::getNullValue(CGF.Int8PtrTy), + EntryBlock); + return CGF.Builder.CreateBitCast(emitRTtypeidCall(CGF, ValuePHI), + StdTypeInfoPtrTy); + } + // C++ [expr.typeid]p2: // If the glvalue expression is obtained by applying the unary * operator to // a pointer and the pointer is a null pointer value, the typeid expression |

