Skip to content

Commit 9dea725

Browse files
committed
Sema: Record ASTNode type changes in PreparedOverload
Also, move some methods from ConstraintSystem.h to ConstraintSystem.cpp.
1 parent ad324b7 commit 9dea725

File tree

5 files changed

+115
-73
lines changed

5 files changed

+115
-73
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,7 +3068,7 @@ class ConstraintSystem {
30683068
return nullptr;
30693069
}
30703070

3071-
TypeBase* getFavoredType(Expr *E) {
3071+
TypeBase *getFavoredType(Expr *E) {
30723072
assert(E != nullptr);
30733073
return this->FavoredTypes[E];
30743074
}
@@ -3082,95 +3082,46 @@ class ConstraintSystem {
30823082
///
30833083
/// The side tables are used through the expression type checker to avoid
30843084
/// mutating nodes until we know we have successfully type-checked them.
3085-
void setType(ASTNode node, Type type) {
3086-
ASSERT(!node.isNull() && "Cannot set type information on null node");
3087-
ASSERT(type && "Expected non-null type");
3088-
3089-
// Record the type.
3090-
Type &entry = NodeTypes[node];
3091-
Type oldType = entry;
3092-
entry = type;
3093-
3094-
if (oldType.getPointer() != type.getPointer()) {
3095-
// Record the fact that we assigned a type to this node.
3096-
if (solverState)
3097-
recordChange(SolverTrail::Change::RecordedNodeType(node, oldType));
3098-
}
3099-
}
3085+
void setType(ASTNode node, Type type,
3086+
PreparedOverloadBuilder *preparedOverload=nullptr);
31003087

31013088
/// Undo the above change.
3102-
void restoreType(ASTNode node, Type oldType) {
3103-
ASSERT(!node.isNull() && "Cannot set type information on null node");
3104-
3105-
if (oldType) {
3106-
auto found = NodeTypes.find(node);
3107-
ASSERT(found != NodeTypes.end());
3108-
found->second = oldType;
3109-
} else {
3110-
bool erased = NodeTypes.erase(node);
3111-
ASSERT(erased);
3112-
}
3113-
}
3089+
void restoreType(ASTNode node, Type oldType);
31143090

31153091
/// Check to see if we have a type for a node.
31163092
bool hasType(ASTNode node) const {
3117-
assert(!node.isNull() && "Expected non-null node");
3093+
ASSERT(!node.isNull() && "Expected non-null node");
31183094
return NodeTypes.count(node) > 0;
31193095
}
31203096

31213097
/// Set the type in our type map for a given expression. The side
31223098
/// map is used throughout the expression type checker in order to
31233099
/// avoid mutating expressions until we know we have successfully
31243100
/// type-checked them.
3125-
void setType(const KeyPathExpr *KP, unsigned I, Type T) {
3126-
ASSERT(KP && "Expected non-null key path parameter!");
3127-
ASSERT(T && "Expected non-null type!");
3101+
void setType(const KeyPathExpr *KP, unsigned I, Type T);
31283102

3129-
Type &entry = KeyPathComponentTypes[{KP, I}];
3130-
Type oldType = entry;
3131-
entry = T;
3132-
3133-
if (oldType.getPointer() != T.getPointer()) {
3134-
if (solverState) {
3135-
recordChange(
3136-
SolverTrail::Change::RecordedKeyPathComponentType(
3137-
KP, I, oldType));
3138-
}
3139-
}
3140-
}
3141-
3142-
void restoreType(const KeyPathExpr *KP, unsigned I, Type T) {
3143-
ASSERT(KP && "Expected non-null key path parameter!");
3144-
3145-
if (T) {
3146-
auto found = KeyPathComponentTypes.find({KP, I});
3147-
ASSERT(found != KeyPathComponentTypes.end());
3148-
found->second = T;
3149-
} else {
3150-
bool erased = KeyPathComponentTypes.erase({KP, I});
3151-
ASSERT(erased);
3152-
}
3153-
}
3103+
void restoreType(const KeyPathExpr *KP, unsigned I, Type T);
31543104

31553105
bool hasType(const KeyPathExpr *KP, unsigned I) const {
3156-
assert(KP && "Expected non-null key path parameter!");
3106+
ASSERT(KP && "Expected non-null key path parameter!");
31573107
return KeyPathComponentTypes.find(std::make_pair(KP, I))
31583108
!= KeyPathComponentTypes.end();
31593109
}
31603110

31613111
/// Get the type for an node.
31623112
Type getType(ASTNode node) const {
3163-
assert(hasType(node) && "Expected type to have been set!");
3164-
// FIXME: lvalue differences
3165-
// assert((!E->getType() ||
3166-
// E->getType()->isEqual(ExprTypes.find(E)->second)) &&
3167-
// "Mismatched types!");
3168-
return NodeTypes.find(node)->second;
3113+
ASSERT(!node.isNull() && "Expected non-null node");
3114+
auto found = NodeTypes.find(node);
3115+
ASSERT(found != NodeTypes.end() && "Expected type to have been set!");
3116+
return found->second;
31693117
}
31703118

31713119
Type getType(const KeyPathExpr *KP, unsigned I) const {
3172-
assert(hasType(KP, I) && "Expected type to have been set!");
3173-
return KeyPathComponentTypes.find(std::make_pair(KP, I))->second;
3120+
ASSERT(KP && "Expected non-null key path parameter!");
3121+
auto found = KeyPathComponentTypes.find(std::make_pair(KP, I));
3122+
ASSERT(found != KeyPathComponentTypes.end() &&
3123+
"Expected type to have been set!");
3124+
return found->second;
31743125
}
31753126

31763127
/// Retrieve the type of the node, if known.
@@ -3182,11 +3133,6 @@ class ConstraintSystem {
31823133
return known->second;
31833134
}
31843135

3185-
/// Retrieve type of the given declaration to be used in
3186-
/// constraint system, this is better than calling `getType()`
3187-
/// directly because it accounts of constraint system flags.
3188-
Type getVarType(const VarDecl *var);
3189-
31903136
/// Cache the type of the expression argument and return that same
31913137
/// argument.
31923138
template <typename T>

include/swift/Sema/PreparedOverload.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ struct PreparedOverloadChange {
8888
AppliedPropertyWrapper,
8989

9090
/// A fix was recorded because a property wrapper application failed.
91-
AddedFix
91+
AddedFix,
92+
93+
/// The type of an AST node was changed.
94+
RecordedNodeType
9295
};
9396

9497
/// The kind of change.
@@ -132,7 +135,17 @@ struct PreparedOverloadChange {
132135
ConstraintFix *TheFix;
133136
unsigned Impact;
134137
} Fix;
138+
139+
/// For ChangeKind::RecordedNodeType.
140+
struct {
141+
ASTNode Node;
142+
Type TheType;
143+
} Node;
135144
};
145+
146+
PreparedOverloadChange()
147+
: Kind(ChangeKind::AddedTypeVariable),
148+
TypeVar(nullptr) { }
136149
};
137150

