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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
//===--- 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 "Conversion.h"
#include "Initialization.h"
using namespace swift;
using namespace Lowering;
RValue &ArgumentSource::peekRValue() & {
assert(isRValue() && "Undefined behavior to call this method without the "
"ArgumentSource actually being an RValue");
return Storage.get<RValueStorage>(StoredKind).Value;
}
RValue ArgumentSource::getAsRValue(SILGenFunction &SGF, SGFContext C) && {
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::LValue:
llvm_unreachable("cannot get l-value as r-value");
case Kind::RValue:
return std::move(*this).asKnownRValue(SGF);
case Kind::Expr:
return SGF.emitRValue(std::move(*this).asKnownExpr(), C);
}
llvm_unreachable("bad kind");
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &SGF,
SGFContext C) && {
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::LValue: {
auto loc = getKnownLValueLocation();
LValue &&lv = std::move(*this).asKnownLValue();
return SGF.emitAddressOfLValue(loc, std::move(lv));
}
case Kind::RValue: {
auto loc = getKnownRValueLocation();
if (auto init = C.getEmitInto()) {
std::move(*this).asKnownRValue(SGF)
.ensurePlusOne(SGF, loc)
.forwardInto(SGF, loc, init);
return ManagedValue::forInContext();
} else {
return std::move(*this).asKnownRValue(SGF).getAsSingleValue(SGF, loc);
}
}
case Kind::Expr: {
auto e = std::move(*this).asKnownExpr();
if (e->isSemanticallyInOutExpr()) {
auto lv = SGF.emitLValue(e, SGFAccessKind::ReadWrite);
return SGF.emitAddressOfLValue(e, std::move(lv));
} else {
return SGF.emitRValueAsSingleValue(e, C);
}
}
}
llvm_unreachable("bad kind");
}
ManagedValue ArgumentSource::getAsSingleValue(SILGenFunction &SGF,
AbstractionPattern origFormalType,
SILType loweredTy,
SGFContext C) && {
auto substFormalType = getSubstRValueType();
auto loweredFormalTy = SGF.getLoweredType(substFormalType);
auto conversion =
Conversion::getSubstToOrig(origFormalType, substFormalType,
loweredFormalTy, loweredTy);
return std::move(*this).getConverted(SGF, conversion, C);
}
ManagedValue ArgumentSource::getConverted(SILGenFunction &SGF,
const Conversion &conversion,
SGFContext C) && {
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::LValue:
llvm_unreachable("cannot get converted l-value");
case Kind::RValue:
case Kind::Expr:
return SGF.emitConvertedRValue(getLocation(), conversion, C,
[&](SILGenFunction &SGF, SILLocation loc, SGFContext C) {
return std::move(*this).getAsSingleValue(SGF, C);
});
}
llvm_unreachable("bad kind");
}
void ArgumentSource::forwardInto(SILGenFunction &SGF, Initialization *dest) && {
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::LValue:
llvm_unreachable("cannot forward an l-value");
case Kind::RValue: {
auto loc = getKnownRValueLocation();
std::move(*this).asKnownRValue(SGF).ensurePlusOne(SGF, loc).forwardInto(SGF, loc, dest);
return;
}
case Kind::Expr: {
auto e = std::move(*this).asKnownExpr();
SGF.emitExprInto(e, dest);
return;
}
}
llvm_unreachable("bad kind");
}
// FIXME: Once uncurrying is removed, get rid of this constructor.
ArgumentSource::ArgumentSource(SILLocation loc, RValue &&rv, Kind kind)
: Storage(), StoredKind(kind) {
Storage.emplaceAggregate<RValueStorage>(StoredKind, std::move(rv), loc);
}
ArgumentSource ArgumentSource::borrow(SILGenFunction &SGF) const & {
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::LValue:
llvm_unreachable("cannot borrow an l-value");
case Kind::RValue: {
auto loc = getKnownRValueLocation();
return ArgumentSource(loc, asKnownRValue().borrow(SGF, loc));
}
case Kind::Expr: {
llvm_unreachable("cannot borrow an expression");
}
}
llvm_unreachable("bad kind");
}
ManagedValue ArgumentSource::materialize(SILGenFunction &SGF) && {
if (isRValue()) {
auto loc = getKnownRValueLocation();
return std::move(*this).asKnownRValue(SGF).materialize(SGF, loc);
}
auto loc = getLocation();
auto temp = SGF.emitTemporary(loc, SGF.getTypeLowering(getSubstRValueType()));
std::move(*this).forwardInto(SGF, temp.get());
return temp->getManagedAddress();
}
ManagedValue ArgumentSource::materialize(SILGenFunction &SGF,
AbstractionPattern origFormalType,
SILType destType) && {
auto substFormalType = getSubstRValueType();
assert(!destType || destType.getObjectType() ==
SGF.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 = getSubstRValueType();
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,
destTL.getLoweredType(),
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().getASTType();
RValue(SGF, loc, substLoweredType, outputValue).forwardInto(SGF, loc, dest);
}
void ArgumentSource::dump() const {
dump(llvm::errs());
}
void ArgumentSource::dump(raw_ostream &out, unsigned indent) const {
out.indent(indent) << "ArgumentSource::";
switch (StoredKind) {
case Kind::Invalid:
out << "Invalid\n";
return;
case Kind::LValue:
out << "LValue\n";
Storage.get<LValueStorage>(StoredKind).Value.dump(out, indent + 2);
return;
case Kind::RValue:
out << "RValue\n";
Storage.get<RValueStorage>(StoredKind).Value.dump(out, indent + 2);
return;
case Kind::Expr:
out << "Expr\n";
Storage.get<Expr*>(StoredKind)->dump(out); // FIXME: indent
out << "\n";
return;
}
llvm_unreachable("bad kind");
}
PreparedArguments::PreparedArguments(ArrayRef<AnyFunctionType::Param> params,
ArgumentList *argList)
: PreparedArguments(params) {
for (auto arg : *argList)
addArbitrary(arg.getExpr());
}
PreparedArguments
PreparedArguments::copy(SILGenFunction &SGF, SILLocation loc) const {
if (isNull()) return PreparedArguments();
assert(isValid());
PreparedArguments result(getParams());
for (auto &elt : Arguments) {
assert(elt.isRValue());
result.add(elt.getKnownRValueLocation(),
elt.asKnownRValue().copy(SGF, loc));
}
assert(isValid());
return result;
}
bool PreparedArguments::isObviouslyEqual(const PreparedArguments &other) const {
if (isNull() != other.isNull())
return false;
if (isNull())
return true;
assert(isValid() && other.isValid());
if (Arguments.size() != other.Arguments.size())
return false;
for (auto i : indices(Arguments)) {
if (!Arguments[i].isObviouslyEqual(other.Arguments[i]))
return false;
}
return true;
}
bool ArgumentSource::isObviouslyEqual(const ArgumentSource &other) const {
if (StoredKind != other.StoredKind)
return false;
switch (StoredKind) {
case Kind::Invalid:
llvm_unreachable("argument source is invalid");
case Kind::RValue:
return asKnownRValue().isObviouslyEqual(other.asKnownRValue());
case Kind::LValue:
return false; // TODO?
case Kind::Expr:
return false; // TODO?
}
llvm_unreachable("bad kind");
}
PreparedArguments PreparedArguments::copyForDiagnostics() const {
if (isNull())
return PreparedArguments();
assert(isValid());
PreparedArguments result(getParams());
for (auto &arg : Arguments) {
result.Arguments.push_back(arg.copyForDiagnostics());
}
return result;
}
ArgumentSource ArgumentSource::copyForDiagnostics() const {
switch (StoredKind) {
case Kind::Invalid:
return ArgumentSource();
case Kind::LValue:
// We have no way to copy an l-value for diagnostics.
return {getKnownLValueLocation(), LValue()};
case Kind::RValue:
return {getKnownRValueLocation(), asKnownRValue().copyForDiagnostics()};
case Kind::Expr:
return asKnownExpr();
}
llvm_unreachable("bad kind");
}
ArgumentSourceExpansion::ArgumentSourceExpansion(SILGenFunction &SGF,
ArgumentSource &&arg,
bool vanishes) {
if (vanishes) {
StoredKind = Kind::Vanishing;
Storage.emplace<ArgumentSource *>(StoredKind, &arg);
#ifndef NDEBUG
NumRemainingElements = 1;
#endif
return;
}
#ifndef NDEBUG
NumRemainingElements =
cast<TupleType>(arg.getSubstRValueType())->getNumElements();
#endif
// If we have an expression, check whether it's something we can
// naturally split.
assert(!arg.isLValue());
Expr *expr = nullptr;
if (arg.isExpr()) {
expr = std::move(arg).asKnownExpr()->getSemanticsProvidingExpr();
// Currently, the only case of this is a tuple literal.
if (auto tupleExpr = dyn_cast<TupleExpr>(expr)) {
StoredKind = Kind::TupleExpr;
Storage.emplace<TupleExpr*>(StoredKind, tupleExpr);
return;
}
}
// Otherwise, get the arg as an r-value and extract the elements.
// The location will be overwritten in the cases below.
StoredKind = Kind::ElementRValues;
auto &rvalues = Storage.emplace<ElementRValuesStorage>(StoredKind,
SILLocation::invalid());
// This may require emitting the expression if we had a non-TupleExpr
// expression above.
if (expr) {
rvalues.Loc = expr;
auto rvalue = SGF.emitRValue(expr);
std::move(rvalue).extractElements(rvalues.Elements);
} else {
rvalues.Loc = arg.getKnownRValueLocation();
std::move(arg).asKnownRValue(SGF).extractElements(rvalues.Elements);
}
assert(rvalues.Elements.size() == NumRemainingElements);
}
void ArgumentSourceExpansion::withElement(unsigned i,
llvm::function_ref<void (ArgumentSource &&)> function) {
#ifndef NDEBUG
assert(NumRemainingElements > 0);
NumRemainingElements--;
#endif
switch (StoredKind) {
case Kind::ElementRValues: {
auto &storage = Storage.get<ElementRValuesStorage>(StoredKind);
auto &eltRV = storage.Elements[i];
assert(!eltRV.isNull());
function(ArgumentSource(storage.Loc, std::move(eltRV)));
#ifndef NDEBUG
eltRV = RValue();
#endif
return;
}
case Kind::TupleExpr: {
auto expr = Storage.get<TupleExpr*>(StoredKind);
function(ArgumentSource(expr->getElement(i)));
return;
}
case Kind::Vanishing: {
assert(NumRemainingElements == 0);
auto &source = *Storage.get<ArgumentSource *>(StoredKind);
function(std::move(source));
return;
}
}
llvm_unreachable("bad kind");
}
|