Skip to content

Commit 7ea9750

Browse files
committed
Sema: Record ASTNode types in the trail
1 parent 7f597c6 commit 7ea9750

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ class SolverTrail {
7676
/// Recorded the mapping from a pack element expression to its parent
7777
/// pack expansion expression.
7878
RecordedPackEnvironment,
79-
/// Record a defaulted constraint at a locator.
79+
/// Recorded a defaulted constraint at a locator.
8080
RecordedDefaultedConstraint,
81+
/// Recorded an assignment of a type to an AST node.
82+
RecordedNodeType,
8183
};
8284

8385
/// A change made to the constraint system.
@@ -142,6 +144,11 @@ class SolverTrail {
142144
Type ReqTy;
143145
} FixedRequirement;
144146

147+
struct {
148+
ASTNode Node;
149+
Type OldType;
150+
} Node;
151+
145152
ConstraintLocator *Locator;
146153
PackExpansionType *ExpansionTy;
147154
PackElementExpr *ElementExpr;
@@ -221,6 +228,9 @@ class SolverTrail {
221228
/// Create a change that recorded a defaulted constraint at a locator.
222229
static Change recordedDefaultedConstraint(ConstraintLocator *locator);
223230

231+
/// Create a change that recorded an assignment of a type to an AST node.
232+
static Change recordedNodeType(ASTNode node, Type oldType);
233+
224234
/// Undo this change, reverting the constraint graph to the state it
225235
/// had prior to this change.
226236
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,11 +2256,7 @@ class ConstraintSystem {
22562256
/// nodes themselves. This allows us to typecheck and
22572257
/// run through various diagnostics passes without actually mutating
22582258
/// the types on the nodes.
2259-
llvm::MapVector<ASTNode, Type> NodeTypes;
2260-
2261-
/// The nodes for which we have produced types, along with the prior type
2262-
/// each node had before introducing this type.
2263-
llvm::SmallVector<std::pair<ASTNode, Type>, 8> addedNodeTypes;
2259+
llvm::DenseMap<ASTNode, Type> NodeTypes;
22642260

22652261
/// Maps components in a key path expression to their type. Needed because
22662262
/// KeyPathExpr + Index isn't an \c ASTNode and thus can't be stored in \c
@@ -2894,8 +2890,6 @@ class ConstraintSystem {
28942890
/// FIXME: Remove this.
28952891
unsigned numFixes;
28962892

2897-
unsigned numAddedNodeTypes;
2898-
28992893
unsigned numAddedKeyPathComponentTypes;
29002894

29012895
unsigned numDisabledConstraints;
@@ -3205,21 +3199,39 @@ class ConstraintSystem {
32053199
this->FavoredTypes[E] = T;
32063200
}
32073201

3208-
/// Set the type in our type map for the given node.
3202+
/// Set the type in our type map for the given node, and record the change
3203+
/// in the trail.
32093204
///
3210-
/// The side tables are used through the expression type checker to avoid mutating nodes until
3211-
/// we know we have successfully type-checked them.
3205+
/// The side tables are used through the expression type checker to avoid
3206+
/// mutating nodes until we know we have successfully type-checked them.
32123207
void setType(ASTNode node, Type type) {
3213-
assert(!node.isNull() && "Cannot set type information on null node");
3214-
assert(type && "Expected non-null type");
3208+
ASSERT(!node.isNull() && "Cannot set type information on null node");
3209+
ASSERT(type && "Expected non-null type");
32153210

32163211
// Record the type.
32173212
Type &entry = NodeTypes[node];
32183213
Type oldType = entry;
32193214
entry = type;
32203215

3221-
// Record the fact that we ascribed a type to this node.
3222-
addedNodeTypes.push_back({node, oldType});
3216+
if (oldType.getPointer() != type.getPointer()) {
3217+
// Record the fact that we assigned a type to this node.
3218+
if (isRecordingChanges())
3219+
recordChange(SolverTrail::Change::recordedNodeType(node, oldType));
3220+
}
3221+
}
3222+
3223+
/// Undo the above change.
3224+
void restoreType(ASTNode node, Type oldType) {
3225+
ASSERT(!node.isNull() && "Cannot set type information on null node");
3226+
3227+
if (oldType) {
3228+
auto found = NodeTypes.find(node);
3229+
ASSERT(found != NodeTypes.end());
3230+
found->second = oldType;
3231+
} else {
3232+
bool erased = NodeTypes.erase(node);
3233+
ASSERT(erased);
3234+
}
32233235
}
32243236

32253237
/// Set the type in our type map for a given expression. The side

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
663663

664664
numTypeVariables = cs.TypeVariables.size();
665665
numFixes = cs.Fixes.size();
666-
numAddedNodeTypes = cs.addedNodeTypes.size();
667666
numAddedKeyPathComponentTypes = cs.addedKeyPathComponentTypes.size();
668667
numKeyPaths = cs.KeyPaths.size();
669668
numDisabledConstraints = cs.solverState->getNumDisabledConstraints();
@@ -718,17 +717,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
718717
// constraints introduced by the current scope.
719718
cs.solverState->rollback(this);
720719

721-
// Remove any node types we registered.
722-
for (unsigned i :
723-
reverse(range(numAddedNodeTypes, cs.addedNodeTypes.size()))) {
724-
auto node = cs.addedNodeTypes[i].first;
725-
if (Type oldType = cs.addedNodeTypes[i].second)
726-
cs.NodeTypes[node] = oldType;
727-
else
728-
cs.NodeTypes.erase(node);
729-
}
730-
truncate(cs.addedNodeTypes, numAddedNodeTypes);
731-
732720
// Remove any node types we registered.
733721
for (unsigned i : reverse(range(numAddedKeyPathComponentTypes,
734722
cs.addedKeyPathComponentTypes.size()))) {

lib/Sema/CSTrail.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ SolverTrail::Change::recordedDefaultedConstraint(ConstraintLocator *locator) {
225225
return result;
226226
}
227227

228+
SolverTrail::Change
229+
SolverTrail::Change::recordedNodeType(ASTNode node, Type oldType) {
230+
Change result;
231+
result.Kind = ChangeKind::RecordedNodeType;
232+
result.Node.Node = node;
233+
result.Node.OldType = oldType;
234+
return result;
235+
}
236+
228237
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
229238
auto &cg = cs.getConstraintGraph();
230239

@@ -313,6 +322,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
313322
case ChangeKind::RecordedDefaultedConstraint:
314323
cs.removeDefaultedConstraint(Locator);
315324
break;
325+
326+
case ChangeKind::RecordedNodeType:
327+
cs.restoreType(Node.Node, Node.OldType);
328+
break;
316329
}
317330
}
318331

@@ -468,6 +481,7 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
468481
break;
469482

470483
case ChangeKind::RecordedPackEnvironment:
484+
// FIXME: Print short form of PackExpansionExpr
471485
out << "(recorded pack environment)\n";
472486
break;
473487

@@ -476,6 +490,17 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
476490
Locator->dump(&cs.getASTContext().SourceMgr, out);
477491
out << ")\n";
478492
break;
493+
494+
case ChangeKind::RecordedNodeType:
495+
out << "(recorded node type at ";
496+
Node.Node.getStartLoc().print(out, cs.getASTContext().SourceMgr);
497+
out << " previous ";
498+
if (Node.OldType)
499+
Node.OldType->print(out, PO);
500+
else
501+
out << "null";
502+
out << ")\n";
503+
break;
479504
}
480505
}
481506

0 commit comments

Comments
 (0)