138151
/// A "pre-cooked" representation of all type variables and constraints
@@ -238,6 +251,14 @@ struct PreparedOverloadBuilder {
238251
change.Fix.Impact = impact;
239252
Changes.push_back(change);
240253
}
254+
255+
void recordedNodeType(ASTNode node, Type type) {
256+
PreparedOverload::Change change;
257+
change.Kind = PreparedOverload::Change::RecordedNodeType;
258+
change.Node.Node = node;
259+
change.Node.TheType = type;
260+
Changes.push_back(change);
261+
}
241262
};
242263

243264
} // end namespace constraints

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,9 @@ namespace {
28112811
// interface type request via `var->getType()` that would
28122812
// attempt to validate `weak` attribute, and produce a
28132813
// diagnostic in the middle of the solver path.
2814+
//
2815+
// FIXME: getVarType() is now gone. Is the above still
2816+
// relevant?
28142817

28152818
CS.addConstraint(ConstraintKind::OneWayEqual, oneWayVarType,
28162819
varType, locator);
@@ -5241,7 +5244,8 @@ ConstraintSystem::applyPropertyWrapperToParameter(
52415244
locator,
52425245
/*isFavored=*/false,
52435246
preparedOverload);
5244-
setType(param->getPropertyWrapperWrappedValueVar(), wrappedValueType);
5247+
setType(param->getPropertyWrapperWrappedValueVar(), wrappedValueType,
5248+
preparedOverload);
52455249

52465250
applyPropertyWrapper(anchor,
52475251
{ wrapperType, PropertyWrapperInitKind::WrappedValue },

lib/Sema/ConstraintSystem.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,73 @@ ConstraintLocator *ConstraintSystem::getOpenOpaqueLocator(
905905
{ LocatorPathElt::OpenedOpaqueArchetype(opaqueDecl) }, 0);
906906
}
907907

908+
void ConstraintSystem::setType(ASTNode node, Type type,
909+
PreparedOverloadBuilder *preparedOverload) {
910+
ASSERT(PreparingOverload == !!preparedOverload);
911+
912+
ASSERT(!node.isNull() && "Cannot set type information on null node");
913+
ASSERT(type && "Expected non-null type");
914+
915+
if (preparedOverload) {
916+
preparedOverload->recordedNodeType(node, type);
917+
return;
918+
}
919+
920+
// Record the type.
921+
Type &entry = NodeTypes[node];
922+
Type oldType = entry;
923+
entry = type;
924+
925+
if (oldType.getPointer() != type.getPointer()) {
926+
// Record the fact that we assigned a type to this node.
927+
if (solverState)
928+
recordChange(SolverTrail::Change::RecordedNodeType(node, oldType));
929+
}
930+
}
931+
932+
void ConstraintSystem::restoreType(ASTNode node, Type oldType) {
933+
ASSERT(!node.isNull() && "Cannot set type information on null node");
934+
935+
if (oldType) {
936+
auto found = NodeTypes.find(node);
937+
ASSERT(found != NodeTypes.end());
938+
found->second = oldType;
939+
} else {
940+
bool erased = NodeTypes.erase(node);
941+
ASSERT(erased);
942+
}
943+
}
944+
945+
void ConstraintSystem::setType(const KeyPathExpr *KP, unsigned I, Type T) {
946+
ASSERT(KP && "Expected non-null key path parameter!");
947+
ASSERT(T && "Expected non-null type!");
948+
949+
Type &entry = KeyPathComponentTypes[{KP, I}];
950+
Type oldType = entry;
951+
entry = T;
952+
953+
if (oldType.getPointer() != T.getPointer()) {
954+
if (solverState) {
955+
recordChange(
956+
SolverTrail::Change::RecordedKeyPathComponentType(
957+
KP, I, oldType));
958+
}
959+
}
960+
}
961+
962+
void ConstraintSystem::restoreType(const KeyPathExpr *KP, unsigned I, Type T) {
963+
ASSERT(KP && "Expected non-null key path parameter!");
964+
965+
if (T) {
966+
auto found = KeyPathComponentTypes.find({KP, I});
967+
ASSERT(found != KeyPathComponentTypes.end());
968+
found->second = T;
969+
} else {
970+
bool erased = KeyPathComponentTypes.erase({KP, I});
971+
ASSERT(erased);
972+
}
973+
}
974+
908975
std::pair<Type, ExistentialArchetypeType *>
909976
ConstraintSystem::openAnyExistentialType(Type type,
910977
ConstraintLocator *locator) {

lib/Sema/TypeOfReference.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,10 @@ void ConstraintSystem::replayChanges(
28342834
case PreparedOverload::Change::AddedFix:
28352835
recordFix(change.Fix.TheFix, change.Fix.Impact);
28362836
break;
2837+
2838+
case PreparedOverload::Change::RecordedNodeType:
2839+
setType(change.Node.Node, change.Node.TheType);
2840+
break;
28372841
}
28382842
}
28392843
}

0 commit comments

Comments
 (0)