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

Side by Side Diff: pkg/compiler/lib/src/js_emitter/program_builder/field_visitor.dart

Issue 1859343004: dartfmt pkg/compiler (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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
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(VariableElement member, 25 typedef void AcceptField(
26 js.Name name, 26 VariableElement member,
27 js.Name accessorName, 27 js.Name name,
28 bool needsGetter, 28 js.Name accessorName,
29 bool needsSetter, 29 bool needsGetter,
30 bool needsCheckedSetter); 30 bool needsSetter,
31 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 36
37 JavaScriptBackend get backend => compiler.backend; 37 JavaScriptBackend get backend => compiler.backend;
38 38
39 FieldVisitor(this.compiler, this.namer); 39 FieldVisitor(this.compiler, this.namer);
40 40
41 /** 41 /**
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // We can only generate getters and setters for [element] since 86 // We can only generate getters and setters for [element] since
87 // the fields of super classes could be overwritten with getters or 87 // the fields of super classes could be overwritten with getters or
88 // setters. 88 // setters.
89 bool needsGetter = false; 89 bool needsGetter = false;
90 bool needsSetter = false; 90 bool needsSetter = false;
91 if (isLibrary || isMixinNativeField || holder == element) { 91 if (isLibrary || isMixinNativeField || holder == element) {
92 needsGetter = fieldNeedsGetter(field); 92 needsGetter = fieldNeedsGetter(field);
93 needsSetter = fieldNeedsSetter(field); 93 needsSetter = fieldNeedsSetter(field);
94 } 94 }
95 95
96 if ((isInstantiated && !backend.isNative(holder)) 96 if ((isInstantiated && !backend.isNative(holder)) ||
97 || needsGetter 97 needsGetter ||
98 || needsSetter) { 98 needsSetter) {
99 js.Name accessorName = namer.fieldAccessorName(field); 99 js.Name accessorName = namer.fieldAccessorName(field);
100 js.Name fieldName = namer.fieldPropertyName(field); 100 js.Name fieldName = namer.fieldPropertyName(field);
101 bool needsCheckedSetter = false; 101 bool needsCheckedSetter = false;
102 if (compiler.options.enableTypeAssertions 102 if (compiler.options.enableTypeAssertions &&
103 && needsSetter 103 needsSetter &&
104 && !canAvoidGeneratedCheckedSetter(field)) { 104 !canAvoidGeneratedCheckedSetter(field)) {
105 needsCheckedSetter = true; 105 needsCheckedSetter = true;
106 needsSetter = false; 106 needsSetter = false;
107 } 107 }
108 // Getters and setters with suffixes will be generated dynamically. 108 // Getters and setters with suffixes will be generated dynamically.
109 f(field, fieldName, accessorName, needsGetter, needsSetter, 109 f(field, fieldName, accessorName, needsGetter, needsSetter,
110 needsCheckedSetter); 110 needsCheckedSetter);
111 } 111 }
112 } 112 }
113 113
114 if (isLibrary) { 114 if (isLibrary) {
115 LibraryElement library = element; 115 LibraryElement library = element;
116 library.implementation.forEachLocalMember((Element member) { 116 library.implementation.forEachLocalMember((Element member) {
117 if (member.isField) visitField(library, member); 117 if (member.isField) visitField(library, member);
118 }); 118 });
119 } else if (visitStatics) { 119 } else if (visitStatics) {
120 ClassElement cls = element; 120 ClassElement cls = element;
121 cls.implementation.forEachStaticField(visitField); 121 cls.implementation.forEachStaticField(visitField);
122 } else { 122 } else {
123 ClassElement cls = element; 123 ClassElement cls = element;
124 // TODO(kasperl): We should make sure to only emit one version of 124 // TODO(kasperl): We should make sure to only emit one version of
125 // overridden fields. Right now, we rely on the ordering so the 125 // overridden fields. Right now, we rely on the ordering so the
126 // fields pulled in from mixins are replaced with the fields from 126 // fields pulled in from mixins are replaced with the fields from
127 // the class definition. 127 // the class definition.
128 128
129 // If a class is not instantiated then we add the field just so we can 129 // If a class is not instantiated then we add the field just so we can
130 // generate the field getter/setter dynamically. Since this is only 130 // generate the field getter/setter dynamically. Since this is only
131 // allowed on fields that are in [element] we don't need to visit 131 // allowed on fields that are in [element] we don't need to visit
132 // superclasses for non-instantiated classes. 132 // superclasses for non-instantiated classes.
133 cls.implementation.forEachInstanceField( 133 cls.implementation.forEachInstanceField(visitField,
134 visitField, includeSuperAndInjectedMembers: isInstantiated); 134 includeSuperAndInjectedMembers: isInstantiated);
135 } 135 }
136 } 136 }
137 137
138 bool fieldNeedsGetter(VariableElement field) { 138 bool fieldNeedsGetter(VariableElement field) {
139 assert(field.isField); 139 assert(field.isField);
140 if (fieldAccessNeverThrows(field)) return false; 140 if (fieldAccessNeverThrows(field)) return false;
141 if (backend.shouldRetainGetter(field)) return true; 141 if (backend.shouldRetainGetter(field)) return true;
142 return field.isClassMember && 142 return field.isClassMember &&
143 compiler.codegenWorld.hasInvokedGetter(field, compiler.world); 143 compiler.codegenWorld.hasInvokedGetter(field, compiler.world);
144 } 144 }
145 145
146 bool fieldNeedsSetter(VariableElement field) { 146 bool fieldNeedsSetter(VariableElement field) {
147 assert(field.isField); 147 assert(field.isField);
148 if (fieldAccessNeverThrows(field)) return false; 148 if (fieldAccessNeverThrows(field)) return false;
149 if (field.isFinal || field.isConst) return false; 149 if (field.isFinal || field.isConst) return false;
150 if (backend.shouldRetainSetter(field)) return true; 150 if (backend.shouldRetainSetter(field)) return true;
151 return field.isClassMember && 151 return field.isClassMember &&
152 compiler.codegenWorld.hasInvokedSetter(field, compiler.world); 152 compiler.codegenWorld.hasInvokedSetter(field, compiler.world);
153 } 153 }
154 154
155 static bool fieldAccessNeverThrows(VariableElement field) { 155 static bool fieldAccessNeverThrows(VariableElement field) {
156 return 156 return
157 // We never access a field in a closure (a captured variable) without 157 // 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 158 // 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 159 // (that will throw if the getter method is missing), but can always
160 // access the field directly. 160 // access the field directly.
161 field is ClosureFieldElement; 161 field is ClosureFieldElement;
162 } 162 }
163 163
164 bool canAvoidGeneratedCheckedSetter(VariableElement member) { 164 bool canAvoidGeneratedCheckedSetter(VariableElement member) {
165 // We never generate accessors for top-level/static fields. 165 // We never generate accessors for top-level/static fields.
166 if (!member.isInstanceMember) return true; 166 if (!member.isInstanceMember) return true;
167 DartType type = member.type; 167 DartType type = member.type;
168 return type.treatAsDynamic || type.isObject; 168 return type.treatAsDynamic || type.isObject;
169 } 169 }
170 } 170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698