Skip to content

[NFC] Excise simpler uses of getInput() #17127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions include/swift/AST/TypeMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ class TypeMatcher {

auto sugaredFirstFunc = sugaredFirstType->castTo<AnyFunctionType>();
if (firstFunc->getParams().size() != secondFunc->getParams().size())
return mismatch(firstFunc.getInput().getPointer(), secondFunc->getInput(),
sugaredFirstFunc->getInput());
return mismatch(firstFunc.getPointer(), secondFunc, sugaredFirstFunc);

for (unsigned i = 0, n = firstFunc->getParams().size(); i != n; ++i) {
const auto &firstElt = firstFunc->getParams()[i];
Expand All @@ -218,9 +217,9 @@ class TypeMatcher {
if (firstElt.getLabel() != secondElt.getLabel() ||
firstElt.isVariadic() != secondElt.isVariadic() ||
firstElt.isInOut() != secondElt.isInOut())
return mismatch(firstFunc.getInput().getPointer(),
secondFunc->getInput(),
sugaredFirstFunc->getInput());
return mismatch(firstElt.getType().getPointer(),
secondElt.getType(),
sugaredFirstFunc->getParams()[i].getType());

// Recurse on parameter components.
if (!this->visit(firstElt.getType()->getCanonicalType(),
Expand Down
49 changes: 33 additions & 16 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,18 @@ Type TypeBase::addCurriedSelfType(const DeclContext *dc) {
GenericSignature *sig = dc->getGenericSignatureOfContext();
if (auto *genericFn = type->getAs<GenericFunctionType>()) {
sig = genericFn->getGenericSignature();
type = FunctionType::get(genericFn->getInput(),
type = FunctionType::get(genericFn->getParams(),
genericFn->getResult(),
genericFn->getExtInfo());
}

auto selfTy = dc->getDeclaredInterfaceType();
auto selfParam = AnyFunctionType::Param(selfTy,
Identifier(), ParameterTypeFlags());
if (sig)
return GenericFunctionType::get(sig, selfTy, type,
return GenericFunctionType::get(sig, {selfParam}, type,
AnyFunctionType::ExtInfo());
return FunctionType::get(selfTy, type);
return FunctionType::get({selfParam}, type, AnyFunctionType::ExtInfo());
}

void
Expand Down Expand Up @@ -1591,9 +1593,11 @@ bool TypeBase::isBindableTo(Type b) {
if (func->getExtInfo() != substFunc->getExtInfo())
return false;

if (!visit(func->getInput()->getCanonicalType(),
substFunc->getInput()->getCanonicalType()))
return false;
for (unsigned i : indices(func->getParams())) {
if (!visit(func->getParams()[i].getType(),
substFunc.getParams()[i].getType()))
return false;
}

return visit(func->getResult()->getCanonicalType(),
substFunc->getResult()->getCanonicalType());
Expand Down Expand Up @@ -2323,10 +2327,23 @@ static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
return false;

auto paramsAndResultMatch = [&]() {
// Inputs are contravariant, results are covariant.
return (matches(fn2.getInput(), fn1.getInput(), matchMode,
ParameterPosition::Parameter, OptionalUnwrapping::None) &&
matches(fn1.getResult(), fn2.getResult(), matchMode,
auto fn2Params = fn2.getParams();
auto fn1Params = fn1.getParams();
if (fn2Params.size() != fn1Params.size()) {
return false;
}

// Inputs are contravariant.
for (auto i : indices(fn2.getParams())) {
if (!matches(fn2Params[i].getType(), fn1Params[i].getType(),
matchMode, ParameterPosition::ParameterTupleElement,
OptionalUnwrapping::None)) {
return false;
}
}

// Results are covariant.
return (matches(fn1.getResult(), fn2.getResult(), matchMode,
ParameterPosition::NotParameter,
OptionalUnwrapping::None));
};
Expand Down Expand Up @@ -2771,9 +2788,9 @@ bool AnyFunctionType::isCanonicalFunctionInputType(Type input) {

FunctionType *
GenericFunctionType::substGenericArgs(SubstitutionMap subs) {
Type input = getInput().subst(subs);
Type result = getResult().subst(subs);
return FunctionType::get(input, result, getExtInfo());
auto substFn = Type(this).subst(subs)->castTo<AnyFunctionType>();
return FunctionType::get(substFn->getParams(),
substFn->getResult(), getExtInfo());
}

static Type getMemberForBaseType(LookupConformanceFn lookupConformances,
Expand Down Expand Up @@ -2931,7 +2948,7 @@ static Type substGenericFunctionType(GenericFunctionType *genericFnType,
LookupConformanceFn lookupConformances,
SubstOptions options) {
// Substitute into the function type (without generic signature).
auto *bareFnType = FunctionType::get(genericFnType->getInput(),
auto *bareFnType = FunctionType::get(genericFnType->getParams(),
genericFnType->getResult(),
genericFnType->getExtInfo());
Type result =
Expand Down Expand Up @@ -3013,7 +3030,7 @@ static Type substGenericFunctionType(GenericFunctionType *genericFnType,
}

// Produce the new generic function type.
return GenericFunctionType::get(genericSig, fnType->getInput(),
return GenericFunctionType::get(genericSig, fnType->getParams(),
fnType->getResult(), fnType->getExtInfo());
}

Expand Down Expand Up @@ -3371,7 +3388,7 @@ Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
baseDecl, derivedDecl, /*derivedSubs=*/None);

if (auto *genericMemberType = memberType->getAs<GenericFunctionType>()) {
memberType = FunctionType::get(genericMemberType->getInput(),
memberType = FunctionType::get(genericMemberType->getParams(),
genericMemberType->getResult(),
genericMemberType->getExtInfo());
}
Expand Down
25 changes: 16 additions & 9 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4121,15 +4121,16 @@ namespace {

// Update the method type with the new result type.
auto methodTy = type->castTo<FunctionType>();
type = FunctionType::get(methodTy->getInput(), resultTy,
type = FunctionType::get(methodTy->getParams(), resultTy,
methodTy->getExtInfo());
}

// Add the 'self' parameter to the function type. NB. a method's formal
// type should be (Type) -> (Args...) -> Ret, not Type -> (Args...) ->
// Ret.
auto parenSelfType = ParenType::get(Impl.SwiftContext, selfInterfaceType);
type = FunctionType::get(parenSelfType, type);
auto selfParam = AnyFunctionType::Param(selfInterfaceType,
Identifier(), ParameterTypeFlags());
type = FunctionType::get({selfParam}, type, AnyFunctionType::ExtInfo());

auto interfaceType = getGenericMethodType(dc, type->castTo<AnyFunctionType>());
result->setInterfaceType(interfaceType);
Expand Down Expand Up @@ -6160,8 +6161,6 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
// Add the implicit 'self' parameter patterns.
SmallVector<ParameterList *, 4> bodyParams;
auto selfMetaVar = ParamDecl::createSelf(SourceLoc(), dc, /*static*/ true);
auto selfTy = dc->getSelfInterfaceType();
auto selfMetaTy = MetatypeType::get(selfTy);
bodyParams.push_back(ParameterList::createWithoutLoc(selfMetaVar));

// Import the type that this method will have.
Expand Down Expand Up @@ -6189,16 +6188,24 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
}

// Rebuild the function type with the appropriate result type;
Type resultTy = selfTy;
Type resultTy = dc->getSelfInterfaceType();
if (resultIsOptional)
resultTy = OptionalType::get(resultTy);

type = FunctionType::get(oldFnType->getInput(), resultTy,
type = FunctionType::get(oldFnType->getParams(), resultTy,
oldFnType->getExtInfo());

// Add the 'self' parameter to the function types.
Type allocType = FunctionType::get(selfMetaTy, type);
Type initType = FunctionType::get(selfTy, type);
auto selfTy = dc->getSelfInterfaceType();
auto selfParam = AnyFunctionType::Param(selfTy,
Identifier(), ParameterTypeFlags());
auto selfMetaTy = MetatypeType::get(selfTy);
auto selfMetaParam = AnyFunctionType::Param(selfMetaTy, Identifier(),
ParameterTypeFlags());
Type allocType = FunctionType::get({selfMetaParam}, type,
AnyFunctionType::ExtInfo());
Type initType = FunctionType::get({selfParam}, type,
AnyFunctionType::ExtInfo());

// Look for other imported constructors that occur in this context with
// the same name.
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace {
if (pointeeQualType->isFunctionType()) {
auto funcTy = pointeeType->castTo<FunctionType>();
return {
FunctionType::get(funcTy->getInput(), funcTy->getResult(),
FunctionType::get(funcTy->getParams(), funcTy->getResult(),
funcTy->getExtInfo().withRepresentation(
AnyFunctionType::Representation::CFunctionPointer)),
ImportHint::CFunctionPointer
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ static bool hasTrivialTrailingClosure(const FuncDecl *FD,
auto param = funcType->getParams().back();
if (!param.isAutoClosure()) {
if (auto Fn = param.getType()->getAs<AnyFunctionType>()) {
return Fn->getInput()->isVoid() && Fn->getResult()->isVoid();
return Fn->getParams().empty() && Fn->getResult()->isVoid();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,8 @@ void NecessaryBindings::addTypeMetadata(CanType type) {
return;
}
if (auto fn = dyn_cast<FunctionType>(type)) {
addTypeMetadata(fn.getInput());
for (const auto &elt : fn.getParams())
addTypeMetadata(elt.getType());
addTypeMetadata(fn.getResult());
return;
}
Expand Down
12 changes: 7 additions & 5 deletions lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ SILFunction *SILGenModule::emitProtocolWitness(
auto *genericEnv = witness.getSyntheticEnvironment();

// The type of the witness thunk.
auto input = reqtOrigTy->getInput().subst(reqtSubMap)->getCanonicalType();
auto result = reqtOrigTy->getResult().subst(reqtSubMap)->getCanonicalType();
auto substReqtTy =
cast<AnyFunctionType>(reqtOrigTy.subst(reqtSubMap)->getCanonicalType());

// If there's something to map to for the witness thunk, the conformance
// should be phrased in the same terms. This particularly applies to classes
Expand All @@ -632,10 +632,12 @@ SILFunction *SILGenModule::emitProtocolWitness(
auto *genericSig = genericEnv->getGenericSignature();
reqtSubstTy = CanGenericFunctionType::get(
genericSig->getCanonicalSignature(),
input, result, reqtOrigTy->getExtInfo());
substReqtTy->getParams(), substReqtTy.getResult(),
reqtOrigTy->getExtInfo());
} else {
reqtSubstTy = CanFunctionType::get(
input, result, reqtOrigTy->getExtInfo());
reqtSubstTy = CanFunctionType::get(substReqtTy->getParams(),
substReqtTy.getResult(),
reqtOrigTy->getExtInfo());
}

// FIXME: this needs to pull out the conformances/witness-tables for any
Expand Down