Skip to content

Commit a69a806

Browse files
authored
Merge pull request swiftlang#18693 from slavapestov/minor-function-type-cleanups
Minor function type cleanups
2 parents 3ba8325 + c0e3327 commit a69a806

21 files changed

+78
-93
lines changed

include/swift/AST/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,8 @@ class alignas(1 << TypeAlignInBits) TypeBase {
947947
/// Otherwise, returns the type itself.
948948
Type getWithoutSpecifierType();
949949

950-
/// getRValueInstanceType - Looks through inout types and metatypes.
951-
Type getRValueInstanceType();
950+
/// getMetatypeInstanceType - Looks through metatypes.
951+
Type getMetatypeInstanceType();
952952

953953
/// For a ReferenceStorageType like @unowned, this returns the referent.
954954
/// Otherwise, it returns the type itself.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ ASTContext::getBehaviorConformance(Type conformingType,
17331733
auto conformance = new (*this, AllocationArena::Permanent)
17341734
NormalProtocolConformance(conformingType, protocol, loc, storage, state);
17351735

1736-
if (auto nominal = conformingType->getRValueInstanceType()->getAnyNominal()) {
1736+
if (auto nominal = conformingType->getAnyNominal()) {
17371737
// Note: this is an egregious hack. The conformances need to be associated
17381738
// with the actual storage declarations.
17391739
SmallVector<ProtocolConformance *, 2> conformances;

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ void ASTMangler::appendFunctionInputType(
17111711

17121712
case 1: {
17131713
const auto &param = params.front();
1714-
auto type = param.getType();
1714+
auto type = param.getPlainType();
17151715

17161716
// If this is just a single parenthesized type,
17171717
// to save space in the mangled name, let's encode
@@ -1730,7 +1730,7 @@ void ASTMangler::appendFunctionInputType(
17301730
default:
17311731
bool isFirstParam = true;
17321732
for (auto &param : params) {
1733-
appendTypeListElement(Identifier(), param.getType(),
1733+
appendTypeListElement(Identifier(), param.getPlainType(),
17341734
param.getParameterFlags());
17351735
appendListSeparator(isFirstParam);
17361736
}
@@ -1749,8 +1749,11 @@ void ASTMangler::appendTypeList(Type listTy) {
17491749
return appendOperator("y");
17501750
bool firstField = true;
17511751
for (auto &field : tuple->getElements()) {
1752-
appendTypeListElement(field.getName(), field.getType(),
1753-
field.getParameterFlags());
1752+
// FIXME: We shouldn't put @escaping in non-parameter list tuples
1753+
auto flags = field.getParameterFlags().withEscaping(false);
1754+
1755+
assert(flags.isNone());
1756+
appendTypeListElement(field.getName(), field.getRawType(), flags);
17541757
appendListSeparator(firstField);
17551758
}
17561759
} else {
@@ -1761,7 +1764,7 @@ void ASTMangler::appendTypeList(Type listTy) {
17611764

17621765
void ASTMangler::appendTypeListElement(Identifier name, Type elementType,
17631766
ParameterTypeFlags flags) {
1764-
appendType(elementType->getInOutObjectType());
1767+
appendType(elementType);
17651768
switch (flags.getValueOwnership()) {
17661769
case ValueOwnership::Default:
17671770
/* nothing */

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
23032303
// through the paren types so that we don't print excessive @escapings.
23042304
unsigned numParens = 0;
23052305
if (!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
2306-
auto type = TheTypeLoc.getType()->getInOutObjectType();
2306+
auto type = TheTypeLoc.getType();
23072307

23082308
printParameterFlags(Printer, Options, paramFlags);
23092309
while (auto parenTy = dyn_cast<ParenType>(type.getPointer())) {
@@ -3302,7 +3302,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
33023302
if (i)
33033303
Printer << ", ";
33043304
const TupleTypeElt &TD = Fields[i];
3305-
Type EltType = TD.getType()->getInOutObjectType();
3305+
Type EltType = TD.getRawType();
33063306

33073307
Printer.callPrintStructurePre(PrintStructureKind::TupleElement);
33083308
SWIFT_DEFER {

lib/AST/Builtins.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
184184
/// Build a builtin function declaration.
185185
static FuncDecl *
186186
getBuiltinGenericFunction(Identifier Id,
187-
ArrayRef<TupleTypeElt> ArgParamTypes,
187+
ArrayRef<AnyFunctionType::Param> ArgParamTypes,
188188
Type ResType,
189189
GenericParamList *GenericParams,
190190
GenericEnvironment *Env) {
@@ -196,7 +196,7 @@ getBuiltinGenericFunction(Identifier Id,
196196

197197
SmallVector<ParamDecl*, 4> params;
198198
for (unsigned i = 0, e = ArgParamTypes.size(); i < e; i++) {
199-
auto paramIfaceType = ArgParamTypes[i].getRawType();
199+
auto paramIfaceType = ArgParamTypes[i].getPlainType();
200200
auto specifier = (ArgParamTypes[i].getParameterFlags().isInOut())
201201
? VarDecl::Specifier::InOut
202202
: VarDecl::Specifier::Default;
@@ -211,7 +211,7 @@ getBuiltinGenericFunction(Identifier Id,
211211
}
212212

213213
auto *paramList = ParameterList::create(Context, params);
214-
214+
215215
DeclName Name(Context, Id, paramList);
216216
auto func = FuncDecl::create(Context, /*StaticLoc=*/SourceLoc(),
217217
StaticSpellingKind::None,
@@ -222,7 +222,7 @@ getBuiltinGenericFunction(Identifier Id,
222222
/*SelfDecl=*/nullptr,
223223
paramList,
224224
TypeLoc::withoutLoc(ResType), DC);
225-
225+
226226
func->setGenericEnvironment(Env);
227227
func->computeType();
228228
func->setValidationToChecked();
@@ -456,7 +456,7 @@ namespace {
456456
GenericParamList *TheGenericParamList;
457457
SmallVector<GenericTypeParamDecl*, 2> GenericTypeParams;
458458
GenericEnvironment *GenericEnv = nullptr;
459-
SmallVector<TupleTypeElt, 4> InterfaceParams;
459+
SmallVector<AnyFunctionType::Param, 4> InterfaceParams;
460460
Type InterfaceResult;
461461

462462
public:
@@ -478,16 +478,15 @@ namespace {
478478
template <class G>
479479
void addParameter(const G &generator) {
480480
Type gTyIface = generator.build(*this);
481-
InterfaceParams.push_back({gTyIface->getInOutObjectType(),
482-
Identifier(), ParameterTypeFlags()});
481+
auto flags = ParameterTypeFlags();
482+
InterfaceParams.emplace_back(gTyIface, Identifier(), flags);
483483
}
484484

485485
template <class G>
486486
void addInOutParameter(const G &generator) {
487487
Type gTyIface = generator.build(*this);
488-
auto iFaceflags = ParameterTypeFlags().withInOut(true);
489-
InterfaceParams.push_back(TupleTypeElt(gTyIface->getInOutObjectType(),
490-
Identifier(), iFaceflags));
488+
auto flags = ParameterTypeFlags().withInOut(true);
489+
InterfaceParams.emplace_back(gTyIface, Identifier(), flags);
491490
}
492491

493492
template <class G>
@@ -523,17 +522,6 @@ namespace {
523522
return TheFunction(builder);
524523
}
525524
};
526-
template <class T, class U>
527-
struct FunctionGenerator {
528-
T Arg;
529-
U Result;
530-
FunctionType::ExtInfo ExtInfo;
531-
Type build(BuiltinGenericSignatureBuilder &builder) const {
532-
return FunctionType::get(Arg.build(builder),
533-
Result.build(builder),
534-
ExtInfo);
535-
}
536-
};
537525
template <class T>
538526
struct MetatypeGenerator {
539527
T Object;
@@ -568,13 +556,6 @@ makeTuple(const Gs & ...elementGenerators) {
568556
};
569557
}
570558

571-
template <class T, class U>
572-
static BuiltinGenericSignatureBuilder::FunctionGenerator<T,U>
573-
makeFunction(const T &arg, const U &result,
574-
FunctionType::ExtInfo extInfo = FunctionType::ExtInfo()) {
575-
return { arg, result, extInfo };
576-
}
577-
578559
template <class T>
579560
static BuiltinGenericSignatureBuilder::MetatypeGenerator<T>
580561
makeMetatype(const T &object, Optional<MetatypeRepresentation> repr = None) {

lib/AST/Decl.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,12 +1982,7 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
19821982
if (curryLevels == 0) {
19831983
// In an initializer, ignore optionality.
19841984
if (isInitializer) {
1985-
if (auto inOutTy = type->getAs<InOutType>()) {
1986-
if (auto objectType =
1987-
inOutTy->getObjectType()->getOptionalObjectType()) {
1988-
type = InOutType::get(objectType);
1989-
}
1990-
} else if (auto objectType = type->getOptionalObjectType()) {
1985+
if (auto objectType = type->getOptionalObjectType()) {
19911986
type = objectType;
19921987
}
19931988
}
@@ -1998,16 +1993,15 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
19981993
auto funcTy = type->castTo<AnyFunctionType>();
19991994
SmallVector<AnyFunctionType::Param, 4> newParams;
20001995
for (const auto &param : funcTy->getParams()) {
2001-
auto newParamType = mapSignatureParamType(ctx, param.getType());
1996+
auto newParamType = mapSignatureParamType(ctx, param.getPlainType());
20021997
ParameterTypeFlags newFlags = param.getParameterFlags().withEscaping(false);
20031998

20041999
// For the 'self' of a method, strip off 'inout'.
20052000
if (isMethod) {
20062001
newFlags = newFlags.withInOut(false);
20072002
}
20082003

2009-
AnyFunctionType::Param newParam(newParamType->getInOutObjectType(),
2010-
param.getLabel(), newFlags);
2004+
AnyFunctionType::Param newParam(newParamType, param.getLabel(), newFlags);
20112005
newParams.push_back(newParam);
20122006
}
20132007

@@ -4721,7 +4715,7 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
47214715
typeLoc.setType(Type());
47224716

47234717
if (withTypes && PD->hasInterfaceType())
4724-
setInterfaceType(PD->getInterfaceType()->getInOutObjectType());
4718+
setInterfaceType(PD->getInterfaceType());
47254719

47264720
// FIXME: We should clone the entire attribute list.
47274721
if (PD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>())

lib/AST/Type.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -893,41 +893,46 @@ std::string swift::getParamListAsString(ArrayRef<AnyFunctionType::Param> params)
893893

894894
/// Rebuilds the given 'self' type using the given object type as the
895895
/// replacement for the object type of self.
896-
static Type rebuildSelfTypeWithObjectType(Type selfTy, Type objectTy) {
897-
auto existingObjectTy = selfTy->getRValueInstanceType();
898-
return selfTy.transform([=](Type type) -> Type {
899-
if (type->isEqual(existingObjectTy))
900-
return objectTy;
901-
return type;
902-
});
896+
static AnyFunctionType::Param
897+
rebuildSelfTypeWithObjectType(AnyFunctionType::Param selfParam,
898+
Type objectTy) {
899+
if (selfParam.getPlainType()->getAs<MetatypeType>())
900+
objectTy = MetatypeType::get(objectTy);
901+
902+
return AnyFunctionType::Param(objectTy,
903+
selfParam.getLabel(),
904+
selfParam.getParameterFlags());
903905
}
904906

905907
/// Returns a new function type exactly like this one but with the self
906-
/// parameter replaced. Only makes sense for members of types.
908+
/// parameter replaced. Only makes sense for members of classes.
907909
Type TypeBase::replaceSelfParameterType(Type newSelf) {
908910
auto fnTy = castTo<AnyFunctionType>();
909-
Type input = rebuildSelfTypeWithObjectType(fnTy->getInput(), newSelf);
911+
912+
auto params = fnTy->getParams();
913+
assert(params.size() == 1);
914+
auto selfParam = rebuildSelfTypeWithObjectType(params[0], newSelf);
910915

911916
if (auto genericFnTy = getAs<GenericFunctionType>()) {
912917
return GenericFunctionType::get(genericFnTy->getGenericSignature(),
913-
input,
918+
{selfParam},
914919
fnTy->getResult(),
915920
fnTy->getExtInfo());
916921
}
917922

918-
return FunctionType::get(input,
923+
return FunctionType::get({selfParam},
919924
fnTy->getResult(),
920925
fnTy->getExtInfo());
921926
}
922927

923-
/// Retrieve the object type for a 'self' parameter, digging into
924-
/// inout types, and metatypes.
925-
Type TypeBase::getRValueInstanceType() {
928+
/// Look through a metatype, or just return the original type if it is
929+
/// not a metatype.
930+
Type TypeBase::getMetatypeInstanceType() {
926931
if (auto metaTy = getAs<AnyMetatypeType>())
927932
return metaTy->getInstanceType();
928933

929934
// For mutable value type methods, we need to dig through inout types.
930-
return getInOutObjectType();
935+
return this;
931936
}
932937

933938
/// \brief Collect the protocols in the existential type T into the given
@@ -1181,7 +1186,8 @@ CanType TypeBase::computeCanonicalType() {
11811186
break;
11821187
}
11831188
case TypeKind::LValue:
1184-
Result = LValueType::get(getRValueType()->getCanonicalType());
1189+
Result = LValueType::get(cast<LValueType>(this)->getObjectType()
1190+
->getCanonicalType());
11851191
break;
11861192
case TypeKind::InOut:
11871193
Result = InOutType::get(getInOutObjectType()->getCanonicalType());

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20792079
BaseTy = CurrDeclContext->getInnermostTypeContext()
20802080
->getDeclaredTypeInContext();
20812081
if (BaseTy) {
2082-
BaseTy = BaseTy->getRValueInstanceType();
2082+
BaseTy = BaseTy->getInOutObjectType()->getMetatypeInstanceType();
20832083
if (auto NTD = BaseTy->getAnyNominal()) {
20842084
auto *Module = NTD->getParentModule();
20852085
auto Conformance = Module->lookupConformance(
@@ -3190,7 +3190,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
31903190
DeclContext *DC = nullptr;
31913191
if (VD)
31923192
DC = VD->getInnermostDeclContext();
3193-
else if (auto NTD = ExprType->getRValueInstanceType()->getAnyNominal())
3193+
else if (auto NTD = ExprType->getInOutObjectType()
3194+
->getMetatypeInstanceType()->getAnyNominal())
31943195
DC = NTD;
31953196
if (DC)
31963197
ExprType = DC->mapTypeIntoContext(ExprType);

lib/SIL/SILFunctionType.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,9 @@ TypeConverter::getConstantOverrideInfo(SILDeclRef derived, SILDeclRef base) {
23002300
auto baseInterfaceTy = baseInfo.FormalType;
23012301
auto derivedInterfaceTy = derivedInfo.FormalType;
23022302

2303-
auto selfInterfaceTy = derivedInterfaceTy.getInput()->getRValueInstanceType();
2303+
auto params = derivedInterfaceTy.getParams();
2304+
assert(params.size() == 1);
2305+
auto selfInterfaceTy = params[0].getPlainType()->getMetatypeInstanceType();
23042306

23052307
auto overrideInterfaceTy =
23062308
selfInterfaceTy->adjustSuperclassMemberDeclType(
@@ -2426,8 +2428,8 @@ class SILTypeSubstituter :
24262428
// The Self type can be nested in a few layers of metatypes (etc.), e.g.
24272429
// for a mutable static variable the materializeForSet currently has its
24282430
// last argument as a Self.Type.Type metatype.
2429-
while (1) {
2430-
auto next = selfType->getRValueInstanceType()->getCanonicalType();
2431+
while (auto metatypeType = dyn_cast<MetatypeType>(selfType)) {
2432+
auto next = metatypeType->getInstanceType()->getCanonicalType();
24312433
if (next == selfType)
24322434
break;
24332435
selfType = next;

lib/SILGen/SILGenMaterializeForSet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ struct MaterializeForSetEmitter {
446446

447447
// Metatypes and bases of non-mutating setters on value types
448448
// are always rvalues.
449-
if (!SubstSelfType->getRValueInstanceType()->mayHaveSuperclass()) {
449+
if (!SubstSelfType->getMetatypeInstanceType()->mayHaveSuperclass()) {
450450
return LValue::forValue(self, SubstSelfType);
451451
}
452452

0 commit comments

Comments
 (0)