Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 5 | /* |
| 6 | * type check the whole tree of an expression. |
| 7 | * calculates expression types. |
| 8 | * evaluates compile time constants. |
| 9 | * marks variables that escape the local frame. |
| 10 | * rewrites n->op to be more specific in some cases. |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Russ Cox | 61f84a2 | 2011-08-25 16:25:10 -0400 | [diff] [blame] | 13 | #include <u.h> |
| 14 | #include <libc.h> |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 15 | #include "go.h" |
| 16 | |
| 17 | static void implicitstar(Node**); |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 18 | static int onearg(Node*, char*, ...); |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 19 | static int twoarg(Node*); |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 20 | static int lookdot(Node*, Type*, int); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 21 | static int looktypedot(Node*, Type*, int); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 22 | static void typecheckaste(int, Node*, int, Type*, NodeList*, char*); |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 23 | static Type* lookdot1(Node*, Sym *s, Type *t, Type *f, int); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 24 | static int nokeys(NodeList*); |
| 25 | static void typecheckcomplit(Node**); |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 26 | static void typecheckas2(Node*); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 27 | static void typecheckas(Node*); |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 28 | static void typecheckfunc(Node*); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 29 | static void checklvalue(Node*, char*); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 30 | static void checkassignlist(Node*, NodeList*); |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 31 | static void stringtoarraylit(Node**); |
| 32 | static Node* resolve(Node*); |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 33 | static void checkdefergo(Node*); |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 34 | static int checkmake(Type*, char*, Node*); |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 35 | static int checksliceindex(Node*, Node*, Type*); |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 36 | static int checksliceconst(Node*, Node*); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 37 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 38 | static NodeList* typecheckdefstack; |
| 39 | |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 40 | /* |
| 41 | * resolve ONONAME to definition, if any. |
| 42 | */ |
Lucio De Re | b3cc489 | 2011-08-29 09:35:04 -0400 | [diff] [blame] | 43 | static Node* |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 44 | resolve(Node *n) |
| 45 | { |
| 46 | Node *r; |
| 47 | |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 48 | if(n != N && n->op == ONONAME && n->sym != S && (r = n->sym->def) != N) { |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 49 | if(r->op != OIOTA) |
| 50 | n = r; |
| 51 | else if(n->iota >= 0) |
| 52 | n = nodintconst(n->iota); |
| 53 | } |
| 54 | return n; |
| 55 | } |
| 56 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 57 | void |
| 58 | typechecklist(NodeList *l, int top) |
| 59 | { |
| 60 | for(; l; l=l->next) |
| 61 | typecheck(&l->n, top); |
| 62 | } |
| 63 | |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 64 | static char* _typekind[] = { |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 65 | [TINT] = "int", |
| 66 | [TUINT] = "uint", |
| 67 | [TINT8] = "int8", |
| 68 | [TUINT8] = "uint8", |
| 69 | [TINT16] = "int16", |
| 70 | [TUINT16] = "uint16", |
| 71 | [TINT32] = "int32", |
| 72 | [TUINT32] = "uint32", |
| 73 | [TINT64] = "int64", |
| 74 | [TUINT64] = "uint64", |
| 75 | [TUINTPTR] = "uintptr", |
| 76 | [TCOMPLEX64] = "complex64", |
| 77 | [TCOMPLEX128] = "complex128", |
| 78 | [TFLOAT32] = "float32", |
| 79 | [TFLOAT64] = "float64", |
| 80 | [TBOOL] = "bool", |
| 81 | [TSTRING] = "string", |
| 82 | [TPTR32] = "pointer", |
| 83 | [TPTR64] = "pointer", |
Ryan Hitchman | a15448d | 2012-01-06 14:34:16 -0800 | [diff] [blame] | 84 | [TUNSAFEPTR] = "unsafe.Pointer", |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 85 | [TSTRUCT] = "struct", |
| 86 | [TINTER] = "interface", |
| 87 | [TCHAN] = "chan", |
| 88 | [TMAP] = "map", |
| 89 | [TARRAY] = "array", |
| 90 | [TFUNC] = "func", |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 91 | [TNIL] = "nil", |
Rob Pike | 703b897 | 2013-08-16 12:40:02 +1000 | [diff] [blame] | 92 | [TIDEAL] = "untyped number", |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 95 | static char* |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 96 | typekind(Type *t) |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 97 | { |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 98 | int et; |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 99 | static char buf[50]; |
| 100 | char *s; |
| 101 | |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 102 | if(isslice(t)) |
| 103 | return "slice"; |
| 104 | et = t->etype; |
Russ Cox | bd77eed | 2011-04-14 09:33:53 -0400 | [diff] [blame] | 105 | if(0 <= et && et < nelem(_typekind) && (s=_typekind[et]) != nil) |
| 106 | return s; |
| 107 | snprint(buf, sizeof buf, "etype=%d", et); |
| 108 | return buf; |
| 109 | } |
| 110 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 111 | /* |
Rémy Oudompheng | 892fa3a | 2012-10-07 17:35:21 +0200 | [diff] [blame] | 112 | * sprint_depchain prints a dependency chain |
| 113 | * of nodes into fmt. |
| 114 | * It is used by typecheck in the case of OLITERAL nodes |
| 115 | * to print constant definition loops. |
| 116 | */ |
| 117 | static void |
| 118 | sprint_depchain(Fmt *fmt, NodeList *stack, Node *cur, Node *first) |
| 119 | { |
| 120 | NodeList *l; |
| 121 | |
| 122 | for(l = stack; l; l=l->next) { |
| 123 | if(l->n->op == cur->op) { |
| 124 | if(l->n != first) |
| 125 | sprint_depchain(fmt, l->next, l->n, first); |
| 126 | fmtprint(fmt, "\n\t%L: %N uses %N", l->n->lineno, l->n, cur); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 133 | * type check node *np. |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 134 | * replaces *np with a new pointer in some cases. |
| 135 | * returns the final value of *np as a convenience. |
| 136 | */ |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 137 | static void typecheck1(Node **, int); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 138 | Node* |
| 139 | typecheck(Node **np, int top) |
| 140 | { |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 141 | Node *n; |
| 142 | int lno; |
| 143 | Fmt fmt; |
| 144 | NodeList *l; |
| 145 | static NodeList *tcstack, *tcfree; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 146 | |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 147 | // cannot type check until all the source has been parsed |
| 148 | if(!typecheckok) |
| 149 | fatal("early typecheck"); |
| 150 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 151 | n = *np; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 152 | if(n == N) |
| 153 | return N; |
Russ Cox | 112267d | 2011-07-27 17:39:30 -0400 | [diff] [blame] | 154 | |
| 155 | lno = setlineno(n); |
| 156 | |
| 157 | // Skip over parens. |
| 158 | while(n->op == OPAREN) |
| 159 | n = n->left; |
Luuk van Dijk | 49a835f | 2010-10-15 21:25:34 +0200 | [diff] [blame] | 160 | |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 161 | // Resolve definition of name and value of iota lazily. |
| 162 | n = resolve(n); |
Russ Cox | 112267d | 2011-07-27 17:39:30 -0400 | [diff] [blame] | 163 | |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 164 | *np = n; |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 165 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 166 | // Skip typecheck if already done. |
Russ Cox | e780fa8 | 2009-09-09 01:01:39 -0700 | [diff] [blame] | 167 | // But re-typecheck ONAME/OTYPE/OLITERAL/OPACK node in case context has changed. |
| 168 | if(n->typecheck == 1) { |
| 169 | switch(n->op) { |
| 170 | case ONAME: |
| 171 | case OTYPE: |
| 172 | case OLITERAL: |
| 173 | case OPACK: |
| 174 | break; |
| 175 | default: |
Russ Cox | 2f8190a | 2011-07-28 12:31:16 -0400 | [diff] [blame] | 176 | lineno = lno; |
Russ Cox | e780fa8 | 2009-09-09 01:01:39 -0700 | [diff] [blame] | 177 | return n; |
| 178 | } |
| 179 | } |
| 180 | |
Russ Cox | 4408659 | 2010-03-22 18:51:14 -0700 | [diff] [blame] | 181 | if(n->typecheck == 2) { |
Rémy Oudompheng | 892fa3a | 2012-10-07 17:35:21 +0200 | [diff] [blame] | 182 | // Typechecking loop. Trying printing a meaningful message, |
| 183 | // otherwise a stack trace of typechecking. |
| 184 | switch(n->op) { |
| 185 | case ONAME: |
| 186 | // We can already diagnose variables used as types. |
| 187 | if((top & (Erv|Etype)) == Etype) |
| 188 | yyerror("%N is not a type", n); |
| 189 | break; |
| 190 | case OLITERAL: |
| 191 | if((top & (Erv|Etype)) == Etype) { |
| 192 | yyerror("%N is not a type", n); |
| 193 | break; |
| 194 | } |
| 195 | fmtstrinit(&fmt); |
| 196 | sprint_depchain(&fmt, tcstack, n, n); |
| 197 | yyerrorl(n->lineno, "constant definition loop%s", fmtstrflush(&fmt)); |
| 198 | break; |
| 199 | } |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 200 | if(nsavederrors+nerrors == 0) { |
| 201 | fmtstrinit(&fmt); |
| 202 | for(l=tcstack; l; l=l->next) |
| 203 | fmtprint(&fmt, "\n\t%L %N", l->n->lineno, l->n); |
| 204 | yyerror("typechecking loop involving %N%s", n, fmtstrflush(&fmt)); |
| 205 | } |
Russ Cox | 2f8190a | 2011-07-28 12:31:16 -0400 | [diff] [blame] | 206 | lineno = lno; |
Russ Cox | 4408659 | 2010-03-22 18:51:14 -0700 | [diff] [blame] | 207 | return n; |
| 208 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 209 | n->typecheck = 2; |
Rémy Oudompheng | 892fa3a | 2012-10-07 17:35:21 +0200 | [diff] [blame] | 210 | |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 211 | if(tcfree != nil) { |
| 212 | l = tcfree; |
| 213 | tcfree = l->next; |
| 214 | } else |
| 215 | l = mal(sizeof *l); |
| 216 | l->next = tcstack; |
| 217 | l->n = n; |
| 218 | tcstack = l; |
| 219 | |
| 220 | typecheck1(&n, top); |
| 221 | *np = n; |
| 222 | n->typecheck = 1; |
| 223 | |
| 224 | if(tcstack != l) |
| 225 | fatal("typecheck stack out of sync"); |
| 226 | tcstack = l->next; |
| 227 | l->next = tcfree; |
| 228 | tcfree = l; |
| 229 | |
| 230 | lineno = lno; |
| 231 | return n; |
| 232 | } |
| 233 | |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 234 | /* |
| 235 | * does n contain a call or receive operation? |
| 236 | */ |
| 237 | static int callrecvlist(NodeList*); |
| 238 | |
| 239 | static int |
| 240 | callrecv(Node *n) |
| 241 | { |
| 242 | if(n == nil) |
| 243 | return 0; |
| 244 | |
| 245 | switch(n->op) { |
| 246 | case OCALL: |
| 247 | case OCALLMETH: |
| 248 | case OCALLINTER: |
| 249 | case OCALLFUNC: |
| 250 | case ORECV: |
Daniel Morsing | 48af64b | 2012-10-22 19:14:30 +0200 | [diff] [blame] | 251 | case OCAP: |
| 252 | case OLEN: |
| 253 | case OCOPY: |
| 254 | case ONEW: |
| 255 | case OAPPEND: |
| 256 | case ODELETE: |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | return callrecv(n->left) || |
| 261 | callrecv(n->right) || |
| 262 | callrecv(n->ntest) || |
| 263 | callrecv(n->nincr) || |
| 264 | callrecvlist(n->ninit) || |
| 265 | callrecvlist(n->nbody) || |
| 266 | callrecvlist(n->nelse) || |
| 267 | callrecvlist(n->list) || |
| 268 | callrecvlist(n->rlist); |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | callrecvlist(NodeList *l) |
| 273 | { |
| 274 | for(; l; l=l->next) |
| 275 | if(callrecv(l->n)) |
| 276 | return 1; |
| 277 | return 0; |
| 278 | } |
| 279 | |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 280 | // indexlit implements typechecking of untyped values as |
| 281 | // array/slice indexes. It is equivalent to defaultlit |
| 282 | // except for constants of numerical kind, which are acceptable |
| 283 | // whenever they can be represented by a value of type int. |
| 284 | static void |
| 285 | indexlit(Node **np) |
| 286 | { |
| 287 | Node *n; |
| 288 | |
| 289 | n = *np; |
| 290 | if(n == N || !isideal(n->type)) |
| 291 | return; |
| 292 | switch(consttype(n)) { |
| 293 | case CTINT: |
| 294 | case CTRUNE: |
| 295 | case CTFLT: |
| 296 | case CTCPLX: |
| 297 | defaultlit(np, types[TINT]); |
| 298 | break; |
| 299 | } |
| 300 | defaultlit(np, T); |
| 301 | } |
| 302 | |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 303 | static void |
| 304 | typecheck1(Node **np, int top) |
| 305 | { |
| 306 | int et, aop, op, ptr; |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 307 | Node *n, *l, *r, *lo, *mid, *hi; |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 308 | NodeList *args; |
| 309 | int ok, ntop; |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 310 | Type *t, *tp, *missing, *have, *badtype; |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 311 | Val v; |
Jan Ziak | a599b48 | 2014-04-11 15:57:30 +0200 | [diff] [blame] | 312 | char *why, *desc, descbuf[64]; |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 313 | vlong x; |
Russ Cox | 2aafad2 | 2012-02-11 01:04:33 -0500 | [diff] [blame] | 314 | |
| 315 | n = *np; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 316 | |
Russ Cox | 680ee6a | 2009-10-08 23:03:34 -0700 | [diff] [blame] | 317 | if(n->sym) { |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 318 | if(n->op == ONAME && n->etype != 0 && !(top & Ecall)) { |
Russ Cox | 709c5b2 | 2010-05-20 22:57:08 -0700 | [diff] [blame] | 319 | yyerror("use of builtin %S not in function call", n->sym); |
| 320 | goto error; |
| 321 | } |
Lorenzo Stoakes | b6f0632 | 2011-04-28 00:13:49 -0400 | [diff] [blame] | 322 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 323 | typecheckdef(n); |
Russ Cox | 680ee6a | 2009-10-08 23:03:34 -0700 | [diff] [blame] | 324 | if(n->op == ONONAME) |
| 325 | goto error; |
| 326 | } |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 327 | *np = n; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 328 | |
| 329 | reswitch: |
| 330 | ok = 0; |
| 331 | switch(n->op) { |
| 332 | default: |
| 333 | // until typecheck is complete, do nothing. |
| 334 | dump("typecheck", n); |
| 335 | fatal("typecheck %O", n->op); |
| 336 | |
| 337 | /* |
| 338 | * names |
| 339 | */ |
| 340 | case OLITERAL: |
| 341 | ok |= Erv; |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 342 | if(n->type == T && n->val.ctype == CTSTR) |
Russ Cox | 14be733 | 2009-08-24 13:41:47 -0700 | [diff] [blame] | 343 | n->type = idealstring; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 344 | goto ret; |
| 345 | |
| 346 | case ONONAME: |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 347 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 348 | goto ret; |
| 349 | |
| 350 | case ONAME: |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 351 | if(n->decldepth == 0) |
| 352 | n->decldepth = decldepth; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 353 | if(n->etype != 0) { |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 354 | ok |= Ecall; |
| 355 | goto ret; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 356 | } |
Russ Cox | 5991472 | 2009-09-14 18:38:30 -0700 | [diff] [blame] | 357 | if(!(top & Easgn)) { |
| 358 | // not a write to the variable |
| 359 | if(isblank(n)) { |
| 360 | yyerror("cannot use _ as value"); |
| 361 | goto error; |
| 362 | } |
| 363 | n->used = 1; |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 364 | } |
Luuk van Dijk | 924ea51 | 2011-11-09 18:30:54 +0100 | [diff] [blame] | 365 | if(!(top &Ecall) && isunsafebuiltin(n)) { |
| 366 | yyerror("%N is not an expression, must be called", n); |
| 367 | goto error; |
| 368 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 369 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 370 | goto ret; |
| 371 | |
Russ Cox | 3e98a40 | 2009-08-12 15:58:31 -0700 | [diff] [blame] | 372 | case OPACK: |
Brendan Daniel Tracey | d309496 | 2014-02-10 20:27:31 -0500 | [diff] [blame] | 373 | yyerror("use of package %S without selector", n->sym); |
Russ Cox | 3e98a40 | 2009-08-12 15:58:31 -0700 | [diff] [blame] | 374 | goto error; |
| 375 | |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 376 | case ODDD: |
| 377 | break; |
| 378 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 379 | /* |
| 380 | * types (OIND is with exprs) |
| 381 | */ |
| 382 | case OTYPE: |
| 383 | ok |= Etype; |
| 384 | if(n->type == T) |
| 385 | goto error; |
| 386 | break; |
Russ Cox | b1311cb | 2010-08-23 23:10:25 -0400 | [diff] [blame] | 387 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 388 | case OTARRAY: |
| 389 | ok |= Etype; |
| 390 | t = typ(TARRAY); |
| 391 | l = n->left; |
| 392 | r = n->right; |
| 393 | if(l == nil) { |
Russ Cox | 5ab8f00 | 2010-02-12 13:59:02 -0800 | [diff] [blame] | 394 | t->bound = -1; // slice |
| 395 | } else if(l->op == ODDD) { |
| 396 | t->bound = -100; // to be filled in |
Russ Cox | f607c47 | 2013-02-01 21:21:27 -0500 | [diff] [blame] | 397 | if(!(top&Ecomplit) && !n->diag) { |
| 398 | t->broke = 1; |
| 399 | n->diag = 1; |
Russ Cox | bf899be | 2011-07-26 00:52:02 -0400 | [diff] [blame] | 400 | yyerror("use of [...] array outside of array literal"); |
Russ Cox | f607c47 | 2013-02-01 21:21:27 -0500 | [diff] [blame] | 401 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 402 | } else { |
Russ Cox | 5ab8f00 | 2010-02-12 13:59:02 -0800 | [diff] [blame] | 403 | l = typecheck(&n->left, Erv); |
| 404 | switch(consttype(l)) { |
| 405 | case CTINT: |
Russ Cox | be0ffbf | 2011-12-08 22:07:43 -0500 | [diff] [blame] | 406 | case CTRUNE: |
Russ Cox | 5ab8f00 | 2010-02-12 13:59:02 -0800 | [diff] [blame] | 407 | v = l->val; |
| 408 | break; |
| 409 | case CTFLT: |
| 410 | v = toint(l->val); |
| 411 | break; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 412 | default: |
Russ Cox | 8d61334 | 2014-09-16 10:21:54 -0400 | [diff] [blame] | 413 | if(l->type != T && isint[l->type->etype] && l->op != OLITERAL) |
| 414 | yyerror("non-constant array bound %N", l); |
| 415 | else |
| 416 | yyerror("invalid array bound %N", l); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 417 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 418 | } |
Russ Cox | 5ab8f00 | 2010-02-12 13:59:02 -0800 | [diff] [blame] | 419 | t->bound = mpgetfix(v.u.xval); |
Daniel Morsing | bf9a00b | 2013-06-01 16:33:54 +0200 | [diff] [blame] | 420 | if(doesoverflow(v, types[TINT])) { |
Daniel Morsing | a7a3fe7 | 2012-10-21 19:22:51 +0200 | [diff] [blame] | 421 | yyerror("array bound is too large"); |
| 422 | goto error; |
Daniel Morsing | bf9a00b | 2013-06-01 16:33:54 +0200 | [diff] [blame] | 423 | } else if(t->bound < 0) { |
| 424 | yyerror("array bound must be non-negative"); |
| 425 | goto error; |
Daniel Morsing | a7a3fe7 | 2012-10-21 19:22:51 +0200 | [diff] [blame] | 426 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 427 | } |
| 428 | typecheck(&r, Etype); |
| 429 | if(r->type == T) |
| 430 | goto error; |
| 431 | t->type = r->type; |
| 432 | n->op = OTYPE; |
| 433 | n->type = t; |
| 434 | n->left = N; |
| 435 | n->right = N; |
Russ Cox | a1391c2 | 2009-09-02 23:26:13 -0700 | [diff] [blame] | 436 | if(t->bound != -100) |
| 437 | checkwidth(t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 438 | break; |
| 439 | |
| 440 | case OTMAP: |
| 441 | ok |= Etype; |
| 442 | l = typecheck(&n->left, Etype); |
| 443 | r = typecheck(&n->right, Etype); |
| 444 | if(l->type == T || r->type == T) |
| 445 | goto error; |
| 446 | n->op = OTYPE; |
| 447 | n->type = maptype(l->type, r->type); |
| 448 | n->left = N; |
| 449 | n->right = N; |
| 450 | break; |
| 451 | |
| 452 | case OTCHAN: |
| 453 | ok |= Etype; |
| 454 | l = typecheck(&n->left, Etype); |
| 455 | if(l->type == T) |
| 456 | goto error; |
| 457 | t = typ(TCHAN); |
| 458 | t->type = l->type; |
| 459 | t->chan = n->etype; |
| 460 | n->op = OTYPE; |
| 461 | n->type = t; |
| 462 | n->left = N; |
| 463 | n->etype = 0; |
| 464 | break; |
| 465 | |
| 466 | case OTSTRUCT: |
| 467 | ok |= Etype; |
| 468 | n->op = OTYPE; |
Luuk van Dijk | 087bec3 | 2011-11-07 21:35:13 +0100 | [diff] [blame] | 469 | n->type = tostruct(n->list); |
Rémy Oudompheng | c208a3a | 2012-11-07 23:09:01 +0100 | [diff] [blame] | 470 | if(n->type == T || n->type->broke) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 471 | goto error; |
| 472 | n->list = nil; |
| 473 | break; |
| 474 | |
| 475 | case OTINTER: |
| 476 | ok |= Etype; |
| 477 | n->op = OTYPE; |
Luuk van Dijk | 087bec3 | 2011-11-07 21:35:13 +0100 | [diff] [blame] | 478 | n->type = tointerface(n->list); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 479 | if(n->type == T) |
| 480 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 481 | break; |
| 482 | |
| 483 | case OTFUNC: |
| 484 | ok |= Etype; |
| 485 | n->op = OTYPE; |
| 486 | n->type = functype(n->left, n->list, n->rlist); |
| 487 | if(n->type == T) |
| 488 | goto error; |
| 489 | break; |
| 490 | |
| 491 | /* |
| 492 | * type or expr |
| 493 | */ |
| 494 | case OIND: |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 495 | ntop = Erv | Etype; |
Luuk van Dijk | 847b61b | 2011-08-24 19:07:08 +0200 | [diff] [blame] | 496 | if(!(top & Eaddr)) // The *x in &*x is not an indirect. |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 497 | ntop |= Eindir; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 498 | ntop |= top & Ecomplit; |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 499 | l = typecheck(&n->left, ntop); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 500 | if((t = l->type) == T) |
| 501 | goto error; |
| 502 | if(l->op == OTYPE) { |
| 503 | ok |= Etype; |
| 504 | n->op = OTYPE; |
| 505 | n->type = ptrto(l->type); |
| 506 | n->left = N; |
| 507 | goto ret; |
| 508 | } |
Daniel Morsing | c0d9bf5 | 2013-01-18 22:46:10 +0100 | [diff] [blame] | 509 | if(!isptr[t->etype]) { |
| 510 | if(top & (Erv | Etop)) { |
| 511 | yyerror("invalid indirect of %lN", n->left); |
| 512 | goto error; |
| 513 | } |
| 514 | goto ret; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 515 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 516 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 517 | n->type = t->type; |
| 518 | goto ret; |
| 519 | |
| 520 | /* |
| 521 | * arithmetic exprs |
| 522 | */ |
| 523 | case OASOP: |
| 524 | ok |= Etop; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 525 | l = typecheck(&n->left, Erv); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 526 | r = typecheck(&n->right, Erv); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 527 | checkassign(n, n->left); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 528 | if(l->type == T || r->type == T) |
| 529 | goto error; |
| 530 | op = n->etype; |
| 531 | goto arith; |
| 532 | |
| 533 | case OADD: |
| 534 | case OAND: |
| 535 | case OANDAND: |
| 536 | case OANDNOT: |
| 537 | case ODIV: |
| 538 | case OEQ: |
| 539 | case OGE: |
| 540 | case OGT: |
| 541 | case OLE: |
| 542 | case OLT: |
| 543 | case OLSH: |
| 544 | case ORSH: |
| 545 | case OMOD: |
| 546 | case OMUL: |
| 547 | case ONE: |
| 548 | case OOR: |
| 549 | case OOROR: |
| 550 | case OSUB: |
| 551 | case OXOR: |
| 552 | ok |= Erv; |
Russ Cox | 866b272 | 2009-08-11 17:05:22 -0700 | [diff] [blame] | 553 | l = typecheck(&n->left, Erv | (top & Eiota)); |
| 554 | r = typecheck(&n->right, Erv | (top & Eiota)); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 555 | if(l->type == T || r->type == T) |
| 556 | goto error; |
| 557 | op = n->op; |
| 558 | arith: |
| 559 | if(op == OLSH || op == ORSH) |
| 560 | goto shift; |
| 561 | // ideal mixed with non-ideal |
| 562 | defaultlit2(&l, &r, 0); |
| 563 | n->left = l; |
| 564 | n->right = r; |
Russ Cox | 83bdb80 | 2009-09-09 01:21:20 -0700 | [diff] [blame] | 565 | if(l->type == T || r->type == T) |
| 566 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 567 | t = l->type; |
| 568 | if(t->etype == TIDEAL) |
| 569 | t = r->type; |
| 570 | et = t->etype; |
| 571 | if(et == TIDEAL) |
| 572 | et = TINT; |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 573 | if(iscmp[n->op] && t->etype != TIDEAL && !eqtype(l->type, r->type)) { |
| 574 | // comparison is okay as long as one side is |
| 575 | // assignable to the other. convert so they have |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 576 | // the same type. |
| 577 | // |
| 578 | // the only conversion that isn't a no-op is concrete == interface. |
| 579 | // in that case, check comparability of the concrete type. |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 580 | if(r->type->etype != TBLANK && (aop = assignop(l->type, r->type, nil)) != 0) { |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 581 | if(isinter(r->type) && !isinter(l->type) && algtype1(l->type, nil) == ANOEQ) { |
| 582 | yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(l->type)); |
| 583 | goto error; |
| 584 | } |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 585 | l = nod(aop, l, N); |
| 586 | l->type = r->type; |
| 587 | l->typecheck = 1; |
| 588 | n->left = l; |
| 589 | t = l->type; |
Russ Cox | e82003e | 2015-01-21 22:19:15 -0500 | [diff] [blame] | 590 | goto converted; |
| 591 | } |
| 592 | if(l->type->etype != TBLANK && (aop = assignop(r->type, l->type, nil)) != 0) { |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 593 | if(isinter(l->type) && !isinter(r->type) && algtype1(r->type, nil) == ANOEQ) { |
| 594 | yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(r->type)); |
| 595 | goto error; |
| 596 | } |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 597 | r = nod(aop, r, N); |
| 598 | r->type = l->type; |
| 599 | r->typecheck = 1; |
| 600 | n->right = r; |
| 601 | t = r->type; |
| 602 | } |
Russ Cox | e82003e | 2015-01-21 22:19:15 -0500 | [diff] [blame] | 603 | converted: |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 604 | et = t->etype; |
| 605 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 606 | if(t->etype != TIDEAL && !eqtype(l->type, r->type)) { |
Russ Cox | b754b43 | 2009-07-30 18:56:44 -0700 | [diff] [blame] | 607 | defaultlit2(&l, &r, 1); |
Russ Cox | 52e9bca | 2014-09-25 13:13:02 -0400 | [diff] [blame] | 608 | if(n->op == OASOP && n->implicit) { |
| 609 | yyerror("invalid operation: %N (non-numeric type %T)", n, l->type); |
| 610 | goto error; |
| 611 | } |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 612 | yyerror("invalid operation: %N (mismatched types %T and %T)", n, l->type, r->type); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 613 | goto error; |
| 614 | } |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 615 | if(!okfor[op][et]) { |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 616 | yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(t)); |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 617 | goto error; |
| 618 | } |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 619 | // okfor allows any array == array, map == map, func == func. |
| 620 | // restrict to slice/map/func == nil and nil == slice/map/func. |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 621 | if(isfixedarray(l->type) && algtype1(l->type, nil) == ANOEQ) { |
| 622 | yyerror("invalid operation: %N (%T cannot be compared)", n, l->type); |
| 623 | goto error; |
| 624 | } |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 625 | if(isslice(l->type) && !isnil(l) && !isnil(r)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 626 | yyerror("invalid operation: %N (slice can only be compared to nil)", n); |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 627 | goto error; |
| 628 | } |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 629 | if(l->type->etype == TMAP && !isnil(l) && !isnil(r)) { |
| 630 | yyerror("invalid operation: %N (map can only be compared to nil)", n); |
| 631 | goto error; |
| 632 | } |
| 633 | if(l->type->etype == TFUNC && !isnil(l) && !isnil(r)) { |
| 634 | yyerror("invalid operation: %N (func can only be compared to nil)", n); |
| 635 | goto error; |
| 636 | } |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 637 | if(l->type->etype == TSTRUCT && algtype1(l->type, &badtype) == ANOEQ) { |
| 638 | yyerror("invalid operation: %N (struct containing %T cannot be compared)", n, badtype); |
| 639 | goto error; |
| 640 | } |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 641 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 642 | t = l->type; |
Russ Cox | b754b43 | 2009-07-30 18:56:44 -0700 | [diff] [blame] | 643 | if(iscmp[n->op]) { |
Russ Cox | b754b43 | 2009-07-30 18:56:44 -0700 | [diff] [blame] | 644 | evconst(n); |
Russ Cox | e29d3df | 2012-02-22 00:29:37 -0500 | [diff] [blame] | 645 | t = idealbool; |
Russ Cox | b754b43 | 2009-07-30 18:56:44 -0700 | [diff] [blame] | 646 | if(n->op != OLITERAL) { |
| 647 | defaultlit2(&l, &r, 1); |
| 648 | n->left = l; |
| 649 | n->right = r; |
| 650 | } |
Chris Manghane | 5cc29ab | 2014-12-09 07:16:38 -0800 | [diff] [blame] | 651 | } else if(n->op == OANDAND || n->op == OOROR) { |
| 652 | if(l->type == r->type) |
| 653 | t = l->type; |
| 654 | else if(l->type == idealbool) |
| 655 | t = r->type; |
| 656 | else if(r->type == idealbool) |
| 657 | t = l->type; |
Daniel Morsing | 5188c0b | 2012-11-26 22:23:13 +0100 | [diff] [blame] | 658 | // non-comparison operators on ideal bools should make them lose their ideal-ness |
| 659 | } else if(t == idealbool) |
| 660 | t = types[TBOOL]; |
| 661 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 662 | if(et == TSTRING) { |
| 663 | if(iscmp[n->op]) { |
| 664 | n->etype = n->op; |
| 665 | n->op = OCMPSTR; |
Russ Cox | daca06f | 2014-04-01 20:02:54 -0400 | [diff] [blame] | 666 | } else if(n->op == OADD) { |
| 667 | // create OADDSTR node with list of strings in x + y + z + (w + v) + ... |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 668 | n->op = OADDSTR; |
Russ Cox | daca06f | 2014-04-01 20:02:54 -0400 | [diff] [blame] | 669 | if(l->op == OADDSTR) |
| 670 | n->list = l->list; |
| 671 | else |
| 672 | n->list = list1(l); |
| 673 | if(r->op == OADDSTR) |
| 674 | n->list = concat(n->list, r->list); |
| 675 | else |
| 676 | n->list = list(n->list, r); |
| 677 | n->left = N; |
| 678 | n->right = N; |
| 679 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 680 | } |
| 681 | if(et == TINTER) { |
Russ Cox | 89f69bb | 2009-08-24 15:20:37 -0700 | [diff] [blame] | 682 | if(l->op == OLITERAL && l->val.ctype == CTNIL) { |
| 683 | // swap for back end |
| 684 | n->left = r; |
| 685 | n->right = l; |
| 686 | } else if(r->op == OLITERAL && r->val.ctype == CTNIL) { |
| 687 | // leave alone for back end |
| 688 | } else { |
| 689 | n->etype = n->op; |
| 690 | n->op = OCMPIFACE; |
| 691 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 692 | } |
Daniel Morsing | ba05a43 | 2013-01-30 20:21:08 +0100 | [diff] [blame] | 693 | |
| 694 | if((op == ODIV || op == OMOD) && isconst(r, CTINT)) |
| 695 | if(mpcmpfixc(r->val.u.xval, 0) == 0) { |
| 696 | yyerror("division by zero"); |
| 697 | goto error; |
| 698 | } |
| 699 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 700 | n->type = t; |
| 701 | goto ret; |
| 702 | |
| 703 | shift: |
| 704 | defaultlit(&r, types[TUINT]); |
| 705 | n->right = r; |
| 706 | t = r->type; |
| 707 | if(!isint[t->etype] || issigned[t->etype]) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 708 | yyerror("invalid operation: %N (shift count type %T, must be unsigned integer)", n, r->type); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 709 | goto error; |
| 710 | } |
Russ Cox | ac9d833 | 2009-08-23 18:09:44 -0700 | [diff] [blame] | 711 | t = l->type; |
| 712 | if(t != T && t->etype != TIDEAL && !isint[t->etype]) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 713 | yyerror("invalid operation: %N (shift of type %T)", n, t); |
Russ Cox | ac9d833 | 2009-08-23 18:09:44 -0700 | [diff] [blame] | 714 | goto error; |
| 715 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 716 | // no defaultlit for left |
| 717 | // the outer context gives the type |
| 718 | n->type = l->type; |
| 719 | goto ret; |
| 720 | |
| 721 | case OCOM: |
| 722 | case OMINUS: |
| 723 | case ONOT: |
| 724 | case OPLUS: |
| 725 | ok |= Erv; |
Russ Cox | 866b272 | 2009-08-11 17:05:22 -0700 | [diff] [blame] | 726 | l = typecheck(&n->left, Erv | (top & Eiota)); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 727 | if((t = l->type) == T) |
| 728 | goto error; |
| 729 | if(!okfor[n->op][t->etype]) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 730 | yyerror("invalid operation: %O %T", n->op, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 731 | goto error; |
| 732 | } |
| 733 | n->type = t; |
| 734 | goto ret; |
| 735 | |
| 736 | /* |
| 737 | * exprs |
| 738 | */ |
| 739 | case OADDR: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 740 | ok |= Erv; |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 741 | typecheck(&n->left, Erv | Eaddr); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 742 | if(n->left->type == T) |
| 743 | goto error; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 744 | checklvalue(n->left, "take the address of"); |
Russ Cox | 1a3ee67 | 2014-02-15 20:01:15 -0500 | [diff] [blame] | 745 | r = outervalue(n->left); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 746 | for(l = n->left; l != r; l = l->left) { |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 747 | l->addrtaken = 1; |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 748 | if(l->closure) |
| 749 | l->closure->addrtaken = 1; |
| 750 | } |
Russ Cox | 334056a | 2014-01-14 10:43:13 -0500 | [diff] [blame] | 751 | if(l->orig != l && l->op == ONAME) |
| 752 | fatal("found non-orig name node %N", l); |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 753 | l->addrtaken = 1; |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 754 | if(l->closure) |
| 755 | l->closure->addrtaken = 1; |
Russ Cox | a2b8e38 | 2009-07-31 09:29:28 -0700 | [diff] [blame] | 756 | defaultlit(&n->left, T); |
| 757 | l = n->left; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 758 | if((t = l->type) == T) |
| 759 | goto error; |
| 760 | n->type = ptrto(t); |
| 761 | goto ret; |
| 762 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 763 | case OCOMPLIT: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 764 | ok |= Erv; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 765 | typecheckcomplit(&n); |
| 766 | if(n->type == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 767 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 768 | goto ret; |
| 769 | |
Russ Cox | 2609731 | 2009-08-05 02:33:30 -0700 | [diff] [blame] | 770 | case OXDOT: |
| 771 | n = adddot(n); |
| 772 | n->op = ODOT; |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 773 | if(n->left == N) |
| 774 | goto error; |
Russ Cox | 2609731 | 2009-08-05 02:33:30 -0700 | [diff] [blame] | 775 | // fall through |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 776 | case ODOT: |
Russ Cox | c6f4d68 | 2010-01-07 23:20:00 -0800 | [diff] [blame] | 777 | typecheck(&n->left, Erv|Etype); |
| 778 | defaultlit(&n->left, T); |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 779 | if((t = n->left->type) == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 780 | goto error; |
| 781 | if(n->right->op != ONAME) { |
| 782 | yyerror("rhs of . must be a name"); // impossible |
| 783 | goto error; |
| 784 | } |
Russ Cox | d3c758d | 2013-03-20 17:11:09 -0400 | [diff] [blame] | 785 | r = n->right; |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 786 | |
| 787 | if(n->left->op == OTYPE) { |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 788 | if(!looktypedot(n, t, 0)) { |
| 789 | if(looktypedot(n, t, 1)) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 790 | yyerror("%N undefined (cannot refer to unexported method %S)", n, n->right->sym); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 791 | else |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 792 | yyerror("%N undefined (type %T has no method %S)", n, t, n->right->sym); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 793 | goto error; |
| 794 | } |
| 795 | if(n->type->etype != TFUNC || n->type->thistuple != 1) { |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 796 | yyerror("type %T has no method %hS", n->left->type, n->right->sym); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 797 | n->type = T; |
| 798 | goto error; |
| 799 | } |
| 800 | n->op = ONAME; |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 801 | n->sym = n->right->sym; |
| 802 | n->type = methodfunc(n->type, n->left->type); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 803 | n->xoffset = 0; |
| 804 | n->class = PFUNC; |
| 805 | ok = Erv; |
| 806 | goto ret; |
| 807 | } |
Russ Cox | bee2d5b | 2010-09-30 14:59:41 -0400 | [diff] [blame] | 808 | if(isptr[t->etype] && t->type->etype != TINTER) { |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 809 | t = t->type; |
| 810 | if(t == T) |
| 811 | goto error; |
| 812 | n->op = ODOTPTR; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 813 | checkwidth(t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 814 | } |
Daniel Morsing | b65acae | 2013-03-04 17:01:42 +0100 | [diff] [blame] | 815 | if(isblank(n->right)) { |
| 816 | yyerror("cannot refer to blank field or method"); |
| 817 | goto error; |
| 818 | } |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 819 | if(!lookdot(n, t, 0)) { |
| 820 | if(lookdot(n, t, 1)) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 821 | yyerror("%N undefined (cannot refer to unexported field or method %S)", n, n->right->sym); |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 822 | else |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 823 | yyerror("%N undefined (type %T has no field or method %S)", n, n->left->type, n->right->sym); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 824 | goto error; |
| 825 | } |
| 826 | switch(n->op) { |
| 827 | case ODOTINTER: |
| 828 | case ODOTMETH: |
Russ Cox | d3c758d | 2013-03-20 17:11:09 -0400 | [diff] [blame] | 829 | if(top&Ecall) |
| 830 | ok |= Ecall; |
| 831 | else { |
| 832 | typecheckpartialcall(n, r); |
| 833 | ok |= Erv; |
| 834 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 835 | break; |
| 836 | default: |
| 837 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | goto ret; |
| 841 | |
| 842 | case ODOTTYPE: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 843 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 844 | typecheck(&n->left, Erv); |
| 845 | defaultlit(&n->left, T); |
| 846 | l = n->left; |
| 847 | if((t = l->type) == T) |
| 848 | goto error; |
| 849 | if(!isinter(t)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 850 | yyerror("invalid type assertion: %N (non-interface type %T on left)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 851 | goto error; |
| 852 | } |
| 853 | if(n->right != N) { |
| 854 | typecheck(&n->right, Etype); |
| 855 | n->type = n->right->type; |
| 856 | n->right = N; |
| 857 | if(n->type == T) |
| 858 | goto error; |
| 859 | } |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 860 | if(n->type != T && n->type->etype != TINTER) |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 861 | if(!implements(n->type, t, &missing, &have, &ptr)) { |
Daniel Morsing | 1c2021c | 2012-09-01 13:52:55 -0400 | [diff] [blame] | 862 | if(have && have->sym == missing->sym) |
| 863 | yyerror("impossible type assertion:\n\t%T does not implement %T (wrong type for %S method)\n" |
| 864 | "\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym, |
| 865 | have->sym, have->type, missing->sym, missing->type); |
| 866 | else if(ptr) |
Russ Cox | 2254785 | 2013-07-15 20:39:07 -0400 | [diff] [blame] | 867 | yyerror("impossible type assertion:\n\t%T does not implement %T (%S method has pointer receiver)", |
Daniel Morsing | 1c2021c | 2012-09-01 13:52:55 -0400 | [diff] [blame] | 868 | n->type, t, missing->sym); |
| 869 | else if(have) |
| 870 | yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)\n" |
| 871 | "\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym, |
| 872 | have->sym, have->type, missing->sym, missing->type); |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 873 | else |
Daniel Morsing | 1c2021c | 2012-09-01 13:52:55 -0400 | [diff] [blame] | 874 | yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)", |
| 875 | n->type, t, missing->sym); |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 876 | goto error; |
| 877 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 878 | goto ret; |
| 879 | |
| 880 | case OINDEX: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 881 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 882 | typecheck(&n->left, Erv); |
| 883 | defaultlit(&n->left, T); |
| 884 | implicitstar(&n->left); |
| 885 | l = n->left; |
| 886 | typecheck(&n->right, Erv); |
| 887 | r = n->right; |
| 888 | if((t = l->type) == T || r->type == T) |
| 889 | goto error; |
| 890 | switch(t->etype) { |
| 891 | default: |
Shenghou Ma | 656a402 | 2014-02-13 21:01:33 -0500 | [diff] [blame] | 892 | yyerror("invalid operation: %N (type %T does not support indexing)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 893 | goto error; |
| 894 | |
Russ Cox | d82dcad | 2013-02-03 02:01:05 -0500 | [diff] [blame] | 895 | |
| 896 | case TSTRING: |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 897 | case TARRAY: |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 898 | indexlit(&n->right); |
Russ Cox | d82dcad | 2013-02-03 02:01:05 -0500 | [diff] [blame] | 899 | if(t->etype == TSTRING) |
| 900 | n->type = types[TUINT8]; |
| 901 | else |
| 902 | n->type = t->type; |
| 903 | why = "string"; |
| 904 | if(t->etype == TARRAY) { |
| 905 | if(isfixedarray(t)) |
| 906 | why = "array"; |
| 907 | else |
| 908 | why = "slice"; |
| 909 | } |
Daniel Morsing | 85d60a7 | 2012-11-01 18:45:19 +0100 | [diff] [blame] | 910 | if(n->right->type != T && !isint[n->right->type->etype]) { |
Russ Cox | d82dcad | 2013-02-03 02:01:05 -0500 | [diff] [blame] | 911 | yyerror("non-integer %s index %N", why, n->right); |
Daniel Morsing | 85d60a7 | 2012-11-01 18:45:19 +0100 | [diff] [blame] | 912 | break; |
| 913 | } |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 914 | if(isconst(n->right, CTINT)) { |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 915 | x = mpgetfix(n->right->val.u.xval); |
| 916 | if(x < 0) |
Daniel Morsing | 85d60a7 | 2012-11-01 18:45:19 +0100 | [diff] [blame] | 917 | yyerror("invalid %s index %N (index must be non-negative)", why, n->right); |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 918 | else if(isfixedarray(t) && t->bound > 0 && x >= t->bound) |
Daniel Morsing | 85d60a7 | 2012-11-01 18:45:19 +0100 | [diff] [blame] | 919 | yyerror("invalid array index %N (out of bounds for %d-element array)", n->right, t->bound); |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 920 | else if(isconst(n->left, CTSTR) && x >= n->left->val.u.sval->len) |
Russ Cox | d82dcad | 2013-02-03 02:01:05 -0500 | [diff] [blame] | 921 | yyerror("invalid string index %N (out of bounds for %d-byte string)", n->right, n->left->val.u.sval->len); |
| 922 | else if(mpcmpfixfix(n->right->val.u.xval, maxintval[TINT]) > 0) |
Ian Lance Taylor | e3977f0 | 2012-11-07 17:34:06 -0800 | [diff] [blame] | 923 | yyerror("invalid %s index %N (index too large)", why, n->right); |
Daniel Morsing | 85d60a7 | 2012-11-01 18:45:19 +0100 | [diff] [blame] | 924 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 925 | break; |
| 926 | |
| 927 | case TMAP: |
Russ Cox | a2b8e38 | 2009-07-31 09:29:28 -0700 | [diff] [blame] | 928 | n->etype = 0; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 929 | defaultlit(&n->right, t->down); |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 930 | if(n->right->type != T) |
| 931 | n->right = assignconv(n->right, t->down, "map index"); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 932 | n->type = t->type; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 933 | n->op = OINDEXMAP; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 934 | break; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 935 | } |
| 936 | goto ret; |
| 937 | |
| 938 | case ORECV: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 939 | ok |= Etop | Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 940 | typecheck(&n->left, Erv); |
| 941 | defaultlit(&n->left, T); |
| 942 | l = n->left; |
| 943 | if((t = l->type) == T) |
| 944 | goto error; |
| 945 | if(t->etype != TCHAN) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 946 | yyerror("invalid operation: %N (receive from non-chan type %T)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 947 | goto error; |
| 948 | } |
| 949 | if(!(t->chan & Crecv)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 950 | yyerror("invalid operation: %N (receive from send-only type %T)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 951 | goto error; |
| 952 | } |
| 953 | n->type = t->type; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 954 | goto ret; |
| 955 | |
| 956 | case OSEND: |
Russ Cox | e1ac157 | 2013-09-06 15:47:52 -0400 | [diff] [blame] | 957 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 958 | l = typecheck(&n->left, Erv); |
| 959 | typecheck(&n->right, Erv); |
| 960 | defaultlit(&n->left, T); |
| 961 | l = n->left; |
| 962 | if((t = l->type) == T) |
| 963 | goto error; |
Russ Cox | 42c26b7 | 2010-10-07 03:33:42 -0400 | [diff] [blame] | 964 | if(t->etype != TCHAN) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 965 | yyerror("invalid operation: %N (send to non-chan type %T)", n, t); |
Russ Cox | 42c26b7 | 2010-10-07 03:33:42 -0400 | [diff] [blame] | 966 | goto error; |
| 967 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 968 | if(!(t->chan & Csend)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 969 | yyerror("invalid operation: %N (send to receive-only type %T)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 970 | goto error; |
| 971 | } |
| 972 | defaultlit(&n->right, t->type); |
| 973 | r = n->right; |
Lucio De Re | b3cc489 | 2011-08-29 09:35:04 -0400 | [diff] [blame] | 974 | if(r->type == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 975 | goto error; |
Russ Cox | b700cb4 | 2014-04-01 13:31:38 -0400 | [diff] [blame] | 976 | n->right = assignconv(r, l->type->type, "send"); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 977 | // TODO: more aggressive |
Russ Cox | a2b8e38 | 2009-07-31 09:29:28 -0700 | [diff] [blame] | 978 | n->etype = 0; |
Russ Cox | a95ee61 | 2009-09-21 15:45:55 -0700 | [diff] [blame] | 979 | n->type = T; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 980 | goto ret; |
| 981 | |
| 982 | case OSLICE: |
| 983 | ok |= Erv; |
| 984 | typecheck(&n->left, top); |
| 985 | typecheck(&n->right->left, Erv); |
| 986 | typecheck(&n->right->right, Erv); |
| 987 | defaultlit(&n->left, T); |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 988 | indexlit(&n->right->left); |
| 989 | indexlit(&n->right->right); |
Ian Lance Taylor | 72bf3bc | 2012-11-06 11:35:58 -0800 | [diff] [blame] | 990 | l = n->left; |
| 991 | if(isfixedarray(l->type)) { |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 992 | if(!islvalue(n->left)) { |
| 993 | yyerror("invalid operation %N (slice of unaddressable value)", n); |
| 994 | goto error; |
| 995 | } |
Russ Cox | 10eb76b | 2010-04-29 15:52:27 -0700 | [diff] [blame] | 996 | n->left = nod(OADDR, n->left, N); |
Luuk van Dijk | 29a5ae6 | 2011-11-02 15:36:33 +0100 | [diff] [blame] | 997 | n->left->implicit = 1; |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 998 | typecheck(&n->left, Erv); |
Ian Lance Taylor | 72bf3bc | 2012-11-06 11:35:58 -0800 | [diff] [blame] | 999 | l = n->left; |
| 1000 | } |
| 1001 | if((t = l->type) == T) |
| 1002 | goto error; |
| 1003 | tp = nil; |
| 1004 | if(istype(t, TSTRING)) { |
| 1005 | n->type = t; |
| 1006 | n->op = OSLICESTR; |
| 1007 | } else if(isptr[t->etype] && isfixedarray(t->type)) { |
| 1008 | tp = t->type; |
| 1009 | n->type = typ(TARRAY); |
| 1010 | n->type->type = tp->type; |
| 1011 | n->type->bound = -1; |
| 1012 | dowidth(n->type); |
| 1013 | n->op = OSLICEARR; |
| 1014 | } else if(isslice(t)) { |
| 1015 | n->type = t; |
| 1016 | } else { |
| 1017 | yyerror("cannot slice %N (type %T)", l, t); |
| 1018 | goto error; |
Russ Cox | 10eb76b | 2010-04-29 15:52:27 -0700 | [diff] [blame] | 1019 | } |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1020 | if((lo = n->right->left) != N && checksliceindex(l, lo, tp) < 0) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1021 | goto error; |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1022 | if((hi = n->right->right) != N && checksliceindex(l, hi, tp) < 0) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1023 | goto error; |
| 1024 | if(checksliceconst(lo, hi) < 0) |
| 1025 | goto error; |
| 1026 | goto ret; |
| 1027 | |
| 1028 | case OSLICE3: |
| 1029 | ok |= Erv; |
| 1030 | typecheck(&n->left, top); |
| 1031 | typecheck(&n->right->left, Erv); |
| 1032 | typecheck(&n->right->right->left, Erv); |
| 1033 | typecheck(&n->right->right->right, Erv); |
| 1034 | defaultlit(&n->left, T); |
| 1035 | indexlit(&n->right->left); |
| 1036 | indexlit(&n->right->right->left); |
| 1037 | indexlit(&n->right->right->right); |
| 1038 | l = n->left; |
| 1039 | if(isfixedarray(l->type)) { |
| 1040 | if(!islvalue(n->left)) { |
| 1041 | yyerror("invalid operation %N (slice of unaddressable value)", n); |
Scott Lawrence | fce222a | 2010-09-08 22:20:29 -0400 | [diff] [blame] | 1042 | goto error; |
Russ Cox | 82ee481 | 2010-09-10 11:53:27 -0400 | [diff] [blame] | 1043 | } |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1044 | n->left = nod(OADDR, n->left, N); |
| 1045 | n->left->implicit = 1; |
| 1046 | typecheck(&n->left, Erv); |
| 1047 | l = n->left; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1048 | } |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1049 | if((t = l->type) == T) |
| 1050 | goto error; |
| 1051 | tp = nil; |
| 1052 | if(istype(t, TSTRING)) { |
| 1053 | yyerror("invalid operation %N (3-index slice of string)", n); |
Ian Lance Taylor | 08918ba | 2012-12-05 15:46:45 -0800 | [diff] [blame] | 1054 | goto error; |
| 1055 | } |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1056 | if(isptr[t->etype] && isfixedarray(t->type)) { |
| 1057 | tp = t->type; |
| 1058 | n->type = typ(TARRAY); |
| 1059 | n->type->type = tp->type; |
| 1060 | n->type->bound = -1; |
| 1061 | dowidth(n->type); |
| 1062 | n->op = OSLICE3ARR; |
| 1063 | } else if(isslice(t)) { |
| 1064 | n->type = t; |
| 1065 | } else { |
| 1066 | yyerror("cannot slice %N (type %T)", l, t); |
| 1067 | goto error; |
| 1068 | } |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1069 | if((lo = n->right->left) != N && checksliceindex(l, lo, tp) < 0) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1070 | goto error; |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1071 | if((mid = n->right->right->left) != N && checksliceindex(l, mid, tp) < 0) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1072 | goto error; |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1073 | if((hi = n->right->right->right) != N && checksliceindex(l, hi, tp) < 0) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1074 | goto error; |
| 1075 | if(checksliceconst(lo, hi) < 0 || checksliceconst(lo, mid) < 0 || checksliceconst(mid, hi) < 0) |
| 1076 | goto error; |
Ian Lance Taylor | 72bf3bc | 2012-11-06 11:35:58 -0800 | [diff] [blame] | 1077 | goto ret; |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 1078 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1079 | /* |
| 1080 | * call and call like |
| 1081 | */ |
| 1082 | case OCALL: |
| 1083 | l = n->left; |
Russ Cox | cdb446f | 2010-07-15 16:13:47 -0700 | [diff] [blame] | 1084 | if(l->op == ONAME && (r = unsafenmagic(n)) != N) { |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 1085 | if(n->isddd) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1086 | yyerror("invalid use of ... with builtin %N", l); |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 1087 | n = r; |
| 1088 | goto reswitch; |
| 1089 | } |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 1090 | typecheck(&n->left, Erv | Etype | Ecall |(top&Eproc)); |
Jan Ziak | 1483747 | 2014-03-14 16:42:42 +0100 | [diff] [blame] | 1091 | n->diag |= n->left->diag; |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 1092 | l = n->left; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1093 | if(l->op == ONAME && l->etype != 0) { |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1094 | if(n->isddd && l->etype != OAPPEND) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1095 | yyerror("invalid use of ... with builtin %N", l); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1096 | // builtin: OLEN, OCAP, etc. |
| 1097 | n->op = l->etype; |
| 1098 | n->left = n->right; |
| 1099 | n->right = N; |
| 1100 | goto reswitch; |
| 1101 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1102 | defaultlit(&n->left, T); |
| 1103 | l = n->left; |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 1104 | if(l->op == OTYPE) { |
Russ Cox | f607c47 | 2013-02-01 21:21:27 -0500 | [diff] [blame] | 1105 | if(n->isddd || l->type->bound == -100) { |
| 1106 | if(!l->type->broke) |
| 1107 | yyerror("invalid use of ... in type conversion", l); |
| 1108 | n->diag = 1; |
| 1109 | } |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 1110 | // pick off before type-checking arguments |
| 1111 | ok |= Erv; |
| 1112 | // turn CALL(type, arg) into CONV(arg) w/ type |
| 1113 | n->left = N; |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 1114 | n->op = OCONV; |
| 1115 | n->type = l->type; |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1116 | if(onearg(n, "conversion to %T", l->type) < 0) |
| 1117 | goto error; |
Russ Cox | a75e347 | 2009-10-15 16:03:01 -0700 | [diff] [blame] | 1118 | goto doconv; |
| 1119 | } |
| 1120 | |
Rémy Oudompheng | 656b192 | 2012-07-13 08:05:41 +0200 | [diff] [blame] | 1121 | if(count(n->list) == 1 && !n->isddd) |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1122 | typecheck(&n->list->n, Erv | Efnstruct); |
| 1123 | else |
| 1124 | typechecklist(n->list, Erv); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1125 | if((t = l->type) == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1126 | goto error; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1127 | checkwidth(t); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1128 | |
| 1129 | switch(l->op) { |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1130 | case ODOTINTER: |
| 1131 | n->op = OCALLINTER; |
| 1132 | break; |
| 1133 | |
| 1134 | case ODOTMETH: |
| 1135 | n->op = OCALLMETH; |
Anthony Martin | 0b209b3 | 2011-05-24 19:48:19 -0400 | [diff] [blame] | 1136 | // typecheckaste was used here but there wasn't enough |
| 1137 | // information further down the call chain to know if we |
| 1138 | // were testing a method receiver for unexported fields. |
| 1139 | // It isn't necessary, so just do a sanity check. |
| 1140 | tp = getthisx(t)->type->type; |
| 1141 | if(l->left == N || !eqtype(l->left->type, tp)) |
| 1142 | fatal("method receiver"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | |
| 1145 | default: |
| 1146 | n->op = OCALLFUNC; |
| 1147 | if(t->etype != TFUNC) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1148 | yyerror("cannot call non-function %N (type %T)", l, t); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1149 | goto error; |
| 1150 | } |
| 1151 | break; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1152 | } |
Jan Ziak | a599b48 | 2014-04-11 15:57:30 +0200 | [diff] [blame] | 1153 | if(snprint(descbuf, sizeof descbuf, "argument to %N", n->left) < sizeof descbuf) |
| 1154 | desc = descbuf; |
| 1155 | else |
| 1156 | desc = "function argument"; |
| 1157 | typecheckaste(OCALL, n->left, n->isddd, getinargx(t), n->list, desc); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1158 | ok |= Etop; |
| 1159 | if(t->outtuple == 0) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1160 | goto ret; |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1161 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1162 | if(t->outtuple == 1) { |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1163 | t = getoutargx(l->type)->type; |
Russ Cox | 680ee6a | 2009-10-08 23:03:34 -0700 | [diff] [blame] | 1164 | if(t == T) |
| 1165 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1166 | if(t->etype == TFIELD) |
| 1167 | t = t->type; |
| 1168 | n->type = t; |
| 1169 | goto ret; |
| 1170 | } |
| 1171 | // multiple return |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1172 | if(!(top & (Efnstruct | Etop))) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1173 | yyerror("multiple-value %N() in single-value context", l); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1174 | goto ret; |
| 1175 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1176 | n->type = getoutargx(l->type); |
| 1177 | goto ret; |
| 1178 | |
| 1179 | case OCAP: |
| 1180 | case OLEN: |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1181 | case OREAL: |
| 1182 | case OIMAG: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1183 | ok |= Erv; |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1184 | if(onearg(n, "%O", n->op) < 0) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1185 | goto error; |
| 1186 | typecheck(&n->left, Erv); |
| 1187 | defaultlit(&n->left, T); |
| 1188 | implicitstar(&n->left); |
| 1189 | l = n->left; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1190 | t = l->type; |
| 1191 | if(t == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1192 | goto error; |
| 1193 | switch(n->op) { |
| 1194 | case OCAP: |
| 1195 | if(!okforcap[t->etype]) |
| 1196 | goto badcall1; |
| 1197 | break; |
| 1198 | case OLEN: |
| 1199 | if(!okforlen[t->etype]) |
| 1200 | goto badcall1; |
| 1201 | break; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1202 | case OREAL: |
| 1203 | case OIMAG: |
| 1204 | if(!iscomplex[t->etype]) |
| 1205 | goto badcall1; |
Anthony Martin | 94df1a0 | 2011-01-05 13:12:30 -0500 | [diff] [blame] | 1206 | if(isconst(l, CTCPLX)){ |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1207 | r = n; |
Anthony Martin | 94df1a0 | 2011-01-05 13:12:30 -0500 | [diff] [blame] | 1208 | if(n->op == OREAL) |
| 1209 | n = nodfltconst(&l->val.u.cval->real); |
| 1210 | else |
| 1211 | n = nodfltconst(&l->val.u.cval->imag); |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1212 | n->orig = r; |
Anthony Martin | 94df1a0 | 2011-01-05 13:12:30 -0500 | [diff] [blame] | 1213 | } |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1214 | n->type = types[cplxsubtype(t->etype)]; |
| 1215 | goto ret; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1216 | } |
| 1217 | // might be constant |
| 1218 | switch(t->etype) { |
| 1219 | case TSTRING: |
Russ Cox | 8133cb3 | 2011-04-28 13:14:35 -0400 | [diff] [blame] | 1220 | if(isconst(l, CTSTR)) { |
| 1221 | r = nod(OXXX, N, N); |
| 1222 | nodconst(r, types[TINT], l->val.u.sval->len); |
| 1223 | r->orig = n; |
| 1224 | n = r; |
| 1225 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | case TARRAY: |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 1228 | if(t->bound < 0) // slice |
| 1229 | break; |
| 1230 | if(callrecv(l)) // has call or receive |
| 1231 | break; |
| 1232 | r = nod(OXXX, N, N); |
| 1233 | nodconst(r, types[TINT], t->bound); |
| 1234 | r->orig = n; |
| 1235 | n = r; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1236 | break; |
| 1237 | } |
| 1238 | n->type = types[TINT]; |
| 1239 | goto ret; |
| 1240 | |
Russ Cox | 0849944 | 2011-01-19 23:08:11 -0500 | [diff] [blame] | 1241 | case OCOMPLEX: |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1242 | ok |= Erv; |
Chris Manghane | 671cc6e | 2014-03-05 14:16:21 -0500 | [diff] [blame] | 1243 | if(count(n->list) == 1) { |
| 1244 | typechecklist(n->list, Efnstruct); |
Shenghou Ma | cb1897a | 2014-12-28 19:18:54 -0500 | [diff] [blame] | 1245 | if(n->list->n->op != OCALLFUNC && n->list->n->op != OCALLMETH) { |
| 1246 | yyerror("invalid operation: complex expects two arguments"); |
| 1247 | goto error; |
| 1248 | } |
Chris Manghane | 671cc6e | 2014-03-05 14:16:21 -0500 | [diff] [blame] | 1249 | t = n->list->n->left->type; |
| 1250 | if(t->outtuple != 2) { |
| 1251 | yyerror("invalid operation: complex expects two arguments, %N returns %d results", n->list->n, t->outtuple); |
| 1252 | goto error; |
| 1253 | } |
| 1254 | t = n->list->n->type->type; |
| 1255 | l = t->nname; |
| 1256 | r = t->down->nname; |
| 1257 | } else { |
| 1258 | if(twoarg(n) < 0) |
| 1259 | goto error; |
| 1260 | l = typecheck(&n->left, Erv | (top & Eiota)); |
| 1261 | r = typecheck(&n->right, Erv | (top & Eiota)); |
| 1262 | if(l->type == T || r->type == T) |
| 1263 | goto error; |
| 1264 | defaultlit2(&l, &r, 0); |
| 1265 | if(l->type == T || r->type == T) |
| 1266 | goto error; |
| 1267 | n->left = l; |
| 1268 | n->right = r; |
| 1269 | } |
Rémy Oudompheng | 401e0fe | 2013-03-11 22:55:14 +0100 | [diff] [blame] | 1270 | if(!eqtype(l->type, r->type)) { |
Rémy Oudompheng | 401e0fe | 2013-03-11 22:55:14 +0100 | [diff] [blame] | 1271 | yyerror("invalid operation: %N (mismatched types %T and %T)", n, l->type, r->type); |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1272 | goto error; |
| 1273 | } |
| 1274 | switch(l->type->etype) { |
| 1275 | default: |
Rémy Oudompheng | 861aa46 | 2013-03-16 00:37:28 +0100 | [diff] [blame] | 1276 | yyerror("invalid operation: %N (arguments have type %T, expected floating-point)", n, l->type, r->type); |
| 1277 | goto error; |
Russ Cox | 4ac011a | 2010-03-08 15:44:18 -0800 | [diff] [blame] | 1278 | case TIDEAL: |
| 1279 | t = types[TIDEAL]; |
| 1280 | break; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1281 | case TFLOAT32: |
Russ Cox | 4ac011a | 2010-03-08 15:44:18 -0800 | [diff] [blame] | 1282 | t = types[TCOMPLEX64]; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1283 | break; |
| 1284 | case TFLOAT64: |
Russ Cox | 4ac011a | 2010-03-08 15:44:18 -0800 | [diff] [blame] | 1285 | t = types[TCOMPLEX128]; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1286 | break; |
| 1287 | } |
Russ Cox | 4ac011a | 2010-03-08 15:44:18 -0800 | [diff] [blame] | 1288 | if(l->op == OLITERAL && r->op == OLITERAL) { |
| 1289 | // make it a complex literal |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1290 | r = nodcplxlit(l->val, r->val); |
| 1291 | r->orig = n; |
| 1292 | n = r; |
Russ Cox | 4ac011a | 2010-03-08 15:44:18 -0800 | [diff] [blame] | 1293 | } |
| 1294 | n->type = t; |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 1295 | goto ret; |
| 1296 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1297 | case OCLOSE: |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1298 | if(onearg(n, "%O", n->op) < 0) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1299 | goto error; |
| 1300 | typecheck(&n->left, Erv); |
| 1301 | defaultlit(&n->left, T); |
| 1302 | l = n->left; |
| 1303 | if((t = l->type) == T) |
| 1304 | goto error; |
| 1305 | if(t->etype != TCHAN) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1306 | yyerror("invalid operation: %N (non-chan type %T)", n, t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1307 | goto error; |
| 1308 | } |
Russ Cox | f58ed4e | 2011-10-13 16:58:04 -0400 | [diff] [blame] | 1309 | if(!(t->chan & Csend)) { |
Luuk van Dijk | 556258e | 2012-01-10 11:09:04 +0100 | [diff] [blame] | 1310 | yyerror("invalid operation: %N (cannot close receive-only channel)", n); |
Russ Cox | f58ed4e | 2011-10-13 16:58:04 -0400 | [diff] [blame] | 1311 | goto error; |
| 1312 | } |
Russ Cox | 8bf34e3 | 2011-03-11 14:47:26 -0500 | [diff] [blame] | 1313 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1314 | goto ret; |
| 1315 | |
Russ Cox | 1d687c7 | 2011-10-18 09:41:32 -0400 | [diff] [blame] | 1316 | case ODELETE: |
| 1317 | args = n->list; |
| 1318 | if(args == nil) { |
| 1319 | yyerror("missing arguments to delete"); |
| 1320 | goto error; |
| 1321 | } |
| 1322 | if(args->next == nil) { |
| 1323 | yyerror("missing second (key) argument to delete"); |
| 1324 | goto error; |
| 1325 | } |
| 1326 | if(args->next->next != nil) { |
| 1327 | yyerror("too many arguments to delete"); |
| 1328 | goto error; |
| 1329 | } |
| 1330 | ok |= Etop; |
| 1331 | typechecklist(args, Erv); |
| 1332 | l = args->n; |
| 1333 | r = args->next->n; |
| 1334 | if(l->type != T && l->type->etype != TMAP) { |
| 1335 | yyerror("first argument to delete must be map; have %lT", l->type); |
| 1336 | goto error; |
| 1337 | } |
| 1338 | args->next->n = assignconv(r, l->type->down, "delete"); |
| 1339 | goto ret; |
| 1340 | |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1341 | case OAPPEND: |
| 1342 | ok |= Erv; |
| 1343 | args = n->list; |
| 1344 | if(args == nil) { |
| 1345 | yyerror("missing arguments to append"); |
| 1346 | goto error; |
| 1347 | } |
Chris Manghane | 671cc6e | 2014-03-05 14:16:21 -0500 | [diff] [blame] | 1348 | |
| 1349 | if(count(args) == 1 && !n->isddd) |
| 1350 | typecheck(&args->n, Erv | Efnstruct); |
| 1351 | else |
| 1352 | typechecklist(args, Erv); |
| 1353 | |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1354 | if((t = args->n->type) == T) |
| 1355 | goto error; |
Chris Manghane | 671cc6e | 2014-03-05 14:16:21 -0500 | [diff] [blame] | 1356 | |
| 1357 | // Unpack multiple-return result before type-checking. |
Chris Manghane | f5b8813 | 2015-01-20 14:35:33 -0800 | [diff] [blame] | 1358 | if(istype(t, TSTRUCT) && t->funarg) { |
Chris Manghane | 671cc6e | 2014-03-05 14:16:21 -0500 | [diff] [blame] | 1359 | t = t->type; |
| 1360 | if(istype(t, TFIELD)) |
| 1361 | t = t->type; |
| 1362 | } |
| 1363 | |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1364 | n->type = t; |
| 1365 | if(!isslice(t)) { |
Russ Cox | fcc1f2a | 2012-05-15 12:51:58 -0400 | [diff] [blame] | 1366 | if(isconst(args->n, CTNIL)) { |
| 1367 | yyerror("first argument to append must be typed slice; have untyped nil", t); |
| 1368 | goto error; |
| 1369 | } |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1370 | yyerror("first argument to append must be slice; have %lT", t); |
| 1371 | goto error; |
| 1372 | } |
Luuk van Dijk | 151b2f1 | 2011-11-09 11:17:06 +0100 | [diff] [blame] | 1373 | |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1374 | if(n->isddd) { |
| 1375 | if(args->next == nil) { |
| 1376 | yyerror("cannot use ... on first argument to append"); |
| 1377 | goto error; |
| 1378 | } |
| 1379 | if(args->next->next != nil) { |
| 1380 | yyerror("too many arguments to append"); |
| 1381 | goto error; |
| 1382 | } |
Russ Cox | 862179b | 2011-10-18 14:55:50 -0400 | [diff] [blame] | 1383 | if(istype(t->type, TUINT8) && istype(args->next->n->type, TSTRING)) { |
Luuk van Dijk | 77fac21 | 2011-10-12 15:59:23 +0200 | [diff] [blame] | 1384 | defaultlit(&args->next->n, types[TSTRING]); |
| 1385 | goto ret; |
| 1386 | } |
Russ Cox | d8b5d03 | 2010-10-27 17:56:32 -0700 | [diff] [blame] | 1387 | args->next->n = assignconv(args->next->n, t->orig, "append"); |
| 1388 | goto ret; |
| 1389 | } |
| 1390 | for(args=args->next; args != nil; args=args->next) { |
| 1391 | if(args->n->type == T) |
| 1392 | continue; |
| 1393 | args->n = assignconv(args->n, t->type, "append"); |
| 1394 | } |
| 1395 | goto ret; |
| 1396 | |
Ken Thompson | c4606d0 | 2009-11-17 20:41:44 -0800 | [diff] [blame] | 1397 | case OCOPY: |
Ken Thompson | 01c2de0 | 2009-11-17 22:00:59 -0800 | [diff] [blame] | 1398 | ok |= Etop|Erv; |
Ken Thompson | c4606d0 | 2009-11-17 20:41:44 -0800 | [diff] [blame] | 1399 | args = n->list; |
| 1400 | if(args == nil || args->next == nil) { |
| 1401 | yyerror("missing arguments to copy"); |
| 1402 | goto error; |
| 1403 | } |
| 1404 | if(args->next->next != nil) { |
| 1405 | yyerror("too many arguments to copy"); |
| 1406 | goto error; |
| 1407 | } |
Ken Thompson | c4606d0 | 2009-11-17 20:41:44 -0800 | [diff] [blame] | 1408 | n->left = args->n; |
| 1409 | n->right = args->next->n; |
Luuk van Dijk | 847b61b | 2011-08-24 19:07:08 +0200 | [diff] [blame] | 1410 | n->list = nil; |
Ken Thompson | c4606d0 | 2009-11-17 20:41:44 -0800 | [diff] [blame] | 1411 | n->type = types[TINT]; |
Ken Thompson | 01c2de0 | 2009-11-17 22:00:59 -0800 | [diff] [blame] | 1412 | typecheck(&n->left, Erv); |
| 1413 | typecheck(&n->right, Erv); |
Russ Cox | 9be56ad | 2009-11-18 14:26:28 -0800 | [diff] [blame] | 1414 | if(n->left->type == T || n->right->type == T) |
| 1415 | goto error; |
Russ Cox | 07fc145 | 2010-01-18 16:00:13 -0800 | [diff] [blame] | 1416 | defaultlit(&n->left, T); |
| 1417 | defaultlit(&n->right, T); |
Russ Cox | 56b983c | 2014-03-03 19:55:40 -0500 | [diff] [blame] | 1418 | if(n->left->type == T || n->right->type == T) |
| 1419 | goto error; |
Luuk van Dijk | 77fac21 | 2011-10-12 15:59:23 +0200 | [diff] [blame] | 1420 | |
Russ Cox | 82c6f5e | 2010-10-26 08:36:07 -0700 | [diff] [blame] | 1421 | // copy([]byte, string) |
Quan Yong Zhai | 256df10 | 2011-04-21 12:09:29 -0400 | [diff] [blame] | 1422 | if(isslice(n->left->type) && n->right->type->etype == TSTRING) { |
Russ Cox | 862179b | 2011-10-18 14:55:50 -0400 | [diff] [blame] | 1423 | if(eqtype(n->left->type->type, bytetype)) |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 1424 | goto ret; |
| 1425 | yyerror("arguments to copy have different element types: %lT and string", n->left->type); |
Quan Yong Zhai | 256df10 | 2011-04-21 12:09:29 -0400 | [diff] [blame] | 1426 | goto error; |
| 1427 | } |
Luuk van Dijk | 77fac21 | 2011-10-12 15:59:23 +0200 | [diff] [blame] | 1428 | |
Ken Thompson | 01c2de0 | 2009-11-17 22:00:59 -0800 | [diff] [blame] | 1429 | if(!isslice(n->left->type) || !isslice(n->right->type)) { |
Russ Cox | 07fc145 | 2010-01-18 16:00:13 -0800 | [diff] [blame] | 1430 | if(!isslice(n->left->type) && !isslice(n->right->type)) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1431 | yyerror("arguments to copy must be slices; have %lT, %lT", n->left->type, n->right->type); |
Russ Cox | 07fc145 | 2010-01-18 16:00:13 -0800 | [diff] [blame] | 1432 | else if(!isslice(n->left->type)) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1433 | yyerror("first argument to copy should be slice; have %lT", n->left->type); |
Russ Cox | 07fc145 | 2010-01-18 16:00:13 -0800 | [diff] [blame] | 1434 | else |
Russ Cox | 82c6f5e | 2010-10-26 08:36:07 -0700 | [diff] [blame] | 1435 | yyerror("second argument to copy should be slice or string; have %lT", n->right->type); |
Ken Thompson | 01c2de0 | 2009-11-17 22:00:59 -0800 | [diff] [blame] | 1436 | goto error; |
| 1437 | } |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1438 | if(!eqtype(n->left->type->type, n->right->type->type)) { |
| 1439 | yyerror("arguments to copy have different element types: %lT and %lT", n->left->type, n->right->type); |
Ken Thompson | 01c2de0 | 2009-11-17 22:00:59 -0800 | [diff] [blame] | 1440 | goto error; |
| 1441 | } |
Ken Thompson | c4606d0 | 2009-11-17 20:41:44 -0800 | [diff] [blame] | 1442 | goto ret; |
| 1443 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1444 | case OCONV: |
| 1445 | doconv: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1446 | ok |= Erv; |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 1447 | saveorignode(n); |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 1448 | typecheck(&n->left, Erv | (top & (Eindir | Eiota))); |
Russ Cox | 60ff8cc | 2009-10-20 08:27:14 -0700 | [diff] [blame] | 1449 | convlit1(&n->left, n->type, 1); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1450 | if((t = n->left->type) == T || n->type == T) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1451 | goto error; |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1452 | if((n->op = convertop(t, n->type, &why)) == 0) { |
Russ Cox | f607c47 | 2013-02-01 21:21:27 -0500 | [diff] [blame] | 1453 | if(!n->diag && !n->type->broke) { |
| 1454 | yyerror("cannot convert %lN to type %T%s", n->left, n->type, why); |
| 1455 | n->diag = 1; |
| 1456 | } |
Lucio De Re | b3cc489 | 2011-08-29 09:35:04 -0400 | [diff] [blame] | 1457 | n->op = OCONV; |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1458 | } |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 1459 | switch(n->op) { |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1460 | case OCONVNOP: |
Chris Manghane | 5cc29ab | 2014-12-09 07:16:38 -0800 | [diff] [blame] | 1461 | if(n->left->op == OLITERAL && n->type != types[TBOOL]) { |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1462 | r = nod(OXXX, N, N); |
| 1463 | n->op = OCONV; |
| 1464 | n->orig = r; |
| 1465 | *r = *n; |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 1466 | n->op = OLITERAL; |
| 1467 | n->val = n->left->val; |
| 1468 | } |
| 1469 | break; |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 1470 | case OSTRARRAYBYTE: |
Russ Cox | d227d68 | 2014-01-06 20:43:44 -0500 | [diff] [blame] | 1471 | // do not use stringtoarraylit. |
| 1472 | // generated code and compiler memory footprint is better without it. |
| 1473 | break; |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 1474 | case OSTRARRAYRUNE: |
| 1475 | if(n->left->op == OLITERAL) |
| 1476 | stringtoarraylit(&n); |
| 1477 | break; |
| 1478 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1479 | goto ret; |
| 1480 | |
| 1481 | case OMAKE: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1482 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1483 | args = n->list; |
| 1484 | if(args == nil) { |
| 1485 | yyerror("missing argument to make"); |
| 1486 | goto error; |
| 1487 | } |
Russ Cox | ee9bfb0 | 2012-01-25 17:53:50 -0500 | [diff] [blame] | 1488 | n->list = nil; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1489 | l = args->n; |
| 1490 | args = args->next; |
| 1491 | typecheck(&l, Etype); |
| 1492 | if((t = l->type) == T) |
| 1493 | goto error; |
| 1494 | |
| 1495 | switch(t->etype) { |
| 1496 | default: |
| 1497 | badmake: |
| 1498 | yyerror("cannot make type %T", t); |
| 1499 | goto error; |
| 1500 | |
| 1501 | case TARRAY: |
| 1502 | if(!isslice(t)) |
| 1503 | goto badmake; |
| 1504 | if(args == nil) { |
| 1505 | yyerror("missing len argument to make(%T)", t); |
| 1506 | goto error; |
| 1507 | } |
| 1508 | l = args->n; |
| 1509 | args = args->next; |
| 1510 | typecheck(&l, Erv); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1511 | r = N; |
| 1512 | if(args != nil) { |
| 1513 | r = args->n; |
| 1514 | args = args->next; |
| 1515 | typecheck(&r, Erv); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1516 | } |
| 1517 | if(l->type == T || (r && r->type == T)) |
| 1518 | goto error; |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 1519 | et = checkmake(t, "len", l) < 0; |
| 1520 | et |= r && checkmake(t, "cap", r) < 0; |
| 1521 | if(et) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1522 | goto error; |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 1523 | if(isconst(l, CTINT) && r && isconst(r, CTINT) && mpcmpfixfix(l->val.u.xval, r->val.u.xval) > 0) { |
| 1524 | yyerror("len larger than cap in make(%T)", t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1525 | goto error; |
| 1526 | } |
| 1527 | n->left = l; |
| 1528 | n->right = r; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1529 | n->op = OMAKESLICE; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1530 | break; |
| 1531 | |
| 1532 | case TMAP: |
| 1533 | if(args != nil) { |
| 1534 | l = args->n; |
| 1535 | args = args->next; |
| 1536 | typecheck(&l, Erv); |
Russ Cox | 6361f52 | 2010-05-01 13:15:42 -0700 | [diff] [blame] | 1537 | defaultlit(&l, types[TINT]); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1538 | if(l->type == T) |
| 1539 | goto error; |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 1540 | if(checkmake(t, "size", l) < 0) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1541 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1542 | n->left = l; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1543 | } else |
| 1544 | n->left = nodintconst(0); |
| 1545 | n->op = OMAKEMAP; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1546 | break; |
| 1547 | |
| 1548 | case TCHAN: |
| 1549 | l = N; |
| 1550 | if(args != nil) { |
| 1551 | l = args->n; |
| 1552 | args = args->next; |
| 1553 | typecheck(&l, Erv); |
Russ Cox | 6361f52 | 2010-05-01 13:15:42 -0700 | [diff] [blame] | 1554 | defaultlit(&l, types[TINT]); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1555 | if(l->type == T) |
| 1556 | goto error; |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 1557 | if(checkmake(t, "buffer", l) < 0) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1558 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1559 | n->left = l; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1560 | } else |
| 1561 | n->left = nodintconst(0); |
| 1562 | n->op = OMAKECHAN; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1563 | break; |
| 1564 | } |
| 1565 | if(args != nil) { |
| 1566 | yyerror("too many arguments to make(%T)", t); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1567 | n->op = OMAKE; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1568 | goto error; |
| 1569 | } |
| 1570 | n->type = t; |
| 1571 | goto ret; |
| 1572 | |
| 1573 | case ONEW: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1574 | ok |= Erv; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1575 | args = n->list; |
| 1576 | if(args == nil) { |
| 1577 | yyerror("missing argument to new"); |
| 1578 | goto error; |
| 1579 | } |
| 1580 | l = args->n; |
| 1581 | typecheck(&l, Etype); |
| 1582 | if((t = l->type) == T) |
| 1583 | goto error; |
| 1584 | if(args->next != nil) { |
| 1585 | yyerror("too many arguments to new(%T)", t); |
| 1586 | goto error; |
| 1587 | } |
| 1588 | n->left = l; |
| 1589 | n->type = ptrto(t); |
| 1590 | goto ret; |
| 1591 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1592 | case OPRINT: |
| 1593 | case OPRINTN: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1594 | ok |= Etop; |
Russ Cox | dc9a3b2 | 2010-12-13 16:22:19 -0500 | [diff] [blame] | 1595 | typechecklist(n->list, Erv | Eindir); // Eindir: address does not escape |
Russ Cox | e29d3df | 2012-02-22 00:29:37 -0500 | [diff] [blame] | 1596 | for(args=n->list; args; args=args->next) { |
| 1597 | // Special case for print: int constant is int64, not int. |
| 1598 | if(isconst(args->n, CTINT)) |
| 1599 | defaultlit(&args->n, types[TINT64]); |
| 1600 | else |
| 1601 | defaultlit(&args->n, T); |
| 1602 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1603 | goto ret; |
| 1604 | |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 1605 | case OPANIC: |
| 1606 | ok |= Etop; |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1607 | if(onearg(n, "panic") < 0) |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 1608 | goto error; |
| 1609 | typecheck(&n->left, Erv); |
Russ Cox | 585eae3 | 2010-08-03 01:07:40 -0700 | [diff] [blame] | 1610 | defaultlit(&n->left, types[TINTER]); |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 1611 | if(n->left->type == T) |
| 1612 | goto error; |
| 1613 | goto ret; |
| 1614 | |
| 1615 | case ORECOVER: |
| 1616 | ok |= Erv|Etop; |
| 1617 | if(n->list != nil) { |
| 1618 | yyerror("too many arguments to recover"); |
| 1619 | goto error; |
| 1620 | } |
| 1621 | n->type = types[TINTER]; |
| 1622 | goto ret; |
| 1623 | |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1624 | case OCLOSURE: |
| 1625 | ok |= Erv; |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 1626 | typecheckclosure(n, top); |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1627 | if(n->type == T) |
| 1628 | goto error; |
| 1629 | goto ret; |
Russ Cox | f91cc3b | 2012-02-11 00:19:24 -0500 | [diff] [blame] | 1630 | |
| 1631 | case OITAB: |
| 1632 | ok |= Erv; |
| 1633 | typecheck(&n->left, Erv); |
| 1634 | if((t = n->left->type) == T) |
| 1635 | goto error; |
| 1636 | if(t->etype != TINTER) |
| 1637 | fatal("OITAB of %T", t); |
| 1638 | n->type = ptrto(types[TUINTPTR]); |
| 1639 | goto ret; |
Rémy Oudompheng | ff416a3 | 2013-09-12 00:15:28 +0200 | [diff] [blame] | 1640 | |
| 1641 | case OSPTR: |
| 1642 | ok |= Erv; |
| 1643 | typecheck(&n->left, Erv); |
| 1644 | if((t = n->left->type) == T) |
| 1645 | goto error; |
| 1646 | if(!isslice(t) && t->etype != TSTRING) |
| 1647 | fatal("OSPTR of %T", t); |
| 1648 | if(t->etype == TSTRING) |
| 1649 | n->type = ptrto(types[TUINT8]); |
| 1650 | else |
| 1651 | n->type = ptrto(t->type); |
| 1652 | goto ret; |
| 1653 | |
Russ Cox | 9f64728 | 2013-02-22 14:25:50 -0500 | [diff] [blame] | 1654 | case OCLOSUREVAR: |
| 1655 | ok |= Erv; |
| 1656 | goto ret; |
| 1657 | |
| 1658 | case OCFUNC: |
| 1659 | ok |= Erv; |
| 1660 | typecheck(&n->left, Erv); |
| 1661 | n->type = types[TUINTPTR]; |
| 1662 | goto ret; |
| 1663 | |
| 1664 | case OCONVNOP: |
| 1665 | ok |= Erv; |
| 1666 | typecheck(&n->left, Erv); |
| 1667 | goto ret; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1668 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1669 | /* |
| 1670 | * statements |
| 1671 | */ |
| 1672 | case OAS: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1673 | ok |= Etop; |
| 1674 | typecheckas(n); |
Dmitry Vyukov | a7bb393 | 2015-01-26 17:19:00 +0300 | [diff] [blame] | 1675 | // Code that creates temps does not bother to set defn, so do it here. |
| 1676 | if(n->left->op == ONAME && strncmp(n->left->sym->name, "autotmp_", 8) == 0) |
| 1677 | n->left->defn = n; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1678 | goto ret; |
| 1679 | |
| 1680 | case OAS2: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1681 | ok |= Etop; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 1682 | typecheckas2(n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1683 | goto ret; |
| 1684 | |
| 1685 | case OBREAK: |
| 1686 | case OCONTINUE: |
| 1687 | case ODCL: |
| 1688 | case OEMPTY: |
| 1689 | case OGOTO: |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1690 | case OXFALL: |
Russ Cox | b700cb4 | 2014-04-01 13:31:38 -0400 | [diff] [blame] | 1691 | case OVARKILL: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1692 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1693 | goto ret; |
| 1694 | |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 1695 | case OLABEL: |
| 1696 | ok |= Etop; |
| 1697 | decldepth++; |
| 1698 | goto ret; |
| 1699 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1700 | case ODEFER: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1701 | ok |= Etop; |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1702 | typecheck(&n->left, Etop|Erv); |
| 1703 | if(!n->left->diag) |
| 1704 | checkdefergo(n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1705 | goto ret; |
| 1706 | |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 1707 | case OPROC: |
| 1708 | ok |= Etop; |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1709 | typecheck(&n->left, Etop|Eproc|Erv); |
| 1710 | checkdefergo(n); |
Luuk van Dijk | 2c4edb0 | 2011-06-01 17:02:43 +0200 | [diff] [blame] | 1711 | goto ret; |
| 1712 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1713 | case OFOR: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1714 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1715 | typechecklist(n->ninit, Etop); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 1716 | decldepth++; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 1717 | typecheck(&n->ntest, Erv); |
| 1718 | if(n->ntest != N && (t = n->ntest->type) != T && t->etype != TBOOL) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1719 | yyerror("non-bool %lN used as for condition", n->ntest); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1720 | typecheck(&n->nincr, Etop); |
| 1721 | typechecklist(n->nbody, Etop); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 1722 | decldepth--; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1723 | goto ret; |
| 1724 | |
| 1725 | case OIF: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1726 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1727 | typechecklist(n->ninit, Etop); |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 1728 | typecheck(&n->ntest, Erv); |
| 1729 | if(n->ntest != N && (t = n->ntest->type) != T && t->etype != TBOOL) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1730 | yyerror("non-bool %lN used as if condition", n->ntest); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1731 | typechecklist(n->nbody, Etop); |
| 1732 | typechecklist(n->nelse, Etop); |
| 1733 | goto ret; |
| 1734 | |
| 1735 | case ORETURN: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1736 | ok |= Etop; |
Rémy Oudompheng | 1d3ca92 | 2012-02-16 23:42:19 +0100 | [diff] [blame] | 1737 | if(count(n->list) == 1) |
| 1738 | typechecklist(n->list, Erv | Efnstruct); |
| 1739 | else |
| 1740 | typechecklist(n->list, Erv); |
Russ Cox | f20c2e1 | 2010-07-26 17:34:17 -0700 | [diff] [blame] | 1741 | if(curfn == N) { |
| 1742 | yyerror("return outside function"); |
| 1743 | goto error; |
| 1744 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1745 | if(curfn->type->outnamed && n->list == nil) |
| 1746 | goto ret; |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 1747 | typecheckaste(ORETURN, nil, 0, getoutargx(curfn->type), n->list, "return argument"); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1748 | goto ret; |
Russ Cox | 1f51d27 | 2013-06-11 09:41:49 -0400 | [diff] [blame] | 1749 | |
| 1750 | case ORETJMP: |
| 1751 | ok |= Etop; |
| 1752 | goto ret; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1753 | |
| 1754 | case OSELECT: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1755 | ok |= Etop; |
Russ Cox | f7a867e | 2009-08-04 12:57:48 -0700 | [diff] [blame] | 1756 | typecheckselect(n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1757 | goto ret; |
| 1758 | |
| 1759 | case OSWITCH: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1760 | ok |= Etop; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 1761 | typecheckswitch(n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1762 | goto ret; |
| 1763 | |
Russ Cox | 2609731 | 2009-08-05 02:33:30 -0700 | [diff] [blame] | 1764 | case ORANGE: |
| 1765 | ok |= Etop; |
| 1766 | typecheckrange(n); |
| 1767 | goto ret; |
| 1768 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1769 | case OTYPESW: |
Russ Cox | 83bdb80 | 2009-09-09 01:21:20 -0700 | [diff] [blame] | 1770 | yyerror("use of .(type) outside type switch"); |
| 1771 | goto error; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1772 | |
| 1773 | case OXCASE: |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1774 | ok |= Etop; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1775 | typechecklist(n->list, Erv); |
| 1776 | typechecklist(n->nbody, Etop); |
| 1777 | goto ret; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1778 | |
| 1779 | case ODCLFUNC: |
| 1780 | ok |= Etop; |
| 1781 | typecheckfunc(n); |
| 1782 | goto ret; |
| 1783 | |
| 1784 | case ODCLCONST: |
| 1785 | ok |= Etop; |
| 1786 | typecheck(&n->left, Erv); |
| 1787 | goto ret; |
| 1788 | |
| 1789 | case ODCLTYPE: |
| 1790 | ok |= Etop; |
| 1791 | typecheck(&n->left, Etype); |
Russ Cox | 05a1eb1 | 2009-12-03 01:12:02 -0800 | [diff] [blame] | 1792 | if(!incannedimport) |
| 1793 | checkwidth(n->left->type); |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1794 | goto ret; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | ret: |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1798 | t = n->type; |
| 1799 | if(t && !t->funarg && n->op != OTYPE) { |
| 1800 | switch(t->etype) { |
| 1801 | case TFUNC: // might have TANY; wait until its called |
| 1802 | case TANY: |
| 1803 | case TFORW: |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1804 | case TIDEAL: |
| 1805 | case TNIL: |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 1806 | case TBLANK: |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1807 | break; |
| 1808 | default: |
| 1809 | checkwidth(t); |
| 1810 | } |
| 1811 | } |
Russ Cox | d20ad1c | 2010-06-11 15:28:43 -0700 | [diff] [blame] | 1812 | |
Russ Cox | c29f4e0 | 2012-09-21 21:12:41 -0400 | [diff] [blame] | 1813 | if(safemode && !incannedimport && !importpkg && !compiling_wrappers && t && t->etype == TUNSAFEPTR) |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 1814 | yyerror("cannot use unsafe.Pointer"); |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1815 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1816 | evconst(n); |
| 1817 | if(n->op == OTYPE && !(top & Etype)) { |
| 1818 | yyerror("type %T is not an expression", n->type); |
| 1819 | goto error; |
| 1820 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 1821 | if((top & (Erv|Etype)) == Etype && n->op != OTYPE) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1822 | yyerror("%N is not a type", n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1823 | goto error; |
| 1824 | } |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1825 | // TODO(rsc): simplify |
| 1826 | if((top & (Ecall|Erv|Etype)) && !(top & Etop) && !(ok & (Erv|Etype|Ecall))) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1827 | yyerror("%N used as value", n); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 1828 | goto error; |
| 1829 | } |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 1830 | if((top & Etop) && !(top & (Ecall|Erv|Etype)) && !(ok & Etop)) { |
Russ Cox | 8133cb3 | 2011-04-28 13:14:35 -0400 | [diff] [blame] | 1831 | if(n->diag == 0) { |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1832 | yyerror("%N evaluated but not used", n); |
Russ Cox | 8133cb3 | 2011-04-28 13:14:35 -0400 | [diff] [blame] | 1833 | n->diag = 1; |
| 1834 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1835 | goto error; |
| 1836 | } |
| 1837 | |
| 1838 | /* TODO |
| 1839 | if(n->type == T) |
| 1840 | fatal("typecheck nil type"); |
| 1841 | */ |
| 1842 | goto out; |
| 1843 | |
| 1844 | badcall1: |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1845 | yyerror("invalid argument %lN for %O", n->left, n->op); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1846 | goto error; |
| 1847 | |
| 1848 | error: |
| 1849 | n->type = T; |
| 1850 | |
| 1851 | out: |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1852 | *np = n; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1855 | static int |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1856 | checksliceindex(Node *l, Node *r, Type *tp) |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1857 | { |
| 1858 | Type *t; |
| 1859 | |
| 1860 | if((t = r->type) == T) |
| 1861 | return -1; |
| 1862 | if(!isint[t->etype]) { |
| 1863 | yyerror("invalid slice index %N (type %T)", r, t); |
| 1864 | return -1; |
| 1865 | } |
| 1866 | if(r->op == OLITERAL) { |
| 1867 | if(mpgetfix(r->val.u.xval) < 0) { |
| 1868 | yyerror("invalid slice index %N (index must be non-negative)", r); |
| 1869 | return -1; |
| 1870 | } else if(tp != nil && tp->bound > 0 && mpgetfix(r->val.u.xval) > tp->bound) { |
| 1871 | yyerror("invalid slice index %N (out of bounds for %d-element array)", r, tp->bound); |
| 1872 | return -1; |
Russ Cox | 870f4e1 | 2014-09-25 13:24:43 -0400 | [diff] [blame] | 1873 | } else if(isconst(l, CTSTR) && mpgetfix(r->val.u.xval) > l->val.u.sval->len) { |
| 1874 | yyerror("invalid slice index %N (out of bounds for %d-byte string)", r, l->val.u.sval->len); |
| 1875 | return -1; |
Russ Cox | b4e92ce | 2013-07-01 20:32:36 -0400 | [diff] [blame] | 1876 | } else if(mpcmpfixfix(r->val.u.xval, maxintval[TINT]) > 0) { |
| 1877 | yyerror("invalid slice index %N (index too large)", r); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | } |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | static int |
| 1885 | checksliceconst(Node *lo, Node *hi) |
| 1886 | { |
| 1887 | if(lo != N && hi != N && lo->op == OLITERAL && hi->op == OLITERAL |
| 1888 | && mpcmpfixfix(lo->val.u.xval, hi->val.u.xval) > 0) { |
| 1889 | yyerror("invalid slice index: %N > %N", lo, hi); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1895 | static void |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1896 | checkdefergo(Node *n) |
| 1897 | { |
| 1898 | char *what; |
| 1899 | |
| 1900 | what = "defer"; |
| 1901 | if(n->op == OPROC) |
| 1902 | what = "go"; |
| 1903 | |
| 1904 | switch(n->left->op) { |
| 1905 | case OCALLINTER: |
| 1906 | case OCALLMETH: |
| 1907 | case OCALLFUNC: |
| 1908 | case OCLOSE: |
| 1909 | case OCOPY: |
| 1910 | case ODELETE: |
| 1911 | case OPANIC: |
| 1912 | case OPRINT: |
| 1913 | case OPRINTN: |
| 1914 | case ORECOVER: |
| 1915 | // ok |
| 1916 | break; |
| 1917 | case OAPPEND: |
| 1918 | case OCAP: |
| 1919 | case OCOMPLEX: |
| 1920 | case OIMAG: |
| 1921 | case OLEN: |
| 1922 | case OMAKE: |
| 1923 | case OMAKESLICE: |
| 1924 | case OMAKECHAN: |
| 1925 | case OMAKEMAP: |
| 1926 | case ONEW: |
| 1927 | case OREAL: |
| 1928 | case OLITERAL: // conversion or unsafe.Alignof, Offsetof, Sizeof |
| 1929 | if(n->left->orig != N && n->left->orig->op == OCONV) |
| 1930 | goto conv; |
| 1931 | yyerror("%s discards result of %N", what, n->left); |
| 1932 | break; |
| 1933 | default: |
| 1934 | conv: |
Daniel Morsing | 6f5af9c | 2013-05-21 18:35:47 +0200 | [diff] [blame] | 1935 | // type is broken or missing, most likely a method call on a broken type |
| 1936 | // we will warn about the broken type elsewhere. no need to emit a potentially confusing error |
| 1937 | if(n->left->type == T || n->left->type->broke) |
| 1938 | break; |
| 1939 | |
Russ Cox | 79a16a3 | 2013-02-01 21:02:15 -0500 | [diff] [blame] | 1940 | if(!n->diag) { |
| 1941 | // The syntax made sure it was a call, so this must be |
| 1942 | // a conversion. |
| 1943 | n->diag = 1; |
| 1944 | yyerror("%s requires function call, not conversion", what); |
| 1945 | } |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | static void |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1951 | implicitstar(Node **nn) |
| 1952 | { |
| 1953 | Type *t; |
| 1954 | Node *n; |
| 1955 | |
Russ Cox | 82ee481 | 2010-09-10 11:53:27 -0400 | [diff] [blame] | 1956 | // insert implicit * if needed for fixed array |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1957 | n = *nn; |
| 1958 | t = n->type; |
| 1959 | if(t == T || !isptr[t->etype]) |
| 1960 | return; |
| 1961 | t = t->type; |
| 1962 | if(t == T) |
| 1963 | return; |
| 1964 | if(!isfixedarray(t)) |
| 1965 | return; |
| 1966 | n = nod(OIND, n, N); |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 1967 | n->implicit = 1; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1968 | typecheck(&n, Erv); |
| 1969 | *nn = n; |
| 1970 | } |
| 1971 | |
| 1972 | static int |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1973 | onearg(Node *n, char *f, ...) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1974 | { |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1975 | va_list arg; |
| 1976 | char *p; |
| 1977 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1978 | if(n->left != N) |
| 1979 | return 0; |
| 1980 | if(n->list == nil) { |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1981 | va_start(arg, f); |
| 1982 | p = vsmprint(f, arg); |
| 1983 | va_end(arg); |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1984 | yyerror("missing argument to %s: %N", p, n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1985 | return -1; |
| 1986 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1987 | if(n->list->next != nil) { |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1988 | va_start(arg, f); |
| 1989 | p = vsmprint(f, arg); |
| 1990 | va_end(arg); |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 1991 | yyerror("too many arguments to %s: %N", p, n); |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1992 | n->left = n->list->n; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1993 | n->list = nil; |
| 1994 | return -1; |
| 1995 | } |
Russ Cox | a212d17 | 2010-06-20 11:45:53 -0700 | [diff] [blame] | 1996 | n->left = n->list->n; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 1997 | n->list = nil; |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 2001 | static int |
| 2002 | twoarg(Node *n) |
| 2003 | { |
| 2004 | if(n->left != N) |
| 2005 | return 0; |
| 2006 | if(n->list == nil) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2007 | yyerror("missing argument to %O - %N", n->op, n); |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 2008 | return -1; |
| 2009 | } |
| 2010 | n->left = n->list->n; |
| 2011 | if(n->list->next == nil) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2012 | yyerror("missing argument to %O - %N", n->op, n); |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 2013 | n->list = nil; |
| 2014 | return -1; |
| 2015 | } |
| 2016 | if(n->list->next->next != nil) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2017 | yyerror("too many arguments to %O - %N", n->op, n); |
Ken Thompson | 426099f | 2010-03-05 20:16:04 -0800 | [diff] [blame] | 2018 | n->list = nil; |
| 2019 | return -1; |
| 2020 | } |
| 2021 | n->right = n->list->next->n; |
| 2022 | n->list = nil; |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2026 | static Type* |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2027 | lookdot1(Node *errnode, Sym *s, Type *t, Type *f, int dostrcmp) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2028 | { |
| 2029 | Type *r; |
| 2030 | |
| 2031 | r = T; |
| 2032 | for(; f!=T; f=f->down) { |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 2033 | if(dostrcmp && strcmp(f->sym->name, s->name) == 0) |
| 2034 | return f; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2035 | if(f->sym != s) |
| 2036 | continue; |
| 2037 | if(r != T) { |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2038 | if(errnode) |
| 2039 | yyerror("ambiguous selector %N", errnode); |
| 2040 | else if(isptr[t->etype]) |
| 2041 | yyerror("ambiguous selector (%T).%S", t, s); |
| 2042 | else |
| 2043 | yyerror("ambiguous selector %T.%S", t, s); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2044 | break; |
| 2045 | } |
| 2046 | r = f; |
| 2047 | } |
| 2048 | return r; |
| 2049 | } |
| 2050 | |
| 2051 | static int |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2052 | looktypedot(Node *n, Type *t, int dostrcmp) |
| 2053 | { |
Rémy Oudompheng | ced8004 | 2012-12-22 19:13:45 +0100 | [diff] [blame] | 2054 | Type *f1, *f2; |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2055 | Sym *s; |
| 2056 | |
| 2057 | s = n->right->sym; |
| 2058 | |
| 2059 | if(t->etype == TINTER) { |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2060 | f1 = lookdot1(n, s, t, t->type, dostrcmp); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2061 | if(f1 == T) |
| 2062 | return 0; |
| 2063 | |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2064 | n->right = methodname(n->right, t); |
| 2065 | n->xoffset = f1->width; |
| 2066 | n->type = f1->type; |
| 2067 | n->op = ODOTINTER; |
| 2068 | return 1; |
| 2069 | } |
| 2070 | |
Rémy Oudompheng | ced8004 | 2012-12-22 19:13:45 +0100 | [diff] [blame] | 2071 | // Find the base type: methtype will fail if t |
| 2072 | // is not of the form T or *T. |
| 2073 | f2 = methtype(t, 0); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2074 | if(f2 == T) |
| 2075 | return 0; |
| 2076 | |
Russ Cox | 4267974 | 2012-03-07 02:27:15 -0500 | [diff] [blame] | 2077 | expandmeth(f2); |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2078 | f2 = lookdot1(n, s, f2, f2->xmethod, dostrcmp); |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2079 | if(f2 == T) |
| 2080 | return 0; |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2081 | |
| 2082 | // disallow T.m if m requires *T receiver |
| 2083 | if(isptr[getthisx(f2->type)->type->type->etype] |
| 2084 | && !isptr[t->etype] |
| 2085 | && f2->embedded != 2 |
| 2086 | && !isifacemethod(f2->type)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2087 | yyerror("invalid method expression %N (needs pointer receiver: (*%T).%hS)", n, t, f2->sym); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | n->right = methodname(n->right, t); |
| 2092 | n->xoffset = f2->width; |
| 2093 | n->type = f2->type; |
| 2094 | n->op = ODOTMETH; |
| 2095 | return 1; |
| 2096 | } |
| 2097 | |
Luuk van Dijk | 7df9ff5 | 2011-11-02 17:18:53 +0100 | [diff] [blame] | 2098 | static Type* |
| 2099 | derefall(Type* t) |
| 2100 | { |
| 2101 | while(t && t->etype == tptr) |
| 2102 | t = t->type; |
| 2103 | return t; |
| 2104 | } |
| 2105 | |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2106 | static int |
Russ Cox | 5d754bf | 2009-12-15 16:22:04 -0800 | [diff] [blame] | 2107 | lookdot(Node *n, Type *t, int dostrcmp) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2108 | { |
| 2109 | Type *f1, *f2, *tt, *rcvr; |
| 2110 | Sym *s; |
| 2111 | |
| 2112 | s = n->right->sym; |
| 2113 | |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 2114 | dowidth(t); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2115 | f1 = T; |
| 2116 | if(t->etype == TSTRUCT || t->etype == TINTER) |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2117 | f1 = lookdot1(n, s, t, t->type, dostrcmp); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2118 | |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2119 | f2 = T; |
| 2120 | if(n->left->type == t || n->left->type->sym == S) { |
Russ Cox | 4267974 | 2012-03-07 02:27:15 -0500 | [diff] [blame] | 2121 | f2 = methtype(t, 0); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2122 | if(f2 != T) { |
| 2123 | // Use f2->method, not f2->xmethod: adddot has |
| 2124 | // already inserted all the necessary embedded dots. |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2125 | f2 = lookdot1(n, s, f2, f2->method, dostrcmp); |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2126 | } |
| 2127 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2128 | |
| 2129 | if(f1 != T) { |
| 2130 | if(f2 != T) |
Russ Cox | bf0c190 | 2012-01-23 15:10:53 -0500 | [diff] [blame] | 2131 | yyerror("%S is both field and method", |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2132 | n->right->sym); |
Russ Cox | 4589c34 | 2010-02-18 18:31:13 -0800 | [diff] [blame] | 2133 | if(f1->width == BADWIDTH) |
| 2134 | fatal("lookdot badwidth %T %p", f1, f1); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2135 | n->xoffset = f1->width; |
| 2136 | n->type = f1->type; |
Russ Cox | 3d40062 | 2012-11-02 00:17:21 -0400 | [diff] [blame] | 2137 | n->paramfld = f1; |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2138 | if(t->etype == TINTER) { |
| 2139 | if(isptr[n->left->type->etype]) { |
| 2140 | n->left = nod(OIND, n->left, N); // implicitstar |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2141 | n->left->implicit = 1; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2142 | typecheck(&n->left, Erv); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2143 | } |
| 2144 | n->op = ODOTINTER; |
| 2145 | } |
| 2146 | return 1; |
| 2147 | } |
| 2148 | |
| 2149 | if(f2 != T) { |
| 2150 | tt = n->left->type; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 2151 | dowidth(tt); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2152 | rcvr = getthisx(f2->type)->type->type; |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2153 | if(!eqtype(rcvr, tt)) { |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2154 | if(rcvr->etype == tptr && eqtype(rcvr->type, tt)) { |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2155 | checklvalue(n->left, "call pointer method on"); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2156 | n->left = nod(OADDR, n->left, N); |
Russ Cox | 2a70198 | 2010-10-07 04:42:44 -0400 | [diff] [blame] | 2157 | n->left->implicit = 1; |
Russ Cox | 0d66825 | 2009-12-18 17:24:58 -0800 | [diff] [blame] | 2158 | typecheck(&n->left, Etype|Erv); |
Russ Cox | 93fcb92 | 2014-10-20 22:04:12 -0400 | [diff] [blame] | 2159 | } else if(tt->etype == tptr && rcvr->etype != tptr && eqtype(tt->type, rcvr)) { |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2160 | n->left = nod(OIND, n->left, N); |
Russ Cox | 2a70198 | 2010-10-07 04:42:44 -0400 | [diff] [blame] | 2161 | n->left->implicit = 1; |
Russ Cox | 0d66825 | 2009-12-18 17:24:58 -0800 | [diff] [blame] | 2162 | typecheck(&n->left, Etype|Erv); |
Russ Cox | 93fcb92 | 2014-10-20 22:04:12 -0400 | [diff] [blame] | 2163 | } else if(tt->etype == tptr && tt->type->etype == tptr && eqtype(derefall(tt), derefall(rcvr))) { |
Luuk van Dijk | 7df9ff5 | 2011-11-02 17:18:53 +0100 | [diff] [blame] | 2164 | yyerror("calling method %N with receiver %lN requires explicit dereference", n->right, n->left); |
| 2165 | while(tt->etype == tptr) { |
Russ Cox | 93fcb92 | 2014-10-20 22:04:12 -0400 | [diff] [blame] | 2166 | // Stop one level early for method with pointer receiver. |
| 2167 | if(rcvr->etype == tptr && tt->type->etype != tptr) |
| 2168 | break; |
Luuk van Dijk | 7df9ff5 | 2011-11-02 17:18:53 +0100 | [diff] [blame] | 2169 | n->left = nod(OIND, n->left, N); |
| 2170 | n->left->implicit = 1; |
| 2171 | typecheck(&n->left, Etype|Erv); |
| 2172 | tt = tt->type; |
| 2173 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2174 | } else { |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2175 | fatal("method mismatch: %T for %T", rcvr, tt); |
| 2176 | } |
| 2177 | } |
| 2178 | n->right = methodname(n->right, n->left->type); |
| 2179 | n->xoffset = f2->width; |
| 2180 | n->type = f2->type; |
Russ Cox | fc12840 | 2011-12-09 08:03:51 -0500 | [diff] [blame] | 2181 | // print("lookdot found [%p] %T\n", f2->type, f2->type); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2182 | n->op = ODOTMETH; |
| 2183 | return 1; |
| 2184 | } |
| 2185 | |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2189 | static int |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2190 | nokeys(NodeList *l) |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2191 | { |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2192 | for(; l; l=l->next) |
| 2193 | if(l->n->op == OKEY) |
| 2194 | return 0; |
| 2195 | return 1; |
| 2196 | } |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2197 | |
Jan Ziak | 1d2b71c | 2014-04-16 22:42:09 -0400 | [diff] [blame] | 2198 | static int |
| 2199 | hasddd(Type *t) |
| 2200 | { |
| 2201 | Type *tl; |
| 2202 | |
| 2203 | for(tl=t->type; tl; tl=tl->down) { |
| 2204 | if(tl->isddd) |
| 2205 | return 1; |
| 2206 | } |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
| 2210 | static int |
| 2211 | downcount(Type *t) |
| 2212 | { |
| 2213 | Type *tl; |
| 2214 | int n; |
| 2215 | |
| 2216 | n = 0; |
| 2217 | for(tl=t->type; tl; tl=tl->down) { |
| 2218 | n++; |
| 2219 | } |
| 2220 | return n; |
| 2221 | } |
| 2222 | |
Russ Cox | a95ee61 | 2009-09-21 15:45:55 -0700 | [diff] [blame] | 2223 | /* |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2224 | * typecheck assignment: type list = expression list |
| 2225 | */ |
| 2226 | static void |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2227 | typecheckaste(int op, Node *call, int isddd, Type *tstruct, NodeList *nl, char *desc) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2228 | { |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2229 | Type *t, *tl, *tn; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2230 | Node *n; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2231 | int lno; |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2232 | char *why; |
Jan Ziak | 1d2b71c | 2014-04-16 22:42:09 -0400 | [diff] [blame] | 2233 | int n1, n2; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2234 | |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2235 | lno = lineno; |
| 2236 | |
Russ Cox | 680ee6a | 2009-10-08 23:03:34 -0700 | [diff] [blame] | 2237 | if(tstruct->broke) |
| 2238 | goto out; |
| 2239 | |
Jan Ziak | 1483747 | 2014-03-14 16:42:42 +0100 | [diff] [blame] | 2240 | n = N; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2241 | if(nl != nil && nl->next == nil && (n = nl->n)->type != T) |
| 2242 | if(n->type->etype == TSTRUCT && n->type->funarg) { |
Jan Ziak | 1d2b71c | 2014-04-16 22:42:09 -0400 | [diff] [blame] | 2243 | if(!hasddd(tstruct)) { |
| 2244 | n1 = downcount(tstruct); |
| 2245 | n2 = downcount(n->type); |
| 2246 | if(n2 > n1) |
| 2247 | goto toomany; |
| 2248 | if(n2 < n1) |
| 2249 | goto notenough; |
| 2250 | } |
| 2251 | |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2252 | tn = n->type->type; |
| 2253 | for(tl=tstruct->type; tl; tl=tl->down) { |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 2254 | if(tl->isddd) { |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2255 | for(; tn; tn=tn->down) { |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2256 | if(assignop(tn->type, tl->type->type, &why) == 0) { |
| 2257 | if(call != N) |
Daniel Morsing | 7e270cf | 2013-07-16 11:43:11 +0200 | [diff] [blame] | 2258 | yyerror("cannot use %T as type %T in argument to %N%s", tn->type, tl->type->type, call, why); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2259 | else |
Daniel Morsing | 7e270cf | 2013-07-16 11:43:11 +0200 | [diff] [blame] | 2260 | yyerror("cannot use %T as type %T in %s%s", tn->type, tl->type->type, desc, why); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2261 | } |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2262 | } |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 2263 | goto out; |
| 2264 | } |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2265 | if(tn == T) |
| 2266 | goto notenough; |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2267 | if(assignop(tn->type, tl->type, &why) == 0) { |
| 2268 | if(call != N) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2269 | yyerror("cannot use %T as type %T in argument to %N%s", tn->type, tl->type, call, why); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2270 | else |
| 2271 | yyerror("cannot use %T as type %T in %s%s", tn->type, tl->type, desc, why); |
| 2272 | } |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2273 | tn = tn->down; |
| 2274 | } |
| 2275 | if(tn != T) |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2276 | goto toomany; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2277 | goto out; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
Jan Ziak | 1d2b71c | 2014-04-16 22:42:09 -0400 | [diff] [blame] | 2280 | n1 = downcount(tstruct); |
| 2281 | n2 = count(nl); |
| 2282 | if(!hasddd(tstruct)) { |
| 2283 | if(n2 > n1) |
| 2284 | goto toomany; |
| 2285 | if(n2 < n1) |
| 2286 | goto notenough; |
| 2287 | } |
| 2288 | else { |
| 2289 | if(!isddd) { |
| 2290 | if(n2 < n1-1) |
| 2291 | goto notenough; |
| 2292 | } else { |
| 2293 | if(n2 > n1) |
| 2294 | goto toomany; |
| 2295 | if(n2 < n1) |
| 2296 | goto notenough; |
| 2297 | } |
| 2298 | } |
| 2299 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2300 | for(tl=tstruct->type; tl; tl=tl->down) { |
| 2301 | t = tl->type; |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 2302 | if(tl->isddd) { |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2303 | if(isddd) { |
| 2304 | if(nl == nil) |
| 2305 | goto notenough; |
| 2306 | if(nl->next != nil) |
| 2307 | goto toomany; |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2308 | n = nl->n; |
| 2309 | setlineno(n); |
| 2310 | if(n->type != T) |
| 2311 | nl->n = assignconv(n, t, desc); |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 2312 | goto out; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2313 | } |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2314 | for(; nl; nl=nl->next) { |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2315 | n = nl->n; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2316 | setlineno(nl->n); |
Russ Cox | a3c6822 | 2010-10-03 11:50:44 -0400 | [diff] [blame] | 2317 | if(n->type != T) |
| 2318 | nl->n = assignconv(n, t->type, desc); |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2319 | } |
| 2320 | goto out; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2321 | } |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2322 | if(nl == nil) |
| 2323 | goto notenough; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2324 | n = nl->n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2325 | setlineno(n); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2326 | if(n->type != T) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2327 | nl->n = assignconv(n, t, desc); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2328 | nl = nl->next; |
| 2329 | } |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2330 | if(nl != nil) |
| 2331 | goto toomany; |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2332 | if(isddd) { |
| 2333 | if(call != N) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2334 | yyerror("invalid use of ... in call to %N", call); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2335 | else |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2336 | yyerror("invalid use of ... in %O", op); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2337 | } |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2338 | |
| 2339 | out: |
| 2340 | lineno = lno; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2341 | return; |
| 2342 | |
| 2343 | notenough: |
Jan Ziak | 1483747 | 2014-03-14 16:42:42 +0100 | [diff] [blame] | 2344 | if(n == N || !n->diag) { |
| 2345 | if(call != N) |
| 2346 | yyerror("not enough arguments in call to %N", call); |
| 2347 | else |
| 2348 | yyerror("not enough arguments to %O", op); |
| 2349 | if(n != N) |
| 2350 | n->diag = 1; |
| 2351 | } |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2352 | goto out; |
| 2353 | |
| 2354 | toomany: |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2355 | if(call != N) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2356 | yyerror("too many arguments in call to %N", call); |
Russ Cox | 5038792 | 2011-01-30 16:07:57 -0500 | [diff] [blame] | 2357 | else |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2358 | yyerror("too many arguments to %O", op); |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2359 | goto out; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | /* |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2363 | * type check composite |
| 2364 | */ |
| 2365 | |
| 2366 | static void |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2367 | fielddup(Node *n, Node **hash, ulong nhash) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2368 | { |
| 2369 | uint h; |
| 2370 | char *s; |
| 2371 | Node *a; |
| 2372 | |
| 2373 | if(n->op != ONAME) |
| 2374 | fatal("fielddup: not ONAME"); |
| 2375 | s = n->sym->name; |
| 2376 | h = stringhash(s)%nhash; |
| 2377 | for(a=hash[h]; a!=N; a=a->ntest) { |
| 2378 | if(strcmp(a->sym->name, s) == 0) { |
| 2379 | yyerror("duplicate field name in struct literal: %s", s); |
| 2380 | return; |
| 2381 | } |
| 2382 | } |
| 2383 | n->ntest = hash[h]; |
| 2384 | hash[h] = n; |
| 2385 | } |
| 2386 | |
| 2387 | static void |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2388 | keydup(Node *n, Node **hash, ulong nhash) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2389 | { |
| 2390 | uint h; |
| 2391 | ulong b; |
| 2392 | double d; |
| 2393 | int i; |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2394 | Node *a, *orign; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2395 | Node cmp; |
| 2396 | char *s; |
| 2397 | |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2398 | orign = n; |
| 2399 | if(n->op == OCONVIFACE) |
| 2400 | n = n->left; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2401 | evconst(n); |
| 2402 | if(n->op != OLITERAL) |
| 2403 | return; // we dont check variables |
| 2404 | |
| 2405 | switch(n->val.ctype) { |
| 2406 | default: // unknown, bool, nil |
| 2407 | b = 23; |
| 2408 | break; |
| 2409 | case CTINT: |
Russ Cox | be0ffbf | 2011-12-08 22:07:43 -0500 | [diff] [blame] | 2410 | case CTRUNE: |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2411 | b = mpgetfix(n->val.u.xval); |
| 2412 | break; |
| 2413 | case CTFLT: |
| 2414 | d = mpgetflt(n->val.u.fval); |
| 2415 | s = (char*)&d; |
| 2416 | b = 0; |
| 2417 | for(i=sizeof(d); i>0; i--) |
| 2418 | b = b*PRIME1 + *s++; |
| 2419 | break; |
| 2420 | case CTSTR: |
| 2421 | b = 0; |
| 2422 | s = n->val.u.sval->s; |
| 2423 | for(i=n->val.u.sval->len; i>0; i--) |
| 2424 | b = b*PRIME1 + *s++; |
| 2425 | break; |
| 2426 | } |
| 2427 | |
| 2428 | h = b%nhash; |
| 2429 | memset(&cmp, 0, sizeof(cmp)); |
| 2430 | for(a=hash[h]; a!=N; a=a->ntest) { |
| 2431 | cmp.op = OEQ; |
| 2432 | cmp.left = n; |
Russ Cox | ec38c6f | 2014-05-15 15:34:37 -0400 | [diff] [blame] | 2433 | b = 0; |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2434 | if(a->op == OCONVIFACE && orign->op == OCONVIFACE) { |
Russ Cox | ec38c6f | 2014-05-15 15:34:37 -0400 | [diff] [blame] | 2435 | if(eqtype(a->left->type, n->type)) { |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2436 | cmp.right = a->left; |
| 2437 | evconst(&cmp); |
| 2438 | b = cmp.val.u.bval; |
| 2439 | } |
Russ Cox | ec38c6f | 2014-05-15 15:34:37 -0400 | [diff] [blame] | 2440 | } else if(eqtype(a->type, n->type)) { |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2441 | cmp.right = a; |
| 2442 | evconst(&cmp); |
| 2443 | b = cmp.val.u.bval; |
| 2444 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2445 | if(b) { |
Russ Cox | 5ab9d2b | 2012-03-05 13:47:36 -0500 | [diff] [blame] | 2446 | yyerror("duplicate key %N in map literal", n); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2447 | return; |
| 2448 | } |
| 2449 | } |
Jan Ziak | 3072df5 | 2014-04-04 16:46:23 +0200 | [diff] [blame] | 2450 | orign->ntest = hash[h]; |
| 2451 | hash[h] = orign; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | static void |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2455 | indexdup(Node *n, Node **hash, ulong nhash) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2456 | { |
| 2457 | uint h; |
| 2458 | Node *a; |
| 2459 | ulong b, c; |
| 2460 | |
| 2461 | if(n->op != OLITERAL) |
| 2462 | fatal("indexdup: not OLITERAL"); |
| 2463 | |
| 2464 | b = mpgetfix(n->val.u.xval); |
| 2465 | h = b%nhash; |
| 2466 | for(a=hash[h]; a!=N; a=a->ntest) { |
| 2467 | c = mpgetfix(a->val.u.xval); |
| 2468 | if(b == c) { |
| 2469 | yyerror("duplicate index in array literal: %ld", b); |
| 2470 | return; |
| 2471 | } |
| 2472 | } |
| 2473 | n->ntest = hash[h]; |
| 2474 | hash[h] = n; |
| 2475 | } |
| 2476 | |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2477 | static int |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2478 | prime(ulong h, ulong sr) |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2479 | { |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2480 | ulong n; |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2481 | |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2482 | for(n=3; n<=sr; n+=2) |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2483 | if(h%n == 0) |
| 2484 | return 0; |
| 2485 | return 1; |
| 2486 | } |
| 2487 | |
| 2488 | static ulong |
| 2489 | inithash(Node *n, Node ***hash, Node **autohash, ulong nautohash) |
| 2490 | { |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2491 | ulong h, sr; |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2492 | NodeList *ll; |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2493 | int i; |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2494 | |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2495 | // count the number of entries |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2496 | h = 0; |
| 2497 | for(ll=n->list; ll; ll=ll->next) |
| 2498 | h++; |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2499 | |
| 2500 | // if the auto hash table is |
| 2501 | // large enough use it. |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2502 | if(h <= nautohash) { |
| 2503 | *hash = autohash; |
| 2504 | memset(*hash, 0, nautohash * sizeof(**hash)); |
| 2505 | return nautohash; |
| 2506 | } |
Ken Thompson | 8cb8ba1 | 2010-11-20 15:58:28 -0800 | [diff] [blame] | 2507 | |
| 2508 | // make hash size odd and 12% larger than entries |
| 2509 | h += h/8; |
| 2510 | h |= 1; |
| 2511 | |
| 2512 | // calculate sqrt of h |
| 2513 | sr = h/2; |
| 2514 | for(i=0; i<5; i++) |
| 2515 | sr = (sr + h/sr)/2; |
| 2516 | |
| 2517 | // check for primeality |
| 2518 | while(!prime(h, sr)) |
| 2519 | h += 2; |
| 2520 | |
| 2521 | // build and return a throw-away hash table |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2522 | *hash = mal(h * sizeof(**hash)); |
| 2523 | memset(*hash, 0, h * sizeof(**hash)); |
| 2524 | return h; |
| 2525 | } |
| 2526 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2527 | static int |
| 2528 | iscomptype(Type *t) |
| 2529 | { |
| 2530 | switch(t->etype) { |
| 2531 | case TARRAY: |
| 2532 | case TSTRUCT: |
| 2533 | case TMAP: |
| 2534 | return 1; |
| 2535 | case TPTR32: |
| 2536 | case TPTR64: |
| 2537 | switch(t->type->etype) { |
| 2538 | case TARRAY: |
| 2539 | case TSTRUCT: |
| 2540 | case TMAP: |
| 2541 | return 1; |
| 2542 | } |
| 2543 | break; |
| 2544 | } |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
| 2548 | static void |
| 2549 | pushtype(Node *n, Type *t) |
| 2550 | { |
| 2551 | if(n == N || n->op != OCOMPLIT || !iscomptype(t)) |
| 2552 | return; |
| 2553 | |
| 2554 | if(n->right == N) { |
| 2555 | n->right = typenod(t); |
Russ Cox | 5c52404 | 2012-02-09 00:26:08 -0500 | [diff] [blame] | 2556 | n->implicit = 1; // don't print |
| 2557 | n->right->implicit = 1; // * is okay |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2558 | } |
| 2559 | else if(debug['s']) { |
| 2560 | typecheck(&n->right, Etype); |
| 2561 | if(n->right->type != T && eqtype(n->right->type, t)) |
Shenghou Ma | 76b2f06 | 2014-12-30 19:48:26 -0500 | [diff] [blame] | 2562 | print("%L: redundant type: %T\n", n->lineno, t); |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2566 | static void |
| 2567 | typecheckcomplit(Node **np) |
| 2568 | { |
Rob Pike | 4dcb13b | 2013-04-29 22:44:40 -0700 | [diff] [blame] | 2569 | int bad, i, nerr; |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2570 | int64 length; |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 2571 | Node *l, *n, *norig, *r, **hash; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2572 | NodeList *ll; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2573 | Type *t, *f; |
Daniel Morsing | 87c35d8 | 2012-10-07 16:47:53 +0200 | [diff] [blame] | 2574 | Sym *s, *s1; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2575 | int32 lno; |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2576 | ulong nhash; |
| 2577 | Node *autohash[101]; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2578 | |
| 2579 | n = *np; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2580 | lno = lineno; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2581 | |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2582 | if(n->right == N) { |
| 2583 | if(n->list != nil) |
| 2584 | setlineno(n->list->n); |
| 2585 | yyerror("missing type in composite literal"); |
| 2586 | goto error; |
| 2587 | } |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 2588 | |
| 2589 | // Save original node (including n->right) |
| 2590 | norig = nod(n->op, N, N); |
| 2591 | *norig = *n; |
| 2592 | |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2593 | setlineno(n->right); |
Russ Cox | bf899be | 2011-07-26 00:52:02 -0400 | [diff] [blame] | 2594 | l = typecheck(&n->right /* sic */, Etype|Ecomplit); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2595 | if((t = l->type) == T) |
| 2596 | goto error; |
| 2597 | nerr = nerrors; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2598 | n->type = t; |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 2599 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2600 | if(isptr[t->etype]) { |
Luuk van Dijk | 419c53a | 2012-02-06 12:19:59 +0100 | [diff] [blame] | 2601 | // For better or worse, we don't allow pointers as the composite literal type, |
Russ Cox | 5c52404 | 2012-02-09 00:26:08 -0500 | [diff] [blame] | 2602 | // except when using the &T syntax, which sets implicit on the OIND. |
| 2603 | if(!n->right->implicit) { |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2604 | yyerror("invalid pointer type %T for composite literal (use &%T instead)", t, t->type); |
| 2605 | goto error; |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2606 | } |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2607 | // Also, the underlying type must be a struct, map, slice, or array. |
| 2608 | if(!iscomptype(t)) { |
| 2609 | yyerror("invalid pointer type %T for composite literal", t); |
| 2610 | goto error; |
| 2611 | } |
Rémy Oudompheng | 9e66ee4 | 2013-02-26 00:43:31 +0100 | [diff] [blame] | 2612 | t = t->type; |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2613 | } |
| 2614 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2615 | switch(t->etype) { |
| 2616 | default: |
| 2617 | yyerror("invalid type for composite literal: %T", t); |
| 2618 | n->type = T; |
| 2619 | break; |
| 2620 | |
| 2621 | case TARRAY: |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2622 | nhash = inithash(n, &hash, autohash, nelem(autohash)); |
| 2623 | |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2624 | length = 0; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2625 | i = 0; |
| 2626 | for(ll=n->list; ll; ll=ll->next) { |
| 2627 | l = ll->n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2628 | setlineno(l); |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2629 | if(l->op != OKEY) { |
| 2630 | l = nod(OKEY, nodintconst(i), l); |
| 2631 | l->left->type = types[TINT]; |
| 2632 | l->left->typecheck = 1; |
| 2633 | ll->n = l; |
| 2634 | } |
| 2635 | |
| 2636 | typecheck(&l->left, Erv); |
| 2637 | evconst(l->left); |
| 2638 | i = nonnegconst(l->left); |
Jan Ziak | 2ca9950 | 2014-03-29 15:45:40 +0100 | [diff] [blame] | 2639 | if(i < 0 && !l->left->diag) { |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2640 | yyerror("array index must be non-negative integer constant"); |
Jan Ziak | 2ca9950 | 2014-03-29 15:45:40 +0100 | [diff] [blame] | 2641 | l->left->diag = 1; |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2642 | i = -(1<<30); // stay negative for a while |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2643 | } |
| 2644 | if(i >= 0) |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2645 | indexdup(l->left, hash, nhash); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2646 | i++; |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2647 | if(i > length) { |
| 2648 | length = i; |
| 2649 | if(t->bound >= 0 && length > t->bound) { |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2650 | setlineno(l); |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2651 | yyerror("array index %lld out of bounds [0:%lld]", length-1, t->bound); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2652 | t->bound = -1; // no more errors |
| 2653 | } |
| 2654 | } |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2655 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2656 | r = l->right; |
| 2657 | pushtype(r, t->type); |
| 2658 | typecheck(&r, Erv); |
| 2659 | defaultlit(&r, t->type); |
| 2660 | l->right = assignconv(r, t->type, "array element"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2661 | } |
| 2662 | if(t->bound == -100) |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2663 | t->bound = length; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2664 | if(t->bound < 0) |
Russ Cox | 8d44ede | 2015-01-22 12:10:59 -0500 | [diff] [blame] | 2665 | n->right = nodintconst(length); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2666 | n->op = OARRAYLIT; |
| 2667 | break; |
| 2668 | |
| 2669 | case TMAP: |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2670 | nhash = inithash(n, &hash, autohash, nelem(autohash)); |
| 2671 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2672 | for(ll=n->list; ll; ll=ll->next) { |
| 2673 | l = ll->n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2674 | setlineno(l); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2675 | if(l->op != OKEY) { |
| 2676 | typecheck(&ll->n, Erv); |
| 2677 | yyerror("missing key in map literal"); |
| 2678 | continue; |
| 2679 | } |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2680 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2681 | typecheck(&l->left, Erv); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2682 | defaultlit(&l->left, t->down); |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2683 | l->left = assignconv(l->left, t->down, "map key"); |
Jeff R. Allen | 46e7cb5 | 2012-01-20 13:34:38 -0500 | [diff] [blame] | 2684 | if (l->left->op != OCONV) |
| 2685 | keydup(l->left, hash, nhash); |
Russ Cox | 8ffc4ec | 2010-10-21 23:17:20 -0400 | [diff] [blame] | 2686 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2687 | r = l->right; |
| 2688 | pushtype(r, t->type); |
| 2689 | typecheck(&r, Erv); |
| 2690 | defaultlit(&r, t->type); |
| 2691 | l->right = assignconv(r, t->type, "map value"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2692 | } |
| 2693 | n->op = OMAPLIT; |
| 2694 | break; |
| 2695 | |
| 2696 | case TSTRUCT: |
| 2697 | bad = 0; |
| 2698 | if(n->list != nil && nokeys(n->list)) { |
| 2699 | // simple list of variables |
| 2700 | f = t->type; |
| 2701 | for(ll=n->list; ll; ll=ll->next) { |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2702 | setlineno(ll->n); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2703 | typecheck(&ll->n, Erv); |
| 2704 | if(f == nil) { |
| 2705 | if(!bad++) |
| 2706 | yyerror("too many values in struct initializer"); |
| 2707 | continue; |
| 2708 | } |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 2709 | s = f->sym; |
Russ Cox | 758f2bc | 2010-01-22 17:06:20 -0800 | [diff] [blame] | 2710 | if(s != nil && !exportname(s->name) && s->pkg != localpkg) |
Russ Cox | 9475caf | 2010-04-11 14:52:06 -0700 | [diff] [blame] | 2711 | yyerror("implicit assignment of unexported field '%s' in %T literal", s->name, t); |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2712 | // No pushtype allowed here. Must name fields for that. |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2713 | ll->n = assignconv(ll->n, f->type, "field value"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2714 | ll->n = nod(OKEY, newname(f->sym), ll->n); |
Russ Cox | 335da67 | 2011-08-31 07:37:14 -0400 | [diff] [blame] | 2715 | ll->n->left->type = f; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2716 | ll->n->left->typecheck = 1; |
| 2717 | f = f->down; |
| 2718 | } |
Russ Cox | ef3e681 | 2009-08-24 09:23:04 -0700 | [diff] [blame] | 2719 | if(f != nil) |
| 2720 | yyerror("too few values in struct initializer"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2721 | } else { |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2722 | nhash = inithash(n, &hash, autohash, nelem(autohash)); |
| 2723 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2724 | // keyed list |
| 2725 | for(ll=n->list; ll; ll=ll->next) { |
| 2726 | l = ll->n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2727 | setlineno(l); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2728 | if(l->op != OKEY) { |
| 2729 | if(!bad++) |
| 2730 | yyerror("mixture of field:value and value initializers"); |
| 2731 | typecheck(&ll->n, Erv); |
| 2732 | continue; |
| 2733 | } |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 2734 | s = l->left->sym; |
| 2735 | if(s == S) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2736 | yyerror("invalid field name %N in struct initializer", l->left); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2737 | typecheck(&l->right, Erv); |
| 2738 | continue; |
| 2739 | } |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 2740 | |
Russ Cox | 7b240e8 | 2010-07-26 14:21:39 -0700 | [diff] [blame] | 2741 | // Sym might have resolved to name in other top-level |
| 2742 | // package, because of import dot. Redirect to correct sym |
| 2743 | // before we do the lookup. |
Daniel Morsing | 87c35d8 | 2012-10-07 16:47:53 +0200 | [diff] [blame] | 2744 | if(s->pkg != localpkg && exportname(s->name)) { |
| 2745 | s1 = lookup(s->name); |
| 2746 | if(s1->origpkg == s->pkg) |
| 2747 | s = s1; |
| 2748 | } |
Russ Cox | 7ae1fe4 | 2012-02-10 22:46:56 -0500 | [diff] [blame] | 2749 | f = lookdot1(nil, s, t, t->type, 0); |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 2750 | if(f == nil) { |
Luuk van Dijk | 40b2fe0 | 2011-12-05 14:40:19 -0500 | [diff] [blame] | 2751 | yyerror("unknown %T field '%S' in struct literal", t, s); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2752 | continue; |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 2753 | } |
Russ Cox | 335da67 | 2011-08-31 07:37:14 -0400 | [diff] [blame] | 2754 | l->left = newname(s); |
| 2755 | l->left->typecheck = 1; |
| 2756 | l->left->type = f; |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 2757 | s = f->sym; |
Ken Thompson | b3dd22f | 2010-11-18 13:07:34 -0800 | [diff] [blame] | 2758 | fielddup(newname(s), hash, nhash); |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2759 | r = l->right; |
Russ Cox | 5cb1c82 | 2011-12-05 14:22:41 -0500 | [diff] [blame] | 2760 | // No pushtype allowed here. Tried and rejected. |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2761 | typecheck(&r, Erv); |
| 2762 | l->right = assignconv(r, f->type, "field value"); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | n->op = OSTRUCTLIT; |
| 2766 | break; |
| 2767 | } |
| 2768 | if(nerr != nerrors) |
| 2769 | goto error; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2770 | |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 2771 | n->orig = norig; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2772 | if(isptr[n->type->etype]) { |
| 2773 | n = nod(OPTRLIT, n, N); |
| 2774 | n->typecheck = 1; |
| 2775 | n->type = n->left->type; |
| 2776 | n->left->type = t; |
Russ Cox | 5e98505 | 2011-12-07 15:48:55 -0500 | [diff] [blame] | 2777 | n->left->typecheck = 1; |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 2778 | } |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2779 | |
Rémy Oudompheng | b0bb6f8 | 2013-03-04 16:42:03 +0100 | [diff] [blame] | 2780 | n->orig = norig; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2781 | *np = n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2782 | lineno = lno; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2783 | return; |
| 2784 | |
| 2785 | error: |
| 2786 | n->type = T; |
| 2787 | *np = n; |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 2788 | lineno = lno; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | /* |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2792 | * lvalue etc |
| 2793 | */ |
Russ Cox | 0bd41e2 | 2010-02-01 22:18:51 -0800 | [diff] [blame] | 2794 | int |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2795 | islvalue(Node *n) |
| 2796 | { |
| 2797 | switch(n->op) { |
| 2798 | case OINDEX: |
Russ Cox | 5e25364 | 2010-05-24 14:22:54 -0700 | [diff] [blame] | 2799 | if(isfixedarray(n->left->type)) |
| 2800 | return islvalue(n->left); |
Russ Cox | e48c0fb | 2010-10-26 21:11:17 -0700 | [diff] [blame] | 2801 | if(n->left->type != T && n->left->type->etype == TSTRING) |
| 2802 | return 0; |
Russ Cox | 5e25364 | 2010-05-24 14:22:54 -0700 | [diff] [blame] | 2803 | // fall through |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2804 | case OIND: |
| 2805 | case ODOTPTR: |
Russ Cox | d3c758d | 2013-03-20 17:11:09 -0400 | [diff] [blame] | 2806 | case OCLOSUREVAR: |
Russ Cox | e5ef657 | 2014-12-29 11:47:04 -0500 | [diff] [blame] | 2807 | case OPARAM: |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2808 | return 1; |
| 2809 | case ODOT: |
| 2810 | return islvalue(n->left); |
| 2811 | case ONAME: |
| 2812 | if(n->class == PFUNC) |
| 2813 | return 0; |
| 2814 | return 1; |
| 2815 | } |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | static void |
| 2820 | checklvalue(Node *n, char *verb) |
| 2821 | { |
| 2822 | if(!islvalue(n)) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2823 | yyerror("cannot %s %N", verb, n); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2824 | } |
| 2825 | |
Dmitry Vyukov | b581ca5 | 2015-01-26 11:26:55 +0300 | [diff] [blame] | 2826 | void |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2827 | checkassign(Node *stmt, Node *n) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2828 | { |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2829 | Node *r, *l; |
| 2830 | |
Dmitry Vyukov | eaa8720 | 2015-01-29 18:33:19 +0300 | [diff] [blame] | 2831 | // Variables declared in ORANGE are assigned on every iteration. |
| 2832 | if(n->defn != stmt || stmt->op == ORANGE) { |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2833 | r = outervalue(n); |
| 2834 | for(l = n; l != r; l = l->left) { |
| 2835 | l->assigned = 1; |
| 2836 | if(l->closure) |
| 2837 | l->closure->assigned = 1; |
| 2838 | } |
| 2839 | l->assigned = 1; |
| 2840 | if(l->closure) |
| 2841 | l->closure->assigned = 1; |
| 2842 | } |
| 2843 | |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2844 | if(islvalue(n)) |
| 2845 | return; |
| 2846 | if(n->op == OINDEXMAP) { |
| 2847 | n->etype = 1; |
| 2848 | return; |
| 2849 | } |
Daniel Morsing | f2e94b5 | 2014-01-03 21:03:20 +0100 | [diff] [blame] | 2850 | |
| 2851 | // have already complained about n being undefined |
| 2852 | if(n->op == ONONAME) |
| 2853 | return; |
| 2854 | |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2855 | yyerror("cannot assign to %N", n); |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | static void |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2859 | checkassignlist(Node *stmt, NodeList *l) |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 2860 | { |
| 2861 | for(; l; l=l->next) |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2862 | checkassign(stmt, l->n); |
Russ Cox | ff3a73b | 2009-07-30 16:53:08 -0700 | [diff] [blame] | 2863 | } |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2864 | |
Russ Cox | 1552e62 | 2014-10-16 12:43:17 -0400 | [diff] [blame] | 2865 | // Check whether l and r are the same side effect-free expression, |
| 2866 | // so that it is safe to reuse one instead of computing both. |
Josh Bleecher Snyder | f03c920 | 2015-01-07 17:44:49 -0800 | [diff] [blame] | 2867 | int |
Russ Cox | 1552e62 | 2014-10-16 12:43:17 -0400 | [diff] [blame] | 2868 | samesafeexpr(Node *l, Node *r) |
| 2869 | { |
| 2870 | if(l->op != r->op || !eqtype(l->type, r->type)) |
| 2871 | return 0; |
| 2872 | |
| 2873 | switch(l->op) { |
| 2874 | case ONAME: |
| 2875 | case OCLOSUREVAR: |
| 2876 | return l == r; |
| 2877 | |
| 2878 | case ODOT: |
| 2879 | case ODOTPTR: |
| 2880 | return l->right != nil && r->right != nil && l->right->sym == r->right->sym && samesafeexpr(l->left, r->left); |
| 2881 | |
| 2882 | case OIND: |
| 2883 | return samesafeexpr(l->left, r->left); |
| 2884 | |
| 2885 | case OINDEX: |
| 2886 | return samesafeexpr(l->left, r->left) && samesafeexpr(l->right, r->right); |
| 2887 | } |
| 2888 | |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2892 | /* |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2893 | * type check assignment. |
| 2894 | * if this assignment is the definition of a var on the left side, |
| 2895 | * fill in the var's type. |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2896 | */ |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2897 | |
| 2898 | static void |
| 2899 | typecheckas(Node *n) |
| 2900 | { |
| 2901 | // delicate little dance. |
| 2902 | // the definition of n may refer to this assignment |
| 2903 | // as its definition, in which case it will call typecheckas. |
| 2904 | // in that case, do not call typecheck back, or it will cycle. |
| 2905 | // if the variable has a type (ntype) then typechecking |
| 2906 | // will not look at defn, so it is okay (and desirable, |
| 2907 | // so that the conversion below happens). |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 2908 | n->left = resolve(n->left); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2909 | if(n->left->defn != n || n->left->ntype) |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 2910 | typecheck(&n->left, Erv | Easgn); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2911 | |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2912 | typecheck(&n->right, Erv); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2913 | checkassign(n, n->left); |
Russ Cox | 9da6666 | 2009-12-03 22:09:58 -0800 | [diff] [blame] | 2914 | if(n->right && n->right->type != T) { |
| 2915 | if(n->left->type != T) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2916 | n->right = assignconv(n->right, n->left->type, "assignment"); |
Russ Cox | 9da6666 | 2009-12-03 22:09:58 -0800 | [diff] [blame] | 2917 | } |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2918 | if(n->left->defn == n && n->left->ntype == N) { |
| 2919 | defaultlit(&n->right, T); |
| 2920 | n->left->type = n->right->type; |
| 2921 | } |
| 2922 | |
| 2923 | // second half of dance. |
| 2924 | // now that right is done, typecheck the left |
| 2925 | // just to get it over with. see dance above. |
| 2926 | n->typecheck = 1; |
| 2927 | if(n->left->typecheck == 0) |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 2928 | typecheck(&n->left, Erv | Easgn); |
Russ Cox | 1552e62 | 2014-10-16 12:43:17 -0400 | [diff] [blame] | 2929 | |
| 2930 | // Recognize slices being updated in place, for better code generation later. |
| 2931 | // Don't rewrite if using race detector, to avoid needing to teach race detector |
| 2932 | // about this optimization. |
| 2933 | if(n->left && n->left->op != OINDEXMAP && n->right && !flag_race) { |
| 2934 | switch(n->right->op) { |
| 2935 | case OSLICE: |
| 2936 | case OSLICE3: |
| 2937 | case OSLICESTR: |
| 2938 | // For x = x[0:y], x can be updated in place, without touching pointer. |
Russ Cox | b035e97 | 2014-10-30 10:16:03 -0400 | [diff] [blame] | 2939 | // TODO(rsc): Reenable once it is actually updated in place without touching the pointer. |
| 2940 | if(0 && samesafeexpr(n->left, n->right->left) && (n->right->right->left == N || iszero(n->right->right->left))) |
Russ Cox | 1552e62 | 2014-10-16 12:43:17 -0400 | [diff] [blame] | 2941 | n->right->reslice = 1; |
| 2942 | break; |
| 2943 | |
| 2944 | case OAPPEND: |
| 2945 | // For x = append(x, ...), x can be updated in place when there is capacity, |
| 2946 | // without touching the pointer; otherwise the emitted code to growslice |
| 2947 | // can take care of updating the pointer, and only in that case. |
Russ Cox | b035e97 | 2014-10-30 10:16:03 -0400 | [diff] [blame] | 2948 | // TODO(rsc): Reenable once the emitted code does update the pointer. |
| 2949 | if(0 && n->right->list != nil && samesafeexpr(n->left, n->right->list->n)) |
Russ Cox | 1552e62 | 2014-10-16 12:43:17 -0400 | [diff] [blame] | 2950 | n->right->reslice = 1; |
| 2951 | break; |
| 2952 | } |
| 2953 | } |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2956 | static void |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2957 | checkassignto(Type *src, Node *dst) |
| 2958 | { |
| 2959 | char *why; |
| 2960 | |
| 2961 | if(assignop(src, dst->type, &why) == 0) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 2962 | yyerror("cannot assign %T to %lN in multiple assignment%s", src, dst, why); |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2963 | return; |
| 2964 | } |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | static void |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2968 | typecheckas2(Node *n) |
| 2969 | { |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2970 | int cl, cr; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2971 | NodeList *ll, *lr; |
Russ Cox | 5fc3771 | 2011-11-11 16:48:25 -0500 | [diff] [blame] | 2972 | Node *l, *r; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2973 | Iter s; |
| 2974 | Type *t; |
| 2975 | |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2976 | for(ll=n->list; ll; ll=ll->next) { |
| 2977 | // delicate little dance. |
Russ Cox | 76da278 | 2010-06-12 11:17:24 -0700 | [diff] [blame] | 2978 | ll->n = resolve(ll->n); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2979 | if(ll->n->defn != n || ll->n->ntype) |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 2980 | typecheck(&ll->n, Erv | Easgn); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2981 | } |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2982 | cl = count(n->list); |
| 2983 | cr = count(n->rlist); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2984 | if(cl > 1 && cr == 1) |
| 2985 | typecheck(&n->rlist->n, Erv | Efnstruct); |
| 2986 | else |
| 2987 | typechecklist(n->rlist, Erv); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 2988 | checkassignlist(n, n->list); |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2989 | |
| 2990 | if(cl == cr) { |
| 2991 | // easy |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2992 | for(ll=n->list, lr=n->rlist; ll; ll=ll->next, lr=lr->next) { |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 2993 | if(ll->n->type != T && lr->n->type != T) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 2994 | lr->n = assignconv(lr->n, ll->n->type, "assignment"); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 2995 | if(ll->n->defn == n && ll->n->ntype == N) { |
| 2996 | defaultlit(&lr->n, T); |
| 2997 | ll->n->type = lr->n->type; |
| 2998 | } |
| 2999 | } |
| 3000 | goto out; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | |
| 3004 | l = n->list->n; |
| 3005 | r = n->rlist->n; |
| 3006 | |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3007 | // x,y,z = f() |
| 3008 | if(cr == 1) { |
| 3009 | if(r->type == T) |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3010 | goto out; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3011 | switch(r->op) { |
| 3012 | case OCALLMETH: |
| 3013 | case OCALLINTER: |
| 3014 | case OCALLFUNC: |
| 3015 | if(r->type->etype != TSTRUCT || r->type->funarg == 0) |
| 3016 | break; |
| 3017 | cr = structcount(r->type); |
| 3018 | if(cr != cl) |
| 3019 | goto mismatch; |
| 3020 | n->op = OAS2FUNC; |
| 3021 | t = structfirst(&s, &r->type); |
| 3022 | for(ll=n->list; ll; ll=ll->next) { |
Dominik Honnef | 062ae45 | 2014-01-21 22:44:54 -0500 | [diff] [blame] | 3023 | if(t->type != T && ll->n->type != T) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 3024 | checkassignto(t->type, ll->n); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3025 | if(ll->n->defn == n && ll->n->ntype == N) |
| 3026 | ll->n->type = t->type; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3027 | t = structnext(&s); |
| 3028 | } |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3029 | goto out; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | // x, ok = y |
| 3034 | if(cl == 2 && cr == 1) { |
| 3035 | if(r->type == T) |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3036 | goto out; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3037 | switch(r->op) { |
| 3038 | case OINDEXMAP: |
| 3039 | n->op = OAS2MAPR; |
| 3040 | goto common; |
| 3041 | case ORECV: |
Russ Cox | 8bf34e3 | 2011-03-11 14:47:26 -0500 | [diff] [blame] | 3042 | n->op = OAS2RECV; |
Russ Cox | 8bf34e3 | 2011-03-11 14:47:26 -0500 | [diff] [blame] | 3043 | goto common; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3044 | case ODOTTYPE: |
| 3045 | n->op = OAS2DOTTYPE; |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 3046 | r->op = ODOTTYPE2; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3047 | common: |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 3048 | if(l->type != T) |
| 3049 | checkassignto(r->type, l); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3050 | if(l->defn == n) |
| 3051 | l->type = r->type; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3052 | l = n->list->next->n; |
Chris Manghane | 897f7a3 | 2014-08-11 16:11:55 -0700 | [diff] [blame] | 3053 | if(l->type != T && l->type->etype != TBOOL) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 3054 | checkassignto(types[TBOOL], l); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3055 | if(l->defn == n && l->ntype == N) |
| 3056 | l->type = types[TBOOL]; |
| 3057 | goto out; |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | mismatch: |
| 3062 | yyerror("assignment count mismatch: %d = %d", cl, cr); |
Russ Cox | 54b4037 | 2009-08-05 00:42:44 -0700 | [diff] [blame] | 3063 | |
| 3064 | out: |
| 3065 | // second half of dance |
| 3066 | n->typecheck = 1; |
| 3067 | for(ll=n->list; ll; ll=ll->next) |
| 3068 | if(ll->n->typecheck == 0) |
Russ Cox | 1c9e4b3 | 2009-09-15 14:11:43 -0700 | [diff] [blame] | 3069 | typecheck(&ll->n, Erv | Easgn); |
Russ Cox | d8c19c8 | 2009-08-04 10:26:29 -0700 | [diff] [blame] | 3070 | } |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 3071 | |
| 3072 | /* |
| 3073 | * type check function definition |
| 3074 | */ |
| 3075 | static void |
| 3076 | typecheckfunc(Node *n) |
| 3077 | { |
| 3078 | Type *t, *rcvr; |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 3079 | NodeList *l; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 3080 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 3081 | typecheck(&n->nname, Erv | Easgn); |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 3082 | if((t = n->nname->type) == T) |
| 3083 | return; |
| 3084 | n->type = t; |
Russ Cox | fc12840 | 2011-12-09 08:03:51 -0500 | [diff] [blame] | 3085 | t->nname = n->nname; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 3086 | rcvr = getthisx(t)->type; |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 3087 | if(rcvr != nil && n->shortname != N && !isblank(n->shortname)) |
Russ Cox | 3d40062 | 2012-11-02 00:17:21 -0400 | [diff] [blame] | 3088 | addmethod(n->shortname->sym, t, 1, n->nname->nointerface); |
Dmitry Vyukov | 0e80b2e | 2015-01-19 22:59:58 +0300 | [diff] [blame] | 3089 | |
| 3090 | for(l=n->dcl; l; l=l->next) |
| 3091 | if(l->n->op == ONAME && (l->n->class == PPARAM || l->n->class == PPARAMOUT)) |
| 3092 | l->n->decldepth = 1; |
Russ Cox | b648716 | 2009-08-07 12:50:26 -0700 | [diff] [blame] | 3093 | } |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 3094 | |
| 3095 | static void |
| 3096 | stringtoarraylit(Node **np) |
| 3097 | { |
| 3098 | int32 i; |
| 3099 | NodeList *l; |
| 3100 | Strlit *s; |
| 3101 | char *p, *ep; |
| 3102 | Rune r; |
| 3103 | Node *nn, *n; |
| 3104 | |
| 3105 | n = *np; |
| 3106 | if(n->left->op != OLITERAL || n->left->val.ctype != CTSTR) |
| 3107 | fatal("stringtoarraylit %N", n); |
| 3108 | |
| 3109 | s = n->left->val.u.sval; |
| 3110 | l = nil; |
| 3111 | p = s->s; |
| 3112 | ep = s->s + s->len; |
| 3113 | i = 0; |
| 3114 | if(n->type->type->etype == TUINT8) { |
| 3115 | // raw []byte |
| 3116 | while(p < ep) |
| 3117 | l = list(l, nod(OKEY, nodintconst(i++), nodintconst((uchar)*p++))); |
| 3118 | } else { |
Russ Cox | 6ed3fa6 | 2011-10-25 22:19:39 -0700 | [diff] [blame] | 3119 | // utf-8 []rune |
Russ Cox | 3910161 | 2010-02-25 15:11:07 -0800 | [diff] [blame] | 3120 | while(p < ep) { |
| 3121 | p += chartorune(&r, p); |
| 3122 | l = list(l, nod(OKEY, nodintconst(i++), nodintconst(r))); |
| 3123 | } |
| 3124 | } |
| 3125 | nn = nod(OCOMPLIT, N, typenod(n->type)); |
| 3126 | nn->list = l; |
| 3127 | typecheck(&nn, Erv); |
| 3128 | *np = nn; |
| 3129 | } |
Lorenzo Stoakes | b6f0632 | 2011-04-28 00:13:49 -0400 | [diff] [blame] | 3130 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3131 | |
| 3132 | static int ntypecheckdeftype; |
| 3133 | static NodeList *methodqueue; |
| 3134 | |
| 3135 | static void |
| 3136 | domethod(Node *n) |
| 3137 | { |
| 3138 | Node *nt; |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 3139 | Type *t; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3140 | |
| 3141 | nt = n->type->nname; |
| 3142 | typecheck(&nt, Etype); |
| 3143 | if(nt->type == T) { |
| 3144 | // type check failed; leave empty func |
| 3145 | n->type->etype = TFUNC; |
| 3146 | n->type->nod = N; |
| 3147 | return; |
| 3148 | } |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 3149 | |
| 3150 | // If we have |
| 3151 | // type I interface { |
| 3152 | // M(_ int) |
| 3153 | // } |
| 3154 | // then even though I.M looks like it doesn't care about the |
| 3155 | // value of its argument, a specific implementation of I may |
| 3156 | // care. The _ would suppress the assignment to that argument |
| 3157 | // while generating a call, so remove it. |
| 3158 | for(t=getinargx(nt->type)->type; t; t=t->down) { |
| 3159 | if(t->sym != nil && strcmp(t->sym->name, "_") == 0) |
| 3160 | t->sym = nil; |
| 3161 | } |
| 3162 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3163 | *n->type = *nt->type; |
| 3164 | n->type->nod = N; |
| 3165 | checkwidth(n->type); |
| 3166 | } |
| 3167 | |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 3168 | static NodeList *mapqueue; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3169 | |
| 3170 | void |
| 3171 | copytype(Node *n, Type *t) |
| 3172 | { |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 3173 | int maplineno, embedlineno, lno; |
| 3174 | NodeList *l; |
| 3175 | |
| 3176 | if(t->etype == TFORW) { |
| 3177 | // This type isn't computed yet; when it is, update n. |
| 3178 | t->copyto = list(t->copyto, n); |
| 3179 | return; |
| 3180 | } |
| 3181 | |
| 3182 | maplineno = n->type->maplineno; |
| 3183 | embedlineno = n->type->embedlineno; |
| 3184 | |
| 3185 | l = n->type->copyto; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3186 | *n->type = *t; |
| 3187 | |
| 3188 | t = n->type; |
| 3189 | t->sym = n->sym; |
| 3190 | t->local = n->local; |
| 3191 | t->vargen = n->vargen; |
| 3192 | t->siggen = 0; |
| 3193 | t->method = nil; |
Maxim Pimenov | ffa6b38 | 2011-11-28 11:52:16 -0500 | [diff] [blame] | 3194 | t->xmethod = nil; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3195 | t->nod = N; |
| 3196 | t->printed = 0; |
| 3197 | t->deferwidth = 0; |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 3198 | t->copyto = nil; |
| 3199 | |
| 3200 | // Update nodes waiting on this type. |
| 3201 | for(; l; l=l->next) |
| 3202 | copytype(l->n, t); |
| 3203 | |
| 3204 | // Double-check use of type as embedded type. |
| 3205 | lno = lineno; |
| 3206 | if(embedlineno) { |
| 3207 | lineno = embedlineno; |
| 3208 | if(isptr[t->etype]) |
| 3209 | yyerror("embedded type cannot be a pointer"); |
| 3210 | } |
| 3211 | lineno = lno; |
| 3212 | |
| 3213 | // Queue check for map until all the types are done settling. |
| 3214 | if(maplineno) { |
| 3215 | t->maplineno = maplineno; |
| 3216 | mapqueue = list(mapqueue, n); |
| 3217 | } |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | static void |
| 3221 | typecheckdeftype(Node *n) |
| 3222 | { |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 3223 | int lno; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3224 | Type *t; |
| 3225 | NodeList *l; |
| 3226 | |
| 3227 | ntypecheckdeftype++; |
| 3228 | lno = lineno; |
| 3229 | setlineno(n); |
| 3230 | n->type->sym = n->sym; |
| 3231 | n->typecheck = 1; |
| 3232 | typecheck(&n->ntype, Etype); |
| 3233 | if((t = n->ntype->type) == T) { |
| 3234 | n->diag = 1; |
Daniel Morsing | 85ce3c7 | 2012-08-31 13:02:29 -0400 | [diff] [blame] | 3235 | n->type = T; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3236 | goto ret; |
| 3237 | } |
| 3238 | if(n->type == T) { |
| 3239 | n->diag = 1; |
| 3240 | goto ret; |
| 3241 | } |
| 3242 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3243 | // copy new type and clear fields |
| 3244 | // that don't come along. |
| 3245 | // anything zeroed here must be zeroed in |
| 3246 | // typedcl2 too. |
| 3247 | copytype(n, t); |
| 3248 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3249 | ret: |
| 3250 | lineno = lno; |
| 3251 | |
| 3252 | // if there are no type definitions going on, it's safe to |
| 3253 | // try to resolve the method types for the interfaces |
| 3254 | // we just read. |
| 3255 | if(ntypecheckdeftype == 1) { |
| 3256 | while((l = methodqueue) != nil) { |
| 3257 | methodqueue = nil; |
| 3258 | for(; l; l=l->next) |
| 3259 | domethod(l->n); |
| 3260 | } |
Russ Cox | 6363fc5 | 2012-06-07 03:06:40 -0400 | [diff] [blame] | 3261 | for(l=mapqueue; l; l=l->next) { |
| 3262 | lineno = l->n->type->maplineno; |
| 3263 | maptype(l->n->type, types[TBOOL]); |
| 3264 | } |
| 3265 | lineno = lno; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3266 | } |
| 3267 | ntypecheckdeftype--; |
| 3268 | } |
| 3269 | |
| 3270 | void |
| 3271 | queuemethod(Node *n) |
| 3272 | { |
| 3273 | if(ntypecheckdeftype == 0) { |
| 3274 | domethod(n); |
| 3275 | return; |
| 3276 | } |
| 3277 | methodqueue = list(methodqueue, n); |
| 3278 | } |
| 3279 | |
| 3280 | Node* |
| 3281 | typecheckdef(Node *n) |
| 3282 | { |
Russ Cox | 933d712 | 2013-09-09 13:03:59 -0400 | [diff] [blame] | 3283 | int lno, nerrors0; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3284 | Node *e; |
| 3285 | Type *t; |
| 3286 | NodeList *l; |
| 3287 | |
| 3288 | lno = lineno; |
| 3289 | setlineno(n); |
| 3290 | |
| 3291 | if(n->op == ONONAME) { |
| 3292 | if(!n->diag) { |
| 3293 | n->diag = 1; |
| 3294 | if(n->lineno != 0) |
| 3295 | lineno = n->lineno; |
| 3296 | yyerror("undefined: %S", n->sym); |
| 3297 | } |
| 3298 | return n; |
| 3299 | } |
| 3300 | |
| 3301 | if(n->walkdef == 1) |
| 3302 | return n; |
| 3303 | |
| 3304 | l = mal(sizeof *l); |
| 3305 | l->n = n; |
| 3306 | l->next = typecheckdefstack; |
| 3307 | typecheckdefstack = l; |
| 3308 | |
| 3309 | if(n->walkdef == 2) { |
| 3310 | flusherrors(); |
| 3311 | print("typecheckdef loop:"); |
| 3312 | for(l=typecheckdefstack; l; l=l->next) |
| 3313 | print(" %S", l->n->sym); |
| 3314 | print("\n"); |
| 3315 | fatal("typecheckdef loop"); |
| 3316 | } |
| 3317 | n->walkdef = 2; |
| 3318 | |
| 3319 | if(n->type != T || n->sym == S) // builtin or no name |
| 3320 | goto ret; |
| 3321 | |
| 3322 | switch(n->op) { |
| 3323 | default: |
| 3324 | fatal("typecheckdef %O", n->op); |
| 3325 | |
Russ Cox | 7f4c5ea | 2011-06-17 15:25:05 -0400 | [diff] [blame] | 3326 | case OGOTO: |
| 3327 | case OLABEL: |
| 3328 | // not really syms |
| 3329 | break; |
| 3330 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3331 | case OLITERAL: |
| 3332 | if(n->ntype != N) { |
| 3333 | typecheck(&n->ntype, Etype); |
| 3334 | n->type = n->ntype->type; |
| 3335 | n->ntype = N; |
| 3336 | if(n->type == T) { |
| 3337 | n->diag = 1; |
| 3338 | goto ret; |
| 3339 | } |
| 3340 | } |
| 3341 | e = n->defn; |
| 3342 | n->defn = N; |
| 3343 | if(e == N) { |
| 3344 | lineno = n->lineno; |
| 3345 | dump("typecheckdef nil defn", n); |
| 3346 | yyerror("xxx"); |
| 3347 | } |
| 3348 | typecheck(&e, Erv | Eiota); |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3349 | if(isconst(e, CTNIL)) { |
| 3350 | yyerror("const initializer cannot be nil"); |
| 3351 | goto ret; |
| 3352 | } |
Russ Cox | 8931306 | 2013-02-01 23:10:02 -0500 | [diff] [blame] | 3353 | if(e->type != T && e->op != OLITERAL || !isgoconst(e)) { |
Jan Ziak | 833dae6 | 2014-03-24 20:36:42 +0100 | [diff] [blame] | 3354 | if(!e->diag) { |
| 3355 | yyerror("const initializer %N is not a constant", e); |
| 3356 | e->diag = 1; |
| 3357 | } |
Russ Cox | 8931306 | 2013-02-01 23:10:02 -0500 | [diff] [blame] | 3358 | goto ret; |
| 3359 | } |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3360 | t = n->type; |
| 3361 | if(t != T) { |
| 3362 | if(!okforconst[t->etype]) { |
| 3363 | yyerror("invalid constant type %T", t); |
| 3364 | goto ret; |
| 3365 | } |
| 3366 | if(!isideal(e->type) && !eqtype(t, e->type)) { |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 3367 | yyerror("cannot use %lN as type %T in const initializer", e, t); |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3368 | goto ret; |
| 3369 | } |
| 3370 | convlit(&e, t); |
| 3371 | } |
| 3372 | n->val = e->val; |
| 3373 | n->type = e->type; |
| 3374 | break; |
| 3375 | |
| 3376 | case ONAME: |
| 3377 | if(n->ntype != N) { |
| 3378 | typecheck(&n->ntype, Etype); |
| 3379 | n->type = n->ntype->type; |
Luuk van Dijk | 847b61b | 2011-08-24 19:07:08 +0200 | [diff] [blame] | 3380 | |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3381 | if(n->type == T) { |
| 3382 | n->diag = 1; |
| 3383 | goto ret; |
| 3384 | } |
| 3385 | } |
| 3386 | if(n->type != T) |
| 3387 | break; |
| 3388 | if(n->defn == N) { |
| 3389 | if(n->etype != 0) // like OPRINTN |
| 3390 | break; |
Russ Cox | 7f4c5ea | 2011-06-17 15:25:05 -0400 | [diff] [blame] | 3391 | if(nsavederrors+nerrors > 0) { |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3392 | // Can have undefined variables in x := foo |
| 3393 | // that make x have an n->ndefn == nil. |
| 3394 | // If there are other errors anyway, don't |
| 3395 | // bother adding to the noise. |
| 3396 | break; |
| 3397 | } |
| 3398 | fatal("var without type, init: %S", n->sym); |
| 3399 | } |
| 3400 | if(n->defn->op == ONAME) { |
| 3401 | typecheck(&n->defn, Erv); |
| 3402 | n->type = n->defn->type; |
| 3403 | break; |
| 3404 | } |
| 3405 | typecheck(&n->defn, Etop); // fills in n->type |
| 3406 | break; |
| 3407 | |
| 3408 | case OTYPE: |
| 3409 | if(curfn) |
| 3410 | defercheckwidth(); |
| 3411 | n->walkdef = 1; |
| 3412 | n->type = typ(TFORW); |
| 3413 | n->type->sym = n->sym; |
Russ Cox | 933d712 | 2013-09-09 13:03:59 -0400 | [diff] [blame] | 3414 | nerrors0 = nerrors; |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3415 | typecheckdeftype(n); |
David du Colombier | d0591d5 | 2014-02-07 17:05:16 +0100 | [diff] [blame] | 3416 | if(n->type->etype == TFORW && nerrors > nerrors0) { |
Russ Cox | 933d712 | 2013-09-09 13:03:59 -0400 | [diff] [blame] | 3417 | // Something went wrong during type-checking, |
| 3418 | // but it was reported. Silence future errors. |
| 3419 | n->type->broke = 1; |
| 3420 | } |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3421 | if(curfn) |
| 3422 | resumecheckwidth(); |
| 3423 | break; |
| 3424 | |
| 3425 | case OPACK: |
| 3426 | // nothing to see here |
| 3427 | break; |
| 3428 | } |
| 3429 | |
| 3430 | ret: |
Russ Cox | e29d3df | 2012-02-22 00:29:37 -0500 | [diff] [blame] | 3431 | if(n->op != OLITERAL && n->type != T && isideal(n->type)) |
| 3432 | fatal("got %T for %N", n->type, n); |
Luuk van Dijk | ab8ed7f | 2011-06-03 17:44:02 +0200 | [diff] [blame] | 3433 | if(typecheckdefstack->n != n) |
| 3434 | fatal("typecheckdefstack mismatch"); |
| 3435 | l = typecheckdefstack; |
| 3436 | typecheckdefstack = l->next; |
| 3437 | |
| 3438 | lineno = lno; |
| 3439 | n->walkdef = 1; |
| 3440 | return n; |
| 3441 | } |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3442 | |
| 3443 | static int |
| 3444 | checkmake(Type *t, char *arg, Node *n) |
| 3445 | { |
| 3446 | if(n->op == OLITERAL) { |
Shenghou Ma | e33e47e | 2014-02-23 16:31:48 -0500 | [diff] [blame] | 3447 | switch(n->val.ctype) { |
| 3448 | case CTINT: |
| 3449 | case CTRUNE: |
| 3450 | case CTFLT: |
| 3451 | case CTCPLX: |
| 3452 | n->val = toint(n->val); |
| 3453 | if(mpcmpfixc(n->val.u.xval, 0) < 0) { |
| 3454 | yyerror("negative %s argument in make(%T)", arg, t); |
| 3455 | return -1; |
| 3456 | } |
| 3457 | if(mpcmpfixfix(n->val.u.xval, maxintval[TINT]) > 0) { |
| 3458 | yyerror("%s argument too large in make(%T)", arg, t); |
| 3459 | return -1; |
| 3460 | } |
| 3461 | |
| 3462 | // Delay defaultlit until after we've checked range, to avoid |
| 3463 | // a redundant "constant NNN overflows int" error. |
| 3464 | defaultlit(&n, types[TINT]); |
| 3465 | return 0; |
| 3466 | default: |
| 3467 | break; |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3468 | } |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3469 | } |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3470 | |
Shenghou Ma | e33e47e | 2014-02-23 16:31:48 -0500 | [diff] [blame] | 3471 | if(!isint[n->type->etype] && n->type->etype != TIDEAL) { |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3472 | yyerror("non-integer %s argument in make(%T) - %T", arg, t, n->type); |
| 3473 | return -1; |
| 3474 | } |
Shenghou Ma | e33e47e | 2014-02-23 16:31:48 -0500 | [diff] [blame] | 3475 | |
| 3476 | // Defaultlit still necessary for non-constant: n might be 1<<k. |
| 3477 | defaultlit(&n, types[TINT]); |
| 3478 | |
Russ Cox | f02067a | 2013-02-03 14:28:44 -0500 | [diff] [blame] | 3479 | return 0; |
| 3480 | } |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 3481 | |
| 3482 | static void markbreaklist(NodeList*, Node*); |
| 3483 | |
| 3484 | static void |
| 3485 | markbreak(Node *n, Node *implicit) |
| 3486 | { |
| 3487 | Label *lab; |
| 3488 | |
| 3489 | if(n == N) |
| 3490 | return; |
| 3491 | |
| 3492 | switch(n->op) { |
| 3493 | case OBREAK: |
| 3494 | if(n->left == N) { |
| 3495 | if(implicit) |
| 3496 | implicit->hasbreak = 1; |
| 3497 | } else { |
| 3498 | lab = n->left->sym->label; |
| 3499 | if(lab != L) |
| 3500 | lab->def->hasbreak = 1; |
| 3501 | } |
| 3502 | break; |
| 3503 | |
| 3504 | case OFOR: |
| 3505 | case OSWITCH: |
| 3506 | case OTYPESW: |
| 3507 | case OSELECT: |
| 3508 | case ORANGE: |
| 3509 | implicit = n; |
| 3510 | // fall through |
| 3511 | |
| 3512 | default: |
| 3513 | markbreak(n->left, implicit); |
| 3514 | markbreak(n->right, implicit); |
| 3515 | markbreak(n->ntest, implicit); |
| 3516 | markbreak(n->nincr, implicit); |
| 3517 | markbreaklist(n->ninit, implicit); |
| 3518 | markbreaklist(n->nbody, implicit); |
| 3519 | markbreaklist(n->nelse, implicit); |
| 3520 | markbreaklist(n->list, implicit); |
| 3521 | markbreaklist(n->rlist, implicit); |
| 3522 | break; |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | static void |
| 3527 | markbreaklist(NodeList *l, Node *implicit) |
| 3528 | { |
| 3529 | Node *n; |
| 3530 | Label *lab; |
| 3531 | |
| 3532 | for(; l; l=l->next) { |
| 3533 | n = l->n; |
| 3534 | if(n->op == OLABEL && l->next && n->defn == l->next->n) { |
| 3535 | switch(n->defn->op) { |
| 3536 | case OFOR: |
| 3537 | case OSWITCH: |
| 3538 | case OTYPESW: |
| 3539 | case OSELECT: |
| 3540 | case ORANGE: |
| 3541 | lab = mal(sizeof *lab); |
| 3542 | lab->def = n->defn; |
| 3543 | n->left->sym->label = lab; |
| 3544 | markbreak(n->defn, n->defn); |
| 3545 | n->left->sym->label = L; |
| 3546 | l = l->next; |
| 3547 | continue; |
| 3548 | } |
| 3549 | } |
| 3550 | markbreak(n, implicit); |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | static int |
| 3555 | isterminating(NodeList *l, int top) |
| 3556 | { |
| 3557 | int def; |
| 3558 | Node *n; |
| 3559 | |
| 3560 | if(l == nil) |
| 3561 | return 0; |
| 3562 | if(top) { |
| 3563 | while(l->next && l->n->op != OLABEL) |
| 3564 | l = l->next; |
| 3565 | markbreaklist(l, nil); |
| 3566 | } |
| 3567 | while(l->next) |
| 3568 | l = l->next; |
| 3569 | n = l->n; |
| 3570 | |
| 3571 | if(n == N) |
| 3572 | return 0; |
| 3573 | |
| 3574 | switch(n->op) { |
| 3575 | // NOTE: OLABEL is treated as a separate statement, |
| 3576 | // not a separate prefix, so skipping to the last statement |
| 3577 | // in the block handles the labeled statement case by |
| 3578 | // skipping over the label. No case OLABEL here. |
| 3579 | |
| 3580 | case OBLOCK: |
| 3581 | return isterminating(n->list, 0); |
| 3582 | |
| 3583 | case OGOTO: |
| 3584 | case ORETURN: |
Russ Cox | 1f51d27 | 2013-06-11 09:41:49 -0400 | [diff] [blame] | 3585 | case ORETJMP: |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 3586 | case OPANIC: |
| 3587 | case OXFALL: |
| 3588 | return 1; |
| 3589 | |
| 3590 | case OFOR: |
| 3591 | if(n->ntest != N) |
| 3592 | return 0; |
| 3593 | if(n->hasbreak) |
| 3594 | return 0; |
| 3595 | return 1; |
| 3596 | |
| 3597 | case OIF: |
| 3598 | return isterminating(n->nbody, 0) && isterminating(n->nelse, 0); |
| 3599 | |
| 3600 | case OSWITCH: |
| 3601 | case OTYPESW: |
| 3602 | case OSELECT: |
| 3603 | if(n->hasbreak) |
| 3604 | return 0; |
| 3605 | def = 0; |
| 3606 | for(l=n->list; l; l=l->next) { |
| 3607 | if(!isterminating(l->n->nbody, 0)) |
| 3608 | return 0; |
| 3609 | if(l->n->list == nil) // default |
| 3610 | def = 1; |
| 3611 | } |
| 3612 | if(n->op != OSELECT && !def) |
| 3613 | return 0; |
| 3614 | return 1; |
| 3615 | } |
| 3616 | |
| 3617 | return 0; |
| 3618 | } |
| 3619 | |
| 3620 | void |
| 3621 | checkreturn(Node *fn) |
| 3622 | { |
| 3623 | if(fn->type->outtuple && fn->nbody != nil) |
| 3624 | if(!isterminating(fn->nbody, 1)) |
| 3625 | yyerrorl(fn->endlineno, "missing return at end of function"); |
| 3626 | } |