forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentSource.h
305 lines (270 loc) · 9.09 KB
/
ArgumentSource.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//===--- ArgumentSource.h - Abstracted source of an argument ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A structure for holding an abstracted source of a call argument.
// It's either:
//
// - an expression, yielding an l-value or r-value
// - an RValue
// - an LValue
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_ARGUMENTSOURCE_H
#define SWIFT_LOWERING_ARGUMENTSOURCE_H
#include "RValue.h"
#include "LValue.h"
namespace swift {
namespace Lowering {
/// A means of generating an argument.
///
/// This is useful as a way to pass values around without either:
/// - requiring them to have already been evaluated or
/// - requiring them to come from an identifiable expression.
///
/// Being able to propagate values is important because there are a
/// number of cases (involving, say, property accessors) where values
/// are implicitly or previously generated. However, being able to
/// propagate Expr*s is also important because there are several kinds
/// of expressions (such as closures) which can be emitted more
/// efficiently with a known target abstraction level.
///
/// Because an ArgumentSource might contain an unevaluated expression,
/// care must be taken when dealing with multiple ArgumentSources to
/// preserve the original evaluation order of the program. APIs
/// working with multiple ArgumentSources should document the order in
/// which they plan to evaluate them.
class ArgumentSource {
union Storage {
struct {
RValue Value;
SILLocation Loc;
} TheRV;
struct {
LValue Value;
SILLocation Loc;
} TheLV;
Expr *TheExpr;
Storage() {}
~Storage() {}
} Storage;
enum class Kind : unsigned char {
RValue,
LValue,
Expr,
} StoredKind;
void initRV(SILLocation loc, RValue &&value) {
assert(StoredKind == Kind::RValue);
Storage.TheRV.Loc = loc;
new (&Storage.TheRV.Value) RValue(std::move(value));
}
void initLV(SILLocation loc, LValue &&value) {
assert(StoredKind == Kind::LValue);
Storage.TheLV.Loc = loc;
new (&Storage.TheLV.Value) LValue(std::move(value));
}
public:
ArgumentSource() : StoredKind(Kind::Expr) {
Storage.TheExpr = nullptr;
}
ArgumentSource(SILLocation loc, RValue &&value) : StoredKind(Kind::RValue) {
initRV(loc, std::move(value));
}
ArgumentSource(SILLocation loc, LValue &&value) : StoredKind(Kind::LValue) {
initLV(loc, std::move(value));
}
ArgumentSource(Expr *e) : StoredKind(Kind::Expr) {
assert(e && "initializing ArgumentSource with null expression");
Storage.TheExpr = e;
}
// Cannot be copied.
ArgumentSource(const ArgumentSource &other) = delete;
ArgumentSource &operator=(const ArgumentSource &other) = delete;
// Can be moved.
ArgumentSource(ArgumentSource &&other) : StoredKind(other.StoredKind) {
switch (StoredKind) {
case Kind::RValue:
initRV(other.getKnownRValueLocation(), std::move(other).asKnownRValue());
return;
case Kind::LValue:
initLV(other.getKnownLValueLocation(), std::move(other).asKnownLValue());
return;
case Kind::Expr:
Storage.TheExpr = std::move(other).asKnownExpr();
return;
}
llvm_unreachable("bad kind");
}
ArgumentSource &operator=(ArgumentSource &&other) {
// If the kinds don't align, just move the other object over this.
if (StoredKind != other.StoredKind) {
this->~ArgumentSource();
new (this) ArgumentSource(std::move(other));
return *this;
}
// Otherwise, move RValue and LValue objects in-place.
switch (StoredKind) {
case Kind::RValue:
Storage.TheRV.Value = std::move(other).asKnownRValue();
Storage.TheRV.Loc = other.getKnownRValueLocation();
return *this;
case Kind::LValue:
Storage.TheLV.Value = std::move(other).asKnownLValue();
Storage.TheLV.Loc = other.getKnownLValueLocation();
return *this;
case Kind::Expr:
Storage.TheExpr = std::move(other).asKnownExpr();
return *this;
}
llvm_unreachable("bad kind");
}
~ArgumentSource() {
switch (StoredKind) {
case Kind::RValue:
asKnownRValue().~RValue();
return;
case Kind::LValue:
asKnownLValue().~LValue();
return;
case Kind::Expr:
return;
}
llvm_unreachable("bad kind");
}
explicit operator bool() const & {
switch (StoredKind) {
case Kind::RValue:
return bool(asKnownRValue());
case Kind::LValue:
return asKnownLValue().isValid();
case Kind::Expr:
return asKnownExpr() != nullptr;
}
llvm_unreachable("bad kind");
}
CanType getSubstType() const & {
switch (StoredKind) {
case Kind::RValue:
return asKnownRValue().getType();
case Kind::LValue:
return CanInOutType::get(asKnownLValue().getSubstFormalType());
case Kind::Expr:
return asKnownExpr()->getType()->getCanonicalType();
}
llvm_unreachable("bad kind");
}
CanType getSubstRValueType() const & {
switch (StoredKind) {
case Kind::RValue:
return asKnownRValue().getType();
case Kind::LValue:
return asKnownLValue().getSubstFormalType();
case Kind::Expr:
return asKnownExpr()->getType()->getInOutObjectType()->getCanonicalType();
}
llvm_unreachable("bad kind");
}
bool hasLValueType() const & {
switch (StoredKind) {
case Kind::RValue: return false;
case Kind::LValue: return true;
case Kind::Expr: return asKnownExpr()->getType()->is<InOutType>();
}
llvm_unreachable("bad kind");
}
SILLocation getLocation() const & {
switch (StoredKind) {
case Kind::RValue:
return getKnownRValueLocation();
case Kind::LValue:
return getKnownLValueLocation();
case Kind::Expr:
return asKnownExpr();
}
llvm_unreachable("bad kind");
}
bool isRValue() const & { return StoredKind == Kind::RValue; }
bool isLValue() const & { return StoredKind == Kind::LValue; }
/// Given that this source is storing an RValue, extract and clear
/// that value.
RValue &&asKnownRValue() && {
assert(isRValue());
return std::move(Storage.TheRV.Value);
}
SILLocation getKnownRValueLocation() const & {
assert(isRValue());
return Storage.TheRV.Loc;
}
/// Given that this source is storing an LValue, extract and clear
/// that value.
LValue &&asKnownLValue() && {
assert(isLValue());
return std::move(Storage.TheLV.Value);
}
SILLocation getKnownLValueLocation() const & {
assert(isLValue());
return Storage.TheLV.Loc;
}
/// Given that this source is an expression, extract and clear
/// that expression.
Expr *asKnownExpr() && {
assert(StoredKind == Kind::Expr);
Expr *result = Storage.TheExpr;
Storage.TheExpr = nullptr;
return result;
}
/// Force this source to become an r-value, then return an unmoved
/// handle to that r-value.
RValue &forceAndPeekRValue(SILGenFunction &gen) &;
/// Return an unowned handle to the r-value stored in this source. Undefined
/// if this ArgumentSource is not an rvalue.
RValue &peekRValue() &;
RValue getAsRValue(SILGenFunction &gen, SGFContext C = SGFContext()) &&;
ManagedValue getAsSingleValue(SILGenFunction &gen,
SGFContext C = SGFContext()) &&;
ManagedValue getAsSingleValue(SILGenFunction &gen,
AbstractionPattern origFormalType,
SGFContext C = SGFContext()) &&;
void forwardInto(SILGenFunction &gen, Initialization *dest) &&;
void forwardInto(SILGenFunction &gen, AbstractionPattern origFormalType,
Initialization *dest, const TypeLowering &destTL) &&;
ManagedValue materialize(SILGenFunction &gen) &&;
/// Emit this value to memory so that it follows the abstraction
/// patterns of the original formal type.
///
/// \param expectedType - the lowering of getSubstType() under the
/// abstractions of origFormalType
ManagedValue materialize(SILGenFunction &gen,
AbstractionPattern origFormalType,
SILType expectedType = SILType()) &&;
// This is a hack and should be avoided.
void rewriteType(CanType newType) &;
private:
// Make the non-move accessors private to make it more difficult
// to accidentally re-emit values.
const RValue &asKnownRValue() const & {
assert(isRValue());
return Storage.TheRV.Value;
}
// Make the non-move accessors private to make it more difficult
// to accidentally re-emit values.
const LValue &asKnownLValue() const & {
assert(isLValue());
return Storage.TheLV.Value;
}
Expr *asKnownExpr() const & {
assert(StoredKind == Kind::Expr);
return Storage.TheExpr;
}
};
} // end namespace Lowering
} // end namespace swift
#endif