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.
21 * declaration stack & operations
30 a->package = b->package;
32 a->vargen = b->vargen;
34 a->lastlineno = b->lastlineno;
35 a->offset = b->offset;
67 for(d=dclstack; d!=S; d=d->link) {
70 s = pkglookup(d->name, d->package);
73 print("\t%L pop %S\n", lineno, s);
76 fatal("popdcl: no mark");
86 for(d=dclstack; d!=S; d=d->link) {
89 s = pkglookup(d->name, d->package);
92 print("\t%L pop %S\n", lineno, s);
95 fatal("poptodcl: no mark");
105 d->name = nil; // used as a mark in fifo
112 // print("markdcl\n");
122 for(d=dclstack; d!=S; d=d->link) {
124 print(" %.2d %p", i, d);
129 print(" '%s'", d->name);
130 s = pkglookup(d->name, d->package);
140 for(d=dclstack; d!=S; d=d->link) {
142 yyerror("mark left on the stack");
149 * declare individual names - var, typ, const
152 redeclare(char *str, Sym *s)
154 if(s->block == block) {
155 yyerror("%s %S redeclared in this block", str, s);
156 print(" previous declaration at %L\n", s->lastlineno);
159 s->lastlineno = lineno;
163 addvar(Node *n, Type *t, int ctxt)
169 if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
170 fatal("addvar: n=%N t=%T nil", n, t);
174 if(ctxt == PEXTERN || ctxt == PFUNC) {
184 redeclare("variable", s);
190 n->funcdepth = funcdepth;
205 print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
206 else if(ctxt == PFUNC)
207 print("extern func-dcl %S G%ld %T\n", s, s->vargen, t);
209 print("auto var-dcl %S G%ld %T\n", s, s->vargen, t);
214 addtyp(Type *n, int ctxt)
220 if(n==T || n->sym == S)
221 fatal("addtyp: n=%T t=%T nil", n);
230 n->vargen = ++typgen;
233 redeclare("type", s);
256 print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
258 print("auto typ-dcl %S G%ld %T\n", s, s->vargen, n);
264 addconst(Node *n, Node *e, int ctxt)
269 if(n->op != ONAME && n->op != ONONAME)
270 fatal("addconst: not a name");
272 if(e->op != OLITERAL) {
273 yyerror("expression must be a constant");
286 redeclare("constant", s);
299 print("const-dcl %S %N\n", n->sym, n->sym->def);
303 * declare (possible list) n of type t.
304 * append ODCL nodes to *init
307 dodclvar(Node *n, Type *t, NodeList **init)
312 if(t != T && (t->etype == TIDEAL || t->etype == TNIL))
313 fatal("dodclvar %T", t);
316 // in case of type checking error,
317 // use "undefined" type for variable type,
318 // to avoid fatal in addvar.
322 addvar(n, t, dclcontext);
325 *init = list(*init, nod(ODCL, n, N));
330 dodclconst(Node *n, Node *e)
334 addconst(n, e, dclcontext);
339 * introduce a type named n
340 * but it is an unknown type for now
347 // if n has been forward declared,
348 // use the Type* created then
350 if((funcdepth == 0 || s->block == block) && s->def != N && s->def->op == OTYPE) {
351 switch(s->def->type->etype) {
355 if(s->block != block) {
356 // completing forward struct from other file
371 // otherwise declare a new type
372 addtyp(n, dclcontext);
381 * now we know what n is: it's t
384 updatetype(Type *n, Type *t)
388 int maplineno, lno, etype;
393 if(s == S || s->def == N || s->def->op != OTYPE || s->def->type != n)
394 fatal("updatetype %T = %T", n, t);
402 if(t->etype != TSTRUCT) {
403 yyerror("%T forward declared as struct", n);
410 if(t->etype != TINTER) {
411 yyerror("%T forward declared as interface", n);
417 fatal("updatetype %T / %T", n, t);
422 // copy t, but then zero out state associated with t
423 // that is no longer associated with n.
424 maplineno = n->maplineno;
436 // catch declaration of incomplete type
445 // double-check use of type as map key
449 maptype(n, types[TBOOL]);
455 * declare variables from grammar
456 * new_name_list (type | [type] = expr_list)
459 variter(NodeList *vl, Node *t, NodeList *el)
469 for(; vl; vl=vl->next) {
472 yyerror("missing expr in var dcl");
482 if(dclcontext == PEXTERN || dclcontext == PFUNC) {
491 redeclare("variable", s);
498 v->class = dclcontext;
500 v->funcdepth = funcdepth;
502 if(e != N || funcdepth > 0) {
504 init = list(init, nod(ODCL, v, N));
506 init = list(init, e);
521 yyerror("extra expr in var dcl");
526 * declare constants from grammar
527 * new_name_list [[type] = expr_list]
530 constiter(NodeList *vl, Node *t, NodeList *cl)
539 yyerror("constdcl cannot have type without expr");
546 cl = listtreecopy(cl);
548 for(; vl; vl=vl->next) {
550 yyerror("missing expr in const dcl");
558 if(dclcontext != PEXTERN)
560 redeclare("constant", s);
569 yyerror("extra expr in const dcl");
575 * this generates a new name that is
576 * pushed down on the declaration list.
577 * no diagnostics are produced as this
578 * name will soon be declared.
585 n = nod(ONAME, N, N);
599 // top-level name: might already have been
600 // referred to, in which case s->def is already
601 // set to an ONONAME.
602 if(dclcontext == PEXTERN && s->block == 0) {
603 // toss predefined name like "close"
604 // TODO(rsc): put close in at the end.
605 if(s->def != N && s->def->etype)
613 n->op = ONONAME; // caller will correct it
621 t->nod = nod(OTYPE, N, N);
623 t->nod->sym = t->sym;
630 * this will return an old name
631 * that has already been pushed on the
632 * declaration list. a diagnostic is
633 * generated if no name has been defined.
643 // maybe a top-level name will come along
644 // to give this a definition later.
649 if(n->funcdepth > 0 && n->funcdepth != funcdepth && n->op == ONAME) {
650 // inner func is referring to var
652 if(n->closure == N || n->closure->funcdepth != funcdepth) {
654 // create new closure var.
655 c = nod(ONAME, N, N);
657 c->class = PPARAMREF;
661 c->funcdepth = funcdepth;
662 c->outer = n->closure;
666 funclit->cvars = list(funclit->cvars, c);
668 // return ref to closure var, not original
695 if(s->def == N || s->def->op != OTYPE) {
697 yyerror("%S is not a type", s);
703 * If t is lowercase and not in our package
704 * and this isn't a reference during the parsing
705 * of import data, complain.
707 if(pkgimportname == S && !exportname(s->name) && strcmp(s->package, package) != 0)
708 yyerror("cannot use type %T", t);
713 * type check top level declarations
720 for(d=externdcl; d!=D; d=d->forw) {
723 typecheck(&d->dnode, Erv);
729 * structs, functions, and methods.
730 * they don't belong here, but where do they belong?
735 * turn a parsed struct into a type
738 stotype(NodeList *l, int et, Type **t)
748 for(; l; l=l->next) {
753 if(n->op != ODCLFIELD)
754 fatal("stotype: oops %N\n", n);
756 typecheck(&n->right, Etype);
757 n->type = n->right->type;
759 if(n->embedded && n->type != T) {
761 if(t1->sym == S && isptr[t1->etype])
763 if(t1 != T && isptr[t1->etype])
764 yyerror("embedded type cannot be a pointer");
769 // assume error already printed
773 switch(n->val.ctype) {
776 yyerror("interface method cannot have annotation");
777 note = n->val.u.sval;
781 yyerror("interface method cannot have annotation");
783 yyerror("field annotation must be string");
789 if(et == TINTER && n->left == N) {
790 // embedded interface - inline the methods
791 if(n->type->etype != TINTER) {
792 yyerror("interface contains embedded non-interface %T", t);
795 for(t1=n->type->type; t1!=T; t1=t1->down) {
796 // TODO(rsc): Is this really an error?
797 if(strcmp(t1->sym->package, package) != 0)
798 yyerror("embedded interface contains unexported method %S", t1->sym);
802 f->nname = newname(t1->sym);
815 if(n->left != N && n->left->op == ONAME) {
817 f->embedded = n->embedded;
818 f->sym = f->nname->sym;
819 if(pkgimportname != S && !exportname(f->sym->name))
820 f->sym = pkglookup(f->sym->name, structpkg);
833 dostruct(NodeList *l, int et)
839 * convert a parsed id/type list into
840 * a type for struct/interface/arglist
850 stotype(l, et, &t->type);
863 // Names sometimes have disambiguation junk
864 // appended after a center dot. Discard it when
865 // making the name for the embedded struct field.
866 enum { CenterDot = 0xB7 };
868 if(utfrune(s->name, CenterDot)) {
869 name = strdup(s->name);
870 *utfrune(name, CenterDot) = 0;
873 n = newname(lookup(name));
874 n = nod(ODCLFIELD, n, N);
878 n->right = oldname(s);
883 findtype(NodeList *l)
897 typecheck(&nt, Etype);
899 if(nt->op != OTYPE) {
900 yyerror("%S is not a type", nt->sym);
903 n = nod(ODCLFIELD, N, N);
909 namedcl(Node *nn, Node *nt)
917 typecheck(&nn, Etype);
918 yyerror("cannot mix anonymous %T with named arguments", nn->type);
923 yyerror("missing type for argument %S", nn->sym);
925 typecheck(&nt, Etype);
927 yyerror("%S is not a type", nt->sym);
931 n = nod(ODCLFIELD, newname(nn->sym), N);
937 * check that the list of declarations is either all anonymous or all named
940 checkarglist(NodeList *all)
947 for(l=all; l; l=l->next) {
948 if(l->n->op == OKEY) {
954 for(l=all; l; l=l->next) {
956 l->n = namedcl(l->n, findtype(l));
958 l->n = xanondcl(l->n);
961 if(r != N && r->type != T && r->type->etype == TDDD)
962 yyerror("only last argument can have type ...");
974 n = nod(ODCLFIELD, N, N);
975 n->type = ptrto(typ(TSTRUCT));
980 * Is this field a method on an interface?
981 * Those methods have an anonymous
982 * *struct{} as the receiver.
983 * (See fakethis above.)
986 isifacemethod(Type *f)
991 rcvr = getthisx(f->type)->type;
998 if(t->sym != S || t->etype != TSTRUCT || t->type != T)
1004 * turn a parsed function declaration
1008 functype(Node *this, NodeList *in, NodeList *out)
1018 t->type = dostruct(rcvr, TFUNC);
1019 t->type->down = dostruct(out, TFUNC);
1020 t->type->down->down = dostruct(in, TFUNC);
1024 t->outtuple = count(out);
1025 t->intuple = count(in);
1032 methcmp(Type *t1, Type *t2)
1034 if(t1->etype != TFUNC)
1036 if(t2->etype != TFUNC)
1039 t1 = t1->type->down; // skip this arg
1040 t2 = t2->type->down; // skip this arg
1044 if(t1 == T || t2 == T)
1046 if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
1049 if(!eqtype(t1->type, t2->type))
1059 methodsym(Sym *nsym, Type *t0)
1070 if(!isptr[t->etype])
1080 // if t0 == *t and t0 has a sym,
1081 // we want to see *t, not t0, in the method name.
1082 if(t != t0 && t0->sym)
1085 snprint(buf, sizeof(buf), "%#hTĀ·%s", t0, nsym->name);
1086 //print("methodname %s\n", buf);
1087 return pkglookup(buf, s->package);
1090 yyerror("illegal <this> type: %T", t);
1095 methodname(Node *n, Type *t)
1099 s = methodsym(n->sym, t);
1106 * add a method, declared as a function,
1107 * n is fieldname, pa is base type, t is function type
1110 addmethod(Node *n, Type *t, int local)
1127 // get parent type sym
1128 pa = *getthis(t); // ptr to this structure
1131 pa = pa->type; // ptr to this field
1134 pa = pa->type; // ptr to this type
1143 if(pkgimportname != S && !exportname(sf->name))
1144 sf = pkglookup(sf->name, pkgimportname->name);
1146 n = nod(ODCLFIELD, newname(sf), N);
1149 d = T; // last found
1150 for(f=pa->method; f!=T; f=f->down) {
1152 if(f->etype != TFIELD)
1153 fatal("addmethod: not TFIELD: %N", f);
1154 if(strcmp(sf->name, f->sym->name) != 0)
1156 if(!eqtype(t, f->type)) {
1157 yyerror("method redeclared: %T.%S", pa, sf);
1158 print("\t%T\n\t%T\n", f->type, t);
1163 if(local && !pa->local) {
1164 // defining method on non-local type.
1165 // method must have been forward declared
1166 // elsewhere, i.e. where the type was.
1167 yyerror("cannot define new methods on non-local type %T", pa);
1172 stotype(list1(n), 0, &pa->method);
1174 stotype(list1(n), 0, &d->down);
1178 yyerror("invalid receiver type %T", pa);
1182 * declare the function proper.
1183 * and declare the arguments
1184 * called in extern-declaration context
1185 * returns in auto-declaration context.
1195 if(on != N && (on->op != ONAME || on->builtin))
1198 // check for same types
1200 if(eqtype(n->type, on->type)) {
1201 if(!eqargs(n->type, on->type)) {
1202 yyerror("function arg names changed: %S", s);
1203 print("\t%T\n\t%T\n", on->type, n->type);
1206 yyerror("function redeclared: %S", s);
1207 print("\t%T\n\t%T\n", on->type, n->type);
1212 // check for forward declaration
1214 // initial declaration or redeclaration
1215 // declare fun name, argument types and argument names
1216 n->nname->type = n->type;
1217 if(n->type->thistuple == 0)
1218 addvar(n->nname, n->type, PFUNC);
1220 n->nname->class = PFUNC;
1222 // identical redeclaration
1223 // steal previous names
1226 n->class = on->class;
1230 // change the declaration context from extern to auto
1232 autodcl->back = autodcl;
1234 if(funcdepth == 0 && dclcontext != PEXTERN)
1235 fatal("funchdr: dclcontext");
1251 // declare the this/in arguments
1252 t = funcfirst(&save, ft);
1255 t->nname->xoffset = t->width;
1256 addvar(t->nname, t->type, PPARAM);
1258 t = funcnext(&save);
1261 // declare the outgoing arguments
1263 t = structfirst(&save, getoutarg(ft));
1266 t->nname->xoffset = t->width;
1268 addvar(t->nname, t->type, PPARAMOUT);
1272 t = structnext(&save);
1275 // this test is remarkedly similar to checkarglist
1277 yyerror("cannot mix anonymous and named output arguments");
1285 * compile the function.
1286 * called in auto-declaration context.
1287 * returns in extern-declaration context.
1295 // change the declaration context from auto to extern
1296 if(dclcontext != PAUTO)
1297 fatal("funcbody: dclcontext");
1301 dclcontext = PEXTERN;
1309 n = nod(OXXX, N, N);
1314 // new declaration context
1316 autodcl->back = autodcl;
1318 typecheck(&t, Etype);
1324 funclit1(Node *ntype, NodeList *body)
1328 Node *a, *d, *f, *n, *clos;
1332 NodeList *args, *l, *in, *out;
1337 funclit = func->outer;
1339 // build up type of func f that we're going to compile.
1340 // as we referred to variables from the outer function,
1341 // we accumulated a list of PHEAP names in func->cvars.
1343 // add PHEAP versions as function arguments.
1345 for(l=func->cvars; l; l=l->next) {
1347 d = nod(ODCLFIELD, a, N);
1348 d->type = ptrto(a->type);
1351 // while we're here, set up a->heapaddr for back end
1352 n = nod(ONAME, N, N);
1353 snprint(namebuf, sizeof namebuf, "&%s", a->sym->name);
1354 n->sym = lookup(namebuf);
1355 n->type = ptrto(a->type);
1357 n->xoffset = narg*types[tptr]->width;
1365 // unlink from actual ONAME in symbol table
1366 a->closure->closure = a->outer;
1369 // add a dummy arg for the closure's caller pc
1370 d = nod(ODCLFIELD, N, N);
1371 d->type = types[TUINTPTR];
1374 // slide param offset to make room for ptrs above.
1375 // narg+1 to skip over caller pc.
1376 shift = (narg+1)*types[tptr]->width;
1378 // now the original arguments.
1379 for(t=structfirst(&save, getinarg(type)); t; t=structnext(&save)) {
1380 d = nod(ODCLFIELD, t->nname, N);
1386 if(a->stackparam != N)
1388 a->xoffset += shift;
1394 for(t=structfirst(&save, getoutarg(type)); t; t=structnext(&save)) {
1395 d = nod(ODCLFIELD, t->nname, N);
1401 if(a->stackparam != N)
1403 a->xoffset += shift;
1407 ft = functype(N, in, out);
1408 ft->outnamed = type->outnamed;
1410 // declare function.
1412 snprint(namebuf, sizeof(namebuf), "_f%.3ldĀ·%s", vargen, filename);
1413 f = newname(lookup(namebuf));
1414 addvar(f, ft, PFUNC);
1418 n = nod(ODCLFUNC, N, N);
1422 body = list1(nod(OEMPTY, N, N));
1426 autodcl = func->dcl;
1428 // build up type for this instance of the closure func.
1430 d = nod(ODCLFIELD, N, N); // siz
1431 d->type = types[TINT];
1433 d = nod(ODCLFIELD, N, N); // f
1436 for(l=func->cvars; l; l=l->next) {
1438 d = nod(ODCLFIELD, N, N); // arg
1439 d->type = ptrto(a->type);
1443 d = nod(ODCLFIELD, N, N);
1447 clos = syslook("closure", 1);
1448 clos->type = functype(N, in, out);
1450 // literal expression is sys.closure(siz, f, arg0, arg1, ...)
1451 // which builds a function that calls f after filling in arg0,
1452 // arg1, ... for the PHEAP arguments above.
1454 if(narg*widthptr > 100)
1455 yyerror("closure needs too many variables; runtime will reject it");
1456 a = nodintconst(narg*widthptr);
1457 args = list(args, a); // siz
1458 args = list(args, f); // f
1459 for(l=func->cvars; l; l=l->next) {
1461 d = oldname(a->sym);
1462 args = list(args, nod(OADDR, d, N));
1464 typechecklist(args, Erv);
1466 n = nod(OCALL, clos, N);