@@ -856,7 +856,7 @@ class Solution {
856
856
llvm::SmallPtrSet<ConstraintLocator *, 2 > DefaultedConstraints;
857
857
858
858
// / The node -> type mappings introduced by this solution.
859
- llvm::MapVector<TypedNode, Type> addedNodeTypes ;
859
+ llvm::MapVector<TypedNode, Type> nodeTypes ;
860
860
861
861
// / Contextual types introduced by this solution.
862
862
std::vector<std::pair<const Expr *, ContextualTypeInfo>> contextualTypes;
@@ -953,8 +953,8 @@ class Solution {
953
953
954
954
// / Retrieve the type of the given node, as recorded in this solution.
955
955
Type getType (TypedNode node) const {
956
- auto known = addedNodeTypes .find (node);
957
- assert (known != addedNodeTypes .end ());
956
+ auto known = nodeTypes .find (node);
957
+ assert (known != nodeTypes .end ());
958
958
return known->second ;
959
959
}
960
960
@@ -1479,15 +1479,12 @@ class ConstraintSystem {
1479
1479
// / from declared parameters/result and body.
1480
1480
llvm::MapVector<const ClosureExpr *, FunctionType *> ClosureTypes;
1481
1481
1482
- // / Maps expression types used within all portions of the constraint
1483
- // / system, instead of directly using the types on the expression
1484
- // / nodes themselves. This allows us to typecheck an expression and
1482
+ // / Maps node types used within all portions of the constraint
1483
+ // / system, instead of directly using the types on the
1484
+ // / nodes themselves. This allows us to typecheck and
1485
1485
// / run through various diagnostics passes without actually mutating
1486
- // / the types on the expression nodes.
1487
- llvm::DenseMap<const Expr *, TypeBase *> ExprTypes;
1488
- llvm::DenseMap<const TypeLoc *, TypeBase *> TypeLocTypes;
1489
- llvm::DenseMap<const VarDecl *, TypeBase *> VarTypes;
1490
- llvm::DenseMap<const Pattern *, TypeBase *> PatternTypes;
1486
+ // / the types on the nodes.
1487
+ llvm::DenseMap<TypedNode, Type> NodeTypes;
1491
1488
llvm::DenseMap<std::pair<const KeyPathExpr *, unsigned >, TypeBase *>
1492
1489
KeyPathComponentTypes;
1493
1490
@@ -1555,7 +1552,8 @@ class ConstraintSystem {
1555
1552
SmallVector<std::pair<ConstraintLocator *, OpenedArchetypeType *>, 4 >
1556
1553
OpenedExistentialTypes;
1557
1554
1558
- // / The node -> type mappings introduced by generating constraints.
1555
+ // / The nodes for which we have produced types, along with the prior type
1556
+ // / each node had before introducing this type.
1559
1557
llvm::SmallVector<std::pair<TypedNode, Type>, 8 > addedNodeTypes;
1560
1558
1561
1559
std::vector<std::pair<ConstraintLocator *, ProtocolConformanceRef>>
@@ -2238,19 +2236,12 @@ class ConstraintSystem {
2238
2236
assert (type && " Expected non-null type" );
2239
2237
2240
2238
// Record the type.
2241
- if (auto expr = node.dyn_cast <const Expr *>()) {
2242
- ExprTypes[expr] = type.getPointer ();
2243
- } else if (auto typeLoc = node.dyn_cast <const TypeLoc *>()) {
2244
- TypeLocTypes[typeLoc] = type.getPointer ();
2245
- } else if (auto var = node.dyn_cast <const VarDecl *>()) {
2246
- VarTypes[var] = type.getPointer ();
2247
- } else {
2248
- auto pattern = node.get <const Pattern *>();
2249
- PatternTypes[pattern] = type.getPointer ();
2250
- }
2239
+ Type &entry = NodeTypes[node];
2240
+ Type oldType = entry;
2241
+ entry = type;
2251
2242
2252
2243
// Record the fact that we ascribed a type to this node.
2253
- addedNodeTypes.push_back ({node, type });
2244
+ addedNodeTypes.push_back ({node, oldType });
2254
2245
}
2255
2246
2256
2247
// / Set the type in our type map for a given expression. The side
@@ -2261,49 +2252,20 @@ class ConstraintSystem {
2261
2252
setType (TypedNode (&L), T);
2262
2253
}
2263
2254
2264
- // / Erase the type for the given node.
2265
- void eraseType (TypedNode node) {
2266
- if (auto expr = node.dyn_cast <const Expr *>()) {
2267
- ExprTypes.erase (expr);
2268
- } else if (auto typeLoc = node.dyn_cast <const TypeLoc *>()) {
2269
- TypeLocTypes.erase (typeLoc);
2270
- } else if (auto var = node.dyn_cast <const VarDecl *>()) {
2271
- VarTypes.erase (var);
2272
- } else {
2273
- auto pattern = node.get <const Pattern *>();
2274
- PatternTypes.erase (pattern);
2275
- }
2276
- }
2277
-
2278
2255
void setType (KeyPathExpr *KP, unsigned I, Type T) {
2279
2256
assert (KP && " Expected non-null key path parameter!" );
2280
2257
assert (T && " Expected non-null type!" );
2281
2258
KeyPathComponentTypes[std::make_pair (KP, I)] = T.getPointer ();
2282
2259
}
2283
2260
2284
- // / Check to see if we have a type for an expression.
2285
- bool hasType (const Expr *E) const {
2286
- assert (E != nullptr && " Expected non-null expression!" );
2287
- return ExprTypes.find (E) != ExprTypes.end ();
2288
- }
2289
-
2290
2261
bool hasType (const TypeLoc &L) const {
2291
2262
return hasType (TypedNode (&L));
2292
2263
}
2293
2264
2294
2265
// / Check to see if we have a type for a node.
2295
2266
bool hasType (TypedNode node) const {
2296
2267
assert (!node.isNull () && " Expected non-null node" );
2297
- if (auto expr = node.dyn_cast <const Expr *>()) {
2298
- return ExprTypes.find (expr) != ExprTypes.end ();
2299
- } else if (auto typeLoc = node.dyn_cast <const TypeLoc *>()) {
2300
- return TypeLocTypes.find (typeLoc) != TypeLocTypes.end ();
2301
- } else if (auto var = node.dyn_cast <const VarDecl *>()) {
2302
- return VarTypes.find (var) != VarTypes.end ();
2303
- } else {
2304
- auto pattern = node.get <const Pattern *>();
2305
- return PatternTypes.find (pattern) != PatternTypes.end ();
2306
- }
2268
+ return NodeTypes.count (node) > 0 ;
2307
2269
}
2308
2270
2309
2271
bool hasType (const KeyPathExpr *KP, unsigned I) const {
@@ -2312,35 +2274,29 @@ class ConstraintSystem {
2312
2274
!= KeyPathComponentTypes.end ();
2313
2275
}
2314
2276
2315
- // / Get the type for an expression .
2316
- Type getType (const Expr *E ) const {
2317
- assert (hasType (E ) && " Expected type to have been set!" );
2277
+ // / Get the type for an node .
2278
+ Type getType (TypedNode node ) const {
2279
+ assert (hasType (node ) && " Expected type to have been set!" );
2318
2280
// FIXME: lvalue differences
2319
2281
// assert((!E->getType() ||
2320
2282
// E->getType()->isEqual(ExprTypes.find(E)->second)) &&
2321
2283
// "Mismatched types!");
2322
- return ExprTypes .find (E )->second ;
2284
+ return NodeTypes .find (node )->second ;
2323
2285
}
2324
2286
2325
2287
Type getType (const TypeLoc &L) const {
2326
- assert (hasType (L) && " Expected type to have been set!" );
2327
- return TypeLocTypes.find (&L)->second ;
2328
- }
2329
-
2330
- Type getType (const VarDecl *VD) const {
2331
- assert (hasType (VD) && " Expected type to have been set!" );
2332
- return VarTypes.find (VD)->second ;
2288
+ return getType (TypedNode (&L));
2333
2289
}
2334
2290
2335
2291
Type getType (const KeyPathExpr *KP, unsigned I) const {
2336
2292
assert (hasType (KP, I) && " Expected type to have been set!" );
2337
2293
return KeyPathComponentTypes.find (std::make_pair (KP, I))->second ;
2338
2294
}
2339
2295
2340
- // / Retrieve the type of the variable , if known.
2341
- Type getTypeIfAvailable (const VarDecl *VD ) const {
2342
- auto known = VarTypes .find (VD );
2343
- if (known == VarTypes .end ())
2296
+ // / Retrieve the type of the node , if known.
2297
+ Type getTypeIfAvailable (TypedNode node ) const {
2298
+ auto known = NodeTypes .find (node );
2299
+ if (known == NodeTypes .end ())
2344
2300
return Type ();
2345
2301
2346
2302
return known->second ;
0 commit comments