Files
swift-mirror/lib/SILGen/Varargs.h
Slava Pestov b9ef5708e2 Sema: Simplify representation of vararg forwarding
VarargExpansionExpr shows up in call argument lists in synthesized
initializers and modify accessors when we need to forward arguments
to a call taking varargs.

Previously we would say that the type of VarargExpansionExpr is
$T when its subexpression type is [$T]. matchCallArguments() would
then 'collect' the single VarargExpansionExpr into a variadic
argument list with a single element, and build an ArgumentShuffleExpr
for the argument list.

In turn, SILGen would peephole vararg emission of a variadic
argument list with a single entry that happens to be a
VarargExpansionExpr, by returning the subexpression's value,
which happened to be an array of the right element type,
instead of building a new array containing the elements of the
variadic argument list.

This was all too complicated. Instead, let's say that the type of
a VarargExpansionExpr is [$T], except that when it appears in a
TupleExpr, the variadic bit of the corresponding element is set.

Then, matchCallArguments() needs to support a case where both
the parameter and argument list have a matching vararg element.
In this case, instead of collecting multiple arguments into a
single variadic argument list, we treat the variadic argument like
an ordinary parameter, bypassing construction of the
ArgumentShuffleExpr altogether.

Finally, SILGen now needs to be able to emit a VarargExpansionExpr
in ordinary rvalue position, since it now appears as a child of a
TupleExpr; it can do this by simply emitting the sub-expression
to produce an array value.
2019-03-28 23:23:58 -04:00

77 lines
2.4 KiB
C++

//===--- Varargs.h - SIL generation for (native) Swift varargs --*- 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
//
//===----------------------------------------------------------------------===//
//
// A storage structure for holding a destructured rvalue with an optional
// cleanup(s).
// Ownership of the rvalue can be "forwarded" to disable the associated
// cleanup(s).
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_VARARGS_H
#define SWIFT_LOWERING_VARARGS_H
#include "ManagedValue.h"
#include "swift/SIL/AbstractionPattern.h"
namespace swift {
namespace Lowering {
class SILGenFunction;
class TypeLowering;
/// Information about a varargs emission.
class VarargsInfo {
ManagedValue Array;
CleanupHandle AbortCleanup;
SILValue BaseAddress;
AbstractionPattern BasePattern;
const TypeLowering &BaseTL;
public:
VarargsInfo(ManagedValue array, CleanupHandle abortCleanup,
SILValue baseAddress, const TypeLowering &baseTL,
AbstractionPattern basePattern)
: Array(array), AbortCleanup(abortCleanup),
BaseAddress(baseAddress), BasePattern(basePattern), BaseTL(baseTL) {}
/// Return the array value. emitEndVarargs() is really the only
/// function that should be accessing this directly.
ManagedValue getArray() const {
return Array;
}
CleanupHandle getAbortCleanup() const { return AbortCleanup; }
/// An address of the lowered type.
SILValue getBaseAddress() const { return BaseAddress; }
AbstractionPattern getBaseAbstractionPattern() const {
return BasePattern;
}
const TypeLowering &getBaseTypeLowering() const {
return BaseTL;
}
};
/// Begin a varargs emission sequence.
VarargsInfo emitBeginVarargs(SILGenFunction &SGF, SILLocation loc,
CanType baseTy, CanType arrayTy,
unsigned numElements);
/// Successfully end a varargs emission sequence.
ManagedValue emitEndVarargs(SILGenFunction &SGF, SILLocation loc,
VarargsInfo &&varargs);
} // end namespace Lowering
} // end namespace swift
#endif