decls.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. #include "limbo.h"
  2. char *storename[Dend]=
  3. {
  4. /* Dtype */ "type",
  5. /* Dfn */ "function",
  6. /* Dglobal */ "global",
  7. /* Darg */ "argument",
  8. /* Dlocal */ "local",
  9. /* Dconst */ "con",
  10. /* Dfield */ "field",
  11. /* Dtag */ "pick tag",
  12. /* Dimport */ "import",
  13. /* Dunbound */ "unbound",
  14. /* Dundef */ "undefined",
  15. /* Dwundef */ "undefined",
  16. };
  17. char *storeart[Dend] =
  18. {
  19. /* Dtype */ "a ",
  20. /* Dfn */ "a ",
  21. /* Dglobal */ "a ",
  22. /* Darg */ "an ",
  23. /* Dlocal */ "a ",
  24. /* Dconst */ "a ",
  25. /* Dfield */ "a ",
  26. /* Dtag */ "a",
  27. /* Dimport */ "an ",
  28. /* Dunbound */ "",
  29. /* Dundef */ "",
  30. /* Dwundef */ "",
  31. };
  32. int storespace[Dend] =
  33. {
  34. /* Dtype */ 0,
  35. /* Dfn */ 0,
  36. /* Dglobal */ 1,
  37. /* Darg */ 1,
  38. /* Dlocal */ 1,
  39. /* Dconst */ 0,
  40. /* Dfield */ 1,
  41. /* Dtag */ 0,
  42. /* Dimport */ 0,
  43. /* Dunbound */ 0,
  44. /* Dundef */ 0,
  45. /* Dwundef */ 0,
  46. };
  47. static Decl *scopes[MaxScope];
  48. static Decl *tails[MaxScope];
  49. static Node *scopenode[MaxScope];
  50. static uchar scopekind[MaxScope];
  51. static void freeloc(Decl*);
  52. void
  53. popscopes(void)
  54. {
  55. Decl *d;
  56. Dlist *id;
  57. /*
  58. * clear out any decls left in syms
  59. */
  60. while(scope >= ScopeBuiltin){
  61. for(d = scopes[scope--]; d != nil; d = d->next){
  62. if(d->sym != nil){
  63. d->sym->decl = d->old;
  64. d->old = nil;
  65. }
  66. }
  67. }
  68. for(id = impdecls; id != nil; id = id->next){
  69. for(d = id->d->ty->ids; d != nil; d = d->next){
  70. d->sym->decl = nil;
  71. d->old = nil;
  72. }
  73. }
  74. impdecls = nil;
  75. scope = ScopeBuiltin;
  76. scopes[ScopeBuiltin] = nil;
  77. tails[ScopeBuiltin] = nil;
  78. }
  79. void
  80. declstart(void)
  81. {
  82. Decl *d;
  83. iota = mkids(&nosrc, enter("iota", 0), tint, nil);
  84. iota->init = mkconst(&nosrc, 0);
  85. scope = ScopeNils;
  86. scopes[ScopeNils] = nil;
  87. tails[ScopeNils] = nil;
  88. nildecl = mkdecl(&nosrc, Dglobal, tany);
  89. nildecl->sym = enter("nil", 0);
  90. installids(Dglobal, nildecl);
  91. d = mkdecl(&nosrc, Dglobal, tstring);
  92. d->sym = enter("", 0);
  93. installids(Dglobal, d);
  94. scope = ScopeGlobal;
  95. scopes[ScopeGlobal] = nil;
  96. tails[ScopeGlobal] = nil;
  97. }
  98. void
  99. redecl(Decl *d)
  100. {
  101. Decl *old;
  102. old = d->sym->decl;
  103. if(old->store == Dwundef)
  104. return;
  105. error(d->src.start, "redeclaration of %K, previously declared as %k on line %L",
  106. d, old, old->src.start);
  107. }
  108. void
  109. checkrefs(Decl *d)
  110. {
  111. Decl *id, *m;
  112. long refs;
  113. for(; d != nil; d = d->next){
  114. if(d->das)
  115. d->refs--;
  116. switch(d->store){
  117. case Dtype:
  118. refs = d->refs;
  119. if(d->ty->kind == Tadt){
  120. for(id = d->ty->ids; id != nil; id = id->next){
  121. d->refs += id->refs;
  122. if(id->store != Dfn)
  123. continue;
  124. if(id->init == nil && id->link == nil && d->importid == nil)
  125. error(d->src.start, "function %s.%s not defined", d->sym->name, id->sym->name);
  126. if(superwarn && !id->refs && d->importid == nil)
  127. warn(d->src.start, "function %s.%s not referenced", d->sym->name, id->sym->name);
  128. }
  129. }
  130. if(d->ty->kind == Tmodule){
  131. for(id = d->ty->ids; id != nil; id = id->next){
  132. refs += id->refs;
  133. if(id->iface != nil)
  134. id->iface->refs += id->refs;
  135. if(id->store == Dtype){
  136. for(m = id->ty->ids; m != nil; m = m->next){
  137. refs += m->refs;
  138. if(m->iface != nil)
  139. m->iface->refs += m->refs;
  140. }
  141. }
  142. }
  143. d->refs = refs;
  144. }
  145. if(superwarn && !refs && d->importid == nil)
  146. warn(d->src.start, "%K not referenced", d);
  147. break;
  148. case Dglobal:
  149. if(!superwarn)
  150. break;
  151. case Dlocal:
  152. case Darg:
  153. if(!d->refs && d->sym != nil
  154. && d->sym->name != nil && d->sym->name[0] != '.')
  155. warn(d->src.start, "%K not referenced", d);
  156. break;
  157. case Dconst:
  158. if(superwarn && !d->refs && d->sym != nil)
  159. warn(d->src.start, "%K not referenced", d);
  160. if(d->ty == tstring && d->init != nil)
  161. d->init->decl->refs += d->refs;
  162. break;
  163. case Dfn:
  164. if(d->init == nil && d->importid == nil)
  165. error(d->src.start, "%K not defined", d);
  166. if(superwarn && !d->refs)
  167. warn(d->src.start, "%K not referenced", d);
  168. break;
  169. case Dimport:
  170. if(superwarn && !d->refs)
  171. warn(d->src.start, "%K not referenced", d);
  172. break;
  173. }
  174. if(d->das)
  175. d->refs++;
  176. }
  177. }
  178. Node*
  179. vardecl(Decl *ids, Type *t)
  180. {
  181. Node *n;
  182. n = mkn(Ovardecl, mkn(Oseq, nil, nil), nil);
  183. n->decl = ids;
  184. n->ty = t;
  185. return n;
  186. }
  187. void
  188. vardecled(Node *n)
  189. {
  190. Decl *ids, *last;
  191. Type *t;
  192. int store;
  193. store = Dlocal;
  194. if(scope == ScopeGlobal)
  195. store = Dglobal;
  196. if(n->ty->kind == Texception && n->ty->cons){
  197. store = Dconst;
  198. fatal("Texception in vardecled");
  199. }
  200. ids = n->decl;
  201. installids(store, ids);
  202. t = n->ty;
  203. for(last = ids; ids != nil; ids = ids->next){
  204. ids->ty = t;
  205. last = ids;
  206. }
  207. n->left->decl = last;
  208. }
  209. Node*
  210. condecl(Decl *ids, Node *init)
  211. {
  212. Node *n;
  213. n = mkn(Ocondecl, mkn(Oseq, nil, nil), init);
  214. n->decl = ids;
  215. return n;
  216. }
  217. void
  218. condecled(Node *n)
  219. {
  220. Decl *ids, *last;
  221. ids = n->decl;
  222. installids(Dconst, ids);
  223. for(last = ids; ids != nil; ids = ids->next){
  224. ids->ty = tunknown;
  225. last = ids;
  226. }
  227. n->left->decl = last;
  228. }
  229. Node*
  230. exdecl(Decl *ids, Decl *tids)
  231. {
  232. Node *n;
  233. Type *t;
  234. t = mktype(&ids->src.start, &ids->src.stop, Texception, nil, tids);
  235. t->cons = 1;
  236. n = mkn(Oexdecl, mkn(Oseq, nil, nil), nil);
  237. n->decl = ids;
  238. n->ty = t;
  239. return n;
  240. }
  241. void
  242. exdecled(Node *n)
  243. {
  244. Decl *ids, *last;
  245. Type *t;
  246. ids = n->decl;
  247. installids(Dconst, ids);
  248. t = n->ty;
  249. for(last = ids; ids != nil; ids = ids->next){
  250. ids->ty = t;
  251. last = ids;
  252. }
  253. n->left->decl = last;
  254. }
  255. Node*
  256. importdecl(Node *m, Decl *ids)
  257. {
  258. Node *n;
  259. n = mkn(Oimport, mkn(Oseq, nil, nil), m);
  260. n->decl = ids;
  261. return n;
  262. }
  263. void
  264. importdecled(Node *n)
  265. {
  266. Decl *ids, *last;
  267. ids = n->decl;
  268. installids(Dimport, ids);
  269. for(last = ids; ids != nil; ids = ids->next){
  270. ids->ty = tunknown;
  271. last = ids;
  272. }
  273. n->left->decl = last;
  274. }
  275. Node*
  276. mkscope(Node *body)
  277. {
  278. Node *n;
  279. n = mkn(Oscope, nil, body);
  280. if(body != nil)
  281. n->src = body->src;
  282. return n;
  283. }
  284. Node*
  285. fndecl(Node *n, Type *t, Node *body)
  286. {
  287. n = mkbin(Ofunc, n, body);
  288. n->ty = t;
  289. return n;
  290. }
  291. void
  292. fndecled(Node *n)
  293. {
  294. Decl *d;
  295. Node *left;
  296. left = n->left;
  297. if(left->op == Oname){
  298. d = left->decl->sym->decl;
  299. if(d == nil || d->store == Dimport){
  300. d = mkids(&left->src, left->decl->sym, n->ty, nil);
  301. installids(Dfn, d);
  302. }
  303. left->decl = d;
  304. d->refs++;
  305. }
  306. if(left->op == Odot)
  307. pushscope(nil, Sother);
  308. if(n->ty->polys != nil){
  309. pushscope(nil, Sother);
  310. installids(Dtype, n->ty->polys);
  311. }
  312. pushscope(nil, Sother);
  313. installids(Darg, n->ty->ids);
  314. n->ty->ids = popscope();
  315. if(n->ty->val != nil)
  316. mergepolydecs(n->ty);
  317. if(n->ty->polys != nil)
  318. n->ty->polys = popscope();
  319. if(left->op == Odot)
  320. popscope();
  321. }
  322. /*
  323. * check the function declaration only
  324. * the body will be type checked later by fncheck
  325. */
  326. Decl *
  327. fnchk(Node *n)
  328. {
  329. int bad;
  330. Decl *d, *inadt, *adtp;
  331. Type *t;
  332. bad = 0;
  333. d = n->left->decl;
  334. if(n->left->op == Odot)
  335. d = n->left->right->decl;
  336. if(d == nil)
  337. fatal("decl() fnchk nil");
  338. n->left->decl = d;
  339. if(d->store == Dglobal || d->store == Dfield)
  340. d->store = Dfn;
  341. if(d->store != Dfn || d->init != nil){
  342. nerror(n, "redeclaration of function %D, previously declared as %k on line %L",
  343. d, d, d->src.start);
  344. if(d->store == Dfn && d->init != nil)
  345. bad = 1;
  346. }
  347. d->init = n;
  348. t = n->ty;
  349. inadt = d->dot;
  350. if(inadt != nil && (inadt->store != Dtype || inadt->ty->kind != Tadt))
  351. inadt = nil;
  352. if(n->left->op == Odot){
  353. pushscope(nil, Sother);
  354. adtp = outerpolys(n->left);
  355. if(adtp != nil)
  356. installids(Dtype, adtp);
  357. if(!polyequal(adtp, n->decl))
  358. nerror(n, "adt polymorphic type mismatch");
  359. n->decl = nil;
  360. }
  361. t = validtype(t, inadt);
  362. if(n->left->op == Odot)
  363. popscope();
  364. if(debug['d'])
  365. print("declare function %D ty %T newty %T\n", d, d->ty, t);
  366. t = usetype(t);
  367. if(!polyequal(d->ty->polys, t->polys))
  368. nerror(n, "function polymorphic type mismatch");
  369. if(!tcompat(d->ty, t, 0))
  370. nerror(n, "type mismatch: %D defined as %T declared as %T on line %L",
  371. d, t, d->ty, d->src.start);
  372. else if(!raisescompat(d->ty->u.eraises, t->u.eraises))
  373. nerror(n, "raises mismatch: %D", d);
  374. if(t->varargs != 0)
  375. nerror(n, "cannot define functions with a '*' argument, such as %D", d);
  376. t->u.eraises = d->ty->u.eraises;
  377. d->ty = t;
  378. d->offset = idoffsets(t->ids, MaxTemp, IBY2WD);
  379. d->src = n->src;
  380. d->locals = nil;
  381. n->ty = t;
  382. return bad ? nil: d;
  383. }
  384. Node*
  385. globalas(Node *dst, Node *v, int valok)
  386. {
  387. Node *tv;
  388. if(v == nil)
  389. return nil;
  390. if(v->op == Oas || v->op == Odas){
  391. v = globalas(v->left, v->right, valok);
  392. if(v == nil)
  393. return nil;
  394. }else if(valok && !initable(dst, v, 0))
  395. return nil;
  396. switch(dst->op){
  397. case Oname:
  398. if(dst->decl->init != nil)
  399. nerror(dst, "duplicate assignment to %V, previously assigned on line %L",
  400. dst, dst->decl->init->src.start);
  401. if(valok)
  402. dst->decl->init = v;
  403. return v;
  404. case Otuple:
  405. if(valok && v->op != Otuple)
  406. fatal("can't deal with %n in tuple case of globalas", v);
  407. tv = v->left;
  408. for(dst = dst->left; dst != nil; dst = dst->right){
  409. globalas(dst->left, tv->left, valok);
  410. if(valok)
  411. tv = tv->right;
  412. }
  413. return v;
  414. }
  415. fatal("can't deal with %n in globalas", dst);
  416. return nil;
  417. }
  418. int
  419. needsstore(Decl *d)
  420. {
  421. if(!d->refs)
  422. return 0;
  423. if(d->importid != nil)
  424. return 0;
  425. if(storespace[d->store])
  426. return 1;
  427. return 0;
  428. }
  429. /*
  430. * return the list of all referenced storage variables
  431. */
  432. Decl*
  433. vars(Decl *d)
  434. {
  435. Decl *v, *n;
  436. while(d != nil && !needsstore(d))
  437. d = d->next;
  438. for(v = d; v != nil; v = v->next){
  439. while(v->next != nil){
  440. n = v->next;
  441. if(needsstore(n))
  442. break;
  443. v->next = n->next;
  444. }
  445. }
  446. return d;
  447. }
  448. /*
  449. * declare variables from the left side of a := statement
  450. */
  451. static int
  452. recdasdecl(Node *n, int store, int *nid)
  453. {
  454. Decl *d, *old;
  455. int ok;
  456. switch(n->op){
  457. case Otuple:
  458. ok = 1;
  459. for(n = n->left; n != nil; n = n->right)
  460. ok &= recdasdecl(n->left, store, nid);
  461. return ok;
  462. case Oname:
  463. if(n->decl == nildecl){
  464. *nid = -1;
  465. return 1;
  466. }
  467. d = mkids(&n->src, n->decl->sym, nil, nil);
  468. installids(store, d);
  469. old = d->old;
  470. if(old != nil
  471. && old->store != Dfn
  472. && old->store != Dwundef
  473. && old->store != Dundef)
  474. warn(d->src.start, "redeclaration of %K, previously declared as %k on line %L",
  475. d, old, old->src.start);
  476. n->decl = d;
  477. d->refs++;
  478. d->das = 1;
  479. if(*nid >= 0)
  480. (*nid)++;
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. static int
  486. recmark(Node *n, int nid)
  487. {
  488. switch(n->op){
  489. case Otuple:
  490. for(n = n->left; n != nil; n = n->right)
  491. nid = recmark(n->left, nid);
  492. break;
  493. case Oname:
  494. n->decl->nid = nid;
  495. nid = 0;
  496. break;
  497. }
  498. return nid;
  499. }
  500. int
  501. dasdecl(Node *n)
  502. {
  503. int store, ok, nid;
  504. nid = 0;
  505. if(scope == ScopeGlobal)
  506. store = Dglobal;
  507. else
  508. store = Dlocal;
  509. ok = recdasdecl(n, store, &nid);
  510. if(!ok)
  511. nerror(n, "illegal declaration expression %V", n);
  512. if(ok && store == Dlocal && nid > 1)
  513. recmark(n, nid);
  514. return ok;
  515. }
  516. /*
  517. * declare global variables in nested := expressions
  518. */
  519. void
  520. gdasdecl(Node *n)
  521. {
  522. if(n == nil)
  523. return;
  524. if(n->op == Odas){
  525. gdasdecl(n->right);
  526. dasdecl(n->left);
  527. }else{
  528. gdasdecl(n->left);
  529. gdasdecl(n->right);
  530. }
  531. }
  532. Decl*
  533. undefed(Src *src, Sym *s)
  534. {
  535. Decl *d;
  536. d = mkids(src, s, tnone, nil);
  537. error(src->start, "%s is not declared", s->name);
  538. installids(Dwundef, d);
  539. return d;
  540. }
  541. /*
  542. int
  543. inloop()
  544. {
  545. int i;
  546. for (i = scope; i > 0; i--)
  547. if (scopekind[i] == Sloop)
  548. return 1;
  549. return 0;
  550. }
  551. */
  552. int
  553. nested()
  554. {
  555. int i;
  556. for (i = scope; i > 0; i--)
  557. if (scopekind[i] == Sscope || scopekind[i] == Sloop)
  558. return 1;
  559. return 0;
  560. }
  561. void
  562. decltozero(Node *n)
  563. {
  564. Node *scp;
  565. if ((scp = scopenode[scope]) != nil) {
  566. /* can happen if we do
  567. * x[i] := ......
  568. * which is an error
  569. */
  570. if (n->right != nil && errors == 0)
  571. fatal("Ovardecl/Oname/Otuple has right field\n");
  572. n->right = scp->left;
  573. scp->left = n;
  574. }
  575. }
  576. void
  577. pushscope(Node *scp, int kind)
  578. {
  579. if(scope >= MaxScope)
  580. fatal("scope too deep");
  581. scope++;
  582. scopes[scope] = nil;
  583. tails[scope] = nil;
  584. scopenode[scope] = scp;
  585. scopekind[scope] = kind;
  586. }
  587. Decl*
  588. curscope(void)
  589. {
  590. return scopes[scope];
  591. }
  592. /*
  593. * revert to old declarations for each symbol in the currect scope.
  594. * remove the effects of any imported adt types
  595. * whenever the adt is imported from a module,
  596. * we record in the type's decl the module to use
  597. * when calling members. the process is reversed here.
  598. */
  599. Decl*
  600. popscope(void)
  601. {
  602. Decl *id;
  603. Type *t;
  604. if (debug['X'])
  605. print("popscope\n");
  606. for(id = scopes[scope]; id != nil; id = id->next){
  607. if(id->sym != nil){
  608. if (debug['X'])
  609. print("%s : %s %d\n", id->sym->name, kindname[id->ty->kind], id->init != nil ? id->init->op : 0);
  610. id->sym->decl = id->old;
  611. id->old = nil;
  612. }
  613. if(id->importid != nil)
  614. id->importid->refs += id->refs;
  615. t = id->ty;
  616. if(id->store == Dtype
  617. && t->decl != nil
  618. && t->decl->timport == id)
  619. t->decl->timport = id->timport;
  620. if(id->store == Dlocal)
  621. freeloc(id);
  622. }
  623. return scopes[scope--];
  624. }
  625. /*
  626. * make a new scope,
  627. * preinstalled with some previously installed identifiers
  628. * don't add the identifiers to the scope chain,
  629. * so they remain separate from any newly installed ids
  630. *
  631. * these routines assume no ids are imports
  632. */
  633. void
  634. repushids(Decl *ids)
  635. {
  636. Sym *s;
  637. if(scope >= MaxScope)
  638. fatal("scope too deep");
  639. scope++;
  640. scopes[scope] = nil;
  641. tails[scope] = nil;
  642. scopenode[scope] = nil;
  643. scopekind[scope] = Sother;
  644. for(; ids != nil; ids = ids->next){
  645. if(ids->scope != scope
  646. && (ids->dot == nil || !isimpmod(ids->dot->sym)
  647. || ids->scope != ScopeGlobal || scope != ScopeGlobal + 1))
  648. fatal("repushids scope mismatch");
  649. s = ids->sym;
  650. if(s != nil && ids->store != Dtag){
  651. if(s->decl != nil && s->decl->scope >= scope)
  652. ids->old = s->decl->old;
  653. else
  654. ids->old = s->decl;
  655. s->decl = ids;
  656. }
  657. }
  658. }
  659. /*
  660. * pop a scope which was started with repushids
  661. * return any newly installed ids
  662. */
  663. Decl*
  664. popids(Decl *ids)
  665. {
  666. for(; ids != nil; ids = ids->next){
  667. if(ids->sym != nil && ids->store != Dtag){
  668. ids->sym->decl = ids->old;
  669. ids->old = nil;
  670. }
  671. }
  672. return popscope();
  673. }
  674. void
  675. installids(int store, Decl *ids)
  676. {
  677. Decl *d, *last;
  678. Sym *s;
  679. last = nil;
  680. for(d = ids; d != nil; d = d->next){
  681. d->scope = scope;
  682. if(d->store == Dundef)
  683. d->store = store;
  684. s = d->sym;
  685. if(s != nil){
  686. if(s->decl != nil && s->decl->scope >= scope){
  687. redecl(d);
  688. d->old = s->decl->old;
  689. }else
  690. d->old = s->decl;
  691. s->decl = d;
  692. }
  693. last = d;
  694. }
  695. if(ids != nil){
  696. d = tails[scope];
  697. if(d == nil)
  698. scopes[scope] = ids;
  699. else
  700. d->next = ids;
  701. tails[scope] = last;
  702. }
  703. }
  704. Decl*
  705. lookup(Sym *sym)
  706. {
  707. int s;
  708. Decl *d;
  709. for(s = scope; s >= ScopeBuiltin; s--){
  710. for(d = scopes[s]; d != nil; d = d->next){
  711. if(d->sym == sym)
  712. return d;
  713. }
  714. }
  715. return nil;
  716. }
  717. Decl*
  718. mkids(Src *src, Sym *s, Type *t, Decl *next)
  719. {
  720. Decl *d;
  721. d = mkdecl(src, Dundef, t);
  722. d->next = next;
  723. d->sym = s;
  724. return d;
  725. }
  726. Decl*
  727. mkdecl(Src *src, int store, Type *t)
  728. {
  729. Decl *d;
  730. static Decl z;
  731. d = allocmem(sizeof *d);
  732. *d = z;
  733. d->src = *src;
  734. d->store = store;
  735. d->ty = t;
  736. d->nid = 1;
  737. return d;
  738. }
  739. Decl*
  740. dupdecl(Decl *old)
  741. {
  742. Decl *d;
  743. d = allocmem(sizeof *d);
  744. *d = *old;
  745. d->next = nil;
  746. return d;
  747. }
  748. Decl*
  749. dupdecls(Decl *old)
  750. {
  751. Decl *d, *nd, *first, *last;
  752. first = last = nil;
  753. for(d = old; d != nil; d = d->next){
  754. nd = dupdecl(d);
  755. if(first == nil)
  756. first = nd;
  757. else
  758. last->next = nd;
  759. last = nd;
  760. }
  761. return first;
  762. }
  763. Decl*
  764. appdecls(Decl *d, Decl *dd)
  765. {
  766. Decl *t;
  767. if(d == nil)
  768. return dd;
  769. for(t = d; t->next != nil; t = t->next)
  770. ;
  771. t->next = dd;
  772. return d;
  773. }
  774. Decl*
  775. revids(Decl *id)
  776. {
  777. Decl *d, *next;
  778. d = nil;
  779. for(; id != nil; id = next){
  780. next = id->next;
  781. id->next = d;
  782. d = id;
  783. }
  784. return d;
  785. }
  786. long
  787. idoffsets(Decl *id, long offset, int al)
  788. {
  789. int a, algn;
  790. Decl *d;
  791. algn = 1;
  792. for(; id != nil; id = id->next){
  793. if(storespace[id->store]){
  794. usedty(id->ty);
  795. if(id->store == Dlocal && id->link != nil){
  796. /* id->nid always 1 */
  797. id->offset = id->link->offset;
  798. continue;
  799. }
  800. a = id->ty->align;
  801. if(id->nid > 1){
  802. for(d = id->next; d != nil && d->nid == 0; d = d->next)
  803. if(d->ty->align > a)
  804. a = d->ty->align;
  805. algn = a;
  806. }
  807. offset = align(offset, a);
  808. id->offset = offset;
  809. offset += id->ty->size;
  810. if(id->nid == 0 && (id->next == nil || id->next->nid != 0))
  811. offset = align(offset, algn);
  812. }
  813. }
  814. return align(offset, al);
  815. }
  816. long
  817. idindices(Decl *id)
  818. {
  819. int i;
  820. i = 0;
  821. for(; id != nil; id = id->next){
  822. if(storespace[id->store]){
  823. usedty(id->ty);
  824. id->offset = i++;
  825. }
  826. }
  827. return i;
  828. }
  829. int
  830. declconv(Fmt *f)
  831. {
  832. Decl *d;
  833. char buf[4096], *s;
  834. d = va_arg(f->args, Decl*);
  835. if(d->sym == nil)
  836. s = "<nil>";
  837. else
  838. s = d->sym->name;
  839. seprint(buf, buf+sizeof(buf), "%s %s", storename[d->store], s);
  840. return fmtstrcpy(f, buf);
  841. }
  842. int
  843. storeconv(Fmt *f)
  844. {
  845. Decl *d;
  846. char buf[4096];
  847. d = va_arg(f->args, Decl*);
  848. seprint(buf, buf+sizeof(buf), "%s%s", storeart[d->store], storename[d->store]);
  849. return fmtstrcpy(f, buf);
  850. }
  851. int
  852. dotconv(Fmt *f)
  853. {
  854. Decl *d;
  855. char buf[4096], *p, *s;
  856. d = va_arg(f->args, Decl*);
  857. buf[0] = 0;
  858. p = buf;
  859. if(d->dot != nil && !isimpmod(d->dot->sym)){
  860. s = ".";
  861. if(d->dot->ty != nil && d->dot->ty->kind == Tmodule)
  862. s = "->";
  863. p = seprint(buf, buf+sizeof(buf), "%D%s", d->dot, s);
  864. }
  865. seprint(p, buf+sizeof(buf), "%s", d->sym->name);
  866. return fmtstrcpy(f, buf);
  867. }
  868. /*
  869. * merge together two sorted lists, yielding a sorted list
  870. */
  871. static Decl*
  872. namemerge(Decl *e, Decl *f)
  873. {
  874. Decl rock, *d;
  875. d = &rock;
  876. while(e != nil && f != nil){
  877. if(strcmp(e->sym->name, f->sym->name) <= 0){
  878. d->next = e;
  879. e = e->next;
  880. }else{
  881. d->next = f;
  882. f = f->next;
  883. }
  884. d = d->next;
  885. }
  886. if(e != nil)
  887. d->next = e;
  888. else
  889. d->next = f;
  890. return rock.next;
  891. }
  892. /*
  893. * recursively split lists and remerge them after they are sorted
  894. */
  895. static Decl*
  896. recnamesort(Decl *d, int n)
  897. {
  898. Decl *r, *dd;
  899. int i, m;
  900. if(n <= 1)
  901. return d;
  902. m = n / 2 - 1;
  903. dd = d;
  904. for(i = 0; i < m; i++)
  905. dd = dd->next;
  906. r = dd->next;
  907. dd->next = nil;
  908. return namemerge(recnamesort(d, n / 2),
  909. recnamesort(r, (n + 1) / 2));
  910. }
  911. /*
  912. * sort the ids by name
  913. */
  914. Decl*
  915. namesort(Decl *d)
  916. {
  917. Decl *dd;
  918. int n;
  919. n = 0;
  920. for(dd = d; dd != nil; dd = dd->next)
  921. n++;
  922. return recnamesort(d, n);
  923. }
  924. void
  925. printdecls(Decl *d)
  926. {
  927. for(; d != nil; d = d->next)
  928. print("%ld: %K %T ref %d\n", d->offset, d, d->ty, d->refs);
  929. }
  930. void
  931. mergepolydecs(Type *t)
  932. {
  933. Node *n, *nn;
  934. Decl *id, *ids, *ids1;
  935. for(n = t->val; n != nil; n = n->right){
  936. nn = n->left;
  937. for(ids = nn->decl; ids != nil; ids = ids->next){
  938. id = ids->sym->decl;
  939. if(id == nil){
  940. undefed(&ids->src, ids->sym);
  941. break;
  942. }
  943. if(id->store != Dtype){
  944. error(ids->src.start, "%K is not a type", id);
  945. break;
  946. }
  947. if(id->ty->kind != Tpoly){
  948. error(ids->src.start, "%K is not a polymorphic type", id);
  949. break;
  950. }
  951. if(id->ty->ids != nil)
  952. error(ids->src.start, "%K redefined", id);
  953. pushscope(nil, Sother);
  954. fielddecled(nn->left);
  955. id->ty->ids = popscope();
  956. for(ids1 = id->ty->ids; ids1 != nil; ids1 = ids1->next){
  957. ids1->dot = id;
  958. bindtypes(ids1->ty);
  959. if(ids1->ty->kind != Tfn){
  960. error(ids1->src.start, "only function types expected");
  961. id->ty->ids = nil;
  962. }
  963. }
  964. }
  965. }
  966. t->val = nil;
  967. }
  968. static void
  969. adjfnptrs(Decl *d, Decl *polys1, Decl *polys2)
  970. {
  971. int n;
  972. Decl *id, *idt, *idf, *arg;
  973. if(debug['U'])
  974. print("adjnptrs %s\n", d->sym->name);
  975. n = 0;
  976. for(id = d->ty->ids; id != nil; id = id->next)
  977. n++;
  978. for(idt = polys1; idt != nil; idt = idt->next)
  979. for(idf = idt->ty->ids; idf != nil; idf = idf->next)
  980. n -= 2;
  981. for(idt = polys2; idt != nil; idt = idt->next)
  982. for(idf = idt->ty->ids; idf != nil; idf = idf->next)
  983. n -= 2;
  984. for(arg = d->ty->ids; --n >= 0; arg = arg->next)
  985. ;
  986. for(idt = polys1; idt != nil; idt = idt->next){
  987. for(idf = idt->ty->ids; idf != nil; idf = idf->next){
  988. idf->link = arg;
  989. arg = arg->next->next;
  990. }
  991. }
  992. for(idt = polys2; idt != nil; idt = idt->next){
  993. for(idf = idt->ty->ids; idf != nil; idf = idf->next){
  994. idf->link = arg;
  995. arg = arg->next->next;
  996. }
  997. }
  998. }
  999. static void
  1000. addptrs(Decl *polys, Decl** fps, Decl **last, int link, Src *src)
  1001. {
  1002. Decl *idt, *idf, *fp;
  1003. if(debug['U'])
  1004. print("addptrs\n");
  1005. for(idt = polys; idt != nil; idt = idt->next){
  1006. for(idf = idt->ty->ids; idf != nil; idf = idf->next){
  1007. fp = mkdecl(src, Darg, tany);
  1008. fp->sym = idf->sym;
  1009. if(link)
  1010. idf->link = fp;
  1011. if(*fps == nil)
  1012. *fps = fp;
  1013. else
  1014. (*last)->next = fp;
  1015. *last = fp;
  1016. fp = mkdecl(src, Darg, tint);
  1017. fp->sym = idf->sym;
  1018. (*last)->next = fp;
  1019. *last = fp;
  1020. }
  1021. }
  1022. }
  1023. void
  1024. addfnptrs(Decl *d, int link)
  1025. {
  1026. Decl *fps, *last, *polys;
  1027. if(debug['U'])
  1028. print("addfnptrs %s %d\n", d->sym->name, link);
  1029. polys = encpolys(d);
  1030. if(d->ty->flags&FULLARGS){
  1031. if(link)
  1032. adjfnptrs(d, d->ty->polys, polys);
  1033. if(0 && debug['U']){
  1034. for(d = d->ty->ids; d != nil; d = d->next)
  1035. print("%s=%ld(%d) ", d->sym->name, d->offset, tattr[d->ty->kind].isptr);
  1036. print("\n");
  1037. }
  1038. return;
  1039. }
  1040. d->ty->flags |= FULLARGS;
  1041. fps = last = nil;
  1042. addptrs(d->ty->polys, &fps, &last, link, &d->src);
  1043. addptrs(polys, &fps, &last, link, &d->src);
  1044. for(last = d->ty->ids; last != nil && last->next != nil; last = last->next)
  1045. ;
  1046. if(last != nil)
  1047. last->next = fps;
  1048. else
  1049. d->ty->ids = fps;
  1050. d->offset = idoffsets(d->ty->ids, MaxTemp, IBY2WD);
  1051. if(0 && debug['U']){
  1052. for(d = d->ty->ids; d != nil; d = d->next)
  1053. print("%s=%ld(%d) ", d->sym->name, d->offset, tattr[d->ty->kind].isptr);
  1054. print("\n");
  1055. }
  1056. }
  1057. void
  1058. rmfnptrs(Decl *d)
  1059. {
  1060. int n;
  1061. Decl *id, *idt, *idf;
  1062. if(debug['U'])
  1063. print("rmfnptrs %s\n", d->sym->name);
  1064. if(!(d->ty->flags&FULLARGS))
  1065. return;
  1066. d->ty->flags &= ~FULLARGS;
  1067. n = 0;
  1068. for(id = d->ty->ids; id != nil; id = id->next)
  1069. n++;
  1070. for(idt = d->ty->polys; idt != nil; idt = idt->next)
  1071. for(idf = idt->ty->ids; idf != nil; idf = idf->next)
  1072. n -= 2;
  1073. for(idt = encpolys(d); idt != nil; idt = idt->next)
  1074. for(idf = idt->ty->ids; idf != nil; idf = idf->next)
  1075. n -= 2;
  1076. if(n == 0){
  1077. d->ty->ids = nil;
  1078. return;
  1079. }
  1080. for(id = d->ty->ids; --n > 0; id = id->next)
  1081. ;
  1082. id->next = nil;
  1083. d->offset = idoffsets(d->ty->ids, MaxTemp, IBY2WD);
  1084. }
  1085. int
  1086. local(Decl *d)
  1087. {
  1088. for(d = d->dot; d != nil; d = d->dot)
  1089. if(d->store == Dtype && d->ty->kind == Tmodule)
  1090. return 0;
  1091. return 1;
  1092. }
  1093. Decl*
  1094. module(Decl *d)
  1095. {
  1096. for(d = d->dot; d != nil; d = d->dot)
  1097. if(d->store == Dtype && d->ty->kind == Tmodule)
  1098. return d;
  1099. return nil;
  1100. }
  1101. Decl*
  1102. outerpolys(Node *n)
  1103. {
  1104. Decl *d;
  1105. if(n->op == Odot){
  1106. d = n->right->decl;
  1107. if(d == nil)
  1108. fatal("decl() outeradt nil");
  1109. d = d->dot;
  1110. if(d != nil && d->store == Dtype && d->ty->kind == Tadt)
  1111. return d->ty->polys;
  1112. }
  1113. return nil;
  1114. }
  1115. Decl*
  1116. encpolys(Decl *d)
  1117. {
  1118. if((d = d->dot) == nil)
  1119. return nil;
  1120. return d->ty->polys;
  1121. }
  1122. Decl*
  1123. fnlookup(Sym *s, Type *t, Node **m)
  1124. {
  1125. Decl *id;
  1126. Node *mod;
  1127. id = nil;
  1128. mod = nil;
  1129. if(t->kind == Tpoly || t->kind == Tmodule)
  1130. id = namedot(t->ids, s);
  1131. else if(t->kind == Tref){
  1132. t = t->tof;
  1133. if(t->kind == Tadt){
  1134. id = namedot(t->ids, s);
  1135. if(t->decl != nil && t->decl->timport != nil)
  1136. mod = t->decl->timport->eimport;
  1137. }
  1138. else if(t->kind == Tadtpick){
  1139. id = namedot(t->ids, s);
  1140. if(t->decl != nil && t->decl->timport != nil)
  1141. mod = t->decl->timport->eimport;
  1142. t = t->decl->dot->ty;
  1143. if(id == nil)
  1144. id = namedot(t->ids, s);
  1145. if(t->decl != nil && t->decl->timport != nil)
  1146. mod = t->decl->timport->eimport;
  1147. }
  1148. }
  1149. if(id == nil){
  1150. id = lookup(s);
  1151. if(id != nil)
  1152. mod = id->eimport;
  1153. }
  1154. if(m != nil)
  1155. *m = mod;
  1156. return id;
  1157. }
  1158. int
  1159. isimpmod(Sym *s)
  1160. {
  1161. Decl *d;
  1162. for(d = impmods; d != nil; d = d->next)
  1163. if(d->sym == s)
  1164. return 1;
  1165. return 0;
  1166. }
  1167. int
  1168. dequal(Decl *d1, Decl *d2, int full)
  1169. {
  1170. return d1->sym == d2->sym &&
  1171. d1->store == d2->store &&
  1172. d1->implicit == d2->implicit &&
  1173. d1->cyc == d2->cyc &&
  1174. (!full || tequal(d1->ty, d2->ty)) &&
  1175. (!full || d1->store == Dfn || sametree(d1->init, d2->init));
  1176. }
  1177. static int
  1178. tzero(Type *t)
  1179. {
  1180. return t->kind == Texception || tmustzero(t);
  1181. }
  1182. static int
  1183. isptr(Type *t)
  1184. {
  1185. return t->kind == Texception || tattr[t->kind].isptr;
  1186. }
  1187. /* can d share the same stack location as another local ? */
  1188. void
  1189. shareloc(Decl *d)
  1190. {
  1191. int z;
  1192. Type *t, *tt;
  1193. Decl *dd, *res;
  1194. if(d->store != Dlocal || d->nid != 1)
  1195. return;
  1196. t = d->ty;
  1197. res = nil;
  1198. for(dd = fndecls; dd != nil; dd = dd->next){
  1199. if(d == dd)
  1200. fatal("d==dd in shareloc");
  1201. if(dd->store != Dlocal || dd->nid != 1 || dd->link != nil || dd->tref != 0)
  1202. continue;
  1203. tt = dd->ty;
  1204. if(t->size != tt->size || t->align != tt->align)
  1205. continue;
  1206. z = tzero(t)+tzero(tt);
  1207. if(z > 0)
  1208. continue; /* for now */
  1209. if(t == tt || tequal(t, tt))
  1210. res = dd;
  1211. else{
  1212. if(z == 1)
  1213. continue;
  1214. if(z == 0 || isptr(t) || isptr(tt) || mktdesc(t) == mktdesc(tt))
  1215. res = dd;
  1216. }
  1217. if(res != nil){
  1218. /* print("%L %K share %L %K\n", d->src.start, d, res->src.start, res); */
  1219. d->link = res;
  1220. res->tref = 1;
  1221. return;
  1222. }
  1223. }
  1224. return;
  1225. }
  1226. static void
  1227. freeloc(Decl *d)
  1228. {
  1229. if(d->link != nil)
  1230. d->link->tref = 0;
  1231. }