OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library dev_compiler.src.codegen.js_field_storage; |
| 6 |
| 7 import 'dart:collection' show HashMap, HashSet; |
| 8 import 'package:analyzer/src/generated/ast.dart' show Identifier; |
| 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 |
| 11 import 'package:dev_compiler/src/info.dart' show LibraryUnit; |
| 12 |
| 13 /// We use a storage slot for fields that override or can be overridden by |
| 14 /// getter/setter pairs. |
| 15 HashSet<FieldElement> findFieldsNeedingStorage(LibraryUnit library) { |
| 16 var overrides = new HashSet<FieldElement>(); |
| 17 for (var unit in library.partsThenLibrary) { |
| 18 for (var cls in unit.element.types) { |
| 19 var superclasses = getSuperclasses(cls); |
| 20 for (var field in cls.fields) { |
| 21 if (!field.isSynthetic && !overrides.contains(field)) { |
| 22 checkForPropertyOverride(field, superclasses, overrides); |
| 23 } |
| 24 } |
| 25 } |
| 26 } |
| 27 |
| 28 return overrides; |
| 29 } |
| 30 |
| 31 void checkForPropertyOverride(FieldElement field, |
| 32 List<ClassElement> superclasses, HashSet<FieldElement> overrides) { |
| 33 assert(!field.isSynthetic); |
| 34 |
| 35 var library = field.library; |
| 36 |
| 37 bool found = false; |
| 38 for (var superclass in superclasses) { |
| 39 var superprop = getProperty(superclass, library, field.name); |
| 40 if (superprop != null) { |
| 41 // If we find an abstract getter/setter pair, stop the search. |
| 42 var getter = superprop.getter; |
| 43 var setter = superprop.setter; |
| 44 if ((getter == null || getter.isAbstract) && |
| 45 (setter == null || setter.isAbstract)) { |
| 46 break; |
| 47 } |
| 48 |
| 49 found = true; |
| 50 // Record that the super property is overridden. |
| 51 if (superprop.library == library) overrides.add(superprop); |
| 52 } |
| 53 } |
| 54 |
| 55 // If this we found a super property, then this property overrides it. |
| 56 if (found) overrides.add(field); |
| 57 } |
| 58 |
| 59 PropertyInducingElement getProperty( |
| 60 ClassElement cls, LibraryElement fromLibrary, String name) { |
| 61 // Properties from a different library are not accessible. |
| 62 if (Identifier.isPrivateName(name) && cls.library != fromLibrary) { |
| 63 return null; |
| 64 } |
| 65 for (var accessor in cls.accessors) { |
| 66 var prop = accessor.variable; |
| 67 if (prop.name == name) return prop; |
| 68 } |
| 69 return null; |
| 70 } |
| 71 |
| 72 List<ClassElement> getSuperclasses(ClassElement cls) { |
| 73 var result = <ClassElement>[]; |
| 74 var visited = new HashSet<ClassElement>(); |
| 75 while (cls != null && visited.add(cls)) { |
| 76 for (var mixinType in cls.mixins.reversed) { |
| 77 var mixin = mixinType.element; |
| 78 if (mixin != null) result.add(mixin); |
| 79 } |
| 80 var supertype = cls.supertype; |
| 81 if (supertype == null) break; |
| 82 |
| 83 cls = supertype.element; |
| 84 result.add(cls); |
| 85 } |
| 86 return result; |
| 87 } |
OLD | NEW |