Skip to content

Commit 892e79c

Browse files
committed
Sema: Use an xmacro to clean up some duplication in CSTrail.cpp
1 parent 9201a37 commit 892e79c

File tree

10 files changed

+198
-245
lines changed

10 files changed

+198
-245
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- CSTrail.def - Trail Change Kinds ---------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// This file enumerates the kinds of SolverTrail::Change.
14+
///
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef CHANGE
18+
#define CHANGE(Name)
19+
#endif
20+
21+
#ifndef LOCATOR_CHANGE
22+
#define LOCATOR_CHANGE(Name) CHANGE(Name)
23+
#endif
24+
25+
#ifndef LAST_CHANGE
26+
#define LAST_CHANGE(Name)
27+
#endif
28+
29+
LOCATOR_CHANGE(RecordedAppliedDisjunction)
30+
LOCATOR_CHANGE(RecordedMatchCallArgumentResult)
31+
LOCATOR_CHANGE(RecordedOpenedTypes)
32+
LOCATOR_CHANGE(RecordedOpenedExistentialType)
33+
LOCATOR_CHANGE(RecordedPackExpansionEnvironment)
34+
LOCATOR_CHANGE(RecordedDefaultedConstraint)
35+
36+
CHANGE(AddedTypeVariable)
37+
CHANGE(AddedConstraint)
38+
CHANGE(RemovedConstraint)
39+
CHANGE(ExtendedEquivalenceClass)
40+
CHANGE(RelatedTypeVariables)
41+
CHANGE(InferredBindings)
42+
CHANGE(RetractedBindings)
43+
CHANGE(UpdatedTypeVariable)
44+
CHANGE(AddedConversionRestriction)
45+
CHANGE(AddedFix)
46+
CHANGE(AddedFixedRequirement)
47+
CHANGE(RecordedDisjunctionChoice)
48+
CHANGE(RecordedOpenedPackExpansionType)
49+
CHANGE(RecordedPackEnvironment)
50+
CHANGE(RecordedNodeType)
51+
CHANGE(RecordedKeyPathComponentType)
52+
CHANGE(DisabledConstraint)
53+
CHANGE(FavoredConstraint)
54+
CHANGE(RecordedResultBuilderTransform)
55+
CHANGE(AppliedPropertyWrapper)
56+
57+
LAST_CHANGE(AppliedPropertyWrapper)
58+
59+
#undef LOCATOR_CHANGE
60+
#undef LAST_CHANGE
61+
#undef CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 31 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,11 @@ class SolverTrail {
4040

4141
/// The kind of change made to the graph.
4242
enum class ChangeKind: unsigned {
43-
/// Added a new vertex to the constraint graph.
44-
AddedTypeVariable,
45-
/// Added a new constraint to the constraint graph.
46-
AddedConstraint,
47-
/// Removed an existing constraint from the constraint graph.
48-
RemovedConstraint,
49-
/// Extended the equivalence class of a type variable in the constraint graph.
50-
ExtendedEquivalenceClass,
51-
/// Added a new edge in the constraint graph.
52-
RelatedTypeVariables,
53-
/// Inferred potential bindings from a constraint.
54-
InferredBindings,
55-
/// Retracted potential bindings from a constraint.
56-
RetractedBindings,
57-
/// Set the fixed type or parent and flags for a type variable.
58-
UpdatedTypeVariable,
59-
/// Recorded a conversion restriction kind.
60-
AddedConversionRestriction,
61-
/// Recorded a fix.
62-
AddedFix,
63-
/// Recorded a fixed requirement.
64-
AddedFixedRequirement,
65-
/// Recorded a disjunction choice.
66-
RecordedDisjunctionChoice,
67-
/// Recorded an applied disjunction.
68-
RecordedAppliedDisjunction,
69-
/// Recorded an argument matching choice.
70-
RecordedMatchCallArgumentResult,
71-
/// Recorded a list of opened types at a locator.
72-
RecordedOpenedTypes,
73-
/// Recorded the opening of an existential type at a locator.
74-
RecordedOpenedExistentialType,
75-
/// Recorded the opening of a pack existential type.
76-
RecordedOpenedPackExpansionType,
77-
/// Recorded the creation of a generic environment for a pack expansion expression.
78-
RecordedPackExpansionEnvironment,
79-
/// Recorded the mapping from a pack element expression to its parent
80-
/// pack expansion expression.
81-
RecordedPackEnvironment,
82-
/// Recorded a defaulted constraint at a locator.
83-
RecordedDefaultedConstraint,
84-
/// Recorded an assignment of a type to an AST node.
85-
RecordedNodeType,
86-
/// Recorded an assignment of a type to a keypath component.
87-
RecordedKeyPathComponentType,
88-
/// Disabled a constraint.
89-
DisabledConstraint,
90-
/// Favored a constraint.
91-
FavoredConstraint,
92-
/// Recorded a result builder transform.
93-
RecordedResultBuilderTransform,
94-
/// Applied a property wrapper.
95-
AppliedPropertyWrapper,
43+
#define CHANGE(Name) Name,
44+
#define LAST_CHANGE(Name) Last = Name
45+
#include "CSTrail.def"
46+
#undef CHANGE
47+
#undef LAST_CHANGE
9648
};
9749

