1 //===--- 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 "clang/AST/StmtCXX.h"
15 
16 #include "llvm/Intrinsics.h"
17 
18 #include "CodeGenFunction.h"
19 using namespace clang;
20 using namespace CodeGen;
21 
22 static 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 
34 static 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 
46 static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
47   // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48   //                  void (*dest) (void *));
49 
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 =
54     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55                             Args, false);
56 
57   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58 }
59 
60 static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
61   // void __cxa_rethrow();
62 
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 
69 static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
70   // void* __cxa_begin_catch();
71 
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 =
76     llvm::FunctionType::get(Int8PtrTy, Args, false);
77 
78   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79 }
80 
81 static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
82   // void __cxa_end_catch();
83 
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 
90 static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91   // void __cxa_call_unexepcted(void *thrown_exception);
92 
93   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
94   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
95 
96   const llvm::FunctionType *FTy =
97     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98                             Args, false);
99 
100   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101 }
102 
103 llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
104   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
105   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
106 
107   const llvm::FunctionType *FTy =
108     llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args,
109                             false);
110 
111   if (CGM.getLangOptions().SjLjExceptions)
112     return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
113   return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
114 }
115 
116 static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
117   // void __terminate();
118 
119   const llvm::FunctionType *FTy =
120     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
121 
122   return CGF.CGM.CreateRuntimeFunction(FTy,
123       CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort");
124 }
125 
126 static llvm::Constant *getPersonalityFn(CodeGenModule &CGM) {
127   const char *PersonalityFnName = "__gcc_personality_v0";
128   LangOptions Opts = CGM.getLangOptions();
129   if (Opts.CPlusPlus) {
130     if (Opts.SjLjExceptions)
131      PersonalityFnName = "__gxx_personality_sj0";
132     else
133      PersonalityFnName = "__gxx_personality_v0";
134   } else if (Opts.ObjC1) {
135     if (Opts.NeXTRuntime) {
136       if (Opts.ObjCNonFragileABI)
137         PersonalityFnName = "__gcc_personality_v0";
138     } else
139       PersonalityFnName = "__gnu_objc_personality_v0";
140   }
141 
142   llvm::Constant *Personality =
143   CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(
144                                                         CGM.getLLVMContext()),
145                                                     true),
146       PersonalityFnName);
147   return llvm::ConstantExpr::getBitCast(Personality, CGM.PtrToInt8Ty);
148 }
149 
150 // Emits an exception expression into the given location.  This
151 // differs from EmitAnyExprToMem only in that, if a final copy-ctor
152 // call is required, an exception within that copy ctor causes
153 // std::terminate to be invoked.
154 static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E,
155                              llvm::Value *ExnLoc) {
156   // We want to release the allocated exception object if this
157   // expression throws.  We do this by pushing an EH-only cleanup
158   // block which, furthermore, deactivates itself after the expression
159   // is complete.
160   llvm::AllocaInst *ShouldFreeVar =
161     CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
162                          "should-free-exnobj.var");
163   CGF.InitTempAlloca(ShouldFreeVar,
164                      llvm::ConstantInt::getFalse(CGF.getLLVMContext()));
165 
166   // A variable holding the exception pointer.  This is necessary
167   // because the throw expression does not necessarily dominate the
168   // cleanup, for example if it appears in a conditional expression.
169   llvm::AllocaInst *ExnLocVar =
170     CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var");
171 
172   llvm::BasicBlock *SavedInvokeDest = CGF.getInvokeDest();
173   {
174     CodeGenFunction::EHCleanupBlock Cleanup(CGF);
175     llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj");
176     llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done");
177 
178     llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar,
179                                                      "should-free-exnobj");
180     CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB);
181     CGF.EmitBlock(FreeBB);
182     llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj");
183     CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal);
184     CGF.EmitBlock(DoneBB);
185   }
186   llvm::BasicBlock *Cleanup = CGF.getInvokeDest();
187 
188   CGF.Builder.CreateStore(ExnLoc, ExnLocVar);
189   CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
190                           ShouldFreeVar);
191 
192   // __cxa_allocate_exception returns a void*;  we need to cast this
193   // to the appropriate type for the object.
194   const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo();
195   llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty);
196 
197   // FIXME: this isn't quite right!  If there's a final unelided call
198   // to a copy constructor, then according to [except.terminate]p1 we
199   // must call std::terminate() if that constructor throws, because
200   // technically that copy occurs after the exception expression is
201   // evaluated but before the exception is caught.  But the best way
202   // to handle that is to teach EmitAggExpr to do the final copy
203   // differently if it can't be elided.
204   CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false);
205 
206   CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
207                           ShouldFreeVar);
208 
209   // Pop the cleanup block if it's still the top of the cleanup stack.
210   // Otherwise, temporaries have been created and our cleanup will get
211   // properly removed in time.
212   // TODO: this is not very resilient.
213   if (CGF.getInvokeDest() == Cleanup)
214     CGF.setInvokeDest(SavedInvokeDest);
215 }
216 
217 // CopyObject - Utility to copy an object.  Calls copy constructor as necessary.
218 // N is casted to the right type.
219 static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
220                        bool WasPointer, bool WasPointerReference,
221                        llvm::Value *E, llvm::Value *N) {
222   // Store the throw exception in the exception object.
223   if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
224     llvm::Value *Value = E;
225     if (!WasPointer)
226       Value = CGF.Builder.CreateLoad(Value);
227     const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
228     if (WasPointerReference) {
229       llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
230       CGF.Builder.CreateStore(Value, Tmp);
231       Value = Tmp;
232       ValuePtrTy = Value->getType()->getPointerTo(0);
233     }
234     N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
235     CGF.Builder.CreateStore(Value, N);
236   } else {
237     const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
238     const CXXRecordDecl *RD;
239     RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
240     llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
241     if (RD->hasTrivialCopyConstructor()) {
242       CGF.EmitAggregateCopy(This, E, ObjectType);
243     } else if (CXXConstructorDecl *CopyCtor
244                = RD->getCopyConstructor(CGF.getContext(), 0)) {
245       llvm::Value *Src = E;
246 
247       // Stolen from EmitClassAggrMemberwiseCopy
248       llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
249                                                             Ctor_Complete);
250       CallArgList CallArgs;
251       CallArgs.push_back(std::make_pair(RValue::get(This),
252                                       CopyCtor->getThisType(CGF.getContext())));
253 
254       // Push the Src ptr.
255       CallArgs.push_back(std::make_pair(RValue::get(Src),
256                                         CopyCtor->getParamDecl(0)->getType()));
257 
258       const FunctionProtoType *FPT
259         = CopyCtor->getType()->getAs<FunctionProtoType>();
260       CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
261                    Callee, ReturnValueSlot(), CallArgs, CopyCtor);
262     } else
263       llvm_unreachable("uncopyable object");
264   }
265 }
266 
267 void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
268   if (!E->getSubExpr()) {
269     if (getInvokeDest()) {
270       llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
271       Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
272         ->setDoesNotReturn();
273       EmitBlock(Cont);
274     } else
275       Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
276     Builder.CreateUnreachable();
277 
278     // Clear the insertion point to indicate we are in unreachable code.
279     Builder.ClearInsertionPoint();
280     return;
281   }
282 
283   QualType ThrowType = E->getSubExpr()->getType();
284 
285   // Now allocate the exception object.
286   const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
287   uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
288 
289   llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
290   llvm::Value *ExceptionPtr =
291     Builder.CreateCall(AllocExceptionFn,
292                        llvm::ConstantInt::get(SizeTy, TypeSize),
293                        "exception");
294 
295   EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
296 
297   // Now throw the exception.
298   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
299   llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
300 
301   // The address of the destructor.  If the exception type has a
302   // trivial destructor (or isn't a record), we just pass null.
303   llvm::Constant *Dtor = 0;
304   if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
305     CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
306     if (!Record->hasTrivialDestructor()) {
307       CXXDestructorDecl *DtorD = Record->getDestructor(getContext());
308       Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
309       Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
310     }
311   }
312   if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
313 
314   if (getInvokeDest()) {
315     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
316     llvm::InvokeInst *ThrowCall =
317       Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
318                             ExceptionPtr, TypeInfo, Dtor);
319     ThrowCall->setDoesNotReturn();
320     EmitBlock(Cont);
321   } else {
322     llvm::CallInst *ThrowCall =
323       Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
324     ThrowCall->setDoesNotReturn();
325   }
326   Builder.CreateUnreachable();
327 
328   // Clear the insertion point to indicate we are in unreachable code.
329   Builder.ClearInsertionPoint();
330 
331   // FIXME: For now, emit a dummy basic block because expr emitters in generally
332   // are not ready to handle emitting expressions at unreachable points.
333   EnsureInsertPoint();
334 }
335 
336 void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
337   if (!Exceptions)
338     return;
339 
340   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
341   if (FD == 0)
342     return;
343   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
344   if (Proto == 0)
345     return;
346 
347   assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
348 
349   if (!Proto->hasExceptionSpec())
350     return;
351 
352   llvm::Constant *Personality = getPersonalityFn(CGM);
353   llvm::Value *llvm_eh_exception =
354     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
355   llvm::Value *llvm_eh_selector =
356     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
357   const llvm::IntegerType *Int8Ty;
358   const llvm::PointerType *PtrToInt8Ty;
359   Int8Ty = llvm::Type::getInt8Ty(VMContext);
360   // C string type.  Used in lots of places.
361   PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
362   llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
363   llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
364 
365   llvm::BasicBlock *PrevLandingPad = getInvokeDest();
366   llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
367   llvm::BasicBlock *Match = createBasicBlock("match");
368   llvm::BasicBlock *Unwind = 0;
369 
370   assert(PrevLandingPad == 0 && "EHSpec has invoke context");
371   (void)PrevLandingPad;
372 
373   llvm::BasicBlock *Cont = createBasicBlock("cont");
374 
375   EmitBranchThroughCleanup(Cont);
376 
377   // Emit the statements in the try {} block
378   setInvokeDest(EHSpecHandler);
379 
380   EmitBlock(EHSpecHandler);
381   // Exception object
382   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
383   llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
384 
385   SelectorArgs.push_back(Exc);
386   SelectorArgs.push_back(Personality);
387   SelectorArgs.push_back(llvm::ConstantInt::get(Int32Ty,
388                                                 Proto->getNumExceptions()+1));
389 
390   for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
391     QualType Ty = Proto->getExceptionType(i);
392     QualType ExceptType
393       = Ty.getNonReferenceType().getUnqualifiedType();
394     llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
395     SelectorArgs.push_back(EHType);
396   }
397   if (Proto->getNumExceptions())
398     SelectorArgs.push_back(Null);
399 
400   // Find which handler was matched.
401   llvm::Value *Selector
402     = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
403                          SelectorArgs.end(), "selector");
404   if (Proto->getNumExceptions()) {
405     Unwind = createBasicBlock("Unwind");
406 
407     Builder.CreateStore(Exc, RethrowPtr);
408     Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
409                                            llvm::ConstantInt::get(Int32Ty, 0)),
410                          Match, Unwind);
411 
412     EmitBlock(Match);
413   }
414   Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
415   Builder.CreateUnreachable();
416 
417   if (Proto->getNumExceptions()) {
418     EmitBlock(Unwind);
419     Builder.CreateCall(getUnwindResumeOrRethrowFn(),
420                        Builder.CreateLoad(RethrowPtr));
421     Builder.CreateUnreachable();
422   }
423 
424   EmitBlock(Cont);
425 }
426 
427 void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
428   if (!Exceptions)
429     return;
430 
431   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
432   if (FD == 0)
433     return;
434   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
435   if (Proto == 0)
436     return;
437 
438   if (!Proto->hasExceptionSpec())
439     return;
440 
441   setInvokeDest(0);
442 }
443 
444 void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
445   CXXTryStmtInfo Info = EnterCXXTryStmt(S);
446   EmitStmt(S.getTryBlock());
447   ExitCXXTryStmt(S, Info);
448 }
449 
450 CodeGenFunction::CXXTryStmtInfo
451 CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
452   CXXTryStmtInfo Info;
453   Info.SavedLandingPad = getInvokeDest();
454   Info.HandlerBlock = createBasicBlock("try.handler");
455   Info.FinallyBlock = createBasicBlock("finally");
456 
457   PushCleanupBlock(Info.FinallyBlock);
458   setInvokeDest(Info.HandlerBlock);
459 
460   return Info;
461 }
462 
463 void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
464                                      CXXTryStmtInfo TryInfo) {
465   // Pointer to the personality function
466   llvm::Constant *Personality = getPersonalityFn(CGM);
467   llvm::Value *llvm_eh_exception =
468     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
469   llvm::Value *llvm_eh_selector =
470     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
471 
472   llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
473   llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
474   llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
475   llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
476   llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
477 
478   // Jump to end if there is no exception
479   EmitBranchThroughCleanup(FinallyEnd);
480 
481   llvm::BasicBlock *TerminateHandler = getTerminateHandler();
482 
483   // Emit the handlers
484   EmitBlock(TryHandler);
485 
486   const llvm::IntegerType *Int8Ty;
487   const llvm::PointerType *PtrToInt8Ty;
488   Int8Ty = llvm::Type::getInt8Ty(VMContext);
489   // C string type.  Used in lots of places.
490   PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
491   llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
492   llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
493   llvm::Value *llvm_eh_typeid_for =
494     CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
495   // Exception object
496   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
497   llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
498 
499   SelectorArgs.push_back(Exc);
500   SelectorArgs.push_back(Personality);
501 
502   bool HasCatchAll = false;
503   for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
504     const CXXCatchStmt *C = S.getHandler(i);
505     VarDecl *CatchParam = C->getExceptionDecl();
506     if (CatchParam) {
507       // C++ [except.handle]p3 indicates that top-level cv-qualifiers
508       // are ignored.
509       QualType CaughtType = C->getCaughtType().getNonReferenceType();
510       llvm::Value *EHTypeInfo
511         = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
512       SelectorArgs.push_back(EHTypeInfo);
513     } else {
514       // null indicates catch all
515       SelectorArgs.push_back(Null);
516       HasCatchAll = true;
517     }
518   }
519 
520   // We use a cleanup unless there was already a catch all.
521   if (!HasCatchAll) {
522     SelectorArgs.push_back(Null);
523   }
524 
525   // Find which handler was matched.
526   llvm::Value *Selector
527     = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
528                          SelectorArgs.end(), "selector");
529   for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
530     const CXXCatchStmt *C = S.getHandler(i);
531     VarDecl *CatchParam = C->getExceptionDecl();
532     Stmt *CatchBody = C->getHandlerBlock();
533 
534     llvm::BasicBlock *Next = 0;
535 
536     if (SelectorArgs[i+2] != Null) {
537       llvm::BasicBlock *Match = createBasicBlock("match");
538       Next = createBasicBlock("catch.next");
539       const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
540       llvm::Value *Id
541         = Builder.CreateCall(llvm_eh_typeid_for,
542                              Builder.CreateBitCast(SelectorArgs[i+2],
543                                                    Int8PtrTy));
544       Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
545                            Match, Next);
546       EmitBlock(Match);
547     }
548 
549     llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
550     llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
551 
552     PushCleanupBlock(MatchEnd);
553     setInvokeDest(MatchHandler);
554 
555     llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
556 
557     {
558       CleanupScope CatchScope(*this);
559       // Bind the catch parameter if it exists.
560       if (CatchParam) {
561         QualType CatchType = CatchParam->getType().getNonReferenceType();
562         setInvokeDest(TerminateHandler);
563         bool WasPointer = true;
564         bool WasPointerReference = false;
565         CatchType = CGM.getContext().getCanonicalType(CatchType);
566         if (CatchType.getTypePtr()->isPointerType()) {
567           if (isa<ReferenceType>(CatchParam->getType()))
568             WasPointerReference = true;
569         } else {
570           if (!isa<ReferenceType>(CatchParam->getType()))
571             WasPointer = false;
572           CatchType = getContext().getPointerType(CatchType);
573         }
574         ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
575         EmitLocalBlockVarDecl(*CatchParam);
576         // FIXME: we need to do this sooner so that the EH region for the
577         // cleanup doesn't start until after the ctor completes, use a decl
578         // init?
579         CopyObject(*this, CatchParam->getType().getNonReferenceType(),
580                    WasPointer, WasPointerReference, ExcObject,
581                    GetAddrOfLocalVar(CatchParam));
582         setInvokeDest(MatchHandler);
583       }
584 
585       EmitStmt(CatchBody);
586     }
587 
588     EmitBranchThroughCleanup(FinallyEnd);
589 
590     EmitBlock(MatchHandler);
591 
592     llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
593     // We are required to emit this call to satisfy LLVM, even
594     // though we don't use the result.
595     llvm::Value *Args[] = {
596       Exc, Personality, llvm::ConstantInt::getNullValue(Int32Ty)
597     };
598     Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
599     Builder.CreateStore(Exc, RethrowPtr);
600     EmitBranchThroughCleanup(FinallyRethrow);
601 
602     CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
603 
604     EmitBlock(MatchEnd);
605 
606     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
607     Builder.CreateInvoke(getEndCatchFn(*this),
608                          Cont, TerminateHandler,
609                          &Args[0], &Args[0]);
610     EmitBlock(Cont);
611     if (Info.SwitchBlock)
612       EmitBlock(Info.SwitchBlock);
613     if (Info.EndBlock)
614       EmitBlock(Info.EndBlock);
615 
616     Exc = Builder.CreateCall(llvm_eh_exception, "exc");
617     Builder.CreateStore(Exc, RethrowPtr);
618     EmitBranchThroughCleanup(FinallyRethrow);
619 
620     if (Next)
621       EmitBlock(Next);
622   }
623   if (!HasCatchAll) {
624     Builder.CreateStore(Exc, RethrowPtr);
625     EmitBranchThroughCleanup(FinallyRethrow);
626   }
627 
628   CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
629 
630   setInvokeDest(PrevLandingPad);
631 
632   EmitBlock(FinallyBlock);
633 
634   if (Info.SwitchBlock)
635     EmitBlock(Info.SwitchBlock);
636   if (Info.EndBlock)
637     EmitBlock(Info.EndBlock);
638 
639   // Branch around the rethrow code.
640   EmitBranch(FinallyEnd);
641 
642   EmitBlock(FinallyRethrow);
643   // FIXME: Eventually we can chain the handlers together and just do a call
644   // here.
645   if (getInvokeDest()) {
646     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
647     Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
648                          getInvokeDest(),
649                          Builder.CreateLoad(RethrowPtr));
650     EmitBlock(Cont);
651   } else
652     Builder.CreateCall(getUnwindResumeOrRethrowFn(),
653                        Builder.CreateLoad(RethrowPtr));
654 
655   Builder.CreateUnreachable();
656 
657   EmitBlock(FinallyEnd);
658 }
659 
660 CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
661   CGF.setInvokeDest(PreviousInvokeDest);
662 
663   llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
664 
665   // Jump to the beginning of the cleanup.
666   CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
667 
668   // The libstdc++ personality function.
669   // TODO: generalize to work with other libraries.
670   llvm::Constant *Personality = getPersonalityFn(CGF.CGM);
671 
672   // %exception = call i8* @llvm.eh.exception()
673   //   Magic intrinsic which tells gives us a handle to the caught
674   //   exception.
675   llvm::Value *llvm_eh_exception =
676     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
677   llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
678 
679   llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
680 
681   // %ignored = call i32 @llvm.eh.selector(i8* %exception,
682   //                                       i8* @__gxx_personality_v0,
683   //                                       i8* null)
684   //   Magic intrinsic which tells LLVM that this invoke landing pad is
685   //   just a cleanup block.
686   llvm::Value *Args[] = { Exc, Personality, Null };
687   llvm::Value *llvm_eh_selector =
688     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
689   CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
690 
691   // And then we fall through into the code that the user put there.
692   // Jump back to the end of the cleanup.
693   CGF.Builder.SetInsertPoint(EndOfCleanup);
694 
695   // Rethrow the exception.
696   if (CGF.getInvokeDest()) {
697     llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
698     CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
699                              CGF.getInvokeDest(), Exc);
700     CGF.EmitBlock(Cont);
701   } else
702     CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
703   CGF.Builder.CreateUnreachable();
704 
705   // Resume inserting where we started, but put the new cleanup
706   // handler in place.
707   if (PreviousInsertionBlock)
708     CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
709   else
710     CGF.Builder.ClearInsertionPoint();
711 
712   if (CGF.Exceptions)
713     CGF.setInvokeDest(CleanupHandler);
714 }
715 
716 llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
717   if (TerminateHandler)
718     return TerminateHandler;
719 
720   // We don't want to change anything at the current location, so
721   // save it aside and clear the insert point.
722   llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
723   llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
724   Builder.ClearInsertionPoint();
725 
726   llvm::Constant *Personality = getPersonalityFn(CGM);
727   llvm::Value *llvm_eh_exception =
728     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
729   llvm::Value *llvm_eh_selector =
730     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
731 
732   // Set up terminate handler
733   TerminateHandler = createBasicBlock("terminate.handler");
734   EmitBlock(TerminateHandler);
735   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
736   // We are required to emit this call to satisfy LLVM, even
737   // though we don't use the result.
738   llvm::Value *Args[] = {
739     Exc, Personality, llvm::ConstantInt::get(Int32Ty, 1)
740   };
741   Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
742   llvm::CallInst *TerminateCall =
743     Builder.CreateCall(getTerminateFn(*this));
744   TerminateCall->setDoesNotReturn();
745   TerminateCall->setDoesNotThrow();
746   Builder.CreateUnreachable();
747 
748   // Restore the saved insertion state.
749   Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint);
750 
751   return TerminateHandler;
752 }
753