OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart2js.js_emitter.program_builder; | 5 part of dart2js.js_emitter.program_builder; |
6 | 6 |
7 /** | 7 /** |
8 * [member] is a field (instance, static, or top level). | 8 * [member] is a field (instance, static, or top level). |
9 * | 9 * |
10 * [name] is the field name that the [Namer] has picked for this field's | 10 * [name] is the field name that the [Namer] has picked for this field's |
11 * storage, that is, the JavaScript property name. | 11 * storage, that is, the JavaScript property name. |
12 * | 12 * |
13 * [accessorName] is the name of the accessor. For instance fields this is | 13 * [accessorName] is the name of the accessor. For instance fields this is |
14 * mostly the same as [name] except when [member] is shadowing a field in its | 14 * mostly the same as [name] except when [member] is shadowing a field in its |
15 * superclass. For other fields, they are rarely the same. | 15 * superclass. For other fields, they are rarely the same. |
16 * | 16 * |
17 * [needsGetter] and [needsSetter] represent if a getter or a setter | 17 * [needsGetter] and [needsSetter] represent if a getter or a setter |
18 * respectively is needed. There are many factors in this, for example, if the | 18 * respectively is needed. There are many factors in this, for example, if the |
19 * accessor can be inlined. | 19 * accessor can be inlined. |
20 * | 20 * |
21 * [needsCheckedSetter] indicates that a checked getter is needed, and in this | 21 * [needsCheckedSetter] indicates that a checked getter is needed, and in this |
22 * case, [needsSetter] is always false. [needsCheckedSetter] is only true when | 22 * case, [needsSetter] is always false. [needsCheckedSetter] is only true when |
23 * type assertions are enabled (checked mode). | 23 * type assertions are enabled (checked mode). |
24 */ | 24 */ |
25 typedef void AcceptField( | 25 typedef void AcceptField(FieldEntity member, js.Name name, js.Name accessorName, |
26 VariableElement member, | 26 bool needsGetter, bool needsSetter, bool needsCheckedSetter); |
27 js.Name name, | |
28 js.Name accessorName, | |
29 bool needsGetter, | |
30 bool needsSetter, | |
31 bool needsCheckedSetter); | |
32 | 27 |
33 class FieldVisitor { | 28 class FieldVisitor { |
34 final Compiler compiler; | 29 final Compiler compiler; |
35 final Namer namer; | 30 final Namer namer; |
36 final ClosedWorld closedWorld; | 31 final ClosedWorld closedWorld; |
37 | 32 |
38 JavaScriptBackend get backend => compiler.backend; | 33 JavaScriptBackend get backend => compiler.backend; |
39 | 34 |
40 FieldVisitor(this.compiler, this.namer, this.closedWorld); | 35 FieldVisitor(this.compiler, this.namer, this.closedWorld); |
41 | 36 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 122 |
128 // If a class is not instantiated then we add the field just so we can | 123 // If a class is not instantiated then we add the field just so we can |
129 // generate the field getter/setter dynamically. Since this is only | 124 // generate the field getter/setter dynamically. Since this is only |
130 // allowed on fields that are in [element] we don't need to visit | 125 // allowed on fields that are in [element] we don't need to visit |
131 // superclasses for non-instantiated classes. | 126 // superclasses for non-instantiated classes. |
132 cls.implementation.forEachInstanceField(visitField, | 127 cls.implementation.forEachInstanceField(visitField, |
133 includeSuperAndInjectedMembers: isInstantiated); | 128 includeSuperAndInjectedMembers: isInstantiated); |
134 } | 129 } |
135 } | 130 } |
136 | 131 |
137 bool fieldNeedsGetter(VariableElement field) { | 132 bool fieldNeedsGetter(FieldElement field) { |
138 assert(field.isField); | 133 assert(field.isField); |
139 if (fieldAccessNeverThrows(field)) return false; | 134 if (fieldAccessNeverThrows(field)) return false; |
140 if (backend.mirrorsData.shouldRetainGetter(field)) return true; | 135 if (backend.mirrorsData.shouldRetainGetter(field)) return true; |
141 return field.isClassMember && | 136 return field.isClassMember && |
142 compiler.codegenWorldBuilder.hasInvokedGetter(field, closedWorld); | 137 compiler.codegenWorldBuilder.hasInvokedGetter(field, closedWorld); |
143 } | 138 } |
144 | 139 |
145 bool fieldNeedsSetter(VariableElement field) { | 140 bool fieldNeedsSetter(FieldElement field) { |
146 assert(field.isField); | 141 assert(field.isField); |
147 if (fieldAccessNeverThrows(field)) return false; | 142 if (fieldAccessNeverThrows(field)) return false; |
148 if (field.isFinal || field.isConst) return false; | 143 if (field.isFinal || field.isConst) return false; |
149 if (backend.mirrorsData.shouldRetainSetter(field)) return true; | 144 if (backend.mirrorsData.shouldRetainSetter(field)) return true; |
150 return field.isClassMember && | 145 return field.isClassMember && |
151 compiler.codegenWorldBuilder.hasInvokedSetter(field, closedWorld); | 146 compiler.codegenWorldBuilder.hasInvokedSetter(field, closedWorld); |
152 } | 147 } |
153 | 148 |
154 static bool fieldAccessNeverThrows(VariableElement field) { | 149 static bool fieldAccessNeverThrows(VariableElement field) { |
155 return | 150 return |
156 // We never access a field in a closure (a captured variable) without | 151 // We never access a field in a closure (a captured variable) without |
157 // knowing that it is there. Therefore we don't need to use a getter | 152 // knowing that it is there. Therefore we don't need to use a getter |
158 // (that will throw if the getter method is missing), but can always | 153 // (that will throw if the getter method is missing), but can always |
159 // access the field directly. | 154 // access the field directly. |
160 field is ClosureFieldElement; | 155 field is ClosureFieldElement; |
161 } | 156 } |
162 | 157 |
163 bool canAvoidGeneratedCheckedSetter(VariableElement member) { | 158 bool canAvoidGeneratedCheckedSetter(VariableElement member) { |
164 // We never generate accessors for top-level/static fields. | 159 // We never generate accessors for top-level/static fields. |
165 if (!member.isInstanceMember) return true; | 160 if (!member.isInstanceMember) return true; |
166 ResolutionDartType type = member.type; | 161 ResolutionDartType type = member.type; |
167 return type.treatAsDynamic || type.isObject; | 162 return type.treatAsDynamic || type.isObject; |
168 } | 163 } |
169 } | 164 } |
OLD | NEW |