blob: 5782343013c335409f51d6f1fade0cac44c10e9b [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"
John McCall7f416cc2015-09-08 08:05:57 +000018
19#include "Address.h"
Reid Kleckner200fe222013-06-09 16:45:02 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000022
23namespace llvm {
24class BasicBlock;
25class Value;
26class ConstantInt;
27class AllocaInst;
28}
John McCalled1ae862011-01-28 11:13:47 +000029
30namespace clang {
David Majnemerc28d46e2015-07-22 23:46:21 +000031class FunctionDecl;
John McCalled1ae862011-01-28 11:13:47 +000032namespace CodeGen {
David Majnemerc28d46e2015-07-22 23:46:21 +000033class CodeGenModule;
34class CodeGenFunction;
John McCalled1ae862011-01-28 11:13:47 +000035
36/// A protected scope for zero-cost EH handling.
37class EHScope {
38 llvm::BasicBlock *CachedLandingPad;
John McCall8e4c74b2011-08-11 02:22:43 +000039 llvm::BasicBlock *CachedEHDispatchBlock;
John McCalled1ae862011-01-28 11:13:47 +000040
John McCall8e4c74b2011-08-11 02:22:43 +000041 EHScopeStack::stable_iterator EnclosingEHScope;
42
43 class CommonBitFields {
44 friend class EHScope;
David Majnemerdbf10452015-07-31 17:58:45 +000045 unsigned Kind : 3;
John McCall8e4c74b2011-08-11 02:22:43 +000046 };
David Majnemerdbf10452015-07-31 17:58:45 +000047 enum { NumCommonBits = 3 };
John McCalled1ae862011-01-28 11:13:47 +000048
49protected:
John McCall8e4c74b2011-08-11 02:22:43 +000050 class CatchBitFields {
51 friend class EHCatchScope;
52 unsigned : NumCommonBits;
53
54 unsigned NumHandlers : 32 - NumCommonBits;
55 };
56
57 class CleanupBitFields {
58 friend class EHCleanupScope;
59 unsigned : NumCommonBits;
60
61 /// Whether this cleanup needs to be run along normal edges.
62 unsigned IsNormalCleanup : 1;
63
64 /// Whether this cleanup needs to be run along exception edges.
65 unsigned IsEHCleanup : 1;
66
67 /// Whether this cleanup is currently active.
68 unsigned IsActive : 1;
69
David Majnemerdc012fa2015-04-22 21:38:15 +000070 /// Whether this cleanup is a lifetime marker
71 unsigned IsLifetimeMarker : 1;
72
John McCall8e4c74b2011-08-11 02:22:43 +000073 /// Whether the normal cleanup should test the activation flag.
74 unsigned TestFlagInNormalCleanup : 1;
75
76 /// Whether the EH cleanup should test the activation flag.
77 unsigned TestFlagInEHCleanup : 1;
78
79 /// The amount of extra storage needed by the Cleanup.
80 /// Always a multiple of the scope-stack alignment.
81 unsigned CleanupSize : 12;
82
83 /// The number of fixups required by enclosing scopes (not including
84 /// this one). If this is the top cleanup scope, all the fixups
85 /// from this index onwards belong to this scope.
David Majnemerdbf10452015-07-31 17:58:45 +000086 unsigned FixupDepth : 32 - 18 - NumCommonBits; // currently 12
John McCall8e4c74b2011-08-11 02:22:43 +000087 };
88
89 class FilterBitFields {
90 friend class EHFilterScope;
91 unsigned : NumCommonBits;
92
93 unsigned NumFilters : 32 - NumCommonBits;
94 };
95
96 union {
97 CommonBitFields CommonBits;
98 CatchBitFields CatchBits;
99 CleanupBitFields CleanupBits;
100 FilterBitFields FilterBits;
101 };
John McCalled1ae862011-01-28 11:13:47 +0000102
103public:
Reid Kleckner2586aac2015-09-10 22:11:13 +0000104 enum Kind { Cleanup, Catch, Terminate, Filter, PadEnd };
John McCalled1ae862011-01-28 11:13:47 +0000105
John McCall8e4c74b2011-08-11 02:22:43 +0000106 EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
Craig Topper8a13c412014-05-21 05:09:00 +0000107 : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),
John McCall8e4c74b2011-08-11 02:22:43 +0000108 EnclosingEHScope(enclosingEHScope) {
109 CommonBits.Kind = kind;
110 }
John McCalled1ae862011-01-28 11:13:47 +0000111
John McCall8e4c74b2011-08-11 02:22:43 +0000112 Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); }
John McCalled1ae862011-01-28 11:13:47 +0000113
114 llvm::BasicBlock *getCachedLandingPad() const {
115 return CachedLandingPad;
116 }
117
John McCall8e4c74b2011-08-11 02:22:43 +0000118 void setCachedLandingPad(llvm::BasicBlock *block) {
119 CachedLandingPad = block;
120 }
121
122 llvm::BasicBlock *getCachedEHDispatchBlock() const {
123 return CachedEHDispatchBlock;
124 }
125
126 void setCachedEHDispatchBlock(llvm::BasicBlock *block) {
127 CachedEHDispatchBlock = block;
128 }
129
130 bool hasEHBranches() const {
131 if (llvm::BasicBlock *block = getCachedEHDispatchBlock())
132 return !block->use_empty();
133 return false;
134 }
135
136 EHScopeStack::stable_iterator getEnclosingEHScope() const {
137 return EnclosingEHScope;
John McCalled1ae862011-01-28 11:13:47 +0000138 }
139};
140
141/// A scope which attempts to handle some, possibly all, types of
142/// exceptions.
143///
James Dennettbe302452012-06-15 22:10:14 +0000144/// Objective C \@finally blocks are represented using a cleanup scope
John McCalled1ae862011-01-28 11:13:47 +0000145/// after the catch scope.
146class EHCatchScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000147 // In effect, we have a flexible array member
148 // Handler Handlers[0];
149 // But that's only standard in C99, not C++, so we have to do
150 // annoying pointer arithmetic instead.
151
152public:
153 struct Handler {
154 /// A type info value, or null (C++ null, not an LLVM null pointer)
155 /// for a catch-all.
Rafael Espindolabb9e7a32014-06-04 18:51:46 +0000156 llvm::Constant *Type;
John McCalled1ae862011-01-28 11:13:47 +0000157
158 /// The catch handler for this type.
159 llvm::BasicBlock *Block;
160
Craig Topper8a13c412014-05-21 05:09:00 +0000161 bool isCatchAll() const { return Type == nullptr; }
John McCalled1ae862011-01-28 11:13:47 +0000162 };
163
164private:
165 friend class EHScopeStack;
166
167 Handler *getHandlers() {
168 return reinterpret_cast<Handler*>(this+1);
169 }
170
171 const Handler *getHandlers() const {
172 return reinterpret_cast<const Handler*>(this+1);
173 }
174
175public:
176 static size_t getSizeForNumHandlers(unsigned N) {
177 return sizeof(EHCatchScope) + N * sizeof(Handler);
178 }
179
John McCall8e4c74b2011-08-11 02:22:43 +0000180 EHCatchScope(unsigned numHandlers,
181 EHScopeStack::stable_iterator enclosingEHScope)
182 : EHScope(Catch, enclosingEHScope) {
183 CatchBits.NumHandlers = numHandlers;
John McCalled1ae862011-01-28 11:13:47 +0000184 }
185
186 unsigned getNumHandlers() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000187 return CatchBits.NumHandlers;
John McCalled1ae862011-01-28 11:13:47 +0000188 }
189
190 void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
Craig Topper8a13c412014-05-21 05:09:00 +0000191 setHandler(I, /*catchall*/ nullptr, Block);
John McCalled1ae862011-01-28 11:13:47 +0000192 }
193
Rafael Espindolabb9e7a32014-06-04 18:51:46 +0000194 void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
John McCalled1ae862011-01-28 11:13:47 +0000195 assert(I < getNumHandlers());
196 getHandlers()[I].Type = Type;
197 getHandlers()[I].Block = Block;
198 }
199
200 const Handler &getHandler(unsigned I) const {
201 assert(I < getNumHandlers());
202 return getHandlers()[I];
203 }
204
Kostya Serebryanyba4aced2014-01-09 09:22:32 +0000205 // Clear all handler blocks.
206 // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
207 // 'takeHandler' or some such function which removes ownership from the
208 // EHCatchScope object if the handlers should live longer than EHCatchScope.
209 void clearHandlerBlocks() {
210 for (unsigned I = 0, N = getNumHandlers(); I != N; ++I)
211 delete getHandler(I).Block;
212 }
213
John McCalled1ae862011-01-28 11:13:47 +0000214 typedef const Handler *iterator;
215 iterator begin() const { return getHandlers(); }
216 iterator end() const { return getHandlers() + getNumHandlers(); }
217
218 static bool classof(const EHScope *Scope) {
219 return Scope->getKind() == Catch;
220 }
221};
222
223/// A cleanup scope which generates the cleanup blocks lazily.
James Y Knight53c76162015-07-17 18:21:37 +0000224class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) EHCleanupScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000225 /// The nearest normal cleanup scope enclosing this one.
226 EHScopeStack::stable_iterator EnclosingNormal;
227
John McCall8e4c74b2011-08-11 02:22:43 +0000228 /// The nearest EH scope enclosing this one.
John McCalled1ae862011-01-28 11:13:47 +0000229 EHScopeStack::stable_iterator EnclosingEH;
230
231 /// The dual entry/exit block along the normal edge. This is lazily
232 /// created if needed before the cleanup is popped.
233 llvm::BasicBlock *NormalBlock;
234
John McCalled1ae862011-01-28 11:13:47 +0000235 /// An optional i1 variable indicating whether this cleanup has been
236 /// activated yet.
237 llvm::AllocaInst *ActiveFlag;
238
239 /// Extra information required for cleanups that have resolved
240 /// branches through them. This has to be allocated on the side
241 /// because everything on the cleanup stack has be trivially
242 /// movable.
243 struct ExtInfo {
244 /// The destinations of normal branch-afters and branch-throughs.
245 llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches;
246
247 /// Normal branch-afters.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000248 SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4>
John McCalled1ae862011-01-28 11:13:47 +0000249 BranchAfters;
John McCalled1ae862011-01-28 11:13:47 +0000250 };
251 mutable struct ExtInfo *ExtInfo;
252
253 struct ExtInfo &getExtInfo() {
254 if (!ExtInfo) ExtInfo = new struct ExtInfo();
255 return *ExtInfo;
256 }
257
258 const struct ExtInfo &getExtInfo() const {
259 if (!ExtInfo) ExtInfo = new struct ExtInfo();
260 return *ExtInfo;
261 }
262
263public:
264 /// Gets the size required for a lazy cleanup scope with the given
265 /// cleanup-data requirements.
266 static size_t getSizeForCleanupSize(size_t Size) {
267 return sizeof(EHCleanupScope) + Size;
268 }
269
270 size_t getAllocatedSize() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000271 return sizeof(EHCleanupScope) + CleanupBits.CleanupSize;
John McCalled1ae862011-01-28 11:13:47 +0000272 }
273
John McCall8e4c74b2011-08-11 02:22:43 +0000274 EHCleanupScope(bool isNormal, bool isEH, bool isActive,
275 unsigned cleanupSize, unsigned fixupDepth,
276 EHScopeStack::stable_iterator enclosingNormal,
277 EHScopeStack::stable_iterator enclosingEH)
278 : EHScope(EHScope::Cleanup, enclosingEH), EnclosingNormal(enclosingNormal),
Craig Topper8a13c412014-05-21 05:09:00 +0000279 NormalBlock(nullptr), ActiveFlag(nullptr), ExtInfo(nullptr) {
John McCall8e4c74b2011-08-11 02:22:43 +0000280 CleanupBits.IsNormalCleanup = isNormal;
281 CleanupBits.IsEHCleanup = isEH;
282 CleanupBits.IsActive = isActive;
David Majnemerdc012fa2015-04-22 21:38:15 +0000283 CleanupBits.IsLifetimeMarker = false;
John McCall8e4c74b2011-08-11 02:22:43 +0000284 CleanupBits.TestFlagInNormalCleanup = false;
285 CleanupBits.TestFlagInEHCleanup = false;
286 CleanupBits.CleanupSize = cleanupSize;
287 CleanupBits.FixupDepth = fixupDepth;
288
289 assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");
John McCalled1ae862011-01-28 11:13:47 +0000290 }
291
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000292 void Destroy() {
John McCalled1ae862011-01-28 11:13:47 +0000293 delete ExtInfo;
294 }
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000295 // Objects of EHCleanupScope are not destructed. Use Destroy().
Aaron Ballmanabc18922015-02-15 22:54:08 +0000296 ~EHCleanupScope() = delete;
John McCalled1ae862011-01-28 11:13:47 +0000297
John McCall8e4c74b2011-08-11 02:22:43 +0000298 bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000299 llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
300 void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }
301
John McCall8e4c74b2011-08-11 02:22:43 +0000302 bool isEHCleanup() const { return CleanupBits.IsEHCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000303
John McCall8e4c74b2011-08-11 02:22:43 +0000304 bool isActive() const { return CleanupBits.IsActive; }
305 void setActive(bool A) { CleanupBits.IsActive = A; }
John McCalled1ae862011-01-28 11:13:47 +0000306
David Majnemerdc012fa2015-04-22 21:38:15 +0000307 bool isLifetimeMarker() const { return CleanupBits.IsLifetimeMarker; }
308 void setLifetimeMarker() { CleanupBits.IsLifetimeMarker = true; }
309
John McCall7f416cc2015-09-08 08:05:57 +0000310 bool hasActiveFlag() const { return ActiveFlag != nullptr; }
311 Address getActiveFlag() const {
312 return Address(ActiveFlag, CharUnits::One());
313 }
314 void setActiveFlag(Address Var) {
315 assert(Var.getAlignment().isOne());
316 ActiveFlag = cast<llvm::AllocaInst>(Var.getPointer());
317 }
John McCalled1ae862011-01-28 11:13:47 +0000318
John McCall8e4c74b2011-08-11 02:22:43 +0000319 void setTestFlagInNormalCleanup() {
320 CleanupBits.TestFlagInNormalCleanup = true;
321 }
322 bool shouldTestFlagInNormalCleanup() const {
323 return CleanupBits.TestFlagInNormalCleanup;
324 }
John McCalled1ae862011-01-28 11:13:47 +0000325
John McCall8e4c74b2011-08-11 02:22:43 +0000326 void setTestFlagInEHCleanup() {
327 CleanupBits.TestFlagInEHCleanup = true;
328 }
329 bool shouldTestFlagInEHCleanup() const {
330 return CleanupBits.TestFlagInEHCleanup;
331 }
John McCalled1ae862011-01-28 11:13:47 +0000332
John McCall8e4c74b2011-08-11 02:22:43 +0000333 unsigned getFixupDepth() const { return CleanupBits.FixupDepth; }
John McCalled1ae862011-01-28 11:13:47 +0000334 EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
335 return EnclosingNormal;
336 }
John McCalled1ae862011-01-28 11:13:47 +0000337
John McCall8e4c74b2011-08-11 02:22:43 +0000338 size_t getCleanupSize() const { return CleanupBits.CleanupSize; }
John McCalled1ae862011-01-28 11:13:47 +0000339 void *getCleanupBuffer() { return this + 1; }
340
341 EHScopeStack::Cleanup *getCleanup() {
342 return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer());
343 }
344
345 /// True if this cleanup scope has any branch-afters or branch-throughs.
346 bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }
347
348 /// Add a branch-after to this cleanup scope. A branch-after is a
349 /// branch from a point protected by this (normal) cleanup to a
350 /// point in the normal cleanup scope immediately containing it.
351 /// For example,
352 /// for (;;) { A a; break; }
353 /// contains a branch-after.
354 ///
355 /// Branch-afters each have their own destination out of the
356 /// cleanup, guaranteed distinct from anything else threaded through
357 /// it. Therefore branch-afters usually force a switch after the
358 /// cleanup.
359 void addBranchAfter(llvm::ConstantInt *Index,
360 llvm::BasicBlock *Block) {
361 struct ExtInfo &ExtInfo = getExtInfo();
David Blaikie82e95a32014-11-19 07:49:47 +0000362 if (ExtInfo.Branches.insert(Block).second)
John McCalled1ae862011-01-28 11:13:47 +0000363 ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));
364 }
365
366 /// Return the number of unique branch-afters on this scope.
367 unsigned getNumBranchAfters() const {
368 return ExtInfo ? ExtInfo->BranchAfters.size() : 0;
369 }
370
371 llvm::BasicBlock *getBranchAfterBlock(unsigned I) const {
372 assert(I < getNumBranchAfters());
373 return ExtInfo->BranchAfters[I].first;
374 }
375
376 llvm::ConstantInt *getBranchAfterIndex(unsigned I) const {
377 assert(I < getNumBranchAfters());
378 return ExtInfo->BranchAfters[I].second;
379 }
380
381 /// Add a branch-through to this cleanup scope. A branch-through is
382 /// a branch from a scope protected by this (normal) cleanup to an
383 /// enclosing scope other than the immediately-enclosing normal
384 /// cleanup scope.
385 ///
386 /// In the following example, the branch through B's scope is a
387 /// branch-through, while the branch through A's scope is a
388 /// branch-after:
389 /// for (;;) { A a; B b; break; }
390 ///
391 /// All branch-throughs have a common destination out of the
392 /// cleanup, one possibly shared with the fall-through. Therefore
393 /// branch-throughs usually don't force a switch after the cleanup.
394 ///
395 /// \return true if the branch-through was new to this scope
396 bool addBranchThrough(llvm::BasicBlock *Block) {
David Blaikie82e95a32014-11-19 07:49:47 +0000397 return getExtInfo().Branches.insert(Block).second;
John McCalled1ae862011-01-28 11:13:47 +0000398 }
399
400 /// Determines if this cleanup scope has any branch throughs.
401 bool hasBranchThroughs() const {
402 if (!ExtInfo) return false;
403 return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());
404 }
405
John McCalled1ae862011-01-28 11:13:47 +0000406 static bool classof(const EHScope *Scope) {
407 return (Scope->getKind() == Cleanup);
408 }
409};
James Y Knight53c76162015-07-17 18:21:37 +0000410// NOTE: there's a bunch of different data classes tacked on after an
411// EHCleanupScope. It is asserted (in EHScopeStack::pushCleanup*) that
412// they don't require greater alignment than ScopeStackAlignment. So,
413// EHCleanupScope ought to have alignment equal to that -- not more
414// (would be misaligned by the stack allocator), and not less (would
415// break the appended classes).
416static_assert(llvm::AlignOf<EHCleanupScope>::Alignment ==
417 EHScopeStack::ScopeStackAlignment,
418 "EHCleanupScope expected alignment");
John McCalled1ae862011-01-28 11:13:47 +0000419
420/// An exceptions scope which filters exceptions thrown through it.
421/// Only exceptions matching the filter types will be permitted to be
422/// thrown.
423///
424/// This is used to implement C++ exception specifications.
425class EHFilterScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000426 // Essentially ends in a flexible array member:
427 // llvm::Value *FilterTypes[0];
428
429 llvm::Value **getFilters() {
430 return reinterpret_cast<llvm::Value**>(this+1);
431 }
432
433 llvm::Value * const *getFilters() const {
434 return reinterpret_cast<llvm::Value* const *>(this+1);
435 }
436
437public:
John McCall8e4c74b2011-08-11 02:22:43 +0000438 EHFilterScope(unsigned numFilters)
439 : EHScope(Filter, EHScopeStack::stable_end()) {
440 FilterBits.NumFilters = numFilters;
John McCalled1ae862011-01-28 11:13:47 +0000441 }
442
John McCall8e4c74b2011-08-11 02:22:43 +0000443 static size_t getSizeForNumFilters(unsigned numFilters) {
444 return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);
John McCalled1ae862011-01-28 11:13:47 +0000445 }
446
John McCall8e4c74b2011-08-11 02:22:43 +0000447 unsigned getNumFilters() const { return FilterBits.NumFilters; }
448
449 void setFilter(unsigned i, llvm::Value *filterValue) {
450 assert(i < getNumFilters());
451 getFilters()[i] = filterValue;
John McCalled1ae862011-01-28 11:13:47 +0000452 }
453
John McCall8e4c74b2011-08-11 02:22:43 +0000454 llvm::Value *getFilter(unsigned i) const {
455 assert(i < getNumFilters());
456 return getFilters()[i];
457 }
458
459 static bool classof(const EHScope *scope) {
460 return scope->getKind() == Filter;
John McCalled1ae862011-01-28 11:13:47 +0000461 }
462};
463
464/// An exceptions scope which calls std::terminate if any exception
465/// reaches it.
466class EHTerminateScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000467public:
John McCall8e4c74b2011-08-11 02:22:43 +0000468 EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)
469 : EHScope(Terminate, enclosingEHScope) {}
John McCalled1ae862011-01-28 11:13:47 +0000470 static size_t getSize() { return sizeof(EHTerminateScope); }
471
John McCall8e4c74b2011-08-11 02:22:43 +0000472 static bool classof(const EHScope *scope) {
473 return scope->getKind() == Terminate;
John McCalled1ae862011-01-28 11:13:47 +0000474 }
475};
476
Reid Kleckner2586aac2015-09-10 22:11:13 +0000477class EHPadEndScope : public EHScope {
David Majnemerdbf10452015-07-31 17:58:45 +0000478public:
Reid Kleckner2586aac2015-09-10 22:11:13 +0000479 EHPadEndScope(EHScopeStack::stable_iterator enclosingEHScope)
480 : EHScope(PadEnd, enclosingEHScope) {}
481 static size_t getSize() { return sizeof(EHPadEndScope); }
David Majnemerdbf10452015-07-31 17:58:45 +0000482
483 static bool classof(const EHScope *scope) {
Reid Kleckner2586aac2015-09-10 22:11:13 +0000484 return scope->getKind() == PadEnd;
David Majnemerdbf10452015-07-31 17:58:45 +0000485 }
486};
487
John McCalled1ae862011-01-28 11:13:47 +0000488/// A non-stable pointer into the scope stack.
489class EHScopeStack::iterator {
490 char *Ptr;
491
492 friend class EHScopeStack;
493 explicit iterator(char *Ptr) : Ptr(Ptr) {}
494
495public:
Craig Topper8a13c412014-05-21 05:09:00 +0000496 iterator() : Ptr(nullptr) {}
John McCalled1ae862011-01-28 11:13:47 +0000497
498 EHScope *get() const {
499 return reinterpret_cast<EHScope*>(Ptr);
500 }
501
502 EHScope *operator->() const { return get(); }
503 EHScope &operator*() const { return *get(); }
504
505 iterator &operator++() {
James Y Knight53c76162015-07-17 18:21:37 +0000506 size_t Size;
John McCalled1ae862011-01-28 11:13:47 +0000507 switch (get()->getKind()) {
508 case EHScope::Catch:
James Y Knight53c76162015-07-17 18:21:37 +0000509 Size = EHCatchScope::getSizeForNumHandlers(
510 static_cast<const EHCatchScope *>(get())->getNumHandlers());
John McCalled1ae862011-01-28 11:13:47 +0000511 break;
512
513 case EHScope::Filter:
James Y Knight53c76162015-07-17 18:21:37 +0000514 Size = EHFilterScope::getSizeForNumFilters(
515 static_cast<const EHFilterScope *>(get())->getNumFilters());
John McCalled1ae862011-01-28 11:13:47 +0000516 break;
517
518 case EHScope::Cleanup:
James Y Knight53c76162015-07-17 18:21:37 +0000519 Size = static_cast<const EHCleanupScope *>(get())->getAllocatedSize();
John McCalled1ae862011-01-28 11:13:47 +0000520 break;
521
522 case EHScope::Terminate:
James Y Knight53c76162015-07-17 18:21:37 +0000523 Size = EHTerminateScope::getSize();
John McCalled1ae862011-01-28 11:13:47 +0000524 break;
David Majnemerdbf10452015-07-31 17:58:45 +0000525
Reid Kleckner2586aac2015-09-10 22:11:13 +0000526 case EHScope::PadEnd:
527 Size = EHPadEndScope::getSize();
David Majnemerdbf10452015-07-31 17:58:45 +0000528 break;
John McCalled1ae862011-01-28 11:13:47 +0000529 }
James Y Knight53c76162015-07-17 18:21:37 +0000530 Ptr += llvm::RoundUpToAlignment(Size, ScopeStackAlignment);
John McCalled1ae862011-01-28 11:13:47 +0000531 return *this;
532 }
533
534 iterator next() {
535 iterator copy = *this;
536 ++copy;
537 return copy;
538 }
539
540 iterator operator++(int) {
541 iterator copy = *this;
542 operator++();
543 return copy;
544 }
545
546 bool encloses(iterator other) const { return Ptr >= other.Ptr; }
547 bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; }
548
549 bool operator==(iterator other) const { return Ptr == other.Ptr; }
550 bool operator!=(iterator other) const { return Ptr != other.Ptr; }
551};
552
553inline EHScopeStack::iterator EHScopeStack::begin() const {
554 return iterator(StartOfData);
555}
556
557inline EHScopeStack::iterator EHScopeStack::end() const {
558 return iterator(EndOfBuffer);
559}
560
561inline void EHScopeStack::popCatch() {
562 assert(!empty() && "popping exception stack when not empty");
563
John McCall8e4c74b2011-08-11 02:22:43 +0000564 EHCatchScope &scope = cast<EHCatchScope>(*begin());
565 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000566 deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers()));
John McCalled1ae862011-01-28 11:13:47 +0000567}
568
569inline void EHScopeStack::popTerminate() {
570 assert(!empty() && "popping exception stack when not empty");
571
John McCall8e4c74b2011-08-11 02:22:43 +0000572 EHTerminateScope &scope = cast<EHTerminateScope>(*begin());
573 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000574 deallocate(EHTerminateScope::getSize());
John McCalled1ae862011-01-28 11:13:47 +0000575}
576
Reid Kleckner2586aac2015-09-10 22:11:13 +0000577inline void EHScopeStack::popPadEnd() {
David Majnemerdbf10452015-07-31 17:58:45 +0000578 assert(!empty() && "popping exception stack when not empty");
579
Reid Kleckner2586aac2015-09-10 22:11:13 +0000580 EHPadEndScope &scope = cast<EHPadEndScope>(*begin());
David Majnemerdbf10452015-07-31 17:58:45 +0000581 InnermostEHScope = scope.getEnclosingEHScope();
Reid Kleckner2586aac2015-09-10 22:11:13 +0000582 deallocate(EHPadEndScope::getSize());
David Majnemerdbf10452015-07-31 17:58:45 +0000583}
584
John McCalled1ae862011-01-28 11:13:47 +0000585inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
586 assert(sp.isValid() && "finding invalid savepoint");
587 assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
588 return iterator(EndOfBuffer - sp.Size);
589}
590
591inline EHScopeStack::stable_iterator
592EHScopeStack::stabilize(iterator ir) const {
593 assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
594 return stable_iterator(EndOfBuffer - ir.Ptr);
595}
596
David Majnemerc28d46e2015-07-22 23:46:21 +0000597/// The exceptions personality for a function.
598struct EHPersonality {
599 const char *PersonalityFn;
600
601 // If this is non-null, this personality requires a non-standard
602 // function for rethrowing an exception after a catchall cleanup.
603 // This function must have prototype void(void*).
604 const char *CatchallRethrowFn;
605
606 static const EHPersonality &get(CodeGenModule &CGM, const FunctionDecl *FD);
607 static const EHPersonality &get(CodeGenFunction &CGF);
608
609 static const EHPersonality GNU_C;
610 static const EHPersonality GNU_C_SJLJ;
611 static const EHPersonality GNU_C_SEH;
612 static const EHPersonality GNU_ObjC;
613 static const EHPersonality GNUstep_ObjC;
614 static const EHPersonality GNU_ObjCXX;
615 static const EHPersonality NeXT_ObjC;
616 static const EHPersonality GNU_CPlusPlus;
617 static const EHPersonality GNU_CPlusPlus_SJLJ;
618 static const EHPersonality GNU_CPlusPlus_SEH;
619 static const EHPersonality MSVC_except_handler;
620 static const EHPersonality MSVC_C_specific_handler;
621 static const EHPersonality MSVC_CxxFrameHandler3;
David Majnemerdbf10452015-07-31 17:58:45 +0000622
623 bool isMSVCPersonality() const {
624 return this == &MSVC_except_handler || this == &MSVC_C_specific_handler ||
625 this == &MSVC_CxxFrameHandler3;
626 }
627
628 bool isMSVCXXPersonality() const { return this == &MSVC_CxxFrameHandler3; }
David Majnemerc28d46e2015-07-22 23:46:21 +0000629};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000630}
631}
John McCalled1ae862011-01-28 11:13:47 +0000632
633#endif