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 "CodeGenFunction.h"
15#include "CGCleanup.h"
16#include "CGObjCRuntime.h"
17#include "TargetInfo.h"
18#include "clang/AST/StmtCXX.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/Support/CallSite.h"
21
22using namespace clang;
23using namespace CodeGen;
24
25static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
26  // void *__cxa_allocate_exception(size_t thrown_size);
27
28  llvm::FunctionType *FTy =
29    llvm::FunctionType::get(CGF.Int8PtrTy, CGF.SizeTy, /*IsVarArgs=*/false);
30
31  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
34static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35  // void __cxa_free_exception(void *thrown_exception);
36
37  llvm::FunctionType *FTy =
38    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
39
40  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
41}
42
43static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
44  // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
45  //                  void (*dest) (void *));
46
47  llvm::Type *Args[3] = { CGF.Int8PtrTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
48  llvm::FunctionType *FTy =
49    llvm::FunctionType::get(CGF.VoidTy, Args, /*IsVarArgs=*/false);
50
51  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
52}
53
54static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
55  // void __cxa_rethrow();
56
57  llvm::FunctionType *FTy =
58    llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
59
60  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
61}
62
63static llvm::Constant *getGetExceptionPtrFn(CodeGenFunction &CGF) {
64  // void *__cxa_get_exception_ptr(void*);
65
66  llvm::FunctionType *FTy =
67    llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
68
69  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
70}
71
72static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
73  // void *__cxa_begin_catch(void*);
74
75  llvm::FunctionType *FTy =
76    llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
77
78  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
82  // void __cxa_end_catch();
83
84  llvm::FunctionType *FTy =
85    llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
86
87  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
90static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91  // void __cxa_call_unexepcted(void *thrown_exception);
92
93  llvm::FunctionType *FTy =
94    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
95
96  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
97}
98
99llvm::Constant *CodeGenFunction::getUnwindResumeFn() {
100  llvm::FunctionType *FTy =
101    llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
102
103  if (CGM.getLangOpts().SjLjExceptions)
104    return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
105  return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume");
106}
107
108llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
109  llvm::FunctionType *FTy =
110    llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
111
112  if (CGM.getLangOpts().SjLjExceptions)
113    return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume_or_Rethrow");
114  return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
115}
116
117static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
118  // void __terminate();
119
120  llvm::FunctionType *FTy =
121    llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
122
123  StringRef name;
124
125  // In C++, use std::terminate().
126  if (CGF.getLangOpts().CPlusPlus)
127    name = "_ZSt9terminatev"; // FIXME: mangling!
128  else if (CGF.getLangOpts().ObjC1 &&
129           CGF.getLangOpts().ObjCRuntime.hasTerminate())
130    name = "objc_terminate";
131  else
132    name = "abort";
133  return CGF.CGM.CreateRuntimeFunction(FTy, name);
134}
135
136static llvm::Constant *getCatchallRethrowFn(CodeGenFunction &CGF,
137                                            StringRef Name) {
138  llvm::FunctionType *FTy =
139    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
140
141  return CGF.CGM.CreateRuntimeFunction(FTy, Name);
142}
143
144namespace {
145  /// The exceptions personality for a function.
146  struct EHPersonality {
147    const char *PersonalityFn;
148
149    // If this is non-null, this personality requires a non-standard
150    // function for rethrowing an exception after a catchall cleanup.
151    // This function must have prototype void(void*).
152    const char *CatchallRethrowFn;
153
154    static const EHPersonality &get(const LangOptions &Lang);
155    static const EHPersonality GNU_C;
156    static const EHPersonality GNU_C_SJLJ;
157    static const EHPersonality GNU_ObjC;
158    static const EHPersonality GNU_ObjCXX;
159    static const EHPersonality NeXT_ObjC;
160    static const EHPersonality GNU_CPlusPlus;
161    static const EHPersonality GNU_CPlusPlus_SJLJ;
162  };
163}
164
165const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", 0 };
166const EHPersonality EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", 0 };
167const EHPersonality EHPersonality::NeXT_ObjC = { "__objc_personality_v0", 0 };
168const EHPersonality EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", 0};
169const EHPersonality
170EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", 0 };
171const EHPersonality
172EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
173const EHPersonality
174EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", 0 };
175
176static const EHPersonality &getCPersonality(const LangOptions &L) {
177  if (L.SjLjExceptions)
178    return EHPersonality::GNU_C_SJLJ;
179  return EHPersonality::GNU_C;
180}
181
182static const EHPersonality &getObjCPersonality(const LangOptions &L) {
183  switch (L.ObjCRuntime.getKind()) {
184  case ObjCRuntime::FragileMacOSX:
185    return getCPersonality(L);
186  case ObjCRuntime::MacOSX:
187  case ObjCRuntime::iOS:
188    return EHPersonality::NeXT_ObjC;
189  case ObjCRuntime::GNUstep:
190  case ObjCRuntime::GCC:
191  case ObjCRuntime::ObjFW:
192    return EHPersonality::GNU_ObjC;
193  }
194  llvm_unreachable("bad runtime kind");
195}
196
197static const EHPersonality &getCXXPersonality(const LangOptions &L) {
198  if (L.SjLjExceptions)
199    return EHPersonality::GNU_CPlusPlus_SJLJ;
200  else
201    return EHPersonality::GNU_CPlusPlus;
202}
203
204/// Determines the personality function to use when both C++
205/// and Objective-C exceptions are being caught.
206static const EHPersonality &getObjCXXPersonality(const LangOptions &L) {
207  switch (L.ObjCRuntime.getKind()) {
208  // The ObjC personality defers to the C++ personality for non-ObjC
209  // handlers.  Unlike the C++ case, we use the same personality
210  // function on targets using (backend-driven) SJLJ EH.
211  case ObjCRuntime::MacOSX:
212  case ObjCRuntime::iOS:
213    return EHPersonality::NeXT_ObjC;
214
215  // In the fragile ABI, just use C++ exception handling and hope
216  // they're not doing crazy exception mixing.
217  case ObjCRuntime::FragileMacOSX:
218    return getCXXPersonality(L);
219
220  // The GCC runtime's personality function inherently doesn't support
221  // mixed EH.  Use the C++ personality just to avoid returning null.
222  case ObjCRuntime::GCC:
223  case ObjCRuntime::ObjFW: // XXX: this will change soon
224    return EHPersonality::GNU_ObjC;
225  case ObjCRuntime::GNUstep:
226    return EHPersonality::GNU_ObjCXX;
227  }
228  llvm_unreachable("bad runtime kind");
229}
230
231const EHPersonality &EHPersonality::get(const LangOptions &L) {
232  if (L.CPlusPlus && L.ObjC1)
233    return getObjCXXPersonality(L);
234  else if (L.CPlusPlus)
235    return getCXXPersonality(L);
236  else if (L.ObjC1)
237    return getObjCPersonality(L);
238  else
239    return getCPersonality(L);
240}
241
242static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
243                                        const EHPersonality &Personality) {
244  llvm::Constant *Fn =
245    CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
246                              Personality.PersonalityFn);
247  return Fn;
248}
249
250static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
251                                        const EHPersonality &Personality) {
252  llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
253  return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
254}
255
256/// Check whether a personality function could reasonably be swapped
257/// for a C++ personality function.
258static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
259  for (llvm::Constant::use_iterator
260         I = Fn->use_begin(), E = Fn->use_end(); I != E; ++I) {
261    llvm::User *User = *I;
262
263    // Conditionally white-list bitcasts.
264    if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(User)) {
265      if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
266      if (!PersonalityHasOnlyCXXUses(CE))
267        return false;
268      continue;
269    }
270
271    // Otherwise, it has to be a landingpad instruction.
272    llvm::LandingPadInst *LPI = dyn_cast<llvm::LandingPadInst>(User);
273    if (!LPI) return false;
274
275    for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
276      // Look for something that would've been returned by the ObjC
277      // runtime's GetEHType() method.
278      llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
279      if (LPI->isCatch(I)) {
280        // Check if the catch value has the ObjC prefix.
281        if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
282          // ObjC EH selector entries are always global variables with
283          // names starting like this.
284          if (GV->getName().startswith("OBJC_EHTYPE"))
285            return false;
286      } else {
287        // Check if any of the filter values have the ObjC prefix.
288        llvm::Constant *CVal = cast<llvm::Constant>(Val);
289        for (llvm::User::op_iterator
290               II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
291          if (llvm::GlobalVariable *GV =
292              cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
293            // ObjC EH selector entries are always global variables with
294            // names starting like this.
295            if (GV->getName().startswith("OBJC_EHTYPE"))
296              return false;
297        }
298      }
299    }
300  }
301
302  return true;
303}
304
305/// Try to use the C++ personality function in ObjC++.  Not doing this
306/// can cause some incompatibilities with gcc, which is more
307/// aggressive about only using the ObjC++ personality in a function
308/// when it really needs it.
309void CodeGenModule::SimplifyPersonality() {
310  // For now, this is really a Darwin-specific operation.
311  if (!Context.getTargetInfo().getTriple().isOSDarwin())
312    return;
313
314  // If we're not in ObjC++ -fexceptions, there's nothing to do.
315  if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions)
316    return;
317
318  const EHPersonality &ObjCXX = EHPersonality::get(LangOpts);
319  const EHPersonality &CXX = getCXXPersonality(LangOpts);
320  if (&ObjCXX == &CXX)
321    return;
322
323  assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
324         "Different EHPersonalities using the same personality function.");
325
326  llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
327
328  // Nothing to do if it's unused.
329  if (!Fn || Fn->use_empty()) return;
330
331  // Can't do the optimization if it has non-C++ uses.
332  if (!PersonalityHasOnlyCXXUses(Fn)) return;
333
334  // Create the C++ personality function and kill off the old
335  // function.
336  llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
337
338  // This can happen if the user is screwing with us.
339  if (Fn->getType() != CXXFn->getType()) return;
340
341  Fn->replaceAllUsesWith(CXXFn);
342  Fn->eraseFromParent();
343}
344
345/// Returns the value to inject into a selector to indicate the
346/// presence of a catch-all.
347static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
348  // Possibly we should use @llvm.eh.catch.all.value here.
349  return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
350}
351
352namespace {
353  /// A cleanup to free the exception object if its initialization
354  /// throws.
355  struct FreeException : EHScopeStack::Cleanup {
356    llvm::Value *exn;
357    FreeException(llvm::Value *exn) : exn(exn) {}
358    void Emit(CodeGenFunction &CGF, Flags flags) {
359      CGF.Builder.CreateCall(getFreeExceptionFn(CGF), exn)
360        ->setDoesNotThrow();
361    }
362  };
363}
364
365// Emits an exception expression into the given location.  This
366// differs from EmitAnyExprToMem only in that, if a final copy-ctor
367// call is required, an exception within that copy ctor causes
368// std::terminate to be invoked.
369static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
370                             llvm::Value *addr) {
371  // Make sure the exception object is cleaned up if there's an
372  // exception during initialization.
373  CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
374  EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
375
376  // __cxa_allocate_exception returns a void*;  we need to cast this
377  // to the appropriate type for the object.
378  llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
379  llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
380
381  // FIXME: this isn't quite right!  If there's a final unelided call
382  // to a copy constructor, then according to [except.terminate]p1 we
383  // must call std::terminate() if that constructor throws, because
384  // technically that copy occurs after the exception expression is
385  // evaluated but before the exception is caught.  But the best way
386  // to handle that is to teach EmitAggExpr to do the final copy
387  // differently if it can't be elided.
388  CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
389                       /*IsInit*/ true);
390
391  // Deactivate the cleanup block.
392  CGF.DeactivateCleanupBlock(cleanup, cast<llvm::Instruction>(typedAddr));
393}
394
395llvm::Value *CodeGenFunction::getExceptionSlot() {
396  if (!ExceptionSlot)
397    ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
398  return ExceptionSlot;
399}
400
401llvm::Value *CodeGenFunction::getEHSelectorSlot() {
402  if (!EHSelectorSlot)
403    EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
404  return EHSelectorSlot;
405}
406
407llvm::Value *CodeGenFunction::getExceptionFromSlot() {
408  return Builder.CreateLoad(getExceptionSlot(), "exn");
409}
410
411llvm::Value *CodeGenFunction::getSelectorFromSlot() {
412  return Builder.CreateLoad(getEHSelectorSlot(), "sel");
413}
414
415void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
416  if (!E->getSubExpr()) {
417    if (getInvokeDest()) {
418      Builder.CreateInvoke(getReThrowFn(*this),
419                           getUnreachableBlock(),
420                           getInvokeDest())
421        ->setDoesNotReturn();
422    } else {
423      Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
424      Builder.CreateUnreachable();
425    }
426
427    // throw is an expression, and the expression emitters expect us
428    // to leave ourselves at a valid insertion point.
429    EmitBlock(createBasicBlock("throw.cont"));
430
431    return;
432  }
433
434  QualType ThrowType = E->getSubExpr()->getType();
435
436  // Now allocate the exception object.
437  llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
438  uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
439
440  llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
441  llvm::CallInst *ExceptionPtr =
442    Builder.CreateCall(AllocExceptionFn,
443                       llvm::ConstantInt::get(SizeTy, TypeSize),
444                       "exception");
445  ExceptionPtr->setDoesNotThrow();
446
447  EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
448
449  // Now throw the exception.
450  llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
451                                                         /*ForEH=*/true);
452
453  // The address of the destructor.  If the exception type has a
454  // trivial destructor (or isn't a record), we just pass null.
455  llvm::Constant *Dtor = 0;
456  if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
457    CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
458    if (!Record->hasTrivialDestructor()) {
459      CXXDestructorDecl *DtorD = Record->getDestructor();
460      Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
461      Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
462    }
463  }
464  if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
465
466  if (getInvokeDest()) {
467    llvm::InvokeInst *ThrowCall =
468      Builder.CreateInvoke3(getThrowFn(*this),
469                            getUnreachableBlock(), getInvokeDest(),
470                            ExceptionPtr, TypeInfo, Dtor);
471    ThrowCall->setDoesNotReturn();
472  } else {
473    llvm::CallInst *ThrowCall =
474      Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
475    ThrowCall->setDoesNotReturn();
476    Builder.CreateUnreachable();
477  }
478
479  // throw is an expression, and the expression emitters expect us
480  // to leave ourselves at a valid insertion point.
481  EmitBlock(createBasicBlock("throw.cont"));
482}
483
484void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
485  if (!CGM.getLangOpts().CXXExceptions)
486    return;
487
488  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
489  if (FD == 0)
490    return;
491  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
492  if (Proto == 0)
493    return;
494
495  ExceptionSpecificationType EST = Proto->getExceptionSpecType();
496  if (isNoexceptExceptionSpec(EST)) {
497    if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
498      // noexcept functions are simple terminate scopes.
499      EHStack.pushTerminate();
500    }
501  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
502    unsigned NumExceptions = Proto->getNumExceptions();
503    EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
504
505    for (unsigned I = 0; I != NumExceptions; ++I) {
506      QualType Ty = Proto->getExceptionType(I);
507      QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
508      llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
509                                                        /*ForEH=*/true);
510      Filter->setFilter(I, EHType);
511    }
512  }
513}
514
515/// Emit the dispatch block for a filter scope if necessary.
516static void emitFilterDispatchBlock(CodeGenFunction &CGF,
517                                    EHFilterScope &filterScope) {
518  llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
519  if (!dispatchBlock) return;
520  if (dispatchBlock->use_empty()) {
521    delete dispatchBlock;
522    return;
523  }
524
525  CGF.EmitBlockAfterUses(dispatchBlock);
526
527  // If this isn't a catch-all filter, we need to check whether we got
528  // here because the filter triggered.
529  if (filterScope.getNumFilters()) {
530    // Load the selector value.
531    llvm::Value *selector = CGF.getSelectorFromSlot();
532    llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
533
534    llvm::Value *zero = CGF.Builder.getInt32(0);
535    llvm::Value *failsFilter =
536      CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
537    CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, CGF.getEHResumeBlock());
538
539    CGF.EmitBlock(unexpectedBB);
540  }
541
542  // Call __cxa_call_unexpected.  This doesn't need to be an invoke
543  // because __cxa_call_unexpected magically filters exceptions
544  // according to the last landing pad the exception was thrown
545  // into.  Seriously.
546  llvm::Value *exn = CGF.getExceptionFromSlot();
547  CGF.Builder.CreateCall(getUnexpectedFn(CGF), exn)
548    ->setDoesNotReturn();
549  CGF.Builder.CreateUnreachable();
550}
551
552void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
553  if (!CGM.getLangOpts().CXXExceptions)
554    return;
555
556  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
557  if (FD == 0)
558    return;
559  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
560  if (Proto == 0)
561    return;
562
563  ExceptionSpecificationType EST = Proto->getExceptionSpecType();
564  if (isNoexceptExceptionSpec(EST)) {
565    if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
566      EHStack.popTerminate();
567    }
568  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
569    EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
570    emitFilterDispatchBlock(*this, filterScope);
571    EHStack.popFilter();
572  }
573}
574
575void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
576  EnterCXXTryStmt(S);
577  EmitStmt(S.getTryBlock());
578  ExitCXXTryStmt(S);
579}
580
581void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
582  unsigned NumHandlers = S.getNumHandlers();
583  EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
584
585  for (unsigned I = 0; I != NumHandlers; ++I) {
586    const CXXCatchStmt *C = S.getHandler(I);
587
588    llvm::BasicBlock *Handler = createBasicBlock("catch");
589    if (C->getExceptionDecl()) {
590      // FIXME: Dropping the reference type on the type into makes it
591      // impossible to correctly implement catch-by-reference
592      // semantics for pointers.  Unfortunately, this is what all
593      // existing compilers do, and it's not clear that the standard
594      // personality routine is capable of doing this right.  See C++ DR 388:
595      //   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
596      QualType CaughtType = C->getCaughtType();
597      CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType();
598
599      llvm::Value *TypeInfo = 0;
600      if (CaughtType->isObjCObjectPointerType())
601        TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
602      else
603        TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
604      CatchScope->setHandler(I, TypeInfo, Handler);
605    } else {
606      // No exception decl indicates '...', a catch-all.
607      CatchScope->setCatchAllHandler(I, Handler);
608    }
609  }
610}
611
612llvm::BasicBlock *
613CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
614  // The dispatch block for the end of the scope chain is a block that
615  // just resumes unwinding.
616  if (si == EHStack.stable_end())
617    return getEHResumeBlock();
618
619  // Otherwise, we should look at the actual scope.
620  EHScope &scope = *EHStack.find(si);
621
622  llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
623  if (!dispatchBlock) {
624    switch (scope.getKind()) {
625    case EHScope::Catch: {
626      // Apply a special case to a single catch-all.
627      EHCatchScope &catchScope = cast<EHCatchScope>(scope);
628      if (catchScope.getNumHandlers() == 1 &&
629          catchScope.getHandler(0).isCatchAll()) {
630        dispatchBlock = catchScope.getHandler(0).Block;
631
632      // Otherwise, make a dispatch block.
633      } else {
634        dispatchBlock = createBasicBlock("catch.dispatch");
635      }
636      break;
637    }
638
639    case EHScope::Cleanup:
640      dispatchBlock = createBasicBlock("ehcleanup");
641      break;
642
643    case EHScope::Filter:
644      dispatchBlock = createBasicBlock("filter.dispatch");
645      break;
646
647    case EHScope::Terminate:
648      dispatchBlock = getTerminateHandler();
649      break;
650    }
651    scope.setCachedEHDispatchBlock(dispatchBlock);
652  }
653  return dispatchBlock;
654}
655
656/// Check whether this is a non-EH scope, i.e. a scope which doesn't
657/// affect exception handling.  Currently, the only non-EH scopes are
658/// normal-only cleanup scopes.
659static bool isNonEHScope(const EHScope &S) {
660  switch (S.getKind()) {
661  case EHScope::Cleanup:
662    return !cast<EHCleanupScope>(S).isEHCleanup();
663  case EHScope::Filter:
664  case EHScope::Catch:
665  case EHScope::Terminate:
666    return false;
667  }
668
669  llvm_unreachable("Invalid EHScope Kind!");
670}
671
672llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
673  assert(EHStack.requiresLandingPad());
674  assert(!EHStack.empty());
675
676  if (!CGM.getLangOpts().Exceptions)
677    return 0;
678
679  // Check the innermost scope for a cached landing pad.  If this is
680  // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
681  llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
682  if (LP) return LP;
683
684  // Build the landing pad for this scope.
685  LP = EmitLandingPad();
686  assert(LP);
687
688  // Cache the landing pad on the innermost scope.  If this is a
689  // non-EH scope, cache the landing pad on the enclosing scope, too.
690  for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
691    ir->setCachedLandingPad(LP);
692    if (!isNonEHScope(*ir)) break;
693  }
694
695  return LP;
696}
697
698// This code contains a hack to work around a design flaw in
699// LLVM's EH IR which breaks semantics after inlining.  This same
700// hack is implemented in llvm-gcc.
701//
702// The LLVM EH abstraction is basically a thin veneer over the
703// traditional GCC zero-cost design: for each range of instructions
704// in the function, there is (at most) one "landing pad" with an
705// associated chain of EH actions.  A language-specific personality
706// function interprets this chain of actions and (1) decides whether
707// or not to resume execution at the landing pad and (2) if so,
708// provides an integer indicating why it's stopping.  In LLVM IR,
709// the association of a landing pad with a range of instructions is
710// achieved via an invoke instruction, the chain of actions becomes
711// the arguments to the @llvm.eh.selector call, and the selector
712// call returns the integer indicator.  Other than the required
713// presence of two intrinsic function calls in the landing pad,
714// the IR exactly describes the layout of the output code.
715//
716// A principal advantage of this design is that it is completely
717// language-agnostic; in theory, the LLVM optimizers can treat
718// landing pads neutrally, and targets need only know how to lower
719// the intrinsics to have a functioning exceptions system (assuming
720// that platform exceptions follow something approximately like the
721// GCC design).  Unfortunately, landing pads cannot be combined in a
722// language-agnostic way: given selectors A and B, there is no way
723// to make a single landing pad which faithfully represents the
724// semantics of propagating an exception first through A, then
725// through B, without knowing how the personality will interpret the
726// (lowered form of the) selectors.  This means that inlining has no
727// choice but to crudely chain invokes (i.e., to ignore invokes in
728// the inlined function, but to turn all unwindable calls into
729// invokes), which is only semantically valid if every unwind stops
730// at every landing pad.
731//
732// Therefore, the invoke-inline hack is to guarantee that every
733// landing pad has a catch-all.
734enum CleanupHackLevel_t {
735  /// A level of hack that requires that all landing pads have
736  /// catch-alls.
737  CHL_MandatoryCatchall,
738
739  /// A level of hack that requires that all landing pads handle
740  /// cleanups.
741  CHL_MandatoryCleanup,
742
743  /// No hacks at all;  ideal IR generation.
744  CHL_Ideal
745};
746const CleanupHackLevel_t CleanupHackLevel = CHL_MandatoryCleanup;
747
748llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
749  assert(EHStack.requiresLandingPad());
750
751  EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
752  switch (innermostEHScope.getKind()) {
753  case EHScope::Terminate:
754    return getTerminateLandingPad();
755
756  case EHScope::Catch:
757  case EHScope::Cleanup:
758  case EHScope::Filter:
759    if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
760      return lpad;
761  }
762
763  // Save the current IR generation state.
764  CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
765
766  const EHPersonality &personality = EHPersonality::get(getLangOpts());
767
768  // Create and configure the landing pad.
769  llvm::BasicBlock *lpad = createBasicBlock("lpad");
770  EmitBlock(lpad);
771
772  llvm::LandingPadInst *LPadInst =
773    Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
774                             getOpaquePersonalityFn(CGM, personality), 0);
775
776  llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
777  Builder.CreateStore(LPadExn, getExceptionSlot());
778  llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
779  Builder.CreateStore(LPadSel, getEHSelectorSlot());
780
781  // Save the exception pointer.  It's safe to use a single exception
782  // pointer per function because EH cleanups can never have nested
783  // try/catches.
784  // Build the landingpad instruction.
785
786  // Accumulate all the handlers in scope.
787  bool hasCatchAll = false;
788  bool hasCleanup = false;
789  bool hasFilter = false;
790  SmallVector<llvm::Value*, 4> filterTypes;
791  llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
792  for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
793         I != E; ++I) {
794
795    switch (I->getKind()) {
796    case EHScope::Cleanup:
797      // If we have a cleanup, remember that.
798      hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
799      continue;
800
801    case EHScope::Filter: {
802      assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
803      assert(!hasCatchAll && "EH filter reached after catch-all");
804
805      // Filter scopes get added to the landingpad in weird ways.
806      EHFilterScope &filter = cast<EHFilterScope>(*I);
807      hasFilter = true;
808
809      // Add all the filter values.
810      for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
811        filterTypes.push_back(filter.getFilter(i));
812      goto done;
813    }
814
815    case EHScope::Terminate:
816      // Terminate scopes are basically catch-alls.
817      assert(!hasCatchAll);
818      hasCatchAll = true;
819      goto done;
820
821    case EHScope::Catch:
822      break;
823    }
824
825    EHCatchScope &catchScope = cast<EHCatchScope>(*I);
826    for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
827      EHCatchScope::Handler handler = catchScope.getHandler(hi);
828
829      // If this is a catch-all, register that and abort.
830      if (!handler.Type) {
831        assert(!hasCatchAll);
832        hasCatchAll = true;
833        goto done;
834      }
835
836      // Check whether we already have a handler for this type.
837      if (catchTypes.insert(handler.Type))
838        // If not, add it directly to the landingpad.
839        LPadInst->addClause(handler.Type);
840    }
841  }
842
843 done:
844  // If we have a catch-all, add null to the landingpad.
845  assert(!(hasCatchAll && hasFilter));
846  if (hasCatchAll) {
847    LPadInst->addClause(getCatchAllValue(*this));
848
849  // If we have an EH filter, we need to add those handlers in the
850  // right place in the landingpad, which is to say, at the end.
851  } else if (hasFilter) {
852    // Create a filter expression: a constant array indicating which filter
853    // types there are. The personality routine only lands here if the filter
854    // doesn't match.
855    llvm::SmallVector<llvm::Constant*, 8> Filters;
856    llvm::ArrayType *AType =
857      llvm::ArrayType::get(!filterTypes.empty() ?
858                             filterTypes[0]->getType() : Int8PtrTy,
859                           filterTypes.size());
860
861    for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
862      Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
863    llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
864    LPadInst->addClause(FilterArray);
865
866    // Also check whether we need a cleanup.
867    if (hasCleanup)
868      LPadInst->setCleanup(true);
869
870  // Otherwise, signal that we at least have cleanups.
871  } else if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup) {
872    if (CleanupHackLevel == CHL_MandatoryCatchall)
873      LPadInst->addClause(getCatchAllValue(*this));
874    else
875      LPadInst->setCleanup(true);
876  }
877
878  assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
879         "landingpad instruction has no clauses!");
880
881  // Tell the backend how to generate the landing pad.
882  Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
883
884  // Restore the old IR generation state.
885  Builder.restoreIP(savedIP);
886
887  return lpad;
888}
889
890namespace {
891  /// A cleanup to call __cxa_end_catch.  In many cases, the caught
892  /// exception type lets us state definitively that the thrown exception
893  /// type does not have a destructor.  In particular:
894  ///   - Catch-alls tell us nothing, so we have to conservatively
895  ///     assume that the thrown exception might have a destructor.
896  ///   - Catches by reference behave according to their base types.
897  ///   - Catches of non-record types will only trigger for exceptions
898  ///     of non-record types, which never have destructors.
899  ///   - Catches of record types can trigger for arbitrary subclasses
900  ///     of the caught type, so we have to assume the actual thrown
901  ///     exception type might have a throwing destructor, even if the
902  ///     caught type's destructor is trivial or nothrow.
903  struct CallEndCatch : EHScopeStack::Cleanup {
904    CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
905    bool MightThrow;
906
907    void Emit(CodeGenFunction &CGF, Flags flags) {
908      if (!MightThrow) {
909        CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow();
910        return;
911      }
912
913      CGF.EmitCallOrInvoke(getEndCatchFn(CGF));
914    }
915  };
916}
917
918/// Emits a call to __cxa_begin_catch and enters a cleanup to call
919/// __cxa_end_catch.
920///
921/// \param EndMightThrow - true if __cxa_end_catch might throw
922static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
923                                   llvm::Value *Exn,
924                                   bool EndMightThrow) {
925  llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn);
926  Call->setDoesNotThrow();
927
928  CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
929
930  return Call;
931}
932
933/// A "special initializer" callback for initializing a catch
934/// parameter during catch initialization.
935static void InitCatchParam(CodeGenFunction &CGF,
936                           const VarDecl &CatchParam,
937                           llvm::Value *ParamAddr) {
938  // Load the exception from where the landing pad saved it.
939  llvm::Value *Exn = CGF.getExceptionFromSlot();
940
941  CanQualType CatchType =
942    CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
943  llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
944
945  // If we're catching by reference, we can just cast the object
946  // pointer to the appropriate pointer.
947  if (isa<ReferenceType>(CatchType)) {
948    QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
949    bool EndCatchMightThrow = CaughtType->isRecordType();
950
951    // __cxa_begin_catch returns the adjusted object pointer.
952    llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
953
954    // We have no way to tell the personality function that we're
955    // catching by reference, so if we're catching a pointer,
956    // __cxa_begin_catch will actually return that pointer by value.
957    if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
958      QualType PointeeType = PT->getPointeeType();
959
960      // When catching by reference, generally we should just ignore
961      // this by-value pointer and use the exception object instead.
962      if (!PointeeType->isRecordType()) {
963
964        // Exn points to the struct _Unwind_Exception header, which
965        // we have to skip past in order to reach the exception data.
966        unsigned HeaderSize =
967          CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
968        AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
969
970      // However, if we're catching a pointer-to-record type that won't
971      // work, because the personality function might have adjusted
972      // the pointer.  There's actually no way for us to fully satisfy
973      // the language/ABI contract here:  we can't use Exn because it
974      // might have the wrong adjustment, but we can't use the by-value
975      // pointer because it's off by a level of abstraction.
976      //
977      // The current solution is to dump the adjusted pointer into an
978      // alloca, which breaks language semantics (because changing the
979      // pointer doesn't change the exception) but at least works.
980      // The better solution would be to filter out non-exact matches
981      // and rethrow them, but this is tricky because the rethrow
982      // really needs to be catchable by other sites at this landing
983      // pad.  The best solution is to fix the personality function.
984      } else {
985        // Pull the pointer for the reference type off.
986        llvm::Type *PtrTy =
987          cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
988
989        // Create the temporary and write the adjusted pointer into it.
990        llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
991        llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
992        CGF.Builder.CreateStore(Casted, ExnPtrTmp);
993
994        // Bind the reference to the temporary.
995        AdjustedExn = ExnPtrTmp;
996      }
997    }
998
999    llvm::Value *ExnCast =
1000      CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
1001    CGF.Builder.CreateStore(ExnCast, ParamAddr);
1002    return;
1003  }
1004
1005  // Non-aggregates (plus complexes).
1006  bool IsComplex = false;
1007  if (!CGF.hasAggregateLLVMType(CatchType) ||
1008      (IsComplex = CatchType->isAnyComplexType())) {
1009    llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
1010
1011    // If the catch type is a pointer type, __cxa_begin_catch returns
1012    // the pointer by value.
1013    if (CatchType->hasPointerRepresentation()) {
1014      llvm::Value *CastExn =
1015        CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
1016
1017      switch (CatchType.getQualifiers().getObjCLifetime()) {
1018      case Qualifiers::OCL_Strong:
1019        CastExn = CGF.EmitARCRetainNonBlock(CastExn);
1020        // fallthrough
1021
1022      case Qualifiers::OCL_None:
1023      case Qualifiers::OCL_ExplicitNone:
1024      case Qualifiers::OCL_Autoreleasing:
1025        CGF.Builder.CreateStore(CastExn, ParamAddr);
1026        return;
1027
1028      case Qualifiers::OCL_Weak:
1029        CGF.EmitARCInitWeak(ParamAddr, CastExn);
1030        return;
1031      }
1032      llvm_unreachable("bad ownership qualifier!");
1033    }
1034
1035    // Otherwise, it returns a pointer into the exception object.
1036
1037    llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
1038    llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
1039
1040    if (IsComplex) {
1041      CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false),
1042                             ParamAddr, /*volatile*/ false);
1043    } else {
1044      unsigned Alignment =
1045        CGF.getContext().getDeclAlign(&CatchParam).getQuantity();
1046      llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar");
1047      CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, Alignment,
1048                            CatchType);
1049    }
1050    return;
1051  }
1052
1053  assert(isa<RecordType>(CatchType) && "unexpected catch type!");
1054
1055  llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
1056
1057  // Check for a copy expression.  If we don't have a copy expression,
1058  // that means a trivial copy is okay.
1059  const Expr *copyExpr = CatchParam.getInit();
1060  if (!copyExpr) {
1061    llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
1062    llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1063    CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
1064    return;
1065  }
1066
1067  // We have to call __cxa_get_exception_ptr to get the adjusted
1068  // pointer before copying.
1069  llvm::CallInst *rawAdjustedExn =
1070    CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn);
1071  rawAdjustedExn->setDoesNotThrow();
1072
1073  // Cast that to the appropriate type.
1074  llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1075
1076  // The copy expression is defined in terms of an OpaqueValueExpr.
1077  // Find it and map it to the adjusted expression.
1078  CodeGenFunction::OpaqueValueMapping
1079    opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
1080           CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
1081
1082  // Call the copy ctor in a terminate scope.
1083  CGF.EHStack.pushTerminate();
1084
1085  // Perform the copy construction.
1086  CharUnits Alignment = CGF.getContext().getDeclAlign(&CatchParam);
1087  CGF.EmitAggExpr(copyExpr,
1088                  AggValueSlot::forAddr(ParamAddr, Alignment, Qualifiers(),
1089                                        AggValueSlot::IsNotDestructed,
1090                                        AggValueSlot::DoesNotNeedGCBarriers,
1091                                        AggValueSlot::IsNotAliased));
1092
1093  // Leave the terminate scope.
1094  CGF.EHStack.popTerminate();
1095
1096  // Undo the opaque value mapping.
1097  opaque.pop();
1098
1099  // Finally we can call __cxa_begin_catch.
1100  CallBeginCatch(CGF, Exn, true);
1101}
1102
1103/// Begins a catch statement by initializing the catch variable and
1104/// calling __cxa_begin_catch.
1105static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
1106  // We have to be very careful with the ordering of cleanups here:
1107  //   C++ [except.throw]p4:
1108  //     The destruction [of the exception temporary] occurs
1109  //     immediately after the destruction of the object declared in
1110  //     the exception-declaration in the handler.
1111  //
1112  // So the precise ordering is:
1113  //   1.  Construct catch variable.
1114  //   2.  __cxa_begin_catch
1115  //   3.  Enter __cxa_end_catch cleanup
1116  //   4.  Enter dtor cleanup
1117  //
1118  // We do this by using a slightly abnormal initialization process.
1119  // Delegation sequence:
1120  //   - ExitCXXTryStmt opens a RunCleanupsScope
1121  //     - EmitAutoVarAlloca creates the variable and debug info
1122  //       - InitCatchParam initializes the variable from the exception
1123  //       - CallBeginCatch calls __cxa_begin_catch
1124  //       - CallBeginCatch enters the __cxa_end_catch cleanup
1125  //     - EmitAutoVarCleanups enters the variable destructor cleanup
1126  //   - EmitCXXTryStmt emits the code for the catch body
1127  //   - EmitCXXTryStmt close the RunCleanupsScope
1128
1129  VarDecl *CatchParam = S->getExceptionDecl();
1130  if (!CatchParam) {
1131    llvm::Value *Exn = CGF.getExceptionFromSlot();
1132    CallBeginCatch(CGF, Exn, true);
1133    return;
1134  }
1135
1136  // Emit the local.
1137  CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
1138  InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
1139  CGF.EmitAutoVarCleanups(var);
1140}
1141
1142/// Emit the structure of the dispatch block for the given catch scope.
1143/// It is an invariant that the dispatch block already exists.
1144static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1145                                   EHCatchScope &catchScope) {
1146  llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1147  assert(dispatchBlock);
1148
1149  // If there's only a single catch-all, getEHDispatchBlock returned
1150  // that catch-all as the dispatch block.
1151  if (catchScope.getNumHandlers() == 1 &&
1152      catchScope.getHandler(0).isCatchAll()) {
1153    assert(dispatchBlock == catchScope.getHandler(0).Block);
1154    return;
1155  }
1156
1157  CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1158  CGF.EmitBlockAfterUses(dispatchBlock);
1159
1160  // Select the right handler.
1161  llvm::Value *llvm_eh_typeid_for =
1162    CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1163
1164  // Load the selector value.
1165  llvm::Value *selector = CGF.getSelectorFromSlot();
1166
1167  // Test against each of the exception types we claim to catch.
1168  for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1169    assert(i < e && "ran off end of handlers!");
1170    const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1171
1172    llvm::Value *typeValue = handler.Type;
1173    assert(typeValue && "fell into catch-all case!");
1174    typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1175
1176    // Figure out the next block.
1177    bool nextIsEnd;
1178    llvm::BasicBlock *nextBlock;
1179
1180    // If this is the last handler, we're at the end, and the next
1181    // block is the block for the enclosing EH scope.
1182    if (i + 1 == e) {
1183      nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1184      nextIsEnd = true;
1185
1186    // If the next handler is a catch-all, we're at the end, and the
1187    // next block is that handler.
1188    } else if (catchScope.getHandler(i+1).isCatchAll()) {
1189      nextBlock = catchScope.getHandler(i+1).Block;
1190      nextIsEnd = true;
1191
1192    // Otherwise, we're not at the end and we need a new block.
1193    } else {
1194      nextBlock = CGF.createBasicBlock("catch.fallthrough");
1195      nextIsEnd = false;
1196    }
1197
1198    // Figure out the catch type's index in the LSDA's type table.
1199    llvm::CallInst *typeIndex =
1200      CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1201    typeIndex->setDoesNotThrow();
1202
1203    llvm::Value *matchesTypeIndex =
1204      CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1205    CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1206
1207    // If the next handler is a catch-all, we're completely done.
1208    if (nextIsEnd) {
1209      CGF.Builder.restoreIP(savedIP);
1210      return;
1211    }
1212    // Otherwise we need to emit and continue at that block.
1213    CGF.EmitBlock(nextBlock);
1214  }
1215}
1216
1217void CodeGenFunction::popCatchScope() {
1218  EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1219  if (catchScope.hasEHBranches())
1220    emitCatchDispatchBlock(*this, catchScope);
1221  EHStack.popCatch();
1222}
1223
1224void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
1225  unsigned NumHandlers = S.getNumHandlers();
1226  EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1227  assert(CatchScope.getNumHandlers() == NumHandlers);
1228
1229  // If the catch was not required, bail out now.
1230  if (!CatchScope.hasEHBranches()) {
1231    EHStack.popCatch();
1232    return;
1233  }
1234
1235  // Emit the structure of the EH dispatch for this catch.
1236  emitCatchDispatchBlock(*this, CatchScope);
1237
1238  // Copy the handler blocks off before we pop the EH stack.  Emitting
1239  // the handlers might scribble on this memory.
1240  SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
1241  memcpy(Handlers.data(), CatchScope.begin(),
1242         NumHandlers * sizeof(EHCatchScope::Handler));
1243
1244  EHStack.popCatch();
1245
1246  // The fall-through block.
1247  llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
1248
1249  // We just emitted the body of the try; jump to the continue block.
1250  if (HaveInsertPoint())
1251    Builder.CreateBr(ContBB);
1252
1253  // Determine if we need an implicit rethrow for all these catch handlers;
1254  // see the comment below.
1255  bool doImplicitRethrow = false;
1256  if (IsFnTryBlock)
1257    doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1258                        isa<CXXConstructorDecl>(CurCodeDecl);
1259
1260  // Perversely, we emit the handlers backwards precisely because we
1261  // want them to appear in source order.  In all of these cases, the
1262  // catch block will have exactly one predecessor, which will be a
1263  // particular block in the catch dispatch.  However, in the case of
1264  // a catch-all, one of the dispatch blocks will branch to two
1265  // different handlers, and EmitBlockAfterUses will cause the second
1266  // handler to be moved before the first.
1267  for (unsigned I = NumHandlers; I != 0; --I) {
1268    llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1269    EmitBlockAfterUses(CatchBlock);
1270
1271    // Catch the exception if this isn't a catch-all.
1272    const CXXCatchStmt *C = S.getHandler(I-1);
1273
1274    // Enter a cleanup scope, including the catch variable and the
1275    // end-catch.
1276    RunCleanupsScope CatchScope(*this);
1277
1278    // Initialize the catch variable and set up the cleanups.
1279    BeginCatch(*this, C);
1280
1281    // Perform the body of the catch.
1282    EmitStmt(C->getHandlerBlock());
1283
1284    // [except.handle]p11:
1285    //   The currently handled exception is rethrown if control
1286    //   reaches the end of a handler of the function-try-block of a
1287    //   constructor or destructor.
1288
1289    // It is important that we only do this on fallthrough and not on
1290    // return.  Note that it's illegal to put a return in a
1291    // constructor function-try-block's catch handler (p14), so this
1292    // really only applies to destructors.
1293    if (doImplicitRethrow && HaveInsertPoint()) {
1294      EmitCallOrInvoke(getReThrowFn(*this));
1295      Builder.CreateUnreachable();
1296      Builder.ClearInsertionPoint();
1297    }
1298
1299    // Fall out through the catch cleanups.
1300    CatchScope.ForceCleanup();
1301
1302    // Branch out of the try.
1303    if (HaveInsertPoint())
1304      Builder.CreateBr(ContBB);
1305  }
1306
1307  EmitBlock(ContBB);
1308}
1309
1310namespace {
1311  struct CallEndCatchForFinally : EHScopeStack::Cleanup {
1312    llvm::Value *ForEHVar;
1313    llvm::Value *EndCatchFn;
1314    CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1315      : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1316
1317    void Emit(CodeGenFunction &CGF, Flags flags) {
1318      llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1319      llvm::BasicBlock *CleanupContBB =
1320        CGF.createBasicBlock("finally.cleanup.cont");
1321
1322      llvm::Value *ShouldEndCatch =
1323        CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
1324      CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1325      CGF.EmitBlock(EndCatchBB);
1326      CGF.EmitCallOrInvoke(EndCatchFn); // catch-all, so might throw
1327      CGF.EmitBlock(CleanupContBB);
1328    }
1329  };
1330
1331  struct PerformFinally : EHScopeStack::Cleanup {
1332    const Stmt *Body;
1333    llvm::Value *ForEHVar;
1334    llvm::Value *EndCatchFn;
1335    llvm::Value *RethrowFn;
1336    llvm::Value *SavedExnVar;
1337
1338    PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1339                   llvm::Value *EndCatchFn,
1340                   llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1341      : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1342        RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1343
1344    void Emit(CodeGenFunction &CGF, Flags flags) {
1345      // Enter a cleanup to call the end-catch function if one was provided.
1346      if (EndCatchFn)
1347        CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1348                                                        ForEHVar, EndCatchFn);
1349
1350      // Save the current cleanup destination in case there are
1351      // cleanups in the finally block.
1352      llvm::Value *SavedCleanupDest =
1353        CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1354                               "cleanup.dest.saved");
1355
1356      // Emit the finally block.
1357      CGF.EmitStmt(Body);
1358
1359      // If the end of the finally is reachable, check whether this was
1360      // for EH.  If so, rethrow.
1361      if (CGF.HaveInsertPoint()) {
1362        llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1363        llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1364
1365        llvm::Value *ShouldRethrow =
1366          CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
1367        CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1368
1369        CGF.EmitBlock(RethrowBB);
1370        if (SavedExnVar) {
1371          CGF.EmitCallOrInvoke(RethrowFn, CGF.Builder.CreateLoad(SavedExnVar));
1372        } else {
1373          CGF.EmitCallOrInvoke(RethrowFn);
1374        }
1375        CGF.Builder.CreateUnreachable();
1376
1377        CGF.EmitBlock(ContBB);
1378
1379        // Restore the cleanup destination.
1380        CGF.Builder.CreateStore(SavedCleanupDest,
1381                                CGF.getNormalCleanupDestSlot());
1382      }
1383
1384      // Leave the end-catch cleanup.  As an optimization, pretend that
1385      // the fallthrough path was inaccessible; we've dynamically proven
1386      // that we're not in the EH case along that path.
1387      if (EndCatchFn) {
1388        CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1389        CGF.PopCleanupBlock();
1390        CGF.Builder.restoreIP(SavedIP);
1391      }
1392
1393      // Now make sure we actually have an insertion point or the
1394      // cleanup gods will hate us.
1395      CGF.EnsureInsertPoint();
1396    }
1397  };
1398}
1399
1400/// Enters a finally block for an implementation using zero-cost
1401/// exceptions.  This is mostly general, but hard-codes some
1402/// language/ABI-specific behavior in the catch-all sections.
1403void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1404                                         const Stmt *body,
1405                                         llvm::Constant *beginCatchFn,
1406                                         llvm::Constant *endCatchFn,
1407                                         llvm::Constant *rethrowFn) {
1408  assert((beginCatchFn != 0) == (endCatchFn != 0) &&
1409         "begin/end catch functions not paired");
1410  assert(rethrowFn && "rethrow function is required");
1411
1412  BeginCatchFn = beginCatchFn;
1413
1414  // The rethrow function has one of the following two types:
1415  //   void (*)()
1416  //   void (*)(void*)
1417  // In the latter case we need to pass it the exception object.
1418  // But we can't use the exception slot because the @finally might
1419  // have a landing pad (which would overwrite the exception slot).
1420  llvm::FunctionType *rethrowFnTy =
1421    cast<llvm::FunctionType>(
1422      cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1423  SavedExnVar = 0;
1424  if (rethrowFnTy->getNumParams())
1425    SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
1426
1427  // A finally block is a statement which must be executed on any edge
1428  // out of a given scope.  Unlike a cleanup, the finally block may
1429  // contain arbitrary control flow leading out of itself.  In
1430  // addition, finally blocks should always be executed, even if there
1431  // are no catch handlers higher on the stack.  Therefore, we
1432  // surround the protected scope with a combination of a normal
1433  // cleanup (to catch attempts to break out of the block via normal
1434  // control flow) and an EH catch-all (semantically "outside" any try
1435  // statement to which the finally block might have been attached).
1436  // The finally block itself is generated in the context of a cleanup
1437  // which conditionally leaves the catch-all.
1438
1439  // Jump destination for performing the finally block on an exception
1440  // edge.  We'll never actually reach this block, so unreachable is
1441  // fine.
1442  RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
1443
1444  // Whether the finally block is being executed for EH purposes.
1445  ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1446  CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
1447
1448  // Enter a normal cleanup which will perform the @finally block.
1449  CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1450                                          ForEHVar, endCatchFn,
1451                                          rethrowFn, SavedExnVar);
1452
1453  // Enter a catch-all scope.
1454  llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1455  EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1456  catchScope->setCatchAllHandler(0, catchBB);
1457}
1458
1459void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
1460  // Leave the finally catch-all.
1461  EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1462  llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
1463
1464  CGF.popCatchScope();
1465
1466  // If there are any references to the catch-all block, emit it.
1467  if (catchBB->use_empty()) {
1468    delete catchBB;
1469  } else {
1470    CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1471    CGF.EmitBlock(catchBB);
1472
1473    llvm::Value *exn = 0;
1474
1475    // If there's a begin-catch function, call it.
1476    if (BeginCatchFn) {
1477      exn = CGF.getExceptionFromSlot();
1478      CGF.Builder.CreateCall(BeginCatchFn, exn)->setDoesNotThrow();
1479    }
1480
1481    // If we need to remember the exception pointer to rethrow later, do so.
1482    if (SavedExnVar) {
1483      if (!exn) exn = CGF.getExceptionFromSlot();
1484      CGF.Builder.CreateStore(exn, SavedExnVar);
1485    }
1486
1487    // Tell the cleanups in the finally block that we're do this for EH.
1488    CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
1489
1490    // Thread a jump through the finally cleanup.
1491    CGF.EmitBranchThroughCleanup(RethrowDest);
1492
1493    CGF.Builder.restoreIP(savedIP);
1494  }
1495
1496  // Finally, leave the @finally cleanup.
1497  CGF.PopCleanupBlock();
1498}
1499
1500llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1501  if (TerminateLandingPad)
1502    return TerminateLandingPad;
1503
1504  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1505
1506  // This will get inserted at the end of the function.
1507  TerminateLandingPad = createBasicBlock("terminate.lpad");
1508  Builder.SetInsertPoint(TerminateLandingPad);
1509
1510  // Tell the backend that this is a landing pad.
1511  const EHPersonality &Personality = EHPersonality::get(CGM.getLangOpts());
1512  llvm::LandingPadInst *LPadInst =
1513    Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
1514                             getOpaquePersonalityFn(CGM, Personality), 0);
1515  LPadInst->addClause(getCatchAllValue(*this));
1516
1517  llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
1518  TerminateCall->setDoesNotReturn();
1519  TerminateCall->setDoesNotThrow();
1520  Builder.CreateUnreachable();
1521
1522  // Restore the saved insertion state.
1523  Builder.restoreIP(SavedIP);
1524
1525  return TerminateLandingPad;
1526}
1527
1528llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
1529  if (TerminateHandler)
1530    return TerminateHandler;
1531
1532  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1533
1534  // Set up the terminate handler.  This block is inserted at the very
1535  // end of the function by FinishFunction.
1536  TerminateHandler = createBasicBlock("terminate.handler");
1537  Builder.SetInsertPoint(TerminateHandler);
1538  llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
1539  TerminateCall->setDoesNotReturn();
1540  TerminateCall->setDoesNotThrow();
1541  Builder.CreateUnreachable();
1542
1543  // Restore the saved insertion state.
1544  Builder.restoreIP(SavedIP);
1545
1546  return TerminateHandler;
1547}
1548
1549llvm::BasicBlock *CodeGenFunction::getEHResumeBlock() {
1550  if (EHResumeBlock) return EHResumeBlock;
1551
1552  CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1553
1554  // We emit a jump to a notional label at the outermost unwind state.
1555  EHResumeBlock = createBasicBlock("eh.resume");
1556  Builder.SetInsertPoint(EHResumeBlock);
1557
1558  const EHPersonality &Personality = EHPersonality::get(CGM.getLangOpts());
1559
1560  // This can always be a call because we necessarily didn't find
1561  // anything on the EH stack which needs our help.
1562  const char *RethrowName = Personality.CatchallRethrowFn;
1563  if (RethrowName != 0) {
1564    Builder.CreateCall(getCatchallRethrowFn(*this, RethrowName),
1565                       getExceptionFromSlot())
1566      ->setDoesNotReturn();
1567  } else {
1568    switch (CleanupHackLevel) {
1569    case CHL_MandatoryCatchall:
1570      // In mandatory-catchall mode, we need to use
1571      // _Unwind_Resume_or_Rethrow, or whatever the personality's
1572      // equivalent is.
1573      Builder.CreateCall(getUnwindResumeOrRethrowFn(),
1574                         getExceptionFromSlot())
1575        ->setDoesNotReturn();
1576      break;
1577    case CHL_MandatoryCleanup: {
1578      // In mandatory-cleanup mode, we should use 'resume'.
1579
1580      // Recreate the landingpad's return value for the 'resume' instruction.
1581      llvm::Value *Exn = getExceptionFromSlot();
1582      llvm::Value *Sel = getSelectorFromSlot();
1583
1584      llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
1585                                                   Sel->getType(), NULL);
1586      llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1587      LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1588      LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1589
1590      Builder.CreateResume(LPadVal);
1591      Builder.restoreIP(SavedIP);
1592      return EHResumeBlock;
1593    }
1594    case CHL_Ideal:
1595      // In an idealized mode where we don't have to worry about the
1596      // optimizer combining landing pads, we should just use
1597      // _Unwind_Resume (or the personality's equivalent).
1598      Builder.CreateCall(getUnwindResumeFn(), getExceptionFromSlot())
1599        ->setDoesNotReturn();
1600      break;
1601    }
1602  }
1603
1604  Builder.CreateUnreachable();
1605
1606  Builder.restoreIP(SavedIP);
1607
1608  return EHResumeBlock;
1609}
1610