1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
|
//===--- SubstitutionMap.cpp - Type substitution map ----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the SubstitutionMap class. A SubstitutionMap packages
// together a set of replacement types and protocol conformances for
// specializing generic types.
//
// SubstitutionMaps either have type parameters or archetypes as keys,
// based on whether they were built from a GenericSignature or a
// GenericEnvironment.
//
// To specialize a type, call Type::subst() with the right SubstitutionMap.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/SubstitutionMap.h"
#include "SubstitutionMapStorage.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/InFlightSubstitution.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/PackConformance.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Defer.h"
#include "llvm/Support/Debug.h"
using namespace swift;
SubstitutionMap::Storage::Storage(
GenericSignature genericSig,
ArrayRef<Type> replacementTypes,
ArrayRef<ProtocolConformanceRef> conformances)
: genericSig(genericSig),
numConformanceRequirements(genericSig->getNumConformanceRequirements())
{
assert(replacementTypes.size() == getNumReplacementTypes());
assert(conformances.size() == numConformanceRequirements);
std::copy(replacementTypes.begin(), replacementTypes.end(),
getReplacementTypes().data());
std::copy(conformances.begin(), conformances.end(),
getConformances().data());
populatedAllReplacements = false;
}
SubstitutionMap::SubstitutionMap(
GenericSignature genericSig,
ArrayRef<Type> replacementTypes,
ArrayRef<ProtocolConformanceRef> conformances)
: storage(Storage::get(genericSig, replacementTypes, conformances)) {
#ifndef NDEBUG
if (genericSig->getASTContext().LangOpts.VerifyAllSubstitutionMaps)
verify();
#endif
}
ArrayRef<Type> SubstitutionMap::getReplacementTypesBuffer() const {
return storage ? storage->getReplacementTypes() : ArrayRef<Type>();
}
MutableArrayRef<Type> SubstitutionMap::getReplacementTypesBuffer() {
return storage ? storage->getReplacementTypes() : MutableArrayRef<Type>();
}
MutableArrayRef<ProtocolConformanceRef>
SubstitutionMap::getConformancesBuffer() {
return storage ? storage->getConformances()
: MutableArrayRef<ProtocolConformanceRef>();
}
ArrayRef<ProtocolConformanceRef> SubstitutionMap::getConformances() const {
return storage ? storage->getConformances()
: ArrayRef<ProtocolConformanceRef>();
}
ArrayRef<Type> SubstitutionMap::getReplacementTypes() const {
if (empty()) return { };
// Make sure we've filled in all of the replacement types.
if (!storage->populatedAllReplacements) {
for (auto gp : getGenericSignature().getGenericParams()) {
(void)lookupSubstitution(cast<SubstitutableType>(gp->getCanonicalType()));
}
storage->populatedAllReplacements = true;
}
return getReplacementTypesBuffer();
}
ArrayRef<Type> SubstitutionMap::getInnermostReplacementTypes() const {
if (empty()) return { };
return getReplacementTypes().take_back(
getGenericSignature().getInnermostGenericParams().size());
}
GenericSignature SubstitutionMap::getGenericSignature() const {
return storage ? storage->getGenericSignature() : nullptr;
}
bool SubstitutionMap::empty() const {
return getGenericSignature().isNull();
}
bool SubstitutionMap::hasAnySubstitutableParams() const {
auto genericSig = getGenericSignature();
if (!genericSig) return false;
return !genericSig->areAllParamsConcrete();
}
bool SubstitutionMap::hasArchetypes() const {
for (Type replacementTy : getReplacementTypesBuffer()) {
if (replacementTy && replacementTy->hasArchetype())
return true;
}
return false;
}
bool SubstitutionMap::hasLocalArchetypes() const {
for (Type replacementTy : getReplacementTypesBuffer()) {
if (replacementTy && replacementTy->hasLocalArchetype())
return true;
}
return false;
}
bool SubstitutionMap::hasDynamicSelf() const {
for (Type replacementTy : getReplacementTypesBuffer()) {
if (replacementTy && replacementTy->hasDynamicSelfType())
return true;
}
return false;
}
bool SubstitutionMap::isCanonical() const {
if (empty()) return true;
if (!getGenericSignature()->isCanonical()) return false;
for (Type replacementTy : getReplacementTypesBuffer()) {
if (replacementTy && !replacementTy->isCanonical())
return false;
}
for (auto conf : getConformances()) {
if (!conf.isCanonical())
return false;
}
return true;
}
SubstitutionMap SubstitutionMap::getCanonical(bool canonicalizeSignature) const {
if (empty()) return *this;
auto sig = getGenericSignature();
if (canonicalizeSignature) sig = sig.getCanonicalSignature();
SmallVector<Type, 4> replacementTypes;
for (Type replacementType : getReplacementTypesBuffer()) {
if (replacementType)
replacementTypes.push_back(replacementType->getCanonicalType());
else
replacementTypes.push_back(nullptr);
}
SmallVector<ProtocolConformanceRef, 4> conformances;
for (auto conf : getConformances()) {
conformances.push_back(conf.getCanonicalConformanceRef());
}
return SubstitutionMap::get(sig,
ArrayRef<Type>(replacementTypes),
ArrayRef<ProtocolConformanceRef>(conformances));
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
SubstitutionMap substitutions) {
if (!genericSig)
return SubstitutionMap();
return SubstitutionMap::get(genericSig,
[&](SubstitutableType *type) -> Type {
return substitutions.lookupSubstitution(
cast<SubstitutableType>(type->getCanonicalType()));
},
LookUpConformanceInSubstitutionMap(substitutions));
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
TypeSubstitutionFn subs,
LookupConformanceFn lookupConformance) {
InFlightSubstitution IFS(subs, lookupConformance, std::nullopt);
return get(genericSig, IFS);
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
ArrayRef<Type> types,
LookupConformanceFn lookupConformance) {
return get(genericSig,
QueryReplacementTypeArray{genericSig, types},
lookupConformance);
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
InFlightSubstitution &IFS) {
if (!genericSig) {
return SubstitutionMap();
}
// Form the replacement types.
SmallVector<Type, 4> replacementTypes;
replacementTypes.reserve(genericSig.getGenericParams().size());
genericSig->forEachParam([&](GenericTypeParamType *gp, bool canonical) {
// Don't eagerly form replacements for non-canonical generic parameters.
if (!canonical) {
replacementTypes.push_back(Type());
return;
}
// Record the replacement.
Type replacement = Type(gp).subst(IFS);
assert((!replacement || replacement->hasError() ||
gp->isParameterPack() == replacement->is<PackType>()) &&
"replacement for pack parameter must be a pack type");
replacementTypes.push_back(replacement);
});
// Form the stored conformances.
SmallVector<ProtocolConformanceRef, 4> conformances;
for (const auto &req : genericSig.getRequirements()) {
if (req.getKind() != RequirementKind::Conformance) continue;
CanType depTy = req.getFirstType()->getCanonicalType();
auto replacement = depTy.subst(IFS);
auto *proto = req.getProtocolDecl();
auto conformance = IFS.lookupConformance(depTy, replacement, proto,
/*level=*/0);
conformances.push_back(conformance);
}
return SubstitutionMap(genericSig, replacementTypes, conformances);
}
Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
if (empty())
return Type();
// If we have an archetype, map out of the context so we can compute a
// conformance access path.
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
// Only consider root archetypes.
if (!archetype->isRoot())
return Type();
if (!isa<PrimaryArchetypeType>(archetype) &&
!isa<PackArchetypeType>(archetype))
return Type();
type = cast<GenericTypeParamType>(
archetype->getInterfaceType()->getCanonicalType());
}
// Find the index of the replacement type based on the generic parameter we
// have.
auto genericParam = cast<GenericTypeParamType>(type);
auto mutableThis = const_cast<SubstitutionMap *>(this);
auto replacementTypes = mutableThis->getReplacementTypesBuffer();
auto genericSig = getGenericSignature();
assert(genericSig);
auto genericParams = genericSig.getGenericParams();
auto replacementIndex =
GenericParamKey(genericParam).findIndexIn(genericParams);
// If this generic parameter isn't represented, we don't have a replacement
// type for it.
if (replacementIndex == genericParams.size())
return Type();
// If we already have a replacement type, return it.
Type &replacementType = replacementTypes[replacementIndex];
if (replacementType)
return replacementType;
// The generic parameter may have been made concrete by the generic signature,
// substitute into the concrete type.
if (auto concreteType = genericSig->getConcreteType(genericParam)) {
// Set the replacement type to an error, to block infinite recursion.
replacementType = ErrorType::get(concreteType);
// Substitute into the replacement type.
replacementType = concreteType.subst(*this);
// If the generic signature is canonical, canonicalize the replacement type.
if (getGenericSignature()->isCanonical())
replacementType = replacementType->getCanonicalType();
return replacementType;
}
// The generic parameter may not be reduced. Retrieve the reduced
// type, which will be dependent.
CanType canonicalType = genericSig.getReducedType(genericParam);
// If nothing changed, we don't have a replacement.
if (canonicalType == type) return Type();
// If we're left with a substitutable type, substitute into that.
// First, set the replacement type to an error, to block infinite recursion.
replacementType = ErrorType::get(type);
replacementType = lookupSubstitution(cast<SubstitutableType>(canonicalType));
// If the generic signature is canonical, canonicalize the replacement type.
if (getGenericSignature()->isCanonical())
replacementType = replacementType->getCanonicalType();
return replacementType;
}
ProtocolConformanceRef
SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
if (empty())
return ProtocolConformanceRef::forInvalid();
// If we have an archetype, map out of the context so we can compute a
// conformance access path.
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
if (!isa<OpaqueTypeArchetypeType>(archetype)) {
type = archetype->getInterfaceType()->getCanonicalType();
}
}
// Error path: if we don't have a type parameter, there is no conformance.
// FIXME: Query concrete conformances in the generic signature?
if (!type->isTypeParameter())
return ProtocolConformanceRef::forInvalid();
auto genericSig = getGenericSignature();
auto getSignatureConformance =
[&](Type type,
ProtocolDecl *proto) -> std::optional<ProtocolConformanceRef> {
unsigned index = 0;
for (auto reqt : genericSig.getRequirements()) {
if (reqt.getKind() == RequirementKind::Conformance) {
if (reqt.getFirstType()->isEqual(type) &&
reqt.getProtocolDecl() == proto)
return getConformances()[index];
++index;
}
}
return std::nullopt;
};
// Fast path -- check if the generic signature directly states the
// conformance.
if (auto directConformance = getSignatureConformance(type, proto))
return *directConformance;
// If the type doesn't conform to this protocol, the result isn't formed
// from these requirements.
if (!genericSig->requiresProtocol(type, proto)) {
Type substType = type.subst(*this);
return ProtocolConformanceRef::forMissingOrInvalid(substType, proto);
}
// If the protocol is invertible, fall back to a global lookup instead of
// evaluating a conformance path, to avoid an infinite substitution issue.
if (proto->getInvertibleProtocolKind()) {
auto substType = type.subst(*this);
if (!substType->isTypeParameter())
return proto->getModuleContext()->lookupConformance(substType, proto);
return ProtocolConformanceRef(proto);
}
auto path = genericSig->getConformancePath(type, proto);
ProtocolConformanceRef conformance;
for (const auto &step : path) {
// For the first step, grab the initial conformance.
if (conformance.isInvalid()) {
if (auto initialConformance = getSignatureConformance(
step.first, step.second)) {
conformance = *initialConformance;
continue;
}
// We couldn't find the initial conformance, fail.
return ProtocolConformanceRef::forInvalid();
}
// If we've hit an abstract conformance, everything from here on out is
// abstract.
// FIXME: This may not always be true, but it holds for now.
if (conformance.isAbstract()) {
// FIXME: Rip this out once we can get a concrete conformance from
// an archetype.
auto substType = type.subst(*this);
if (substType->hasError())
return ProtocolConformanceRef(proto);
if ((!substType->is<ArchetypeType>() ||
substType->castTo<ArchetypeType>()->getSuperclass()) &&
!substType->isTypeParameter() &&
!substType->isExistentialType()) {
auto *M = proto->getParentModule();
return M->lookupConformance(substType, proto);
}
return ProtocolConformanceRef(proto);
}
// For the second step, we're looking into the requirement signature for
// this protocol.
if (conformance.isPack()) {
auto pack = conformance.getPack();
conformance = ProtocolConformanceRef(
pack->getAssociatedConformance(step.first, step.second));
if (conformance.isInvalid())
return conformance;
continue;
}
auto concrete = conformance.getConcrete();
auto normal = concrete->getRootNormalConformance();
// If we haven't set the signature conformances yet, force the issue now.
if (!normal->hasComputedAssociatedConformances()) {
// If we're in the process of checking the type witnesses, fail
// gracefully.
//
// FIXME: This is unsound, because we may not have diagnosed anything but
// still end up with an ErrorType in the AST.
if (proto->getASTContext().evaluator.hasActiveRequest(
ResolveTypeWitnessesRequest{normal})) {
return ProtocolConformanceRef::forInvalid();
}
}
// Get the associated conformance.
conformance = concrete->getAssociatedConformance(step.first, step.second);
if (conformance.isInvalid())
return conformance;
}
return conformance;
}
SubstitutionMap SubstitutionMap::mapReplacementTypesOutOfContext() const {
return subst(MapTypeOutOfContext(),
MakeAbstractConformanceForGenericType(),
SubstFlags::PreservePackExpansionLevel);
}
SubstitutionMap SubstitutionMap::subst(SubstitutionMap subMap,
SubstOptions options) const {
InFlightSubstitutionViaSubMap IFS(subMap, options);
return subst(IFS);
}
SubstitutionMap SubstitutionMap::subst(TypeSubstitutionFn subs,
LookupConformanceFn conformances,
SubstOptions options) const {
InFlightSubstitution IFS(subs, conformances, options);
return subst(IFS);
}
SubstitutionMap SubstitutionMap::subst(InFlightSubstitution &IFS) const {
if (empty()) return SubstitutionMap();
SmallVector<Type, 4> newSubs;
for (Type type : getReplacementTypesBuffer()) {
if (!type) {
// Non-canonical parameter.
newSubs.push_back(Type());
continue;
}
newSubs.push_back(type.subst(IFS));
assert(type->is<PackType>() == newSubs.back()->is<PackType>() &&
"substitution changed the pack-ness of a replacement type");
}
SmallVector<ProtocolConformanceRef, 4> newConformances;
auto oldConformances = getConformances();
auto genericSig = getGenericSignature();
for (const auto &req : genericSig.getRequirements()) {
if (req.getKind() != RequirementKind::Conformance) continue;
auto conformance = oldConformances[0];
// Fast path for concrete case -- we don't need to compute substType
// at all.
if (conformance.isConcrete() &&
!IFS.shouldSubstituteOpaqueArchetypes()) {
newConformances.push_back(
ProtocolConformanceRef(conformance.getConcrete()->subst(IFS)));
} else {
auto origType = req.getFirstType();
auto substType = origType.subst(*this, IFS.getOptions());
newConformances.push_back(conformance.subst(substType, IFS));
}
oldConformances = oldConformances.slice(1);
}
assert(oldConformances.empty());
return SubstitutionMap(genericSig, newSubs, newConformances);
}
SubstitutionMap
SubstitutionMap::getProtocolSubstitutions(ProtocolDecl *protocol,
Type selfType,
ProtocolConformanceRef conformance) {
return get(protocol->getGenericSignature(), llvm::ArrayRef<Type>(selfType),
llvm::ArrayRef<ProtocolConformanceRef>(conformance));
}
SubstitutionMap
SubstitutionMap::getOverrideSubstitutions(
const ValueDecl *baseDecl,
const ValueDecl *derivedDecl) {
// For overrides within a protocol hierarchy, substitute the Self type.
if (auto baseProto = baseDecl->getDeclContext()->getSelfProtocolDecl()) {
auto baseSig = baseDecl->getInnermostDeclContext()
->getGenericSignatureOfContext();
return baseSig->getIdentitySubstitutionMap();
}
auto *baseClass = baseDecl->getDeclContext()->getSelfClassDecl();
auto *derivedClass = derivedDecl->getDeclContext()->getSelfClassDecl();
auto baseSig = baseDecl->getInnermostDeclContext()
->getGenericSignatureOfContext();
// If more kinds of overridable decls with generic parameter lists appear,
// add them here.
GenericParamList *derivedParams = nullptr;
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(derivedDecl))
derivedParams = funcDecl->getGenericParams();
else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(derivedDecl))
derivedParams = subscriptDecl->getGenericParams();
return getOverrideSubstitutions(baseClass, derivedClass, baseSig, derivedParams);
}
OverrideSubsInfo::OverrideSubsInfo(const NominalTypeDecl *baseNominal,
const NominalTypeDecl *derivedNominal,
GenericSignature baseSig,
const GenericParamList *derivedParams)
: Ctx(baseSig->getASTContext()),
BaseDepth(0),
OrigDepth(0),
DerivedParams(derivedParams) {
if (auto baseNominalSig = baseNominal->getGenericSignature()) {
BaseDepth = baseNominalSig.getGenericParams().back()->getDepth() + 1;
auto *genericEnv = derivedNominal->getGenericEnvironment();
auto derivedNominalTy = derivedNominal->getDeclaredInterfaceType();
// FIXME: Map in and out of context to get more accurate
// conformance information. If the base generic signature
// is <T: P> and the derived generic signature is <T: C>
// where C is a class that conforms to P, then we want the
// substitution map to store the concrete conformance C: P
// and not the abstract conformance T: P.
if (genericEnv) {
derivedNominalTy = genericEnv->mapTypeIntoContext(
derivedNominalTy);
}
BaseSubMap = derivedNominalTy->getContextSubstitutionMap(
baseNominal->getParentModule(), baseNominal,
genericEnv);
BaseSubMap = BaseSubMap.mapReplacementTypesOutOfContext();
}
if (auto derivedNominalSig = derivedNominal->getGenericSignature())
OrigDepth = derivedNominalSig.getGenericParams().back()->getDepth() + 1;
}
Type QueryOverrideSubs::operator()(SubstitutableType *type) const {
if (auto gp = type->getAs<GenericTypeParamType>()) {
if (gp->getDepth() >= info.BaseDepth) {
assert(gp->getDepth() == info.BaseDepth);
if (info.DerivedParams != nullptr) {
return info.DerivedParams->getParams()[gp->getIndex()]
->getDeclaredInterfaceType();
}
return GenericTypeParamType::get(
gp->isParameterPack(),
gp->getDepth() + info.OrigDepth - info.BaseDepth,
gp->getIndex(), info.Ctx);
}
}
return Type(type).subst(info.BaseSubMap);
}
ProtocolConformanceRef
LookUpConformanceInOverrideSubs::operator()(CanType type,
Type substType,
ProtocolDecl *proto) const {
if (type->getRootGenericParam()->getDepth() >= info.BaseDepth)
return ProtocolConformanceRef(proto);
if (auto conformance = info.BaseSubMap.lookupConformance(type, proto))
return conformance;
if (substType->isTypeParameter())
return ProtocolConformanceRef(proto);
return proto->getParentModule()->lookupConformance(substType, proto);
}
SubstitutionMap
SubstitutionMap::getOverrideSubstitutions(const NominalTypeDecl *baseNominal,
const NominalTypeDecl *derivedNominal,
GenericSignature baseSig,
const GenericParamList *derivedParams) {
if (baseSig.isNull())
return SubstitutionMap();
OverrideSubsInfo info(baseNominal, derivedNominal, baseSig, derivedParams);
return get(baseSig,
QueryOverrideSubs(info),
LookUpConformanceInOverrideSubs(info));
}
SubstitutionMap
SubstitutionMap::combineSubstitutionMaps(SubstitutionMap firstSubMap,
SubstitutionMap secondSubMap,
CombineSubstitutionMaps how,
unsigned firstDepthOrIndex,
unsigned secondDepthOrIndex,
GenericSignature genericSig) {
auto &ctx = genericSig->getASTContext();
auto replaceGenericParameter = [&](Type type) -> Type {
if (auto gp = type->getAs<GenericTypeParamType>()) {
if (how == CombineSubstitutionMaps::AtDepth) {
if (gp->getDepth() < firstDepthOrIndex)
return Type();
return GenericTypeParamType::get(gp->isParameterPack(),
gp->getDepth() + secondDepthOrIndex -
firstDepthOrIndex,
gp->getIndex(), ctx);
}
assert(how == CombineSubstitutionMaps::AtIndex);
if (gp->getIndex() < firstDepthOrIndex)
return Type();
return GenericTypeParamType::get(
gp->isParameterPack(), gp->getDepth(),
gp->getIndex() + secondDepthOrIndex - firstDepthOrIndex, ctx);
}
return type;
};
return get(
genericSig,
[&](SubstitutableType *type) {
if (auto replacement = replaceGenericParameter(type))
return Type(replacement).subst(secondSubMap);
return Type(type).subst(firstSubMap);
},
[&](CanType type, Type substType, ProtocolDecl *proto) {
if (auto replacement = type.transform(replaceGenericParameter))
return secondSubMap.lookupConformance(replacement->getCanonicalType(),
proto);
if (auto conformance = firstSubMap.lookupConformance(type, proto))
return conformance;
// We might not have enough information in the substitution maps alone.
//
// Eg,
//
// class Base<T1> {
// func foo<U1>(_: U1) where T1 : P {}
// }
//
// class Derived<T2> : Base<Foo<T2>> {
// override func foo<U2>(_: U2) where T2 : Q {}
// }
//
// Suppose we're devirtualizing a call to Base.foo() on a value whose
// type is known to be Derived<Bar>. We start with substitutions written
// in terms of Base.foo()'s generic signature:
//
// <T1, U1 where T1 : P>
// T1 := Foo<Bar>
// T1 : P := Foo<Bar> : P
//
// We want to build substitutions in terms of Derived.foo()'s
// generic signature:
//
// <T2, U2 where T2 : Q>
// T2 := Bar
// T2 : Q := Bar : Q
//
// The conformance Bar : Q is difficult to recover in the general case.
//
// Some combination of storing substitution maps in BoundGenericTypes
// as well as for method overrides would solve this, but for now, just
// punt to module lookup.
if (substType->isTypeParameter())
return ProtocolConformanceRef(proto);
return proto->getParentModule()->lookupConformance(substType, proto);
});
}
void SubstitutionMap::verify() const {
#ifndef NDEBUG
if (empty())
return;
unsigned conformanceIndex = 0;
for (const auto &req : getGenericSignature().getRequirements()) {
if (req.getKind() != RequirementKind::Conformance)
continue;
SWIFT_DEFER { ++conformanceIndex; };
auto substType = req.getFirstType().subst(*this);
if (substType->isTypeParameter() ||
substType->is<ArchetypeType>() ||
substType->isTypeVariableOrMember() ||
substType->is<UnresolvedType>() ||
substType->hasError())
continue;
auto conformance = getConformances()[conformanceIndex];
if (conformance.isInvalid())
continue;
// All of the conformances should be concrete.
if (!conformance.isConcrete()) {
llvm::dbgs() << "Concrete type cannot have abstract conformance:\n";
substType->dump(llvm::dbgs());
llvm::dbgs() << "SubstitutionMap:\n";
dump(llvm::dbgs());
llvm::dbgs() << "\n";
llvm::dbgs() << "Requirement:\n";
req.dump(llvm::dbgs());
llvm::dbgs() << "\n";
}
assert(conformance.isConcrete() && "Conformance should be concrete");
if (substType->is<UnboundGenericType>())
continue;
auto conformanceTy = conformance.getConcrete()->getType();
if (conformanceTy->hasTypeParameter()
&& !substType->hasTypeParameter()) {
conformanceTy = conformance.getConcrete()->getDeclContext()
->mapTypeIntoContext(conformanceTy);
}
if (!substType->isEqual(conformanceTy)) {
llvm::dbgs() << "Conformance must match concrete replacement type:\n";
substType->dump(llvm::dbgs());
llvm::dbgs() << "Conformance type:\n";
conformance.getConcrete()->getType()->dump(llvm::dbgs());
llvm::dbgs() << "Conformance:\n";
conformance.dump(llvm::dbgs());
llvm::dbgs() << "\n";
llvm::dbgs() << "SubstitutionMap:\n";
dump(llvm::dbgs());
llvm::dbgs() << "\n";
llvm::dbgs() << "Requirement:\n";
req.dump(llvm::dbgs());
llvm::dbgs() << "\n";
}
assert(substType->isEqual(conformanceTy)
&& "conformance should match corresponding type");
if (substType->isExistentialType()) {
assert(isa<SelfProtocolConformance>(conformance.getConcrete()) &&
"Existential type cannot have normal conformance");
}
}
#endif
}
void SubstitutionMap::profile(llvm::FoldingSetNodeID &id) const {
id.AddPointer(storage);
}
bool SubstitutionMap::isIdentity() const {
if (empty())
return true;
for (auto conf : getConformances()) {
if (conf.isAbstract())
continue;
if (conf.isPack()) {
auto patternConfs = conf.getPack()->getPatternConformances();
if (patternConfs.size() == 1 && patternConfs[0].isAbstract())
continue;
}
return false;
}
GenericSignature sig = getGenericSignature();
bool hasNonIdentityReplacement = false;
auto replacements = getReplacementTypesBuffer();
sig->forEachParam([&](GenericTypeParamType *paramTy, bool isCanonical) {
if (isCanonical) {
Type wrappedParamTy = paramTy;
if (paramTy->isParameterPack())
wrappedParamTy = PackType::getSingletonPackExpansion(paramTy);
if (!wrappedParamTy->isEqual(replacements[0]))
hasNonIdentityReplacement = true;
}
replacements = replacements.slice(1);
});
assert(replacements.empty());
return !hasNonIdentityReplacement;
}
SubstitutionMap SubstitutionMap::mapIntoTypeExpansionContext(
TypeExpansionContext context) const {
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
context.getContext(), context.getResilienceExpansion(),
context.isWholeModuleContext());
return this->subst(replacer, replacer,
SubstFlags::SubstituteOpaqueArchetypes |
SubstFlags::PreservePackExpansionLevel);
}
|