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

Side by Side Diff: dart/sdk/lib/_internal/compiler/implementation/compiler.dart

Issue 20742002: Clean up error handling. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added documentation guide lines. Created 7 years, 4 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
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 part of dart2js; 5 part of dart2js;
6 6
7 /** 7 /**
8 * If true, print a warning for each method that was resolved, but not 8 * If true, print a warning for each method that was resolved, but not
9 * compiled. 9 * compiled.
10 */ 10 */
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 * [element]. Can be nested. 390 * [element]. Can be nested.
391 */ 391 */
392 withCurrentElement(Element element, f()) { 392 withCurrentElement(Element element, f()) {
393 Element old = currentElement; 393 Element old = currentElement;
394 _currentElement = element; 394 _currentElement = element;
395 try { 395 try {
396 return f(); 396 return f();
397 } on SpannableAssertionFailure catch (ex) { 397 } on SpannableAssertionFailure catch (ex) {
398 if (!hasCrashed) { 398 if (!hasCrashed) {
399 SourceSpan span = spanFromSpannable(ex.node); 399 SourceSpan span = spanFromSpannable(ex.node);
400 reportDiagnostic(span, ex.message, api.Diagnostic.ERROR); 400 reportError(ex.node, MessageKind.GENERIC, {'text': ex.message});
401 pleaseReportCrash(); 401 pleaseReportCrash();
402 } 402 }
403 hasCrashed = true; 403 hasCrashed = true;
404 rethrow; 404 rethrow;
405 } on CompilerCancelledException catch (ex) { 405 } on CompilerCancelledException catch (ex) {
406 rethrow; 406 rethrow;
407 } on StackOverflowError catch (ex) { 407 } on StackOverflowError catch (ex) {
408 // We cannot report anything useful in this case, because we 408 // We cannot report anything useful in this case, because we
409 // do not have enough stack space. 409 // do not have enough stack space.
410 rethrow; 410 rethrow;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 {Node node, Token token, HInstruction instruction, 575 {Node node, Token token, HInstruction instruction,
576 Element element}) { 576 Element element}) {
577 internalError("$methodName not implemented", 577 internalError("$methodName not implemented",
578 node: node, token: token, 578 node: node, token: token,
579 instruction: instruction, element: element); 579 instruction: instruction, element: element);
580 } 580 }
581 581
582 void internalError(String message, 582 void internalError(String message,
583 {Node node, Token token, HInstruction instruction, 583 {Node node, Token token, HInstruction instruction,
584 Element element}) { 584 Element element}) {
585 cancel('Internal error: $message', 585 cancel('Internal Error: $message',
586 node: node, token: token, 586 node: node, token: token,
587 instruction: instruction, element: element); 587 instruction: instruction, element: element);
588 } 588 }
589 589
590 void internalErrorOnElement(Element element, String message) { 590 void internalErrorOnElement(Element element, String message) {
591 internalError(message, element: element); 591 internalError(message, element: element);
592 } 592 }
593 593
594 void unhandledExceptionOnElement(Element element) { 594 void unhandledExceptionOnElement(Element element) {
595 if (hasCrashed) return; 595 if (hasCrashed) return;
596 hasCrashed = true; 596 hasCrashed = true;
597 reportDiagnostic(spanFromElement(element), 597 reportDiagnostic(spanFromElement(element),
598 MessageKind.COMPILER_CRASHED.error().toString(), 598 MessageKind.COMPILER_CRASHED.error().toString(),
599 api.Diagnostic.CRASH); 599 api.Diagnostic.CRASH);
600 pleaseReportCrash(); 600 pleaseReportCrash();
601 } 601 }
602 602
603 void pleaseReportCrash() { 603 void pleaseReportCrash() {
604 print(MessageKind.PLEASE_REPORT_THE_CRASH.message({'buildId': buildId})); 604 print(MessageKind.PLEASE_REPORT_THE_CRASH.message({'buildId': buildId}));
605 } 605 }
606 606
607 void cancel(String reason, {Node node, Token token, 607 void cancel(String reason, {Node node, Token token,
608 HInstruction instruction, Element element}) { 608 HInstruction instruction, Element element}) {
609 assembledCode = null; // Compilation failed. Make sure that we 609 assembledCode = null; // Compilation failed. Make sure that we
610 // don't return a bogus result. 610 // don't return a bogus result.
611 SourceSpan span = null; 611 Spannable spannable = null;
612 if (node != null) { 612 if (node != null) {
613 span = spanFromNode(node); 613 spannable = node;
614 } else if (token != null) { 614 } else if (token != null) {
615 span = spanFromTokens(token, token); 615 spannable = token;
616 } else if (instruction != null) { 616 } else if (instruction != null) {
617 span = spanFromHInstruction(instruction); 617 spannable = instruction;
618 } else if (element != null) { 618 } else if (element != null) {
619 span = spanFromElement(element); 619 spannable = element;
620 } else { 620 } else {
621 throw 'No error location for error: $reason'; 621 throw 'No error location for error: $reason';
622 } 622 }
623 reportDiagnostic(span, reason, api.Diagnostic.ERROR); 623 reportError(spannable, MessageKind.GENERIC, {'text': reason});
624 throw new CompilerCancelledException(reason); 624 throw new CompilerCancelledException(reason);
625 } 625 }
626 626
627 SourceSpan spanFromSpannable(Spannable node, [Uri uri]) { 627 SourceSpan spanFromSpannable(Spannable node, [Uri uri]) {
628 if (node == null) return null;
628 if (node == CURRENT_ELEMENT_SPANNABLE) { 629 if (node == CURRENT_ELEMENT_SPANNABLE) {
629 node = currentElement; 630 node = currentElement;
630 } 631 }
631 if (node is Node) { 632 if (node is Node) {
632 return spanFromNode(node, uri); 633 return spanFromNode(node, uri);
633 } else if (node is Token) { 634 } else if (node is Token) {
634 return spanFromTokens(node, node, uri); 635 return spanFromTokens(node, node, uri);
635 } else if (node is HInstruction) { 636 } else if (node is HInstruction) {
636 return spanFromHInstruction(node); 637 return spanFromHInstruction(node);
637 } else if (node is Element) { 638 } else if (node is Element) {
638 return spanFromElement(node); 639 return spanFromElement(node);
639 } else if (node is MetadataAnnotation) { 640 } else if (node is MetadataAnnotation) {
640 MetadataAnnotation annotation = node; 641 MetadataAnnotation annotation = node;
641 return spanFromTokens(annotation.beginToken, annotation.endToken); 642 return spanFromTokens(annotation.beginToken, annotation.endToken);
642 } else { 643 } else {
643 throw 'No error location.'; 644 throw 'No error location.';
644 } 645 }
645 } 646 }
646 647
647 void reportFatalError(String reason, Element element,
648 {Node node, Token token, HInstruction instruction}) {
649 withCurrentElement(element, () {
650 cancel(reason, node: node, token: token, instruction: instruction,
651 element: element);
652 });
653 }
654
655 void log(message) { 648 void log(message) {
656 reportDiagnostic(null, message, api.Diagnostic.VERBOSE_INFO); 649 reportDiagnostic(null, message, api.Diagnostic.VERBOSE_INFO);
657 } 650 }
658 651
659 bool run(Uri uri) { 652 bool run(Uri uri) {
660 totalCompileTime.start(); 653 totalCompileTime.start();
661 try { 654 try {
662 runCompiler(uri); 655 runCompiler(uri);
663 } on CompilerCancelledException catch (exception) { 656 } on CompilerCancelledException catch (exception) {
664 log('Error: $exception'); 657 log('Error: $exception');
(...skipping 26 matching lines...) Expand all
691 */ 684 */
692 void onLibraryScanned(LibraryElement library, Uri uri) { 685 void onLibraryScanned(LibraryElement library, Uri uri) {
693 if (dynamicClass != null) { 686 if (dynamicClass != null) {
694 // When loading the built-in libraries, dynamicClass is null. We 687 // When loading the built-in libraries, dynamicClass is null. We
695 // take advantage of this as core imports js_helper and sees [dynamic] 688 // take advantage of this as core imports js_helper and sees [dynamic]
696 // this way. 689 // this way.
697 withCurrentElement(dynamicClass, () { 690 withCurrentElement(dynamicClass, () {
698 library.addToScope(dynamicClass, this); 691 library.addToScope(dynamicClass, this);
699 }); 692 });
700 } 693 }
701 if (uri == new Uri(scheme: 'dart', path: 'dart:mirrors')) { 694 if (uri == new Uri(scheme: 'dart', path: 'mirrors')) {
702 mirrorSystemClass = 695 mirrorSystemClass =
703 findRequiredElement(library, const SourceString('MirrorSystem')); 696 findRequiredElement(library, const SourceString('MirrorSystem'));
704 } else if (uri == new Uri(scheme: 'dart', path: '_collection-dev')) { 697 } else if (uri == new Uri(scheme: 'dart', path: '_collection-dev')) {
705 symbolImplementationClass = 698 symbolImplementationClass =
706 findRequiredElement(library, const SourceString('Symbol')); 699 findRequiredElement(library, const SourceString('Symbol'));
707 } else if (uri == new Uri(scheme: 'dart', path: 'async')) { 700 } else if (uri == new Uri(scheme: 'dart', path: 'async')) {
708 deferredLibraryClass = 701 deferredLibraryClass =
709 findRequiredElement(library, const SourceString('DeferredLibrary')); 702 findRequiredElement(library, const SourceString('DeferredLibrary'));
710 } 703 }
711 backend.onLibraryScanned(library, uri); 704 backend.onLibraryScanned(library, uri);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 log('compiling $uri ($buildId)'); 861 log('compiling $uri ($buildId)');
869 } 862 }
870 mainApp = libraryLoader.loadLibrary(uri, null, uri); 863 mainApp = libraryLoader.loadLibrary(uri, null, uri);
871 } 864 }
872 Element main = null; 865 Element main = null;
873 if (mainApp != null) { 866 if (mainApp != null) {
874 main = mainApp.find(MAIN); 867 main = mainApp.find(MAIN);
875 if (main == null) { 868 if (main == null) {
876 if (!analyzeOnly) { 869 if (!analyzeOnly) {
877 // Allow analyze only of libraries with no main. 870 // Allow analyze only of libraries with no main.
878 reportFatalError('Could not find $MAIN', mainApp); 871 reportFatalError(
872 mainApp,
873 MessageKind.GENERIC,
874 {'text': 'Error: Could not find "${MAIN.slowToString()}".'});
879 } else if (!analyzeAll) { 875 } else if (!analyzeAll) {
880 reportFatalError( 876 reportFatalError(
881 "Could not find $MAIN. " 877 mainApp,
882 "No source will be analyzed. " 878 MessageKind.GENERIC,
883 "Use '--analyze-all' to analyze all code in the library.", 879 {'text': 'Error: Could not find "${MAIN.slowToString()}". '
884 mainApp); 880 'No source will be analyzed. '
881 'Use "--analyze-all" to analyze all code in the library.'});
885 } 882 }
886 } else { 883 } else {
887 if (!main.isFunction()) { 884 if (!main.isFunction()) {
888 reportFatalError('main is not a function', main); 885 reportFatalError(
886 main,
887 MessageKind.GENERIC,
888 {'text': 'Error: "${MAIN.slowToString()}" is not a function.'});
889 } 889 }
890 FunctionElement mainMethod = main; 890 FunctionElement mainMethod = main;
891 FunctionSignature parameters = mainMethod.computeSignature(this); 891 FunctionSignature parameters = mainMethod.computeSignature(this);
892 parameters.forEachParameter((Element parameter) { 892 parameters.forEachParameter((Element parameter) {
893 reportFatalError('main cannot have parameters', parameter); 893 reportError(
894 parameter,
895 MessageKind.GENERIC,
896 {'text':
897 'Error: "${MAIN.slowToString()}" cannot have parameters.'});
894 }); 898 });
895 } 899 }
896 900
897 // In order to see if a library is deferred, we must compute the 901 // In order to see if a library is deferred, we must compute the
898 // compile-time constants that are metadata. This means adding 902 // compile-time constants that are metadata. This means adding
899 // something to the resolution queue. So we cannot wait with 903 // something to the resolution queue. So we cannot wait with
900 // this until after the resolution queue is processed. 904 // this until after the resolution queue is processed.
901 // TODO(ahe): Clean this up, for example, by not enqueueing 905 // TODO(ahe): Clean this up, for example, by not enqueueing
902 // classes only used for metadata. 906 // classes only used for metadata.
903 deferredLoadTask.findDeferredLibraries(mainApp); 907 deferredLoadTask.findDeferredLibraries(mainApp);
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 if (message is TypeWarning) { 1133 if (message is TypeWarning) {
1130 // TODO(ahe): Don't supress these warning when the type checker 1134 // TODO(ahe): Don't supress these warning when the type checker
1131 // is more complete. 1135 // is more complete.
1132 if (identical(message.message.kind, MessageKind.MISSING_RETURN)) return; 1136 if (identical(message.message.kind, MessageKind.MISSING_RETURN)) return;
1133 if (identical(message.message.kind, MessageKind.MAYBE_MISSING_RETURN)) { 1137 if (identical(message.message.kind, MessageKind.MAYBE_MISSING_RETURN)) {
1134 return; 1138 return;
1135 } 1139 }
1136 } 1140 }
1137 SourceSpan span = spanFromNode(node); 1141 SourceSpan span = spanFromNode(node);
1138 1142
1139 reportDiagnostic(span, 'Warning: $message', api.Diagnostic.WARNING); 1143 reportDiagnostic(span, '$message', api.Diagnostic.WARNING);
1140 } 1144 }
1141 1145
1142 // TODO(ahe): Remove this method. 1146 void reportError(Spannable node,
1143 reportError(Node node, var message) { 1147 MessageKind errorCode,
1144 SourceSpan span = spanFromNode(node); 1148 [Map arguments = const {}]) {
1145 reportDiagnostic(span, 'Error: $message', api.Diagnostic.ERROR);
1146 throw new CompilerCancelledException(message.toString());
1147 }
1148
1149 // TODO(ahe): Rename to reportError when that method has been removed.
1150 void reportErrorCode(Spannable node, MessageKind errorCode,
1151 [Map arguments = const {}]) {
1152 reportMessage(spanFromSpannable(node), 1149 reportMessage(spanFromSpannable(node),
1153 errorCode.error(arguments), 1150 errorCode.error(arguments),
1154 api.Diagnostic.ERROR); 1151 api.Diagnostic.ERROR);
1155 } 1152 }
1156 1153
1154 void reportFatalError(Spannable node, MessageKind errorCode,
1155 [Map arguments = const {}]) {
1156 reportError(node, errorCode, arguments);
1157 // TODO(ahe): Make this only abort the current method.
1158 throw new CompilerCancelledException(
1159 'Error: Cannot continue due to previous error.');
1160 }
1161
1157 // TODO(ahe): Rename to reportWarning when that method has been removed. 1162 // TODO(ahe): Rename to reportWarning when that method has been removed.
1158 void reportWarningCode(Spannable node, MessageKind errorCode, 1163 void reportWarningCode(Spannable node, MessageKind errorCode,
1159 [Map arguments = const {}]) { 1164 [Map arguments = const {}]) {
1160 reportMessage(spanFromSpannable(node), 1165 reportMessage(spanFromSpannable(node),
1161 errorCode.error(arguments), 1166 errorCode.error(arguments),
1162 api.Diagnostic.WARNING); 1167 api.Diagnostic.WARNING);
1163 } 1168 }
1164 1169
1165 void reportInfo(Spannable node, MessageKind errorCode, 1170 void reportInfo(Spannable node, MessageKind errorCode,
1166 [Map arguments = const {}]) { 1171 [Map arguments = const {}]) {
1167 reportMessage(spanFromSpannable(node), 1172 reportMessage(spanFromSpannable(node),
1168 errorCode.error(arguments), 1173 errorCode.error(arguments),
1169 api.Diagnostic.INFO); 1174 api.Diagnostic.INFO);
1170 } 1175 }
1171 1176
1172 void reportHint(Spannable node, MessageKind errorCode, 1177 void reportHint(Spannable node, MessageKind errorCode,
1173 [Map arguments = const {}]) { 1178 [Map arguments = const {}]) {
1174 reportMessage(spanFromSpannable(node), 1179 reportMessage(spanFromSpannable(node),
1175 errorCode.error(arguments), 1180 errorCode.error(arguments),
1176 api.Diagnostic.HINT); 1181 api.Diagnostic.HINT);
1177 } 1182 }
1178 1183
1179 void reportInternalError(Spannable node, String message) { 1184 void reportInternalError(Spannable node, String message) {
1180 reportMessage(spanFromSpannable(node), 1185 reportError(
1181 MessageKind.GENERIC.error({'text': message}), 1186 node, MessageKind.GENERIC, {'text': 'Internal Error: $message'});
1182 api.Diagnostic.ERROR);
1183 } 1187 }
1184 1188
1185 void reportMessage(SourceSpan span, Diagnostic message, api.Diagnostic kind) { 1189 void reportMessage(SourceSpan span, Diagnostic message, api.Diagnostic kind) {
1186 // TODO(ahe): The names Diagnostic and api.Diagnostic are in 1190 // TODO(ahe): The names Diagnostic and api.Diagnostic are in
1187 // conflict. Fix it. 1191 // conflict. Fix it.
1188 reportDiagnostic(span, "$message", kind); 1192 reportDiagnostic(span, "$message", kind);
1189 } 1193 }
1190 1194
1191 /// Returns true if a diagnostic was emitted. 1195 /// Returns true if a diagnostic was emitted.
1192 bool onDeprecatedFeature(Spannable span, String feature) { 1196 bool onDeprecatedFeature(Spannable span, String feature) {
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 1457
1454 void close() {} 1458 void close() {}
1455 1459
1456 toString() => name; 1460 toString() => name;
1457 1461
1458 /// Convenience method for getting an [api.CompilerOutputProvider]. 1462 /// Convenience method for getting an [api.CompilerOutputProvider].
1459 static NullSink outputProvider(String name, String extension) { 1463 static NullSink outputProvider(String name, String extension) {
1460 return new NullSink('$name.$extension'); 1464 return new NullSink('$name.$extension');
1461 } 1465 }
1462 } 1466 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698