Skip to content

Commit 5c623eb

Browse files
committed
[ConstraintGraph] NFC: Clarify names of inference APIs and add comments
1 parent cb36fb7 commit 5c623eb

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,35 @@ class ConstraintGraphNode {
119119
/// Remove a type variable which used to reference this type variable.
120120
void removeReferencedBy(TypeVariableType *typeVar);
121121

122-
/// Experimental {
123-
void introduceToInference(Constraint *constraint, bool notifyFixedBindings);
124-
void retractFromInference(Constraint *constraint, bool notifyFixedBindings);
125-
void reintroduceToInference(Constraint *constraint, bool notifyFixedBindings);
122+
/// Binding Inference {
123+
124+
/// Infer bindings from the given constraint and notify referenced variables
125+
/// about its arrival (if requested). This happens every time a new constraint
126+
/// gets added to a constraint graph node.
127+
void introduceToInference(Constraint *constraint, bool notifyReferencedVars);
128+
129+
/// Forget about the given constraint. This happens every time a constraint
130+
/// gets removed for a constraint graph.
131+
void retractFromInference(Constraint *constraint, bool notifyReferencedVars);
132+
133+
/// Re-evaluate the given constraint. This happens when there are changes
134+
/// in associated type variables e.g. bound/unbound to/from a fixed type,
135+
/// equivalence class changes.
136+
void reintroduceToInference(Constraint *constraint, bool notifyReferencedVars);
137+
138+
/// Drop all previously collected bindings and re-infer based on the
139+
/// current set constraints associated with this equivalence class.
126140
void resetBindingSet();
127-
void updateAdjacentVars() const;
141+
142+
/// Notify all of the type variables that have this one (or any member of
143+
/// its equivalence class) referenced in their fixed type.
144+
///
145+
/// This is a traversal up the reference change which triggers constraint
146+
/// re-introduction to affected type variables.
147+
///
148+
/// This is useful in situations when type variable gets bound and unbound,
149+
/// or equivalence class changes.
150+
void notifyReferencingVars() const;
128151
/// }
129152

130153
/// The constraint graph this node belongs to.

lib/Sema/ConstraintGraph.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
138138
Constraints.pop_back();
139139
}
140140

141-
void ConstraintGraphNode::updateAdjacentVars() const {
141+
void ConstraintGraphNode::notifyReferencingVars() const {
142142
SmallVector<TypeVariableType *, 4> stack;
143143

144144
stack.push_back(TypeVar);
@@ -161,7 +161,7 @@ void ConstraintGraphNode::updateAdjacentVars() const {
161161

162162
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
163163
CG[repr].reintroduceToInference(constraint,
164-
/*notifyFixedBindings=*/false);
164+
/*notifyReferencedVars=*/false);
165165
}
166166
}
167167
};
@@ -210,10 +210,10 @@ void ConstraintGraphNode::addToEquivalenceClass(
210210
auto &node = CG[newMember];
211211

212212
for (auto *constraint : node.getConstraints()) {
213-
introduceToInference(constraint, /*notifyFixedBindings=*/true);
213+
introduceToInference(constraint, /*notifyReferencedVars=*/true);
214214
}
215215

216-
node.updateAdjacentVars();
216+
node.notifyReferencingVars();
217217
}
218218
}
219219
}
@@ -237,7 +237,7 @@ void ConstraintGraphNode::truncateEquivalenceClass(unsigned prevSize) {
237237

238238
// Re-infer bindings all all of the newly made representatives.
239239
for (auto *typeVar : disconnectedVars)
240-
CG[typeVar].updateAdjacentVars();
240+
CG[typeVar].notifyReferencingVars();
241241
}
242242
}
243243

@@ -288,51 +288,51 @@ static bool isUsefulForReferencedVars(Constraint *constraint) {
288288
}
289289

290290
void ConstraintGraphNode::introduceToInference(Constraint *constraint,
291-
bool notifyFixedBindings) {
291+
bool notifyReferencedVars) {
292292
if (forRepresentativeVar()) {
293293
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
294294
if (!fixedType || fixedType->isTypeVariableOrMember())
295295
getCurrentBindings().infer(constraint);
296296
} else {
297297
auto *repr =
298298
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
299-
CG[repr].introduceToInference(constraint, /*notifyFixedBindings=*/false);
299+
CG[repr].introduceToInference(constraint, /*notifyReferencedVars=*/false);
300300
}
301301

302-
if (!notifyFixedBindings || !isUsefulForReferencedVars(constraint))
302+
if (!notifyReferencedVars || !isUsefulForReferencedVars(constraint))
303303
return;
304304

305305
for (auto *fixedBinding : getReferencedVars()) {
306306
CG[fixedBinding].introduceToInference(constraint,
307-
/*notifyFixedBindings=*/false);
307+
/*notifyReferencedVars=*/false);
308308
}
309309
}
310310

311311
void ConstraintGraphNode::retractFromInference(Constraint *constraint,
312-
bool notifyFixedBindings) {
312+
bool notifyReferencedVars) {
313313
if (forRepresentativeVar()) {
314314
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
315315
if (!fixedType || fixedType->isTypeVariableOrMember())
316316
getCurrentBindings().retract(constraint);
317317
} else {
318318
auto *repr =
319319
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
320-
CG[repr].retractFromInference(constraint, /*notifyFixedBindings=*/false);
320+
CG[repr].retractFromInference(constraint, /*notifyReferencedVars=*/false);
321321
}
322322

323-
if (!notifyFixedBindings || !isUsefulForReferencedVars(constraint))
323+
if (!notifyReferencedVars || !isUsefulForReferencedVars(constraint))
324324
return;
325325

326326
for (auto *fixedBinding : getReferencedVars()) {
327327
CG[fixedBinding].retractFromInference(constraint,
328-
/*notifyFixedBindings=*/false);
328+
/*notifyReferencedVars=*/false);
329329
}
330330
}
331331

332332
void ConstraintGraphNode::reintroduceToInference(Constraint *constraint,
333-
bool notifyFixedBindings) {
334-
retractFromInference(constraint, notifyFixedBindings);
335-
introduceToInference(constraint, notifyFixedBindings);
333+
bool notifyReferencedVars) {
334+
retractFromInference(constraint, notifyReferencedVars);
335+
introduceToInference(constraint, notifyReferencedVars);
336336
}
337337

338338
void ConstraintGraphNode::resetBindingSet() {
@@ -557,8 +557,9 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
557557
// Newly referred vars need to re-introduce all constraints associated
558558
// with this type variable.
559559
for (auto *constraint : (*this)[typeVar].getConstraints()) {
560-
otherNode.reintroduceToInference(constraint,
561-
/*notifyFixedBindings=*/false);
560+
if (isUsefulForReferencedVars(constraint))
561+
otherNode.reintroduceToInference(constraint,
562+
/*notifyReferencedVars=*/false);
562563
}
563564
}
564565
}
@@ -568,7 +569,7 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
568569
// whole equivalence class of the current type variable and all of its
569570
// fixed bindings.
570571
{
571-
(*this)[typeVar].updateAdjacentVars();
572+
(*this)[typeVar].notifyReferencingVars();
572573
}
573574
}
574575

@@ -580,7 +581,7 @@ void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed)
580581
{
581582
auto &node = (*this)[typeVar];
582583

583-
node.updateAdjacentVars();
584+
node.notifyReferencingVars();
584585

585586
llvm::SmallPtrSet<TypeVariableType *, 4> typeVars;
586587

0 commit comments

Comments
 (0)