Skip to content

Commit 26293c4

Browse files
committed
RequirementMachine: Factor out Symbol::withConcreteSubstitutions()
1 parent 99b0be2 commit 26293c4

File tree

4 files changed

+32
-55
lines changed

4 files changed

+32
-55
lines changed

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,7 @@ void RewritePathEvaluator::applyDecompose(const RewriteStep &step,
312312
}
313313

314314
// Build the new symbol with the new substitutions.
315-
auto newSymbol = (symbol.getKind() == Symbol::Kind::Superclass
316-
? Symbol::forSuperclass(symbol.getSuperclass(),
317-
substitutions, ctx)
318-
: Symbol::forConcreteType(symbol.getConcreteType(),
319-
substitutions, ctx));
320-
321-
// Update the term with the new symbol.
322-
term.back() = newSymbol;
315+
term.back() = symbol.withConcreteSubstitutions(substitutions, ctx);
323316

324317
// Pop the substitutions from the A stack.
325318
A.resize(A.size() - numSubstitutions);

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,33 +285,7 @@ void RewriteSystem::simplifySubstitutions(MutableTerm &term,
285285
}
286286

287287
// Build the new symbol with simplified substitutions.
288-
switch (symbol.getKind()) {
289-
case Symbol::Kind::Superclass:
290-
term.back() = Symbol::forSuperclass(symbol.getSuperclass(),
291-
newSubstitutions, Context);
292-
return;
293-
294-
case Symbol::Kind::ConcreteType:
295-
term.back() = Symbol::forConcreteType(symbol.getConcreteType(),
296-
newSubstitutions, Context);
297-
return;
298-
299-
case Symbol::Kind::ConcreteConformance:
300-
term.back() = Symbol::forConcreteConformance(symbol.getConcreteType(),
301-
newSubstitutions,
302-
symbol.getProtocol(),
303-
Context);
304-
return;
305-
306-
case Symbol::Kind::Protocol:
307-
case Symbol::Kind::Name:
308-
case Symbol::Kind::AssociatedType:
309-
case Symbol::Kind::GenericParam:
310-
case Symbol::Kind::Layout:
311-
break;
312-
}
313-
314-
llvm_unreachable("Bad symbol kind");
288+
term.back() = symbol.withConcreteSubstitutions(newSubstitutions, Context);
315289
}
316290

317291
/// Adds a rewrite rule, returning true if the new rule was non-trivial.

lib/AST/RequirementMachine/Symbol.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,31 @@ int Symbol::compare(Symbol other, RewriteContext &ctx) const {
646646
return result;
647647
}
648648

649+
Symbol Symbol::withConcreteSubstitutions(
650+
ArrayRef<Term> substitutions,
651+
RewriteContext &ctx) const {
652+
switch (getKind()) {
653+
case Kind::Superclass:
654+
return Symbol::forSuperclass(getSuperclass(), substitutions, ctx);
655+
656+
case Kind::ConcreteType:
657+
return Symbol::forConcreteType(getConcreteType(), substitutions, ctx);
658+
659+
case Kind::ConcreteConformance:
660+
return Symbol::forConcreteConformance(getConcreteType(), substitutions,
661+
getProtocol(), ctx);
662+
663+
case Kind::GenericParam:
664+
case Kind::Name:
665+
case Kind::Protocol:
666+
case Kind::AssociatedType:
667+
case Kind::Layout:
668+
break;
669+
}
670+
671+
llvm_unreachable("Bad symbol kind");
672+
}
673+
649674
/// For a superclass or concrete type symbol
650675
///
651676
/// [concrete: Foo<X1, ..., Xn>]
@@ -679,26 +704,7 @@ Symbol Symbol::transformConcreteSubstitutions(
679704
if (!anyChanged)
680705
return *this;
681706

682-
switch (getKind()) {
683-
case Kind::Superclass:
684-
return Symbol::forSuperclass(getSuperclass(), substitutions, ctx);
685-
686-
case Kind::ConcreteType:
687-
return Symbol::forConcreteType(getConcreteType(), substitutions, ctx);
688-
689-
case Kind::ConcreteConformance:
690-
return Symbol::forConcreteConformance(getConcreteType(), substitutions,
691-
getProtocol(), ctx);
692-
693-
case Kind::GenericParam:
694-
case Kind::Name:
695-
case Kind::Protocol:
696-
case Kind::AssociatedType:
697-
case Kind::Layout:
698-
break;
699-
}
700-
701-
llvm_unreachable("Bad symbol kind");
707+
return withConcreteSubstitutions(substitutions, ctx);
702708
}
703709

704710
/// Print the symbol using our mnemonic representation.

lib/AST/RequirementMachine/Symbol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ class Symbol final {
227227

228228
int compare(Symbol other, RewriteContext &ctx) const;
229229

230+
Symbol withConcreteSubstitutions(
231+
ArrayRef<Term> substitutions,
232+
RewriteContext &ctx) const;
233+
230234
Symbol transformConcreteSubstitutions(
231235
llvm::function_ref<Term(Term)> fn,
232236
RewriteContext &ctx) const;

0 commit comments

Comments
 (0)