OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 /* | 5 /* |
6 * type check the whole tree of an expression. | 6 * type check the whole tree of an expression. |
7 * calculates expression types. | 7 * calculates expression types. |
8 * evaluates compile time constants. | 8 * evaluates compile time constants. |
9 * marks variables that escape the local frame. | 9 * marks variables that escape the local frame. |
10 * rewrites n->op to be more specific in some cases. | 10 * rewrites n->op to be more specific in some cases. |
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 r = n->right; | 810 r = n->right; |
811 if((t = l->type) == T || r->type == T) | 811 if((t = l->type) == T || r->type == T) |
812 goto error; | 812 goto error; |
813 switch(t->etype) { | 813 switch(t->etype) { |
814 default: | 814 default: |
815 yyerror("invalid operation: %N (index of type %T)", n, t
); | 815 yyerror("invalid operation: %N (index of type %T)", n, t
); |
816 goto error; | 816 goto error; |
817 | 817 |
818 case TARRAY: | 818 case TARRAY: |
819 defaultlit(&n->right, T); | 819 defaultlit(&n->right, T); |
820 » » » if(n->right->type != T && !isint[n->right->type->etype]) | 820 » » » n->type = t->type; |
| 821 » » » if(n->right->type != T && !isint[n->right->type->etype])
{ |
821 yyerror("non-integer array index %N", n->right); | 822 yyerror("non-integer array index %N", n->right); |
822 » » » n->type = t->type; | 823 » » » » break; |
| 824 » » » } |
| 825 » » » if(n->right->op == OLITERAL) { |
| 826 » » » »if(mpgetfix(n->right->val.u.xval) < 0) { |
| 827 » » » » » why = isfixedarray(t) ? "array" : "slice
"; |
| 828 » » » » » yyerror("invalid %s index %N (index must
be non-negative)", why, n->right); |
| 829 » » » » } else if(isfixedarray(t) && t->bound > 0 && mpg
etfix(n->right->val.u.xval) >= t->bound) |
| 830 » » » » » yyerror("invalid array index %N (out of
bounds for %d-element array)", n->right, t->bound); |
| 831 » » » } |
823 break; | 832 break; |
824 | 833 |
825 case TMAP: | 834 case TMAP: |
826 n->etype = 0; | 835 n->etype = 0; |
827 defaultlit(&n->right, t->down); | 836 defaultlit(&n->right, t->down); |
828 if(n->right->type != T) | 837 if(n->right->type != T) |
829 n->right = assignconv(n->right, t->down, "map in
dex"); | 838 n->right = assignconv(n->right, t->down, "map in
dex"); |
830 n->type = t->type; | 839 n->type = t->type; |
831 n->op = OINDEXMAP; | 840 n->op = OINDEXMAP; |
832 break; | 841 break; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 n->left->implicit = 1; | 914 n->left->implicit = 1; |
906 typecheck(&n->left, Erv); | 915 typecheck(&n->left, Erv); |
907 } | 916 } |
908 if(n->right->left != N) { | 917 if(n->right->left != N) { |
909 if((t = n->right->left->type) == T) | 918 if((t = n->right->left->type) == T) |
910 goto error; | 919 goto error; |
911 if(!isint[t->etype]) { | 920 if(!isint[t->etype]) { |
912 yyerror("invalid slice index %N (type %T)", n->r
ight->left, t); | 921 yyerror("invalid slice index %N (type %T)", n->r
ight->left, t); |
913 goto error; | 922 goto error; |
914 } | 923 } |
| 924 if(n->right->left->op == OLITERAL && mpgetfix(n->right->
left->val.u.xval) < 0) |
| 925 yyerror("invalid slice index %N (index must be n
on-negative)", n->right->left); |
915 } | 926 } |
916 if(n->right->right != N) { | 927 if(n->right->right != N) { |
917 if((t = n->right->right->type) == T) | 928 if((t = n->right->right->type) == T) |
918 goto error; | 929 goto error; |
919 if(!isint[t->etype]) { | 930 if(!isint[t->etype]) { |
920 yyerror("invalid slice index %N (type %T)", n->r
ight->right, t); | 931 yyerror("invalid slice index %N (type %T)", n->r
ight->right, t); |
921 goto error; | 932 goto error; |
922 } | 933 } |
| 934 if(n->right->right->op == OLITERAL && mpgetfix(n->right-
>right->val.u.xval) < 0) |
| 935 yyerror("invalid slice index %N (index must be n
on-negative)", n->right->right); |
923 } | 936 } |
924 l = n->left; | 937 l = n->left; |
925 if((t = l->type) == T) | 938 if((t = l->type) == T) |
926 goto error; | 939 goto error; |
927 if(istype(t, TSTRING)) { | 940 if(istype(t, TSTRING)) { |
928 n->type = t; | 941 n->type = t; |
929 n->op = OSLICESTR; | 942 n->op = OSLICESTR; |
930 goto ret; | 943 goto ret; |
931 } | 944 } |
932 if(isptr[t->etype] && isfixedarray(t->type)) { | 945 if(isptr[t->etype] && isfixedarray(t->type)) { |
(...skipping 2025 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2958 fatal("got %T for %N", n->type, n); | 2971 fatal("got %T for %N", n->type, n); |
2959 if(typecheckdefstack->n != n) | 2972 if(typecheckdefstack->n != n) |
2960 fatal("typecheckdefstack mismatch"); | 2973 fatal("typecheckdefstack mismatch"); |
2961 l = typecheckdefstack; | 2974 l = typecheckdefstack; |
2962 typecheckdefstack = l->next; | 2975 typecheckdefstack = l->next; |
2963 | 2976 |
2964 lineno = lno; | 2977 lineno = lno; |
2965 n->walkdef = 1; | 2978 n->walkdef = 1; |
2966 return n; | 2979 return n; |
2967 } | 2980 } |
OLD | NEW |