Skip to content

Commit b885c29

Browse files
committed
Sema: Split up SolverTrail::Change::AddedConstraint and ::RemovedConstraint
1 parent ae53c8e commit b885c29

File tree

4 files changed

+109
-59
lines changed

4 files changed

+109
-59
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ class SolverTrail {
6464

6565
union {
6666
TypeVariableType *TypeVar;
67-
Constraint *TheConstraint;
67+
68+
struct {
69+
/// The type variable we're adding or removing a constraint from.
70+
TypeVariableType *TypeVar;
71+
72+
/// The constraint.
73+
Constraint *Constraint;
74+
} TheConstraint;
6875

6976
struct {
7077
/// The type variable whose equivalence class was extended.
@@ -108,10 +115,10 @@ class SolverTrail {
108115
static Change addedTypeVariable(TypeVariableType *typeVar);
109116

110117
/// Create a change that added a constraint.
111-
static Change addedConstraint(Constraint *constraint);
118+
static Change addedConstraint(TypeVariableType *typeVar, Constraint *constraint);
112119

113120
/// Create a change that removed a constraint.
114-
static Change removedConstraint(Constraint *constraint);
121+
static Change removedConstraint(TypeVariableType *typeVar, Constraint *constraint);
115122

116123
/// Create a change that extended an equivalence class.
117124
static Change extendedEquivalenceClass(TypeVariableType *typeVar,

include/swift/Sema/ConstraintGraph.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,15 @@ class ConstraintGraph {
259259
/// Add a new constraint to the graph.
260260
void addConstraint(Constraint *constraint);
261261

262+
/// Primitive form for SolverTrail::Change::undo().
263+
void addConstraint(TypeVariableType *typeVar, Constraint *constraint);
264+
262265
/// Remove a constraint from the graph.
263266
void removeConstraint(Constraint *constraint);
264267

268+
/// Primitive form for SolverTrail::Change::undo().
269+
void removeConstraint(TypeVariableType *typeVar, Constraint *constraint);
270+
265271
/// Merge the two nodes for the two given type variables.
266272
///
267273
/// The type variables must actually have been merged already; this
@@ -416,25 +422,23 @@ class ConstraintGraph {
416422
private:
417423
/// Remove the node corresponding to the given type variable.
418424
///
419-
/// This operation assumes that the any constraints that refer to
420-
/// this type variable have been or will be removed before other
421-
/// graph queries are performed.
425+
/// This operation assumes that the any constraints that refer to this type
426+
/// variable have been or will be removed before other graph queries are
427+
/// performed.
422428
///
423-
/// Note that this change is not recorded and cannot be undone. Use with
424-
/// caution.
429+
/// Note that this it only meant to be called by SolverTrail::Change::undo().
425430
void removeNode(TypeVariableType *typeVar);
426431

427432
/// Remove an edge from the constraint graph.
428433
///
429-
/// Note that this change is not recorded and cannot be undone. Use with
430-
/// caution.
434+
///
435+
/// Note that this it only meant to be called by SolverTrail::Change::undo().
431436
void unrelateTypeVariables(TypeVariableType *typeVar,
432437
TypeVariableType *otherTypeVar);
433438

434439
/// Retract the given type variable from inference.
435440
///
436-
/// Note that this change is not recorded and cannot be undone. Use with
437-
/// caution.
441+
/// Note that this it only meant to be called by SolverTrail::Change::undo().
438442
void retractFromInference(TypeVariableType *typeVar, Type fixedType);
439443

440444
/// Perform edge contraction on the constraint graph, merging equivalence

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ SolverTrail::Change::addedTypeVariable(TypeVariableType *typeVar) {
5050
}
5151

5252
SolverTrail::Change
53-
SolverTrail::Change::addedConstraint(Constraint *constraint) {
53+
SolverTrail::Change::addedConstraint(TypeVariableType *typeVar,
54+
Constraint *constraint) {
5455
Change result;
5556
result.Kind = ChangeKind::AddedConstraint;
56-
result.TheConstraint = constraint;
57+
result.TheConstraint.TypeVar = typeVar;
58+
result.TheConstraint.Constraint = constraint;
5759
return result;
5860
}
5961

6062
SolverTrail::Change
61-
SolverTrail::Change::removedConstraint(Constraint *constraint) {
63+
SolverTrail::Change::removedConstraint(TypeVariableType *typeVar,
64+
Constraint *constraint) {
6265
Change result;
6366
result.Kind = ChangeKind::RemovedConstraint;
64-
result.TheConstraint = constraint;
67+
result.TheConstraint.TypeVar = typeVar;
68+
result.TheConstraint.Constraint = constraint;
6569
return result;
6670
}
6771

@@ -117,11 +121,11 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
117121
break;
118122

119123
case ChangeKind::AddedConstraint:
120-
cg.removeConstraint(TheConstraint);
124+
cg.removeConstraint(TheConstraint.TypeVar, TheConstraint.Constraint);
121125
break;
122126

123127
case ChangeKind::RemovedConstraint:
124-
cg.addConstraint(TheConstraint);
128+
cg.addConstraint(TheConstraint.TypeVar, TheConstraint.Constraint);
125129
break;
126130

127131
case ChangeKind::ExtendedEquivalenceClass: {
@@ -162,13 +166,19 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
162166

163167
case ChangeKind::AddedConstraint:
164168
out << "(added constraint ";
165-
TheConstraint->print(out, &cs.getASTContext().SourceMgr, indent + 2);
169+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
170+
indent + 2);
171+
out << " to type variable ";
172+
TheConstraint.TypeVar->print(out, PO);
166173
out << ")\n";
167174
break;
168175

169176
case ChangeKind::RemovedConstraint:
170177
out << "(removed constraint ";
171-
TheConstraint->print(out, &cs.getASTContext().SourceMgr, indent + 2);
178+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
179+
indent + 2);
180+
out << " from type variable ";
181+
TheConstraint.TypeVar->print(out, PO);
172182
out << ")\n";
173183
break;
174184

lib/Sema/ConstraintGraph.cpp

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,6 @@ void ConstraintGraphNode::addConstraint(Constraint *constraint) {
155155
assert(ConstraintIndex.count(constraint) == 0 && "Constraint re-insertion");
156156
ConstraintIndex[constraint] = Constraints.size();
157157
Constraints.push_back(constraint);
158-
159-
{
160-
introduceToInference(constraint);
161-
162-
if (isUsefulForReferencedVars(constraint)) {
163-
notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
164-
referencedVar.introduceToInference(constraint);
165-
});
166-
}
167-
}
168158
}
169159

170160
void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
@@ -176,16 +166,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
176166
ConstraintIndex.erase(pos);
177167
assert(Constraints[index] == constraint && "Mismatched constraint");
178168

179-
{
180-
retractFromInference(constraint);
181-
182-
if (isUsefulForReferencedVars(constraint)) {
183-
notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
184-
referencedVar.retractFromInference(constraint);
185-
});
186-
}
187-
}
188-
189169
// If this is the last constraint, just pop it off the list and we're done.
190170
unsigned lastIndex = Constraints.size()-1;
191171
if (index == lastIndex) {
@@ -471,48 +451,97 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
471451
// For the nodes corresponding to each type variable...
472452
auto referencedTypeVars = constraint->getTypeVariables();
473453
for (auto typeVar : referencedTypeVars) {
454+
// Record the change, if there are active scopes.
455+
if (CS.isRecordingChanges())
456+
CS.recordChange(SolverTrail::Change::addedConstraint(typeVar, constraint));
457+
458+
addConstraint(typeVar, constraint);
459+
}
460+
461+
// If the constraint doesn't reference any type variables, it's orphaned;
462+
// track it as such.
463+
if (referencedTypeVars.empty()) {
464+
// Record the change, if there are active scopes.
465+
if (CS.isRecordingChanges())
466+
CS.recordChange(SolverTrail::Change::addedConstraint(nullptr, constraint));
467+
468+
addConstraint(nullptr, constraint);
469+
}
470+
}
471+
472+
void ConstraintGraph::addConstraint(TypeVariableType *typeVar,
473+
Constraint *constraint) {
474+
if (typeVar) {
474475
// Find the node for this type variable.
475476
auto &node = (*this)[typeVar];
476477

477478
// Note the constraint within the node for that type variable.
478479
node.addConstraint(constraint);
480+
481+
node.introduceToInference(constraint);
482+
483+
if (isUsefulForReferencedVars(constraint)) {
484+
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
485+
referencedVar.introduceToInference(constraint);
486+
});
487+
}
488+
489+
return;
479490
}
480491

481492
// If the constraint doesn't reference any type variables, it's orphaned;
482493
// track it as such.
483-
if (referencedTypeVars.empty()) {
484-
OrphanedConstraints.push_back(constraint);
485-
}
486-
487-
// Record the change, if there are active scopes.
488-
if (CS.isRecordingChanges())
489-
CS.recordChange(SolverTrail::Change::addedConstraint(constraint));
494+
OrphanedConstraints.push_back(constraint);
490495
}
491496

492497
void ConstraintGraph::removeConstraint(Constraint *constraint) {
493498
// For the nodes corresponding to each type variable...
494499
auto referencedTypeVars = constraint->getTypeVariables();
495500
for (auto typeVar : referencedTypeVars) {
501+
// Record the change, if there are active scopes.
502+
if (CS.isRecordingChanges())
503+
CS.recordChange(SolverTrail::Change::removedConstraint(typeVar, constraint));
504+
505+
removeConstraint(typeVar, constraint);
506+
}
507+
508+
// If this is an orphaned constraint, remove it from the list.
509+
if (referencedTypeVars.empty()) {
510+
// Record the change, if there are active scopes.
511+
if (CS.isRecordingChanges())
512+
CS.recordChange(SolverTrail::Change::removedConstraint(nullptr, constraint));
513+
514+
removeConstraint(nullptr, constraint);
515+
}
516+
}
517+
518+
void ConstraintGraph::removeConstraint(TypeVariableType *typeVar,
519+
Constraint *constraint) {
520+
if (typeVar) {
496521
// Find the node for this type variable.
497522
auto &node = (*this)[typeVar];
498523

524+
node.retractFromInference(constraint);
525+
526+
if (isUsefulForReferencedVars(constraint)) {
527+
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
528+
referencedVar.retractFromInference(constraint);
529+
});
530+
}
531+
499532
// Remove the constraint.
500533
node.removeConstraint(constraint);
501-
}
502534

503-
// If this is an orphaned constraint, remove it from the list.
504-
if (referencedTypeVars.empty()) {
505-
auto known = std::find(OrphanedConstraints.begin(),
506-
OrphanedConstraints.end(),
507-
constraint);
508-
assert(known != OrphanedConstraints.end() && "missing orphaned constraint");
509-
*known = OrphanedConstraints.back();
510-
OrphanedConstraints.pop_back();
535+
return;
511536
}
512537

513-
// Record the change, if there are active scopes.
514-
if (CS.isRecordingChanges())
515-
CS.recordChange(SolverTrail::Change::removedConstraint(constraint));
538+
// If this is an orphaned constraint, remove it from the list.
539+
auto known = std::find(OrphanedConstraints.begin(),
540+
OrphanedConstraints.end(),
541+
constraint);
542+
assert(known != OrphanedConstraints.end() && "missing orphaned constraint");
543+
*known = OrphanedConstraints.back();
544+
OrphanedConstraints.pop_back();
516545
}
517546

518547
void ConstraintGraph::mergeNodes(TypeVariableType *typeVar1,

0 commit comments

Comments
 (0)