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

Side by Side Diff: pkg/compiler/lib/src/closure.dart

Issue 1428853004: Use better names for captured variables in closures. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments Created 5 years, 1 month 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
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/field_naming_mixin.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 library closureToClassMapper; 5 library closureToClassMapper;
6 6
7 import 'common.dart'; 7 import 'common.dart';
8 import 'common/names.dart' show 8 import 'common/names.dart' show
9 Identifiers; 9 Identifiers;
10 import 'common/resolution.dart' show 10 import 'common/resolution.dart' show
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 96 }
97 } 97 }
98 98
99 /// Common interface for [BoxFieldElement] and [ClosureFieldElement] as 99 /// Common interface for [BoxFieldElement] and [ClosureFieldElement] as
100 /// non-elements. 100 /// non-elements.
101 abstract class CapturedVariable implements Element {} 101 abstract class CapturedVariable implements Element {}
102 102
103 // TODO(ahe): These classes continuously cause problems. We need to 103 // TODO(ahe): These classes continuously cause problems. We need to
104 // find a more general solution. 104 // find a more general solution.
105 class ClosureFieldElement extends ElementX 105 class ClosureFieldElement extends ElementX
106 implements FieldElement, CapturedVariable { 106 implements FieldElement, CapturedVariable, PrivatelyNamedJSEntity {
107 /// The [BoxLocal] or [LocalElement] being accessed through the field. 107 /// The [BoxLocal] or [LocalElement] being accessed through the field.
108 final Local local; 108 final Local local;
109 109
110 ClosureFieldElement(String name, 110 ClosureFieldElement(String name,
111 this.local, 111 this.local,
112 ClosureClassElement enclosing) 112 ClosureClassElement enclosing)
113 : super(name, ElementKind.FIELD, enclosing); 113 : super(name, ElementKind.FIELD, enclosing);
114 114
115 /// Use [closureClass] instead. 115 /// Use [closureClass] instead.
116 @deprecated 116 @deprecated
117 get enclosingElement => super.enclosingElement; 117 get enclosingElement => super.enclosingElement;
118 118
119 ClosureClassElement get closureClass => super.enclosingElement; 119 ClosureClassElement get closureClass => super.enclosingElement;
120 120
121 MemberElement get memberContext => closureClass.methodElement.memberContext; 121 MemberElement get memberContext => closureClass.methodElement.memberContext;
122 122
123 @override
124 Entity get declaredEntity => local;
125 @override
126 Entity get rootOfScope => closureClass;
127
123 bool get hasNode => false; 128 bool get hasNode => false;
124 129
125 Node get node { 130 Node get node {
126 throw new SpannableAssertionFailure(local, 131 throw new SpannableAssertionFailure(local,
127 'Should not access node of ClosureFieldElement.'); 132 'Should not access node of ClosureFieldElement.');
128 } 133 }
129 134
130 bool get hasResolvedAst => hasTreeElements; 135 bool get hasResolvedAst => hasTreeElements;
131 136
132 ResolvedAst get resolvedAst { 137 ResolvedAst get resolvedAst {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 class BoxLocal extends Local { 244 class BoxLocal extends Local {
240 final String name; 245 final String name;
241 final ExecutableElement executableContext; 246 final ExecutableElement executableContext;
242 247
243 BoxLocal(this.name, this.executableContext); 248 BoxLocal(this.name, this.executableContext);
244 } 249 }
245 250
246 // TODO(ngeoffray, ahe): These classes continuously cause problems. We need to 251 // TODO(ngeoffray, ahe): These classes continuously cause problems. We need to
247 // find a more general solution. 252 // find a more general solution.
248 class BoxFieldElement extends ElementX 253 class BoxFieldElement extends ElementX
249 implements TypedElement, CapturedVariable, FieldElement { 254 implements TypedElement, CapturedVariable, FieldElement,
255 PrivatelyNamedJSEntity {
250 final BoxLocal box; 256 final BoxLocal box;
251 257
252 BoxFieldElement(String name, this.variableElement, BoxLocal box) 258 BoxFieldElement(String name, this.variableElement,
259 BoxLocal box)
253 : this.box = box, 260 : this.box = box,
254 super(name, ElementKind.FIELD, box.executableContext); 261 super(name, ElementKind.FIELD, box.executableContext);
255 262
256 DartType computeType(Resolution resolution) => type; 263 DartType computeType(Resolution resolution) => type;
257 264
258 DartType get type => variableElement.type; 265 DartType get type => variableElement.type;
259 266
267 @override
268 Entity get declaredEntity => variableElement;
269 @override
270 Entity get rootOfScope => box;
271
260 final VariableElement variableElement; 272 final VariableElement variableElement;
261 273
262 accept(ElementVisitor visitor, arg) { 274 accept(ElementVisitor visitor, arg) {
263 return visitor.visitBoxFieldElement(this, arg); 275 return visitor.visitBoxFieldElement(this, arg);
264 } 276 }
265 277
266 @override 278 @override
267 bool get hasNode => false; 279 bool get hasNode => false;
268 280
269 @override 281 @override
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 524
513 /// Generate a unique name for the [id]th closure field, with proposed name 525 /// Generate a unique name for the [id]th closure field, with proposed name
514 /// [name]. 526 /// [name].
515 /// 527 ///
516 /// The result is used as the name of [ClosureFieldElement]s, and must 528 /// The result is used as the name of [ClosureFieldElement]s, and must
517 /// therefore be unique to avoid breaking an invariant in the element model 529 /// therefore be unique to avoid breaking an invariant in the element model
518 /// (classes cannot declare multiple fields with the same name). 530 /// (classes cannot declare multiple fields with the same name).
519 /// 531 ///
520 /// Also, the names should be distinct from real field names to prevent 532 /// Also, the names should be distinct from real field names to prevent
521 /// clashes with selectors for those fields. 533 /// clashes with selectors for those fields.
534 ///
535 /// These names are not used in generated code, just as element name.
522 String getClosureVariableName(String name, int id) { 536 String getClosureVariableName(String name, int id) {
523 return "_captured_${name}_$id"; 537 return "_captured_${name}_$id";
524 } 538 }
525 539
526 /// Generate a unique name for the [id]th box field, with proposed name 540 /// Generate a unique name for the [id]th box field, with proposed name
527 /// [name]. 541 /// [name].
528 /// 542 ///
529 /// The result is used as the name of [BoxFieldElement]s, and must 543 /// The result is used as the name of [BoxFieldElement]s, and must
530 /// therefore be unique to avoid breaking an invariant in the element model 544 /// therefore be unique to avoid breaking an invariant in the element model
531 /// (classes cannot declare multiple fields with the same name). 545 /// (classes cannot declare multiple fields with the same name).
532 /// 546 ///
533 /// Also, the names should be distinct from real field names to prevent 547 /// Also, the names should be distinct from real field names to prevent
534 /// clashes with selectors for those fields. 548 /// clashes with selectors for those fields.
549 ///
550 /// These names are not used in generated code, just as element name.
535 String getBoxFieldName(int id) { 551 String getBoxFieldName(int id) {
536 return "_box_$id"; 552 return "_box_$id";
537 } 553 }
538 554
539 bool isCapturedVariable(Local element) { 555 bool isCapturedVariable(Local element) {
540 return _capturedVariableMapping.containsKey(element); 556 return _capturedVariableMapping.containsKey(element);
541 } 557 }
542 558
543 void addCapturedVariable(Node node, Local variable) { 559 void addCapturedVariable(Node node, Local variable) {
544 if (_capturedVariableMapping[variable] != null) { 560 if (_capturedVariableMapping[variable] != null) {
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 1143
1128 String get name => typeVariable.name; 1144 String get name => typeVariable.name;
1129 1145
1130 int get hashCode => typeVariable.hashCode; 1146 int get hashCode => typeVariable.hashCode;
1131 1147
1132 bool operator ==(other) { 1148 bool operator ==(other) {
1133 if (other is! TypeVariableLocal) return false; 1149 if (other is! TypeVariableLocal) return false;
1134 return typeVariable == other.typeVariable; 1150 return typeVariable == other.typeVariable;
1135 } 1151 }
1136 } 1152 }
1153
1154 ///
1155 /// Move the below classes to a JS model eventually.
1156 ///
1157 abstract class JSEntity implements Entity {
1158 Entity get declaredEntity;
1159 }
1160
1161 abstract class PrivatelyNamedJSEntity implements JSEntity {
1162 Entity get rootOfScope;
1163 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/field_naming_mixin.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698