Skip to content

Commit f19016b

Browse files
committed
[Constraint graph] Add preVisit hook for depth-first search.
Refactoring so we can use this in a moment.
1 parent 0b7ef34 commit f19016b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -814,19 +814,29 @@ namespace {
814814
}
815815

816816
/// Perform a depth-first search to produce a from the given type variable,
817-
/// notifying the function object \c postVisit after each reachable
818-
/// type variable has been visited.
817+
/// notifying the function object.
818+
///
819+
/// \param getAdjacencies Called to retrieve the set of type variables
820+
/// that are adjacent to the given type variable.
821+
///
822+
/// \param preVisit Called before visiting the adjacencies of the given
823+
/// type variable. When it returns \c true, the adjacencies of this type
824+
/// variable will be visited. When \c false, the adjacencies will not be
825+
/// visited and \c postVisit will not be called.
826+
///
827+
/// \param postVisit Called after visiting the adjacencies of the given
828+
/// type variable.
819829
static void postorderDepthFirstSearchRec(
820830
TypeVariableType *typeVar,
821831
llvm::function_ref<
822832
ArrayRef<TypeVariableType *>(TypeVariableType *)> getAdjacencies,
823-
llvm::function_ref<void(TypeVariableType *)> postVisit,
824-
SmallPtrSet<TypeVariableType *, 4> &visited) {
825-
if (!visited.insert(typeVar).second)
833+
llvm::function_ref<bool(TypeVariableType *)> preVisit,
834+
llvm::function_ref<void(TypeVariableType *)> postVisit) {
835+
if (!preVisit(typeVar))
826836
return;
827837

828838
for (auto adj : getAdjacencies(typeVar)) {
829-
postorderDepthFirstSearchRec(adj, getAdjacencies, postVisit, visited);
839+
postorderDepthFirstSearchRec(adj, getAdjacencies, preVisit, postVisit);
830840
}
831841

832842
postVisit(typeVar);
@@ -855,14 +865,16 @@ namespace {
855865

856866
return oneWayComponent->second.inAdjacencies;
857867
},
868+
[&](TypeVariableType *typeVar) {
869+
return visited.insert(typeVar).second;
870+
},
858871
[&](TypeVariableType *dependsOn) {
859872
// Don't record dependency on ourselves.
860873
if (dependsOn == inAdj)
861874
return;
862875

863876
indirectlyReachable.insert(dependsOn);
864-
},
865-
visited);
877+
});
866878

867879
// Remove any in-adjacency of this component that is indirectly
868880
// reachable.
@@ -922,13 +934,15 @@ namespace {
922934

923935
return oneWayComponent->second.outAdjacencies;
924936
},
937+
[&](TypeVariableType *typeVar) {
938+
return visited.insert(typeVar).second;
939+
},
925940
[&](TypeVariableType *typeVar) {
926941
// Record this type variable, if it's one of the representative
927942
// type variables.
928943
if (validComponents.count(typeVar) > 0)
929944
orderedReps.push_back(typeVar);
930-
},
931-
visited);
945+
});
932946
}
933947

934948
assert(orderedReps.size() == representativeTypeVars.size());

0 commit comments

Comments
 (0)