Skip to content

Commit bdab82a

Browse files
committed
Sema: Use xmacros to clean up more duplication in CSTrail.{cpp,h}
1 parent 5fdc1a8 commit bdab82a

File tree

3 files changed

+56
-135
lines changed

3 files changed

+56
-135
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
#define CONSTRAINT_CHANGE(Name) CHANGE(Name)
3535
#endif
3636

37+
#ifndef GRAPH_NODE_CHANGE
38+
#define GRAPH_NODE_CHANGE(Name) CHANGE(Name)
39+
#endif
40+
41+
#ifndef SCORE_CHANGE
42+
#define SCORE_CHANGE(Name) CHANGE(Name)
43+
#endif
44+
3745
#ifndef LAST_CHANGE
3846
#define LAST_CHANGE(Name)
3947
#endif
@@ -63,13 +71,17 @@ CONSTRAINT_CHANGE(FavoredConstraint)
6371
CONSTRAINT_CHANGE(GeneratedConstraint)
6472
CONSTRAINT_CHANGE(RetiredConstraint)
6573

74+
GRAPH_NODE_CHANGE(AddedConstraint)
75+
GRAPH_NODE_CHANGE(RemovedConstraint)
76+
GRAPH_NODE_CHANGE(InferredBindings)
77+
GRAPH_NODE_CHANGE(RetractedBindings)
78+
79+
SCORE_CHANGE(IncreasedScore)
80+
SCORE_CHANGE(DecreasedScore)
81+
6682
CHANGE(AddedTypeVariable)
67-
CHANGE(AddedConstraint)
68-
CHANGE(RemovedConstraint)
6983
CHANGE(ExtendedEquivalenceClass)
7084
CHANGE(RelatedTypeVariables)
71-
CHANGE(InferredBindings)
72-
CHANGE(RetractedBindings)
7385
CHANGE(UpdatedTypeVariable)
7486
CHANGE(AddedConversionRestriction)
7587
CHANGE(AddedFix)
@@ -85,14 +97,14 @@ CHANGE(RecordedCaseLabelItemInfo)
8597
CHANGE(RecordedPotentialThrowSite)
8698
CHANGE(RecordedIsolatedParam)
8799
CHANGE(RecordedKeyPath)
88-
CHANGE(IncreasedScore)
89-
CHANGE(DecreasedScore)
90100

91-
LAST_CHANGE(DecreasedScore)
101+
LAST_CHANGE(RecordedKeyPath)
92102

93103
#undef LOCATOR_CHANGE
94104
#undef EXPR_CHANGE
95105
#undef CLOSURE_CHANGE
96106
#undef CONSTRAINT_CHANGE
107+
#undef GRAPH_NODE_CHANGE
108+
#undef SCORE_CHANGE
97109
#undef LAST_CHANGE
98110
#undef CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,14 @@ class SolverTrail {
143143
#define EXPR_CHANGE(Name) static Change Name(Expr *expr);
144144
#define CLOSURE_CHANGE(Name) static Change Name(ClosureExpr *closure);
145145
#define CONSTRAINT_CHANGE(Name) static Change Name(Constraint *constraint);
146+
#define SCORE_CHANGE(Name) static Change Name(ScoreKind kind, unsigned value);
147+
#define GRAPH_NODE_CHANGE(Name) static Change Name(TypeVariableType *typeVar, \
148+
Constraint *constraint);
146149
#include "swift/Sema/CSTrail.def"
147150

148151
/// Create a change that added a type variable.
149152
static Change AddedTypeVariable(TypeVariableType *typeVar);
150153

