@@ -143,12 +143,28 @@ void ConstraintGraphNode::addToEquivalenceClass(
143
143
EquivalenceClass.append (typeVars.begin (), typeVars.end ());
144
144
}
145
145
146
- void ConstraintGraphNode::addFixedBinding (TypeVariableType *typeVar) {
147
- FixedBindings.push_back (typeVar);
146
+ void ConstraintGraphNode::addReferencedVar (TypeVariableType *typeVar) {
147
+ bool inserted = References.insert (typeVar);
148
+ assert (inserted && " Attempt to reference a duplicate type variable" );
149
+ (void )inserted;
148
150
}
149
151
150
- void ConstraintGraphNode::removeFixedBinding (TypeVariableType *typeVar) {
151
- FixedBindings.pop_back ();
152
+ void ConstraintGraphNode::addReferencedBy (TypeVariableType *typeVar) {
153
+ bool inserted = ReferencedBy.insert (typeVar);
154
+ assert (inserted && " Already referenced by the given type variable" );
155
+ (void )inserted;
156
+ }
157
+
158
+ void ConstraintGraphNode::removeReference (TypeVariableType *typeVar) {
159
+ auto removed = References.remove (typeVar);
160
+ assert (removed && " Variables are not connected" );
161
+ (void )removed;
162
+ }
163
+
164
+ void ConstraintGraphNode::removeReferencedBy (TypeVariableType *typeVar) {
165
+ auto removed = ReferencedBy.remove (typeVar);
166
+ assert (removed && " Variables are not connected" );
167
+ (void )removed;
152
168
}
153
169
154
170
#pragma mark Graph scope management
@@ -349,11 +365,10 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
349
365
fixed->getTypeVariables (typeVars);
350
366
auto &node = (*this )[typeVar];
351
367
for (auto otherTypeVar : typeVars) {
352
- if (typeVar == otherTypeVar)
353
- continue ;
368
+ if (typeVar == otherTypeVar) continue ;
354
369
355
- (*this )[otherTypeVar].addFixedBinding (typeVar);
356
- node.addFixedBinding (otherTypeVar);
370
+ (*this )[otherTypeVar].addReferencedBy (typeVar);
371
+ node.addReferencedVar (otherTypeVar);
357
372
}
358
373
359
374
// Record the change, if there are active scopes.
@@ -372,8 +387,8 @@ void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed){
372
387
fixed->getTypeVariables (typeVars);
373
388
auto &node = (*this )[typeVar];
374
389
for (auto otherTypeVar : typeVars) {
375
- (*this )[otherTypeVar].removeFixedBinding (typeVar);
376
- node.removeFixedBinding (otherTypeVar);
390
+ (*this )[otherTypeVar].removeReferencedBy (typeVar);
391
+ node.removeReference (otherTypeVar);
377
392
}
378
393
}
379
394
@@ -435,7 +450,8 @@ static void depthFirstSearch(
435
450
}
436
451
437
452
// Walk any type variables related via fixed bindings.
438
- visitAdjacencies (node.getFixedBindings ());
453
+ visitAdjacencies (node.getReferencedBy ());
454
+ visitAdjacencies (node.getReferencedVars ());
439
455
}
440
456
441
457
llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints (
@@ -513,7 +529,11 @@ llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
513
529
constraints.push_back (constraint);
514
530
}
515
531
516
- for (auto adjTypeVar : node.getFixedBindings ()) {
532
+ for (auto adjTypeVar : node.getReferencedBy ()) {
533
+ addTypeVarConstraints (adjTypeVar);
534
+ }
535
+
536
+ for (auto adjTypeVar : node.getReferencedVars ()) {
517
537
addTypeVarConstraints (adjTypeVar);
518
538
}
519
539
}
@@ -1202,24 +1222,31 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
1202
1222
}
1203
1223
}
1204
1224
1205
- // Print fixed bindings.
1206
- if (!FixedBindings.empty ()) {
1207
- out.indent (indent + 2 );
1208
- out << " Fixed bindings: " ;
1209
- SmallVector<TypeVariableType *, 4 > sortedFixedBindings (
1210
- FixedBindings.begin (), FixedBindings.end ());
1211
- std::sort (sortedFixedBindings.begin (), sortedFixedBindings.end (),
1225
+ auto printVarList = [&](ArrayRef<TypeVariableType *> typeVars) {
1226
+ SmallVector<TypeVariableType *, 4 > sorted (typeVars.begin (), typeVars.end ());
1227
+ std::sort (sorted.begin (), sorted.end (),
1212
1228
[&](TypeVariableType *typeVar1, TypeVariableType *typeVar2) {
1213
1229
return typeVar1->getID () < typeVar2->getID ();
1214
1230
});
1215
1231
1216
- interleave (sortedFixedBindings,
1217
- [&](TypeVariableType *typeVar) {
1218
- out << " $T" << typeVar->getID ();
1219
- },
1220
- [&]() {
1221
- out << " , " ;
1222
- });
1232
+ interleave (
1233
+ sorted,
1234
+ [&](TypeVariableType *typeVar) { out << typeVar->getString (PO); },
1235
+ [&out] { out << " , " ; });
1236
+ };
1237
+
1238
+ // Print fixed bindings.
1239
+ if (!ReferencedBy.empty ()) {
1240
+ out.indent (indent + 2 );
1241
+ out << " Referenced By: " ;
1242
+ printVarList (getReferencedBy ());
1243
+ out << " \n " ;
1244
+ }
1245
+
1246
+ if (!References.empty ()) {
1247
+ out.indent (indent + 2 );
1248
+ out << " References: " ;
1249
+ printVarList (getReferencedVars ());
1223
1250
out << " \n " ;
1224
1251
}
1225
1252
0 commit comments