blob: 2374d91a2bfe4a45babfd521344600a83d96038a [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
Mike Stump2bf701e2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
17
Anders Carlsson756b5c42009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlssond3379292009-10-30 02:27:02 +000022static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
23 // void *__cxa_allocate_exception(size_t thrown_size);
24 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
25 std::vector<const llvm::Type*> Args(1, SizeTy);
26
27 const llvm::FunctionType *FTy =
28 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
30
31 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump99533832009-12-02 07:41:41 +000034static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35 // void __cxa_free_exception(void *thrown_exception);
36 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
37 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
38
39 const llvm::FunctionType *FTy =
40 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
42
43 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48 // void (*dest) (void *));
Anders Carlssond3379292009-10-30 02:27:02 +000049
50 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
52
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Anders Carlssond3379292009-10-30 02:27:02 +000056
57 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpb4eea692009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpb4eea692009-11-20 00:56:31 +000062
63 const llvm::FunctionType *FTy =
64 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
65
66 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump2bf701e2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000071
72 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
74
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump2bf701e2009-11-20 23:44:51 +000077
78 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000083
84 const llvm::FunctionType *FTy =
85 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
86
87 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stump0f590be2009-12-01 03:41:18 +000090// FIXME: Eventually this will all go into the backend. Set from the target for
91// now.
92static int using_sjlj_exceptions = 0;
93
94static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
95 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
96 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
97
98 const llvm::FunctionType *FTy =
99 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
100 false);
101
102 if (using_sjlj_exceptions)
103 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
104 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
105}
106
Mike Stump99533832009-12-02 07:41:41 +0000107static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
108 // void __terminate();
109
110 const llvm::FunctionType *FTy =
111 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
112
113 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
114}
115
Mike Stump0f590be2009-12-01 03:41:18 +0000116// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
117// N is casted to the right type.
118static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
119 QualType ObjectType = E->getType();
120
121 // Store the throw exception in the exception object.
122 if (!CGF.hasAggregateLLVMType(ObjectType)) {
123 llvm::Value *Value = CGF.EmitScalarExpr(E);
124 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
125
126 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
127 } else {
128 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
129 const CXXRecordDecl *RD;
130 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
131 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
132 if (RD->hasTrivialCopyConstructor()) {
133 CGF.EmitAggExpr(E, This, false);
134 } else if (CXXConstructorDecl *CopyCtor
135 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump99533832009-12-02 07:41:41 +0000136 // All temporaries end before we call __cxa_throw
Mike Stumpce033902009-12-04 03:55:53 +0000137 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
138 // CodeGenFunction::CleanupScope TryScope(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000139 {
140 // These actions are only on the exceptional edge.
Mike Stumpce033902009-12-04 03:55:53 +0000141#if 0
142 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
Mike Stump99533832009-12-02 07:41:41 +0000143 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
144
145 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
146 const llvm::Type *Int8PtrTy
147 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
148 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
149 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpce033902009-12-04 03:55:53 +0000150#endif
Mike Stump99533832009-12-02 07:41:41 +0000151 }
152
Mike Stump0f590be2009-12-01 03:41:18 +0000153 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
154
155 // Stolen from EmitClassAggrMemberwiseCopy
156 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
157 Ctor_Complete);
158 CallArgList CallArgs;
159 CallArgs.push_back(std::make_pair(RValue::get(This),
160 CopyCtor->getThisType(CGF.getContext())));
161
162 // Push the Src ptr.
163 CallArgs.push_back(std::make_pair(RValue::get(Src),
164 CopyCtor->getParamDecl(0)->getType()));
165 QualType ResultType =
166 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
167 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
168 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000169 } else
Mike Stump99533832009-12-02 07:41:41 +0000170 llvm::llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000171 }
172}
173
174// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
175// N is casted to the right type.
176static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000177 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000178 // Store the throw exception in the exception object.
179 if (!CGF.hasAggregateLLVMType(ObjectType)) {
180 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000181 if (!WasPointer)
182 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000183 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000184 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
185 } else {
186 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
187 const CXXRecordDecl *RD;
188 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
189 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
190 if (RD->hasTrivialCopyConstructor()) {
191 CGF.EmitAggregateCopy(This, E, ObjectType);
192 } else if (CXXConstructorDecl *CopyCtor
193 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000194 llvm::Value *Src = E;
195
196 // Stolen from EmitClassAggrMemberwiseCopy
197 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
198 Ctor_Complete);
199 CallArgList CallArgs;
200 CallArgs.push_back(std::make_pair(RValue::get(This),
201 CopyCtor->getThisType(CGF.getContext())));
202
203 // Push the Src ptr.
204 CallArgs.push_back(std::make_pair(RValue::get(Src),
205 CopyCtor->getParamDecl(0)->getType()));
206 QualType ResultType =
207 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
208 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
209 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000210 } else
211 llvm::llvm_unreachable("uncopyable object");
212 }
213}
214
Anders Carlsson756b5c42009-10-30 01:42:31 +0000215void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000216 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000217 if (getInvokeDest()) {
218 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
219 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
220 ->setDoesNotReturn();
221 EmitBlock(Cont);
222 } else
223 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000224 Builder.CreateUnreachable();
225
226 // Clear the insertion point to indicate we are in unreachable code.
227 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000228 return;
229 }
230
231 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlssond3379292009-10-30 02:27:02 +0000232
233 // Now allocate the exception object.
234 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
235 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
236
237 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
238 llvm::Value *ExceptionPtr =
239 Builder.CreateCall(AllocExceptionFn,
240 llvm::ConstantInt::get(SizeTy, TypeSize),
241 "exception");
242
Mike Stump0f590be2009-12-01 03:41:18 +0000243 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000244
245 // Now throw the exception.
246 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpde050572009-12-02 18:57:08 +0000247 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000248 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
249
Mike Stump0a3816e2009-12-04 01:51:45 +0000250 if (getInvokeDest()) {
251 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
252 llvm::InvokeInst *ThrowCall =
253 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
254 ExceptionPtr, TypeInfo, Dtor);
255 ThrowCall->setDoesNotReturn();
256 EmitBlock(Cont);
257 } else {
258 llvm::CallInst *ThrowCall =
259 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
260 ThrowCall->setDoesNotReturn();
261 }
Anders Carlssond3379292009-10-30 02:27:02 +0000262 Builder.CreateUnreachable();
263
264 // Clear the insertion point to indicate we are in unreachable code.
265 Builder.ClearInsertionPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000266}
Mike Stump2bf701e2009-11-20 23:44:51 +0000267
268void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stumpfffefeb2009-12-04 03:57:07 +0000269 if (0) {
Mike Stumpc88b6732009-12-02 18:20:18 +0000270 EmitStmt(S.getTryBlock());
271 return;
Mike Stump0f590be2009-12-01 03:41:18 +0000272 }
Mike Stump0f590be2009-12-01 03:41:18 +0000273 // FIXME: The below is still just a sketch of the code we need.
Mike Stump2bf701e2009-11-20 23:44:51 +0000274 // Pointer to the personality function
275 llvm::Constant *Personality =
276 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
277 (VMContext),
278 true),
279 "__gxx_personality_v0");
280 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000281 llvm::Value *llvm_eh_exception =
282 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
283 llvm::Value *llvm_eh_selector =
284 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000285
Mike Stump2bf701e2009-11-20 23:44:51 +0000286 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
287 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000288 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000289 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000290 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
291
292 // Push an EH context entry, used for handling rethrows.
293 PushCleanupBlock(FinallyBlock);
294
295 // Emit the statements in the try {} block
296 setInvokeDest(TryHandler);
297
298 EmitStmt(S.getTryBlock());
299
300 // Jump to end if there is no exception
301 EmitBranchThroughCleanup(FinallyEnd);
302
Mike Stump639787c2009-12-02 19:53:57 +0000303 // Set up terminate handler
304 llvm::BasicBlock *TerminateHandler = createBasicBlock("terminate.handler");
305 EmitBlock(TerminateHandler);
306 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
307 // We are required to emit this call to satisfy LLVM, even
308 // though we don't use the result.
309 llvm::SmallVector<llvm::Value*, 8> Args;
310 Args.clear();
311 Args.push_back(Exc);
312 Args.push_back(Personality);
313 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
314 0));
315 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
316 llvm::CallInst *TerminateCall =
317 Builder.CreateCall(getTerminateFn(*this));
318 TerminateCall->setDoesNotReturn();
319 TerminateCall->setDoesNotThrow();
320 Builder.CreateUnreachable();
321
322 // Clear the insertion point to indicate we are in unreachable code.
323 Builder.ClearInsertionPoint();
324
Mike Stump2bf701e2009-11-20 23:44:51 +0000325 // Emit the handlers
326 EmitBlock(TryHandler);
327
328 const llvm::IntegerType *Int8Ty;
329 const llvm::PointerType *PtrToInt8Ty;
330 Int8Ty = llvm::Type::getInt8Ty(VMContext);
331 // C string type. Used in lots of places.
332 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000333 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
334 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000335 llvm::Value *llvm_eh_typeid_for =
336 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000337 // Exception object
Mike Stump639787c2009-12-02 19:53:57 +0000338 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000339 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000340
Mike Stump639787c2009-12-02 19:53:57 +0000341 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000342 SelectorArgs.push_back(Exc);
343 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000344
Mike Stump0f590be2009-12-01 03:41:18 +0000345 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000346 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
347 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000348 VarDecl *CatchParam = C->getExceptionDecl();
349 if (CatchParam) {
Mike Stumpde050572009-12-02 18:57:08 +0000350 llvm::Value *EHType
351 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump0f590be2009-12-01 03:41:18 +0000352 SelectorArgs.push_back(EHType);
Mike Stump2bf701e2009-11-20 23:44:51 +0000353 } else {
354 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000355 SelectorArgs.push_back(Null);
356 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000357 }
358 }
359
Mike Stump0f590be2009-12-01 03:41:18 +0000360 // We use a cleanup unless there was already a catch all.
361 if (!HasCatchAll) {
362 SelectorArgs.push_back(Null);
363 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000364
Mike Stump0f590be2009-12-01 03:41:18 +0000365 // Find which handler was matched.
366 llvm::Value *Selector
367 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
368 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000369 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
370 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000371 VarDecl *CatchParam = C->getExceptionDecl();
372 Stmt *CatchBody = C->getHandlerBlock();
373
374 llvm::BasicBlock *Next = 0;
375
376 if (SelectorArgs[i+2] != Null) {
377 llvm::BasicBlock *Match = createBasicBlock("match");
378 Next = createBasicBlock("catch.next");
379 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
380 llvm::Value *Id
381 = Builder.CreateCall(llvm_eh_typeid_for,
382 Builder.CreateBitCast(SelectorArgs[i+2],
383 Int8PtrTy));
384 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
385 Match, Next);
386 EmitBlock(Match);
387 }
388
389 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
390 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
391
392 PushCleanupBlock(MatchEnd);
393 setInvokeDest(MatchHandler);
394
395 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
396
Mike Stumpf7f74672009-12-02 23:37:16 +0000397 {
398 CleanupScope CatchScope(*this);
399 // Bind the catch parameter if it exists.
400 if (CatchParam) {
401 QualType CatchType = CatchParam->getType().getNonReferenceType();
402 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000403 bool WasPointer = true;
404 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000405 if (!isa<ReferenceType>(CatchParam->getType()))
406 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000407 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000408 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000409 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000410 EmitLocalBlockVarDecl(*CatchParam);
Mike Stump0f590be2009-12-01 03:41:18 +0000411#if 0
Mike Stumpf7f74672009-12-02 23:37:16 +0000412 // FIXME: objects with ctors, references
413 Builder.CreateStore(ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump0f590be2009-12-01 03:41:18 +0000414#else
Mike Stumpf668bd02009-12-03 03:40:14 +0000415 // FIXME: we need to do this sooner so that the EH region for the
416 // cleanup doesn't start until after the ctor completes, use a decl
417 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000418 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000419 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump0f590be2009-12-01 03:41:18 +0000420#endif
Mike Stumpf7f74672009-12-02 23:37:16 +0000421 setInvokeDest(MatchHandler);
422 }
423
424 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000425 }
426
Mike Stump0f590be2009-12-01 03:41:18 +0000427 EmitBranchThroughCleanup(FinallyEnd);
428
429 EmitBlock(MatchHandler);
430
431 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
432 // We are required to emit this call to satisfy LLVM, even
433 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000434 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000435 Args.push_back(Exc);
436 Args.push_back(Personality);
437 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
438 0));
439 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
440 Builder.CreateStore(Exc, RethrowPtr);
441 EmitBranchThroughCleanup(FinallyRethrow);
442
443 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
444
445 EmitBlock(MatchEnd);
446
Mike Stump99533832009-12-02 07:41:41 +0000447 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000448 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000449 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000450 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000451 EmitBlock(Cont);
452 if (Info.SwitchBlock)
453 EmitBlock(Info.SwitchBlock);
454 if (Info.EndBlock)
455 EmitBlock(Info.EndBlock);
456
Mike Stump0f590be2009-12-01 03:41:18 +0000457 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000458 Builder.CreateStore(Exc, RethrowPtr);
459 EmitBranchThroughCleanup(FinallyRethrow);
460
461 if (Next)
462 EmitBlock(Next);
463 }
464 if (!HasCatchAll)
465 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stump2bf701e2009-11-20 23:44:51 +0000466
467 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
468
469 setInvokeDest(PrevLandingPad);
470
471 EmitBlock(FinallyBlock);
472
Mike Stump0f590be2009-12-01 03:41:18 +0000473 if (Info.SwitchBlock)
474 EmitBlock(Info.SwitchBlock);
475 if (Info.EndBlock)
476 EmitBlock(Info.EndBlock);
477
Mike Stump2bf701e2009-11-20 23:44:51 +0000478 // Branch around the rethrow code.
479 EmitBranch(FinallyEnd);
480
481 EmitBlock(FinallyRethrow);
Mike Stump0f590be2009-12-01 03:41:18 +0000482 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
483 Builder.CreateLoad(RethrowPtr));
Mike Stump2bf701e2009-11-20 23:44:51 +0000484 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000485
486 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000487}