Skip to content

Commit 6a790e8

Browse files
authored
Merge pull request #19267 from slavapestov/silgen-new-function-type
Begin migrating AbstractionPattern over to the new function type representation
2 parents aeee1a8 + a64521e commit 6a790e8

File tree

9 files changed

+231
-349
lines changed

9 files changed

+231
-349
lines changed

include/swift/SIL/AbstractionPattern.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,6 @@ class AbstractionPattern {
934934
llvm_unreachable("bad kind");
935935
}
936936

937-
AbstractionPattern transformType(llvm::function_ref<CanType(CanType)>) const;
938-
939937
/// Is the given tuple type a valid substitution of this abstraction
940938
/// pattern?
941939
bool matchesTuple(CanTupleType substType);
@@ -994,8 +992,6 @@ class AbstractionPattern {
994992
llvm_unreachable("bad kind");
995993
}
996994

997-
AbstractionPattern dropLastTupleElement() const;
998-
999995
class TupleElementRange;
1000996

1001997
/// Return a range over the tuple elements.

lib/SIL/AbstractionPattern.cpp

Lines changed: 87 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -499,145 +499,6 @@ AbstractionPattern::getObjCMethodFormalParamPattern(CanType inputType) const {
499499
method->parameters()[paramIndex]->getType().getTypePtr());
500500
}
501501

