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

Side by Side Diff: sdk/lib/core/errors.dart

Issue 2982843002: [fasta] Create a 'constant helper' class which will throw on illegal 'constant' expressions (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « no previous file | no next file » | 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 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * Error objects thrown in the case of a program failure. 8 * Error objects thrown in the case of a program failure.
9 * 9 *
10 * An `Error` object represents a program failure that the programmer 10 * An `Error` object represents a program failure that the programmer
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 : "Reading static variable '$variableName' during its initialization"; 559 : "Reading static variable '$variableName' during its initialization";
560 } 560 }
561 561
562 /// Used by Fasta to throw a compile-time error in a way that is compatible 562 /// Used by Fasta to throw a compile-time error in a way that is compatible
563 /// with compile-time constant evaluation. 563 /// with compile-time constant evaluation.
564 class _ConstantExpressionError { 564 class _ConstantExpressionError {
565 const _ConstantExpressionError(); 565 const _ConstantExpressionError();
566 566
567 external _throw(error); 567 external _throw(error);
568 } 568 }
569
570 /// Used by Fasta to wrap constant expressions so an illegal constant expression
571 /// will throw an error.
572 class _ConstantHelper {
573 _isNumStringBoolOrNull(Object e) {
574 return e is num || e is String || e is bool || e == null;
575 }
576
577 _isNumStringOrNull(Object e) {
578 return e is num || e is String || e == null;
579 }
580
581 _isNumOrNull(Object e) {
582 return e is num || e == null;
583 }
584
585 _isIntOrNull(Object e) {
586 return e is int || e == null;
587 }
588
589 ////////////////////////////////////////
590
591 // An expression of one of the forms e1 == e2 or e1 != e2 where e1 and e2 are
592 // constant expressions that evaluate to a numeric, string or boolean value or
593 // to null.
594
595 equals(Object e1, Object e2, Function onError) {
596 if (!_isNumStringBoolOrNull((e1)) || !_isNumStringBoolOrNull(e2)) onError();
597 return e1 == e2;
598 }
599
600 notEquals(Object e1, Object e2, Function onError) {
601 if (!_isNumStringBoolOrNull((e1)) || !_isNumStringBoolOrNull(e2)) onError();
602 return e1 != e2;
603 }
604
605 ////////////////////////////////////////
606
607 // An expression of one of the forms !e, e1 && e2 or e1 || e2 , where e, e1
608 // and e2 are constant expressions that evaluate to a boolean value.
609
610 not(Object e, Function onError) {
611 if (e is! bool) onError();
612 return !e;
613 }
614
615 logicalAnd(Object e1, Object e2, Function onError) {
616 if (e1 is! bool || e2 is! bool) onError();
617 return e1 && e2;
618 }
619
620 logicalOr(Object e1, Object e2, Function onError) {
621 if (e1 is! bool || e2 is! bool) onError();
622 return e1 || e2;
623 }
624
625 ////////////////////////////////////////
626
627 // An expression of one of the forms ~e, e1 ˆ e2, e1 & e2, e1 | e2, e1 >> e2
628 // or e1 << e2, where e, e1 and e2 are constant expressions that evaluate to
629 // an integer value or to null.
630
631 bitwiseNot(dynamic e, Function onError) {
632 if (!_isIntOrNull(e)) onError();
633 return ~e;
634 }
635
636 bitwiseXor(dynamic e1, dynamic e2, Function onError) {
637 if (!_isIntOrNull(e1) || !_isIntOrNull(e2)) onError();
638 return e1 ^ e2;
639 }
640
641 bitwiseAnd(dynamic e1, dynamic e2, Function onError) {
642 if (!_isIntOrNull(e1) || !_isIntOrNull(e2)) onError();
643 return e1 & e2;
644 }
645
646 bitwiseOr(dynamic e1, dynamic e2, Function onError) {
647 if (!_isIntOrNull(e1) || !_isIntOrNull(e2)) onError();
648 return e1 | e2;
649 }
650
651 rightShift(dynamic e1, dynamic e2, Function onError) {
652 if (!_isIntOrNull(e1) || !_isIntOrNull(e2)) onError();
653 return e1 >> e2;
654 }
655
656 leftShift(dynamic e1, dynamic e2, Function onError) {
657 if (!_isIntOrNull(e1) || !_isIntOrNull(e2)) onError();
658 return e1 << e2;
659 }
660
661 ////////////////////////////////////////
662
663 // An expression of the form e1 + e2 where e1 and e2 are constant expressions
664 // that evaluate to a numeric or string value or to null.
665
666 plus(dynamic e1, dynamic e2, Function onError) {
667 if (!_isNumStringOrNull(e1) || !_isNumStringOrNull(e2)) onError();
668 return e1 + e2;
669 }
670
671 ////////////////////////////////////////
672
673 // An expression of one of the forms -e, e1 - e2, e1 * e2, e1 / e2, e1 ~/ e2,
674 // e1 > e2, e1 < e2, e1 >= e2, e1 <= e2 or e1 % e2, where e, e1 and e2 are
675 // constant expressions that evaluate to a numeric value or to null.
676
677 unary_minus(dynamic e, Function onError) {
678 if (!_isNumOrNull(e)) onError();
679 return -e;
680 }
681
682 minus(dynamic e1, dynamic e2, Function onError) {
683 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
684 return e1 - e2;
685 }
686
687 times(dynamic e1, dynamic e2, Function onError) {
688 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
689 return e1 * e2;
690 }
691
692 div(dynamic e1, dynamic e2, Function onError) {
693 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
694 return e1 / e2;
695 }
696
697 integerDiv(dynamic e1, dynamic e2, Function onError) {
698 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
699 return e1 ~/ e2;
700 }
701
702 greater(dynamic e1, dynamic e2, Function onError) {
703 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
704 return e1 > e2;
705 }
706
707 less(dynamic e1, dynamic e2, Function onError) {
708 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
709 return e1 < e2;
710 }
711
712 greaterEqual(dynamic e1, dynamic e2, Function onError) {
713 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
714 return e1 >= e2;
715 }
716
717 lessEqual(dynamic e1, dynamic e2, Function onError) {
718 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
719 return e1 <= e2;
720 }
721
722 mod(dynamic e1, dynamic e2, Function onError) {
723 if (!_isNumOrNull(e1) || !_isNumOrNull(e2)) onError();
724 return e1 % e2;
725 }
726
727 ////////////////////////////////////////
728
729 // An expression of the form e1 ? e2 : e3 where e1, e2 and e3 are constant
730 // expressions and e1 evaluates to a boolean value.
731
732 conditional(Object e1, Object e2, Object e3, Function onError) {
733 if (e1 is! bool) onError();
734 return e1 ? e2 : e3;
735 }
736
737 ////////////////////////////////////////
738
739 // An expression of the form e1 ?? e2 where e1 and e2 are constant expressions .
740
741 ifNull(Object e1, Object e2, Object e3, Function onError) {
742 if (e1 is! bool) onError();
743 return e1 ?? e2;
744 }
745
746 ////////////////////////////////////////
747
748 // An expression of the form e.length where e is a constant expression that
749 // evaluates to a string value.
750
751 dotLength(dynamic e, Function onError) {
752 if (e is! String) onError();
753 return e.length();
754 }
755 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698