| OLD | NEW |
| 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; | 9 final Map<ClassElement, Set<ClassElement>> subtypes; |
| 10 final Map<ClassElement, Set<ClassElement>> typesImplementedBySubclasses; |
| 10 final Set<ClassElement> classesNeedingRti; | 11 final Set<ClassElement> classesNeedingRti; |
| 11 final Map<ClassElement, Set<ClassElement>> rtiDependencies; | 12 final Map<ClassElement, Set<ClassElement>> rtiDependencies; |
| 12 final FunctionSet userDefinedGetters; | 13 final FunctionSet userDefinedGetters; |
| 13 final FunctionSet userDefinedSetters; | 14 final FunctionSet userDefinedSetters; |
| 14 | 15 |
| 15 World(Compiler compiler) | 16 World(Compiler compiler) |
| 16 : subtypes = new Map<ClassElement, Set<ClassElement>>(), | 17 : subtypes = new Map<ClassElement, Set<ClassElement>>(), |
| 18 typesImplementedBySubclasses = |
| 19 new Map<ClassElement, Set<ClassElement>>(), |
| 17 userDefinedGetters = new FunctionSet(compiler), | 20 userDefinedGetters = new FunctionSet(compiler), |
| 18 userDefinedSetters = new FunctionSet(compiler), | 21 userDefinedSetters = new FunctionSet(compiler), |
| 19 classesNeedingRti = new Set<ClassElement>(), | 22 classesNeedingRti = new Set<ClassElement>(), |
| 20 rtiDependencies = new Map<ClassElement, Set<ClassElement>>(), | 23 rtiDependencies = new Map<ClassElement, Set<ClassElement>>(), |
| 21 this.compiler = compiler; | 24 this.compiler = compiler; |
| 22 | 25 |
| 23 void populate() { | 26 void populate() { |
| 24 void addSubtypes(ClassElement cls) { | 27 void addSubtypes(ClassElement cls) { |
| 25 if (cls.resolutionState != STATE_DONE) { | 28 if (cls.resolutionState != STATE_DONE) { |
| 26 compiler.internalErrorOnElement( | 29 compiler.internalErrorOnElement( |
| 27 cls, 'Class "${cls.name.slowToString()}" is not resolved.'); | 30 cls, 'Class "${cls.name.slowToString()}" is not resolved.'); |
| 28 } | 31 } |
| 32 |
| 29 for (DartType type in cls.allSupertypes) { | 33 for (DartType type in cls.allSupertypes) { |
| 30 Set<Element> subtypesOfCls = | 34 Set<Element> subtypesOfCls = |
| 31 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>()); | 35 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>()); |
| 32 subtypesOfCls.add(cls); | 36 subtypesOfCls.add(cls); |
| 33 } | 37 } |
| 38 |
| 39 // Walk through the superclasses, and record the types |
| 40 // implemented by that type on the superclasses. |
| 41 DartType type = cls.supertype; |
| 42 while (type != null) { |
| 43 Set<Element> typesImplementedBySubclassesOfCls = |
| 44 typesImplementedBySubclasses.putIfAbsent( |
| 45 type.element, () => new Set<ClassElement>()); |
| 46 for (DartType current in cls.allSupertypes) { |
| 47 typesImplementedBySubclassesOfCls.add(current.element); |
| 48 } |
| 49 type = type.element.supertype; |
| 50 } |
| 34 } | 51 } |
| 35 | 52 |
| 36 compiler.resolverWorld.instantiatedClasses.forEach(addSubtypes); | 53 compiler.resolverWorld.instantiatedClasses.forEach(addSubtypes); |
| 37 | 54 |
| 38 // Find the classes that need runtime type information. Such | 55 // Find the classes that need runtime type information. Such |
| 39 // classes are: | 56 // classes are: |
| 40 // (1) used in a is check with type variables, | 57 // (1) used in a is check with type variables, |
| 41 // (2) dependencies of classes in (1), | 58 // (2) dependencies of classes in (1), |
| 42 // (3) subclasses of (2) and (3). | 59 // (3) subclasses of (2) and (3). |
| 43 | 60 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 116 } |
| 100 | 117 |
| 101 bool hasAnyUserDefinedGetter(Selector selector) { | 118 bool hasAnyUserDefinedGetter(Selector selector) { |
| 102 return userDefinedGetters.hasAnyElementMatchingSelector(selector); | 119 return userDefinedGetters.hasAnyElementMatchingSelector(selector); |
| 103 } | 120 } |
| 104 | 121 |
| 105 bool hasAnyUserDefinedSetter(Selector selector) { | 122 bool hasAnyUserDefinedSetter(Selector selector) { |
| 106 return userDefinedSetters.hasAnyElementMatchingSelector(selector); | 123 return userDefinedSetters.hasAnyElementMatchingSelector(selector); |
| 107 } | 124 } |
| 108 | 125 |
| 126 // Returns whether a subclass of [superclass] implements [type]. |
| 127 bool hasAnySubclassThatImplements(ClassElement superclass, DartType type) { |
| 128 Set<ClassElement> typesImplementedBySubclasses = |
| 129 typesImplementedBySubclasses[superclass]; |
| 130 if (typesImplementedBySubclasses == null) return false; |
| 131 return typesImplementedBySubclasses.contains(type.element); |
| 132 } |
| 133 |
| 134 |
| 109 void registerUsedElement(Element element) { | 135 void registerUsedElement(Element element) { |
| 110 if (element.isMember()) { | 136 if (element.isMember()) { |
| 111 if (element.isGetter()) { | 137 if (element.isGetter()) { |
| 112 // We're collecting user-defined getters to let the codegen know which | 138 // We're collecting user-defined getters to let the codegen know which |
| 113 // field accesses might have side effects. | 139 // field accesses might have side effects. |
| 114 recordUserDefinedGetter(element); | 140 recordUserDefinedGetter(element); |
| 115 } else if (element.isSetter()) { | 141 } else if (element.isSetter()) { |
| 116 recordUserDefinedSetter(element); | 142 recordUserDefinedSetter(element); |
| 117 } | 143 } |
| 118 } | 144 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 final SourceString name; | 212 final SourceString name; |
| 187 | 213 |
| 188 MemberSet(SourceString this.name) : elements = new Set<Element>(); | 214 MemberSet(SourceString this.name) : elements = new Set<Element>(); |
| 189 | 215 |
| 190 void add(Element element) { | 216 void add(Element element) { |
| 191 elements.add(element); | 217 elements.add(element); |
| 192 } | 218 } |
| 193 | 219 |
| 194 bool get isEmpty => elements.isEmpty; | 220 bool get isEmpty => elements.isEmpty; |
| 195 } | 221 } |
| OLD | NEW |