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 propertyOverrides = new HashSet<PropertyInducingElement>(); |
| 17 for (var unit in library.partsThenLibrary) { |
| 18 for (var cls in unit.element.types) { |
| 19 var superclasses = getSuperclasses(cls); |
| 20 var visited = new HashSet<PropertyInducingElement>(); |
| 21 for (var accessor in cls.accessors) { |
| 22 var prop = accessor.variable; |
| 23 if (visited.add(prop) && !propertyOverrides.contains(prop)) { |
| 24 checkForPropertyOverride(prop, superclasses, propertyOverrides); |
| 25 } |
| 26 } |
| 27 } |
| 28 } |
| 29 |
| 30 // Return only the field overrides. |
| 31 return new HashSet<FieldElement>.from( |
| 32 propertyOverrides.where((a) => a is FieldElement)); |
| 33 } |
| 34 |
| 35 void checkForPropertyOverride(PropertyInducingElement prop, |
| 36 List<ClassElement> superclasses, |
| 37 HashSet<PropertyInducingElement> propertyOverrides) { |
| 38 |
| 39 // Public fields on public types always need storage, because they could be |
| 40 // extended later in another compilation unit. |
| 41 var library = prop.library; |
| 42 var cls = prop.enclosingElement; |
| 43 if (prop.isPublic && cls.isPublic) { |
| 44 propertyOverrides.add(prop); |
| 45 return; |
| 46 } |
| 47 |
| 48 bool found = false; |
| 49 for (var superclass in superclasses) { |
| 50 var superprop = getProperty(superclass, library, prop.name); |
| 51 if (superprop != null) { |
| 52 found = true; |
| 53 // Record that the super property is overridden. |
| 54 if (superprop.library == library) propertyOverrides.add(superprop); |
| 55 } |
| 56 } |
| 57 |
| 58 // If this we found a super property, then this property overrides it. |
| 59 if (found) propertyOverrides.add(prop); |
| 60 } |
| 61 |
| 62 PropertyInducingElement getProperty( |
| 63 ClassElement cls, LibraryElement fromLibrary, String name) { |
| 64 // Properties from a different library are not accessible. |
| 65 if (Identifier.isPrivateName(name) && cls.library != fromLibrary) { |
| 66 return null; |
| 67 } |
| 68 for (var accessor in cls.accessors) { |
| 69 var prop = accessor.variable; |
| 70 if (prop.name == name) return prop; |
| 71 } |
| 72 return null; |
| 73 } |
| 74 |
| 75 List<ClassElement> getSuperclasses(ClassElement cls) { |
| 76 var result = <ClassElement>[]; |
| 77 var visited = new HashSet<ClassElement>(); |
| 78 while (cls != null && visited.add(cls)) { |
| 79 for (var mixinType in cls.mixins.reversed) { |
| 80 var mixin = mixinType.element; |
| 81 if (mixin != null) result.add(mixin); |
| 82 } |
| 83 var supertype = cls.supertype; |
| 84 if (supertype == null) break; |
| 85 |
| 86 cls = supertype.element; |
| 87 result.add(cls); |
| 88 } |
| 89 return result; |
| 90 } |
OLD | NEW |