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

Side by Side Diff: lib/compiler/implementation/resolver.dart

Issue 10389113: Make a single labeled statement hold all its labels. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/compiler/implementation/scanner/listener.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 interface TreeElements { 5 interface TreeElements {
6 Element operator[](Node node); 6 Element operator[](Node node);
7 Selector getSelector(Send send); 7 Selector getSelector(Send send);
8 Type getType(TypeAnnotation annotation); 8 Type getType(TypeAnnotation annotation);
9 } 9 }
10 10
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 468 }
469 } 469 }
470 470
471 interface LabelScope { 471 interface LabelScope {
472 LabelScope get outer(); 472 LabelScope get outer();
473 LabelElement lookup(String label); 473 LabelElement lookup(String label);
474 } 474 }
475 475
476 class LabeledStatementLabelScope implements LabelScope { 476 class LabeledStatementLabelScope implements LabelScope {
477 final LabelScope outer; 477 final LabelScope outer;
478 final LabelElement label; 478 final Map<String, LabelElement> labels;
479 LabeledStatementLabelScope(this.outer, this.label); 479 LabeledStatementLabelScope(this.outer, this.labels);
ahe 2012/05/14 10:11:40 My guess is that this scope is slower than what yo
Lasse Reichstein Nielsen 2012/05/14 10:44:59 True. It's probably slower if there is only one la
480 LabelElement lookup(String labelName) { 480 LabelElement lookup(String labelName) {
481 if (this.label.labelName == labelName) return label; 481 LabelElement label = labels[labelName];
482 if (label !== null) return label;
482 return outer.lookup(labelName); 483 return outer.lookup(labelName);
483 } 484 }
484 } 485 }
485 486
486 class SwitchLabelScope implements LabelScope { 487 class SwitchLabelScope implements LabelScope {
487 final LabelScope outer; 488 final LabelScope outer;
488 final Map<String, LabelElement> caseLabels; 489 final Map<String, LabelElement> caseLabels;
489 490
490 SwitchLabelScope(this.outer, this.caseLabels); 491 SwitchLabelScope(this.outer, this.caseLabels);
491 492
(...skipping 27 matching lines...) Expand all
519 520
520 LabelElement lookupLabel(String label) { 521 LabelElement lookupLabel(String label) {
521 return labels.lookup(label); 522 return labels.lookup(label);
522 } 523 }
523 TargetElement currentBreakTarget() => 524 TargetElement currentBreakTarget() =>
524 breakTargetStack.isEmpty() ? null : breakTargetStack.head; 525 breakTargetStack.isEmpty() ? null : breakTargetStack.head;
525 526
526 TargetElement currentContinueTarget() => 527 TargetElement currentContinueTarget() =>
527 continueTargetStack.isEmpty() ? null : continueTargetStack.head; 528 continueTargetStack.isEmpty() ? null : continueTargetStack.head;
528 529
529 void enterLabelScope(LabelElement element) { 530 void enterLabelScope(Map<String, LabelElement> elements) {
530 labels = new LabeledStatementLabelScope(labels, element); 531 labels = new LabeledStatementLabelScope(labels, elements);
531 nestingLevel++; 532 nestingLevel++;
532 } 533 }
533 534
534 void exitLabelScope() { 535 void exitLabelScope() {
535 nestingLevel--; 536 nestingLevel--;
536 labels = labels.outer; 537 labels = labels.outer;
537 } 538 }
538 539
539 void enterLoop(TargetElement element) { 540 void enterLoop(TargetElement element) {
540 breakTargetStack = breakTargetStack.prepend(element); 541 breakTargetStack = breakTargetStack.prepend(element);
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 // The variable declaration is either not an identifier, not a 1239 // The variable declaration is either not an identifier, not a
1239 // declaration, or it's declaring more than one variable. 1240 // declaration, or it's declaring more than one variable.
1240 error(node.declaredIdentifier, MessageKind.INVALID_FOR_IN, []); 1241 error(node.declaredIdentifier, MessageKind.INVALID_FOR_IN, []);
1241 } 1242 }
1242 } 1243 }
1243 1244
1244 visitLabel(Label node) { 1245 visitLabel(Label node) {
1245 } 1246 }
1246 1247
1247 visitLabeledStatement(LabeledStatement node) { 1248 visitLabeledStatement(LabeledStatement node) {
1248 String labelName = node.label.slowToString(); 1249 Statement body = node.statement;
1249 LabelElement existingElement = statementScope.lookupLabel(labelName); 1250 TargetElement targetElement = getOrCreateTargetElement(body);
1250 if (existingElement !== null) { 1251 Map<String, LabelElement> labelElements = <LabelElement>{};
1251 warning(node.label, MessageKind.DUPLICATE_LABEL, [labelName]); 1252 for (Label label in node.labels) {
1252 warning(existingElement.label, MessageKind.EXISTING_LABEL, [labelName]); 1253 String labelName = label.slowToString();
1254 if (labelElements.containsKey(labelName)) continue;
1255 LabelElement element = targetElement.addLabel(label, labelName);
1256 labelElements[labelName] = element;
1253 } 1257 }
1254 Node body = node.getBody(); 1258 statementScope.enterLabelScope(labelElements);
1255 TargetElement targetElement = getOrCreateTargetElement(body);
1256
1257 LabelElement element = targetElement.addLabel(node.label, labelName);
1258 statementScope.enterLabelScope(element);
1259 visit(node.statement); 1259 visit(node.statement);
1260 statementScope.exitLabelScope(); 1260 statementScope.exitLabelScope();
1261 if (element.isTarget) { 1261 labelElements.forEach((String labelName, LabelElement element) {
1262 mapping[node.label] = element; 1262 if (element.isTarget) {
1263 } else { 1263 mapping[element.label] = element;
1264 warning(node.label, MessageKind.UNUSED_LABEL, [labelName]); 1264 } else {
1265 } 1265 warning(element.label, MessageKind.UNUSED_LABEL, [labelName]);
1266 }
1267 });
1266 if (!targetElement.isTarget && mapping[body] === targetElement) { 1268 if (!targetElement.isTarget && mapping[body] === targetElement) {
1267 // If the body is itself a break or continue for another target, it 1269 // If the body is itself a break or continue for another target, it
1268 // might have updated its mapping to the target it actually does target. 1270 // might have updated its mapping to the target it actually does target.
1269 mapping.remove(body); 1271 mapping.remove(body);
1270 } 1272 }
1271 } 1273 }
1272 1274
1273 visitLiteralMap(LiteralMap node) { 1275 visitLiteralMap(LiteralMap node) {
1274 node.visitChildren(this); 1276 node.visitChildren(this);
1275 } 1277 }
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 1941
1940 TopScope(LibraryElement library) : super(null, library); 1942 TopScope(LibraryElement library) : super(null, library);
1941 Element lookup(SourceString name) { 1943 Element lookup(SourceString name) {
1942 return library.find(name); 1944 return library.find(name);
1943 } 1945 }
1944 1946
1945 Element add(Element newElement) { 1947 Element add(Element newElement) {
1946 throw "Cannot add an element in the top scope"; 1948 throw "Cannot add an element in the top scope";
1947 } 1949 }
1948 } 1950 }
OLDNEW
« no previous file with comments | « no previous file | lib/compiler/implementation/scanner/listener.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698