Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Unified Diff: pkg/compiler/lib/src/ordered_typeset.dart

Issue 2808763005: Extract OrderedTypeSetBuilderBase from OrderedTypeSetBuilder (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/compiler/lib/src/mirrors_used.dart ('k') | pkg/compiler/lib/src/resolution/class_hierarchy.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/ordered_typeset.dart
diff --git a/pkg/compiler/lib/src/ordered_typeset.dart b/pkg/compiler/lib/src/ordered_typeset.dart
index 464b826fea08ceca0ce0dfae1b27b0661c56c271..8eb8456fd202ed662f0c44388291448da5296228 100644
--- a/pkg/compiler/lib/src/ordered_typeset.dart
+++ b/pkg/compiler/lib/src/ordered_typeset.dart
@@ -5,9 +5,11 @@
library ordered_typeset;
import 'common.dart';
-import 'elements/resolution_types.dart';
import 'diagnostics/diagnostic_listener.dart' show DiagnosticReporter;
import 'elements/elements.dart' show ClassElement;
+import 'elements/entities.dart';
+import 'elements/resolution_types.dart';
+import 'elements/types.dart';
import 'util/util.dart' show Link, LinkBuilder;
import 'package:front_end/src/fasta/util/link_implementation.dart'
show LinkEntry;
@@ -30,35 +32,32 @@ import 'package:front_end/src/fasta/util/link_implementation.dart'
* C: [C, B, A, Object]
*/
class OrderedTypeSet {
- final List<Link<ResolutionDartType>> _levels;
- final Link<ResolutionDartType> types;
- final Link<ResolutionDartType> _supertypes;
+ final List<Link<InterfaceType>> _levels;
+ final Link<InterfaceType> types;
+ final Link<InterfaceType> _supertypes;
- OrderedTypeSet.internal(
- List<Link<ResolutionDartType>> this._levels,
- Link<ResolutionDartType> this.types,
- Link<ResolutionDartType> this._supertypes);
+ OrderedTypeSet.internal(List<Link<InterfaceType>> this._levels,
+ Link<InterfaceType> this.types, Link<InterfaceType> this._supertypes);
- factory OrderedTypeSet.singleton(ResolutionDartType type) {
- Link<ResolutionDartType> types = new LinkEntry<ResolutionDartType>(
- type, const Link<ResolutionDartType>());
- List<Link<ResolutionDartType>> list = new List<Link<ResolutionDartType>>(1);
+ factory OrderedTypeSet.singleton(InterfaceType type) {
+ Link<InterfaceType> types =
+ new LinkEntry<InterfaceType>(type, const Link<InterfaceType>());
+ List<Link<InterfaceType>> list = new List<Link<InterfaceType>>(1);
list[0] = types;
return new OrderedTypeSet.internal(
- list, types, const Link<ResolutionDartType>());
+ list, types, const Link<InterfaceType>());
}
/// Creates a new [OrderedTypeSet] for [type] when it directly extends the
/// class which this set represents. This is for instance used to create the
/// type set for [ClosureClassElement] which extends [Closure].
- OrderedTypeSet extendClass(ResolutionInterfaceType type) {
+ OrderedTypeSet extendClass(InterfaceType type) {
assert(invariant(type.element, types.head.treatAsRaw,
message: 'Cannot extend generic class ${types.head} using '
'OrderedTypeSet.extendClass'));
- Link<ResolutionDartType> extendedTypes =
- new LinkEntry<ResolutionDartType>(type, types);
- List<Link<ResolutionDartType>> list =
- new List<Link<ResolutionDartType>>(levels + 1);
+ Link<InterfaceType> extendedTypes =
+ new LinkEntry<InterfaceType>(type, types);
+ List<Link<InterfaceType>> list = new List<Link<InterfaceType>>(levels + 1);
for (int i = 0; i < levels; i++) {
list[i] = _levels[i];
}
@@ -67,24 +66,24 @@ class OrderedTypeSet {
list, extendedTypes, _supertypes.prepend(types.head));
}
- Link<ResolutionDartType> get supertypes => _supertypes;
+ Link<InterfaceType> get supertypes => _supertypes;
int get levels => _levels.length;
int get maxDepth => levels - 1;
- Link<ResolutionDartType> operator [](int index) {
+ Link<InterfaceType> operator [](int index) {
if (index < levels) {
return _levels[index];
}
- return const Link<ResolutionDartType>();
+ return const Link<InterfaceType>();
}
/// Returns the offsets into [types] at which each level begins.
List<int> get levelOffsets {
List<int> offsets = new List.filled(levels, -1);
int offset = 0;
- Link<ResolutionDartType> pointer = types;
+ Link<InterfaceType> pointer = types;
for (int depth = maxDepth; depth >= 0; depth--) {
while (!identical(pointer, _levels[depth])) {
pointer = pointer.tail;
@@ -95,11 +94,11 @@ class OrderedTypeSet {
return offsets;
}
- void forEach(int level, void f(ResolutionDartType type)) {
+ void forEach(int level, void f(InterfaceType type)) {
if (level < levels) {
- Link<ResolutionDartType> pointer = _levels[level];
- Link<ResolutionDartType> end =
- level > 0 ? _levels[level - 1] : const Link<ResolutionDartType>();
+ Link<InterfaceType> pointer = _levels[level];
+ Link<InterfaceType> end =
+ level > 0 ? _levels[level - 1] : const Link<InterfaceType>();
// TODO(het): checking `isNotEmpty` should be unnecessary, remove when
// constants are properly canonicalized
while (pointer.isNotEmpty && !identical(pointer, end)) {
@@ -109,12 +108,12 @@ class OrderedTypeSet {
}
}
- ResolutionInterfaceType asInstanceOf(ClassElement cls) {
- int level = cls.hierarchyDepth;
+ InterfaceType asInstanceOf(ClassEntity cls, int hierarchyDepth) {
+ int level = hierarchyDepth;
if (level < levels) {
- Link<ResolutionDartType> pointer = _levels[level];
- Link<ResolutionDartType> end =
- level > 0 ? _levels[level - 1] : const Link<ResolutionDartType>();
+ Link<InterfaceType> pointer = _levels[level];
+ Link<InterfaceType> end =
+ level > 0 ? _levels[level - 1] : const Link<InterfaceType>();
// TODO(het): checking `isNotEmpty` should be unnecessary, remove when
// constants are properly canonicalized
while (pointer.isNotEmpty && !identical(pointer, end)) {
@@ -147,52 +146,41 @@ class OrderedTypeSet {
* B: [B, Object]
* C: [C, B, A, Object]
*/
-class OrderedTypeSetBuilder {
- Map<int, LinkEntry<ResolutionDartType>> map =
- new Map<int, LinkEntry<ResolutionDartType>>();
+abstract class OrderedTypeSetBuilderBase {
+ Map<int, LinkEntry<InterfaceType>> map =
+ new Map<int, LinkEntry<InterfaceType>>();
// TODO(15296): Avoid computing this order on the side when member
// lookup handles multiply inherited members correctly.
- LinkBuilder<ResolutionDartType> allSupertypes =
- new LinkBuilder<ResolutionDartType>();
+ LinkBuilder<InterfaceType> allSupertypes = new LinkBuilder<InterfaceType>();
int maxDepth = -1;
final DiagnosticReporter reporter;
- final ClassElement cls;
- ResolutionInterfaceType _objectType;
+ final ClassEntity cls;
+ InterfaceType _objectType;
// TODO(johnniwinther): Provide access to `Object` in deserialization and
// make [objectType] mandatory.
- OrderedTypeSetBuilder(this.cls,
- {this.reporter, ResolutionInterfaceType objectType})
+ OrderedTypeSetBuilderBase(this.cls, {this.reporter, InterfaceType objectType})
: this._objectType = objectType;
- OrderedTypeSet createOrderedTypeSet(
- ResolutionInterfaceType supertype, Link<ResolutionDartType> interfaces) {
- if (_objectType == null) {
- // Find `Object` through in hierarchy. This is used for serialization
- // where it is assumed that the hierarchy is valid.
- _objectType = supertype;
- while (!_objectType.isObject) {
- _objectType = _objectType.element.supertype;
- }
- }
+ InterfaceType _getThisType(ClassEntity cls);
+ InterfaceType _substByContext(InterfaceType type, InterfaceType context);
+ int _getHierarchyDepth(ClassEntity cls);
+ OrderedTypeSet createOrderedTypeSet(
+ InterfaceType supertype, Link<DartType> interfaces) {
// TODO(15296): Collapse these iterations to one when the order is not
// needed.
add(supertype);
- for (Link<ResolutionDartType> link = interfaces;
- !link.isEmpty;
- link = link.tail) {
+ for (Link<DartType> link = interfaces; !link.isEmpty; link = link.tail) {
add(link.head);
}
- addAllSupertypes(supertype);
- for (Link<ResolutionDartType> link = interfaces;
- !link.isEmpty;
- link = link.tail) {
- addAllSupertypes(link.head);
+ _addAllSupertypes(supertype);
+ for (Link<DartType> link = interfaces; !link.isEmpty; link = link.tail) {
+ _addAllSupertypes(link.head);
}
- add(cls.thisType);
+ add(_getThisType(cls));
return toTypeSet();
}
@@ -200,20 +188,20 @@ class OrderedTypeSetBuilder {
* Adds [type] and all supertypes of [type] to [allSupertypes] while
* substituting type variables.
*/
- void addAllSupertypes(ResolutionInterfaceType type) {
+ void _addAllSupertypes(InterfaceType type) {
ClassElement classElement = type.element;
- Link<ResolutionDartType> supertypes = classElement.allSupertypes;
+ Link<InterfaceType> supertypes = classElement.allSupertypes;
assert(invariant(cls, supertypes != null,
message: "Supertypes not computed on $classElement "
"during resolution of $cls"));
while (!supertypes.isEmpty) {
- ResolutionDartType supertype = supertypes.head;
- add(supertype.substByContext(type));
+ InterfaceType supertype = supertypes.head;
+ add(_substByContext(supertype, type));
supertypes = supertypes.tail;
}
}
- void add(ResolutionInterfaceType type) {
+ void add(InterfaceType type) {
if (type.element == cls) {
if (type != _objectType) {
allSupertypes.addLast(_objectType);
@@ -223,20 +211,20 @@ class OrderedTypeSetBuilder {
if (type != _objectType) {
allSupertypes.addLast(type);
}
- _addAtDepth(type, type.element.hierarchyDepth);
+ _addAtDepth(type, _getHierarchyDepth(type.element));
}
}
- void _addAtDepth(ResolutionInterfaceType type, int depth) {
- LinkEntry<ResolutionDartType> prev = null;
- LinkEntry<ResolutionDartType> link = map[depth];
+ void _addAtDepth(InterfaceType type, int depth) {
+ LinkEntry<InterfaceType> prev = null;
+ LinkEntry<InterfaceType> link = map[depth];
while (link != null) {
- ResolutionDartType existingType = link.head;
+ InterfaceType existingType = link.head;
if (existingType == type) return;
if (existingType.element == type.element) {
if (reporter != null) {
reporter.reportErrorMessage(cls, MessageKind.MULTI_INHERITANCE, {
- 'thisType': cls.thisType,
+ 'thisType': _getThisType(cls),
'firstType': existingType,
'secondType': type
});
@@ -249,8 +237,7 @@ class OrderedTypeSetBuilder {
prev = link;
link = link.tail;
}
- LinkEntry<ResolutionDartType> next =
- new LinkEntry<ResolutionDartType>(type);
+ LinkEntry<InterfaceType> next = new LinkEntry<InterfaceType>(type);
next.tail = null;
if (prev == null) {
map[depth] = next;
@@ -263,20 +250,20 @@ class OrderedTypeSetBuilder {
}
OrderedTypeSet toTypeSet() {
- List<Link<ResolutionDartType>> levels =
- new List<Link<ResolutionDartType>>(maxDepth + 1);
+ List<Link<InterfaceType>> levels =
+ new List<Link<InterfaceType>>(maxDepth + 1);
if (maxDepth < 0) {
- return new OrderedTypeSet.internal(levels,
- const Link<ResolutionDartType>(), const Link<ResolutionDartType>());
+ return new OrderedTypeSet.internal(
+ levels, const Link<InterfaceType>(), const Link<InterfaceType>());
}
- Link<ResolutionDartType> next = const Link<ResolutionDartType>();
+ Link<InterfaceType> next = const Link<InterfaceType>();
for (int depth = 0; depth <= maxDepth; depth++) {
- LinkEntry<ResolutionDartType> first = map[depth];
+ LinkEntry<InterfaceType> first = map[depth];
if (first == null) {
levels[depth] = next;
} else {
levels[depth] = first;
- LinkEntry<ResolutionDartType> last = first;
+ LinkEntry<InterfaceType> last = first;
while (last.tail != null) {
last = last.tail;
}
@@ -292,7 +279,7 @@ class OrderedTypeSetBuilder {
StringBuffer sb = new StringBuffer();
for (int depth = 0; depth <= maxDepth; depth++) {
sb.write('$depth: ');
- LinkEntry<ResolutionDartType> first = map[depth];
+ LinkEntry<InterfaceType> first = map[depth];
if (first != null) {
sb.write('${first.head}');
while (first.tail != null) {
@@ -305,3 +292,32 @@ class OrderedTypeSetBuilder {
return sb.toString();
}
}
+
+class OrderedTypeSetBuilder extends OrderedTypeSetBuilderBase {
+ OrderedTypeSetBuilder(ClassElement cls,
+ {DiagnosticReporter reporter, InterfaceType objectType})
+ : super(cls, reporter: reporter, objectType: objectType);
+
+ InterfaceType _getThisType(ClassElement cls) => cls.thisType;
+
+ ResolutionInterfaceType _substByContext(
+ ResolutionInterfaceType type, ResolutionInterfaceType context) {
+ return type.substByContext(context);
+ }
+
+ int _getHierarchyDepth(ClassElement cls) => cls.hierarchyDepth;
+
+ OrderedTypeSet createOrderedTypeSet(
+ InterfaceType supertype, Link<DartType> interfaces) {
+ if (_objectType == null) {
+ // Find `Object` through in hierarchy. This is used for serialization
+ // where it is assumed that the hierarchy is valid.
+ ResolutionInterfaceType objectType = supertype;
+ while (!objectType.isObject) {
+ objectType = objectType.element.supertype;
+ }
+ _objectType = objectType;
+ }
+ return super.createOrderedTypeSet(supertype, interfaces);
+ }
+}
« no previous file with comments | « pkg/compiler/lib/src/mirrors_used.dart ('k') | pkg/compiler/lib/src/resolution/class_hierarchy.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698