9850
/// A change made to the constraint system.
@@ -176,97 +128,82 @@ class SolverTrail {
176128

177129
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
178130

131+
#define LOCATOR_CHANGE(Name) static Change Name(ConstraintLocator *locator);
132+
#include "swift/Sema/CSTrail.def"
133+
179134
/// Create a change that added a type variable.
180-
static Change addedTypeVariable(TypeVariableType *typeVar);
135+
static Change AddedTypeVariable(TypeVariableType *typeVar);
181136

182137
/// Create a change that added a constraint.
183-
static Change addedConstraint(TypeVariableType *typeVar, Constraint *constraint);
138+
static Change AddedConstraint(TypeVariableType *typeVar, Constraint *constraint);
184139

185140
/// Create a change that removed a constraint.
186-
static Change removedConstraint(TypeVariableType *typeVar, Constraint *constraint);
141+
static Change RemovedConstraint(TypeVariableType *typeVar, Constraint *constraint);
187142

188143
/// Create a change that extended an equivalence class.
189-
static Change extendedEquivalenceClass(TypeVariableType *typeVar,
144+
static Change ExtendedEquivalenceClass(TypeVariableType *typeVar,
190145
unsigned prevSize);
191146

192147
/// Create a change that updated the references/referenced by sets of
193148
/// a type variable pair.
194-
static Change relatedTypeVariables(TypeVariableType *typeVar,
149+
static Change RelatedTypeVariables(TypeVariableType *typeVar,
195150
TypeVariableType *otherTypeVar);
196151

197152
/// Create a change that inferred bindings from a constraint.
198-
static Change inferredBindings(TypeVariableType *typeVar,
153+
static Change InferredBindings(TypeVariableType *typeVar,
199154
Constraint *constraint);
200155

201156
/// Create a change that retracted bindings from a constraint.
202-
static Change retractedBindings(TypeVariableType *typeVar,
157+
static Change RetractedBindings(TypeVariableType *typeVar,
203158
Constraint *constraint);
204159

205160
/// Create a change that updated a type variable.
206-
static Change updatedTypeVariable(
161+
static Change UpdatedTypeVariable(
207162
TypeVariableType *typeVar,
208163
llvm::PointerUnion<TypeVariableType *, TypeBase *> parentOrFixed,
209164
unsigned options);
210165

211166
/// Create a change that recorded a restriction.
212-
static Change addedConversionRestriction(Type srcType, Type dstType);
167+
static Change AddedConversionRestriction(Type srcType, Type dstType);
213168

214169
/// Create a change that recorded a fix.
215-
static Change addedFix(ConstraintFix *fix);
170+
static Change AddedFix(ConstraintFix *fix);
216171

217172
/// Create a change that recorded a fixed requirement.
218-
static Change addedFixedRequirement(GenericTypeParamType *GP,
173+
static Change AddedFixedRequirement(GenericTypeParamType *GP,
219174
unsigned reqKind,
220175
Type requirementTy);
221176

222177
/// Create a change that recorded a disjunction choice.
223-
static Change recordedDisjunctionChoice(ConstraintLocator *locator,
178+
static Change RecordedDisjunctionChoice(ConstraintLocator *locator,
224179
unsigned index);
225180

226-
/// Create a change that recorded an applied disjunction.
227-
static Change recordedAppliedDisjunction(ConstraintLocator *locator);
228-
229-
/// Create a change that recorded an applied disjunction.
230-
static Change recordedMatchCallArgumentResult(ConstraintLocator *locator);
231-
232-
/// Create a change that recorded a list of opened types.
233-
static Change recordedOpenedTypes(ConstraintLocator *locator);
234-
235-
/// Create a change that recorded the opening of an existential type.
236-
static Change recordedOpenedExistentialType(ConstraintLocator *locator);
237-
238181
/// Create a change that recorded the opening of a pack expansion type.
239-
static Change recordedOpenedPackExpansionType(PackExpansionType *expansion);
240-
241-
/// Create a change that recorded the opening of a pack expansion type.
242-
static Change recordedPackExpansionEnvironment(ConstraintLocator *locator);
182+
static Change RecordedOpenedPackExpansionType(PackExpansionType *expansion);
243183

244184
/// Create a change that recorded a mapping from a pack element expression
245185
/// to its parent expansion expression.
246-
static Change recordedPackEnvironment(PackElementExpr *packElement);
247-
248-
/// Create a change that recorded a defaulted constraint at a locator.
249-
static Change recordedDefaultedConstraint(ConstraintLocator *locator);
186+
static Change RecordedPackEnvironment(PackElementExpr *packElement);
250187

251188
/// Create a change that recorded an assignment of a type to an AST node.
252-
static Change recordedNodeType(ASTNode node, Type oldType);
189+
static Change RecordedNodeType(ASTNode node, Type oldType);
253190

254191
/// Create a change that recorded an assignment of a type to an AST node.
255-
static Change recordedKeyPathComponentType(const KeyPathExpr *expr,
192+
static Change RecordedKeyPathComponentType(const KeyPathExpr *expr,
256193
unsigned component,
257194
Type oldType);
258195

259196
/// Create a change that disabled a constraint.
260-
static Change disabledConstraint(Constraint *constraint);
197+
static Change DisabledConstraint(Constraint *constraint);
261198

262199
/// Create a change that favored a constraint.
263-
static Change favoredConstraint(Constraint *constraint);
200+
static Change FavoredConstraint(Constraint *constraint);
264201

265202
/// Create a change that recorded a result builder transform.
266-
static Change recordedResultBuilderTransform(AnyFunctionRef fn);
203+
static Change RecordedResultBuilderTransform(AnyFunctionRef fn);
267204

268205
/// Create a change that recorded a result builder transform.
269-
static Change appliedPropertyWrapper(Expr *anchor);
206+
static Change AppliedPropertyWrapper(Expr *anchor);
270207

271208
/// Undo this change, reverting the constraint graph to the state it
272209
/// had prior to this change.
@@ -278,7 +215,7 @@ class SolverTrail {
278215
unsigned indent = 0) const;
279216
};
280217

281-
SolverTrail(ConstraintSystem &cs) : CS(cs) {}
218+
SolverTrail(ConstraintSystem &cs);
282219

283220
~SolverTrail();
284221

@@ -305,6 +242,8 @@ class SolverTrail {
305242
/// The list of changes made to this constraint system.
306243
std::vector<Change> Changes;
307244

245+
uint64_t Profile[unsigned(ChangeKind::Last) + 1];
246+
308247
bool UndoActive = false;
309248
unsigned Total = 0;
310249
unsigned Max = 0;

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class TypeVariableType::Implementation {
435435

436436
/// Record the current type-variable binding.
437437
void recordBinding(constraints::SolverTrail &trail) {
438-
trail.recordChange(constraints::SolverTrail::Change::updatedTypeVariable(
438+
trail.recordChange(constraints::SolverTrail::Change::UpdatedTypeVariable(
439439
getTypeVariable(), ParentOrFixed, getRawOptions()));
440440
}
441441

@@ -2413,7 +2413,7 @@ class ConstraintSystem {
24132413
bool inserted = DefaultedConstraints.insert(locator).second;
24142414
if (inserted) {
24152415
if (solverState)
2416-
recordChange(SolverTrail::Change::recordedDefaultedConstraint(locator));
2416+
recordChange(SolverTrail::Change::RecordedDefaultedConstraint(locator));
24172417
}
24182418
}
24192419

@@ -2643,7 +2643,7 @@ class ConstraintSystem {
26432643
void disableConstraint(Constraint *constraint) {
26442644
ASSERT(!constraint->isDisabled());
26452645
constraint->setDisabled();
2646-
Trail.recordChange(SolverTrail::Change::disabledConstraint(constraint));
2646+
Trail.recordChange(SolverTrail::Change::DisabledConstraint(constraint));
26472647
}
26482648

26492649
/// Favor the given constraint; this change will be rolled back
@@ -2652,7 +2652,7 @@ class ConstraintSystem {
26522652
assert(!constraint->isFavored());
26532653

26542654
constraint->setFavored();
2655-
Trail.recordChange(SolverTrail::Change::favoredConstraint(constraint));
2655+
Trail.recordChange(SolverTrail::Change::FavoredConstraint(constraint));
26562656
}
26572657

26582658
private:
@@ -3172,7 +3172,7 @@ class ConstraintSystem {
31723172
if (oldType.getPointer() != type.getPointer()) {
31733173
// Record the fact that we assigned a type to this node.
31743174
if (solverState)
3175-
recordChange(SolverTrail::Change::recordedNodeType(node, oldType));
3175+
recordChange(SolverTrail::Change::RecordedNodeType(node, oldType));
31763176
}
31773177
}
31783178

@@ -3211,7 +3211,7 @@ class ConstraintSystem {
32113211
if (oldType.getPointer() != T.getPointer()) {
32123212
if (solverState) {
32133213
recordChange(
3214-
SolverTrail::Change::recordedKeyPathComponentType(
3214+
SolverTrail::Change::RecordedKeyPathComponentType(
32153215
KP, I, oldType));
32163216
}
32173217
}

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ void ConstraintSystem::recordResultBuilderTransform(AnyFunctionRef fn,
12261226
ASSERT(inserted);
12271227

12281228
if (solverState)
1229-
recordChange(SolverTrail::Change::recordedResultBuilderTransform(fn));
1229+
recordChange(SolverTrail::Change::RecordedResultBuilderTransform(fn));
12301230
}
12311231

12321232
/// Undo the above change.

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ void PotentialBindings::infer(Constraint *constraint) {
17771777

17781778
// Record the change, if there are active scopes.
17791779
if (CS.isRecordingChanges())
1780-
CS.recordChange(SolverTrail::Change::inferredBindings(TypeVar, constraint));
1780+
CS.recordChange(SolverTrail::Change::InferredBindings(TypeVar, constraint));
17811781

17821782
switch (constraint->getKind()) {
17831783
case ConstraintKind::Bind:
@@ -1952,7 +1952,7 @@ void PotentialBindings::retract(Constraint *constraint) {
19521952

19531953
// Record the change, if there are active scopes.
19541954
if (CS.isRecordingChanges())
1955-
CS.recordChange(SolverTrail::Change::retractedBindings(TypeVar, constraint));
1955+
CS.recordChange(SolverTrail::Change::RetractedBindings(TypeVar, constraint));
19561956

19571957
LLVM_DEBUG(
19581958
llvm::dbgs() << Constraints.size() << " " << Bindings.size() << " "

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5051,7 +5051,7 @@ void ConstraintSystem::applyPropertyWrapper(
50515051
appliedPropertyWrappers[anchor].push_back(applied);
50525052

50535053
if (solverState)
5054-
recordChange(SolverTrail::Change::appliedPropertyWrapper(anchor));
5054+
recordChange(SolverTrail::Change::AppliedPropertyWrapper(anchor));
50555055
}
50565056

50575057
void ConstraintSystem::removePropertyWrapper(Expr *anchor) {

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14930,7 +14930,7 @@ void ConstraintSystem::recordMatchCallArgumentResult(
1493014930
ASSERT(inserted);
1493114931

1493214932
if (solverState)
14933-
recordChange(SolverTrail::Change::recordedMatchCallArgumentResult(locator));
14933+
recordChange(SolverTrail::Change::RecordedMatchCallArgumentResult(locator));
1493414934
}
1493514935

1493614936
void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,

0 commit comments

Comments
 (0)