Skip to content

Commit d983751

Browse files
Christian Altamiranocommit-bot@chromium.org
authored andcommitted
[dart2js] Refactor a few methods for type masks
Pass the CommonMasks abstract value domain instead of the JClosedWorld to TypeMask methods that need access to *their* abstract value domain. It is wrong to get the abstract value domain from the JClosedWorld because when we try to wrap abstract values, the abstract value domain in the JClosedWorld is the wrapper domain, not the domain being wrapped. Change-Id: I5bbd0f4c56abe45714dd0a2657f73ef240efae0e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155640 Commit-Queue: Christian Altamirano <[email protected]> Reviewed-by: Stephen Adams <[email protected]>
1 parent fbe33e8 commit d983751

21 files changed

+184
-179
lines changed

pkg/compiler/lib/src/inferrer/type_graph_nodes.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,26 +1239,24 @@ class DynamicCallSiteTypeInformation<T> extends CallSiteTypeInformation {
12391239
/// library code.
12401240
TypeInformation handleIntrisifiedSelector(
12411241
Selector selector, AbstractValue mask, InferrerEngine inferrer) {
1242-
JClosedWorld closedWorld = inferrer.closedWorld;
1242+
AbstractValueDomain abstractValueDomain = inferrer.abstractValueDomain;
12431243
if (mask == null) return null;
1244-
if (inferrer.abstractValueDomain.isIntegerOrNull(mask).isPotentiallyFalse) {
1244+
if (abstractValueDomain.isIntegerOrNull(mask).isPotentiallyFalse) {
12451245
return null;
12461246
}
12471247
if (!selector.isCall && !selector.isOperator) return null;
12481248
if (!arguments.named.isEmpty) return null;
12491249
if (arguments.positional.length > 1) return null;
12501250

1251-
ClassEntity uint31Implementation = closedWorld.commonElements.jsUInt31Class;
1252-
bool isInt(info) => info.type.containsOnlyInt(closedWorld);
1253-
bool isEmpty(info) => info.type.isEmpty;
1254-
bool isUInt31(info) {
1255-
return info.type.satisfies(uint31Implementation, closedWorld);
1256-
}
1257-
1258-
bool isPositiveInt(info) {
1259-
return info.type.satisfies(
1260-
closedWorld.commonElements.jsPositiveIntClass, closedWorld);
1261-
}
1251+
bool isInt(info) =>
1252+
abstractValueDomain.isIntegerOrNull(info.type).isDefinitelyTrue;
1253+
bool isEmpty(info) =>
1254+
abstractValueDomain.isEmpty(info.type).isDefinitelyTrue;
1255+
bool isUInt31(info) => abstractValueDomain
1256+
.isUInt31(abstractValueDomain.excludeNull(info.type))
1257+
.isDefinitelyTrue;
1258+
bool isPositiveInt(info) =>
1259+
abstractValueDomain.isPositiveIntegerOrNull(info.type).isDefinitelyTrue;
12621260

12631261
TypeInformation tryLater() => inferrer.types.nonNullEmptyType;
12641262

pkg/compiler/lib/src/inferrer/typemasks/container_type_mask.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ class ContainerTypeMask extends AllocationTypeMask {
8888
}
8989

9090
@override
91-
TypeMask intersection(TypeMask other, JClosedWorld closedWorld) {
92-
TypeMask forwardIntersection = forwardTo.intersection(other, closedWorld);
91+
TypeMask intersection(TypeMask other, CommonMasks domain) {
92+
TypeMask forwardIntersection = forwardTo.intersection(other, domain);
9393
if (forwardIntersection.isEmptyOrNull) return forwardIntersection;
9494
return forwardIntersection.isNullable ? nullable() : nonNullable();
9595
}
9696

9797
@override
98-
TypeMask union(dynamic other, JClosedWorld closedWorld) {
98+
TypeMask union(dynamic other, CommonMasks domain) {
9999
if (this == other) {
100100
return this;
101101
} else if (equalsDisregardNull(other)) {
@@ -105,10 +105,9 @@ class ContainerTypeMask extends AllocationTypeMask {
105105
} else if (other.isContainer &&
106106
elementType != null &&
107107
other.elementType != null) {
108-
TypeMask newElementType =
109-
elementType.union(other.elementType, closedWorld);
108+
TypeMask newElementType = elementType.union(other.elementType, domain);
110109
int newLength = (length == other.length) ? length : null;
111-
TypeMask newForwardTo = forwardTo.union(other.forwardTo, closedWorld);
110+
TypeMask newForwardTo = forwardTo.union(other.forwardTo, domain);
112111
return new ContainerTypeMask(
113112
newForwardTo,
114113
allocationNode == other.allocationNode ? allocationNode : null,
@@ -118,7 +117,7 @@ class ContainerTypeMask extends AllocationTypeMask {
118117
newElementType,
119118
newLength);
120119
} else {
121-
return forwardTo.union(other, closedWorld);
120+
return forwardTo.union(other, domain);
122121
}
123122
}
124123

pkg/compiler/lib/src/inferrer/typemasks/dictionary_type_mask.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,24 @@ class DictionaryTypeMask extends MapTypeMask {
9797
}
9898

9999
@override
100-
TypeMask intersection(TypeMask other, JClosedWorld closedWorld) {
101-
TypeMask forwardIntersection = forwardTo.intersection(other, closedWorld);
100+
TypeMask intersection(TypeMask other, CommonMasks domain) {
101+
TypeMask forwardIntersection = forwardTo.intersection(other, domain);
102102
if (forwardIntersection.isEmptyOrNull) return forwardIntersection;
103103
return forwardIntersection.isNullable ? nullable() : nonNullable();
104104
}
105105

106106
@override
107-
TypeMask union(dynamic other, JClosedWorld closedWorld) {
107+
TypeMask union(dynamic other, CommonMasks domain) {
108108
if (this == other) {
109109
return this;
110110
} else if (equalsDisregardNull(other)) {
111111
return other.isNullable ? other : this;
112112
} else if (other.isEmptyOrNull) {
113113
return other.isNullable ? this.nullable() : this;
114114
} else if (other.isDictionary) {
115-
TypeMask newForwardTo = forwardTo.union(other.forwardTo, closedWorld);
116-
TypeMask newKeyType = keyType.union(other.keyType, closedWorld);
117-
TypeMask newValueType = valueType.union(other.valueType, closedWorld);
115+
TypeMask newForwardTo = forwardTo.union(other.forwardTo, domain);
116+
TypeMask newKeyType = keyType.union(other.keyType, domain);
117+
TypeMask newValueType = valueType.union(other.valueType, domain);
118118
Map<String, TypeMask> mappings = <String, TypeMask>{};
119119
_typeMap.forEach((k, dynamic v) {
120120
if (!other._typeMap.containsKey(k)) {
@@ -123,7 +123,7 @@ class DictionaryTypeMask extends MapTypeMask {
123123
});
124124
other._typeMap.forEach((k, v) {
125125
if (_typeMap.containsKey(k)) {
126-
mappings[k] = v.union(_typeMap[k], closedWorld);
126+
mappings[k] = v.union(_typeMap[k], domain);
127127
} else {
128128
mappings[k] = v.nullable();
129129
}
@@ -133,13 +133,13 @@ class DictionaryTypeMask extends MapTypeMask {
133133
} else if (other.isMap &&
134134
(other.keyType != null) &&
135135
(other.valueType != null)) {
136-
TypeMask newForwardTo = forwardTo.union(other.forwardTo, closedWorld);
137-
TypeMask newKeyType = keyType.union(other.keyType, closedWorld);
138-
TypeMask newValueType = valueType.union(other.valueType, closedWorld);
136+
TypeMask newForwardTo = forwardTo.union(other.forwardTo, domain);
137+
TypeMask newKeyType = keyType.union(other.keyType, domain);
138+
TypeMask newValueType = valueType.union(other.valueType, domain);
139139
return new MapTypeMask(
140140
newForwardTo, null, null, newKeyType, newValueType);
141141
} else {
142-
return forwardTo.union(other, closedWorld);
142+
return forwardTo.union(other, domain);
143143
}
144144
}
145145

pkg/compiler/lib/src/inferrer/typemasks/flat_type_mask.dart

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,24 @@ class FlatTypeMask implements TypeMask {
5353
/// Ensures that the generated mask is normalized, i.e., a call to
5454
/// [TypeMask.assertIsNormalized] with the factory's result returns `true`.
5555
factory FlatTypeMask.normalized(
56-
ClassEntity base, int flags, JClosedWorld world) {
57-
if (base == world.commonElements.nullClass) {
56+
ClassEntity base, int flags, CommonMasks domain) {
57+
if (base == domain.commonElements.nullClass) {
5858
return FlatTypeMask.empty();
5959
}
6060
if ((flags >> 1) == EMPTY || ((flags >> 1) == EXACT)) {
6161
return new FlatTypeMask._(base, flags);
6262
}
6363
if ((flags >> 1) == SUBTYPE) {
64-
if (!world.classHierarchy.hasAnyStrictSubtype(base) ||
65-
world.classHierarchy.hasOnlySubclasses(base)) {
64+
if (!domain._closedWorld.classHierarchy.hasAnyStrictSubtype(base) ||
65+
domain._closedWorld.classHierarchy.hasOnlySubclasses(base)) {
6666
flags = (flags & 0x1) | (SUBCLASS << 1);
6767
}
6868
}
6969
if (((flags >> 1) == SUBCLASS) &&
70-
!world.classHierarchy.hasAnyStrictSubclass(base)) {
70+
!domain._closedWorld.classHierarchy.hasAnyStrictSubclass(base)) {
7171
flags = (flags & 0x1) | (EXACT << 1);
7272
}
73-
CommonMasks commonMasks = world.abstractValueDomain;
74-
return commonMasks.getCachedMask(
73+
return domain.getCachedMask(
7574
base, flags, () => new FlatTypeMask._(base, flags));
7675
}
7776

@@ -298,37 +297,38 @@ class FlatTypeMask implements TypeMask {
298297
}
299298

300299
@override
301-
TypeMask union(TypeMask other, JClosedWorld closedWorld) {
300+
TypeMask union(TypeMask other, CommonMasks domain) {
301+
JClosedWorld closedWorld = domain._closedWorld;
302302
assert(other != null);
303303
assert(TypeMask.assertIsNormalized(this, closedWorld));
304304
assert(TypeMask.assertIsNormalized(other, closedWorld));
305-
if (other is! FlatTypeMask) return other.union(this, closedWorld);
305+
if (other is! FlatTypeMask) return other.union(this, domain);
306306
FlatTypeMask flatOther = other;
307307
if (isEmptyOrNull) {
308308
return isNullable ? flatOther.nullable() : flatOther;
309309
} else if (flatOther.isEmptyOrNull) {
310310
return flatOther.isNullable ? nullable() : this;
311311
} else if (base == flatOther.base) {
312-
return unionSame(flatOther, closedWorld);
312+
return unionSame(flatOther, domain);
313313
} else if (closedWorld.classHierarchy.isSubclassOf(flatOther.base, base)) {
314-
return unionStrictSubclass(flatOther, closedWorld);
314+
return unionStrictSubclass(flatOther, domain);
315315
} else if (closedWorld.classHierarchy.isSubclassOf(base, flatOther.base)) {
316-
return flatOther.unionStrictSubclass(this, closedWorld);
316+
return flatOther.unionStrictSubclass(this, domain);
317317
} else if (closedWorld.classHierarchy.isSubtypeOf(flatOther.base, base)) {
318-
return unionStrictSubtype(flatOther, closedWorld);
318+
return unionStrictSubtype(flatOther, domain);
319319
} else if (closedWorld.classHierarchy.isSubtypeOf(base, flatOther.base)) {
320-
return flatOther.unionStrictSubtype(this, closedWorld);
320+
return flatOther.unionStrictSubtype(this, domain);
321321
} else {
322322
return new UnionTypeMask._internal(
323323
<FlatTypeMask>[this.nonNullable(), flatOther.nonNullable()],
324324
isNullable || other.isNullable);
325325
}
326326
}
327327

328-
TypeMask unionSame(FlatTypeMask other, JClosedWorld closedWorld) {
328+
TypeMask unionSame(FlatTypeMask other, CommonMasks domain) {
329329
assert(base == other.base);
330-
assert(TypeMask.assertIsNormalized(this, closedWorld));
331-
assert(TypeMask.assertIsNormalized(other, closedWorld));
330+
assert(TypeMask.assertIsNormalized(this, domain._closedWorld));
331+
assert(TypeMask.assertIsNormalized(other, domain._closedWorld));
332332
// The two masks share the base type, so we must chose the least
333333
// constraining kind (the highest) of the two. If either one of
334334
// the masks are nullable the result should be nullable too.
@@ -341,18 +341,18 @@ class FlatTypeMask implements TypeMask {
341341
} else if (other.flags == combined) {
342342
return other;
343343
} else {
344-
return new FlatTypeMask.normalized(base, combined, closedWorld);
344+
return new FlatTypeMask.normalized(base, combined, domain);
345345
}
346346
}
347347

348-
TypeMask unionStrictSubclass(FlatTypeMask other, JClosedWorld closedWorld) {
348+
TypeMask unionStrictSubclass(FlatTypeMask other, CommonMasks domain) {
349349
assert(base != other.base);
350-
assert(closedWorld.classHierarchy.isSubclassOf(other.base, base));
351-
assert(TypeMask.assertIsNormalized(this, closedWorld));
352-
assert(TypeMask.assertIsNormalized(other, closedWorld));
350+
assert(domain._closedWorld.classHierarchy.isSubclassOf(other.base, base));
351+
assert(TypeMask.assertIsNormalized(this, domain._closedWorld));
352+
assert(TypeMask.assertIsNormalized(other, domain._closedWorld));
353353
int combined;
354354
if ((isExact && other.isExact) ||
355-
base == closedWorld.commonElements.objectClass) {
355+
base == domain.commonElements.objectClass) {
356356
// Since the other mask is a subclass of this mask, we need the
357357
// resulting union to be a subclass too. If either one of the
358358
// masks are nullable the result should be nullable too.
@@ -368,33 +368,33 @@ class FlatTypeMask implements TypeMask {
368368
// If we weaken the constraint on this type, we have to make sure that
369369
// the result is normalized.
370370
return (flags != combined)
371-
? new FlatTypeMask.normalized(base, combined, closedWorld)
371+
? new FlatTypeMask.normalized(base, combined, domain)
372372
: this;
373373
}
374374

375-
TypeMask unionStrictSubtype(FlatTypeMask other, JClosedWorld closedWorld) {
375+
TypeMask unionStrictSubtype(FlatTypeMask other, CommonMasks domain) {
376376
assert(base != other.base);
377-
assert(!closedWorld.classHierarchy.isSubclassOf(other.base, base));
378-
assert(closedWorld.classHierarchy.isSubtypeOf(other.base, base));
379-
assert(TypeMask.assertIsNormalized(this, closedWorld));
380-
assert(TypeMask.assertIsNormalized(other, closedWorld));
377+
assert(!domain._closedWorld.classHierarchy.isSubclassOf(other.base, base));
378+
assert(domain._closedWorld.classHierarchy.isSubtypeOf(other.base, base));
379+
assert(TypeMask.assertIsNormalized(this, domain._closedWorld));
380+
assert(TypeMask.assertIsNormalized(other, domain._closedWorld));
381381
// Since the other mask is a subtype of this mask, we need the
382382
// resulting union to be a subtype too. If either one of the masks
383383
// are nullable the result should be nullable too.
384384
int combined = (SUBTYPE << 1) | ((flags | other.flags) & 1);
385385
// We know there is at least one subtype, [other.base], so no need
386386
// to normalize.
387387
return (flags != combined)
388-
? new FlatTypeMask.normalized(base, combined, closedWorld)
388+
? new FlatTypeMask.normalized(base, combined, domain)
389389
: this;
390390
}
391391

392392
@override
393-
TypeMask intersection(TypeMask other, JClosedWorld closedWorld) {
393+
TypeMask intersection(TypeMask other, CommonMasks domain) {
394394
assert(other != null);
395-
if (other is! FlatTypeMask) return other.intersection(this, closedWorld);
396-
assert(TypeMask.assertIsNormalized(this, closedWorld));
397-
assert(TypeMask.assertIsNormalized(other, closedWorld));
395+
if (other is! FlatTypeMask) return other.intersection(this, domain);
396+
assert(TypeMask.assertIsNormalized(this, domain._closedWorld));
397+
assert(TypeMask.assertIsNormalized(other, domain._closedWorld));
398398
FlatTypeMask flatOther = other;
399399

400400
ClassEntity otherBase = flatOther.base;
@@ -407,14 +407,12 @@ class FlatTypeMask implements TypeMask {
407407
return includeNull ? other : other.nonNullable();
408408
}
409409

410-
SubclassResult result = closedWorld.classHierarchy
410+
SubclassResult result = domain._closedWorld.classHierarchy
411411
.commonSubclasses(base, _classQuery, otherBase, flatOther._classQuery);
412412

413413
switch (result.kind) {
414414
case SubclassResultKind.EMPTY:
415-
return includeNull
416-
? closedWorld.abstractValueDomain.nullType
417-
: closedWorld.abstractValueDomain.emptyType;
415+
return includeNull ? domain.nullType : domain.emptyType;
418416
case SubclassResultKind.EXACT1:
419417
assert(isExact);
420418
return includeNull ? this : nonNullable();
@@ -436,20 +434,19 @@ class FlatTypeMask implements TypeMask {
436434
case SubclassResultKind.SET:
437435
default:
438436
if (result.classes.isEmpty) {
439-
return includeNull
440-
? closedWorld.abstractValueDomain.nullType
441-
: closedWorld.abstractValueDomain.emptyType;
437+
return includeNull ? domain.nullType : domain.emptyType;
442438
} else if (result.classes.length == 1) {
443439
ClassEntity cls = result.classes.first;
444440
return includeNull
445-
? new TypeMask.subclass(cls, closedWorld)
446-
: new TypeMask.nonNullSubclass(cls, closedWorld);
441+
? new TypeMask.subclass(cls, domain._closedWorld)
442+
: new TypeMask.nonNullSubclass(cls, domain._closedWorld);
447443
}
448444

449445
List<FlatTypeMask> masks = List.from(result.classes.map(
450-
(ClassEntity cls) => TypeMask.nonNullSubclass(cls, closedWorld)));
446+
(ClassEntity cls) =>
447+
TypeMask.nonNullSubclass(cls, domain._closedWorld)));
451448
if (masks.length > UnionTypeMask.MAX_UNION_LENGTH) {
452-
return UnionTypeMask.flatten(masks, includeNull, closedWorld);
449+
return UnionTypeMask.flatten(masks, includeNull, domain);
453450
}
454451
return new UnionTypeMask._internal(masks, includeNull);
455452
}
@@ -501,7 +498,7 @@ class FlatTypeMask implements TypeMask {
501498
return true;
502499
}
503500

504-
TypeMask intersectionSame(FlatTypeMask other, JClosedWorld closedWorld) {
501+
TypeMask intersectionSame(FlatTypeMask other, CommonMasks domain) {
505502
assert(base == other.base);
506503
// The two masks share the base type, so we must chose the most
507504
// constraining kind (the lowest) of the two. Only if both masks
@@ -515,14 +512,13 @@ class FlatTypeMask implements TypeMask {
515512
} else if (other.flags == combined) {
516513
return other;
517514
} else {
518-
return new FlatTypeMask.normalized(base, combined, closedWorld);
515+
return new FlatTypeMask.normalized(base, combined, domain);
519516
}
520517
}
521518

522-
TypeMask intersectionStrictSubclass(
523-
FlatTypeMask other, JClosedWorld closedWorld) {
519+
TypeMask intersectionStrictSubclass(FlatTypeMask other, CommonMasks domain) {
524520
assert(base != other.base);
525-
assert(closedWorld.classHierarchy.isSubclassOf(other.base, base));
521+
assert(domain._closedWorld.classHierarchy.isSubclassOf(other.base, base));
526522
// If this mask isn't at least a subclass mask, then the
527523
// intersection with the other mask is empty.
528524
if (isExact) return intersectionEmpty(other);
@@ -535,7 +531,7 @@ class FlatTypeMask implements TypeMask {
535531
if (other.flags == combined) {
536532
return other;
537533
} else {
538-
return new FlatTypeMask.normalized(other.base, combined, closedWorld);
534+
return new FlatTypeMask.normalized(other.base, combined, domain);
539535
}
540536
}
541537

pkg/compiler/lib/src/inferrer/typemasks/forwarding_type_mask.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ abstract class ForwardingTypeMask implements TypeMask {
9898
}
9999

100100
@override
101-
TypeMask union(other, JClosedWorld closedWorld) {
101+
TypeMask union(other, CommonMasks domain) {
102102
if (this == other) {
103103
return this;
104104
} else if (equalsDisregardNull(other)) {
105105
return other.isNullable ? other : this;
106106
} else if (other.isEmptyOrNull) {
107107
return other.isNullable ? this.nullable() : this;
108108
}
109-
return forwardTo.union(other, closedWorld);
109+
return forwardTo.union(other, domain);
110110
}
111111

112112
@override
@@ -115,8 +115,8 @@ abstract class ForwardingTypeMask implements TypeMask {
115115
}
116116

117117
@override
118-
TypeMask intersection(TypeMask other, JClosedWorld closedWorld) {
119-
return forwardTo.intersection(other, closedWorld);
118+
TypeMask intersection(TypeMask other, CommonMasks domain) {
119+
return forwardTo.intersection(other, domain);
120120
}
121121

122122
@override

0 commit comments

Comments
 (0)