| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 VariableElement member, | 26 VariableElement member, |
| 27 js.Name name, | 27 js.Name name, |
| 28 js.Name accessorName, | 28 js.Name accessorName, |
| 29 bool needsGetter, | 29 bool needsGetter, |
| 30 bool needsSetter, | 30 bool needsSetter, |
| 31 bool needsCheckedSetter); | 31 bool needsCheckedSetter); |
| 32 | 32 |
| 33 class FieldVisitor { | 33 class FieldVisitor { |
| 34 final Compiler compiler; | 34 final Compiler compiler; |
| 35 final Namer namer; | 35 final Namer namer; |
| 36 final ClosedWorld closedWorld; |
| 36 | 37 |
| 37 JavaScriptBackend get backend => compiler.backend; | 38 JavaScriptBackend get backend => compiler.backend; |
| 38 | 39 |
| 39 FieldVisitor(this.compiler, this.namer); | 40 FieldVisitor(this.compiler, this.namer, this.closedWorld); |
| 40 | 41 |
| 41 /** | 42 /** |
| 42 * Invokes [f] for each of the fields of [element]. | 43 * Invokes [f] for each of the fields of [element]. |
| 43 * | 44 * |
| 44 * [element] must be a [ClassElement] or a [LibraryElement]. | 45 * [element] must be a [ClassElement] or a [LibraryElement]. |
| 45 * | 46 * |
| 46 * If [element] is a [ClassElement], the static fields of the class are | 47 * If [element] is a [ClassElement], the static fields of the class are |
| 47 * visited if [visitStatics] is true and the instance fields are visited if | 48 * visited if [visitStatics] is true and the instance fields are visited if |
| 48 * [visitStatics] is false. | 49 * [visitStatics] is false. |
| 49 * | 50 * |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 cls.implementation.forEachInstanceField(visitField, | 134 cls.implementation.forEachInstanceField(visitField, |
| 134 includeSuperAndInjectedMembers: isInstantiated); | 135 includeSuperAndInjectedMembers: isInstantiated); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 | 138 |
| 138 bool fieldNeedsGetter(VariableElement field) { | 139 bool fieldNeedsGetter(VariableElement field) { |
| 139 assert(field.isField); | 140 assert(field.isField); |
| 140 if (fieldAccessNeverThrows(field)) return false; | 141 if (fieldAccessNeverThrows(field)) return false; |
| 141 if (backend.shouldRetainGetter(field)) return true; | 142 if (backend.shouldRetainGetter(field)) return true; |
| 142 return field.isClassMember && | 143 return field.isClassMember && |
| 143 compiler.codegenWorld.hasInvokedGetter(field, compiler.closedWorld); | 144 compiler.codegenWorld.hasInvokedGetter(field, closedWorld); |
| 144 } | 145 } |
| 145 | 146 |
| 146 bool fieldNeedsSetter(VariableElement field) { | 147 bool fieldNeedsSetter(VariableElement field) { |
| 147 assert(field.isField); | 148 assert(field.isField); |
| 148 if (fieldAccessNeverThrows(field)) return false; | 149 if (fieldAccessNeverThrows(field)) return false; |
| 149 if (field.isFinal || field.isConst) return false; | 150 if (field.isFinal || field.isConst) return false; |
| 150 if (backend.shouldRetainSetter(field)) return true; | 151 if (backend.shouldRetainSetter(field)) return true; |
| 151 return field.isClassMember && | 152 return field.isClassMember && |
| 152 compiler.codegenWorld.hasInvokedSetter(field, compiler.closedWorld); | 153 compiler.codegenWorld.hasInvokedSetter(field, closedWorld); |
| 153 } | 154 } |
| 154 | 155 |
| 155 static bool fieldAccessNeverThrows(VariableElement field) { | 156 static bool fieldAccessNeverThrows(VariableElement field) { |
| 156 return | 157 return |
| 157 // We never access a field in a closure (a captured variable) without | 158 // We never access a field in a closure (a captured variable) without |
| 158 // knowing that it is there. Therefore we don't need to use a getter | 159 // knowing that it is there. Therefore we don't need to use a getter |
| 159 // (that will throw if the getter method is missing), but can always | 160 // (that will throw if the getter method is missing), but can always |
| 160 // access the field directly. | 161 // access the field directly. |
| 161 field is ClosureFieldElement; | 162 field is ClosureFieldElement; |
| 162 } | 163 } |
| 163 | 164 |
| 164 bool canAvoidGeneratedCheckedSetter(VariableElement member) { | 165 bool canAvoidGeneratedCheckedSetter(VariableElement member) { |
| 165 // We never generate accessors for top-level/static fields. | 166 // We never generate accessors for top-level/static fields. |
| 166 if (!member.isInstanceMember) return true; | 167 if (!member.isInstanceMember) return true; |
| 167 DartType type = member.type; | 168 DartType type = member.type; |
| 168 return type.treatAsDynamic || type.isObject; | 169 return type.treatAsDynamic || type.isObject; |
| 169 } | 170 } |
| 170 } | 171 } |
| OLD | NEW |