-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathArgumentSource.cpp
219 lines (189 loc) · 7.68 KB
/
ArgumentSource.cpp
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
//===--- ArgumentSource.cpp - Latent value representation -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// A structure for holding a r-value or l-value
//
//===----------------------------------------------------------------------===//
#include "ArgumentSource.h"
#include "Initialization.h"
using namespace swift;
using namespace Lowering;
RValue &ArgumentSource::forceAndPeekRValue(SILGenFunction &SGF) & {
if (isRValue()) {
return peekRValue();
}
auto expr = asKnownExpr();
StoredKind = Kind::RValue;
new (&Storage.TheRV.Value) RValue(SGF.emitRValue(expr));
Storage.TheRV.Loc = expr;
return Storage.TheRV.Value;
}
RValue &ArgumentSource::peekRValue() & {
assert(isRValue() && "Undefined behavior to call this method without the "
"ArgumentSource actually being an RValue");
return Storage.TheRV.Value;
}
void ArgumentSource::rewriteType(CanType newType) & {
assert(!isLValue());
if (isRValue()) {
Storage.TheRV.Value.rewriteType(newType);
} else {
Expr *expr = Storage.TheExpr;
if (expr->getType()->isEqual(newType)) return;
llvm_unreachable("unimplemented! hope it doesn't happen");
}
}
bool ArgumentSource::requiresCalleeToEvaluate() {
switch (StoredKind) {
case Kind::RValue:
case Kind::LValue:
return false;
case Kind::Expr:
// FIXME: TupleShuffleExprs come in two flavors:
//
// 1) as apply arguments, where they're used to insert default
// argument value and collect varargs
//
// 2) as tuple conversions, where they can introduce, eliminate
// and re-order fields
//
// Case 1) must be emitted by ArgEmitter, and Case 2) must be
// emitted by RValueEmitter.
//
// It would be good to split up TupleShuffleExpr into these two
// cases, and simplify ArgEmitter since it no longer has to deal
// with re-ordering. However for now, SubscriptExpr emits the
// index argument via the RValueEmitter, so the RValueEmitter has
// to know about varargs, duplicating some of the logic in
// ArgEmitter.
//
// Once this is fixed, we can also consider allowing subscripts
// to have default arguments.
if (auto *shuffleExpr = dyn_cast<TupleShuffleExpr>(asKnownExpr())) {
for (auto index : shuffleExpr->getElementMapping()) {
if (index == TupleShuffleExpr::DefaultInitialize ||
index == TupleShuffleExpr::CallerDefaultInitialize ||
index == TupleShuffleExpr::Variadic)
return true;
}
}
return false;
}
llvm_unreachable("Unhandled Kind in switch.");
}
RValue ArgumentSource::getAsRValue(SILGenFunction &SGF, SGFContext C) && {
assert(!isLValue());
if (isRValue())
return std::move(*this).asKnownRValue();
return SGF.emitRValue(std::move(*this).asKnownExpr(), C);
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &SGF,
SGFContext C) && {
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().getAsSingleValue(SGF, loc);
}
if (isLValue()) {
auto loc = getKnownLValueLocation();
return SGF.emitAddressOfLValue(loc, std::move(*this).asKnownLValue(),
AccessKind::ReadWrite);
}
auto e = std::move(*this).asKnownExpr();
if (e->getType()->is<InOutType>()) {
return SGF.emitAddressOfLValue(e, SGF.emitLValue(e, AccessKind::ReadWrite),
AccessKind::ReadWrite);
} else {
return SGF.emitRValueAsSingleValue(e, C);
}
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &SGF,
AbstractionPattern origFormalType,
SGFContext C) && {
auto loc = getLocation();
auto substFormalType = getSubstType();
ManagedValue outputValue = std::move(*this).getAsSingleValue(SGF);
return SGF.emitSubstToOrigValue(loc,
outputValue, origFormalType,
substFormalType, C);
}
void ArgumentSource::forwardInto(SILGenFunction &SGF, Initialization *dest) && {
assert(!isLValue());
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().forwardInto(SGF, loc, dest);
}
auto e = std::move(*this).asKnownExpr();
return SGF.emitExprInto(e, dest);
}
ManagedValue ArgumentSource::materialize(SILGenFunction &SGF) && {
assert(!isLValue());
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue().materialize(SGF, loc);
}
auto expr = std::move(*this).asKnownExpr();
auto temp = SGF.emitTemporary(expr, SGF.getTypeLowering(expr->getType()));
SGF.emitExprInto(expr, temp.get());
return temp->getManagedAddress();
}
ManagedValue ArgumentSource::materialize(SILGenFunction &SGF,
AbstractionPattern origFormalType,
SILType destType) && {
auto substFormalType = CanType(getSubstType()->getInOutObjectType());
assert(!destType || destType.getObjectType() ==
SGF.SGM.Types.getLoweredType(origFormalType,
substFormalType).getObjectType());
// Fast path: if the types match exactly, no abstraction difference
// is possible and we can just materialize as normal.
if (origFormalType.isExactType(substFormalType))
return std::move(*this).materialize(SGF);
auto &destTL =
(destType ? SGF.getTypeLowering(destType)
: SGF.getTypeLowering(origFormalType, substFormalType));
if (!destType) destType = destTL.getLoweredType();
// If there's no abstraction difference, we can just materialize as normal.
if (destTL.getLoweredType() == SGF.getLoweredType(substFormalType)) {
return std::move(*this).materialize(SGF);
}
// Emit a temporary at the given address.
auto temp = SGF.emitTemporary(getLocation(), destTL);
// Forward into it.
std::move(*this).forwardInto(SGF, origFormalType, temp.get(), destTL);
return temp->getManagedAddress();
}
void ArgumentSource::forwardInto(SILGenFunction &SGF,
AbstractionPattern origFormalType,
Initialization *dest,
const TypeLowering &destTL) && {
auto substFormalType = getSubstType();
assert(destTL.getLoweredType() ==
SGF.getLoweredType(origFormalType, substFormalType));
// If there are no abstraction changes, we can just forward
// normally.
if (origFormalType.isExactType(substFormalType) ||
destTL.getLoweredType() == SGF.getLoweredType(substFormalType)) {
std::move(*this).forwardInto(SGF, dest);
return;
}
// Otherwise, emit as a single independent value.
SILLocation loc = getLocation();
ManagedValue outputValue =
std::move(*this).getAsSingleValue(SGF, origFormalType,
SGFContext(dest));
if (outputValue.isInContext()) return;
// Use RValue's forward-into-initialization code. We have to lie to
// RValue about the formal type (by using the lowered type) because
// we're emitting into an abstracted value, which RValue doesn't
// really handle.
auto substLoweredType = destTL.getLoweredType().getSwiftRValueType();
RValue(SGF, loc, substLoweredType, outputValue).forwardInto(SGF, loc, dest);
}