LEFT | RIGHT |
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 733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 | 744 |
745 case OSLICE: | 745 case OSLICE: |
746 ok |= Erv; | 746 ok |= Erv; |
747 typecheck(&n->left, top); | 747 typecheck(&n->left, top); |
748 typecheck(&n->right->left, Erv); | 748 typecheck(&n->right->left, Erv); |
749 typecheck(&n->right->right, Erv); | 749 typecheck(&n->right->right, Erv); |
750 defaultlit(&n->left, T); | 750 defaultlit(&n->left, T); |
751 defaultlit(&n->right->left, T); | 751 defaultlit(&n->right->left, T); |
752 defaultlit(&n->right->right, T); | 752 defaultlit(&n->right->right, T); |
753 if(isfixedarray(n->left->type)) { | 753 if(isfixedarray(n->left->type)) { |
| 754 if(!islvalue(n->left)) { |
| 755 yyerror("invalid operation %N (slice of unaddres
sable value)", n); |
| 756 goto error; |
| 757 } |
754 n->left = nod(OADDR, n->left, N); | 758 n->left = nod(OADDR, n->left, N); |
755 n->left->implicit = 1; | 759 n->left->implicit = 1; |
756 » » » typecheck(&n->left, top); | 760 » » » typecheck(&n->left, Erv); |
757 } | 761 } |
758 if(n->right->left != N) { | 762 if(n->right->left != N) { |
759 if((t = n->right->left->type) == T) | 763 if((t = n->right->left->type) == T) |
760 goto error; | 764 goto error; |
761 if(!isint[t->etype]) { | 765 if(!isint[t->etype]) { |
762 yyerror("invalid slice index %N (type %T)", n->r
ight->left, t); | 766 yyerror("invalid slice index %N (type %T)", n->r
ight->left, t); |
763 goto error; | 767 goto error; |
764 } | 768 } |
765 } | 769 } |
766 if(n->right->right != N) { | 770 if(n->right->right != N) { |
(...skipping 1194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1961 { | 1965 { |
1962 switch(t->etype) { | 1966 switch(t->etype) { |
1963 case TARRAY: | 1967 case TARRAY: |
1964 case TSTRUCT: | 1968 case TSTRUCT: |
1965 case TMAP: | 1969 case TMAP: |
1966 return 1; | 1970 return 1; |
1967 case TPTR32: | 1971 case TPTR32: |
1968 case TPTR64: | 1972 case TPTR64: |
1969 switch(t->type->etype) { | 1973 switch(t->type->etype) { |
1970 case TARRAY: | 1974 case TARRAY: |
1971 return isfixedarray(t->type); | |
1972 case TSTRUCT: | 1975 case TSTRUCT: |
| 1976 case TMAP: |
1973 return 1; | 1977 return 1; |
1974 } | 1978 } |
1975 break; | 1979 break; |
1976 } | 1980 } |
1977 return 0; | 1981 return 0; |
1978 } | 1982 } |
1979 | 1983 |
1980 static void | 1984 static void |
1981 pushtype(Node *n, Type *t) | 1985 pushtype(Node *n, Type *t) |
1982 { | 1986 { |
1983 if(n == N || n->op != OCOMPLIT || !iscomptype(t)) | 1987 if(n == N || n->op != OCOMPLIT || !iscomptype(t)) |
1984 return; | 1988 return; |
1985 ········ | 1989 ········ |
1986 » if(n->right == N) | 1990 » if(n->right == N) { |
1987 n->right = typenod(t); | 1991 n->right = typenod(t); |
| 1992 n->right->implicit = 1; |
| 1993 } |
1988 else if(debug['s']) { | 1994 else if(debug['s']) { |
1989 typecheck(&n->right, Etype); | 1995 typecheck(&n->right, Etype); |
1990 if(n->right->type != T && eqtype(n->right->type, t)) | 1996 if(n->right->type != T && eqtype(n->right->type, t)) |
1991 » » » print("%lL: redundant type: %T\n", lineno, t); | 1997 » » » print("%lL: redundant type: %T\n", n->right->lineno, t); |
1992 } | 1998 } |
1993 } | 1999 } |
1994 | 2000 |
1995 static void | 2001 static void |
1996 typecheckcomplit(Node **np) | 2002 typecheckcomplit(Node **np) |
1997 { | 2003 { |
1998 int bad, i, len, nerr; | 2004 int bad, i, len, nerr; |
1999 Node *l, *n, *r, **hash; | 2005 Node *l, *n, *r, **hash; |
2000 NodeList *ll; | 2006 NodeList *ll; |
2001 Type *t, *f; | 2007 Type *t, *f; |
(...skipping 21 matching lines...) Expand all Loading... |
2023 ········ | 2029 ········ |
2024 if(isptr[t->etype]) { | 2030 if(isptr[t->etype]) { |
2025 // For better or worse, we don't allow pointers as | 2031 // For better or worse, we don't allow pointers as |
2026 // the composite literal type, except when using | 2032 // the composite literal type, except when using |
2027 // the &T syntax, which sets implicit. | 2033 // the &T syntax, which sets implicit. |
2028 if(!n->right->implicit) { | 2034 if(!n->right->implicit) { |
2029 yyerror("invalid pointer type %T for composite literal (
use &%T instead)", t, t->type); | 2035 yyerror("invalid pointer type %T for composite literal (
use &%T instead)", t, t->type); |
2030 goto error; | 2036 goto error; |
2031 } | 2037 } |
2032 ················ | 2038 ················ |
2033 » » // Also, we only allow & of array and struct. | 2039 » » // Also, the underlying type must be a struct, map, slice, or ar
ray. |
2034 if(!iscomptype(t)) { | 2040 if(!iscomptype(t)) { |
2035 yyerror("invalid pointer type %T for composite literal",
t); | 2041 yyerror("invalid pointer type %T for composite literal",
t); |
2036 goto error; | 2042 goto error; |
2037 } | 2043 } |
2038 t = t->type;············ | 2044 t = t->type;············ |
2039 } | 2045 } |
2040 | 2046 |
2041 switch(t->etype) { | 2047 switch(t->etype) { |
2042 default: | 2048 default: |
2043 yyerror("invalid type for composite literal: %T", t); | 2049 yyerror("invalid type for composite literal: %T", t); |
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2801 ret: | 2807 ret: |
2802 if(typecheckdefstack->n != n) | 2808 if(typecheckdefstack->n != n) |
2803 fatal("typecheckdefstack mismatch"); | 2809 fatal("typecheckdefstack mismatch"); |
2804 l = typecheckdefstack; | 2810 l = typecheckdefstack; |
2805 typecheckdefstack = l->next; | 2811 typecheckdefstack = l->next; |
2806 | 2812 |
2807 lineno = lno; | 2813 lineno = lno; |
2808 n->walkdef = 1; | 2814 n->walkdef = 1; |
2809 return n; | 2815 return n; |
2810 } | 2816 } |
LEFT | RIGHT |