Index: lib/src/codegen/js_field_storage.dart |
diff --git a/lib/src/codegen/js_field_storage.dart b/lib/src/codegen/js_field_storage.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d2b30b51c14d012662ff9f2725df63b5a4c34c8c |
--- /dev/null |
+++ b/lib/src/codegen/js_field_storage.dart |
@@ -0,0 +1,90 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library dev_compiler.src.codegen.js_field_storage; |
+ |
+import 'dart:collection' show HashMap, HashSet; |
+import 'package:analyzer/src/generated/ast.dart' show Identifier; |
+import 'package:analyzer/src/generated/element.dart'; |
+ |
+import 'package:dev_compiler/src/info.dart' show LibraryUnit; |
+ |
+/// We use a storage slot for fields that override or can be overridden by |
+/// getter/setter pairs. |
+HashSet<FieldElement> findFieldsNeedingStorage(LibraryUnit library) { |
+ var propertyOverrides = new HashSet<PropertyInducingElement>(); |
+ for (var unit in library.partsThenLibrary) { |
+ for (var cls in unit.element.types) { |
+ var superclasses = getSuperclasses(cls); |
+ var visited = new HashSet<PropertyInducingElement>(); |
+ for (var accessor in cls.accessors) { |
+ var prop = accessor.variable; |
+ if (visited.add(prop) && !propertyOverrides.contains(prop)) { |
+ checkForPropertyOverride(prop, superclasses, propertyOverrides); |
+ } |
+ } |
+ } |
+ } |
+ |
+ // Return only the field overrides. |
+ return new HashSet<FieldElement>.from( |
+ propertyOverrides.where((a) => a is FieldElement)); |
+} |
+ |
+void checkForPropertyOverride(PropertyInducingElement prop, |
+ List<ClassElement> superclasses, |
+ HashSet<PropertyInducingElement> propertyOverrides) { |
+ |
+ // Public fields on public types always need storage, because they could be |
+ // extended later in another compilation unit. |
+ var library = prop.library; |
+ var cls = prop.enclosingElement; |
+ if (prop.isPublic && cls.isPublic) { |
+ propertyOverrides.add(prop); |
+ return; |
+ } |
+ |
+ bool found = false; |
+ for (var superclass in superclasses) { |
+ var superprop = getProperty(superclass, library, prop.name); |
+ if (superprop != null) { |
+ found = true; |
+ // Record that the super property is overridden. |
+ if (superprop.library == library) propertyOverrides.add(superprop); |
+ } |
+ } |
+ |
+ // If this we found a super property, then this property overrides it. |
+ if (found) propertyOverrides.add(prop); |
+} |
+ |
+PropertyInducingElement getProperty( |
+ ClassElement cls, LibraryElement fromLibrary, String name) { |
+ // Properties from a different library are not accessible. |
+ if (Identifier.isPrivateName(name) && cls.library != fromLibrary) { |
+ return null; |
+ } |
+ for (var accessor in cls.accessors) { |
+ var prop = accessor.variable; |
+ if (prop.name == name) return prop; |
+ } |
+ return null; |
+} |
+ |
+List<ClassElement> getSuperclasses(ClassElement cls) { |
+ var result = <ClassElement>[]; |
+ var visited = new HashSet<ClassElement>(); |
+ while (cls != null && visited.add(cls)) { |
+ for (var mixinType in cls.mixins.reversed) { |
+ var mixin = mixinType.element; |
+ if (mixin != null) result.add(mixin); |
+ } |
+ var supertype = cls.supertype; |
+ if (supertype == null) break; |
+ |
+ cls = supertype.element; |
+ result.add(cls); |
+ } |
+ return result; |
+} |