Skip to content

Commit ae53c8e

Browse files
committed
Sema: Split up SolverTrail::Change::BoundTypeVariable
1 parent 4548880 commit ae53c8e

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ class SolverTrail {
3737

3838
/// The kind of change made to the graph.
3939
enum class ChangeKind {
40-
/// Added a type variable to the constraint graph.
40+
/// Added a new vertex to the constraint graph.
4141
AddedTypeVariable,
4242
/// Added a new constraint to the constraint graph.
4343
AddedConstraint,
4444
/// Removed an existing constraint from the constraint graph.
4545
RemovedConstraint,
4646
/// Extended the equivalence class of a type variable in the constraint graph.
4747
ExtendedEquivalenceClass,
48-
/// Added a fixed binding for a type variable in the constraint graph.
49-
BoundTypeVariable,
48+
/// Added a new edge in the constraint graph.
49+
RelatedTypeVariables,
5050
/// Introduced a type variable's fixed type to inference.
5151
IntroducedToInference,
5252
/// Set the fixed type or parent and flags for a type variable.
@@ -74,6 +74,14 @@ class SolverTrail {
7474
unsigned PrevSize;
7575
} EquivClass;
7676

77+
struct {
78+
/// The first type variable.
79+
TypeVariableType *TypeVar;
80+
81+
/// The second type variable.
82+
TypeVariableType *OtherTypeVar;
83+
} Relation;
84+
7785
struct {
7886
/// The type variable being bound to a fixed type.
7987
TypeVariableType *TypeVar;
@@ -109,8 +117,10 @@ class SolverTrail {
109117
static Change extendedEquivalenceClass(TypeVariableType *typeVar,
110118
unsigned prevSize);
111119

112-
/// Create a change that bound a type variable to a fixed type.
113-
static Change boundTypeVariable(TypeVariableType *typeVar, Type fixed);
120+
/// Create a change that updated the references/referenced by sets of
121+
/// a type variable pair.
122+
static Change relatedTypeVariables(TypeVariableType *typeVar,
123+
TypeVariableType *otherTypeVar);
114124

115125
/// Create a change that introduced a type variable to inference.
116126
static Change introducedToInference(TypeVariableType *typeVar, Type fixed);

include/swift/Sema/ConstraintGraph.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,12 @@ class ConstraintGraph {
424424
/// caution.
425425
void removeNode(TypeVariableType *typeVar);
426426

427-
/// Unbind the given type variable from the given fixed type.
427+
/// Remove an edge from the constraint graph.
428428
///
429429
/// Note that this change is not recorded and cannot be undone. Use with
430430
/// caution.
431-
void unbindTypeVariable(TypeVariableType *typeVar, Type fixedType);
431+
void unrelateTypeVariables(TypeVariableType *typeVar,
432+
TypeVariableType *otherTypeVar);
432433

433434
/// Retract the given type variable from inference.
434435
///

lib/Sema/CSTrail.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ SolverTrail::Change::extendedEquivalenceClass(TypeVariableType *typeVar,
7676
}
7777

7878
SolverTrail::Change
79-
SolverTrail::Change::boundTypeVariable(TypeVariableType *typeVar,
80-
Type fixed) {
79+
SolverTrail::Change::relatedTypeVariables(TypeVariableType *typeVar,
80+
TypeVariableType *otherTypeVar) {
8181
Change result;
82-
result.Kind = ChangeKind::BoundTypeVariable;
83-
result.Binding.TypeVar = typeVar;
84-
result.Binding.FixedType = fixed.getPointer();
82+
result.Kind = ChangeKind::RelatedTypeVariables;
83+
result.Relation.TypeVar = typeVar;
84+
result.Relation.OtherTypeVar = otherTypeVar;
8585
return result;
8686
}
8787

@@ -130,8 +130,8 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
130130
break;
131131
}
132132

133-
case ChangeKind::BoundTypeVariable:
134-
cg.unbindTypeVariable(Binding.TypeVar, Binding.FixedType);
133+
case ChangeKind::RelatedTypeVariables:
134+
cg.unrelateTypeVariables(Relation.TypeVar, Relation.OtherTypeVar);
135135
break;
136136

137137
case ChangeKind::IntroducedToInference:
@@ -179,11 +179,11 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
179179
break;
180180
}
181181

182-
case ChangeKind::BoundTypeVariable:
183-
out << "(bound type variable ";
184-
Binding.TypeVar->print(out, PO);
185-
out << " to fixed type ";
186-
Binding.FixedType->print(out, PO);
182+
case ChangeKind::RelatedTypeVariables:
183+
out << "(related type variable ";
184+
Relation.TypeVar->print(out, PO);
185+
out << " with ";
186+
Relation.OtherTypeVar->print(out, PO);
187187
out << ")\n";
188188
break;
189189

lib/Sema/ConstraintGraph.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -559,29 +559,24 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
559559

560560
otherNode.addReferencedBy(typeVar);
561561
node.addReferencedVar(otherTypeVar);
562-
}
563562

564-
// Record the change, if there are active scopes.
565-
if (CS.isRecordingChanges())
566-
CS.recordChange(SolverTrail::Change::boundTypeVariable(typeVar, fixed));
563+
// Record the change, if there are active scopes.
564+
if (CS.isRecordingChanges())
565+
CS.recordChange(SolverTrail::Change::relatedTypeVariables(typeVar, otherTypeVar));
566+
}
567567
}
568568

569569
void ConstraintGraph::introduceToInference(TypeVariableType *typeVar, Type fixed) {
570570
(*this)[typeVar].introduceToInference(fixed);
571571
}
572572

573-
void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed) {
573+
void ConstraintGraph::unrelateTypeVariables(TypeVariableType *typeVar,
574+
TypeVariableType *otherTypeVar) {
574575
auto &node = (*this)[typeVar];
576+
auto &otherNode = (*this)[otherTypeVar];
575577

576-
llvm::SmallPtrSet<TypeVariableType *, 4> referencedVars;
577-
fixed->getTypeVariables(referencedVars);
578-
579-
for (auto otherTypeVar : referencedVars) {
580-
auto &otherNode = (*this)[otherTypeVar];
581-
582-
otherNode.removeReferencedBy(typeVar);
583-
node.removeReference(otherTypeVar);
584-
}
578+
otherNode.removeReferencedBy(typeVar);
579+
node.removeReference(otherTypeVar);
585580
}
586581

587582
void ConstraintGraph::retractFromInference(TypeVariableType *typeVar, Type fixed) {

0 commit comments

Comments
 (0)