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

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

Issue 10387080: Accept more labels per switch case. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. 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 | « frog/tests/leg_only/switch_test.dart ('k') | 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 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 String labelName = node.target.source.slowToString(); 1206 String labelName = node.target.source.slowToString();
1207 LabelElement label = statementScope.lookupLabel(labelName); 1207 LabelElement label = statementScope.lookupLabel(labelName);
1208 if (label === null) { 1208 if (label === null) {
1209 error(node.target, MessageKind.UNBOUND_LABEL, [labelName]); 1209 error(node.target, MessageKind.UNBOUND_LABEL, [labelName]);
1210 return; 1210 return;
1211 } 1211 }
1212 target = label.target; 1212 target = label.target;
1213 if (!target.statement.isValidContinueTarget()) { 1213 if (!target.statement.isValidContinueTarget()) {
1214 error(node.target, MessageKind.INVALID_CONTINUE, [labelName]); 1214 error(node.target, MessageKind.INVALID_CONTINUE, [labelName]);
1215 } 1215 }
1216 // TODO(lrn): Handle continues to switch cases.
1217 if (target.statement is SwitchCase) {
1218 unimplemented(node, "continue to switch case");
1219 }
1216 label.setContinueTarget(); 1220 label.setContinueTarget();
1217 mapping[node.target] = label; 1221 mapping[node.target] = label;
1218 } 1222 }
1219 mapping[node] = target; 1223 mapping[node] = target;
1220 } 1224 }
1221 1225
1222 visitForIn(ForIn node) { 1226 visitForIn(ForIn node) {
1223 visit(node.expression); 1227 visit(node.expression);
1224 Scope scope = new BlockScope(context); 1228 Scope scope = new BlockScope(context);
1225 Node declaration = node.declaredIdentifier; 1229 Node declaration = node.declaredIdentifier;
1226 visitIn(declaration, scope); 1230 visitIn(declaration, scope);
1227 visitLoopBodyIn(node, node.body, scope); 1231 visitLoopBodyIn(node, node.body, scope);
1228 1232
1229 // TODO(lrn): Also allow a single identifier. 1233 // TODO(lrn): Also allow a single identifier.
1230 if ((declaration is !Send || declaration.asSend().selector is !Identifier) 1234 if ((declaration is !Send || declaration.asSend().selector is !Identifier)
1231 && (declaration is !VariableDefinitions || 1235 && (declaration is !VariableDefinitions ||
1232 !declaration.asVariableDefinitions().definitions.nodes.tail.isEmpty())) 1236 !declaration.asVariableDefinitions().definitions.nodes.tail.isEmpty()))
1233 { 1237 {
1234 // The variable declaration is either not an identifier, not a 1238 // The variable declaration is either not an identifier, not a
1235 // declaration, or it's declaring more than one variable. 1239 // declaration, or it's declaring more than one variable.
1236 error(node.declaredIdentifier, MessageKind.INVALID_FOR_IN, []); 1240 error(node.declaredIdentifier, MessageKind.INVALID_FOR_IN, []);
1237 } 1241 }
1238 } 1242 }
1239 1243
1244 visitLabel(Label node) {
1245 // Labels are handled by their containing statements/cases.
1246 }
1247
1240 visitLabeledStatement(LabeledStatement node) { 1248 visitLabeledStatement(LabeledStatement node) {
1241 String labelName = node.label.slowToString(); 1249 String labelName = node.label.slowToString();
1242 LabelElement existingElement = statementScope.lookupLabel(labelName); 1250 LabelElement existingElement = statementScope.lookupLabel(labelName);
1243 if (existingElement !== null) { 1251 if (existingElement !== null) {
1244 warning(node.label, MessageKind.DUPLICATE_LABEL, [labelName]); 1252 warning(node.label, MessageKind.DUPLICATE_LABEL, [labelName]);
1245 warning(existingElement.label, MessageKind.EXISTING_LABEL, [labelName]); 1253 warning(existingElement.label, MessageKind.EXISTING_LABEL, [labelName]);
1246 } 1254 }
1247 Node body = node.getBody(); 1255 Node body = node.getBody();
1248 TargetElement targetElement = getOrCreateTargetElement(body); 1256 TargetElement targetElement = getOrCreateTargetElement(body);
1249 1257
(...skipping 26 matching lines...) Expand all
1276 } 1284 }
1277 1285
1278 visitSwitchStatement(SwitchStatement node) { 1286 visitSwitchStatement(SwitchStatement node) {
1279 node.expression.accept(this); 1287 node.expression.accept(this);
1280 1288
1281 TargetElement breakElement = getOrCreateTargetElement(node); 1289 TargetElement breakElement = getOrCreateTargetElement(node);
1282 Map<String, LabelElement> continueLabels = <LabelElement>{}; 1290 Map<String, LabelElement> continueLabels = <LabelElement>{};
1283 Link<Node> cases = node.cases.nodes; 1291 Link<Node> cases = node.cases.nodes;
1284 while (!cases.isEmpty()) { 1292 while (!cases.isEmpty()) {
1285 SwitchCase switchCase = cases.head; 1293 SwitchCase switchCase = cases.head;
1286 if (switchCase.label !== null) { 1294 for (Node labelOrCase in switchCase.labelsAndCases) {
1287 Label label = switchCase.label; 1295 if (labelOrCase is! Label) continue;
1296 Label label = labelOrCase;
1288 String labelName = label.slowToString(); 1297 String labelName = label.slowToString();
1289 1298
1290 LabelElement existingElement = continueLabels[labelName]; 1299 LabelElement existingElement = continueLabels[labelName];
1291 if (existingElement !== null) { 1300 if (existingElement !== null) {
1292 // It's an error if the same label occurs twice in the same switch. 1301 // It's an error if the same label occurs twice in the same switch.
1293 warning(label, MessageKind.DUPLICATE_LABEL, [labelName]); 1302 warning(label, MessageKind.DUPLICATE_LABEL, [labelName]);
1294 error(existingElement.label, MessageKind.EXISTING_LABEL, [labelName]); 1303 error(existingElement.label, MessageKind.EXISTING_LABEL, [labelName]);
1295 } else { 1304 } else {
1296 // It's only a warning if it shadows another label. 1305 // It's only a warning if it shadows another label.
1297 existingElement = statementScope.lookupLabel(labelName); 1306 existingElement = statementScope.lookupLabel(labelName);
(...skipping 10 matching lines...) Expand all
1308 enclosingElement); 1317 enclosingElement);
1309 mapping[switchCase] = targetElement; 1318 mapping[switchCase] = targetElement;
1310 1319
1311 LabelElement labelElement = 1320 LabelElement labelElement =
1312 new LabelElement(label, labelName, 1321 new LabelElement(label, labelName,
1313 targetElement, enclosingElement); 1322 targetElement, enclosingElement);
1314 mapping[label] = labelElement; 1323 mapping[label] = labelElement;
1315 continueLabels[labelName] = labelElement; 1324 continueLabels[labelName] = labelElement;
1316 } 1325 }
1317 cases = cases.tail; 1326 cases = cases.tail;
1327 // Test that only the last case, if any, is a default case.
1318 if (switchCase.defaultKeyword !== null && !cases.isEmpty()) { 1328 if (switchCase.defaultKeyword !== null && !cases.isEmpty()) {
1319 error(switchCase, MessageKind.INVALID_CASE_DEFAULT); 1329 error(switchCase, MessageKind.INVALID_CASE_DEFAULT);
1320 } 1330 }
1321 } 1331 }
1332
1322 statementScope.enterSwitch(breakElement, continueLabels); 1333 statementScope.enterSwitch(breakElement, continueLabels);
1323 node.cases.accept(this); 1334 node.cases.accept(this);
1324 statementScope.exitSwitch(); 1335 statementScope.exitSwitch();
1325 1336
1326 // Clean-up unused labels 1337 // Clean-up unused labels.
1327 continueLabels.forEach((String key, LabelElement label) { 1338 continueLabels.forEach((String key, LabelElement label) {
1328 TargetElement targetElement = label.target;
1329 SwitchCase switchCase = targetElement.statement;
1330 if (!label.isContinueTarget) { 1339 if (!label.isContinueTarget) {
1340 TargetElement targetElement = label.target;
1341 SwitchCase switchCase = targetElement.statement;
1331 mapping.remove(switchCase); 1342 mapping.remove(switchCase);
1332 mapping.remove(label.label); 1343 mapping.remove(label.label);
1333 } 1344 }
1334 }); 1345 });
1335 } 1346 }
1336 1347
1337 visitSwitchCase(SwitchCase node) { 1348 visitSwitchCase(SwitchCase node) {
1338 // The label was handled in [visitSwitchStatement(SwitchStatement)]. 1349 node.labelsAndCases.accept(this);
1339 node.expressions.accept(this);
1340 visitIn(node.statements, new BlockScope(context)); 1350 visitIn(node.statements, new BlockScope(context));
1341 } 1351 }
1342 1352
1353 visitCaseMatch(CaseMatch node) {
1354 visit(node.expression);
1355 }
1356
1343 visitTryStatement(TryStatement node) { 1357 visitTryStatement(TryStatement node) {
1344 visit(node.tryBlock); 1358 visit(node.tryBlock);
1345 if (node.catchBlocks.isEmpty() && node.finallyBlock == null) { 1359 if (node.catchBlocks.isEmpty() && node.finallyBlock == null) {
1346 // TODO(ngeoffray): The precise location is 1360 // TODO(ngeoffray): The precise location is
1347 // node.getEndtoken.next. Adjust when issue #1581 is fixed. 1361 // node.getEndtoken.next. Adjust when issue #1581 is fixed.
1348 error(node, MessageKind.NO_CATCH_NOR_FINALLY); 1362 error(node, MessageKind.NO_CATCH_NOR_FINALLY);
1349 } 1363 }
1350 visit(node.catchBlocks); 1364 visit(node.catchBlocks);
1351 visit(node.finallyBlock); 1365 visit(node.finallyBlock);
1352 } 1366 }
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
1926 1940
1927 TopScope(LibraryElement library) : super(null, library); 1941 TopScope(LibraryElement library) : super(null, library);
1928 Element lookup(SourceString name) { 1942 Element lookup(SourceString name) {
1929 return library.find(name); 1943 return library.find(name);
1930 } 1944 }
1931 1945
1932 Element add(Element newElement) { 1946 Element add(Element newElement) {
1933 throw "Cannot add an element in the top scope"; 1947 throw "Cannot add an element in the top scope";
1934 } 1948 }
1935 } 1949 }
OLDNEW
« no previous file with comments | « frog/tests/leg_only/switch_test.dart ('k') | lib/compiler/implementation/scanner/listener.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698