151-
/// Create a change that added a constraint.
152-
static Change AddedConstraint(TypeVariableType *typeVar, Constraint *constraint);
153-
154-
/// Create a change that removed a constraint.
155-
static Change RemovedConstraint(TypeVariableType *typeVar, Constraint *constraint);
156-
157154
/// Create a change that extended an equivalence class.
158155
static Change ExtendedEquivalenceClass(TypeVariableType *typeVar,
159156
unsigned prevSize);
@@ -163,14 +160,6 @@ class SolverTrail {
163160
static Change RelatedTypeVariables(TypeVariableType *typeVar,
164161
TypeVariableType *otherTypeVar);
165162

166-
/// Create a change that inferred bindings from a constraint.
167-
static Change InferredBindings(TypeVariableType *typeVar,
168-
Constraint *constraint);
169-
170-
/// Create a change that retracted bindings from a constraint.
171-
static Change RetractedBindings(TypeVariableType *typeVar,
172-
Constraint *constraint);
173-
174163
/// Create a change that updated a type variable.
175164
static Change UpdatedTypeVariable(
176165
TypeVariableType *typeVar,
@@ -224,12 +213,6 @@ class SolverTrail {
224213
/// Create a change that recorded a key path expression.
225214
static Change RecordedKeyPath(KeyPathExpr *expr);
226215

227-
/// Create a change that increased the score.
228-
static Change IncreasedScore(ScoreKind kind, unsigned value);
229-
230-
/// Create a change that decreased the score.
231-
static Change DecreasedScore(ScoreKind kind, unsigned value);
232-
233216
/// Undo this change, reverting the constraint graph to the state it
234217
/// had prior to this change.
235218
///

lib/Sema/CSTrail.cpp

Lines changed: 34 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ SolverTrail::~SolverTrail() {
8080
result.TheConstraint.Constraint = constraint; \
8181
return result; \
8282
}
83+
#define GRAPH_NODE_CHANGE(Name) \
84+
SolverTrail::Change \
85+
SolverTrail::Change::Name(TypeVariableType *typeVar, \
86+
Constraint *constraint) { \
87+
Change result; \
88+
result.Kind = ChangeKind::Name; \
89+
result.TheConstraint.TypeVar = typeVar; \
90+
result.TheConstraint.Constraint = constraint; \
91+
return result; \
92+
}
93+
#define SCORE_CHANGE(Name) \
94+
SolverTrail::Change \
95+
SolverTrail::Change::Name(ScoreKind kind, unsigned value) { \
96+
ASSERT(value <= 0xffffff && "value must fit in 24 bits"); \
97+
Change result; \
98+
result.Kind = ChangeKind::Name; \
99+
result.Options = unsigned(kind) | (value << 8); \
100+
return result; \
101+
}
83102
#include "swift/Sema/CSTrail.def"
84103

85104
SolverTrail::Change
@@ -90,26 +109,6 @@ SolverTrail::Change::AddedTypeVariable(TypeVariableType *typeVar) {
90109
return result;
91110
}
92111

93-
SolverTrail::Change
94-
SolverTrail::Change::AddedConstraint(TypeVariableType *typeVar,
95-
Constraint *constraint) {
96-
Change result;
97-
result.Kind = ChangeKind::AddedConstraint;
98-
result.TheConstraint.TypeVar = typeVar;
99-
result.TheConstraint.Constraint = constraint;
100-
return result;
101-
}
102-
103-
SolverTrail::Change
104-
SolverTrail::Change::RemovedConstraint(TypeVariableType *typeVar,
105-
Constraint *constraint) {
106-
Change result;
107-
result.Kind = ChangeKind::RemovedConstraint;
108-
result.TheConstraint.TypeVar = typeVar;
109-
result.TheConstraint.Constraint = constraint;
110-
return result;
111-
}
112-
113112
SolverTrail::Change
114113
SolverTrail::Change::ExtendedEquivalenceClass(TypeVariableType *typeVar,
115114
unsigned prevSize) {
@@ -130,26 +129,6 @@ SolverTrail::Change::RelatedTypeVariables(TypeVariableType *typeVar,
130129
return result;
131130
}
132131

133-
SolverTrail::Change
134-
SolverTrail::Change::InferredBindings(TypeVariableType *typeVar,
135-
Constraint *constraint) {
136-
Change result;
137-
result.Kind = ChangeKind::InferredBindings;
138-
result.TheConstraint.TypeVar = typeVar;
139-
result.TheConstraint.Constraint = constraint;
140-
return result;
141-
}
142-
143-
SolverTrail::Change
144-
SolverTrail::Change::RetractedBindings(TypeVariableType *typeVar,
145-
Constraint *constraint) {
146-
Change result;
147-
result.Kind = ChangeKind::RetractedBindings;
148-
result.TheConstraint.TypeVar = typeVar;
149-
result.TheConstraint.Constraint = constraint;
150-
return result;
151-
}
152-
153132
SolverTrail::Change
154133
SolverTrail::Change::UpdatedTypeVariable(
155134
TypeVariableType *typeVar,
@@ -317,26 +296,6 @@ SolverTrail::Change::RecordedKeyPath(KeyPathExpr *expr) {
317296
return result;
318297
}
319298

320-
SolverTrail::Change
321-
SolverTrail::Change::IncreasedScore(ScoreKind kind, unsigned value) {
322-
ASSERT(value <= 0xffffff && "value must fit in 24 bits");
323-
324-
Change result;
325-
result.Kind = ChangeKind::IncreasedScore;
326-
result.Options = unsigned(kind) | (value << 8);
327-
return result;
328-
}
329-
330-
SolverTrail::Change
331-
SolverTrail::Change::DecreasedScore(ScoreKind kind, unsigned value) {
332-
ASSERT(value <= 0xffffff && "value must fit in 24 bits");
333-
334-
Change result;
335-
result.Kind = ChangeKind::DecreasedScore;
336-
result.Options = unsigned(kind) | (value << 8);
337-
return result;
338-
}
339-
340299
SyntacticElementTargetKey
341300
SolverTrail::Change::getSyntacticElementTargetKey() const {
342301
ASSERT(Kind == ChangeKind::RecordedTarget);
@@ -562,6 +521,21 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
562521
indent + 2); \
563522
out << ")\n"; \
564523
break;
524+
#define GRAPH_NODE_CHANGE(Name) \
525+
case ChangeKind::Name: \
526+
out << "(" << #Name << " "; \
527+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr, \
528+
indent + 2); \
529+
out << " on type variable "; \
530+
TheConstraint.TypeVar->print(out, PO); \
531+
out << ")\n"; \
532+
break;
533+
#define SCORE_CHANGE(Name) \
534+
case ChangeKind::Name: \
535+
out << "(" << #Name << " "; \
536+
out << Score::getNameFor(ScoreKind(Options & 0xff)); \
537+
out << " by " << (Options >> 8) << ")\n"; \
538+
break;
565539
#include "swift/Sema/CSTrail.def"
566540

567541
case ChangeKind::AddedTypeVariable:
@@ -570,24 +544,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
570544
out << ")\n";
571545
break;
572546

573-
case ChangeKind::AddedConstraint:
574-
out << "(AddedConstraint ";
575-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
576-
indent + 2);
577-
out << " to type variable ";
578-
TheConstraint.TypeVar->print(out, PO);
579-
out << ")\n";
580-
break;
581-
582-
case ChangeKind::RemovedConstraint:
583-
out << "(RemovedConstraint ";
584-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
585-
indent + 2);
586-
out << " from type variable ";
587-
TheConstraint.TypeVar->print(out, PO);
588-
out << ")\n";
589-
break;
590-
591547
case ChangeKind::ExtendedEquivalenceClass: {
592548
out << "(ExtendedEquivalenceClass ";
593549
EquivClass.TypeVar->print(out, PO);
@@ -603,24 +559,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
603559
out << ")\n";
604560
break;
605561

606-
case ChangeKind::InferredBindings:
607-
out << "(InferredBindings from ";
608-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
609-
indent + 2);
610-
out << " for type variable ";
611-
TheConstraint.TypeVar->print(out, PO);
612-
out << ")\n";
613-
break;
614-
615-
case ChangeKind::RetractedBindings:
616-
out << "(RetractedBindings from ";
617-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
618-
indent + 2);
619-
out << " for type variable ";
620-
TheConstraint.TypeVar->print(out, PO);
621-
out << ")\n";
622-
break;
623-
624562
case ChangeKind::UpdatedTypeVariable: {
625563
out << "(UpdatedTypeVariable ";
626564
Update.TypeVar->print(out, PO);
@@ -736,18 +674,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
736674
simple_display(out, KeyPath.Expr);
737675
out << ")\n";
738676
break;
739-
740-
case ChangeKind::IncreasedScore:
741-
out << "(IncreasedScore ";
742-
out << Score::getNameFor(ScoreKind(Options & 0xff));
743-
out << " by " << (Options >> 8) << ")\n";
744-
break;
745-
746-
case ChangeKind::DecreasedScore:
747-
out << "(DecreasedScore ";
748-
out << Score::getNameFor(ScoreKind(Options & 0xff));
749-
out << " by " << (Options >> 8) << ")\n";
750-
break;
751677
}
752678
}
753679

0 commit comments

Comments
 (0)