blob: 8338691ab5d62b245bdd0f6c4376b81d3943ff3c [file] [log] [blame]
John McCalled1ae862011-01-28 11:13:47 +00001//===-- CGCleanup.h - Classes for cleanups IR generation --------*- C++ -*-===//
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// These classes support the generation of LLVM IR for cleanups.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
15#define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
John McCalled1ae862011-01-28 11:13:47 +000016
Reid Klecknerd29f1342013-06-19 17:07:50 +000017#include "EHScopeStack.h"
Reid Kleckner200fe222013-06-09 16:45:02 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000020
21namespace llvm {
22class BasicBlock;
23class Value;
24class ConstantInt;
25class AllocaInst;
26}
John McCalled1ae862011-01-28 11:13:47 +000027
28namespace clang {
David Majnemerc28d46e2015-07-22 23:46:21 +000029class FunctionDecl;
John McCalled1ae862011-01-28 11:13:47 +000030namespace CodeGen {
David Majnemerc28d46e2015-07-22 23:46:21 +000031class CodeGenModule;
32class CodeGenFunction;
John McCalled1ae862011-01-28 11:13:47 +000033
34/// A protected scope for zero-cost EH handling.
35class EHScope {
36 llvm::BasicBlock *CachedLandingPad;
John McCall8e4c74b2011-08-11 02:22:43 +000037 llvm::BasicBlock *CachedEHDispatchBlock;
John McCalled1ae862011-01-28 11:13:47 +000038
John McCall8e4c74b2011-08-11 02:22:43 +000039 EHScopeStack::stable_iterator EnclosingEHScope;
40
41 class CommonBitFields {
42 friend class EHScope;
43 unsigned Kind : 2;
44 };
45 enum { NumCommonBits = 2 };
John McCalled1ae862011-01-28 11:13:47 +000046
47protected:
John McCall8e4c74b2011-08-11 02:22:43 +000048 class CatchBitFields {
49 friend class EHCatchScope;
50 unsigned : NumCommonBits;
51
52 unsigned NumHandlers : 32 - NumCommonBits;
53 };
54
55 class CleanupBitFields {
56 friend class EHCleanupScope;
57 unsigned : NumCommonBits;
58
59 /// Whether this cleanup needs to be run along normal edges.
60 unsigned IsNormalCleanup : 1;
61
62 /// Whether this cleanup needs to be run along exception edges.
63 unsigned IsEHCleanup : 1;
64
65 /// Whether this cleanup is currently active.
66 unsigned IsActive : 1;
67
David Majnemerdc012fa2015-04-22 21:38:15 +000068 /// Whether this cleanup is a lifetime marker
69 unsigned IsLifetimeMarker : 1;
70
John McCall8e4c74b2011-08-11 02:22:43 +000071 /// Whether the normal cleanup should test the activation flag.
72 unsigned TestFlagInNormalCleanup : 1;
73
74 /// Whether the EH cleanup should test the activation flag.
75 unsigned TestFlagInEHCleanup : 1;
76
77 /// The amount of extra storage needed by the Cleanup.
78 /// Always a multiple of the scope-stack alignment.
79 unsigned CleanupSize : 12;
80
81 /// The number of fixups required by enclosing scopes (not including
82 /// this one). If this is the top cleanup scope, all the fixups
83 /// from this index onwards belong to this scope.
David Majnemerdc012fa2015-04-22 21:38:15 +000084 unsigned FixupDepth : 32 - 18 - NumCommonBits; // currently 13
John McCall8e4c74b2011-08-11 02:22:43 +000085 };
86
87 class FilterBitFields {
88 friend class EHFilterScope;
89 unsigned : NumCommonBits;
90
91 unsigned NumFilters : 32 - NumCommonBits;
92 };
93
94 union {
95 CommonBitFields CommonBits;
96 CatchBitFields CatchBits;
97 CleanupBitFields CleanupBits;
98 FilterBitFields FilterBits;
99 };
John McCalled1ae862011-01-28 11:13:47 +0000100
101public:
102 enum Kind { Cleanup, Catch, Terminate, Filter };
103
John McCall8e4c74b2011-08-11 02:22:43 +0000104 EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
Craig Topper8a13c412014-05-21 05:09:00 +0000105 : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),
John McCall8e4c74b2011-08-11 02:22:43 +0000106 EnclosingEHScope(enclosingEHScope) {
107 CommonBits.Kind = kind;
108 }
John McCalled1ae862011-01-28 11:13:47 +0000109
John McCall8e4c74b2011-08-11 02:22:43 +0000110 Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); }
John McCalled1ae862011-01-28 11:13:47 +0000111
112 llvm::BasicBlock *getCachedLandingPad() const {
113 return CachedLandingPad;
114 }
115
John McCall8e4c74b2011-08-11 02:22:43 +0000116 void setCachedLandingPad(llvm::BasicBlock *block) {
117 CachedLandingPad = block;
118 }
119
120 llvm::BasicBlock *getCachedEHDispatchBlock() const {
121 return CachedEHDispatchBlock;
122 }
123
124 void setCachedEHDispatchBlock(llvm::BasicBlock *block) {
125 CachedEHDispatchBlock = block;
126 }
127
128 bool hasEHBranches() const {
129 if (llvm::BasicBlock *block = getCachedEHDispatchBlock())
130 return !block->use_empty();
131 return false;
132 }
133
134 EHScopeStack::stable_iterator getEnclosingEHScope() const {
135 return EnclosingEHScope;
John McCalled1ae862011-01-28 11:13:47 +0000136 }
137};
138
139/// A scope which attempts to handle some, possibly all, types of
140/// exceptions.
141///
James Dennettbe302452012-06-15 22:10:14 +0000142/// Objective C \@finally blocks are represented using a cleanup scope
John McCalled1ae862011-01-28 11:13:47 +0000143/// after the catch scope.
144class EHCatchScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000145 // In effect, we have a flexible array member
146 // Handler Handlers[0];
147 // But that's only standard in C99, not C++, so we have to do
148 // annoying pointer arithmetic instead.
149
150public:
151 struct Handler {
152 /// A type info value, or null (C++ null, not an LLVM null pointer)
153 /// for a catch-all.
Rafael Espindolabb9e7a32014-06-04 18:51:46 +0000154 llvm::Constant *Type;
John McCalled1ae862011-01-28 11:13:47 +0000155
156 /// The catch handler for this type.
157 llvm::BasicBlock *Block;
158
Craig Topper8a13c412014-05-21 05:09:00 +0000159 bool isCatchAll() const { return Type == nullptr; }
John McCalled1ae862011-01-28 11:13:47 +0000160 };
161
162private:
163 friend class EHScopeStack;
164
165 Handler *getHandlers() {
166 return reinterpret_cast<Handler*>(this+1);
167 }
168
169 const Handler *getHandlers() const {
170 return reinterpret_cast<const Handler*>(this+1);
171 }
172
173public:
174 static size_t getSizeForNumHandlers(unsigned N) {
175 return sizeof(EHCatchScope) + N * sizeof(Handler);
176 }
177
John McCall8e4c74b2011-08-11 02:22:43 +0000178 EHCatchScope(unsigned numHandlers,
179 EHScopeStack::stable_iterator enclosingEHScope)
180 : EHScope(Catch, enclosingEHScope) {
181 CatchBits.NumHandlers = numHandlers;
John McCalled1ae862011-01-28 11:13:47 +0000182 }
183
184 unsigned getNumHandlers() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000185 return CatchBits.NumHandlers;
John McCalled1ae862011-01-28 11:13:47 +0000186 }
187
188 void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
Craig Topper8a13c412014-05-21 05:09:00 +0000189 setHandler(I, /*catchall*/ nullptr, Block);
John McCalled1ae862011-01-28 11:13:47 +0000190 }
191
Rafael Espindolabb9e7a32014-06-04 18:51:46 +0000192 void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
John McCalled1ae862011-01-28 11:13:47 +0000193 assert(I < getNumHandlers());
194 getHandlers()[I].Type = Type;
195 getHandlers()[I].Block = Block;
196 }
197
198 const Handler &getHandler(unsigned I) const {
199 assert(I < getNumHandlers());
200 return getHandlers()[I];
201 }
202
Kostya Serebryanyba4aced2014-01-09 09:22:32 +0000203 // Clear all handler blocks.
204 // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
205 // 'takeHandler' or some such function which removes ownership from the
206 // EHCatchScope object if the handlers should live longer than EHCatchScope.
207 void clearHandlerBlocks() {
208 for (unsigned I = 0, N = getNumHandlers(); I != N; ++I)
209 delete getHandler(I).Block;
210 }
211
John McCalled1ae862011-01-28 11:13:47 +0000212 typedef const Handler *iterator;
213 iterator begin() const { return getHandlers(); }
214 iterator end() const { return getHandlers() + getNumHandlers(); }
215
216 static bool classof(const EHScope *Scope) {
217 return Scope->getKind() == Catch;
218 }
219};
220
221/// A cleanup scope which generates the cleanup blocks lazily.
James Y Knight53c76162015-07-17 18:21:37 +0000222class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) EHCleanupScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000223 /// The nearest normal cleanup scope enclosing this one.
224 EHScopeStack::stable_iterator EnclosingNormal;
225
John McCall8e4c74b2011-08-11 02:22:43 +0000226 /// The nearest EH scope enclosing this one.
John McCalled1ae862011-01-28 11:13:47 +0000227 EHScopeStack::stable_iterator EnclosingEH;
228
229 /// The dual entry/exit block along the normal edge. This is lazily
230 /// created if needed before the cleanup is popped.
231 llvm::BasicBlock *NormalBlock;
232
John McCalled1ae862011-01-28 11:13:47 +0000233 /// An optional i1 variable indicating whether this cleanup has been
234 /// activated yet.
235 llvm::AllocaInst *ActiveFlag;
236
237 /// Extra information required for cleanups that have resolved
238 /// branches through them. This has to be allocated on the side
239 /// because everything on the cleanup stack has be trivially
240 /// movable.
241 struct ExtInfo {
242 /// The destinations of normal branch-afters and branch-throughs.
243 llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches;
244
245 /// Normal branch-afters.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000246 SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4>
John McCalled1ae862011-01-28 11:13:47 +0000247 BranchAfters;
John McCalled1ae862011-01-28 11:13:47 +0000248 };
249 mutable struct ExtInfo *ExtInfo;
250
251 struct ExtInfo &getExtInfo() {
252 if (!ExtInfo) ExtInfo = new struct ExtInfo();
253 return *ExtInfo;
254 }
255
256 const struct ExtInfo &getExtInfo() const {
257 if (!ExtInfo) ExtInfo = new struct ExtInfo();
258 return *ExtInfo;
259 }
260
261public:
262 /// Gets the size required for a lazy cleanup scope with the given
263 /// cleanup-data requirements.
264 static size_t getSizeForCleanupSize(size_t Size) {
265 return sizeof(EHCleanupScope) + Size;
266 }
267
268 size_t getAllocatedSize() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000269 return sizeof(EHCleanupScope) + CleanupBits.CleanupSize;
John McCalled1ae862011-01-28 11:13:47 +0000270 }
271
John McCall8e4c74b2011-08-11 02:22:43 +0000272 EHCleanupScope(bool isNormal, bool isEH, bool isActive,
273 unsigned cleanupSize, unsigned fixupDepth,
274 EHScopeStack::stable_iterator enclosingNormal,
275 EHScopeStack::stable_iterator enclosingEH)
276 : EHScope(EHScope::Cleanup, enclosingEH), EnclosingNormal(enclosingNormal),
Craig Topper8a13c412014-05-21 05:09:00 +0000277 NormalBlock(nullptr), ActiveFlag(nullptr), ExtInfo(nullptr) {
John McCall8e4c74b2011-08-11 02:22:43 +0000278 CleanupBits.IsNormalCleanup = isNormal;
279 CleanupBits.IsEHCleanup = isEH;
280 CleanupBits.IsActive = isActive;
David Majnemerdc012fa2015-04-22 21:38:15 +0000281 CleanupBits.IsLifetimeMarker = false;
John McCall8e4c74b2011-08-11 02:22:43 +0000282 CleanupBits.TestFlagInNormalCleanup = false;
283 CleanupBits.TestFlagInEHCleanup = false;
284 CleanupBits.CleanupSize = cleanupSize;
285 CleanupBits.FixupDepth = fixupDepth;
286
287 assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");
John McCalled1ae862011-01-28 11:13:47 +0000288 }
289
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000290 void Destroy() {
John McCalled1ae862011-01-28 11:13:47 +0000291 delete ExtInfo;
292 }
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000293 // Objects of EHCleanupScope are not destructed. Use Destroy().
Aaron Ballmanabc18922015-02-15 22:54:08 +0000294 ~EHCleanupScope() = delete;
John McCalled1ae862011-01-28 11:13:47 +0000295
John McCall8e4c74b2011-08-11 02:22:43 +0000296 bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000297 llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
298 void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }
299
John McCall8e4c74b2011-08-11 02:22:43 +0000300 bool isEHCleanup() const { return CleanupBits.IsEHCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000301
John McCall8e4c74b2011-08-11 02:22:43 +0000302 bool isActive() const { return CleanupBits.IsActive; }
303 void setActive(bool A) { CleanupBits.IsActive = A; }
John McCalled1ae862011-01-28 11:13:47 +0000304
David Majnemerdc012fa2015-04-22 21:38:15 +0000305 bool isLifetimeMarker() const { return CleanupBits.IsLifetimeMarker; }
306 void setLifetimeMarker() { CleanupBits.IsLifetimeMarker = true; }
307
John McCalled1ae862011-01-28 11:13:47 +0000308 llvm::AllocaInst *getActiveFlag() const { return ActiveFlag; }
309 void setActiveFlag(llvm::AllocaInst *Var) { ActiveFlag = Var; }
310
John McCall8e4c74b2011-08-11 02:22:43 +0000311 void setTestFlagInNormalCleanup() {
312 CleanupBits.TestFlagInNormalCleanup = true;
313 }
314 bool shouldTestFlagInNormalCleanup() const {
315 return CleanupBits.TestFlagInNormalCleanup;
316 }
John McCalled1ae862011-01-28 11:13:47 +0000317
John McCall8e4c74b2011-08-11 02:22:43 +0000318 void setTestFlagInEHCleanup() {
319 CleanupBits.TestFlagInEHCleanup = true;
320 }
321 bool shouldTestFlagInEHCleanup() const {
322 return CleanupBits.TestFlagInEHCleanup;
323 }
John McCalled1ae862011-01-28 11:13:47 +0000324
John McCall8e4c74b2011-08-11 02:22:43 +0000325 unsigned getFixupDepth() const { return CleanupBits.FixupDepth; }
John McCalled1ae862011-01-28 11:13:47 +0000326 EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
327 return EnclosingNormal;
328 }
John McCalled1ae862011-01-28 11:13:47 +0000329
John McCall8e4c74b2011-08-11 02:22:43 +0000330 size_t getCleanupSize() const { return CleanupBits.CleanupSize; }
John McCalled1ae862011-01-28 11:13:47 +0000331 void *getCleanupBuffer() { return this + 1; }
332
333 EHScopeStack::Cleanup *getCleanup() {
334 return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer());
335 }
336
337 /// True if this cleanup scope has any branch-afters or branch-throughs.
338 bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }
339
340 /// Add a branch-after to this cleanup scope. A branch-after is a
341 /// branch from a point protected by this (normal) cleanup to a
342 /// point in the normal cleanup scope immediately containing it.
343 /// For example,
344 /// for (;;) { A a; break; }
345 /// contains a branch-after.
346 ///
347 /// Branch-afters each have their own destination out of the
348 /// cleanup, guaranteed distinct from anything else threaded through
349 /// it. Therefore branch-afters usually force a switch after the
350 /// cleanup.
351 void addBranchAfter(llvm::ConstantInt *Index,
352 llvm::BasicBlock *Block) {
353 struct ExtInfo &ExtInfo = getExtInfo();
David Blaikie82e95a32014-11-19 07:49:47 +0000354 if (ExtInfo.Branches.insert(Block).second)
John McCalled1ae862011-01-28 11:13:47 +0000355 ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));
356 }
357
358 /// Return the number of unique branch-afters on this scope.
359 unsigned getNumBranchAfters() const {
360 return ExtInfo ? ExtInfo->BranchAfters.size() : 0;
361 }
362
363 llvm::BasicBlock *getBranchAfterBlock(unsigned I) const {
364 assert(I < getNumBranchAfters());
365 return ExtInfo->BranchAfters[I].first;
366 }
367
368 llvm::ConstantInt *getBranchAfterIndex(unsigned I) const {
369 assert(I < getNumBranchAfters());
370 return ExtInfo->BranchAfters[I].second;
371 }
372
373 /// Add a branch-through to this cleanup scope. A branch-through is
374 /// a branch from a scope protected by this (normal) cleanup to an
375 /// enclosing scope other than the immediately-enclosing normal
376 /// cleanup scope.
377 ///
378 /// In the following example, the branch through B's scope is a
379 /// branch-through, while the branch through A's scope is a
380 /// branch-after:
381 /// for (;;) { A a; B b; break; }
382 ///
383 /// All branch-throughs have a common destination out of the
384 /// cleanup, one possibly shared with the fall-through. Therefore
385 /// branch-throughs usually don't force a switch after the cleanup.
386 ///
387 /// \return true if the branch-through was new to this scope
388 bool addBranchThrough(llvm::BasicBlock *Block) {
David Blaikie82e95a32014-11-19 07:49:47 +0000389 return getExtInfo().Branches.insert(Block).second;
John McCalled1ae862011-01-28 11:13:47 +0000390 }
391
392 /// Determines if this cleanup scope has any branch throughs.
393 bool hasBranchThroughs() const {
394 if (!ExtInfo) return false;
395 return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());
396 }
397
John McCalled1ae862011-01-28 11:13:47 +0000398 static bool classof(const EHScope *Scope) {
399 return (Scope->getKind() == Cleanup);
400 }
401};
James Y Knight53c76162015-07-17 18:21:37 +0000402// NOTE: there's a bunch of different data classes tacked on after an
403// EHCleanupScope. It is asserted (in EHScopeStack::pushCleanup*) that
404// they don't require greater alignment than ScopeStackAlignment. So,
405// EHCleanupScope ought to have alignment equal to that -- not more
406// (would be misaligned by the stack allocator), and not less (would
407// break the appended classes).
408static_assert(llvm::AlignOf<EHCleanupScope>::Alignment ==
409 EHScopeStack::ScopeStackAlignment,
410 "EHCleanupScope expected alignment");
John McCalled1ae862011-01-28 11:13:47 +0000411
412/// An exceptions scope which filters exceptions thrown through it.
413/// Only exceptions matching the filter types will be permitted to be
414/// thrown.
415///
416/// This is used to implement C++ exception specifications.
417class EHFilterScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000418 // Essentially ends in a flexible array member:
419 // llvm::Value *FilterTypes[0];
420
421 llvm::Value **getFilters() {
422 return reinterpret_cast<llvm::Value**>(this+1);
423 }
424
425 llvm::Value * const *getFilters() const {
426 return reinterpret_cast<llvm::Value* const *>(this+1);
427 }
428
429public:
John McCall8e4c74b2011-08-11 02:22:43 +0000430 EHFilterScope(unsigned numFilters)
431 : EHScope(Filter, EHScopeStack::stable_end()) {
432 FilterBits.NumFilters = numFilters;
John McCalled1ae862011-01-28 11:13:47 +0000433 }
434
John McCall8e4c74b2011-08-11 02:22:43 +0000435 static size_t getSizeForNumFilters(unsigned numFilters) {
436 return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);
John McCalled1ae862011-01-28 11:13:47 +0000437 }
438
John McCall8e4c74b2011-08-11 02:22:43 +0000439 unsigned getNumFilters() const { return FilterBits.NumFilters; }
440
441 void setFilter(unsigned i, llvm::Value *filterValue) {
442 assert(i < getNumFilters());
443 getFilters()[i] = filterValue;
John McCalled1ae862011-01-28 11:13:47 +0000444 }
445
John McCall8e4c74b2011-08-11 02:22:43 +0000446 llvm::Value *getFilter(unsigned i) const {
447 assert(i < getNumFilters());
448 return getFilters()[i];
449 }
450
451 static bool classof(const EHScope *scope) {
452 return scope->getKind() == Filter;
John McCalled1ae862011-01-28 11:13:47 +0000453 }
454};
455
456/// An exceptions scope which calls std::terminate if any exception
457/// reaches it.
458class EHTerminateScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000459public:
John McCall8e4c74b2011-08-11 02:22:43 +0000460 EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)
461 : EHScope(Terminate, enclosingEHScope) {}
John McCalled1ae862011-01-28 11:13:47 +0000462 static size_t getSize() { return sizeof(EHTerminateScope); }
463
John McCall8e4c74b2011-08-11 02:22:43 +0000464 static bool classof(const EHScope *scope) {
465 return scope->getKind() == Terminate;
John McCalled1ae862011-01-28 11:13:47 +0000466 }
467};
468
469/// A non-stable pointer into the scope stack.
470class EHScopeStack::iterator {
471 char *Ptr;
472
473 friend class EHScopeStack;
474 explicit iterator(char *Ptr) : Ptr(Ptr) {}
475
476public:
Craig Topper8a13c412014-05-21 05:09:00 +0000477 iterator() : Ptr(nullptr) {}
John McCalled1ae862011-01-28 11:13:47 +0000478
479 EHScope *get() const {
480 return reinterpret_cast<EHScope*>(Ptr);
481 }
482
483 EHScope *operator->() const { return get(); }
484 EHScope &operator*() const { return *get(); }
485
486 iterator &operator++() {
James Y Knight53c76162015-07-17 18:21:37 +0000487 size_t Size;
John McCalled1ae862011-01-28 11:13:47 +0000488 switch (get()->getKind()) {
489 case EHScope::Catch:
James Y Knight53c76162015-07-17 18:21:37 +0000490 Size = EHCatchScope::getSizeForNumHandlers(
491 static_cast<const EHCatchScope *>(get())->getNumHandlers());
John McCalled1ae862011-01-28 11:13:47 +0000492 break;
493
494 case EHScope::Filter:
James Y Knight53c76162015-07-17 18:21:37 +0000495 Size = EHFilterScope::getSizeForNumFilters(
496 static_cast<const EHFilterScope *>(get())->getNumFilters());
John McCalled1ae862011-01-28 11:13:47 +0000497 break;
498
499 case EHScope::Cleanup:
James Y Knight53c76162015-07-17 18:21:37 +0000500 Size = static_cast<const EHCleanupScope *>(get())->getAllocatedSize();
John McCalled1ae862011-01-28 11:13:47 +0000501 break;
502
503 case EHScope::Terminate:
James Y Knight53c76162015-07-17 18:21:37 +0000504 Size = EHTerminateScope::getSize();
John McCalled1ae862011-01-28 11:13:47 +0000505 break;
506 }
James Y Knight53c76162015-07-17 18:21:37 +0000507 Ptr += llvm::RoundUpToAlignment(Size, ScopeStackAlignment);
John McCalled1ae862011-01-28 11:13:47 +0000508 return *this;
509 }
510
511 iterator next() {
512 iterator copy = *this;
513 ++copy;
514 return copy;
515 }
516
517 iterator operator++(int) {
518 iterator copy = *this;
519 operator++();
520 return copy;
521 }
522
523 bool encloses(iterator other) const { return Ptr >= other.Ptr; }
524 bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; }
525
526 bool operator==(iterator other) const { return Ptr == other.Ptr; }
527 bool operator!=(iterator other) const { return Ptr != other.Ptr; }
528};
529
530inline EHScopeStack::iterator EHScopeStack::begin() const {
531 return iterator(StartOfData);
532}
533
534inline EHScopeStack::iterator EHScopeStack::end() const {
535 return iterator(EndOfBuffer);
536}
537
538inline void EHScopeStack::popCatch() {
539 assert(!empty() && "popping exception stack when not empty");
540
John McCall8e4c74b2011-08-11 02:22:43 +0000541 EHCatchScope &scope = cast<EHCatchScope>(*begin());
542 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000543 deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers()));
John McCalled1ae862011-01-28 11:13:47 +0000544}
545
546inline void EHScopeStack::popTerminate() {
547 assert(!empty() && "popping exception stack when not empty");
548
John McCall8e4c74b2011-08-11 02:22:43 +0000549 EHTerminateScope &scope = cast<EHTerminateScope>(*begin());
550 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000551 deallocate(EHTerminateScope::getSize());
John McCalled1ae862011-01-28 11:13:47 +0000552}
553
554inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
555 assert(sp.isValid() && "finding invalid savepoint");
556 assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
557 return iterator(EndOfBuffer - sp.Size);
558}
559
560inline EHScopeStack::stable_iterator
561EHScopeStack::stabilize(iterator ir) const {
562 assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
563 return stable_iterator(EndOfBuffer - ir.Ptr);
564}
565
David Majnemerc28d46e2015-07-22 23:46:21 +0000566/// The exceptions personality for a function.
567struct EHPersonality {
568 const char *PersonalityFn;
569
570 // If this is non-null, this personality requires a non-standard
571 // function for rethrowing an exception after a catchall cleanup.
572 // This function must have prototype void(void*).
573 const char *CatchallRethrowFn;
574
575 static const EHPersonality &get(CodeGenModule &CGM, const FunctionDecl *FD);
576 static const EHPersonality &get(CodeGenFunction &CGF);
577
578 static const EHPersonality GNU_C;
579 static const EHPersonality GNU_C_SJLJ;
580 static const EHPersonality GNU_C_SEH;
581 static const EHPersonality GNU_ObjC;
582 static const EHPersonality GNUstep_ObjC;
583 static const EHPersonality GNU_ObjCXX;
584 static const EHPersonality NeXT_ObjC;
585 static const EHPersonality GNU_CPlusPlus;
586 static const EHPersonality GNU_CPlusPlus_SJLJ;
587 static const EHPersonality GNU_CPlusPlus_SEH;
588 static const EHPersonality MSVC_except_handler;
589 static const EHPersonality MSVC_C_specific_handler;
590 static const EHPersonality MSVC_CxxFrameHandler3;
591};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000592}
593}
John McCalled1ae862011-01-28 11:13:47 +0000594
595#endif