blob: 3abc40775a8ae8e20949681e9f7c62eedf8efa35 [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();
64 // FIXME: We only handle non-class types for now.
65 if (ThrowType->isRecordType()) {
66 ErrorUnsupported(E, "throw expression");
67 return;
68 }
69
70 // FIXME: Handle cleanup.
71 if (!CleanupEntries.empty()){
72 ErrorUnsupported(E, "throw expression");
73 return;
74 }
75
76 // Now allocate the exception object.
77 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
78 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
79
80 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
81 llvm::Value *ExceptionPtr =
82 Builder.CreateCall(AllocExceptionFn,
83 llvm::ConstantInt::get(SizeTy, TypeSize),
84 "exception");
85
86 // Store the throw exception in the exception object.
87 if (!hasAggregateLLVMType(ThrowType)) {
88 llvm::Value *Value = EmitScalarExpr(E->getSubExpr());
89 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
90
91 Builder.CreateStore(Value, Builder.CreateBitCast(ExceptionPtr, ValuePtrTy));
92 } else {
93 // FIXME: Handle complex and aggregate expressions.
94 ErrorUnsupported(E, "throw expression");
95 }
96
97 // Now throw the exception.
98 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stump23886d02009-11-20 00:43:57 +000099 llvm::Constant *TypeInfo = CGM.GenerateRtti(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000100 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
101
102 llvm::CallInst *ThrowCall =
103 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
104 ThrowCall->setDoesNotReturn();
105 Builder.CreateUnreachable();
106
107 // Clear the insertion point to indicate we are in unreachable code.
108 Builder.ClearInsertionPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000109}