Skip to content

Commit 7c00619

Browse files
committed
RequirementMachine: Fold getRuleForRequirement() into RuleBuilder::addRequirement()
1 parent c91c760 commit 7c00619

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,26 +1258,39 @@ void RuleBuilder::addAssociatedType(const AssociatedTypeDecl *type,
12581258

12591259
/// Lowers a desugared generic requirement to a rewrite rule.
12601260
///
1261-
/// If \p proto is null, this is a generic requirement from the top-level
1262-
/// generic signature. The added rewrite rule will be rooted in a generic
1263-
/// parameter symbol.
1261+
/// Convert a requirement to a rule and add it to the builder.
1262+
///
1263+
/// The types in the requirement must be canonical.
1264+
///
1265+
/// If \p proto is null and \p substitutions is None, this is a generic
1266+
/// requirement from the top-level generic signature. The added rewrite
1267+
/// rule will be rooted in a generic parameter symbol.
12641268
///
12651269
/// If \p proto is non-null, this is a generic requirement in the protocol's
12661270
/// requirement signature. The added rewrite rule will be rooted in a
12671271
/// protocol symbol.
1268-
std::pair<MutableTerm, MutableTerm>
1269-
swift::rewriting::getRuleForRequirement(const Requirement &req,
1270-
const ProtocolDecl *proto,
1271-
Optional<ArrayRef<Term>> substitutions,
1272-
RewriteContext &ctx) {
1272+
///
1273+
/// If \p substitutions is not None, this is a conditional requirement
1274+
/// added by conditional requirement inference. The added rewrite rule
1275+
/// will be added in the corresponding term from the substitution array.
1276+
void RuleBuilder::addRequirement(const Requirement &req,
1277+
const ProtocolDecl *proto,
1278+
Optional<ArrayRef<Term>> substitutions,
1279+
Optional<unsigned> requirementID) {
1280+
if (Dump) {
1281+
llvm::dbgs() << "+ ";
1282+
req.dump(llvm::dbgs());
1283+
llvm::dbgs() << "\n";
1284+
}
1285+
12731286
assert(!substitutions.hasValue() || proto == nullptr && "Can't have both");
12741287

12751288
// Compute the left hand side.
12761289
auto subjectType = CanType(req.getFirstType());
12771290
auto subjectTerm = (substitutions
1278-
? ctx.getRelativeTermForType(
1291+
? Context.getRelativeTermForType(
12791292
subjectType, *substitutions)
1280-
: ctx.getMutableTermForType(
1293+
: Context.getMutableTermForType(
12811294
subjectType, proto));
12821295

12831296
// Compute the right hand side.
@@ -1293,7 +1306,7 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
12931306
auto *proto = req.getProtocolDecl();
12941307

12951308
constraintTerm = subjectTerm;
1296-
constraintTerm.add(Symbol::forProtocol(proto, ctx));
1309+
constraintTerm.add(Symbol::forProtocol(proto, Context));
12971310
break;
12981311
}
12991312

@@ -1306,11 +1319,11 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
13061319
// Build the symbol [superclass: C<X, Y>].
13071320
SmallVector<Term, 1> result;
13081321
otherType = (substitutions
1309-
? ctx.getRelativeSubstitutionSchemaFromType(
1322+
? Context.getRelativeSubstitutionSchemaFromType(
13101323
otherType, *substitutions, result)
1311-
: ctx.getSubstitutionSchemaFromType(
1324+
: Context.getSubstitutionSchemaFromType(
13121325
otherType, proto, result));
1313-
auto superclassSymbol = Symbol::forSuperclass(otherType, result, ctx);
1326+
auto superclassSymbol = Symbol::forSuperclass(otherType, result, Context);
13141327

13151328
// Build the term T.[superclass: C<X, Y>].
13161329
constraintTerm = subjectTerm;
@@ -1323,7 +1336,7 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
13231336
//
13241337
// T.[layout: L] == T
13251338
constraintTerm = subjectTerm;
1326-
constraintTerm.add(Symbol::forLayout(req.getLayoutConstraint(), ctx));
1339+
constraintTerm.add(Symbol::forLayout(req.getLayoutConstraint(), Context));
13271340
break;
13281341
}
13291342

@@ -1337,49 +1350,28 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
13371350
// T.[concrete: C<X, Y>] => T
13381351
SmallVector<Term, 1> result;
13391352
otherType = (substitutions
1340-
? ctx.getRelativeSubstitutionSchemaFromType(
1353+
? Context.getRelativeSubstitutionSchemaFromType(
13411354
otherType, *substitutions, result)
1342-
: ctx.getSubstitutionSchemaFromType(
1355+
: Context.getSubstitutionSchemaFromType(
13431356
otherType, proto, result));
13441357

13451358
constraintTerm = subjectTerm;
1346-
constraintTerm.add(Symbol::forConcreteType(otherType, result, ctx));
1359+
constraintTerm.add(Symbol::forConcreteType(otherType, result, Context));
13471360
break;
13481361
}
13491362

13501363
constraintTerm = (substitutions
1351-
? ctx.getRelativeTermForType(
1364+
? Context.getRelativeTermForType(
13521365
otherType, *substitutions)
1353-
: ctx.getMutableTermForType(
1366+
: Context.getMutableTermForType(
13541367
otherType, proto));
13551368
break;
13561369
}
13571370
}
13581371

1359-
return std::make_pair(subjectTerm, constraintTerm);
1360-
}
1361-
1362-
/// Convert a requirement to a rule and add it to the builder.
1363-
///
1364-
/// The types in the requirement must be canonical.
1365-
///
1366-
/// If \p substitutions is not None, the interface types in the requirement
1367-
/// are converted to terms relative to these substitutions, using
1368-
/// RewriteContext::getRelativeTermForType().
1369-
void RuleBuilder::addRequirement(const Requirement &req,
1370-
const ProtocolDecl *proto,
1371-
Optional<ArrayRef<Term>> substitutions,
1372-
Optional<unsigned> requirementID) {
1373-
if (Dump) {
1374-
llvm::dbgs() << "+ ";
1375-
req.dump(llvm::dbgs());
1376-
llvm::dbgs() << "\n";
1377-
}
1378-
1379-
auto rule =
1380-
getRuleForRequirement(req, proto, substitutions, Context);
1381-
RequirementRules.push_back(
1382-
std::make_tuple(rule.first, rule.second, requirementID));
1372+
RequirementRules.emplace_back(
1373+
std::move(subjectTerm), std::move(constraintTerm),
1374+
requirementID);
13831375
}
13841376

13851377
void RuleBuilder::addRequirement(const StructuralRequirement &req,

lib/AST/RequirementMachine/RequirementLowering.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ bool diagnoseRequirementErrors(ASTContext &ctx,
6363
ArrayRef<RequirementError> errors,
6464
bool allowConcreteGenericParams);
6565

66-
std::pair<MutableTerm, MutableTerm>
67-
getRuleForRequirement(const Requirement &req,
68-
const ProtocolDecl *proto,
69-
Optional<ArrayRef<Term>> substitutions,
70-
RewriteContext &ctx);
71-
7266
/// A utility class for bulding rewrite rules from the top-level requirements
7367
/// of a generic signature.
7468
///

0 commit comments

Comments
 (0)