Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 library kernel.incremental_class_hierarchy; | 4 library kernel.incremental_class_hierarchy; |
| 5 | 5 |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import 'package:kernel/ast.dart'; | 9 import 'package:kernel/ast.dart'; |
| 10 import 'package:kernel/class_hierarchy.dart'; | 10 import 'package:kernel/class_hierarchy.dart'; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 } | 67 } |
| 68 return null; | 68 return null; |
| 69 } | 69 } |
| 70 | 70 |
| 71 /// Lazy and incremental implementation of [ClassHierarchy]. | 71 /// Lazy and incremental implementation of [ClassHierarchy]. |
| 72 class IncrementalClassHierarchy implements ClassHierarchy { | 72 class IncrementalClassHierarchy implements ClassHierarchy { |
| 73 /// The next unique identifier for [_ClassInfo]s. | 73 /// The next unique identifier for [_ClassInfo]s. |
| 74 int _nextId = 0; | 74 int _nextId = 0; |
| 75 | 75 |
| 76 /// The mapping from [Class]es to the corresponding [_ClassInfo]s. | 76 /// The mapping from [Class]es to the corresponding [_ClassInfo]s. |
| 77 /// The map is ordered in such a way that classes are after superclasses. | |
| 77 /// It is filled lazily as the client requests information about classes. | 78 /// It is filled lazily as the client requests information about classes. |
| 78 final Map<Class, _ClassInfo> _info = {}; | 79 final Map<Class, _ClassInfo> _info = new LinkedHashMap<Class, _ClassInfo>(); |
| 79 | |
| 80 @override | |
| 81 Iterable<Class> get classes { | |
| 82 // TODO(scheglov): implement classes | |
| 83 throw new UnimplementedError(); | |
| 84 } | |
| 85 | 80 |
| 86 @override | 81 @override |
| 87 void forEachOverridePair(Class node, | 82 void forEachOverridePair(Class node, |
| 88 callback(Member declaredMember, Member interfaceMember, bool isSetter)) { | 83 callback(Member declaredMember, Member interfaceMember, bool isSetter)) { |
| 89 _ClassInfo info = _getInfo(node); | 84 _ClassInfo info = _getInfo(node); |
| 90 for (var supertype in node.supers) { | 85 for (var supertype in node.supers) { |
| 91 var superNode = supertype.classNode; | 86 var superNode = supertype.classNode; |
| 92 var superInfo = _getInfo(superNode); | 87 var superInfo = _getInfo(superNode); |
| 93 | 88 |
| 94 var superGetters = superInfo.interfaceGettersAndCalls; | 89 var superGetters = superInfo.interfaceGettersAndCalls; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 | 239 |
| 245 @override | 240 @override |
| 246 Member getInterfaceMember(Class node, Name name, {bool setter: false}) { | 241 Member getInterfaceMember(Class node, Name name, {bool setter: false}) { |
| 247 _ClassInfo info = _getInfo(node); | 242 _ClassInfo info = _getInfo(node); |
| 248 List<Member> members = | 243 List<Member> members = |
| 249 setter ? info.interfaceSetters : info.interfaceGettersAndCalls; | 244 setter ? info.interfaceSetters : info.interfaceGettersAndCalls; |
| 250 return _findMemberByName(members, name); | 245 return _findMemberByName(members, name); |
| 251 } | 246 } |
| 252 | 247 |
| 253 @override | 248 @override |
| 249 Iterable<Class> getOrderedClasses(Iterable<Class> unordered) { | |
| 250 unordered.forEach(_getInfo); | |
| 251 return _info.keys; | |
| 252 } | |
| 253 | |
| 254 @override | |
| 254 List<Class> getRankedSuperclasses(Class node) { | 255 List<Class> getRankedSuperclasses(Class node) { |
| 255 var info = _getInfo(node); | 256 var info = _getInfo(node); |
| 256 return _getRankedSuperclassList(info).map((info) => info.node).toList(); | 257 return _getRankedSuperclassList(info).map((info) => info.node).toList(); |
| 257 } | 258 } |
| 258 | 259 |
| 259 @override | 260 @override |
| 260 InterfaceType getTypeAsInstanceOf(InterfaceType type, Class superclass) { | 261 InterfaceType getTypeAsInstanceOf(InterfaceType type, Class superclass) { |
| 261 Supertype castedType = getClassAsInstanceOf(type.classNode, superclass); | 262 Supertype castedType = getClassAsInstanceOf(type.classNode, superclass); |
| 262 if (castedType == null) return null; | 263 if (castedType == null) return null; |
| 263 return Substitution | 264 return Substitution |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 _inheritMembers(declaredGetters, allInheritedGetters); | 354 _inheritMembers(declaredGetters, allInheritedGetters); |
| 354 info.interfaceSetters = | 355 info.interfaceSetters = |
| 355 _inheritMembers(declaredSetters, allInheritedSetters); | 356 _inheritMembers(declaredSetters, allInheritedSetters); |
| 356 } | 357 } |
| 357 | 358 |
| 358 /// Return the [_ClassInfo] for the [node]. | 359 /// Return the [_ClassInfo] for the [node]. |
| 359 _ClassInfo _getInfo(Class node) { | 360 _ClassInfo _getInfo(Class node) { |
| 360 var info = _info[node]; | 361 var info = _info[node]; |
| 361 if (info == null) { | 362 if (info == null) { |
| 362 info = new _ClassInfo(_nextId++, node); | 363 info = new _ClassInfo(_nextId++, node); |
| 363 _info[node] = info; | |
| 364 | 364 |
| 365 void addSupertypeIdentifiers(_ClassInfo superInfo) { | 365 void addSupertypeIdentifiers(_ClassInfo superInfo) { |
| 366 info.supertypeIdSet.add(superInfo.id); | 366 info.supertypeIdSet.add(superInfo.id); |
| 367 info.supertypeIdSet.addAll(superInfo.supertypeIdSet); | 367 info.supertypeIdSet.addAll(superInfo.supertypeIdSet); |
| 368 } | 368 } |
| 369 | 369 |
| 370 int superDepth = -1; | 370 int superDepth = -1; |
| 371 if (node.supertype != null) { | 371 if (node.supertype != null) { |
| 372 var superInfo = _getInfo(node.supertype.classNode); | 372 var superInfo = _getInfo(node.supertype.classNode); |
| 373 superDepth = max(superDepth, superInfo.depth); | 373 superDepth = max(superDepth, superInfo.depth); |
| 374 addSupertypeIdentifiers(superInfo); | 374 addSupertypeIdentifiers(superInfo); |
| 375 _recordSuperTypes(info, node.supertype, superInfo); | 375 _recordSuperTypes(info, node.supertype, superInfo); |
| 376 } | 376 } |
| 377 if (node.mixedInType != null) { | 377 if (node.mixedInType != null) { |
| 378 var mixedInfo = _getInfo(node.mixedInType.classNode); | 378 var mixedInfo = _getInfo(node.mixedInType.classNode); |
| 379 superDepth = max(superDepth, mixedInfo.depth); | 379 superDepth = max(superDepth, mixedInfo.depth); |
| 380 addSupertypeIdentifiers(mixedInfo); | 380 addSupertypeIdentifiers(mixedInfo); |
| 381 _recordSuperTypes(info, node.mixedInType, mixedInfo); | 381 _recordSuperTypes(info, node.mixedInType, mixedInfo); |
| 382 } | 382 } |
| 383 for (var implementedType in node.implementedTypes) { | 383 for (var implementedType in node.implementedTypes) { |
| 384 var implementedInfo = _getInfo(implementedType.classNode); | 384 var implementedInfo = _getInfo(implementedType.classNode); |
| 385 superDepth = max(superDepth, implementedInfo.depth); | 385 superDepth = max(superDepth, implementedInfo.depth); |
| 386 addSupertypeIdentifiers(implementedInfo); | 386 addSupertypeIdentifiers(implementedInfo); |
| 387 _recordSuperTypes(info, implementedType, implementedInfo); | 387 _recordSuperTypes(info, implementedType, implementedInfo); |
| 388 } | 388 } |
| 389 | |
| 389 info.depth = superDepth + 1; | 390 info.depth = superDepth + 1; |
| 391 _info[node] = info; | |
| 390 | 392 |
| 391 _buildDeclaredMembers(info); | 393 _buildDeclaredMembers(info); |
| 392 _buildImplementedMembers(info); | 394 _buildImplementedMembers(info); |
| 393 _buildInterfaceMembers(info); | 395 _buildInterfaceMembers(info); |
| 394 } | 396 } |
| 395 return info; | 397 return info; |
| 396 } | 398 } |
| 397 | 399 |
| 398 List<_ClassInfo> _getRankedSuperclassList(_ClassInfo info) { | 400 List<_ClassInfo> _getRankedSuperclassList(_ClassInfo info) { |
| 399 if (info.rankedSuperclassList != null) { | 401 if (info.rankedSuperclassList != null) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 final int id; | 623 final int id; |
| 622 | 624 |
| 623 /// The [Class] node described by this [_ClassInfo]. | 625 /// The [Class] node described by this [_ClassInfo]. |
| 624 final Class node; | 626 final Class node; |
| 625 | 627 |
| 626 /// The number of steps in the longest inheritance path from the class | 628 /// The number of steps in the longest inheritance path from the class |
| 627 /// to [Object], or `-1` if the depth has not been computed yet. | 629 /// to [Object], or `-1` if the depth has not been computed yet. |
| 628 int depth = -1; | 630 int depth = -1; |
| 629 | 631 |
| 630 /// The list of superclasses sorted by depth (descending order) and | 632 /// The list of superclasses sorted by depth (descending order) and |
| 631 /// unique identifiers (ascending order), or `null` if the lit has not | 633 /// unique identifiers (ascending order), or `null` if the it has not |
|
Paul Berry
2017/06/05 21:59:22
I think you mean "...if the list has not been comp
scheglov
2017/06/05 22:01:33
Correct. Thank you.
| |
| 632 /// been computed yet. | 634 /// been computed yet. |
| 633 List<_ClassInfo> rankedSuperclassList; | 635 List<_ClassInfo> rankedSuperclassList; |
| 634 | 636 |
| 635 /// The set of [id]s for supertypes. | 637 /// The set of [id]s for supertypes. |
| 636 /// TODO(scheglov): Maybe optimize. | 638 /// TODO(scheglov): Maybe optimize. |
| 637 final Set<int> supertypeIdSet = new HashSet<int>(); | 639 final Set<int> supertypeIdSet = new HashSet<int>(); |
| 638 | 640 |
| 639 /// Maps generic supertype classes to the instantiation implemented by this | 641 /// Maps generic supertype classes to the instantiation implemented by this |
| 640 /// class, or `null` if the class does not have generic supertypes. | 642 /// class, or `null` if the class does not have generic supertypes. |
| 641 /// | 643 /// |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 class _LubHeap extends Heap<_ClassInfo> { | 711 class _LubHeap extends Heap<_ClassInfo> { |
| 710 @override | 712 @override |
| 711 bool sortsBefore(_ClassInfo a, _ClassInfo b) => sortsBeforeStatic(a, b); | 713 bool sortsBefore(_ClassInfo a, _ClassInfo b) => sortsBeforeStatic(a, b); |
| 712 | 714 |
| 713 static bool sortsBeforeStatic(_ClassInfo a, _ClassInfo b) { | 715 static bool sortsBeforeStatic(_ClassInfo a, _ClassInfo b) { |
| 714 if (a.depth > b.depth) return true; | 716 if (a.depth > b.depth) return true; |
| 715 if (a.depth < b.depth) return false; | 717 if (a.depth < b.depth) return false; |
| 716 return a.id < b.id; | 718 return a.id < b.id; |
| 717 } | 719 } |
| 718 } | 720 } |
| OLD | NEW |