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 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
|
//===--- ConformanceLookupTable - Conformance Lookup Table ----------------===//
//
// 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 implements the ConformanceLookupTable class.
//
//===----------------------------------------------------------------------===//
#include "ConformanceLookupTable.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/TypeCheckRequests.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
DeclContext *ConformanceLookupTable::ConformanceSource::getDeclContext() const {
switch (getKind()) {
case ConformanceEntryKind::Inherited:
return getInheritingClass();
case ConformanceEntryKind::Explicit:
return getExplicitDeclContext();
case ConformanceEntryKind::Implied:
return getImpliedSource()->Source.getDeclContext();
case ConformanceEntryKind::Synthesized:
return getSynthesizedDeclContext();
case ConformanceEntryKind::PreMacroExpansion:
return getMacroGeneratedDeclContext();
}
llvm_unreachable("Unhandled ConformanceEntryKind in switch.");
}
ProtocolDecl *ConformanceLookupTable::ConformanceEntry::getProtocol() const {
if (auto protocol = Conformance.dyn_cast<ProtocolDecl *>())
return protocol;
return Conformance.get<ProtocolConformance *>()->getProtocol();
}
void ConformanceLookupTable::ConformanceEntry::markSupersededBy(
ConformanceLookupTable &table,
ConformanceEntry *entry,
bool diagnose) {
assert(!isSuperseded() && "Already superseded");
// Note that we've been superseded.
SupersededBy = entry;
if (diagnose) {
// Record the problem in the conformance table. We'll
// diagnose these in semantic analysis.
table.AllSupersededDiagnostics[getDeclContext()].push_back(this);
}
}
void ConformanceLookupTable::ConformanceEntry::dump() const {
dump(llvm::errs());
}
void ConformanceLookupTable::ConformanceEntry::dump(raw_ostream &os,
unsigned indent) const {
os.indent(indent) << "(conformance @" << static_cast<const void *>(this);
os << " protocol=";
getProtocol()->dumpRef(os);
if (Loc.isValid()) {
os << " loc=";
Loc.print(os, getProtocol()->getASTContext().SourceMgr);
}
switch (getKind()) {
case ConformanceEntryKind::Implied:
os << " implied_by=@"
<< static_cast<const void *>(Source.getImpliedSource());
break;
case ConformanceEntryKind::Explicit:
os << " explicit";
break;
case ConformanceEntryKind::Inherited:
os << " inherited";
break;
case ConformanceEntryKind::Synthesized:
os << " synthesized";
break;
case ConformanceEntryKind::PreMacroExpansion:
os << " unexpanded macro";
break;
}
if (auto conf = getConformance()) {
os << " fixed_conformance=@" << static_cast<const void *>(conf);
}
if (SupersededBy)
os << " superseded_by=@" << static_cast<const void *>(SupersededBy);
os << ")\n";
}
ConformanceLookupTable::ConformanceLookupTable(ASTContext &ctx) {
// Register a cleanup with the ASTContext to call the conformance
// table destructor.
ctx.addCleanup([this]() {
this->destroy();
});
}
void ConformanceLookupTable::destroy() {
this->~ConformanceLookupTable();
}
namespace {
struct ConformanceConstructionInfo : public Located<ProtocolDecl *> {
/// The location of the "unchecked" attribute, if present.
const SourceLoc uncheckedLoc;
/// The location of the "preconcurrency" attribute if present.
const SourceLoc preconcurrencyLoc;
ConformanceConstructionInfo() { }
ConformanceConstructionInfo(ProtocolDecl *item, SourceLoc loc,
SourceLoc uncheckedLoc,
SourceLoc preconcurrencyLoc)
: Located(item, loc), uncheckedLoc(uncheckedLoc),
preconcurrencyLoc(preconcurrencyLoc) {}
};
}
template<typename NominalFunc, typename ExtensionFunc>
void ConformanceLookupTable::forEachInStage(ConformanceStage stage,
NominalTypeDecl *nominal,
NominalFunc nominalFunc,
ExtensionFunc extensionFunc) {
assert(static_cast<unsigned>(stage) < NumConformanceStages &&
"NumConformanceStages has not been updated");
LastProcessedEntry &lastProcessed
= LastProcessed[nominal][static_cast<unsigned>(stage)];
// Handle the nominal type.
if (!lastProcessed.getInt()) {
lastProcessed.setInt(true);
// If we have conformances we can load, do so.
// FIXME: This could be lazier.
auto loader = nominal->takeConformanceLoader();
if (loader.first) {
SmallVector<ProtocolConformance *, 2> conformances;
loader.first->loadAllConformances(nominal, loader.second, conformances);
registerProtocolConformances(nominal, conformances);
}
nominalFunc(nominal);
}
// Protocol extensions do not contribute protocol conformances. This
// is enforced by semantic analysis, so the early exit here is a
// performance optimization and also prevents us from erroneously
// including those protocols before they get diagnosed.
if (isa<ProtocolDecl>(nominal))
return;
// Handle the extensions that we have not yet visited.
nominal->prepareExtensions();
while (auto next = lastProcessed.getPointer()
? lastProcessed.getPointer()->NextExtension.getPointer()
: nominal->FirstExtension) {
lastProcessed.setPointer(next);
SmallVector<ConformanceConstructionInfo, 2> protocols;
// If we have conformances we can load, do so.
// FIXME: This could be lazier.
auto loader = next->takeConformanceLoader();
if (loader.first) {
SmallVector<ProtocolConformance *, 2> conformances;
loader.first->loadAllConformances(next, loader.second, conformances);
registerProtocolConformances(next, conformances);
for (auto conf : conformances) {
protocols.push_back(
{conf->getProtocol(), SourceLoc(), SourceLoc(), SourceLoc()});
}
} else if (next->getParentSourceFile() ||
next->getParentModule()->isBuiltinModule()) {
InvertibleProtocolSet inverses;
bool anyObject = false;
for (const auto &found :
getDirectlyInheritedNominalTypeDecls(next, inverses, anyObject)) {
if (auto proto = dyn_cast<ProtocolDecl>(found.Item))
protocols.push_back(
{proto, found.Loc, found.uncheckedLoc, found.preconcurrencyLoc});
}
}
extensionFunc(next, protocols);
}
}
void ConformanceLookupTable::inheritConformances(ClassDecl *classDecl,
ClassDecl *superclassDecl,
ExtensionDecl *superclassExt) {
// Local function to return the location of the superclass. This
// takes a little digging, so compute on first use and cache it.
SourceLoc superclassLoc;
auto getSuperclassLoc = [&] {
if (superclassLoc.isValid())
return superclassLoc;
auto inheritedTypes = classDecl->getInherited();
for (unsigned i : inheritedTypes.getIndices()) {
if (auto inheritedType = inheritedTypes.getEntry(i).getType()) {
if (inheritedType->getClassOrBoundGenericClass()) {
superclassLoc = inheritedTypes.getEntry(i).getSourceRange().Start;
return superclassLoc;
}
if (inheritedType->isExistentialType()) {
auto layout = inheritedType->getExistentialLayout();
if (layout.explicitSuperclass) {
superclassLoc = inheritedTypes.getEntry(i).getSourceRange().Start;
return superclassLoc;
}
}
}
}
superclassLoc = superclassDecl->getLoc();
return superclassLoc;
};
llvm::SmallPtrSet<ProtocolDecl *, 4> protocols;
auto addInheritedConformance = [&](ConformanceEntry *entry) {
auto protocol = entry->getProtocol();
// Don't add unavailable conformances.
if (auto dc = entry->Source.getDeclContext()) {
if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
if (AvailableAttr::isUnavailable(ext))
return;
}
}
// Don't add redundant conformances here. This is merely an
// optimization; resolveConformances() would zap the duplicates
// anyway.
if (!protocols.insert(protocol).second)
return;
// Add the inherited entry.
(void)addProtocol(protocol, getSuperclassLoc(),
ConformanceSource::forInherited(classDecl));
};
// Add inherited conformances.
DeclContext *superDC = superclassExt;
if (!superclassExt)
superDC = superclassDecl;
for (auto entry : superclassDecl->ConformanceTable->AllConformances[superDC]){
addInheritedConformance(entry);
}
}
void ConformanceLookupTable::updateLookupTable(NominalTypeDecl *nominal,
ConformanceStage stage) {
switch (stage) {
case ConformanceStage::RecordedExplicit:
// Record all of the explicit conformances.
forEachInStage(
stage, nominal,
[&](NominalTypeDecl *nominal) {
auto source = ConformanceSource::forExplicit(nominal);
// Get all of the protocols in the inheritance clause.
InvertibleProtocolSet inverses;
bool anyObject = false;
for (const auto &found :
getDirectlyInheritedNominalTypeDecls(nominal, inverses, anyObject)) {
auto proto = dyn_cast<ProtocolDecl>(found.Item);
if (!proto)
continue;
auto kp = proto->getKnownProtocolKind();
assert(!found.isSuppressed ||
kp.has_value() &&
"suppressed conformance for non-known protocol!?");
if (!found.isSuppressed) {
addProtocol(proto, found.Loc,
source.withUncheckedLoc(found.uncheckedLoc)
.withPreconcurrencyLoc(found.preconcurrencyLoc));
}
}
addMacroGeneratedProtocols(
nominal, ConformanceSource::forUnexpandedMacro(nominal));
},
[&](ExtensionDecl *ext, ArrayRef<ConformanceConstructionInfo> protos) {
// The extension decl may not be validated, so we can't use
// its inherited protocols directly.
auto source = ConformanceSource::forExplicit(ext);
for (auto locAndProto : protos)
addProtocol(
locAndProto.Item, locAndProto.Loc,
source.withUncheckedLoc(locAndProto.uncheckedLoc)
.withPreconcurrencyLoc(locAndProto.preconcurrencyLoc));
});
break;
case ConformanceStage::Inherited:
updateLookupTable(nominal, ConformanceStage::RecordedExplicit);
// For classes, expand implied conformances of the superclass,
// because an implied conformance in the superclass is considered
// "fixed" in the subclass.
if (auto classDecl = dyn_cast<ClassDecl>(nominal)) {
if (auto superclassDecl = classDecl->getSuperclassDecl()) {
// Break infinite recursion when visiting ill-formed classes
// with circular inheritance.
if (VisitingSuperclass)
return;
llvm::SaveAndRestore<bool> visiting(VisitingSuperclass, true);
// Don't update our own lookup table if we inherit from ourselves.
if (classDecl == superclassDecl)
break;
// Resolve the conformances of the superclass.
superclassDecl->prepareConformanceTable();
superclassDecl->ConformanceTable->updateLookupTable(
superclassDecl,
ConformanceStage::Resolved);
// Expand inherited conformances from all superclasses.
// We may have circular inheritance in ill-formed classes, so keep an
// eye out for that.
auto circularSuperclass = superclassDecl->getSuperclassDecl();
do {
forEachInStage(
stage, superclassDecl,
[&](NominalTypeDecl *superclass) {
inheritConformances(classDecl, superclassDecl, nullptr);
},
[&](ExtensionDecl *ext,
ArrayRef<ConformanceConstructionInfo> protos) {
(void)protos;
inheritConformances(classDecl, superclassDecl, ext);
});
superclassDecl = superclassDecl->getSuperclassDecl();
if (circularSuperclass)
circularSuperclass = circularSuperclass->getSuperclassDecl();
if (circularSuperclass)
circularSuperclass = circularSuperclass->getSuperclassDecl();
} while (superclassDecl != circularSuperclass);
}
}
break;
case ConformanceStage::ExpandedImplied:
// Record explicit conformances and import inherited conformances
// before expanding.
updateLookupTable(nominal, ConformanceStage::Inherited);
// Expand inherited conformances.
forEachInStage(
stage, nominal,
[&](NominalTypeDecl *nominal) {
expandImpliedConformances(nominal, nominal);
},
[&](ExtensionDecl *ext,
ArrayRef<ConformanceConstructionInfo> protos) {
(void)protos;
expandImpliedConformances(nominal, ext);
});
break;
case ConformanceStage::Resolved:
// Expand inherited conformances so we have the complete set of
// conformances.
updateLookupTable(nominal, ConformanceStage::ExpandedImplied);
/// Determine whether any extensions were added that might require
/// us to compute conformances again.
bool anyChanged = false;
forEachInStage(stage, nominal,
[&](NominalTypeDecl *nominal) { anyChanged = true; },
[&](ExtensionDecl *ext,
ArrayRef<ConformanceConstructionInfo>) {
anyChanged = true;
});
if (anyChanged) {
// Compute the conformances for each protocol.
bool anySuperseded = false;
for (const auto &entry : Conformances) {
if (resolveConformances(entry.first))
anySuperseded = true;
}
if (anySuperseded) {
// Update the lists of all conformances to remove superseded
// conformances.
for (auto &conformances : AllConformances) {
conformances.second.erase(
std::remove_if(conformances.second.begin(),
conformances.second.end(),
[&](ConformanceEntry *entry) {
return entry->isSuperseded();
}),
conformances.second.end());
}
}
}
break;
}
}
void ConformanceLookupTable::registerProtocolConformances(
DeclContext *dc,
ArrayRef<ProtocolConformance*> conformances) {
// If this declaration context came from source, there's nothing to
// do here.
assert(!dc->getParentSourceFile() &&
!dc->getParentModule()->isBuiltinModule());
// Add entries for each loaded conformance.
for (auto conformance : conformances) {
registerProtocolConformance(dc, conformance);
}
}
bool ConformanceLookupTable::addProtocol(ProtocolDecl *protocol, SourceLoc loc,
ConformanceSource source) {
DeclContext *dc = source.getDeclContext();
ASTContext &ctx = dc->getASTContext();
// Determine the kind of conformance.
ConformanceEntryKind kind = source.getKind();
// If this entry is synthesized or implied, scan to determine
// whether there are any explicit better conformances that make this
// conformance trivially superseded (and, therefore, not worth
// recording).
auto &conformanceEntries = Conformances[protocol];
if (kind == ConformanceEntryKind::Implied ||
kind == ConformanceEntryKind::Synthesized) {
for (const auto *existingEntry : conformanceEntries) {
switch (existingEntry->getKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Inherited:
case ConformanceEntryKind::PreMacroExpansion:
return false;
case ConformanceEntryKind::Implied:
// Ignore implied circular protocol inheritance
if (existingEntry->getDeclContext() == dc)
return false;
// An implied conformance is better than a synthesized one, unless
// the implied conformance was deserialized.
if (kind == ConformanceEntryKind::Synthesized &&
existingEntry->getDeclContext()->getParentSourceFile() == nullptr)
return false;
break;
case ConformanceEntryKind::Synthesized:
// An implied conformance is better unless it was deserialized.
if (dc->getParentSourceFile() == nullptr)
return false;
break;
}
}
}
/// Build the conformance entry (if it hasn't been built before).
ConformanceEntry *entry = new (ctx) ConformanceEntry(loc, protocol, source);
conformanceEntries.push_back(entry);
// Record this as a conformance within the given declaration
// context.
AllConformances[dc].push_back(entry);
return true;
}
void ConformanceLookupTable::addMacroGeneratedProtocols(
NominalTypeDecl *nominal, ConformanceSource source) {
nominal->forEachAttachedMacro(
MacroRole::Extension,
[&](CustomAttr *attr, MacroDecl *macro) {
SmallVector<ProtocolDecl *, 2> conformances;
macro->getIntroducedConformances(
nominal, MacroRole::Extension, conformances);
for (auto *protocol : conformances) {
addProtocol(protocol, attr->getLocation(), source);
}
});
}
void ConformanceLookupTable::expandImpliedConformances(NominalTypeDecl *nominal,
DeclContext *dc) {
ASTContext &ctx = nominal->getASTContext();
// Note: recursive type-checking implies that AllConformances
// may be reallocated during this traversal, so pay the lookup cost
// during each iteration.
for (unsigned i = 0; i != AllConformances[dc].size(); ++i) {
/// FIXME: Avoid the possibility of an infinite loop by fixing the root
/// cause instead (incomplete circularity detection).
assert(i <= 16384 &&
"Infinite loop due to circular protocol inheritance?");
ConformanceEntry *conformanceEntry = AllConformances[dc][i];
ProtocolDecl *conformingProtocol = conformanceEntry->getProtocol();
// An @objc enum that explicitly conforms to the Error protocol
// also implicitly conforms to _ObjectiveCBridgeableError, via the
// known protocol _BridgedNSError.
if (conformingProtocol->isSpecificProtocol(
KnownProtocolKind::Error) &&
isa<EnumDecl>(nominal) && nominal->isObjC() &&
cast<EnumDecl>(nominal)->hasCases() &&
cast<EnumDecl>(nominal)->hasOnlyCasesWithoutAssociatedValues()) {
if (auto bridgedNSError
= ctx.getProtocol(KnownProtocolKind::BridgedNSError)) {
addProtocol(bridgedNSError, SourceLoc(),
ConformanceSource::forImplied(conformanceEntry));
}
}
auto source = ConformanceSource::forImplied(conformanceEntry);
for (auto *inherited : conformingProtocol->getInheritedProtocols()) {
// Conforming a ~Copyable nominal to a protocol that inherits Copyable
// should not imply a Copyable conformance on the nominal.
if (inherited->getInvertibleProtocolKind())
continue;
addProtocol(inherited, SourceLoc(), source);
}
}
}
/// Determine whether the given conformance entry kind can be replaced.
static bool isReplaceable(ConformanceEntryKind kind) {
switch (kind) {
case ConformanceEntryKind::Implied:
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::PreMacroExpansion:
return true;
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Inherited:
return false;
}
llvm_unreachable("Unhandled ConformanceEntryKind in switch.");
}
ConformanceLookupTable::Ordering ConformanceLookupTable::compareConformances(
ConformanceEntry *lhs,
ConformanceEntry *rhs,
bool &diagnoseSuperseded) {
ConformanceEntryKind lhsKind = lhs->getRankingKind();
ConformanceEntryKind rhsKind = rhs->getRankingKind();
// Pre-expanded macro conformances are always superseded by
// conformances written in source. If the conformance is not
// written in the original source, the pre-expanded conformance
// will be superseded by the conformance in the macro expansion
// buffer.
if (lhsKind == ConformanceEntryKind::PreMacroExpansion ||
rhsKind == ConformanceEntryKind::PreMacroExpansion) {
if (lhsKind != rhsKind) {
return (lhs->getKind() < rhs->getKind()
? Ordering::Before
: Ordering::After);
}
}
// If only one of the conformances is unconditionally available on the
// current deployment target, pick that one.
//
// FIXME: Conformance lookup should really depend on source location for
// this to be 100% correct.
// FIXME: When a class and an extension with the same availability declare the
// same conformance, this silently takes the class and drops the extension.
if (lhs->getDeclContext()->isAlwaysAvailableConformanceContext() !=
rhs->getDeclContext()->isAlwaysAvailableConformanceContext()) {
return (lhs->getDeclContext()->isAlwaysAvailableConformanceContext()
? Ordering::Before
: Ordering::After);
}
// If one entry is fixed and the other is not, we have our answer.
if (lhs->isFixed() != rhs->isFixed()) {
auto isReplaceableOrMarker = [](ConformanceEntry *entry) -> bool {
ConformanceEntryKind kind = entry->getRankingKind();
if (isReplaceable(kind))
return true;
// Allow replacement of an explicit conformance to a marker protocol.
// (This permits redundant explicit declarations of `Sendable`.)
return (kind == ConformanceEntryKind::Explicit
&& entry->getProtocol()->isMarkerProtocol());
};
// If the non-fixed conformance is not replaceable, we have a failure to
// diagnose.
// FIXME: We should probably diagnose if they have different constraints.
diagnoseSuperseded = (lhs->isFixed() && !isReplaceableOrMarker(rhs)) ||
(rhs->isFixed() && !isReplaceableOrMarker(lhs));
return lhs->isFixed() ? Ordering::Before : Ordering::After;
}
if (lhsKind != ConformanceEntryKind::Implied ||
rhsKind != ConformanceEntryKind::Implied) {
// If both conformances are non-replaceable, diagnose the
// superseded one.
diagnoseSuperseded = !isReplaceable(lhsKind) && !isReplaceable(rhsKind) &&
!(lhsKind == ConformanceEntryKind::Inherited &&
rhsKind == ConformanceEntryKind::Inherited);
// If we can order by kind, do so.
if (lhsKind != rhsKind) {
return (static_cast<unsigned>(lhsKind) < static_cast<unsigned>(rhsKind))
? Ordering::Before
: Ordering::After;
}
// We shouldn't get two synthesized conformances. It's not harmful
// per se, but it's indicative of redundant logic in the frontend.
assert((lhs->getKind() != ConformanceEntryKind::Synthesized ||
rhs->getKind() != ConformanceEntryKind::Synthesized) &&
"Shouldn't ever get two truly synthesized conformances");
// FIXME: Deterministic ordering.
return Ordering::Before;
}
// Both the left- and right-hand sides are implied, so determine where the
// conformance should go.
assert(lhsKind == ConformanceEntryKind::Implied &&
"Expected implied conformance");
assert(rhsKind == ConformanceEntryKind::Implied &&
"Expected implied conformance");
diagnoseSuperseded = false;
// First, try to use the stated explicit conformances to determine where the
// conformance should go.
auto lhsExplicit = lhs->getDeclaredConformance();
auto lhsExplicitProtocol = lhsExplicit->getProtocol();
auto rhsExplicit = rhs->getDeclaredConformance();
auto rhsExplicitProtocol = rhsExplicit->getProtocol();
if (lhsExplicitProtocol != rhsExplicitProtocol) {
// If the explicit protocol for the left-hand side is implied by
// the explicit protocol for the right-hand side, the left-hand
// side supersedes the right-hand side.
if (rhsExplicitProtocol->inheritsFrom(lhsExplicitProtocol))
return Ordering::Before;
// If the explicit protocol for the right-hand side is implied by
// the explicit protocol for the left-hand side, the right-hand
// side supersedes the left-hand side.
if (lhsExplicitProtocol->inheritsFrom(rhsExplicitProtocol))
return Ordering::After;
}
// Prefer the least conditional implier, which we approximate by seeing if one
// of the contexts syntactically has no generic requirements. This misses
// redundant cases like `struct Foo<T: P> {} extension Foo: P where T: P {}`
// (Foo : P is unconditional), but isConstrainedExtension doesn't fly as it
// requires the generic signature of the extension to exist, which requires
// conformances to exist, which is what we're doing here.
auto hasAdditionalRequirements = [&](ConformanceEntry *entry) {
if (auto ED = dyn_cast<ExtensionDecl>(entry->getDeclContext()))
if (auto TWC = ED->getTrailingWhereClause())
return !TWC->getRequirements().empty();
return false;
};
bool lhsHasReqs = hasAdditionalRequirements(lhs);
bool rhsHasReqs = hasAdditionalRequirements(rhs);
if (lhsHasReqs != rhsHasReqs)
return lhsHasReqs ? Ordering::After : Ordering::Before;
// If the two conformances come from the same file, pick the first context
// in the file.
auto lhsSF = lhs->getDeclContext()->getParentSourceFile();
auto rhsSF = rhs->getDeclContext()->getParentSourceFile();
if (lhsSF && lhsSF == rhsSF) {
ASTContext &ctx = lhsSF->getASTContext();
return ctx.SourceMgr.isBeforeInBuffer(lhs->getDeclaredLoc(),
rhs->getDeclaredLoc())
? Ordering::Before
: Ordering::After;
}
// If one of the conformances comes from the same file as the type
// declaration, pick that one; this is so that conformance synthesis works if
// there's any implied conformance in the same file as the type.
auto NTD = lhs->getDeclContext()->getSelfNominalTypeDecl();
auto typeSF = NTD->getParentSourceFile();
if (typeSF) {
if (typeSF == lhsSF)
return Ordering::Before;
if (typeSF == rhsSF)
return Ordering::After;
}
// Otherwise, pick the earlier file unit.
auto lhsFileUnit
= dyn_cast<FileUnit>(lhs->getDeclContext()->getModuleScopeContext());
auto rhsFileUnit
= dyn_cast<FileUnit>(rhs->getDeclContext()->getModuleScopeContext());
assert(lhsFileUnit && rhsFileUnit && "Not from a file unit?");
if (lhsFileUnit == rhsFileUnit) {
// If the file units are the same, just pick arbitrarily; we're not
// actually emitting anything.
// FIXME: Only because we're synthesizing conformances for deserialized
// protocols. Once that's no longer true (because we're serializing
// everything appropriately in the module), we should assert that this
// does not happen.
assert(!lhsSF && !rhsSF && "Source files shouldn't conflict");
return Ordering::Before;
}
auto module = lhs->getDeclContext()->getParentModule();
assert(lhs->getDeclContext()->getParentModule()
== rhs->getDeclContext()->getParentModule() &&
"conformances should be in the same module");
for (auto file : module->getFiles()) {
if (file == lhsFileUnit)
return Ordering::Before;
if (file == rhsFileUnit)
return Ordering::After;
}
llvm_unreachable("files weren't in the parent module?");
}
bool ConformanceLookupTable::resolveConformances(ProtocolDecl *protocol) {
// Find any entries that are superseded by other entries.
ConformanceEntries &entries = Conformances[protocol];
llvm::SmallPtrSet<DeclContext *, 4> knownConformances;
bool anySuperseded = false;
for (auto entry : entries) {
// If this entry has a conformance associated with it, note that.
if (entry->getConformance())
knownConformances.insert(entry->getDeclContext());
// If this entry was superseded, move on.
if (entry->isSuperseded()) {
anySuperseded = true;
continue;
}
// Determine whether this entry is superseded by (or supersedes)
// some other entry.
for (auto otherEntry : entries) {
if (entry == otherEntry)
continue;
if (otherEntry->isSuperseded()) {
anySuperseded = true;
continue;
}
bool diagnoseSuperseded = false;
bool doneWithEntry = false;
switch (compareConformances(entry, otherEntry, diagnoseSuperseded)) {
case Ordering::Equivalent:
break;
case Ordering::Before:
otherEntry->markSupersededBy(*this, entry, diagnoseSuperseded);
anySuperseded = true;
break;
case Ordering::After:
entry->markSupersededBy(*this, otherEntry, diagnoseSuperseded);
anySuperseded = true;
doneWithEntry = true;
break;
}
if (doneWithEntry)
break;
}
}
// If any entries were superseded, remove them now.
if (anySuperseded) {
entries.erase(std::remove_if(entries.begin(), entries.end(),
[&](ConformanceEntry *entry) {
return entry->isSuperseded();
}),
entries.end());
}
return anySuperseded;
}
DeclContext *ConformanceLookupTable::getConformingContext(
NominalTypeDecl *nominal,
ConformanceEntry *entry) {
ProtocolDecl *protocol = entry->getProtocol();
// Dig through the inherited entries to find a non-inherited one.
// Handle recursive inheritance.
SmallPtrSet<ClassDecl *, 4> visited;
while (entry->getKind() == ConformanceEntryKind::Inherited) {
// Make sure we have an up-to-date conformance table for the
// superclass.
auto classDecl = cast<ClassDecl>(nominal);
if (!visited.insert(classDecl).second)
return nullptr;
// If we had a circular dependency, the superclass may not exist.
auto superclassDecl
= classDecl->getSuperclassDecl();
if (!superclassDecl)
return nullptr;
if (!classDecl->ConformanceTable->VisitingSuperclass) {
llvm::SaveAndRestore<bool> visiting(
classDecl->ConformanceTable
->VisitingSuperclass,
true);
superclassDecl->prepareConformanceTable();
superclassDecl->ConformanceTable->resolveConformances(protocol);
}
// Grab the superclass entry and continue searching for a
// non-inherited conformance.
// FIXME: Ambiguity detection and resolution.
const auto &superclassConformances =
superclassDecl->ConformanceTable->Conformances[protocol];
if (superclassConformances.empty()) {
assert(protocol->isSpecificProtocol(KnownProtocolKind::Sendable) ||
protocol->isSpecificProtocol(KnownProtocolKind::Copyable));
// Go dig for a superclass that does conform to Sendable.
// FIXME: This is a hack because the inherited conformances aren't
// getting updated properly.
Type classTy = nominal->getDeclaredInterfaceType();
ModuleDecl *module = nominal->getParentModule();
do {
Type superclassTy = classTy->getSuperclassForDecl(superclassDecl);
if (superclassTy->is<ErrorType>())
return nullptr;
auto inheritedConformance = module->lookupConformance(
superclassTy, protocol, /*allowMissing=*/false);
if (inheritedConformance.hasUnavailableConformance())
inheritedConformance = ProtocolConformanceRef::forInvalid();
if (inheritedConformance)
return superclassDecl;
} while ((superclassDecl = superclassDecl->getSuperclassDecl()));
return nullptr;
}
entry = superclassConformances.front();
nominal = superclassDecl;
}
return entry->getDeclContext();
}
ProtocolConformance *
ConformanceLookupTable::getConformance(NominalTypeDecl *nominal,
ConformanceEntry *entry) {
// If we already have a conformance, we're done.
if (auto conformance = entry->getConformance())
return conformance;
ProtocolDecl *protocol = entry->getProtocol();
// Determine where the explicit conformance actually lives.
// FIXME: This is a hack to ensure that inherited conformances are
// always "single step", which is bad for resilience but is assumed
// elsewhere in the compiler.
DeclContext *conformingDC = getConformingContext(nominal, entry);
if (!conformingDC)
return nullptr;
// Never produce a conformance for a pre-macro-expansion conformance. They
// are placeholders that will be superseded.
if (entry->getKind() == ConformanceEntryKind::PreMacroExpansion) {
if (auto supersedingEntry = entry->SupersededBy) {
return getConformance(nominal, supersedingEntry);
}
return nullptr;
}
auto *conformingNominal = conformingDC->getSelfNominalTypeDecl();
// Form the conformance.
Type type = entry->getDeclContext()->getDeclaredInterfaceType();
ASTContext &ctx = nominal->getASTContext();
if (protocol->getInvertibleProtocolKind() &&
entry->getDeclContext() == nominal &&
(entry->getKind() == ConformanceEntryKind::Synthesized ||
entry->getKind() == ConformanceEntryKind::Inherited)) {
// Unconditional conformances to Copyable and Escapable are represented as
// builtin conformances, which do not need to store a substitution map.
//
// This avoids an exponential blowup when constructing the context
// substitution map for a type like G<G<G<G<...>>>>.
Type conformingType = nominal->getSelfInterfaceType();
entry->Conformance = ctx.getBuiltinConformance(
conformingType, protocol, BuiltinConformanceKind::Synthesized);
} else if (entry->getKind() == ConformanceEntryKind::Inherited) {
// For an inherited conformance, the conforming nominal type will
// be different from the nominal type.
assert(conformingNominal != nominal && "Broken inherited conformance");
// Find the superclass type that matches where the conformance was
// declared.
auto *conformingClass = cast<ClassDecl>(conformingNominal);
Type superclassTy = type->getSuperclassForDecl(conformingClass);
if (superclassTy->is<ErrorType>())
return nullptr;
// Look up the inherited conformance.
ModuleDecl *module = entry->getDeclContext()->getParentModule();
auto inheritedConformance = module->lookupConformance(
superclassTy, protocol, /*allowMissing=*/true);
// Form the inherited conformance.
entry->Conformance =
ctx.getInheritedConformance(type, inheritedConformance.getConcrete());
} else {
// Protocols don't have conformance lookup tables. Self-conformance is
// handled directly in lookupConformance().
assert(!isa<ProtocolDecl>(conformingNominal));
assert(!isa<ProtocolDecl>(conformingDC->getSelfNominalTypeDecl()));
Type conformingType = conformingDC->getSelfInterfaceType();
SourceLoc conformanceLoc =
entry->getLoc().isValid() ? entry->getLoc()
: (conformingNominal == conformingDC
? conformingNominal->getLoc()
: cast<ExtensionDecl>(conformingDC)->getLoc());
NormalProtocolConformance *implyingConf = nullptr;
if (entry->Source.getKind() == ConformanceEntryKind::Implied) {
auto implyingEntry = entry->Source.getImpliedSource();
auto origImplyingConf = getConformance(conformingNominal, implyingEntry);
if (!origImplyingConf)
return nullptr;
implyingConf = origImplyingConf->getRootNormalConformance();
}
// Create or find the normal conformance.
auto normalConf = ctx.getNormalConformance(
conformingType, protocol, conformanceLoc, conformingDC,
ProtocolConformanceState::Incomplete,
entry->Source.getUncheckedLoc().isValid(),
entry->Source.getPreconcurrencyLoc().isValid(),
entry->Source.getPreconcurrencyLoc());
// Invalid code may cause the getConformance call below to loop, so break
// the infinite recursion by setting this eagerly to shortcircuit with the
// early return at the start of this function.
entry->Conformance = normalConf;
normalConf->setSourceKindAndImplyingConformance(entry->Source.getKind(),
implyingConf);
// If the conformance was synthesized by the ClangImporter, give it a
// lazy loader that will be used to populate the conformance.
// First, if this is a conformance to a base protocol of a derived
// protocol, find the most derived protocol.
auto *impliedEntry = entry;
while (impliedEntry->getKind() == ConformanceEntryKind::Implied)
impliedEntry = impliedEntry->Source.getImpliedSource();
// Check if this was a synthesized conformance.
if (impliedEntry->getKind() == ConformanceEntryKind::Synthesized) {
auto *impliedProto = impliedEntry->getProtocol();
// Find a SynthesizedProtocolAttr corresponding to the protocol.
for (auto attr : conformingNominal->getAttrs()
.getAttributes<SynthesizedProtocolAttr>()) {
auto otherProto = attr->getProtocol();
if (otherProto == impliedProto) {
// Set the conformance loader to the loader stashed inside
// the attribute.
normalConf->setLazyLoader(attr->getLazyLoader(), /*context=*/0);
if (attr->isUnchecked())
normalConf->setUnchecked();
break;
}
}
}
}
return entry->Conformance.get<ProtocolConformance *>();
}
void ConformanceLookupTable::addSynthesizedConformance(
NominalTypeDecl *nominal, ProtocolDecl *protocol,
DeclContext *conformanceDC) {
addProtocol(protocol, nominal->getLoc(),
ConformanceSource::forSynthesized(conformanceDC));
}
void ConformanceLookupTable::registerProtocolConformance(
DeclContext *dc, ProtocolConformance *conformance,
bool synthesized) {
auto protocol = conformance->getProtocol();
auto nominal = dc->getSelfNominalTypeDecl();
// If there is an entry to update, do so.
auto &dcConformances = AllConformances[dc];
for (auto entry : dcConformances) {
if (entry->getProtocol() == protocol) {
assert(!entry->getConformance() ||
entry->getConformance() == conformance &&
"Mismatched conformances");
entry->Conformance = conformance;
return;
}
}
// Otherwise, add a new entry.
auto inherited = dyn_cast<InheritedProtocolConformance>(conformance);
ConformanceSource source
= inherited ? ConformanceSource::forInherited(cast<ClassDecl>(nominal)) :
synthesized ? ConformanceSource::forSynthesized(dc) :
ConformanceSource::forExplicit(dc);
ASTContext &ctx = nominal->getASTContext();
ConformanceEntry *entry = new (ctx) ConformanceEntry(SourceLoc(),
protocol,
source);
entry->Conformance = conformance;
// Record that this type conforms to the given protocol.
Conformances[protocol].push_back(entry);
// Record this as a conformance within the given declaration
// context.
dcConformances.push_back(entry);
}
bool ConformanceLookupTable::lookupConformance(
NominalTypeDecl *nominal,
ProtocolDecl *protocol,
SmallVectorImpl<ProtocolConformance *> &conformances) {
// Update to record all explicit and inherited conformances.
updateLookupTable(nominal, ConformanceStage::Inherited);
// Look for conformances to this protocol.
auto known = Conformances.find(protocol);
if (known == Conformances.end()) {
// If we didn't find anything, expand implied conformances.
updateLookupTable(nominal, ConformanceStage::ExpandedImplied);
known = Conformances.find(protocol);
// We didn't find anything.
if (known == Conformances.end())
return false;
}
// Resolve the conformances for this protocol.
resolveConformances(protocol);
for (auto entry : Conformances[protocol]) {
if (auto conformance = getConformance(nominal, entry)) {
conformances.push_back(conformance);
}
}
return !conformances.empty();
}
void ConformanceLookupTable::lookupConformances(
NominalTypeDecl *nominal,
DeclContext *dc,
std::vector<ProtocolConformance *> *conformances,
SmallVectorImpl<ConformanceDiagnostic> *diagnostics) {
// We need to expand all implied conformances before we can find
// those conformances that pertain to this declaration context.
updateLookupTable(nominal, ConformanceStage::ExpandedImplied);
/// Resolve conformances for each of the protocols to which this
/// declaration may provide a conformance. Only some of these will
/// result in conformances that are attributed to this declaration
/// context.
auto &potentialConformances = AllConformances[dc];
for (const auto &potential : potentialConformances) {
resolveConformances(potential->getProtocol());
}
// Remove any superseded conformances from AllConformances.
potentialConformances.erase(
std::remove_if(potentialConformances.begin(),
potentialConformances.end(),
[&](ConformanceEntry *entry) {
if (entry->isSuperseded())
return true;
// Record the conformance.
if (conformances) {
if (auto conformance = getConformance(nominal, entry))
conformances->push_back(conformance);
}
return false;
}),
potentialConformances.end());
// Gather any diagnostics we've produced.
if (diagnostics) {
auto knownDiags = AllSupersededDiagnostics.find(dc);
if (knownDiags != AllSupersededDiagnostics.end()) {
for (const auto *entry : knownDiags->second) {
ConformanceEntry *supersededBy = entry->getSupersededBy();
diagnostics->push_back({entry->getProtocol(),
entry->getDeclaredLoc(),
entry->getKind(),
entry->getDeclaredConformance()->getProtocol(),
supersededBy->getDeclContext(),
supersededBy->getKind(),
supersededBy->getDeclaredConformance()
->getProtocol()});
}
// We have transferred these diagnostics; erase them.
AllSupersededDiagnostics.erase(knownDiags);
}
}
}
void ConformanceLookupTable::getAllProtocols(
NominalTypeDecl *nominal, SmallVectorImpl<ProtocolDecl *> &scratch,
bool sorted) {
// We need to expand all implied conformances to find the complete
// set of protocols to which this nominal type conforms.
updateLookupTable(nominal, ConformanceStage::ExpandedImplied);
// Gather all of the protocols.
for (const auto &conformance : Conformances) {
if (conformance.second.empty())
continue;
scratch.push_back(conformance.first);
}
if (sorted) {
llvm::array_pod_sort(scratch.begin(), scratch.end(), TypeDecl::compare);
}
}
int ConformanceLookupTable::compareProtocolConformances(
ProtocolConformance * const *lhsPtr,
ProtocolConformance * const *rhsPtr) {
ProtocolConformance *lhs = *lhsPtr;
ProtocolConformance *rhs = *rhsPtr;
// If the two conformances are normal conformances with locations,
// sort by location.
if (auto lhsNormal = dyn_cast<NormalProtocolConformance>(lhs)) {
if (auto rhsNormal = dyn_cast<NormalProtocolConformance>(rhs)) {
if (lhsNormal->getLoc().isValid() && rhsNormal->getLoc().isValid()) {
ASTContext &ctx = lhs->getDeclContext()->getASTContext();
unsigned lhsBuffer
= ctx.SourceMgr.findBufferContainingLoc(lhsNormal->getLoc());
unsigned rhsBuffer
= ctx.SourceMgr.findBufferContainingLoc(rhsNormal->getLoc());
// If the buffers are the same, use source location ordering.
if (lhsBuffer == rhsBuffer) {
return ctx.SourceMgr.isBeforeInBuffer(lhsNormal->getLoc(),
rhsNormal->getLoc());
}
// Otherwise, order by buffer identifier.
return ctx.SourceMgr.getIdentifierForBuffer(lhsBuffer)
.compare(ctx.SourceMgr.getIdentifierForBuffer(rhsBuffer));
}
}
}
// Otherwise, sort by protocol.
ProtocolDecl *lhsProto = lhs->getProtocol();
ProtocolDecl *rhsProto = rhs->getProtocol();
return TypeDecl::compare(lhsProto, rhsProto);
}
void ConformanceLookupTable::getAllConformances(
NominalTypeDecl *nominal,
bool sorted,
SmallVectorImpl<ProtocolConformance *> &scratch) {
// We need to expand and resolve all conformances to enumerate them.
updateLookupTable(nominal, ConformanceStage::Resolved);
// Gather all of the protocols.
for (const auto &conformance : AllConformances) {
for (auto entry : conformance.second) {
if (auto conformance = getConformance(nominal, entry))
scratch.push_back(conformance);
}
}
// If requested, sort the results.
if (sorted) {
llvm::array_pod_sort(scratch.begin(), scratch.end(),
&compareProtocolConformances);
}
}
void ConformanceLookupTable::getImplicitProtocols(
NominalTypeDecl *nominal,
SmallVectorImpl<ProtocolDecl *> &protocols) {
for (auto conformance : AllConformances[nominal]) {
if (conformance->getKind() == ConformanceEntryKind::Synthesized) {
protocols.push_back(conformance->getProtocol());
}
}
}
ArrayRef<ValueDecl *>
ConformanceLookupTable::getSatisfiedProtocolRequirementsForMember(
const ValueDecl *member,
NominalTypeDecl *nominal,
bool sorted) {
auto It = ConformingDeclMap.find(member);
if (It != ConformingDeclMap.end())
return It->second;
SmallVector<ProtocolConformance *, 4> result;
getAllConformances(nominal, sorted, result);
auto &reqs = ConformingDeclMap[member];
if (isa<TypeDecl>(member)) {
for (auto *conf : result) {
if (conf->isInvalid())
continue;
conf->forEachTypeWitness([&](const AssociatedTypeDecl *assoc,
Type type,
TypeDecl *typeDecl) -> bool {
if (typeDecl == member)
reqs.push_back(const_cast<AssociatedTypeDecl*>(assoc));
return false;
});
}
} else {
for (auto *conf : result) {
if (conf->isInvalid())
continue;
auto root = conf->getRootConformance();
root->forEachValueWitness([&](ValueDecl *req, Witness witness) {
if (witness.getDecl() == member)
reqs.push_back(req);
});
}
}
return reqs;
}
void ConformanceLookupTable::dump() const {
dump(llvm::errs());
}
void ConformanceLookupTable::dump(raw_ostream &os) const {
for (const auto &dcEntries : AllConformances) {
os << "Conformances in context:\n";
dcEntries.first->printContext(os);
for (auto entry : dcEntries.second) {
entry->dump(os);
}
}
}
|