502-
AbstractionPattern AbstractionPattern::transformType(
503-
llvm::function_ref<CanType(CanType)> transform) const {
504-
switch (getKind()) {
505-
case Kind::Invalid:
506-
llvm_unreachable("querying invalid abstraction pattern!");
507-
case Kind::Tuple:
508-
return *this;
509-
case Kind::Opaque:
510-
return getOpaque();
511-
case Kind::PartialCurriedObjCMethodType:
512-
return getPartialCurriedObjCMethod(getGenericSignature(),
513-
transform(getType()), getObjCMethod(),
514-
getEncodedForeignErrorInfo());
515-
case Kind::CurriedObjCMethodType:
516-
return getCurriedObjCMethod(transform(getType()), getObjCMethod(),
517-
getEncodedForeignErrorInfo());
518-
case Kind::PartialCurriedCFunctionAsMethodType:
519-
return getPartialCurriedCFunctionAsMethod(getGenericSignature(),
520-
transform(getType()),
521-
getClangType(),
522-
getImportAsMemberStatus());
523-
case Kind::CurriedCFunctionAsMethodType:
524-
return getCurriedCFunctionAsMethod(transform(getType()), getClangType(),
525-
getImportAsMemberStatus());
526-
case Kind::CFunctionAsMethodType:
527-
return getCFunctionAsMethod(transform(getType()), getClangType(),
528-
getImportAsMemberStatus());
529-
case Kind::CFunctionAsMethodParamTupleType:
530-
return getCFunctionAsMethodParamTuple(getGenericSignature(),
531-
transform(getType()),
532-
getClangType(),
533-
getImportAsMemberStatus());
534-
case Kind::ObjCMethodType:
535-
return getObjCMethod(transform(getType()), getObjCMethod(),
536-
getEncodedForeignErrorInfo());
537-
case Kind::ClangType:
538-
return AbstractionPattern(getGenericSignature(),
539-
transform(getType()), getClangType());
540-
case Kind::Type:
541-
return AbstractionPattern(getGenericSignature(), transform(getType()));
542-
case Kind::Discard:
543-
return AbstractionPattern::getDiscard(getGenericSignature(),
544-
transform(getType()));
545-
case Kind::ObjCMethodParamTupleType:
546-
return getObjCMethodParamTuple(getGenericSignature(),
547-
transform(getType()), getObjCMethod(),
548-
getEncodedForeignErrorInfo());
549-
550-
// In both of the following cases, if the transform makes it no
551-
// longer a tuple type, we need to change kinds.
552-
case Kind::ClangFunctionParamTupleType: {
553-
auto newType = transform(getType());
554-
if (isa<TupleType>(newType)) {
555-
return getClangFunctionParamTuple(getGenericSignature(),
556-
newType, getClangType());
557-
} else {
558-
assert(getNumTupleElements() == 1);
559-
return AbstractionPattern(getGenericSignature(), newType,
560-
getClangFunctionParameterType(getClangType(), 0));
561-
}
562-
}
563-
case Kind::ObjCMethodFormalParamTupleType: {
564-
auto newType = transform(getType());
565-
if (isa<TupleType>(newType)) {
566-
return getObjCMethodFormalParamTuple(getGenericSignature(),
567-
newType, getObjCMethod(),
568-
getEncodedForeignErrorInfo());
569-
} else {
570-
assert(getNumTupleElements() == 1);
571-
return AbstractionPattern(getGenericSignature(), newType,
572-
getObjCMethod()->parameters()[0]->getType().getTypePtr());
573-
}
574-
}
575-
case Kind::CFunctionAsMethodFormalParamTupleType: {
576-
auto newType = transform(getType());
577-
if (isa<TupleType>(newType)) {
578-
return getCFunctionAsMethodFormalParamTuple(getGenericSignature(),
579-
newType, getClangType(),
580-
getImportAsMemberStatus());
581-
}
582-
}
583-
}
584-
llvm_unreachable("bad kind");
585-
}
586-
587-
static CanType dropLastElement(CanType type) {
588-
auto elts = cast<TupleType>(type)->getElements().drop_back();
589-
return TupleType::get(elts, type->getASTContext())->getCanonicalType();
590-
}
591-
592-
AbstractionPattern AbstractionPattern::dropLastTupleElement() const {
593-
switch (getKind()) {
594-
case Kind::Invalid:
595-
llvm_unreachable("querying invalid abstraction pattern!");
596-
case Kind::Tuple: {
597-
auto n = getNumTupleElements_Stored();
598-
return getTuple(llvm::makeArrayRef(OrigTupleElements, n - 1));
599-
}
600-
case Kind::Opaque:
601-
return getOpaque();
602-
case Kind::CurriedObjCMethodType:
603-
case Kind::PartialCurriedObjCMethodType:
604-
case Kind::CFunctionAsMethodType:
605-
case Kind::CurriedCFunctionAsMethodType:
606-
case Kind::PartialCurriedCFunctionAsMethodType:
607-
case Kind::ObjCMethodType:
608-
llvm_unreachable("not a tuple type");
609-
case Kind::ClangType:
610-
llvm_unreachable("dropping last element of imported array?");
611-
case Kind::ObjCMethodParamTupleType:
612-
case Kind::ObjCMethodFormalParamTupleType:
613-
case Kind::CFunctionAsMethodParamTupleType:
614-
case Kind::CFunctionAsMethodFormalParamTupleType:
615-
llvm_unreachable("operation is not needed on method abstraction patterns");
616-
case Kind::Type:
617-
if (isTypeParameter())
618-
return AbstractionPattern::getOpaque();
619-
return AbstractionPattern(getGenericSignature(),
620-
dropLastElement(getType()));
621-
case Kind::Discard:
622-
llvm_unreachable("don't need to drop element on discarded abstractions "
623-
"yet");
624-
// In both of the following cases, if the transform makes it no
625-
// longer a tuple type, we need to change kinds.
626-
case Kind::ClangFunctionParamTupleType: {
627-
auto newType = dropLastElement(getType());
628-
if (isa<TupleType>(newType)) {
629-
return getClangFunctionParamTuple(getGenericSignature(),
630-
newType, getClangType());
631-
} else {
632-
assert(getNumTupleElements() == 2);
633-
return AbstractionPattern(getGenericSignature(), newType,
634-
getClangFunctionParameterType(getClangType(), 0));
635-
}
636-
}
637-
}
638-
llvm_unreachable("bad kind");
639-
}
640-
641502
AbstractionPattern AbstractionPattern::getWithoutSpecifierType() const {
642503
switch (getKind()) {
643504
case Kind::Invalid:
@@ -793,27 +654,100 @@ AbstractionPattern AbstractionPattern::getFunctionInputType() const {
793654
AbstractionPattern
794655
AbstractionPattern::getFunctionParamType(unsigned index) const {
795656
switch (getKind()) {
657+
case Kind::Opaque:
658+
return *this;
796659
case Kind::Type: {
797660
if (isTypeParameter())
798661
return AbstractionPattern::getOpaque();
799-
auto fnType = cast<AnyFunctionType>(getType());
800-
auto param = fnType.getParams()[index];
801-
auto paramType = param.getType();
802-
// FIXME: Extract this into a utility method
803-
if (param.isVariadic()) {
804-
auto &ctx = paramType->getASTContext();
805-
paramType = CanType(BoundGenericType::get(ctx.getArrayDecl(),
806-
Type(), {paramType}));
662+
auto params = cast<AnyFunctionType>(getType()).getParams();
663+
return AbstractionPattern(getGenericSignatureForFunctionComponent(),
664+
params[index].getParameterType());
665+
}
666+
case Kind::CurriedCFunctionAsMethodType: {
667+
auto params = cast<AnyFunctionType>(getType()).getParams();
668+
assert(params.size() == 1);
669+
return getCFunctionAsMethodSelfPattern(params[0].getParameterType());
670+
}
671+
case Kind::CFunctionAsMethodType:
672+
case Kind::PartialCurriedCFunctionAsMethodType: {
673+
auto params = cast<AnyFunctionType>(getType()).getParams();
674+
675+
// Only the full method type has a 'self' parameter.
676+
if (getKind() == Kind::CFunctionAsMethodType) {
677+
assert(params.size() > 0);
678+
679+
// The last parameter is 'self'.
680+
if (index == params.size() - 1) {
681+
return getCFunctionAsMethodSelfPattern(params.back().getParameterType());
682+
}
683+
}
684+
685+
// A parameter of type () does not correspond to a Clang parameter.
686+
auto paramType = params[index].getParameterType();
687+
if (paramType->isVoid())
688+
return AbstractionPattern(paramType);
689+
690+
// Otherwise, we're talking about the formal parameter clause.
691+
// Jump over the self parameter in the Clang type.
692+
unsigned clangIndex = index;
693+
auto memberStatus = getImportAsMemberStatus();
694+
if (memberStatus.isInstance() && clangIndex >= memberStatus.getSelfIndex())
695+
++clangIndex;
696+
return AbstractionPattern(getGenericSignatureForFunctionComponent(),
697+
paramType,
698+
getClangFunctionParameterType(getClangType(), clangIndex));
699+
}
700+
case Kind::CurriedObjCMethodType: {
701+
auto params = cast<AnyFunctionType>(getType()).getParams();
702+
assert(params.size() == 1);
703+
return getObjCMethodSelfPattern(params[0].getParameterType());
704+
}
705+
case Kind::ObjCMethodType:
706+
case Kind::PartialCurriedObjCMethodType: {
707+
auto params = cast<AnyFunctionType>(getType()).getParams();
708+
709+
// Only the full method type has a 'self' parameter.
710+
if (getKind() == Kind::ObjCMethodType) {
711+
assert(params.size() > 0);
712+
713+
// The last parameter is 'self'.
714+
if (index == params.size() - 1) {
715+
return getObjCMethodSelfPattern(params.back().getParameterType());
716+
}
717+
}
718+
719+
// A parameter of type () does not correspond to a Clang parameter.
720+
auto paramType = params[index].getParameterType();
721+
if (paramType->isVoid())
722+
return AbstractionPattern(paramType);
723+
724+
// Otherwise, we're talking about the formal parameter clause.
725+
auto method = getObjCMethod();
726+
auto errorInfo = getEncodedForeignErrorInfo();
727+
728+
unsigned paramIndex = index;
729+
if (errorInfo.hasErrorParameter()) {
730+
auto errorParamIndex = errorInfo.getErrorParameterIndex();
731+
732+
if (!errorInfo.isErrorParameterReplacedWithVoid()) {
733+
if (paramIndex >= errorParamIndex) {
734+
paramIndex++;
735+
}
736+
}
807737
}
738+
808739
return AbstractionPattern(getGenericSignatureForFunctionComponent(),
809-
paramType);
740+
paramType,
741+
method->parameters()[paramIndex]->getType().getTypePtr());
742+
}
743+
case Kind::ClangType: {
744+
auto params = cast<AnyFunctionType>(getType()).getParams();
745+
return AbstractionPattern(getGenericSignatureForFunctionComponent(),
746+
params[index].getParameterType(),
747+
getClangFunctionParameterType(getClangType(), index));
810748
}
811749
default:
812-
// FIXME: Re-implement this
813-
auto input = getFunctionInputType();
814-
if (input.isTuple() && input.getNumTupleElements() != 0)
815-
return input.getTupleElementType(index);
816-
return input;
750+
llvm_unreachable("does not have function parameters");
817751
}
818752
}
819753

lib/SIL/Bridging.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ Type TypeConverter::getLoweredBridgedType(AbstractionPattern pattern,
108108

109109
// Look through optional types.
110110
if (auto valueTy = t->getOptionalObjectType()) {
111-
pattern = pattern.transformType([](CanType patternTy) {
112-
return CanType(patternTy->getOptionalObjectType());
113-
});
111+
pattern = pattern.getOptionalObjectType();
114112
auto ty = getLoweredCBridgedType(pattern, valueTy, canBridgeBool, false);
115113
return ty ? OptionalType::get(ty) : ty;
116114
}

0 commit comments

Comments
 (0)