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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/world.dart

Issue 12443005: Add superclasses, subclasses, and subtypes tracking to the world. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix long lines. Created 7 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart2js; 5 part of dart2js;
6 6
7 class World { 7 class World {
8 final Compiler compiler; 8 final Compiler compiler;
9 final Map<ClassElement, Set<ClassElement>> subtypes;
10 final Map<ClassElement, Set<MixinApplicationElement>> mixinUses; 9 final Map<ClassElement, Set<MixinApplicationElement>> mixinUses;
11 final Map<ClassElement, Set<ClassElement>> typesImplementedBySubclasses; 10 final Map<ClassElement, Set<ClassElement>> typesImplementedBySubclasses;
12 final Set<ClassElement> classesNeedingRti; 11 final Set<ClassElement> classesNeedingRti;
13 final Map<ClassElement, Set<ClassElement>> rtiDependencies; 12 final Map<ClassElement, Set<ClassElement>> rtiDependencies;
14 final FullFunctionSet allFunctions; 13 final FullFunctionSet allFunctions;
15 14
15 // We keep track of subtype and subclass relationships in four
16 // distinct sets to make class hierarchy analysis faster.
17 final Map<ClassElement, Set<ClassElement>> subclasses =
18 new Map<ClassElement, Set<ClassElement>>();
19 final Map<ClassElement, Set<ClassElement>> superclasses =
20 new Map<ClassElement, Set<ClassElement>>();
21 final Map<ClassElement, Set<ClassElement>> subtypes =
22 new Map<ClassElement, Set<ClassElement>>();
23 final Map<ClassElement, Set<ClassElement>> supertypes =
24 new Map<ClassElement, Set<ClassElement>>();
25
16 World(Compiler compiler) 26 World(Compiler compiler)
17 : subtypes = new Map<ClassElement, Set<ClassElement>>(), 27 : mixinUses = new Map<ClassElement, Set<MixinApplicationElement>>(),
18 mixinUses = new Map<ClassElement, Set<MixinApplicationElement>>(),
19 typesImplementedBySubclasses = 28 typesImplementedBySubclasses =
20 new Map<ClassElement, Set<ClassElement>>(), 29 new Map<ClassElement, Set<ClassElement>>(),
21 classesNeedingRti = new Set<ClassElement>(), 30 classesNeedingRti = new Set<ClassElement>(),
22 rtiDependencies = new Map<ClassElement, Set<ClassElement>>(), 31 rtiDependencies = new Map<ClassElement, Set<ClassElement>>(),
23 allFunctions = new FullFunctionSet(compiler), 32 allFunctions = new FullFunctionSet(compiler),
24 this.compiler = compiler; 33 this.compiler = compiler;
25 34
26 void populate() { 35 void populate() {
27 void addSubtypes(ClassElement cls) { 36 void addSubtypes(ClassElement cls) {
28 if (cls.resolutionState != STATE_DONE) { 37 if (cls.resolutionState != STATE_DONE) {
29 compiler.internalErrorOnElement( 38 compiler.internalErrorOnElement(
30 cls, 'Class "${cls.name.slowToString()}" is not resolved.'); 39 cls, 'Class "${cls.name.slowToString()}" is not resolved.');
31 } 40 }
32 41
33 for (DartType type in cls.allSupertypes) { 42 for (DartType type in cls.allSupertypes) {
34 Set<Element> subtypesOfCls = 43 Set<Element> supertypesOfClass =
35 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>()); 44 supertypes.putIfAbsent(cls, () => new Set<ClassElement>());
36 subtypesOfCls.add(cls); 45 Set<Element> subtypesOfSupertype =
46 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>());
47 supertypesOfClass.add(type.element);
48 subtypesOfSupertype.add(cls);
37 } 49 }
38 50
39 // Walk through the superclasses, and record the types 51 // Walk through the superclasses, and record the types
40 // implemented by that type on the superclasses. 52 // implemented by that type on the superclasses.
41 DartType type = cls.supertype; 53 DartType type = cls.supertype;
42 while (type != null) { 54 while (type != null) {
55 Set<Element> superclassesOfClass =
56 superclasses.putIfAbsent(cls, () => new Set<ClassElement>());
57 Set<Element> subclassesOfSuperclass =
58 subclasses.putIfAbsent(type.element, () => new Set<ClassElement>());
59 superclassesOfClass.add(type.element);
60 subclassesOfSuperclass.add(cls);
61
43 Set<Element> typesImplementedBySubclassesOfCls = 62 Set<Element> typesImplementedBySubclassesOfCls =
44 typesImplementedBySubclasses.putIfAbsent( 63 typesImplementedBySubclasses.putIfAbsent(
45 type.element, () => new Set<ClassElement>()); 64 type.element, () => new Set<ClassElement>());
46 for (DartType current in cls.allSupertypes) { 65 for (DartType current in cls.allSupertypes) {
47 typesImplementedBySubclassesOfCls.add(current.element); 66 typesImplementedBySubclassesOfCls.add(current.element);
48 } 67 }
49 ClassElement classElement = type.element; 68 ClassElement classElement = type.element;
50 type = classElement.supertype; 69 type = classElement.supertype;
51 } 70 }
52 } 71 }
53 72
54 // Use the [:seenClasses:] set to include non-instantiated 73 // Use the [:seenClasses:] set to include non-instantiated
55 // classes: if the superclass of these classes require RTI, then 74 // classes: if the superclass of these classes require RTI, then
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (!itf.isRaw) { 125 if (!itf.isRaw) {
107 potentiallyAddForRti(itf.element); 126 potentiallyAddForRti(itf.element);
108 } 127 }
109 } else if (type.kind == TypeKind.TYPE_VARIABLE) { 128 } else if (type.kind == TypeKind.TYPE_VARIABLE) {
110 TypeVariableElement variable = type.element; 129 TypeVariableElement variable = type.element;
111 potentiallyAddForRti(variable.enclosingElement); 130 potentiallyAddForRti(variable.enclosingElement);
112 } 131 }
113 }); 132 });
114 } 133 }
115 134
135 Iterable<ClassElement> commonSupertypesOf(ClassElement x, ClassElement y) {
ngeoffray 2013/03/05 15:05:50 Please add a comment on why you're not using Set.i
136 Set<ClassElement> xSet = supertypes[x];
137 if (xSet == null) return const <ClassElement>[];
138 Set<ClassElement> ySet = supertypes[y];
139 if (ySet == null) return const <ClassElement>[];
140 Set<ClassElement> smallSet, largeSet;
141 if (xSet.length <= ySet.length) {
142 smallSet = xSet;
143 largeSet = ySet;
144 } else {
145 smallSet = ySet;
146 largeSet = xSet;
147 }
148 return smallSet.where((ClassElement each) => largeSet.contains(each));
149 }
150
116 void registerMixinUse(MixinApplicationElement mixinApplication, 151 void registerMixinUse(MixinApplicationElement mixinApplication,
117 ClassElement mixin) { 152 ClassElement mixin) {
118 Set<MixinApplicationElement> users = 153 Set<MixinApplicationElement> users =
119 mixinUses.putIfAbsent(mixin, () => 154 mixinUses.putIfAbsent(mixin, () =>
120 new Set<MixinApplicationElement>()); 155 new Set<MixinApplicationElement>());
121 users.add(mixinApplication); 156 users.add(mixinApplication);
122 } 157 }
123 158
124 bool isUsedAsMixin(ClassElement cls) { 159 bool isUsedAsMixin(ClassElement cls) {
125 Set<MixinApplicationElement> uses = mixinUses[cls]; 160 Set<MixinApplicationElement> uses = mixinUses[cls];
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 if (mask != null) { 230 if (mask != null) {
196 noSuchMethodSelector = new TypedSelector(mask, noSuchMethodSelector); 231 noSuchMethodSelector = new TypedSelector(mask, noSuchMethodSelector);
197 } 232 }
198 ClassElement objectClass = compiler.objectClass; 233 ClassElement objectClass = compiler.objectClass;
199 return allFunctions 234 return allFunctions
200 .filter(noSuchMethodSelector) 235 .filter(noSuchMethodSelector)
201 .map((Element member) => member.getEnclosingClass()) 236 .map((Element member) => member.getEnclosingClass())
202 .where((ClassElement holder) => !identical(holder, objectClass)); 237 .where((ClassElement holder) => !identical(holder, objectClass));
203 } 238 }
204 } 239 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/types/type_mask.dart ('k') | tests/compiler/dart2js/field_type_inferer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698