-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCSTrail.cpp
779 lines (671 loc) · 22.1 KB
/
CSTrail.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//===--- CSTrail.cpp - Tracking changes that can be undone ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the \c SolverTrail class, which records the decisions taken
// while attempting to find a solution.
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/Defer.h"
#include "swift/Basic/Statistic.h"
#include "swift/Sema/ConstraintGraph.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/CSTrail.h"
#include "swift/Basic/Assertions.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/SaveAndRestore.h"
#include <algorithm>
#include <memory>
#include <numeric>
using namespace swift;
using namespace constraints;
#define DEBUG_TYPE "SolverTrail"
SolverTrail::SolverTrail(ConstraintSystem &cs)
: CS(cs) {
for (unsigned i = 0; i <= unsigned(ChangeKind::Last); ++i) {
Profile[i] = 0;
}
}
SolverTrail::~SolverTrail() {
// If constraint system is in an invalid state, it's
// possible that constraint graph is corrupted as well
// so let's not attempt to check change log.
if (!CS.inInvalidState())
ASSERT(Changes.empty() && "Trail corrupted");
}
#define LOCATOR_CHANGE(Name, _) \
SolverTrail::Change \
SolverTrail::Change::Name(ConstraintLocator *locator) { \
Change result; \
result.Kind = ChangeKind::Name; \
result.TheLocator = locator; \
return result; \
}
#define EXPR_CHANGE(Name) \
SolverTrail::Change \
SolverTrail::Change::Name(Expr *expr) { \
Change result; \
result.Kind = ChangeKind::Name; \
result.TheExpr = expr; \
return result; \
}
#define CLOSURE_CHANGE(Name) \
SolverTrail::Change \
SolverTrail::Change::Name(ClosureExpr *closure) { \
Change result; \
result.Kind = ChangeKind::Name; \
result.TheClosure = closure; \
return result; \
}
#include "swift/Sema/CSTrail.def"
SolverTrail::Change
SolverTrail::Change::AddedTypeVariable(TypeVariableType *typeVar) {
Change result;
result.Kind = ChangeKind::AddedTypeVariable;
result.TypeVar = typeVar;
return result;
}
SolverTrail::Change
SolverTrail::Change::AddedConstraint(TypeVariableType *typeVar,
Constraint *constraint) {
Change result;
result.Kind = ChangeKind::AddedConstraint;
result.TheConstraint.TypeVar = typeVar;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::RemovedConstraint(TypeVariableType *typeVar,
Constraint *constraint) {
Change result;
result.Kind = ChangeKind::RemovedConstraint;
result.TheConstraint.TypeVar = typeVar;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::ExtendedEquivalenceClass(TypeVariableType *typeVar,
unsigned prevSize) {
Change result;
result.Kind = ChangeKind::ExtendedEquivalenceClass;
result.EquivClass.TypeVar = typeVar;
result.EquivClass.PrevSize = prevSize;
return result;
}
SolverTrail::Change
SolverTrail::Change::RelatedTypeVariables(TypeVariableType *typeVar,
TypeVariableType *otherTypeVar) {
Change result;
result.Kind = ChangeKind::RelatedTypeVariables;
result.Relation.TypeVar = typeVar;
result.Relation.OtherTypeVar = otherTypeVar;
return result;
}
SolverTrail::Change
SolverTrail::Change::InferredBindings(TypeVariableType *typeVar,
Constraint *constraint) {
Change result;
result.Kind = ChangeKind::InferredBindings;
result.TheConstraint.TypeVar = typeVar;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::RetractedBindings(TypeVariableType *typeVar,
Constraint *constraint) {
Change result;
result.Kind = ChangeKind::RetractedBindings;
result.TheConstraint.TypeVar = typeVar;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::UpdatedTypeVariable(
TypeVariableType *typeVar,
llvm::PointerUnion<TypeVariableType *, TypeBase *> parentOrFixed,
unsigned options) {
Change result;
result.Kind = ChangeKind::UpdatedTypeVariable;
result.Update.TypeVar = typeVar;
result.Update.ParentOrFixed = parentOrFixed;
result.Options = options;
return result;
}
SolverTrail::Change
SolverTrail::Change::AddedConversionRestriction(Type srcType, Type dstType) {
Change result;
result.Kind = ChangeKind::AddedConversionRestriction;
result.Restriction.SrcType = srcType;
result.Restriction.DstType = dstType;
return result;
}
SolverTrail::Change
SolverTrail::Change::AddedFix(ConstraintFix *fix) {
Change result;
result.Kind = ChangeKind::AddedFix;
result.TheFix = fix;
return result;
}
SolverTrail::Change
SolverTrail::Change::AddedFixedRequirement(GenericTypeParamType *GP,
unsigned reqKind,
Type reqTy) {
Change result;
result.Kind = ChangeKind::AddedFixedRequirement;
result.FixedRequirement.GP = GP;
result.FixedRequirement.ReqTy = reqTy;
result.Options = reqKind;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedOpenedPackExpansionType(PackExpansionType *expansionTy) {
Change result;
result.Kind = ChangeKind::RecordedOpenedPackExpansionType;
result.TheExpansion = expansionTy;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedPackEnvironment(PackElementExpr *packElement) {
Change result;
result.Kind = ChangeKind::RecordedPackEnvironment;
result.TheElement = packElement;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedNodeType(ASTNode node, Type oldType) {
Change result;
result.Kind = ChangeKind::RecordedNodeType;
result.Node.Node = node;
result.Node.OldType = oldType;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedKeyPathComponentType(const KeyPathExpr *expr,
unsigned component,
Type oldType) {
Change result;
result.Kind = ChangeKind::RecordedKeyPathComponentType;
result.Options = component;
result.KeyPath.Expr = expr;
result.KeyPath.OldType = oldType;
return result;
}
SolverTrail::Change
SolverTrail::Change::DisabledConstraint(Constraint *constraint) {
Change result;
result.Kind = ChangeKind::DisabledConstraint;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::FavoredConstraint(Constraint *constraint) {
Change result;
result.Kind = ChangeKind::FavoredConstraint;
result.TheConstraint.Constraint = constraint;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedResultBuilderTransform(AnyFunctionRef fn) {
Change result;
result.Kind = ChangeKind::RecordedResultBuilderTransform;
result.TheRef = fn;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedContextualInfo(ASTNode node) {
Change result;
result.Kind = ChangeKind::RecordedContextualInfo;
result.Node.Node = node;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedTarget(SyntacticElementTargetKey key) {
Change result;
result.Kind = ChangeKind::RecordedTarget;
result.Options = unsigned(key.kind);
switch (key.kind) {
case SyntacticElementTargetKey::Kind::empty:
case SyntacticElementTargetKey::Kind::tombstone:
llvm_unreachable("Invalid SyntacticElementTargetKey::Kind");
case SyntacticElementTargetKey::Kind::stmtCondElement:
result.TheCondElt = key.storage.stmtCondElement;
break;
case SyntacticElementTargetKey::Kind::expr:
result.TheExpr = key.storage.expr;
break;
case SyntacticElementTargetKey::Kind::closure:
result.TheClosure = cast<ClosureExpr>(key.storage.expr);
break;
case SyntacticElementTargetKey::Kind::stmt:
result.TheStmt = key.storage.stmt;
break;
case SyntacticElementTargetKey::Kind::pattern:
result.ThePattern = key.storage.pattern;
break;
case SyntacticElementTargetKey::Kind::patternBindingEntry:
result.ThePatternBinding = key.storage.patternBindingEntry.patternBinding;
result.Options |= key.storage.patternBindingEntry.index << 8;
break;
case SyntacticElementTargetKey::Kind::varDecl:
result.TheVar = key.storage.varDecl;
break;
case SyntacticElementTargetKey::Kind::functionRef:
result.TheDeclContext = key.storage.functionRef;
break;
}
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedCaseLabelItemInfo(CaseLabelItem *item) {
Change result;
result.Kind = ChangeKind::RecordedCaseLabelItemInfo;
result.TheItem = item;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedPotentialThrowSite(CatchNode catchNode) {
Change result;
result.Kind = ChangeKind::RecordedPotentialThrowSite;
result.TheCatchNode = catchNode;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedIsolatedParam(ParamDecl *param) {
Change result;
result.Kind = ChangeKind::RecordedIsolatedParam;
result.TheParam = param;
return result;
}
SolverTrail::Change
SolverTrail::Change::RecordedKeyPath(KeyPathExpr *expr) {
Change result;
result.Kind = ChangeKind::RecordedKeyPath;
result.KeyPath.Expr = expr;
return result;
}
SyntacticElementTargetKey
SolverTrail::Change::getSyntacticElementTargetKey() const {
ASSERT(Kind == ChangeKind::RecordedTarget);
auto kind = SyntacticElementTargetKey::Kind(Options & 0xff);
switch (kind) {
case SyntacticElementTargetKey::Kind::empty:
case SyntacticElementTargetKey::Kind::tombstone:
llvm_unreachable("Invalid SyntacticElementTargetKey::Kind");
case SyntacticElementTargetKey::Kind::stmtCondElement:
return SyntacticElementTargetKey(TheCondElt);
case SyntacticElementTargetKey::Kind::expr:
return SyntacticElementTargetKey(TheExpr);
case SyntacticElementTargetKey::Kind::closure:
return SyntacticElementTargetKey(TheClosure);
case SyntacticElementTargetKey::Kind::stmt:
return SyntacticElementTargetKey(TheStmt);
case SyntacticElementTargetKey::Kind::pattern:
return SyntacticElementTargetKey(ThePattern);
case SyntacticElementTargetKey::Kind::patternBindingEntry:
return SyntacticElementTargetKey(ThePatternBinding, Options >> 8);
case SyntacticElementTargetKey::Kind::varDecl:
return SyntacticElementTargetKey(TheVar);
case SyntacticElementTargetKey::Kind::functionRef:
return SyntacticElementTargetKey(TheDeclContext);
}
}
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
auto &cg = cs.getConstraintGraph();
switch (Kind) {
#define LOCATOR_CHANGE(Name, Map) \
case ChangeKind::Name: { \
bool erased = cs.Map.erase(TheLocator); \
ASSERT(erased); \
break; \
}
#include "swift/Sema/CSTrail.def"
case ChangeKind::AddedTypeVariable:
cg.removeNode(TypeVar);
break;
case ChangeKind::AddedConstraint:
cg.removeConstraint(TheConstraint.TypeVar, TheConstraint.Constraint);
break;
case ChangeKind::RemovedConstraint:
cg.addConstraint(TheConstraint.TypeVar, TheConstraint.Constraint);
break;
case ChangeKind::ExtendedEquivalenceClass: {
auto &node = cg[EquivClass.TypeVar];
node.truncateEquivalenceClass(EquivClass.PrevSize);
break;
}
case ChangeKind::RelatedTypeVariables:
cg.unrelateTypeVariables(Relation.TypeVar, Relation.OtherTypeVar);
break;
case ChangeKind::InferredBindings:
cg.retractBindings(TheConstraint.TypeVar, TheConstraint.Constraint);
break;
case ChangeKind::RetractedBindings:
cg.inferBindings(TheConstraint.TypeVar, TheConstraint.Constraint);
break;
case ChangeKind::UpdatedTypeVariable:
Update.TypeVar->getImpl().setRawOptions(Options);
Update.TypeVar->getImpl().ParentOrFixed = Update.ParentOrFixed;
break;
case ChangeKind::AddedConversionRestriction:
cs.removeConversionRestriction(Restriction.SrcType,
Restriction.DstType);
break;
case ChangeKind::AddedFix:
cs.removeFix(TheFix);
break;
case ChangeKind::AddedFixedRequirement:
cs.removeFixedRequirement(FixedRequirement.GP, Options,
FixedRequirement.ReqTy);
break;
case ChangeKind::RecordedOpenedPackExpansionType:
cs.removeOpenedPackExpansionType(TheExpansion);
break;
case ChangeKind::RecordedPackEnvironment:
cs.removePackEnvironment(TheElement);
break;
case ChangeKind::RecordedNodeType:
cs.restoreType(Node.Node, Node.OldType);
break;
case ChangeKind::RecordedKeyPathComponentType:
cs.restoreType(KeyPath.Expr, Options, KeyPath.OldType);
break;
case ChangeKind::DisabledConstraint:
TheConstraint.Constraint->setEnabled();
break;
case ChangeKind::FavoredConstraint:
ASSERT(TheConstraint.Constraint->isFavored());
TheConstraint.Constraint->setFavored(false);
break;
case ChangeKind::RecordedResultBuilderTransform:
cs.removeResultBuilderTransform(TheRef);
break;
case ChangeKind::AppliedPropertyWrapper:
cs.removePropertyWrapper(TheExpr);
break;
case ChangeKind::RecordedClosureType:
cs.removeClosureType(TheClosure);
break;
case ChangeKind::RecordedImpliedResult:
cs.removeImpliedResult(TheExpr);
break;
case ChangeKind::RecordedContextualInfo:
cs.removeContextualInfo(Node.Node);
break;
case ChangeKind::RecordedTarget:
cs.removeTargetFor(getSyntacticElementTargetKey());
break;
case ChangeKind::RecordedCaseLabelItemInfo:
cs.removeCaseLabelItemInfo(TheItem);
break;
case ChangeKind::RecordedPotentialThrowSite:
cs.removePotentialThrowSite(TheCatchNode);
break;
case ChangeKind::RecordedExprPattern:
cs.removeExprPatternFor(TheExpr);
break;
case ChangeKind::RecordedIsolatedParam:
cs.removeIsolatedParam(TheParam);
break;
case ChangeKind::RecordedPreconcurrencyClosure:
cs.removePreconcurrencyClosure(TheClosure);
break;
case ChangeKind::RecordedKeyPath:
cs.removeKeyPath(KeyPath.Expr);
break;
}
}
void SolverTrail::Change::dump(llvm::raw_ostream &out,
ConstraintSystem &cs,
unsigned indent) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
out.indent(indent);
switch (Kind) {
#define LOCATOR_CHANGE(Name, _) \
case ChangeKind::Name: \
out << "(" << #Name << " at "; \
TheLocator->dump(&cs.getASTContext().SourceMgr, out); \
out << ")\n"; \
break;
#define EXPR_CHANGE(Name) \
case ChangeKind::Name: \
out << "(" << #Name << " "; \
simple_display(out, TheExpr); \
out << ")\n"; \
break;
#define CLOSURE_CHANGE(Name) \
case ChangeKind::Name: \
out << "(" << #Name << " "; \
simple_display(out, TheClosure); \
out << ")\n"; \
break;
#include "swift/Sema/CSTrail.def"
case ChangeKind::AddedTypeVariable:
out << "(AddedTypeVariable ";
TypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::AddedConstraint:
out << "(AddedConstraint ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << " to type variable ";
TheConstraint.TypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::RemovedConstraint:
out << "(RemovedConstraint ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << " from type variable ";
TheConstraint.TypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::ExtendedEquivalenceClass: {
out << "(ExtendedEquivalenceClass ";
EquivClass.TypeVar->print(out, PO);
out << " " << EquivClass.PrevSize << ")\n";
break;
}
case ChangeKind::RelatedTypeVariables:
out << "(RelatedTypeVariables ";
Relation.TypeVar->print(out, PO);
out << " with ";
Relation.OtherTypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::InferredBindings:
out << "(InferredBindings from ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << " for type variable ";
TheConstraint.TypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::RetractedBindings:
out << "(RetractedBindings from ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << " for type variable ";
TheConstraint.TypeVar->print(out, PO);
out << ")\n";
break;
case ChangeKind::UpdatedTypeVariable: {
out << "(UpdatedTypeVariable ";
Update.TypeVar->print(out, PO);
auto parentOrFixed = Update.TypeVar->getImpl().ParentOrFixed;
if (auto *parent = parentOrFixed.dyn_cast<TypeVariableType *>()) {
out << " to parent ";
parent->print(out, PO);
}
else {
out << " to fixed type ";
parentOrFixed.get<TypeBase *>()->print(out, PO);
}
out << " with options 0x";
out.write_hex(Options);
out << ")\n";
break;
}
case ChangeKind::AddedConversionRestriction:
out << "(AddedConversionRestriction with source ";
Restriction.SrcType->print(out, PO);
out << " and destination ";
Restriction.DstType->print(out, PO);
out << ")\n";
break;
case ChangeKind::AddedFix:
out << "(AddedFix ";
TheFix->print(out);
out << ")\n";
break;
case ChangeKind::AddedFixedRequirement:
out << "(AddedFixedRequirement ";
FixedRequirement.GP->print(out, PO);
out << " kind ";
out << Options << " ";
FixedRequirement.ReqTy->print(out, PO);
out << ")\n";
break;
case ChangeKind::RecordedOpenedPackExpansionType:
out << "(RecordedOpenedPackExpansionType for ";
TheExpansion->print(out, PO);
out << ")\n";
break;
case ChangeKind::RecordedPackEnvironment:
// FIXME: Print short form of PackExpansionExpr
out << "(RecordedPackEnvironment ";
simple_display(out, TheElement);
out << "\n";
break;
case ChangeKind::RecordedNodeType:
out << "(RecordedNodeType at ";
Node.Node.getStartLoc().print(out, cs.getASTContext().SourceMgr);
out << " previous ";
if (Node.OldType)
Node.OldType->print(out, PO);
else
out << "null";
out << ")\n";
break;
case ChangeKind::RecordedKeyPathComponentType:
out << "(RecordedKeyPathComponentType ";
simple_display(out, KeyPath.Expr);
out << " with component type ";
if (Node.OldType)
Node.OldType->print(out, PO);
else
out << "null";
out << " for component " << Options << ")\n";
break;
case ChangeKind::DisabledConstraint:
out << "(DisabledConstraint ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << ")\n";
break;
case ChangeKind::FavoredConstraint:
out << "(FavoredConstraint ";
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
indent + 2);
out << ")\n";
break;
case ChangeKind::RecordedResultBuilderTransform:
out << "(RecordedResultBuilderTransform ";
simple_display(out, TheRef);
out << ")\n";
break;
case ChangeKind::RecordedContextualInfo:
// FIXME: Print short form of ASTNode
out << "(RecordedContextualInfo)\n";
break;
case ChangeKind::RecordedTarget:
out << "(RecordedTarget ";
getSyntacticElementTargetKey().dump(out);
out << ")\n";
break;
case ChangeKind::RecordedCaseLabelItemInfo:
// FIXME: Print something here
out << "(RecordedCaseLabelItemInfo)\n";
break;
case ChangeKind::RecordedPotentialThrowSite:
// FIXME: Print something here
out << "(RecordedPotentialThrowSite)\n";
break;
case ChangeKind::RecordedIsolatedParam:
out << "(RecordedIsolatedParam ";
TheParam->dumpRef(out);
out << ")\n";
break;
case ChangeKind::RecordedKeyPath:
out << "(RecordedKeyPath ";
simple_display(out, KeyPath.Expr);
out << ")\n";
break;
}
}
void SolverTrail::recordChange(Change change) {
LLVM_DEBUG(llvm::dbgs() << "+ "; change.dump(llvm::dbgs(), CS, 0););
ASSERT(!UndoActive);
Changes.push_back(change);
++Profile[unsigned(change.Kind)];
++Total;
if (Changes.size() > Max)
Max = Changes.size();
}
void SolverTrail::undo(unsigned toIndex) {
// Don't attempt to rollback if constraint system ended up
// in an invalid state.
if (CS.inInvalidState())
return;
auto dumpHistogram = [&]() {
#define CHANGE(Name) \
if (auto count = Profile[unsigned(ChangeKind::Name)]) \
llvm::dbgs() << "* " << #Name << ": " << count << "\n";
#include "swift/Sema/CSTrail.def"
};
LLVM_DEBUG(llvm::dbgs() << "decisions " << Changes.size()
<< " max " << Max
<< " total " << Total << "\n";
dumpHistogram();
llvm::dbgs() << "\n");
ASSERT(Changes.size() >= toIndex && "Trail corrupted");
ASSERT(!UndoActive);
UndoActive = true;
// FIXME: Undo all changes in the correct order!
for (unsigned i = Changes.size(); i > toIndex; i--) {
auto change = Changes[i - 1];
if (change.Kind == ChangeKind::UpdatedTypeVariable) {
LLVM_DEBUG(llvm::dbgs() << "- "; change.dump(llvm::dbgs(), CS, 0));
change.undo(CS);
}
}
for (unsigned i = Changes.size(); i > toIndex; i--) {
auto change = Changes[i - 1];
if (change.Kind != ChangeKind::UpdatedTypeVariable) {
LLVM_DEBUG(llvm::dbgs() << "- "; change.dump(llvm::dbgs(), CS, 0));
change.undo(CS);
}
}
Changes.resize(toIndex);
UndoActive = false;
}
void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
unsigned fromIndex,
unsigned indent) const {
out.indent(indent);
out << "(changes:\n";
for (unsigned i = fromIndex; i < Changes.size(); ++i)
Changes[i].dump(out, CS, indent + 2);
out.indent(indent);
out << ")\n";
}