Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet; | |
| 6 | |
| 7 import 'package:analyzer/dart/ast/ast.dart' show Identifier; | |
| 8 import 'package:analyzer/dart/element/element.dart'; | |
| 9 import 'package:analyzer/src/dart/element/element.dart' show FieldElementImpl; | |
| 10 | |
| 11 import '../js_ast/js_ast.dart' as JS; | |
| 12 import 'element_helpers.dart'; | |
| 13 import 'js_names.dart' as JS; | |
| 14 | |
| 15 /// Tracks how fields, getters and setters are represented when emitting JS. | |
| 16 /// | |
| 17 /// Dart classes have implicit features that must be made explicit: | |
| 18 /// | |
| 19 /// - virtual fields induce a getter and setter pair. | |
| 20 /// - getters and setters are independent. | |
| 21 /// - getters and setters can be overridden. | |
| 22 /// | |
| 23 class ClassPropertyModel { | |
| 24 /// Fields that are virtual, that is, they must be generated as a property | |
| 25 /// pair in JavaScript. | |
| 26 /// | |
| 27 /// The value property stores the symbol used for the field's storage slot. | |
| 28 final virtualFields = <FieldElement, JS.TemporaryId>{}; | |
| 29 | |
| 30 /// Static fields that are overridden, this does not matter for Dart but in | |
| 31 /// JS we need to take care initializing these because JS classes inherit | |
| 32 /// statics. | |
| 33 final staticFieldOverrides = new HashSet<FieldElement>(); | |
| 34 | |
| 35 /// The set of inherited getters, used because JS getters/setters are paired, | |
| 36 /// so if we're generating a setter we may need to emit a getter that calls | |
| 37 /// super. | |
| 38 final inheritedGetters = new HashSet<String>(); | |
| 39 | |
| 40 | |
| 41 /// The set of inherited setters, used because JS getters/setters are paired, | |
| 42 /// so if we're generating a getter we may need to emit a setter that calls | |
| 43 /// super. | |
| 44 final inheritedSetters = new HashSet<String>(); | |
| 45 | |
| 46 ClassPropertyModel.build( | |
| 47 ClassElement classElem, Iterable<ExecutableElement> extensionMembers) { | |
| 48 // Visit superclasses to collect information about their fields/accessors. | |
| 49 // This is expensive so we try to collect everything in one pass. | |
| 50 for (var base in getSuperclasses(classElem)) { | |
| 51 for (var accessor in base.accessors) { | |
| 52 // For getter/setter pairs only process them once. | |
| 53 if (accessor.correspondingGetter != null) continue; | |
| 54 // Also ignore abstract getters/setters. | |
| 55 if (accessor.isAbstract) continue; | |
|
vsm
2016/12/14 22:30:47
I suspect this might break on the following:
abst
Jennifer Messerly
2016/12/15 00:59:16
that example works.
We should uniformly ignore ab
| |
| 56 | |
| 57 var field = accessor.variable; | |
| 58 var name = field.name; | |
| 59 // Ignore private names from other libraries. | |
| 60 if (Identifier.isPrivateName(name) && | |
| 61 accessor.library != classElem.library) { | |
| 62 continue; | |
| 63 } | |
| 64 | |
| 65 if (field.getter != null) inheritedGetters.add(name); | |
| 66 if (field.setter != null) inheritedSetters.add(name); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 var extensionNames = | |
| 71 new HashSet<String>.from(extensionMembers.map((e) => e.name)); | |
| 72 | |
| 73 // Visit accessors in the current class, and see if they need to be | |
| 74 // generated differently based on the inherited fields/accessors. | |
| 75 for (var accessor in classElem.accessors) { | |
| 76 // For getter/setter pairs only process them once. | |
| 77 if (accessor.correspondingGetter != null) continue; | |
| 78 // Also ignore abstract getters/setters. | |
| 79 if (accessor.isAbstract) continue; | |
|
vsm
2016/12/14 22:30:47
ditto?
Jennifer Messerly
2016/12/15 00:59:16
Acknowledged.
| |
| 80 | |
| 81 var field = accessor.variable; | |
| 82 var name = field.name; | |
| 83 // Is it a field? | |
| 84 if (!field.isSynthetic && field is FieldElementImpl) { | |
| 85 if (inheritedGetters.contains(name) || | |
| 86 inheritedSetters.contains(name) || | |
| 87 extensionNames.contains(name) || | |
| 88 field.isVirtual) { | |
| 89 if (field.isStatic) { | |
| 90 staticFieldOverrides.add(field); | |
| 91 } else { | |
| 92 virtualFields[field] = new JS.TemporaryId(name); | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 } | |
| OLD | NEW |