Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3e7ce99

Browse files
sjindel-googlecommit-bot@chromium.org
authored andcommitted
[vm/tfa] Implementation of type arguments tracking in TFA.
Change-Id: I9398186e27ae7a040e249df010ae16fb6ab6da89 Cq-Include-Trybots: luci.dart.try:vm-kernel-win-release-x64-try, vm-kernel-optcounter-threshold-linux-release-x64-try, vm-kernel-precomp-linux-debug-x64-try, vm-kernel-precomp-linux-release-simarm-try, vm-kernel-precomp-linux-release-simarm64-try, vm-kernel-precomp-linux-release-x64-try, vm-kernel-precomp-win-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/74962 Commit-Queue: Samir Jindel <sjindel@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
1 parent 001343c commit 3e7ce99

25 files changed

Lines changed: 1757 additions & 182 deletions

pkg/kernel/lib/class_hierarchy.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ abstract class ClassHierarchy {
4242
/// True if the component contains another class that is a subtype of given one.
4343
bool hasProperSubtypes(Class class_);
4444

45+
// Returns the instantition of each generic supertype implemented by this
46+
// class (e.g. getClassAsInstanceOf applied to all superclasses and
47+
// interfaces).
48+
List<Supertype> genericSupertypesOf(Class class_);
49+
4550
/// Returns the least upper bound of two interface types, as defined by Dart
4651
/// 1.0.
4752
///
@@ -699,6 +704,15 @@ class ClosedWorldClassHierarchy implements ClassHierarchy {
699704
info.directMixers.isNotEmpty;
700705
}
701706

707+
@override
708+
List<Supertype> genericSupertypesOf(Class class_) {
709+
final supertypes = _infoFor[class_].genericSuperTypes;
710+
if (supertypes == null) return const <Supertype>[];
711+
// Multiple supertypes can arise from ambiguous supertypes. The first
712+
// supertype is the real one; the others are purely informational.
713+
return supertypes.values.map((v) => v.first).toList();
714+
}
715+
702716
@override
703717
ClassHierarchy applyTreeChanges(Iterable<Library> removedLibraries,
704718
Iterable<Library> ensureKnownLibraries,
@@ -1339,7 +1353,7 @@ class _ClassInfo {
13391353
/// Maps generic supertype classes to the instantiation implemented by this
13401354
/// class.
13411355
///
1342-
/// E.g. `List` maps to `List<String>` for a class that directly of indirectly
1356+
/// E.g. `List` maps to `List<String>` for a class that directly or indirectly
13431357
/// implements `List<String>`.
13441358
Map<Class, List<Supertype>> genericSuperTypes;
13451359

pkg/vm/lib/metadata/inferred_type.dart

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,54 @@ class InferredType {
1414
static const int flagNullable = 1 << 0;
1515
static const int flagInt = 1 << 1;
1616

17-
InferredType(Class concreteClass, bool nullable, bool isInt)
18-
: this._byReference(getClassReference(concreteClass),
19-
(nullable ? flagNullable : 0) | (isInt ? flagInt : 0));
17+
// For Parameters and Fields, whether a type-check is required at assignment
18+
// (invocation/setter). Not meaningful on other kernel nodes.
19+
static const int flagSkipCheck = 1 << 2;
2020

21-
InferredType._byReference(this._concreteClassReference, this._flags);
21+
// Entire list may be null if no type arguments were inferred.
22+
// Will always be null if `concreteClass` is null.
23+
//
24+
// Each component may be null if that particular type argument was not
25+
// inferred.
26+
//
27+
// Otherwise, a non-null type argument indicates that that particular type
28+
// argument (in the runtime type) is always exactly a particular `DartType`.
29+
final List<DartType> exactTypeArguments;
30+
31+
InferredType(Class concreteClass, bool nullable, bool isInt,
32+
{List<DartType> exactTypeArguments, bool skipCheck: false})
33+
: this._byReference(
34+
getClassReference(concreteClass),
35+
(nullable ? flagNullable : 0) |
36+
(isInt ? flagInt : 0) |
37+
(skipCheck ? flagSkipCheck : 0),
38+
exactTypeArguments);
39+
40+
InferredType._byReference(
41+
this._concreteClassReference, this._flags, this.exactTypeArguments) {
42+
assert(exactTypeArguments == null || _concreteClassReference != null);
43+
}
2244

2345
Class get concreteClass => _concreteClassReference?.asClass;
2446

2547
bool get nullable => (_flags & flagNullable) != 0;
2648
bool get isInt => (_flags & flagInt) != 0;
49+
bool get skipCheck => (_flags & flagSkipCheck) != 0;
2750

2851
@override
29-
String toString() =>
30-
"${concreteClass != null ? concreteClass : (isInt ? 'int' : '!')}${nullable ? '?' : ''}";
52+
String toString() {
53+
final base =
54+
"${concreteClass != null ? concreteClass : (isInt ? 'int' : '!')}";
55+
final suffix = "${nullable ? '?' : ''}";
56+
String typeArgs = "";
57+
if (exactTypeArguments != null) {
58+
typeArgs =
59+
exactTypeArguments.map((t) => t != null ? "$t" : "?").join(", ");
60+
typeArgs = "<" + typeArgs + ">";
61+
}
62+
final skip = skipCheck ? " (skip check)" : "";
63+
return base + suffix + typeArgs + skip;
64+
}
3165
}
3266

3367
/// Repository for [InferredType].
@@ -40,16 +74,20 @@ class InferredTypeMetadataRepository extends MetadataRepository<InferredType> {
4074

4175
@override
4276
void writeToBinary(InferredType metadata, Node node, BinarySink sink) {
77+
// TODO(sjindel/tfa): Implement serialization of type arguments when can use
78+
// them for optimizations.
4379
sink.writeCanonicalNameReference(
4480
getCanonicalNameOfClass(metadata.concreteClass));
4581
sink.writeByte(metadata._flags);
4682
}
4783

4884
@override
4985
InferredType readFromBinary(Node node, BinarySource source) {
86+
// TODO(sjindel/tfa): Implement serialization of type arguments when can use
87+
// them for optimizations.
5088
final concreteClassReference =
5189
source.readCanonicalNameReference()?.getReference();
5290
final flags = source.readByte();
53-
return new InferredType._byReference(concreteClassReference, flags);
91+
return new InferredType._byReference(concreteClassReference, flags, null);
5492
}
5593
}

0 commit comments

Comments
 (0)