@@ -626,149 +626,36 @@ unsigned GenericSignatureImpl::getGenericParamOrdinal(
626626}
627627
628628Type GenericSignatureImpl::getNonDependentUpperBounds (Type type) const {
629+ return getUpperBound (type);
630+ }
631+
632+ Type GenericSignatureImpl::getDependentUpperBounds (Type type) const {
633+ return getUpperBound (type, /* wantDependentBound=*/ true );
634+ }
635+
636+ Type GenericSignatureImpl::getUpperBound (Type type,
637+ bool wantDependentBound) const {
629638 assert (type->isTypeParameter ());
630639
631640 bool hasExplicitAnyObject = requiresClass (type);
632641
633642 llvm::SmallVector<Type, 2 > types;
634643
635- // Class Inheritence
636- // If the class contains a type parameter that cannot be reduced,
637- // try looking for a non-dependent superclass.
638644 if (Type superclass = getSuperclassBound (type)) {
639- while (superclass &&
640- superclass->hasTypeParameter ()) { // check if the current protocol
641- // has an associated type]
642- auto *boundgeneric = superclass->castTo <BoundGenericClassType>();
643-
644- SmallVector<Type, 2 > argTypes;
645-
646- for (Type argTy : boundgeneric->getGenericArgs ()) {
647- argTypes.push_back (getReducedType (argTy));
648- }
649-
650- boundgeneric = BoundGenericClassType::get (
651- boundgeneric->getDecl (), boundgeneric->getParent (), argTypes);
652- if (!boundgeneric->hasDependentMember () &&
653- !boundgeneric->hasTypeParameter ()) {
654- superclass = boundgeneric;
645+ do {
646+ superclass = getReducedType (superclass);
647+ if (wantDependentBound || !superclass->hasTypeParameter ()) {
655648 break ;
656649 }
657- superclass = superclass->getSuperclass ();
658- }
650+ } while (( superclass = superclass->getSuperclass ()) );
651+
659652 if (superclass) {
660653 types.push_back (superclass);
661654 hasExplicitAnyObject = false ;
662655 }
663656 }
664-
665- // Protocol Inheritence
666- // If there is a reduced type, erase to it.
667- // Otherwise keep going until we hit a type that has unresolved components.
668- for (auto *proto : getRequiredProtocols (type)) {
669- if (proto->requiresClass ())
670- hasExplicitAnyObject = false ;
671-
672- auto *baseType = proto->getDeclaredInterfaceType ()->castTo <ProtocolType>();
673-
674- auto primaryAssocTypes = proto->getPrimaryAssociatedTypes ();
675- if (!primaryAssocTypes.empty ()) {
676- SmallVector<Type, 2 > argTypes;
677-
678- // Attempt to recover same-type requirements on primary associated types.
679- for (auto *assocType : primaryAssocTypes) {
680- // For each primary associated type A of P, compute the reduced type
681- // of T.[P]A.
682- auto *memberType = DependentMemberType::get (type, assocType);
683- auto reducedType = getReducedType (memberType);
684-
685- // If the reduced type is at a lower depth than the root generic
686- // parameter of T, then it's constrained.
687- bool hasOuterGenericParam = false ;
688- bool hasInnerGenericParam = false ;
689- reducedType.visit ([&](Type t) {
690- if (auto *paramTy = t->getAs <GenericTypeParamType>()) {
691- unsigned rootDepth = type->getRootGenericParam ()->getDepth ();
692- if (paramTy->getDepth () == rootDepth)
693- hasInnerGenericParam = true ;
694- else {
695- assert (paramTy->getDepth () < rootDepth);
696- hasOuterGenericParam = true ;
697- }
698- }
699- });
700-
701- if (hasInnerGenericParam && hasOuterGenericParam) {
702- llvm::errs () << " Weird same-type requirements?\n " ;
703- llvm::errs () << " Interface type: " << type << " \n " ;
704- llvm::errs () << " Member type: " << memberType << " \n " ;
705- llvm::errs () << " Reduced member type: " << reducedType << " \n " ;
706- llvm::errs () << GenericSignature (this ) << " \n " ;
707- abort ();
708- }
709-
710- if (!hasInnerGenericParam)
711- argTypes.push_back (reducedType);
712- }
713- // We should have either constrained all primary associated types,
714- // or none of them.
715- if (!argTypes.empty ()) {
716- if (argTypes.size () != primaryAssocTypes.size ()) {
717- llvm::errs () << " Not all primary associated types constrained?\n " ;
718- llvm::errs () << " Interface type: " << type << " \n " ;
719- llvm::errs () << GenericSignature (this ) << " \n " ;
720- abort ();
721- }
722-
723- types.push_back (ParameterizedProtocolType::get (getASTContext (), baseType, argTypes));
724- continue ;
725- }
726- }
727-
728- types.push_back (baseType);
729- }
730-
731- auto constraint = ProtocolCompositionType::get (
732- getASTContext (), types,
733- hasExplicitAnyObject);
734-
735- if (!constraint->isConstraintType ()) {
736- assert (constraint->getClassOrBoundGenericClass ());
737- return constraint;
738- }
739657
740- return ExistentialType::get (constraint);
741- }
742-
743- Type GenericSignatureImpl::getDependentUpperBounds (Type type) const {
744- assert (type->isTypeParameter ());
745-
746- llvm::SmallVector<Type, 2 > types;
747-
748- auto &ctx = type->getASTContext ();
749-
750- bool hasExplicitAnyObject = requiresClass (type);
751-
752- // FIXME: If the superclass bound is implied by one of our protocols, we
753- // shouldn't add it to the constraint type.
754- if (Type superclass = getSuperclassBound (type)) {
755- hasExplicitAnyObject = false ;
756-
757- if (auto *boundgeneric = superclass->getAs <BoundGenericClassType>()) {
758- SmallVector<Type, 2 > argTypes;
759-
760- for (Type argTy : boundgeneric->getGenericArgs ()) {
761- argTypes.push_back (getReducedType (argTy));
762- }
763- boundgeneric = BoundGenericClassType::get (
764- boundgeneric->getDecl (), boundgeneric->getParent (), argTypes);
765- types.push_back (boundgeneric);
766- } else {
767- types.push_back (superclass);
768- }
769- }
770-
771- for (auto proto : getRequiredProtocols (type)) {
658+ for (auto *proto : getRequiredProtocols (type)) {
772659 if (proto->requiresClass ())
773660 hasExplicitAnyObject = false ;
774661
@@ -824,16 +711,15 @@ Type GenericSignatureImpl::getDependentUpperBounds(Type type) const {
824711 //
825712 // In that case just add the base type in the default branch below.
826713 if (argTypes.size () == primaryAssocTypes.size ()) {
827- types.push_back (ParameterizedProtocolType::get (ctx , baseType, argTypes));
714+ types.push_back (ParameterizedProtocolType::get (getASTContext () , baseType, argTypes));
828715 continue ;
829716 }
830717 }
831-
832718 types.push_back (baseType);
833719 }
834720
835- auto constraint = ProtocolCompositionType::get (
836- ctx, types, hasExplicitAnyObject);
721+ auto constraint = ProtocolCompositionType::get (getASTContext (), types,
722+ hasExplicitAnyObject);
837723
838724 if (!constraint->isConstraintType ()) {
839725 assert (constraint->getClassOrBoundGenericClass ());
0 commit comments