github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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->list->n;
    71  	v2 = N;
    72  	if(n->list->next)
    73  		v2 = n->list->next->n;
    74  
    75  	// this is not only a optimization but also a requirement in the spec.
    76  	// "if the second iteration variable is the blank identifier, the range
    77  	// clause is equivalent to the same clause with only the first variable
    78  	// present."
    79  	if(isblank(v2)) {
    80  		n->list = list1(v1);
    81  		v2 = N;
    82  	}
    83  
    84  	if(v1->defn == n)
    85  		v1->type = t1;
    86  	else if(v1->type != T && assignop(t1, v1->type, &why) == 0)
    87  		yyerror("cannot assign type %T to %lN in range%s", t1, v1, why);
    88  	if(v2) {
    89  		if(v2->defn == n)
    90  			v2->type = t2;
    91  		else if(v2->type != T && assignop(t2, v2->type, &why) == 0)
    92  			yyerror("cannot assign type %T to %lN in range%s", t2, v2, why);
    93  	}
    94  
    95  out:
    96  	typechecklist(n->nbody, Etop);
    97  
    98  	// second half of dance
    99  	n->typecheck = 1;
   100  	for(ll=n->list; ll; ll=ll->next)
   101  		if(ll->n->typecheck == 0)
   102  			typecheck(&ll->n, Erv | Easgn);
   103  }
   104  
   105  void
   106  walkrange(Node *n)
   107  {
   108  	Node *ohv1, *hv1, *hv2;	// hidden (old) val 1, 2
   109  	Node *ha, *hit;	// hidden aggregate, iterator
   110  	Node *hn, *hp;	// hidden len, pointer
   111  	Node *hb;  // hidden bool
   112  	Node *a, *v1, *v2;	// not hidden aggregate, val 1, 2
   113  	Node *fn, *tmp;
   114  	NodeList *body, *init;
   115  	Type *th, *t;
   116  	int lno;
   117  
   118  	t = n->type;
   119  	init = nil;
   120  
   121  	a = n->right;
   122  	lno = setlineno(a);
   123  	if(t->etype == TSTRING && !eqtype(t, types[TSTRING])) {
   124  		a = nod(OCONV, n->right, N);
   125  		a->type = types[TSTRING];
   126  	}
   127  
   128  	v1 = n->list->n;
   129  	v2 = N;
   130  	if(n->list->next)
   131  		v2 = n->list->next->n;
   132  	hv2 = N;
   133  
   134  	if(v2 == N && t->etype == TARRAY) {
   135  		// will have just one reference to argument.
   136  		// no need to make a potentially expensive copy.
   137  		ha = a;
   138  	} else {
   139  		ha = temp(a->type);
   140  		init = list(init, nod(OAS, ha, a));
   141  	}
   142  
   143  	switch(t->etype) {
   144  	default:
   145  		fatal("walkrange");
   146  
   147  	case TARRAY:
   148  		hv1 = temp(types[TINT]);
   149  		hn = temp(types[TINT]);
   150  		hp = nil;
   151  
   152  		init = list(init, nod(OAS, hv1, N));
   153  		init = list(init, nod(OAS, hn, nod(OLEN, ha, N)));
   154  		if(v2) {
   155  			hp = temp(ptrto(n->type->type));
   156  			tmp = nod(OINDEX, ha, nodintconst(0));
   157  			tmp->bounded = 1;
   158  			init = list(init, nod(OAS, hp, nod(OADDR, tmp, N)));
   159  		}
   160  
   161  		n->ntest = nod(OLT, hv1, hn);
   162  		n->nincr = nod(OASOP, hv1, nodintconst(1));
   163  		n->nincr->etype = OADD;
   164  		if(v2 == N)
   165  			body = list1(nod(OAS, v1, hv1));
   166  		else {
   167  			a = nod(OAS2, N, N);
   168  			a->list = list(list1(v1), v2);
   169  			a->rlist = list(list1(hv1), nod(OIND, hp, N));
   170  			body = list1(a);
   171  
   172  			tmp = nod(OADD, hp, nodintconst(t->type->width));
   173  			tmp->type = hp->type;
   174  			tmp->typecheck = 1;
   175  			tmp->right->type = types[tptr];
   176  			tmp->right->typecheck = 1;
   177  			body = list(body, nod(OAS, hp, tmp));
   178  		}
   179  		break;
   180  
   181  	case TMAP:
   182  		th = typ(TARRAY);
   183  		th->type = ptrto(types[TUINT8]);
   184  		// see ../../pkg/runtime/hashmap.c:/hash_iter
   185  		// Size of hash_iter in # of pointers.
   186  		th->bound = 11;
   187  		hit = temp(th);
   188  
   189  		fn = syslook("mapiterinit", 1);
   190  		argtype(fn, t->down);
   191  		argtype(fn, t->type);
   192  		argtype(fn, th);
   193  		init = list(init, mkcall1(fn, T, nil, typename(t), ha, nod(OADDR, hit, N)));
   194  		n->ntest = nod(ONE, nod(OINDEX, hit, nodintconst(0)), nodnil());
   195  
   196  		fn = syslook("mapiternext", 1);
   197  		argtype(fn, th);
   198  		n->nincr = mkcall1(fn, T, nil, nod(OADDR, hit, N));
   199  
   200  		if(v2 == N) {
   201  			fn = syslook("mapiter1", 1);
   202  			argtype(fn, th);
   203  			argtype(fn, t->down);
   204  			a = nod(OAS, v1, mkcall1(fn, t->down, nil, nod(OADDR, hit, N)));
   205  		} else {
   206  			fn = syslook("mapiter2", 1);
   207  			argtype(fn, th);
   208  			argtype(fn, t->down);
   209  			argtype(fn, t->type);
   210  			a = nod(OAS2, N, N);
   211  			a->list = list(list1(v1), v2);
   212  			a->rlist = list1(mkcall1(fn, getoutargx(fn->type), nil, nod(OADDR, hit, N)));
   213  		}
   214  		body = list1(a);
   215  		break;
   216  
   217  	case TCHAN:
   218  		hv1 = temp(t->type);
   219  		hb = temp(types[TBOOL]);
   220  
   221  		n->ntest = nod(ONE, hb, nodbool(0));
   222  		a = nod(OAS2RECV, N, N);
   223  		a->typecheck = 1;
   224  		a->list = list(list1(hv1), hb);
   225  		a->rlist = list1(nod(ORECV, ha, N));
   226  		n->ntest->ninit = list1(a);
   227  		body = list1(nod(OAS, v1, hv1));
   228  		break;
   229  
   230  	case TSTRING:
   231  		ohv1 = temp(types[TINT]);
   232  
   233  		hv1 = temp(types[TINT]);
   234  		init = list(init, nod(OAS, hv1, N));
   235  
   236  		if(v2 == N)
   237  			a = nod(OAS, hv1, mkcall("stringiter", types[TINT], nil, ha, hv1));
   238  		else {
   239  			hv2 = temp(runetype);
   240  			a = nod(OAS2, N, N);
   241  			a->list = list(list1(hv1), hv2);
   242  			fn = syslook("stringiter2", 0);
   243  			a->rlist = list1(mkcall1(fn, getoutargx(fn->type), nil, ha, hv1));
   244  		}
   245  		n->ntest = nod(ONE, hv1, nodintconst(0));
   246  		n->ntest->ninit = list(list1(nod(OAS, ohv1, hv1)), a);
   247  
   248  		body = list1(nod(OAS, v1, ohv1));
   249  		if(v2 != N)
   250  			body = list(body, nod(OAS, v2, hv2));
   251  		break;
   252  	}
   253  
   254  	n->op = OFOR;
   255  	typechecklist(init, Etop);
   256  	n->ninit = concat(n->ninit, init);
   257  	typechecklist(n->ntest->ninit, Etop);
   258  	typecheck(&n->ntest, Erv);
   259  	typecheck(&n->nincr, Etop);
   260  	typechecklist(body, Etop);
   261  	n->nbody = concat(body, n->nbody);
   262  	walkstmt(&n);
   263  	
   264  	lineno = lno;
   265  }
   266