Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: lib/src/codegen/js_field_storage.dart

Issue 1090313002: fixes #52, fields shadowing getters/setters or other fields (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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
37 // Public fields on public types always need storage, because they could be
38 // extended later in another compilation unit.
39 var library = prop.library;
40 var cls = prop.enclosingElement;
41 if (prop.isPublic && cls.isPublic) {
42 propertyOverrides.add(prop);
43 return;
44 }
45
46 bool found = false;
47 for (var superclass in superclasses(cls)) {
48 var superprop = getProperty(superclass, library, prop.name);
49 if (superprop != null) {
50 found = true;
51 // Record that the super property is overridden.
52 if (superprop.library == library) propertyOverrides.add(superprop);
53 }
54 }
55
56 // If this we found a super property, then this property overrides it.
57 if (found) propertyOverrides.add(prop);
58 }
59
60 PropertyInducingElement getProperty(
61 ClassElement cls, LibraryElement fromLibrary, String name) {
62 // Properties from a different library are not accessible.
63 if (Identifier.isPrivateName(name) && cls.library != fromLibrary) {
64 return null;
65 }
66 for (var accessor in cls.accessors) {
67 var prop = accessor.variable;
68 if (prop.name == name) return prop;
69 }
70 return null;
71 }
72
73 Iterable<ClassElement> superclasses(ClassElement cls) sync* {
Jacob 2015/04/17 20:28:53 bold move... using sync* :) maybe chat with sra i
Jennifer Messerly 2015/04/20 17:44:34 Good point. Forgot about performance. Will change
Brian Wilkerson 2015/04/20 17:59:19 DBC: This looks like information that other client
Jennifer Messerly 2015/04/20 18:30:39 Good point. What's the best workflow there? I'm gu
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) yield mixin;
79 }
80 var supertype = cls.supertype;
81 if (supertype == null) return;
82
83 cls = supertype.element;
84 yield cls;
85 }
86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698