blob: ffe0f9d9dd2095c24d835a3656ecb2c92ba71d4d [file] [log] [blame]
John McCalled1ae862011-01-28 11:13:47 +00001//===-- CGCleanup.h - Classes for cleanups IR generation --------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
John McCalled1ae862011-01-28 11:13:47 +00006//
7//===----------------------------------------------------------------------===//
8//
9// These classes support the generation of LLVM IR for cleanups.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
14#define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
John McCalled1ae862011-01-28 11:13:47 +000015
Reid Klecknerd29f1342013-06-19 17:07:50 +000016#include "EHScopeStack.h"
John McCall7f416cc2015-09-08 08:05:57 +000017
18#include "Address.h"
Reid Kleckner200fe222013-06-09 16:45:02 +000019#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallVector.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000021
22namespace llvm {
23class BasicBlock;
24class Value;
25class ConstantInt;
26class AllocaInst;
27}
John McCalled1ae862011-01-28 11:13:47 +000028
29namespace clang {
David Majnemerc28d46e2015-07-22 23:46:21 +000030class FunctionDecl;
John McCalled1ae862011-01-28 11:13:47 +000031namespace CodeGen {
David Majnemerc28d46e2015-07-22 23:46:21 +000032class CodeGenModule;
33class CodeGenFunction;
John McCalled1ae862011-01-28 11:13:47 +000034
Reid Kleckner10aa7702015-09-16 20:15:55 +000035/// The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the
36/// type of a catch handler, so we use this wrapper.
37struct CatchTypeInfo {
38 llvm::Constant *RTTI;
39 unsigned Flags;
40};
41
John McCalled1ae862011-01-28 11:13:47 +000042/// A protected scope for zero-cost EH handling.
43class EHScope {
44 llvm::BasicBlock *CachedLandingPad;
John McCall8e4c74b2011-08-11 02:22:43 +000045 llvm::BasicBlock *CachedEHDispatchBlock;
John McCalled1ae862011-01-28 11:13:47 +000046
John McCall8e4c74b2011-08-11 02:22:43 +000047 EHScopeStack::stable_iterator EnclosingEHScope;
48
49 class CommonBitFields {
50 friend class EHScope;
David Majnemerdbf10452015-07-31 17:58:45 +000051 unsigned Kind : 3;
John McCall8e4c74b2011-08-11 02:22:43 +000052 };
David Majnemerdbf10452015-07-31 17:58:45 +000053 enum { NumCommonBits = 3 };
John McCalled1ae862011-01-28 11:13:47 +000054
55protected:
John McCall8e4c74b2011-08-11 02:22:43 +000056 class CatchBitFields {
57 friend class EHCatchScope;
58 unsigned : NumCommonBits;
59
60 unsigned NumHandlers : 32 - NumCommonBits;
61 };
62
63 class CleanupBitFields {
64 friend class EHCleanupScope;
65 unsigned : NumCommonBits;
66
67 /// Whether this cleanup needs to be run along normal edges.
68 unsigned IsNormalCleanup : 1;
69
70 /// Whether this cleanup needs to be run along exception edges.
71 unsigned IsEHCleanup : 1;
72
73 /// Whether this cleanup is currently active.
74 unsigned IsActive : 1;
75
David Majnemerdc012fa2015-04-22 21:38:15 +000076 /// Whether this cleanup is a lifetime marker
77 unsigned IsLifetimeMarker : 1;
78
John McCall8e4c74b2011-08-11 02:22:43 +000079 /// Whether the normal cleanup should test the activation flag.
80 unsigned TestFlagInNormalCleanup : 1;
81
82 /// Whether the EH cleanup should test the activation flag.
83 unsigned TestFlagInEHCleanup : 1;
84
85 /// The amount of extra storage needed by the Cleanup.
86 /// Always a multiple of the scope-stack alignment.
87 unsigned CleanupSize : 12;
John McCall8e4c74b2011-08-11 02:22:43 +000088 };
89
90 class FilterBitFields {
91 friend class EHFilterScope;
92 unsigned : NumCommonBits;
93
94 unsigned NumFilters : 32 - NumCommonBits;
95 };
96
97 union {
98 CommonBitFields CommonBits;
99 CatchBitFields CatchBits;
100 CleanupBitFields CleanupBits;
101 FilterBitFields FilterBits;
102 };
John McCalled1ae862011-01-28 11:13:47 +0000103
104public:
Reid Kleckner2586aac2015-09-10 22:11:13 +0000105 enum Kind { Cleanup, Catch, Terminate, Filter, PadEnd };
John McCalled1ae862011-01-28 11:13:47 +0000106
John McCall8e4c74b2011-08-11 02:22:43 +0000107 EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
Craig Topper8a13c412014-05-21 05:09:00 +0000108 : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),
John McCall8e4c74b2011-08-11 02:22:43 +0000109 EnclosingEHScope(enclosingEHScope) {
110 CommonBits.Kind = kind;
111 }
John McCalled1ae862011-01-28 11:13:47 +0000112
John McCall8e4c74b2011-08-11 02:22:43 +0000113 Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); }
John McCalled1ae862011-01-28 11:13:47 +0000114
115 llvm::BasicBlock *getCachedLandingPad() const {
116 return CachedLandingPad;
117 }
118
John McCall8e4c74b2011-08-11 02:22:43 +0000119 void setCachedLandingPad(llvm::BasicBlock *block) {
120 CachedLandingPad = block;
121 }
122
123 llvm::BasicBlock *getCachedEHDispatchBlock() const {
124 return CachedEHDispatchBlock;
125 }
126
127 void setCachedEHDispatchBlock(llvm::BasicBlock *block) {
128 CachedEHDispatchBlock = block;
129 }
130
131 bool hasEHBranches() const {
132 if (llvm::BasicBlock *block = getCachedEHDispatchBlock())
133 return !block->use_empty();
134 return false;
135 }
136
137 EHScopeStack::stable_iterator getEnclosingEHScope() const {
138 return EnclosingEHScope;
John McCalled1ae862011-01-28 11:13:47 +0000139 }
140};
141
142/// A scope which attempts to handle some, possibly all, types of
143/// exceptions.
144///
James Dennettbe302452012-06-15 22:10:14 +0000145/// Objective C \@finally blocks are represented using a cleanup scope
John McCalled1ae862011-01-28 11:13:47 +0000146/// after the catch scope.
147class EHCatchScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000148 // In effect, we have a flexible array member
149 // Handler Handlers[0];
150 // But that's only standard in C99, not C++, so we have to do
151 // annoying pointer arithmetic instead.
152
153public:
154 struct Handler {
155 /// A type info value, or null (C++ null, not an LLVM null pointer)
156 /// for a catch-all.
Reid Kleckner10aa7702015-09-16 20:15:55 +0000157 CatchTypeInfo Type;
John McCalled1ae862011-01-28 11:13:47 +0000158
159 /// The catch handler for this type.
160 llvm::BasicBlock *Block;
161
Reid Kleckner10aa7702015-09-16 20:15:55 +0000162 bool isCatchAll() const { return Type.RTTI == nullptr; }
John McCalled1ae862011-01-28 11:13:47 +0000163 };
164
165private:
166 friend class EHScopeStack;
167
168 Handler *getHandlers() {
169 return reinterpret_cast<Handler*>(this+1);
170 }
171
172 const Handler *getHandlers() const {
173 return reinterpret_cast<const Handler*>(this+1);
174 }
175
176public:
177 static size_t getSizeForNumHandlers(unsigned N) {
178 return sizeof(EHCatchScope) + N * sizeof(Handler);
179 }
180
John McCall8e4c74b2011-08-11 02:22:43 +0000181 EHCatchScope(unsigned numHandlers,
182 EHScopeStack::stable_iterator enclosingEHScope)
183 : EHScope(Catch, enclosingEHScope) {
184 CatchBits.NumHandlers = numHandlers;
Hans Wennborg9565cf52016-06-22 16:21:14 +0000185 assert(CatchBits.NumHandlers == numHandlers && "NumHandlers overflow?");
John McCalled1ae862011-01-28 11:13:47 +0000186 }
187
188 unsigned getNumHandlers() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000189 return CatchBits.NumHandlers;
John McCalled1ae862011-01-28 11:13:47 +0000190 }
191
192 void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
Reid Kleckner10aa7702015-09-16 20:15:55 +0000193 setHandler(I, CatchTypeInfo{nullptr, 0}, Block);
John McCalled1ae862011-01-28 11:13:47 +0000194 }
195
Rafael Espindolabb9e7a32014-06-04 18:51:46 +0000196 void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
John McCalled1ae862011-01-28 11:13:47 +0000197 assert(I < getNumHandlers());
Reid Kleckner10aa7702015-09-16 20:15:55 +0000198 getHandlers()[I].Type = CatchTypeInfo{Type, 0};
199 getHandlers()[I].Block = Block;
200 }
201
202 void setHandler(unsigned I, CatchTypeInfo Type, llvm::BasicBlock *Block) {
203 assert(I < getNumHandlers());
John McCalled1ae862011-01-28 11:13:47 +0000204 getHandlers()[I].Type = Type;
205 getHandlers()[I].Block = Block;
206 }
207
208 const Handler &getHandler(unsigned I) const {
209 assert(I < getNumHandlers());
210 return getHandlers()[I];
211 }
212
Kostya Serebryanyba4aced2014-01-09 09:22:32 +0000213 // Clear all handler blocks.
214 // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
215 // 'takeHandler' or some such function which removes ownership from the
216 // EHCatchScope object if the handlers should live longer than EHCatchScope.
217 void clearHandlerBlocks() {
218 for (unsigned I = 0, N = getNumHandlers(); I != N; ++I)
219 delete getHandler(I).Block;
220 }
221
John McCalled1ae862011-01-28 11:13:47 +0000222 typedef const Handler *iterator;
223 iterator begin() const { return getHandlers(); }
224 iterator end() const { return getHandlers() + getNumHandlers(); }
225
226 static bool classof(const EHScope *Scope) {
227 return Scope->getKind() == Catch;
228 }
229};
230
231/// A cleanup scope which generates the cleanup blocks lazily.
Richard Smith7027ffa2018-07-17 22:24:11 +0000232class alignas(8) EHCleanupScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000233 /// The nearest normal cleanup scope enclosing this one.
234 EHScopeStack::stable_iterator EnclosingNormal;
235
John McCall8e4c74b2011-08-11 02:22:43 +0000236 /// The nearest EH scope enclosing this one.
John McCalled1ae862011-01-28 11:13:47 +0000237 EHScopeStack::stable_iterator EnclosingEH;
238
239 /// The dual entry/exit block along the normal edge. This is lazily
240 /// created if needed before the cleanup is popped.
241 llvm::BasicBlock *NormalBlock;
242
John McCalled1ae862011-01-28 11:13:47 +0000243 /// An optional i1 variable indicating whether this cleanup has been
244 /// activated yet.
245 llvm::AllocaInst *ActiveFlag;
246
247 /// Extra information required for cleanups that have resolved
248 /// branches through them. This has to be allocated on the side
249 /// because everything on the cleanup stack has be trivially
250 /// movable.
251 struct ExtInfo {
252 /// The destinations of normal branch-afters and branch-throughs.
253 llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches;
254
255 /// Normal branch-afters.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000256 SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4>
John McCalled1ae862011-01-28 11:13:47 +0000257 BranchAfters;
John McCalled1ae862011-01-28 11:13:47 +0000258 };
259 mutable struct ExtInfo *ExtInfo;
260
Hans Wennborg9565cf52016-06-22 16:21:14 +0000261 /// The number of fixups required by enclosing scopes (not including
262 /// this one). If this is the top cleanup scope, all the fixups
263 /// from this index onwards belong to this scope.
264 unsigned FixupDepth;
265
John McCalled1ae862011-01-28 11:13:47 +0000266 struct ExtInfo &getExtInfo() {
267 if (!ExtInfo) ExtInfo = new struct ExtInfo();
268 return *ExtInfo;
269 }
270
271 const struct ExtInfo &getExtInfo() const {
272 if (!ExtInfo) ExtInfo = new struct ExtInfo();
273 return *ExtInfo;
274 }
275
276public:
277 /// Gets the size required for a lazy cleanup scope with the given
278 /// cleanup-data requirements.
279 static size_t getSizeForCleanupSize(size_t Size) {
280 return sizeof(EHCleanupScope) + Size;
281 }
282
283 size_t getAllocatedSize() const {
John McCall8e4c74b2011-08-11 02:22:43 +0000284 return sizeof(EHCleanupScope) + CleanupBits.CleanupSize;
John McCalled1ae862011-01-28 11:13:47 +0000285 }
286
John McCall8e4c74b2011-08-11 02:22:43 +0000287 EHCleanupScope(bool isNormal, bool isEH, bool isActive,
288 unsigned cleanupSize, unsigned fixupDepth,
289 EHScopeStack::stable_iterator enclosingNormal,
290 EHScopeStack::stable_iterator enclosingEH)
Hans Wennborg9565cf52016-06-22 16:21:14 +0000291 : EHScope(EHScope::Cleanup, enclosingEH),
292 EnclosingNormal(enclosingNormal), NormalBlock(nullptr),
293 ActiveFlag(nullptr), ExtInfo(nullptr), FixupDepth(fixupDepth) {
John McCall8e4c74b2011-08-11 02:22:43 +0000294 CleanupBits.IsNormalCleanup = isNormal;
295 CleanupBits.IsEHCleanup = isEH;
296 CleanupBits.IsActive = isActive;
David Majnemerdc012fa2015-04-22 21:38:15 +0000297 CleanupBits.IsLifetimeMarker = false;
John McCall8e4c74b2011-08-11 02:22:43 +0000298 CleanupBits.TestFlagInNormalCleanup = false;
299 CleanupBits.TestFlagInEHCleanup = false;
300 CleanupBits.CleanupSize = cleanupSize;
John McCall8e4c74b2011-08-11 02:22:43 +0000301
302 assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");
John McCalled1ae862011-01-28 11:13:47 +0000303 }
304
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000305 void Destroy() {
John McCalled1ae862011-01-28 11:13:47 +0000306 delete ExtInfo;
307 }
Kostya Serebryanyb21aa762014-10-08 18:31:54 +0000308 // Objects of EHCleanupScope are not destructed. Use Destroy().
Aaron Ballmanabc18922015-02-15 22:54:08 +0000309 ~EHCleanupScope() = delete;
John McCalled1ae862011-01-28 11:13:47 +0000310
John McCall8e4c74b2011-08-11 02:22:43 +0000311 bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000312 llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
313 void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }
314
John McCall8e4c74b2011-08-11 02:22:43 +0000315 bool isEHCleanup() const { return CleanupBits.IsEHCleanup; }
John McCalled1ae862011-01-28 11:13:47 +0000316
John McCall8e4c74b2011-08-11 02:22:43 +0000317 bool isActive() const { return CleanupBits.IsActive; }
318 void setActive(bool A) { CleanupBits.IsActive = A; }
John McCalled1ae862011-01-28 11:13:47 +0000319
David Majnemerdc012fa2015-04-22 21:38:15 +0000320 bool isLifetimeMarker() const { return CleanupBits.IsLifetimeMarker; }
321 void setLifetimeMarker() { CleanupBits.IsLifetimeMarker = true; }
322
John McCall7f416cc2015-09-08 08:05:57 +0000323 bool hasActiveFlag() const { return ActiveFlag != nullptr; }
324 Address getActiveFlag() const {
325 return Address(ActiveFlag, CharUnits::One());
326 }
327 void setActiveFlag(Address Var) {
328 assert(Var.getAlignment().isOne());
329 ActiveFlag = cast<llvm::AllocaInst>(Var.getPointer());
330 }
John McCalled1ae862011-01-28 11:13:47 +0000331
John McCall8e4c74b2011-08-11 02:22:43 +0000332 void setTestFlagInNormalCleanup() {
333 CleanupBits.TestFlagInNormalCleanup = true;
334 }
335 bool shouldTestFlagInNormalCleanup() const {
336 return CleanupBits.TestFlagInNormalCleanup;
337 }
John McCalled1ae862011-01-28 11:13:47 +0000338
John McCall8e4c74b2011-08-11 02:22:43 +0000339 void setTestFlagInEHCleanup() {
340 CleanupBits.TestFlagInEHCleanup = true;
341 }
342 bool shouldTestFlagInEHCleanup() const {
343 return CleanupBits.TestFlagInEHCleanup;
344 }
John McCalled1ae862011-01-28 11:13:47 +0000345
Hans Wennborg9565cf52016-06-22 16:21:14 +0000346 unsigned getFixupDepth() const { return FixupDepth; }
John McCalled1ae862011-01-28 11:13:47 +0000347 EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
348 return EnclosingNormal;
349 }
John McCalled1ae862011-01-28 11:13:47 +0000350
John McCall8e4c74b2011-08-11 02:22:43 +0000351 size_t getCleanupSize() const { return CleanupBits.CleanupSize; }
John McCalled1ae862011-01-28 11:13:47 +0000352 void *getCleanupBuffer() { return this + 1; }
353
354 EHScopeStack::Cleanup *getCleanup() {
355 return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer());
356 }
357
358 /// True if this cleanup scope has any branch-afters or branch-throughs.
359 bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }
360
361 /// Add a branch-after to this cleanup scope. A branch-after is a
362 /// branch from a point protected by this (normal) cleanup to a
363 /// point in the normal cleanup scope immediately containing it.
364 /// For example,
365 /// for (;;) { A a; break; }
366 /// contains a branch-after.
367 ///
368 /// Branch-afters each have their own destination out of the
369 /// cleanup, guaranteed distinct from anything else threaded through
370 /// it. Therefore branch-afters usually force a switch after the
371 /// cleanup.
372 void addBranchAfter(llvm::ConstantInt *Index,
373 llvm::BasicBlock *Block) {
374 struct ExtInfo &ExtInfo = getExtInfo();
David Blaikie82e95a32014-11-19 07:49:47 +0000375 if (ExtInfo.Branches.insert(Block).second)
John McCalled1ae862011-01-28 11:13:47 +0000376 ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));
377 }
378
379 /// Return the number of unique branch-afters on this scope.
380 unsigned getNumBranchAfters() const {
381 return ExtInfo ? ExtInfo->BranchAfters.size() : 0;
382 }
383
384 llvm::BasicBlock *getBranchAfterBlock(unsigned I) const {
385 assert(I < getNumBranchAfters());
386 return ExtInfo->BranchAfters[I].first;
387 }
388
389 llvm::ConstantInt *getBranchAfterIndex(unsigned I) const {
390 assert(I < getNumBranchAfters());
391 return ExtInfo->BranchAfters[I].second;
392 }
393
394 /// Add a branch-through to this cleanup scope. A branch-through is
395 /// a branch from a scope protected by this (normal) cleanup to an
396 /// enclosing scope other than the immediately-enclosing normal
397 /// cleanup scope.
398 ///
399 /// In the following example, the branch through B's scope is a
400 /// branch-through, while the branch through A's scope is a
401 /// branch-after:
402 /// for (;;) { A a; B b; break; }
403 ///
404 /// All branch-throughs have a common destination out of the
405 /// cleanup, one possibly shared with the fall-through. Therefore
406 /// branch-throughs usually don't force a switch after the cleanup.
407 ///
408 /// \return true if the branch-through was new to this scope
409 bool addBranchThrough(llvm::BasicBlock *Block) {
David Blaikie82e95a32014-11-19 07:49:47 +0000410 return getExtInfo().Branches.insert(Block).second;
John McCalled1ae862011-01-28 11:13:47 +0000411 }
412
413 /// Determines if this cleanup scope has any branch throughs.
414 bool hasBranchThroughs() const {
415 if (!ExtInfo) return false;
416 return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());
417 }
418
John McCalled1ae862011-01-28 11:13:47 +0000419 static bool classof(const EHScope *Scope) {
420 return (Scope->getKind() == Cleanup);
421 }
422};
James Y Knight53c76162015-07-17 18:21:37 +0000423// NOTE: there's a bunch of different data classes tacked on after an
424// EHCleanupScope. It is asserted (in EHScopeStack::pushCleanup*) that
425// they don't require greater alignment than ScopeStackAlignment. So,
426// EHCleanupScope ought to have alignment equal to that -- not more
427// (would be misaligned by the stack allocator), and not less (would
428// break the appended classes).
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000429static_assert(alignof(EHCleanupScope) == EHScopeStack::ScopeStackAlignment,
James Y Knight53c76162015-07-17 18:21:37 +0000430 "EHCleanupScope expected alignment");
John McCalled1ae862011-01-28 11:13:47 +0000431
432/// An exceptions scope which filters exceptions thrown through it.
433/// Only exceptions matching the filter types will be permitted to be
434/// thrown.
435///
436/// This is used to implement C++ exception specifications.
437class EHFilterScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000438 // Essentially ends in a flexible array member:
439 // llvm::Value *FilterTypes[0];
440
441 llvm::Value **getFilters() {
442 return reinterpret_cast<llvm::Value**>(this+1);
443 }
444
445 llvm::Value * const *getFilters() const {
446 return reinterpret_cast<llvm::Value* const *>(this+1);
447 }
448
449public:
John McCall8e4c74b2011-08-11 02:22:43 +0000450 EHFilterScope(unsigned numFilters)
451 : EHScope(Filter, EHScopeStack::stable_end()) {
452 FilterBits.NumFilters = numFilters;
Hans Wennborg9565cf52016-06-22 16:21:14 +0000453 assert(FilterBits.NumFilters == numFilters && "NumFilters overflow");
John McCalled1ae862011-01-28 11:13:47 +0000454 }
455
John McCall8e4c74b2011-08-11 02:22:43 +0000456 static size_t getSizeForNumFilters(unsigned numFilters) {
457 return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);
John McCalled1ae862011-01-28 11:13:47 +0000458 }
459
John McCall8e4c74b2011-08-11 02:22:43 +0000460 unsigned getNumFilters() const { return FilterBits.NumFilters; }
461
462 void setFilter(unsigned i, llvm::Value *filterValue) {
463 assert(i < getNumFilters());
464 getFilters()[i] = filterValue;
John McCalled1ae862011-01-28 11:13:47 +0000465 }
466
John McCall8e4c74b2011-08-11 02:22:43 +0000467 llvm::Value *getFilter(unsigned i) const {
468 assert(i < getNumFilters());
469 return getFilters()[i];
470 }
471
472 static bool classof(const EHScope *scope) {
473 return scope->getKind() == Filter;
John McCalled1ae862011-01-28 11:13:47 +0000474 }
475};
476
477/// An exceptions scope which calls std::terminate if any exception
478/// reaches it.
479class EHTerminateScope : public EHScope {
John McCalled1ae862011-01-28 11:13:47 +0000480public:
John McCall8e4c74b2011-08-11 02:22:43 +0000481 EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)
482 : EHScope(Terminate, enclosingEHScope) {}
John McCalled1ae862011-01-28 11:13:47 +0000483 static size_t getSize() { return sizeof(EHTerminateScope); }
484
John McCall8e4c74b2011-08-11 02:22:43 +0000485 static bool classof(const EHScope *scope) {
486 return scope->getKind() == Terminate;
John McCalled1ae862011-01-28 11:13:47 +0000487 }
488};
489
Reid Kleckner2586aac2015-09-10 22:11:13 +0000490class EHPadEndScope : public EHScope {
David Majnemerdbf10452015-07-31 17:58:45 +0000491public:
Reid Kleckner2586aac2015-09-10 22:11:13 +0000492 EHPadEndScope(EHScopeStack::stable_iterator enclosingEHScope)
493 : EHScope(PadEnd, enclosingEHScope) {}
494 static size_t getSize() { return sizeof(EHPadEndScope); }
David Majnemerdbf10452015-07-31 17:58:45 +0000495
496 static bool classof(const EHScope *scope) {
Reid Kleckner2586aac2015-09-10 22:11:13 +0000497 return scope->getKind() == PadEnd;
David Majnemerdbf10452015-07-31 17:58:45 +0000498 }
499};
500
John McCalled1ae862011-01-28 11:13:47 +0000501/// A non-stable pointer into the scope stack.
502class EHScopeStack::iterator {
503 char *Ptr;
504
505 friend class EHScopeStack;
506 explicit iterator(char *Ptr) : Ptr(Ptr) {}
507
508public:
Craig Topper8a13c412014-05-21 05:09:00 +0000509 iterator() : Ptr(nullptr) {}
John McCalled1ae862011-01-28 11:13:47 +0000510
Fangrui Song6907ce22018-07-30 19:24:48 +0000511 EHScope *get() const {
John McCalled1ae862011-01-28 11:13:47 +0000512 return reinterpret_cast<EHScope*>(Ptr);
513 }
514
515 EHScope *operator->() const { return get(); }
516 EHScope &operator*() const { return *get(); }
517
518 iterator &operator++() {
James Y Knight53c76162015-07-17 18:21:37 +0000519 size_t Size;
John McCalled1ae862011-01-28 11:13:47 +0000520 switch (get()->getKind()) {
521 case EHScope::Catch:
James Y Knight53c76162015-07-17 18:21:37 +0000522 Size = EHCatchScope::getSizeForNumHandlers(
523 static_cast<const EHCatchScope *>(get())->getNumHandlers());
John McCalled1ae862011-01-28 11:13:47 +0000524 break;
525
526 case EHScope::Filter:
James Y Knight53c76162015-07-17 18:21:37 +0000527 Size = EHFilterScope::getSizeForNumFilters(
528 static_cast<const EHFilterScope *>(get())->getNumFilters());
John McCalled1ae862011-01-28 11:13:47 +0000529 break;
530
531 case EHScope::Cleanup:
James Y Knight53c76162015-07-17 18:21:37 +0000532 Size = static_cast<const EHCleanupScope *>(get())->getAllocatedSize();
John McCalled1ae862011-01-28 11:13:47 +0000533 break;
534
535 case EHScope::Terminate:
James Y Knight53c76162015-07-17 18:21:37 +0000536 Size = EHTerminateScope::getSize();
John McCalled1ae862011-01-28 11:13:47 +0000537 break;
David Majnemerdbf10452015-07-31 17:58:45 +0000538
Reid Kleckner2586aac2015-09-10 22:11:13 +0000539 case EHScope::PadEnd:
540 Size = EHPadEndScope::getSize();
David Majnemerdbf10452015-07-31 17:58:45 +0000541 break;
John McCalled1ae862011-01-28 11:13:47 +0000542 }
Rui Ueyama83aa9792016-01-14 21:00:27 +0000543 Ptr += llvm::alignTo(Size, ScopeStackAlignment);
John McCalled1ae862011-01-28 11:13:47 +0000544 return *this;
545 }
546
547 iterator next() {
548 iterator copy = *this;
549 ++copy;
550 return copy;
551 }
552
553 iterator operator++(int) {
554 iterator copy = *this;
555 operator++();
556 return copy;
557 }
558
559 bool encloses(iterator other) const { return Ptr >= other.Ptr; }
560 bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; }
561
562 bool operator==(iterator other) const { return Ptr == other.Ptr; }
563 bool operator!=(iterator other) const { return Ptr != other.Ptr; }
564};
565
566inline EHScopeStack::iterator EHScopeStack::begin() const {
567 return iterator(StartOfData);
568}
569
570inline EHScopeStack::iterator EHScopeStack::end() const {
571 return iterator(EndOfBuffer);
572}
573
574inline void EHScopeStack::popCatch() {
575 assert(!empty() && "popping exception stack when not empty");
576
John McCall8e4c74b2011-08-11 02:22:43 +0000577 EHCatchScope &scope = cast<EHCatchScope>(*begin());
578 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000579 deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers()));
John McCalled1ae862011-01-28 11:13:47 +0000580}
581
582inline void EHScopeStack::popTerminate() {
583 assert(!empty() && "popping exception stack when not empty");
584
John McCall8e4c74b2011-08-11 02:22:43 +0000585 EHTerminateScope &scope = cast<EHTerminateScope>(*begin());
586 InnermostEHScope = scope.getEnclosingEHScope();
James Y Knight53c76162015-07-17 18:21:37 +0000587 deallocate(EHTerminateScope::getSize());
John McCalled1ae862011-01-28 11:13:47 +0000588}
589
590inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
591 assert(sp.isValid() && "finding invalid savepoint");
592 assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
593 return iterator(EndOfBuffer - sp.Size);
594}
595
596inline EHScopeStack::stable_iterator
597EHScopeStack::stabilize(iterator ir) const {
598 assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
599 return stable_iterator(EndOfBuffer - ir.Ptr);
600}
601
David Majnemerc28d46e2015-07-22 23:46:21 +0000602/// The exceptions personality for a function.
603struct EHPersonality {
604 const char *PersonalityFn;
605
606 // If this is non-null, this personality requires a non-standard
607 // function for rethrowing an exception after a catchall cleanup.
608 // This function must have prototype void(void*).
609 const char *CatchallRethrowFn;
610
611 static const EHPersonality &get(CodeGenModule &CGM, const FunctionDecl *FD);
612 static const EHPersonality &get(CodeGenFunction &CGF);
613
614 static const EHPersonality GNU_C;
615 static const EHPersonality GNU_C_SJLJ;
616 static const EHPersonality GNU_C_SEH;
617 static const EHPersonality GNU_ObjC;
Benjamin Kramer796c1d92017-01-08 22:58:07 +0000618 static const EHPersonality GNU_ObjC_SJLJ;
619 static const EHPersonality GNU_ObjC_SEH;
David Majnemerc28d46e2015-07-22 23:46:21 +0000620 static const EHPersonality GNUstep_ObjC;
621 static const EHPersonality GNU_ObjCXX;
622 static const EHPersonality NeXT_ObjC;
623 static const EHPersonality GNU_CPlusPlus;
624 static const EHPersonality GNU_CPlusPlus_SJLJ;
625 static const EHPersonality GNU_CPlusPlus_SEH;
626 static const EHPersonality MSVC_except_handler;
627 static const EHPersonality MSVC_C_specific_handler;
628 static const EHPersonality MSVC_CxxFrameHandler3;
Heejin Ahnc6479192018-05-31 22:18:13 +0000629 static const EHPersonality GNU_Wasm_CPlusPlus;
David Majnemerdbf10452015-07-31 17:58:45 +0000630
Reid Kleckner129552b2015-10-08 01:13:52 +0000631 /// Does this personality use landingpads or the family of pad instructions
632 /// designed to form funclets?
Heejin Ahnc6479192018-05-31 22:18:13 +0000633 bool usesFuncletPads() const {
634 return isMSVCPersonality() || isWasmPersonality();
635 }
Reid Kleckner129552b2015-10-08 01:13:52 +0000636
David Majnemerdbf10452015-07-31 17:58:45 +0000637 bool isMSVCPersonality() const {
638 return this == &MSVC_except_handler || this == &MSVC_C_specific_handler ||
639 this == &MSVC_CxxFrameHandler3;
640 }
641
Heejin Ahnc6479192018-05-31 22:18:13 +0000642 bool isWasmPersonality() const { return this == &GNU_Wasm_CPlusPlus; }
643
David Majnemerdbf10452015-07-31 17:58:45 +0000644 bool isMSVCXXPersonality() const { return this == &MSVC_CxxFrameHandler3; }
David Majnemerc28d46e2015-07-22 23:46:21 +0000645};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000646}
647}
John McCalled1ae862011-01-28 11:13:47 +0000648
649#endif