|
33 | 33 | #include "swift/AST/TypeMatcher.h" |
34 | 34 | #include "swift/AST/TypeRepr.h" |
35 | 35 | #include "llvm/ADT/SmallVector.h" |
| 36 | +#include "RequirementMachine.h" |
36 | 37 | #include "RewriteContext.h" |
37 | 38 | #include "RewriteSystem.h" |
38 | 39 | #include "Symbol.h" |
@@ -1403,21 +1404,32 @@ void RuleBuilder::collectRulesFromReferencedProtocols() { |
1403 | 1404 | // if this is a rewrite system for a connected component of the protocol |
1404 | 1405 | // dependency graph, add rewrite rules for each referenced protocol not part |
1405 | 1406 | // of this connected component. |
| 1407 | + |
| 1408 | + // First, collect all unique requirement machines, one for each connected |
| 1409 | + // component of each referenced protocol. |
| 1410 | + llvm::DenseSet<RequirementMachine *> machines; |
| 1411 | + |
| 1412 | + // Now visit each subordinate requirement machine pull in its rules. |
1406 | 1413 | for (auto *proto : ProtocolsToImport) { |
| 1414 | + // This will trigger requirement signature computation for this protocol, |
| 1415 | + // if neccessary, which will cause us to re-enter into a new RuleBuilder |
| 1416 | + // instace under RuleBuilder::initWithProtocolWrittenRequirements(). |
1407 | 1417 | if (Dump) { |
1408 | | - llvm::dbgs() << "protocol " << proto->getName() << " {\n"; |
| 1418 | + llvm::dbgs() << "importing protocol " << proto->getName() << " {\n"; |
1409 | 1419 | } |
1410 | 1420 |
|
1411 | | - addPermanentProtocolRules(proto); |
1412 | | - |
1413 | | - auto reqs = proto->getRequirementSignature(); |
1414 | | - for (auto req : reqs.getRequirements()) |
1415 | | - addRequirement(req.getCanonical(), proto, /*requirementID=*/None); |
1416 | | - for (auto alias : reqs.getTypeAliases()) |
1417 | | - addTypeAlias(alias, proto); |
1418 | | - |
1419 | | - if (Dump) { |
1420 | | - llvm::dbgs() << "}\n"; |
| 1421 | + auto *machine = Context.getRequirementMachine(proto); |
| 1422 | + if (!machines.insert(machine).second) { |
| 1423 | + // We've already seen this connected component. |
| 1424 | + continue; |
1421 | 1425 | } |
| 1426 | + |
| 1427 | + // We grab the machine's local rules, not *all* of its rules, to avoid |
| 1428 | + // duplicates in case multiple machines share a dependency on a downstream |
| 1429 | + // protocol connected component. |
| 1430 | + auto localRules = machine->getLocalRules(); |
| 1431 | + ImportedRules.insert(ImportedRules.end(), |
| 1432 | + localRules.begin(), |
| 1433 | + localRules.end()); |
1422 | 1434 | } |
1423 | 1435 | } |
0 commit comments