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 /// Finds fields in this library that are overridden, as well as any fields |
| 14 /// from *other* libraries that were overridden by us. |
| 15 HashSet<FieldElement> findFieldOverrides(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 visited = new HashSet<PropertyInducingElement>(); |
| 20 for (var accessor in cls.accessors) { |
| 21 var prop = accessor.variable; |
| 22 if (visited.add(prop) && !propertyOverrides.contains(prop)) { |
| 23 checkForPropertyOverride(prop, propertyOverrides); |
| 24 } |
| 25 } |
| 26 } |
| 27 } |
| 28 |
| 29 // Return only the field overrides. |
| 30 return new HashSet<FieldElement>.from( |
| 31 propertyOverrides.where((a) => a is FieldElement)); |
| 32 } |
| 33 |
| 34 void checkForPropertyOverride(PropertyInducingElement prop, |
| 35 HashSet<PropertyInducingElement> propertyOverrides) { |
| 36 var library = prop.library; |
| 37 var type = (prop.enclosingElement as ClassElement).type; |
| 38 |
| 39 // Search for property overrides a particular "level" (mixins+superclass). |
| 40 // Once we find them we don't need to keep searching superclasses, because |
| 41 // Those properties will already be recorded as overridden in other libraries. |
| 42 // Technically, we could make this analysis more precise: continue to search |
| 43 // and explicitly mark that we don't need to emit. |
| 44 bool found = false; |
| 45 while (!found && !type.isObject) { |
| 46 var superclass = type.superclass; |
| 47 var supers = type.mixins.reversed.toList()..add(superclass); |
| 48 for (var s in supers) { |
| 49 var p = getProperty(s, library, prop.name); |
| 50 if (p != null) { |
| 51 if (!found) found = p.library != library; |
| 52 propertyOverrides.add(p); |
| 53 } |
| 54 } |
| 55 |
| 56 type = superclass; |
| 57 } |
| 58 } |
| 59 |
| 60 PropertyInducingElement getProperty( |
| 61 InterfaceType type, LibraryElement fromLibrary, String name) { |
| 62 // Properties from a different library are not accessible. |
| 63 if (Identifier.isPrivateName(name) && type.element.library != fromLibrary) { |
| 64 return null; |
| 65 } |
| 66 for (var accessor in type.accessors) { |
| 67 var prop = accessor.variable; |
| 68 if (prop.name == name) return prop; |
| 69 } |
| 70 return null; |
| 71 } |
OLD | NEW |