github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/cmd/gc/range.c (about) 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 5 /* 6 * range 7 */ 8 9 #include <u.h> 10 #include <libc.h> 11 #include "go.h" 12 13 void 14 typecheckrange(Node *n) 15 { 16 char *why; 17 Type *t, *t1, *t2; 18 Node *v1, *v2; 19 NodeList *ll; 20 21 // delicate little dance. see typecheckas2 22 for(ll=n->list; ll; ll=ll->next) 23 if(ll->n->defn != n) 24 typecheck(&ll->n, Erv | Easgn); 25 26 typecheck(&n->right, Erv); 27 if((t = n->right->type) == T) 28 goto out; 29 if(isptr[t->etype] && isfixedarray(t->type)) 30 t = t->type; 31 n->type = t; 32 33 switch(t->etype) { 34 default: 35 yyerror("cannot range over %lN", n->right); 36 goto out; 37 38 case TARRAY: 39 t1 = types[TINT]; 40 t2 = t->type; 41 break; 42 43 case TMAP: 44 t1 = t->down; 45 t2 = t->type; 46 break; 47 48 case TCHAN: 49 if(!(t->chan & Crecv)) { 50 yyerror("invalid operation: range %N (receive from send-only type %T)", n->right, n->right->type); 51 goto out; 52 } 53 t1 = t->type; 54 t2 = nil; 55 if(count(n->list) == 2) 56 goto toomany; 57 break; 58 59 case TSTRING: 60 t1 = types[TINT]; 61 t2 = runetype; 62 break; 63 } 64 65 if(count(n->list) > 2) { 66 toomany: 67 yyerror("too many variables in range"); 68 } 69 70 v1 = N; 71 if(n->list) 72 v1 = n->list->n; 73 v2 = N; 74 if(n->list && n->list->next) 75 v2 = n->list->next->n; 76 77 // this is not only a optimization but also a requirement in the spec. 78 // "if the second iteration variable is the blank identifier, the range 79 // clause is equivalent to the same clause with only the first variable 80 // present." 81 if(isblank(v2)) { 82 if(v1 != N) 83 n->list = list1(v1); 84 v2 = N; 85 } 86 87 if(v1) { 88 if(v1->defn == n) 89 v1->type = t1; 90 else if(v1->type != T && assignop(t1, v1->type, &why) == 0) 91 yyerror("cannot assign type %T to %lN in range%s", t1, v1, why); 92 } 93 if(v2) { 94 if(v2->defn == n) 95 v2->type = t2; 96 else if(v2->type != T && assignop(t2, v2->type, &why) == 0) 97 yyerror("cannot assign type %T to %lN in range%s", t2, v2, why); 98 } 99 100 out: 101 typechecklist(n->nbody, Etop); 102 103 // second half of dance 104 n->typecheck = 1; 105 for(ll=n->list; ll; ll=ll->next) 106 if(ll->n->typecheck == 0) 107 typecheck(&ll->n, Erv | Easgn); 108 } 109 110 void 111 walkrange(Node *n) 112 { 113 Node *ohv1, *hv1, *hv2; // hidden (old) val 1, 2 114 Node *ha, *hit; // hidden aggregate, iterator 115 Node *hn, *hp; // hidden len, pointer 116 Node *hb; // hidden bool 117 Node *a, *v1, *v2; // not hidden aggregate, val 1, 2 118 Node *fn, *tmp; 119 Node *keyname, *valname; 120 Node *key, *val; 121 NodeList *body, *init; 122 Type *th, *t; 123 int lno; 124 125 t = n->type; 126 init = nil; 127 128 a = n->right; 129 lno = setlineno(a); 130 131 v1 = N; 132 if(n->list) 133 v1 = n->list->n; 134 v2 = N; 135 if(n->list && n->list->next && !isblank(n->list->next->n)) 136 v2 = n->list->next->n; 137 // n->list has no meaning anymore, clear it 138 // to avoid erroneous processing by racewalk. 139 n->list = nil; 140 hv2 = N; 141 142 switch(t->etype) { 143 default: 144 fatal("walkrange"); 145 146 case TARRAY: 147 // orderstmt arranged for a copy of the array/slice variable if needed. 148 ha = a; 149 hv1 = temp(types[TINT]); 150 hn = temp(types[TINT]); 151 hp = nil; 152 153 init = list(init, nod(OAS, hv1, N)); 154 init = list(init, nod(OAS, hn, nod(OLEN, ha, N))); 155 if(v2) { 156 hp = temp(ptrto(n->type->type)); 157 tmp = nod(OINDEX, ha, nodintconst(0)); 158 tmp->bounded = 1; 159 init = list(init, nod(OAS, hp, nod(OADDR, tmp, N))); 160 } 161 162 n->ntest = nod(OLT, hv1, hn); 163 n->nincr = nod(OAS, hv1, nod(OADD, hv1, nodintconst(1))); 164 if(v1 == N) 165 body = nil; 166 else if(v2 == N) 167 body = list1(nod(OAS, v1, hv1)); 168 else { 169 a = nod(OAS2, N, N); 170 a->list = list(list1(v1), v2); 171 a->rlist = list(list1(hv1), nod(OIND, hp, N)); 172 body = list1(a); 173 174 // Advance pointer as part of increment. 175 // We used to advance the pointer before executing the loop body, 176 // but doing so would make the pointer point past the end of the 177 // array during the final iteration, possibly causing another unrelated 178 // piece of memory not to be garbage collected until the loop finished. 179 // Advancing during the increment ensures that the pointer p only points 180 // pass the end of the array during the final "p++; i++; if(i >= len(x)) break;", 181 // after which p is dead, so it cannot confuse the collector. 182 tmp = nod(OADD, hp, nodintconst(t->type->width)); 183 tmp->type = hp->type; 184 tmp->typecheck = 1; 185 tmp->right->type = types[tptr]; 186 tmp->right->typecheck = 1; 187 a = nod(OAS, hp, tmp); 188 typecheck(&a, Etop); 189 n->nincr->ninit = list1(a); 190 } 191 break; 192 193 case TMAP: 194 // orderstmt allocated the iterator for us. 195 // we only use a once, so no copy needed. 196 ha = a; 197 th = hiter(t); 198 hit = n->alloc; 199 hit->type = th; 200 n->left = N; 201 keyname = newname(th->type->sym); // depends on layout of iterator struct. See reflect.c:hiter 202 valname = newname(th->type->down->sym); // ditto 203 204 fn = syslook("mapiterinit", 1); 205 argtype(fn, t->down); 206 argtype(fn, t->type); 207 argtype(fn, th); 208 init = list(init, mkcall1(fn, T, nil, typename(t), ha, nod(OADDR, hit, N))); 209 n->ntest = nod(ONE, nod(ODOT, hit, keyname), nodnil()); 210 211 fn = syslook("mapiternext", 1); 212 argtype(fn, th); 213 n->nincr = mkcall1(fn, T, nil, nod(OADDR, hit, N)); 214 215 key = nod(ODOT, hit, keyname); 216 key = nod(OIND, key, N); 217 if(v1 == N) 218 body = nil; 219 else if(v2 == N) { 220 body = list1(nod(OAS, v1, key)); 221 } else { 222 val = nod(ODOT, hit, valname); 223 val = nod(OIND, val, N); 224 a = nod(OAS2, N, N); 225 a->list = list(list1(v1), v2); 226 a->rlist = list(list1(key), val); 227 body = list1(a); 228 } 229 break; 230 231 case TCHAN: 232 // orderstmt arranged for a copy of the channel variable. 233 ha = a; 234 n->ntest = N; 235 236 hv1 = temp(t->type); 237 hv1->typecheck = 1; 238 if(haspointers(t->type)) 239 init = list(init, nod(OAS, hv1, N)); 240 hb = temp(types[TBOOL]); 241 242 n->ntest = nod(ONE, hb, nodbool(0)); 243 a = nod(OAS2RECV, N, N); 244 a->typecheck = 1; 245 a->list = list(list1(hv1), hb); 246 a->rlist = list1(nod(ORECV, ha, N)); 247 n->ntest->ninit = list1(a); 248 if(v1 == N) 249 body = nil; 250 else 251 body = list1(nod(OAS, v1, hv1)); 252 break; 253 254 case TSTRING: 255 // orderstmt arranged for a copy of the string variable. 256 ha = a; 257 258 ohv1 = temp(types[TINT]); 259 260 hv1 = temp(types[TINT]); 261 init = list(init, nod(OAS, hv1, N)); 262 263 if(v2 == N) 264 a = nod(OAS, hv1, mkcall("stringiter", types[TINT], nil, ha, hv1)); 265 else { 266 hv2 = temp(runetype); 267 a = nod(OAS2, N, N); 268 a->list = list(list1(hv1), hv2); 269 fn = syslook("stringiter2", 0); 270 a->rlist = list1(mkcall1(fn, getoutargx(fn->type), nil, ha, hv1)); 271 } 272 n->ntest = nod(ONE, hv1, nodintconst(0)); 273 n->ntest->ninit = list(list1(nod(OAS, ohv1, hv1)), a); 274 275 276 body = nil; 277 if(v1 != N) 278 body = list1(nod(OAS, v1, ohv1)); 279 if(v2 != N) 280 body = list(body, nod(OAS, v2, hv2)); 281 break; 282 } 283 284 n->op = OFOR; 285 typechecklist(init, Etop); 286 n->ninit = concat(n->ninit, init); 287 typechecklist(n->ntest->ninit, Etop); 288 typecheck(&n->ntest, Erv); 289 typecheck(&n->nincr, Etop); 290 typechecklist(body, Etop); 291 n->nbody = concat(body, n->nbody); 292 walkstmt(&n); 293 294 lineno = lno; 295 } 296