blob: fafdf87aabf382dae27690699726453d457f30c0 [file] [log] [blame]
Anders Carlsson756b5c42009-10-30 01:42:31 +00001//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ exception related code generation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15using namespace clang;
16using namespace CodeGen;
17
Anders Carlssond3379292009-10-30 02:27:02 +000018static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
19 // void *__cxa_allocate_exception(size_t thrown_size);
20 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
21 std::vector<const llvm::Type*> Args(1, SizeTy);
22
23 const llvm::FunctionType *FTy =
24 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
25 Args, false);
26
27 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
28}
29
30static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
31 // void __cxa_throw (void *thrown_exception, std::type_info *tinfo,
32 // void (*dest) (void *) );
33
34 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
35 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
36
37 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000038 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
39 Args, false);
Anders Carlssond3379292009-10-30 02:27:02 +000040
41 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
42}
43
Mike Stumpb4eea692009-11-20 00:56:31 +000044static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
45 // void __cxa_rethrow ();
46
47 const llvm::FunctionType *FTy =
48 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
49
50 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
51}
52
Anders Carlsson756b5c42009-10-30 01:42:31 +000053void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +000054 if (!E->getSubExpr()) {
Mike Stumpb4eea692009-11-20 00:56:31 +000055 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
56 Builder.CreateUnreachable();
57
58 // Clear the insertion point to indicate we are in unreachable code.
59 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +000060 return;
61 }
62
63 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlssond3379292009-10-30 02:27:02 +000064 // FIXME: Handle cleanup.
65 if (!CleanupEntries.empty()){
66 ErrorUnsupported(E, "throw expression");
67 return;
68 }
69
70 // Now allocate the exception object.
71 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
72 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
73
74 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
75 llvm::Value *ExceptionPtr =
76 Builder.CreateCall(AllocExceptionFn,
77 llvm::ConstantInt::get(SizeTy, TypeSize),
78 "exception");
79
80 // Store the throw exception in the exception object.
81 if (!hasAggregateLLVMType(ThrowType)) {
82 llvm::Value *Value = EmitScalarExpr(E->getSubExpr());
83 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
84
85 Builder.CreateStore(Value, Builder.CreateBitCast(ExceptionPtr, ValuePtrTy));
86 } else {
Mike Stumpe36c9ab2009-11-20 01:57:39 +000087 // See EmitCXXConstructorCall.
88 const llvm::Type *Ty = ConvertType(ThrowType)->getPointerTo(0);
89 const CXXRecordDecl *RD;
90 RD = cast<CXXRecordDecl>(ThrowType->getAs<RecordType>()->getDecl());
91 if (RD->hasTrivialCopyConstructor()) {
92 EmitAggExpr(E->getSubExpr(), Builder.CreateBitCast(ExceptionPtr, Ty),
93 false);
94 } else
95 ErrorUnsupported(E, "throw expression with copy ctor");
Anders Carlssond3379292009-10-30 02:27:02 +000096 }
97
98 // Now throw the exception.
99 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stump23886d02009-11-20 00:43:57 +0000100 llvm::Constant *TypeInfo = CGM.GenerateRtti(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000101 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
102
103 llvm::CallInst *ThrowCall =
104 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
105 ThrowCall->setDoesNotReturn();
106 Builder.CreateUnreachable();
107
108 // Clear the insertion point to indicate we are in unreachable code.
109 Builder.ClearInsertionPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000110}