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
|
//===--- RuleBuilder.cpp - Lowering desugared requirements to rules -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 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 lowering of desugared requirements to rewrite rules,
// as well as rule sharing, which imports confluent rewrite rules from a
// protocol connected component into a rewrite system which references that
// protocol.
//
//===----------------------------------------------------------------------===//
#include "RuleBuilder.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Requirement.h"
#include "swift/AST/RequirementSignature.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SetVector.h"
#include "RequirementMachine.h"
#include "RewriteContext.h"
#include "RewriteSystem.h"
#include "Symbol.h"
#include "Term.h"
using namespace swift;
using namespace rewriting;
/// For building a rewrite system for a generic signature from canonical
/// requirements.
void RuleBuilder::initWithGenericSignature(
ArrayRef<GenericTypeParamType *> genericParams,
ArrayRef<Requirement> requirements) {
assert(!Initialized);
Initialized = 1;
// Collect all protocols transitively referenced from these requirements.
for (auto req : requirements) {
if (req.getKind() == RequirementKind::Conformance) {
addReferencedProtocol(req.getProtocolDecl());
}
}
collectRulesFromReferencedProtocols();
collectPackShapeRules(genericParams);
// Add rewrite rules for all top-level requirements.
for (const auto &req : requirements)
addRequirement(req, /*proto=*/nullptr);
}
/// For building a rewrite system for a generic signature from user-written
/// requirements.
void RuleBuilder::initWithWrittenRequirements(
ArrayRef<GenericTypeParamType *> genericParams,
ArrayRef<StructuralRequirement> requirements) {
assert(!Initialized);
Initialized = 1;
// Collect all protocols transitively referenced from these requirements.
for (auto req : requirements) {
if (req.req.getKind() == RequirementKind::Conformance) {
addReferencedProtocol(req.req.getProtocolDecl());
}
}
collectRulesFromReferencedProtocols();
collectPackShapeRules(genericParams);
// Add rewrite rules for all top-level requirements.
for (const auto &req : requirements)
addRequirement(req, /*proto=*/nullptr);
}
/// For building a rewrite system for a protocol connected component from
/// a previously-built requirement signature.
///
/// Will trigger requirement signature computation if we haven't built
/// requirement signatures for this connected component yet, in which case we
/// will recursively end up building another rewrite system for this component
/// using initWithProtocolWrittenRequirements().
void RuleBuilder::initWithProtocolSignatureRequirements(
ArrayRef<const ProtocolDecl *> protos) {
assert(!Initialized);
Initialized = 1;
// Add all protocols to the referenced set, so that subsequent calls
// to addReferencedProtocol() with one of these protocols don't add
// them to the import list.
for (auto *proto : protos) {
ReferencedProtocols.insert(proto);
}
for (auto *proto : protos) {
if (Dump) {
llvm::dbgs() << "protocol " << proto->getName() << " {\n";
}
addPermanentProtocolRules(proto);
auto reqs = proto->getRequirementSignature();
// If completion failed, we'll have a totally empty requirement signature,
// but to maintain invariants around what constitutes a valid rewrite term
// between getTypeForTerm() and isValidTypeParameter(), we need to add rules
// for inherited protocols.
if (reqs.getErrors().contains(GenericSignatureErrorFlags::CompletionFailed)) {
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
Requirement req(RequirementKind::Conformance,
proto->getSelfInterfaceType(),
inheritedProto->getDeclaredInterfaceType());
addRequirement(req.getCanonical(), proto);
}
}
for (auto req : reqs.getRequirements())
addRequirement(req.getCanonical(), proto);
for (auto alias : reqs.getTypeAliases())
addTypeAlias(alias, proto);
for (auto *otherProto : proto->getProtocolDependencies())
addReferencedProtocol(otherProto);
if (Dump) {
llvm::dbgs() << "}\n";
}
}
// Collect all protocols transitively referenced from this connected component
// of the protocol dependency graph.
collectRulesFromReferencedProtocols();
}
/// For building a rewrite system for a protocol connected component from
/// user-written requirements. Used when actually building requirement
/// signatures.
void RuleBuilder::initWithProtocolWrittenRequirements(
ArrayRef<const ProtocolDecl *> component,
const llvm::DenseMap<const ProtocolDecl *,
SmallVector<StructuralRequirement, 4>> protos) {
assert(!Initialized);
Initialized = 1;
// Add all protocols to the referenced set, so that subsequent calls
// to addReferencedProtocol() with one of these protocols don't add
// them to the import list.
for (const auto *proto : component)
ReferencedProtocols.insert(proto);
for (const auto *proto : component) {
auto found = protos.find(proto);
assert(found != protos.end());
const auto &reqs = found->second;
if (Dump) {
llvm::dbgs() << "protocol " << proto->getName() << " {\n";
}
addPermanentProtocolRules(proto);
for (auto req : reqs)
addRequirement(req, proto);
for (auto *otherProto : proto->getProtocolDependencies())
addReferencedProtocol(otherProto);
if (Dump) {
llvm::dbgs() << "}\n";
}
}
// Collect all protocols transitively referenced from this connected component
// of the protocol dependency graph.
collectRulesFromReferencedProtocols();
}
/// For adding conditional conformance requirements to an existing rewrite
/// system. This might pull in additional protocols that we haven't seen
/// before.
///
/// The interface types in the requirements are converted to terms relative
/// to the given array of substitutions, using
/// RewriteContext::getRelativeTermForType().
///
/// For example, given a concrete conformance rule:
///
/// X.Y.[concrete: Array<X.Z> : Equatable]
///
/// The substitutions are {τ_0_0 := X.Z}, and the Array : Equatable conformance
/// has a conditional requirement 'τ_0_0 : Equatable', so the following
/// conformance rule will be added:
///
/// X.Z.[Equatable] => X.Z
void RuleBuilder::initWithConditionalRequirements(
ArrayRef<Requirement> requirements,
ArrayRef<Term> substitutions) {
assert(!Initialized);
Initialized = 1;
// Collect all protocols transitively referenced from these requirements.
for (auto req : requirements) {
if (req.getKind() == RequirementKind::Conformance) {
addReferencedProtocol(req.getProtocolDecl());
}
}
collectRulesFromReferencedProtocols();
// Add rewrite rules for all top-level requirements.
for (const auto &req : requirements)
addRequirement(req.getCanonical(), /*proto=*/nullptr, substitutions);
}
/// Add permanent rules for a protocol, consisting of:
///
/// - The identity conformance rule [P].[P] => [P].
/// - An associated type introduction rule for each associated type.
/// - An inherited associated type introduction rule for each associated
/// type of each inherited protocol.
void RuleBuilder::addPermanentProtocolRules(const ProtocolDecl *proto) {
MutableTerm lhs;
lhs.add(Symbol::forProtocol(proto, Context));
lhs.add(Symbol::forProtocol(proto, Context));
MutableTerm rhs;
rhs.add(Symbol::forProtocol(proto, Context));
PermanentRules.emplace_back(lhs, rhs);
for (auto *assocType : proto->getAssociatedTypeMembers())
addAssociatedType(assocType, proto);
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
for (auto *assocType : inheritedProto->getAssociatedTypeMembers())
addAssociatedType(assocType, proto);
}
}
/// For an associated type T in a protocol P, we add a rewrite rule:
///
/// [P].T => [P:T]
///
/// Intuitively, this means "if a type conforms to P, it has a nested type
/// named T".
void RuleBuilder::addAssociatedType(const AssociatedTypeDecl *type,
const ProtocolDecl *proto) {
MutableTerm lhs;
lhs.add(Symbol::forProtocol(proto, Context));
lhs.add(Symbol::forName(type->getName(), Context));
MutableTerm rhs;
rhs.add(Symbol::forAssociatedType(proto, type->getName(), Context));
PermanentRules.emplace_back(lhs, rhs);
}
/// Lowers a desugared generic requirement to a rewrite rule.
///
/// Convert a requirement to a rule and add it to the builder.
///
/// The types in the requirement must be canonical.
///
/// If \p proto is null and \p substitutions is None, this is a generic
/// requirement from the top-level generic signature. The added rewrite
/// rule will be rooted in a generic parameter symbol.
///
/// If \p proto is non-null, this is a generic requirement in the protocol's
/// requirement signature. The added rewrite rule will be rooted in a
/// protocol symbol.
///
/// If \p substitutions is not None, this is a conditional requirement
/// added by conditional requirement inference. The added rewrite rule
/// will be added in the corresponding term from the substitution array.
void RuleBuilder::addRequirement(const Requirement &req,
const ProtocolDecl *proto,
std::optional<ArrayRef<Term>> substitutions) {
if (Dump) {
llvm::dbgs() << "+ ";
req.dump(llvm::dbgs());
llvm::dbgs() << "\n";
}
assert(!substitutions.has_value() || proto == nullptr && "Can't have both");
// Compute the left hand side.
auto subjectType = CanType(req.getFirstType());
auto subjectTerm = (substitutions
? Context.getRelativeTermForType(
subjectType, *substitutions)
: Context.getMutableTermForType(
subjectType, proto));
// Compute the right hand side.
MutableTerm constraintTerm;
switch (req.getKind()) {
case RequirementKind::SameShape: {
// A same-shape requirement T.shape == U.shape
// becomes a rewrite rule:
//
// T.[shape] => U.[shape]
auto otherType = CanType(req.getSecondType());
assert(otherType->isParameterPack());
constraintTerm = (substitutions
? Context.getRelativeTermForType(
otherType, *substitutions)
: Context.getMutableTermForType(
otherType, proto));
// Add the [shape] symbol to both sides.
subjectTerm.add(Symbol::forShape(Context));
constraintTerm.add(Symbol::forShape(Context));
break;
}
case RequirementKind::Conformance: {
// A conformance requirement T : P becomes a rewrite rule
//
// T.[P] == T
//
// Intuitively, this means "any type ending with T conforms to P".
auto *proto = req.getProtocolDecl();
constraintTerm = subjectTerm;
constraintTerm.add(Symbol::forProtocol(proto, Context));
break;
}
case RequirementKind::Superclass: {
// A superclass requirement T : C<X, Y> becomes a rewrite rule
//
// T.[superclass: C<X, Y>] => T
auto otherType = CanType(req.getSecondType());
// Build the symbol [superclass: C<X, Y>].
SmallVector<Term, 1> result;
otherType = (substitutions
? Context.getRelativeSubstitutionSchemaFromType(
otherType, *substitutions, result)
: Context.getSubstitutionSchemaFromType(
otherType, proto, result));
auto superclassSymbol = Symbol::forSuperclass(otherType, result, Context);
// Build the term T.[superclass: C<X, Y>].
constraintTerm = subjectTerm;
constraintTerm.add(superclassSymbol);
break;
}
case RequirementKind::Layout: {
// A layout requirement T : L becomes a rewrite rule
//
// T.[layout: L] == T
constraintTerm = subjectTerm;
constraintTerm.add(Symbol::forLayout(req.getLayoutConstraint(), Context));
break;
}
case RequirementKind::SameType: {
auto otherType = CanType(req.getSecondType());
if (!otherType->isTypeParameter()) {
// A concrete same-type requirement T == C<X, Y> becomes a
// rewrite rule
//
// T.[concrete: C<X, Y>] => T
SmallVector<Term, 1> result;
otherType = (substitutions
? Context.getRelativeSubstitutionSchemaFromType(
otherType, *substitutions, result)
: Context.getSubstitutionSchemaFromType(
otherType, proto, result));
constraintTerm = subjectTerm;
constraintTerm.add(Symbol::forConcreteType(otherType, result, Context));
break;
}
constraintTerm = (substitutions
? Context.getRelativeTermForType(
otherType, *substitutions)
: Context.getMutableTermForType(
otherType, proto));
break;
}
}
RequirementRules.emplace_back(std::move(subjectTerm), std::move(constraintTerm));
}
void RuleBuilder::addRequirement(const StructuralRequirement &req,
const ProtocolDecl *proto) {
addRequirement(req.req.getCanonical(), proto, /*substitutions=*/std::nullopt);
}
/// Lowers a protocol typealias to a rewrite rule.
void RuleBuilder::addTypeAlias(const ProtocolTypeAlias &alias,
const ProtocolDecl *proto) {
// Build the term [P].T, where P is the protocol and T is a name symbol.
MutableTerm subjectTerm;
subjectTerm.add(Symbol::forProtocol(proto, Context));
subjectTerm.add(Symbol::forName(alias.getName(), Context));
auto constraintType = alias.getUnderlyingType()->getCanonicalType();
MutableTerm constraintTerm;
if (constraintType->isTypeParameter()) {
// If the underlying type of the typealias is a type parameter X, build
// a rule [P].T => X, where X,
constraintTerm = Context.getMutableTermForType(
constraintType, proto);
} else {
// If the underlying type of the typealias is a concrete type C, build
// a rule [P].T.[concrete: C] => [P].T.
constraintTerm = subjectTerm;
SmallVector<Term, 1> result;
auto concreteType =
Context.getSubstitutionSchemaFromType(
constraintType, proto, result);
constraintTerm.add(Symbol::forConcreteType(concreteType, result, Context));
}
RequirementRules.emplace_back(subjectTerm, constraintTerm);
}
/// If we haven't seen this protocol yet, save it for later so that we can
/// import the rewrite rules from its connected component.
void RuleBuilder::addReferencedProtocol(const ProtocolDecl *proto) {
if (ReferencedProtocols.insert(proto).second)
ProtocolsToImport.push_back(proto);
}
/// Compute the transitive closure of the set of all protocols referenced from
/// the right hand sides of conformance requirements, and import the rewrite
/// rules from the requirement machine for each protocol component.
void RuleBuilder::collectRulesFromReferencedProtocols() {
// Compute the transitive closure.
unsigned i = 0;
while (i < ProtocolsToImport.size()) {
auto *proto = ProtocolsToImport[i++];
for (auto *depProto : proto->getProtocolDependencies()) {
addReferencedProtocol(depProto);
}
}
// If this is a rewrite system for a generic signature, add rewrite rules for
// each referenced protocol.
//
// if this is a rewrite system for a connected component of the protocol
// dependency graph, add rewrite rules for each referenced protocol not part
// of this connected component.
llvm::DenseSet<RequirementMachine *> machines;
// Now visit each protocol component requirement machine and pull in its rules.
for (auto *proto : ProtocolsToImport) {
// This will trigger requirement signature computation for this protocol,
// if necessary, which will cause us to re-enter into a new RuleBuilder
// instance under RuleBuilder::initWithProtocolWrittenRequirements().
if (Dump) {
llvm::dbgs() << "importing protocol " << proto->getName() << "\n";
}
auto *machine = Context.getRequirementMachine(proto);
if (!machines.insert(machine).second) {
// We've already seen this protocol component.
continue;
}
// We grab the machine's local rules, not *all* of its rules, to avoid
// duplicates in case multiple machines share a dependency on a downstream
// protocol component.
auto localRules = machine->getLocalRules();
ImportedRules.insert(ImportedRules.end(),
localRules.begin(),
localRules.end());
}
}
void RuleBuilder::collectPackShapeRules(ArrayRef<GenericTypeParamType *> genericParams) {
if (Dump) {
llvm::dbgs() << "adding shape rules\n";
}
if (!llvm::any_of(genericParams,
[](GenericTypeParamType *t) {
return t->isParameterPack();
})) {
return;
}
// Each non-pack generic parameter is part of the "scalar shape class", represented
// by the empty term.
for (auto *genericParam : genericParams) {
if (genericParam->isParameterPack())
continue;
// Add the rule (τ_d_i.[shape] => [shape]).
MutableTerm lhs;
lhs.add(Symbol::forGenericParam(
cast<GenericTypeParamType>(genericParam->getCanonicalType()), Context));
lhs.add(Symbol::forShape(Context));
MutableTerm rhs;
rhs.add(Symbol::forShape(Context));
PermanentRules.emplace_back(lhs, rhs);
}
// A member type T.[P:A] is part of the same shape class as its base type T.
llvm::DenseSet<Symbol> visited;
auto addMemberShapeRule = [&](const ProtocolDecl *proto, AssociatedTypeDecl *assocType) {
auto symbol = Symbol::forAssociatedType(proto, assocType->getName(), Context);
if (!visited.insert(symbol).second)
return;
// Add the rule ([P:A].[shape] => [shape]).
MutableTerm lhs;
lhs.add(symbol);
lhs.add(Symbol::forShape(Context));
MutableTerm rhs;
rhs.add(Symbol::forShape(Context));
// Consider it an imported rule, since it is not part of our minimization
// domain. It would be more logical if we added these in the protocol component
// machine for this protocol, but instead we add them in the "leaf" generic
// signature machine. This avoids polluting machines that do not involve
// parameter packs with these extra rules, which would otherwise just slow
// things down.
Rule rule(Term::get(lhs, Context), Term::get(rhs, Context));
rule.markPermanent();
ImportedRules.push_back(rule);
};
for (auto *proto : ProtocolsToImport) {
if (Dump) {
llvm::dbgs() << "adding member shape rules for protocol " << proto->getName() << "\n";
}
for (auto *assocType : proto->getAssociatedTypeMembers()) {
addMemberShapeRule(proto, assocType);
}
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
for (auto *assocType : inheritedProto->getAssociatedTypeMembers()) {
addMemberShapeRule(proto, assocType);
}
}
}
}
|