-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathScope.h
168 lines (135 loc) · 4.71 KB
/
Scope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===--- Scope.h - Declarations for scope RAII objects ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the Scope and FullExpr RAII objects.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SILGEN_SCOPE_H
#define SWIFT_SILGEN_SCOPE_H
#include "SILGenFunction.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/SILDebugScope.h"
#include "Cleanup.h"
namespace swift {
namespace Lowering {
/// A Scope is a RAII object recording that a scope (e.g. a brace
/// statement) has been entered.
class LLVM_LIBRARY_VISIBILITY Scope {
CleanupManager &cleanups;
CleanupsDepth depth;
Scope *savedInnermostScope;
CleanupLocation loc;
friend class CleanupManager;
public:
explicit Scope(CleanupManager &cleanups, CleanupLocation loc)
: cleanups(cleanups), depth(cleanups.getCleanupsDepth()),
savedInnermostScope(cleanups.innermostScope), loc(loc) {
assert(depth.isValid());
cleanups.innermostScope = this;
if (savedInnermostScope)
cleanups.stack.checkIterator(savedInnermostScope->depth);
}
Scope(const Scope &other) = delete;
Scope &operator=(const Scope &other) = delete;
Scope(Scope &&other)
: cleanups(other.cleanups), depth(other.depth),
savedInnermostScope(other.savedInnermostScope), loc(other.loc) {
// Invalidate other.
other.depth = CleanupsDepth::invalid();
}
Scope &operator=(Scope &&other) {
depth = other.depth;
savedInnermostScope = other.savedInnermostScope;
loc = other.loc;
// Invalidate other.
other.depth = CleanupsDepth::invalid();
return *this;
}
explicit Scope(SILGenFunction &SGF, SILLocation loc)
: Scope(SGF.Cleanups, CleanupLocation(loc)) {}
void pop() {
assert(depth.isValid() && "popping a scope twice!");
popImpl();
depth = CleanupsDepth::invalid();
}
~Scope() {
if (depth.isValid())
popImpl();
}
/// Verify that the invariants of this scope still hold.
void verify();
bool isValid() const { return depth.isValid(); }
/// Pop the scope pushing the +1 ManagedValue through the scope. Asserts if mv
/// is a plus zero managed value.
ManagedValue popPreservingValue(ManagedValue mv);
/// Pop this scope pushing the +1 rvalue through the scope. Asserts if rv is a
/// plus zero rvalue.
RValue popPreservingValue(RValue &&rv);
private:
/// Internal private implementation of popImpl so we can use it in Scope::pop
/// and in Scope's destructor.
void popImpl();
};
/// A scope that must be manually popped by the using code. If not
/// popped, the destructor asserts.
class LLVM_LIBRARY_VISIBILITY AssertingManualScope {
Scope scope;
public:
explicit AssertingManualScope(CleanupManager &cleanups, CleanupLocation loc)
: scope(cleanups, loc) {}
AssertingManualScope(AssertingManualScope &&other)
: scope(std::move(other.scope)) {}
AssertingManualScope &operator=(AssertingManualScope &&other) {
scope = std::move(other.scope);
return *this;
}
~AssertingManualScope() {
assert(!scope.isValid() && "Unpopped manual scope?!");
}
void pop() && { scope.pop(); }
};
/// A FullExpr is a RAII object recording that a full-expression has
/// been entered. A full-expression is essentially a very small scope
/// for the temporaries in an expression, with the added complexity
/// that (eventually, very likely) we have to deal with expressions
/// that are only conditionally evaluated.
class LLVM_LIBRARY_VISIBILITY FullExpr : private Scope {
public:
explicit FullExpr(CleanupManager &cleanups, CleanupLocation loc)
: Scope(cleanups, loc) {}
using Scope::pop;
};
/// A LexicalScope is a Scope that is also exposed to the debug info.
class LLVM_LIBRARY_VISIBILITY LexicalScope : private Scope {
SILGenFunction& SGF;
public:
explicit LexicalScope(SILGenFunction &SGF, CleanupLocation loc)
: Scope(SGF.Cleanups, loc), SGF(SGF) {
SGF.enterDebugScope(loc);
}
using Scope::pop;
~LexicalScope() {
SGF.leaveDebugScope();
}
};
/// A scope that only exists in the debug info.
class LLVM_LIBRARY_VISIBILITY DebugScope {
SILGenFunction &SGF;
public:
explicit DebugScope(SILGenFunction &SGF, SILLocation loc) : SGF(SGF) {
SGF.enterDebugScope(loc);
}
~DebugScope() { SGF.leaveDebugScope(); }
};
} // end namespace Lowering
} // end namespace swift
#endif