typecheck.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625
  1. #include "limbo.h"
  2. #include "y.tab.h"
  3. Node **labstack;
  4. int labdep;
  5. static Node* inexcept;
  6. static Decl* fndec;
  7. void checkraises(Node *n);
  8. static void
  9. increfs(Decl *id)
  10. {
  11. for( ; id != nil; id = id->link)
  12. id->refs++;
  13. }
  14. static int
  15. fninline(Decl *d)
  16. {
  17. Node *n, *left, *right;
  18. left = right = nil;
  19. n = d->init;
  20. if(dontinline || d->caninline < 0 || d->locals != nil || ispoly(d) || n->ty->tof->kind == Tnone || nodes(n) >= 100)
  21. return 0;
  22. n = n->right;
  23. if(n->op == Oseq && n->right == nil)
  24. n = n->left;
  25. /*
  26. * inline
  27. * (a) return e;
  28. * (b) if(c) return e1; else return e2;
  29. * (c) if(c) return e1; return e2;
  30. */
  31. switch(n->op){
  32. case Oret:
  33. break;
  34. case Oif:
  35. right = n->right;
  36. if(right->right == nil || right->left->op != Oret || right->right->op != Oret || !tequal(right->left->left->ty, right->right->left->ty))
  37. return 0;
  38. break;
  39. case Oseq:
  40. left = n->left;
  41. right = n->right;
  42. if(left->op != Oif || left->right->right != nil || left->right->left->op != Oret || right->op != Oseq || right->right != nil || right->left->op != Oret || !tequal(left->right->left->left->ty, right->left->left->ty))
  43. return 0;
  44. break;
  45. default:
  46. return 0;
  47. }
  48. if(occurs(d, n) || hasasgns(n))
  49. return 0;
  50. if(n->op == Oseq){
  51. left->right->right = right->left;
  52. n = left;
  53. right = n->right;
  54. d->init->right->right = nil;
  55. }
  56. if(n->op == Oif){
  57. n->ty = right->ty = right->left->left->ty;
  58. right->left = right->left->left;
  59. right->right = right->right->left;
  60. d->init->right->left = mkunary(Oret, n);
  61. }
  62. return 1;
  63. }
  64. static int
  65. isfnrefty(Type *t)
  66. {
  67. return t->kind == Tref && t->tof->kind == Tfn;
  68. }
  69. static int
  70. isfnref(Decl *d)
  71. {
  72. switch(d->store){
  73. case Dglobal:
  74. case Darg:
  75. case Dlocal:
  76. case Dfield:
  77. case Dimport:
  78. return isfnrefty(d->ty);
  79. }
  80. return 0;
  81. }
  82. int
  83. argncompat(Node *n, Decl *f, Node *a)
  84. {
  85. for(; a != nil; a = a->right){
  86. if(f == nil){
  87. nerror(n, "%V: too many function arguments", n->left);
  88. return 0;
  89. }
  90. f = f->next;
  91. }
  92. if(f != nil){
  93. nerror(n, "%V: too few function arguments", n->left);
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. static void
  99. rewind(Node *n)
  100. {
  101. Node *r, *nn;
  102. r = n;
  103. nn = n->left;
  104. for(n = n->right; n != nil; n = n->right){
  105. if(n->right == nil){
  106. r->left = nn;
  107. r->right = n->left;
  108. }
  109. else
  110. nn = mkbin(Oindex, nn, n->left);
  111. }
  112. }
  113. static void
  114. ckmod(Node *n, Decl *id)
  115. {
  116. Type *t;
  117. Decl *d, *idc;
  118. Node *mod;
  119. if(id == nil)
  120. fatal("can't find function: %n", n);
  121. idc = nil;
  122. mod = nil;
  123. if(n->op == Oname){
  124. idc = id;
  125. mod = id->eimport;
  126. }
  127. else if(n->op == Omdot)
  128. mod = n->left;
  129. else if(n->op == Odot){
  130. idc = id->dot;
  131. t = n->left->ty;
  132. if(t->kind == Tref)
  133. t = t->tof;
  134. if(t->kind == Tadtpick)
  135. t = t->decl->dot->ty;
  136. d = t->decl;
  137. while(d != nil && d->link != nil)
  138. d = d->link;
  139. if(d != nil && d->timport != nil)
  140. mod = d->timport->eimport;
  141. n->right->left = mod;
  142. }
  143. if(mod != nil && mod->ty->kind != Tmodule){
  144. nerror(n, "cannot use %V as a function reference", n);
  145. return;
  146. }
  147. if(mod != nil){
  148. if(valistype(mod)){
  149. nerror(n, "cannot use %V as a function reference because %V is a module interface", n, mod);
  150. return;
  151. }
  152. }else if(idc != nil && idc->dot != nil && !isimpmod(idc->dot->sym)){
  153. nerror(n, "cannot use %V without importing %s from a variable", n, idc->sym->name);
  154. return;
  155. }
  156. if(mod != nil)
  157. modrefable(n->ty);
  158. }
  159. static void
  160. addref(Node *n)
  161. {
  162. Node *nn;
  163. nn = mkn(0, nil, nil);
  164. *nn = *n;
  165. n->op = Oref;
  166. n->left = nn;
  167. n->right = nil;
  168. n->decl = nil;
  169. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Tref, nn->ty, nil));
  170. }
  171. static void
  172. fnref(Node *n, Decl *id)
  173. {
  174. id->caninline = -1;
  175. ckmod(n, id);
  176. addref(n);
  177. while(id->link != nil)
  178. id = id->link;
  179. if(ispoly(id) && encpolys(id) != nil)
  180. nerror(n, "cannot have a polymorphic adt function reference %s", id->sym->name);
  181. }
  182. Decl*
  183. typecheck(int checkimp)
  184. {
  185. Decl *entry, *m, *d;
  186. Sym *s;
  187. int i;
  188. if(errors)
  189. return nil;
  190. /*
  191. * generate the set of all functions
  192. * compile one function at a time
  193. */
  194. gdecl(tree);
  195. gbind(tree);
  196. fns = allocmem(nfns * sizeof(Decl));
  197. i = gcheck(tree, fns, 0);
  198. if(i != nfns)
  199. fatal("wrong number of functions found in gcheck");
  200. maxlabdep = 0;
  201. for(i = 0; i < nfns; i++){
  202. d = fns[i];
  203. if(d != nil)
  204. fndec = d;
  205. if(d != nil)
  206. fncheck(d);
  207. fndec = nil;
  208. }
  209. if(errors)
  210. return nil;
  211. entry = nil;
  212. if(checkimp){
  213. Decl *im;
  214. Dlist *dm;
  215. if(impmods == nil){
  216. yyerror("no implementation module");
  217. return nil;
  218. }
  219. for(im = impmods; im != nil; im = im->next){
  220. for(dm = impdecls; dm != nil; dm = dm->next)
  221. if(dm->d->sym == im->sym)
  222. break;
  223. if(dm == nil || dm->d->ty == nil){
  224. yyerror("no definition for implementation module %s", im->sym->name);
  225. return nil;
  226. }
  227. }
  228. /*
  229. * can't check the module spec until all types and imports are determined,
  230. * which happens in scheck
  231. */
  232. for(dm = impdecls; dm != nil; dm = dm->next){
  233. im = dm->d;
  234. im->refs++;
  235. im->ty = usetype(im->ty);
  236. if(im->store != Dtype || im->ty->kind != Tmodule){
  237. error(im->src.start, "cannot implement %K", im);
  238. return nil;
  239. }
  240. }
  241. /* now check any multiple implementations */
  242. impdecl = modimp(impdecls, impmods);
  243. s = enter("init", 0);
  244. entry = nil;
  245. for(dm = impdecls; dm != nil; dm = dm->next){
  246. im = dm->d;
  247. for(m = im->ty->ids; m != nil; m = m->next){
  248. m->ty = usetype(m->ty);
  249. m->refs++;
  250. if(m->sym == s && m->ty->kind == Tfn && entry == nil)
  251. entry = m;
  252. if(m->store == Dglobal || m->store == Dfn)
  253. modrefable(m->ty);
  254. if(m->store == Dtype && m->ty->kind == Tadt){
  255. for(d = m->ty->ids; d != nil; d = d->next){
  256. d->ty = usetype(d->ty);
  257. modrefable(d->ty);
  258. d->refs++;
  259. }
  260. }
  261. }
  262. checkrefs(im->ty->ids);
  263. }
  264. }
  265. if(errors)
  266. return nil;
  267. gsort(tree);
  268. tree = nil;
  269. return entry;
  270. }
  271. /*
  272. * introduce all global declarations
  273. * also adds all fields to adts and modules
  274. * note the complications due to nested Odas expressions
  275. */
  276. void
  277. gdecl(Node *n)
  278. {
  279. for(;;){
  280. if(n == nil)
  281. return;
  282. if(n->op != Oseq)
  283. break;
  284. gdecl(n->left);
  285. n = n->right;
  286. }
  287. switch(n->op){
  288. case Oimport:
  289. importdecled(n);
  290. gdasdecl(n->right);
  291. break;
  292. case Oadtdecl:
  293. adtdecled(n);
  294. break;
  295. case Ocondecl:
  296. condecled(n);
  297. gdasdecl(n->right);
  298. break;
  299. case Oexdecl:
  300. exdecled(n);
  301. break;
  302. case Omoddecl:
  303. moddecled(n);
  304. break;
  305. case Otypedecl:
  306. typedecled(n);
  307. break;
  308. case Ovardecl:
  309. vardecled(n);
  310. break;
  311. case Ovardecli:
  312. vardecled(n->left);
  313. gdasdecl(n->right);
  314. break;
  315. case Ofunc:
  316. fndecled(n);
  317. break;
  318. case Oas:
  319. case Odas:
  320. case Onothing:
  321. gdasdecl(n);
  322. break;
  323. default:
  324. fatal("can't deal with %O in gdecl", n->op);
  325. }
  326. }
  327. /*
  328. * bind all global type ids,
  329. * including those nested inside modules
  330. * this needs to be done, since we may use such
  331. * a type later in a nested scope, so if we bound
  332. * the type ids then, the type could get bound
  333. * to a nested declaration
  334. */
  335. void
  336. gbind(Node *n)
  337. {
  338. Decl *d, *ids;
  339. for(;;){
  340. if(n == nil)
  341. return;
  342. if(n->op != Oseq)
  343. break;
  344. gbind(n->left);
  345. n = n->right;
  346. }
  347. switch(n->op){
  348. case Oas:
  349. case Ocondecl:
  350. case Odas:
  351. case Oexdecl:
  352. case Ofunc:
  353. case Oimport:
  354. case Onothing:
  355. case Ovardecl:
  356. case Ovardecli:
  357. break;
  358. case Ofielddecl:
  359. bindtypes(n->decl->ty);
  360. break;
  361. case Otypedecl:
  362. bindtypes(n->decl->ty);
  363. if(n->left != nil)
  364. gbind(n->left);
  365. break;
  366. case Opickdecl:
  367. gbind(n->left);
  368. d = n->right->left->decl;
  369. bindtypes(d->ty);
  370. repushids(d->ty->ids);
  371. gbind(n->right->right);
  372. /* get new ids for undefined types; propagate outwards */
  373. ids = popids(d->ty->ids);
  374. if(ids != nil)
  375. installids(Dundef, ids);
  376. break;
  377. case Oadtdecl:
  378. case Omoddecl:
  379. bindtypes(n->ty);
  380. if(n->ty->polys != nil)
  381. repushids(n->ty->polys);
  382. repushids(n->ty->ids);
  383. gbind(n->left);
  384. /* get new ids for undefined types; propagate outwards */
  385. ids = popids(n->ty->ids);
  386. if(ids != nil)
  387. installids(Dundef, ids);
  388. if(n->ty->polys != nil)
  389. popids(n->ty->polys);
  390. break;
  391. default:
  392. fatal("can't deal with %O in gbind", n->op);
  393. }
  394. }
  395. /*
  396. * check all of the > declarations
  397. * bind all type ids referred to within types at the global level
  398. * record decls for defined functions
  399. */
  400. int
  401. gcheck(Node *n, Decl **fns, int nfns)
  402. {
  403. Ok rok;
  404. Decl *d;
  405. for(;;){
  406. if(n == nil)
  407. return nfns;
  408. if(n->op != Oseq)
  409. break;
  410. nfns = gcheck(n->left, fns, nfns);
  411. n = n->right;
  412. }
  413. switch(n->op){
  414. case Ofielddecl:
  415. if(n->decl->ty->u.eraises)
  416. raisescheck(n->decl->ty);
  417. break;
  418. case Onothing:
  419. case Opickdecl:
  420. break;
  421. case Otypedecl:
  422. tcycle(n->ty);
  423. break;
  424. case Oadtdecl:
  425. case Omoddecl:
  426. if(n->ty->polys != nil)
  427. repushids(n->ty->polys);
  428. repushids(n->ty->ids);
  429. if(gcheck(n->left, nil, 0))
  430. fatal("gcheck fn decls nested in modules or adts");
  431. if(popids(n->ty->ids) != nil)
  432. fatal("gcheck installs new ids in a module or adt");
  433. if(n->ty->polys != nil)
  434. popids(n->ty->polys);
  435. break;
  436. case Ovardecl:
  437. varcheck(n, 1);
  438. break;
  439. case Ocondecl:
  440. concheck(n, 1);
  441. break;
  442. case Oexdecl:
  443. excheck(n, 1);
  444. break;
  445. case Oimport:
  446. importcheck(n, 1);
  447. break;
  448. case Ovardecli:
  449. varcheck(n->left, 1);
  450. rok = echeck(n->right, 0, 1, nil);
  451. if(rok.ok){
  452. if(rok.allok)
  453. n->right = fold(n->right);
  454. globalas(n->right->left, n->right->right, rok.allok);
  455. }
  456. break;
  457. case Oas:
  458. case Odas:
  459. rok = echeck(n, 0, 1, nil);
  460. if(rok.ok){
  461. if(rok.allok)
  462. n = fold(n);
  463. globalas(n->left, n->right, rok.allok);
  464. }
  465. break;
  466. case Ofunc:
  467. rok = echeck(n->left, 0, 1, n);
  468. if(rok.ok && n->ty->u.eraises)
  469. raisescheck(n->ty);
  470. d = nil;
  471. if(rok.ok)
  472. d = fnchk(n);
  473. fns[nfns++] = d;
  474. break;
  475. default:
  476. fatal("can't deal with %O in gcheck", n->op);
  477. }
  478. return nfns;
  479. }
  480. /*
  481. * check for unused expression results
  482. * make sure the any calculated expression has
  483. * a destination
  484. */
  485. Node*
  486. checkused(Node *n)
  487. {
  488. Type *t;
  489. Node *nn;
  490. /*
  491. * only nil; and nil = nil; should have type tany
  492. */
  493. if(n->ty == tany){
  494. if(n->op == Oname)
  495. return n;
  496. if(n->op == Oas)
  497. return checkused(n->right);
  498. fatal("line %L checkused %n", n->src.start, n);
  499. }
  500. if(n->op == Ocall && n->left->ty->kind == Tfn && n->left->ty->tof != tnone){
  501. n = mkunary(Oused, n);
  502. n->ty = n->left->ty;
  503. return n;
  504. }
  505. if(n->op == Ocall && isfnrefty(n->left->ty)){
  506. if(n->left->ty->tof->tof != tnone){
  507. n = mkunary(Oused, n);
  508. n->ty = n->left->ty;
  509. }
  510. return n;
  511. }
  512. if(isused[n->op] && (n->op != Ocall || n->left->ty->kind == Tfn))
  513. return n;
  514. t = n->ty;
  515. if(t->kind == Tfn)
  516. nerror(n, "function %V not called", n);
  517. else if(t->kind == Tadt && t->tags != nil || t->kind == Tadtpick)
  518. nerror(n, "expressions cannot have type %T", t);
  519. else if(n->op == Otuple){
  520. for(nn = n->left; nn != nil; nn = nn->right)
  521. checkused(nn->left);
  522. }
  523. else
  524. nwarn(n, "result of expression %V not used", n);
  525. n = mkunary(Oused, n);
  526. n->ty = n->left->ty;
  527. return n;
  528. }
  529. void
  530. fncheck(Decl *d)
  531. {
  532. Node *n;
  533. Decl *adtp;
  534. n = d->init;
  535. if(debug['t'])
  536. print("typecheck tree: %n\n", n);
  537. fndecls = nil;
  538. adtp = outerpolys(n->left);
  539. if(n->left->op == Odot)
  540. repushids(adtp);
  541. if(d->ty->polys != nil)
  542. repushids(d->ty->polys);
  543. repushids(d->ty->ids);
  544. labdep = 0;
  545. labstack = allocmem(maxlabdep * sizeof *labstack);
  546. n->right = scheck(n->right, d->ty->tof, Sother);
  547. if(labdep != 0)
  548. fatal("unbalanced label stack in fncheck");
  549. free(labstack);
  550. d->locals = appdecls(popids(d->ty->ids), fndecls);
  551. if(d->ty->polys != nil)
  552. popids(d->ty->polys);
  553. if(n->left->op == Odot)
  554. popids(adtp);
  555. fndecls = nil;
  556. checkrefs(d->ty->ids);
  557. checkrefs(d->ty->polys);
  558. checkrefs(d->locals);
  559. checkraises(n);
  560. d->caninline = fninline(d);
  561. }
  562. Node*
  563. scheck(Node *n, Type *ret, int kind)
  564. {
  565. Node *left, *right, *last, *top;
  566. Decl *d;
  567. Sym *s;
  568. Ok rok;
  569. int i;
  570. top = n;
  571. last = nil;
  572. for(; n != nil; n = n->right){
  573. left = n->left;
  574. right = n->right;
  575. switch(n->op){
  576. case Ovardecl:
  577. vardecled(n);
  578. varcheck(n, 0);
  579. if (nested() && tmustzero(n->decl->ty))
  580. decltozero(n);
  581. /*
  582. else if (inloop() && tmustzero(n->decl->ty))
  583. decltozero(n);
  584. */
  585. return top;
  586. case Ovardecli:
  587. vardecled(left);
  588. varcheck(left, 0);
  589. echeck(right, 0, 0, nil);
  590. if (nested() && tmustzero(left->decl->ty))
  591. decltozero(left);
  592. return top;
  593. case Otypedecl:
  594. typedecled(n);
  595. bindtypes(n->ty);
  596. tcycle(n->ty);
  597. return top;
  598. case Ocondecl:
  599. condecled(n);
  600. concheck(n, 0);
  601. return top;
  602. case Oexdecl:
  603. exdecled(n);
  604. excheck(n, 0);
  605. return top;
  606. case Oimport:
  607. importdecled(n);
  608. importcheck(n, 0);
  609. return top;
  610. case Ofunc:
  611. fatal("scheck func");
  612. case Oscope:
  613. pushscope(n, kind == Sother ? Sscope : kind);
  614. if (left != nil)
  615. fatal("Oscope has left field");
  616. echeck(left, 0, 0, nil);
  617. n->right = scheck(right, ret, Sother);
  618. d = popscope();
  619. fndecls = appdecls(fndecls, d);
  620. return top;
  621. case Olabel:
  622. echeck(left, 0, 0, nil);
  623. n->right = scheck(right, ret, Sother);
  624. return top;
  625. case Oseq:
  626. n->left = scheck(left, ret, Sother);
  627. /* next time will check n->right */
  628. break;
  629. case Oif:
  630. rok = echeck(left, 0, 0, nil);
  631. if(rok.ok && left->op != Onothing && left->ty != tint)
  632. nerror(n, "if conditional must be an int, not %Q", left);
  633. right->left = scheck(right->left, ret, Sother);
  634. /* next time will check n->right->right */
  635. n = right;
  636. break;
  637. case Ofor:
  638. rok = echeck(left, 0, 0, nil);
  639. if(rok.ok && left->op != Onothing && left->ty != tint)
  640. nerror(n, "for conditional must be an int, not %Q", left);
  641. /*
  642. * do the continue clause before the body
  643. * this reflects the ordering of declarations
  644. */
  645. pushlabel(n);
  646. right->right = scheck(right->right, ret, Sother);
  647. right->left = scheck(right->left, ret, Sloop);
  648. labdep--;
  649. if(n->decl != nil && !n->decl->refs)
  650. nwarn(n, "label %s never referenced", n->decl->sym->name);
  651. return top;
  652. case Odo:
  653. rok = echeck(left, 0, 0, nil);
  654. if(rok.ok && left->op != Onothing && left->ty != tint)
  655. nerror(n, "do conditional must be an int, not %Q", left);
  656. pushlabel(n);
  657. n->right = scheck(n->right, ret, Sloop);
  658. labdep--;
  659. if(n->decl != nil && !n->decl->refs)
  660. nwarn(n, "label %s never referenced", n->decl->sym->name);
  661. return top;
  662. case Oalt:
  663. case Ocase:
  664. case Opick:
  665. case Oexcept:
  666. pushlabel(n);
  667. switch(n->op){
  668. case Oalt:
  669. altcheck(n, ret);
  670. break;
  671. case Ocase:
  672. casecheck(n, ret);
  673. break;
  674. case Opick:
  675. pickcheck(n, ret);
  676. break;
  677. case Oexcept:
  678. exccheck(n, ret);
  679. break;
  680. }
  681. labdep--;
  682. if(n->decl != nil && !n->decl->refs)
  683. nwarn(n, "label %s never referenced", n->decl->sym->name);
  684. return top;
  685. case Oret:
  686. rok = echeck(left, 0, 0, nil);
  687. if(!rok.ok)
  688. return top;
  689. if(left == nil){
  690. if(ret != tnone)
  691. nerror(n, "return of nothing from a fn of %T", ret);
  692. }else if(ret == tnone){
  693. if(left->ty != tnone)
  694. nerror(n, "return %Q from a fn with no return type", left);
  695. }else if(!tcompat(ret, left->ty, 0))
  696. nerror(n, "return %Q from a fn of %T", left, ret);
  697. return top;
  698. case Obreak:
  699. case Ocont:
  700. s = nil;
  701. if(n->decl != nil)
  702. s = n->decl->sym;
  703. for(i = 0; i < labdep; i++){
  704. if(s == nil || labstack[i]->decl != nil && labstack[i]->decl->sym == s){
  705. if(n->op == Ocont
  706. && labstack[i]->op != Ofor && labstack[i]->op != Odo)
  707. continue;
  708. if(s != nil)
  709. labstack[i]->decl->refs++;
  710. return top;
  711. }
  712. }
  713. nerror(n, "no appropriate target for %V", n);
  714. return top;
  715. case Oexit:
  716. case Onothing:
  717. return top;
  718. case Oexstmt:
  719. fndec->handler = 1;
  720. n->left = scheck(left, ret, Sother);
  721. n->right = scheck(right, ret, Sother);
  722. return top;
  723. default:
  724. rok = echeck(n, 0, 0, nil);
  725. if(rok.allok)
  726. n = checkused(n);
  727. if(last == nil)
  728. return n;
  729. last->right = n;
  730. return top;
  731. }
  732. last = n;
  733. }
  734. return top;
  735. }
  736. void
  737. pushlabel(Node *n)
  738. {
  739. Sym *s;
  740. int i;
  741. if(labdep >= maxlabdep){
  742. maxlabdep += MaxScope;
  743. labstack = reallocmem(labstack, maxlabdep * sizeof *labstack);
  744. }
  745. if(n->decl != nil){
  746. s = n->decl->sym;
  747. n->decl->refs = 0;
  748. for(i = 0; i < labdep; i++)
  749. if(labstack[i]->decl != nil && labstack[i]->decl->sym == s)
  750. nerror(n, "label %s duplicated on line %L", s->name, labstack[i]->decl->src.start);
  751. }
  752. labstack[labdep++] = n;
  753. }
  754. void
  755. varcheck(Node *n, int isglobal)
  756. {
  757. Type *t;
  758. Decl *ids, *last;
  759. t = validtype(n->ty, nil);
  760. t = topvartype(t, n->decl, isglobal, 0);
  761. last = n->left->decl;
  762. for(ids = n->decl; ids != last->next; ids = ids->next){
  763. ids->ty = t;
  764. shareloc(ids);
  765. }
  766. if(t->u.eraises)
  767. raisescheck(t);
  768. }
  769. void
  770. concheck(Node *n, int isglobal)
  771. {
  772. Decl *ids, *last;
  773. Type *t;
  774. Node *init;
  775. Ok rok;
  776. int i;
  777. pushscope(nil, Sother);
  778. installids(Dconst, iota);
  779. rok = echeck(n->right, 0, isglobal, nil);
  780. popscope();
  781. init = n->right;
  782. if(!rok.ok){
  783. t = terror;
  784. }else{
  785. t = init->ty;
  786. if(!tattr[t->kind].conable){
  787. nerror(init, "cannot have a %T constant", t);
  788. rok.allok = 0;
  789. }
  790. }
  791. last = n->left->decl;
  792. for(ids = n->decl; ids != last->next; ids = ids->next)
  793. ids->ty = t;
  794. if(!rok.allok)
  795. return;
  796. i = 0;
  797. for(ids = n->decl; ids != last->next; ids = ids->next){
  798. if(rok.ok){
  799. iota->init->val = i;
  800. ids->init = dupn(0, &nosrc, init);
  801. if(!varcom(ids))
  802. rok.ok = 0;
  803. }
  804. i++;
  805. }
  806. }
  807. static char*
  808. exname(Decl *d)
  809. {
  810. int n;
  811. Sym *m;
  812. char *s;
  813. char buf[16];
  814. n = 0;
  815. sprint(buf, "%d", scope-ScopeGlobal);
  816. m = impmods->sym;
  817. if(d->dot)
  818. m = d->dot->sym;
  819. if(m)
  820. n += strlen(m->name)+1;
  821. if(fndec)
  822. n += strlen(fndec->sym->name)+1;
  823. n += strlen(buf)+1+strlen(d->sym->name)+1;
  824. s = malloc(n);
  825. strcpy(s, "");
  826. if(m){
  827. strcat(s, m->name);
  828. strcat(s, ".");
  829. }
  830. if(fndec){
  831. strcat(s, fndec->sym->name);
  832. strcat(s, ".");
  833. }
  834. strcat(s, buf);
  835. strcat(s, ".");
  836. strcat(s, d->sym->name);
  837. return s;
  838. }
  839. void
  840. excheck(Node *n, int isglobal)
  841. {
  842. char *nm;
  843. Type *t;
  844. Decl *ids, *last;
  845. t = validtype(n->ty, nil);
  846. t = topvartype(t, n->decl, isglobal, 0);
  847. last = n->left->decl;
  848. for(ids = n->decl; ids != last->next; ids = ids->next){
  849. ids->ty = t;
  850. nm = exname(ids);
  851. ids->init = mksconst(&n->src, enterstring(nm, strlen(nm)));
  852. /* ids->init = mksconst(&n->src, enterstring(strdup(ids->sym->name), strlen(ids->sym->name))); */
  853. }
  854. }
  855. void
  856. importcheck(Node *n, int isglobal)
  857. {
  858. Node *m;
  859. Decl *id, *last, *v;
  860. Type *t;
  861. Ok rok;
  862. rok = echeck(n->right, 1, isglobal, nil);
  863. if(!rok.ok)
  864. return;
  865. m = n->right;
  866. if(m->ty->kind != Tmodule || m->op != Oname){
  867. nerror(n, "cannot import from %Q", m);
  868. return;
  869. }
  870. last = n->left->decl;
  871. for(id = n->decl; id != last->next; id = id->next){
  872. v = namedot(m->ty->ids, id->sym);
  873. if(v == nil){
  874. error(id->src.start, "%s is not a member of %V", id->sym->name, m);
  875. id->store = Dwundef;
  876. continue;
  877. }
  878. id->store = v->store;
  879. v->ty = validtype(v->ty, nil);
  880. id->ty = t = v->ty;
  881. if(id->store == Dtype && t->decl != nil){
  882. id->timport = t->decl->timport;
  883. t->decl->timport = id;
  884. }
  885. id->init = v->init;
  886. id->importid = v;
  887. id->eimport = m;
  888. }
  889. }
  890. static Decl*
  891. rewcall(Node *n, Decl *d)
  892. {
  893. /* put original function back now we're type checked */
  894. while(d->link != nil)
  895. d = d->link;
  896. if(n->op == Odot)
  897. n->right->decl = d;
  898. else if(n->op == Omdot){
  899. n->right->right->decl = d;
  900. n->right->right->ty = d->ty;
  901. }
  902. else
  903. fatal("bad op in Ocall rewcall");
  904. n->ty = n->right->ty = d->ty;
  905. d->refs++;
  906. usetype(d->ty);
  907. return d;
  908. }
  909. /*
  910. * annotate the expression with types
  911. */
  912. Ok
  913. echeck(Node *n, int typeok, int isglobal, Node *par)
  914. {
  915. Type *t, *tt;
  916. Node *left, *right, *mod, *nn;
  917. Decl *tg, *id, *callee;
  918. Sym *s;
  919. int max, nocheck;
  920. Ok ok, rok, kidsok;
  921. static int tagopt;
  922. ok.ok = ok.allok = 1;
  923. if(n == nil)
  924. return ok;
  925. /* avoid deep recursions */
  926. if(n->op == Oseq){
  927. for( ; n != nil && n->op == Oseq; n = n->right){
  928. rok = echeck(n->left, typeok == 2, isglobal, n);
  929. ok.ok &= rok.ok;
  930. ok.allok &= rok.allok;
  931. n->ty = tnone;
  932. }
  933. if(n == nil)
  934. return ok;
  935. }
  936. left = n->left;
  937. right = n->right;
  938. nocheck = 0;
  939. if(n->op == Odot || n->op == Omdot || n->op == Ocall || n->op == Oref || n->op == Otagof || n->op == Oindex)
  940. nocheck = 1;
  941. if(n->op != Odas /* special case */
  942. && n->op != Oload) /* can have better error recovery */
  943. ok = echeck(left, nocheck, isglobal, n);
  944. if(n->op != Odas /* special case */
  945. && n->op != Odot /* special check */
  946. && n->op != Omdot /* special check */
  947. && n->op != Ocall /* can have better error recovery */
  948. && n->op != Oindex){
  949. rok = echeck(right, 0, isglobal, n);
  950. ok.ok &= rok.ok;
  951. ok.allok &= rok.allok;
  952. }
  953. if(!ok.ok){
  954. n->ty = terror;
  955. ok.allok = 0;
  956. return ok;
  957. }
  958. switch(n->op){
  959. case Odas:
  960. kidsok = echeck(right, 0, isglobal, n);
  961. if(!kidsok.ok)
  962. right->ty = terror;
  963. if(!isglobal && !dasdecl(left)){
  964. kidsok.ok = 0;
  965. }else if(!specific(right->ty) || !declasinfer(left, right->ty)){
  966. nerror(n, "cannot declare %V from %Q", left, right);
  967. declaserr(left);
  968. kidsok.ok = 0;
  969. }
  970. if(right->ty->kind == Texception)
  971. left->ty = n->ty = mkextuptype(right->ty);
  972. else{
  973. left->ty = n->ty = right->ty;
  974. usedty(n->ty);
  975. }
  976. kidsok.allok &= kidsok.ok;
  977. if (nested() && tmustzero(left->ty))
  978. decltozero(left);
  979. return kidsok;
  980. case Oseq:
  981. case Onothing:
  982. n->ty = tnone;
  983. break;
  984. case Owild:
  985. n->ty = tint;
  986. break;
  987. case Ocast:
  988. t = usetype(n->ty);
  989. n->ty = t;
  990. tt = left->ty;
  991. if(tcompat(t, tt, 0)){
  992. left->ty = t;
  993. break;
  994. }
  995. if(tt->kind == Tarray){
  996. if(tt->tof == tbyte && t == tstring)
  997. break;
  998. }else if(t->kind == Tarray){
  999. if(t->tof == tbyte && tt == tstring)
  1000. break;
  1001. }else if(casttab[tt->kind][t->kind]){
  1002. break;
  1003. }
  1004. nerror(n, "cannot make a %T from %Q", n->ty, left);
  1005. ok.ok = ok.allok = 0;
  1006. return ok;
  1007. case Ochan:
  1008. n->ty = usetype(n->ty);
  1009. if(left && left->ty->kind != Tint){
  1010. nerror(n, "channel size %Q is not an int", left);
  1011. ok.ok = ok.allok = 0;
  1012. return ok;
  1013. }
  1014. break;
  1015. case Oload:
  1016. n->ty = usetype(n->ty);
  1017. kidsok = echeck(left, 0, isglobal, n);
  1018. if(n->ty->kind != Tmodule){
  1019. nerror(n, "cannot load a %T, ", n->ty);
  1020. ok.ok = ok.allok = 0;
  1021. return ok;
  1022. }
  1023. if(!kidsok.allok){
  1024. ok.allok = 0;
  1025. break;
  1026. }
  1027. if(left->ty != tstring){
  1028. nerror(n, "cannot load a module from %Q", left);
  1029. ok.allok = 0;
  1030. break;
  1031. }
  1032. if(n->ty->tof->decl->refs != 0)
  1033. n->ty->tof->decl->refs++;
  1034. n->ty->decl->refs++;
  1035. usetype(n->ty->tof);
  1036. break;
  1037. case Oref:
  1038. t = left->ty;
  1039. if(t->kind != Tadt && t->kind != Tadtpick && t->kind != Tfn && t->kind != Ttuple){
  1040. nerror(n, "cannot make a ref from %Q", left);
  1041. ok.ok = ok.allok = 0;
  1042. return ok;
  1043. }
  1044. if(!tagopt && t->kind == Tadt && t->tags != nil && valistype(left)){
  1045. nerror(n, "instances of ref %V must be qualified with a pick tag", left);
  1046. ok.ok = ok.allok = 0;
  1047. return ok;
  1048. }
  1049. if(t->kind == Tadtpick)
  1050. t->tof = usetype(t->tof);
  1051. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Tref, t, nil));
  1052. break;
  1053. case Oarray:
  1054. max = 0;
  1055. if(right != nil){
  1056. max = assignindices(n);
  1057. if(max < 0){
  1058. ok.ok = ok.allok = 0;
  1059. return ok;
  1060. }
  1061. if(!specific(right->left->ty)){
  1062. nerror(n, "type for array not specific");
  1063. ok.ok = ok.allok = 0;
  1064. return ok;
  1065. }
  1066. n->ty = mktype(&n->src.start, &n->src.stop, Tarray, right->left->ty, nil);
  1067. }
  1068. n->ty = usetype(n->ty);
  1069. if(left->op == Onothing)
  1070. n->left = left = mkconst(&n->left->src, max);
  1071. if(left->ty->kind != Tint){
  1072. nerror(n, "array size %Q is not an int", left);
  1073. ok.ok = ok.allok = 0;
  1074. return ok;
  1075. }
  1076. break;
  1077. case Oelem:
  1078. n->ty = right->ty;
  1079. break;
  1080. case Orange:
  1081. if(left->ty != right->ty
  1082. || left->ty != tint && left->ty != tstring){
  1083. nerror(left, "range %Q to %Q is not an int or string range", left, right);
  1084. ok.ok = ok.allok = 0;
  1085. return ok;
  1086. }
  1087. n->ty = left->ty;
  1088. break;
  1089. case Oname:
  1090. id = n->decl;
  1091. if(id == nil){
  1092. nerror(n, "name with no declaration");
  1093. ok.ok = ok.allok = 0;
  1094. return ok;
  1095. }
  1096. if(id->store == Dunbound){
  1097. s = id->sym;
  1098. id = s->decl;
  1099. if(id == nil)
  1100. id = undefed(&n->src, s);
  1101. /* save a little space */
  1102. s->unbound = nil;
  1103. n->decl = id;
  1104. id->refs++;
  1105. }
  1106. n->ty = id->ty = usetype(id->ty);
  1107. switch(id->store){
  1108. case Dfn:
  1109. case Dglobal:
  1110. case Darg:
  1111. case Dlocal:
  1112. case Dimport:
  1113. case Dfield:
  1114. case Dtag:
  1115. break;
  1116. case Dundef:
  1117. nerror(n, "%s is not declared", id->sym->name);
  1118. id->store = Dwundef;
  1119. ok.ok = ok.allok = 0;
  1120. return ok;
  1121. case Dwundef:
  1122. ok.ok = ok.allok = 0;
  1123. return ok;
  1124. case Dconst:
  1125. if(id->init == nil){
  1126. nerror(n, "%s's value cannot be determined", id->sym->name);
  1127. id->store = Dwundef;
  1128. ok.ok = ok.allok = 0;
  1129. return ok;
  1130. }
  1131. break;
  1132. case Dtype:
  1133. if(typeok)
  1134. break;
  1135. nerror(n, "%K is not a variable", id);
  1136. ok.ok = ok.allok = 0;
  1137. return ok;
  1138. default:
  1139. fatal("echeck: unknown symbol storage");
  1140. }
  1141. if(n->ty == nil){
  1142. nerror(n, "%K's type is not fully defined", id);
  1143. id->store = Dwundef;
  1144. ok.ok = ok.allok = 0;
  1145. return ok;
  1146. }
  1147. if(id->importid != nil && valistype(id->eimport)
  1148. && id->store != Dconst && id->store != Dtype && id->store != Dfn){
  1149. nerror(n, "cannot use %V because %V is a module interface", n, id->eimport);
  1150. ok.ok = ok.allok = 0;
  1151. return ok;
  1152. }
  1153. if(n->ty->kind == Texception && !n->ty->cons && par != nil && par->op != Oraise && par->op != Odot){
  1154. nn = mkn(0, nil, nil);
  1155. *nn = *n;
  1156. n->op = Ocast;
  1157. n->left = nn;
  1158. n->decl = nil;
  1159. n->ty = usetype(mkextuptype(n->ty));
  1160. }
  1161. /* function name as function reference */
  1162. if(id->store == Dfn && (par == nil || (par->op != Odot && par->op != Omdot && par->op != Ocall && par->op != Ofunc)))
  1163. fnref(n, id);
  1164. break;
  1165. case Oconst:
  1166. if(n->ty == nil){
  1167. nerror(n, "no type in %V", n);
  1168. ok.ok = ok.allok = 0;
  1169. return ok;
  1170. }
  1171. break;
  1172. case Oas:
  1173. t = right->ty;
  1174. if(t->kind == Texception)
  1175. t = mkextuptype(t);
  1176. if(!tcompat(left->ty, t, 1)){
  1177. nerror(n, "type clash in %Q = %Q", left, right);
  1178. ok.ok = ok.allok = 0;
  1179. return ok;
  1180. }
  1181. if(t == tany)
  1182. t = left->ty;
  1183. n->ty = t;
  1184. left->ty = t;
  1185. if(t->kind == Tadt && t->tags != nil || t->kind == Tadtpick)
  1186. if(left->ty->kind != Tadtpick || right->ty->kind != Tadtpick)
  1187. nerror(n, "expressions cannot have type %T", t);
  1188. if(left->ty->kind == Texception){
  1189. nerror(n, "cannot assign to an exception");
  1190. ok.ok = ok.allok = 0;
  1191. return ok;
  1192. }
  1193. if(islval(left))
  1194. break;
  1195. ok.ok = ok.allok = 0;
  1196. return ok;
  1197. case Osnd:
  1198. if(left->ty->kind != Tchan){
  1199. nerror(n, "cannot send on %Q", left);
  1200. ok.ok = ok.allok = 0;
  1201. return ok;
  1202. }
  1203. if(!tcompat(left->ty->tof, right->ty, 0)){
  1204. nerror(n, "type clash in %Q <-= %Q", left, right);
  1205. ok.ok = ok.allok = 0;
  1206. return ok;
  1207. }
  1208. t = right->ty;
  1209. if(t == tany)
  1210. t = left->ty->tof;
  1211. n->ty = t;
  1212. break;
  1213. case Orcv:
  1214. t = left->ty;
  1215. if(t->kind == Tarray)
  1216. t = t->tof;
  1217. if(t->kind != Tchan){
  1218. nerror(n, "cannot receive on %Q", left);
  1219. ok.ok = ok.allok = 0;
  1220. return ok;
  1221. }
  1222. if(left->ty->kind == Tarray)
  1223. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Ttuple, nil,
  1224. mkids(&n->src, nil, tint, mkids(&n->src, nil, t->tof, nil))));
  1225. else
  1226. n->ty = t->tof;
  1227. break;
  1228. case Ocons:
  1229. if(right->ty->kind != Tlist && right->ty != tany){
  1230. nerror(n, "cannot :: to %Q", right);
  1231. ok.ok = ok.allok = 0;
  1232. return ok;
  1233. }
  1234. n->ty = right->ty;
  1235. if(right->ty == tany)
  1236. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Tlist, left->ty, nil));
  1237. else if(!tcompat(right->ty->tof, left->ty, 0)){
  1238. t = tparent(right->ty->tof, left->ty);
  1239. if(!tcompat(t, left->ty, 0)){
  1240. nerror(n, "type clash in %Q :: %Q", left, right);
  1241. ok.ok = ok.allok = 0;
  1242. return ok;
  1243. }
  1244. else
  1245. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Tlist, t, nil));
  1246. }
  1247. break;
  1248. case Ohd:
  1249. case Otl:
  1250. if(left->ty->kind != Tlist || left->ty->tof == nil){
  1251. nerror(n, "cannot %O %Q", n->op, left);
  1252. ok.ok = ok.allok = 0;
  1253. return ok;
  1254. }
  1255. if(n->op == Ohd)
  1256. n->ty = left->ty->tof;
  1257. else
  1258. n->ty = left->ty;
  1259. break;
  1260. case Otuple:
  1261. n->ty = usetype(mktype(&n->src.start, &n->src.stop, Ttuple, nil, tuplefields(left)));
  1262. break;
  1263. case Ospawn:
  1264. if(left->op != Ocall || left->left->ty->kind != Tfn && !isfnrefty(left->left->ty)){
  1265. nerror(left, "cannot spawn %V", left);
  1266. ok.ok = ok.allok = 0;
  1267. return ok;
  1268. }
  1269. if(left->ty != tnone){
  1270. nerror(left, "cannot spawn functions which return values, such as %Q", left);
  1271. ok.ok = ok.allok = 0;
  1272. return ok;
  1273. }
  1274. break;
  1275. case Oraise:
  1276. if(left->op == Onothing){
  1277. if(inexcept == nil){
  1278. nerror(n, "%V: empty raise not in exception handler", n);
  1279. ok.ok = ok.allok = 0;
  1280. return ok;
  1281. }
  1282. n->left = dupn(1, &n->src, inexcept);
  1283. break;
  1284. }
  1285. if(left->ty != tstring && left->ty->kind != Texception){
  1286. nerror(n, "%V: raise argument %Q is not a string or exception", n, left);
  1287. ok.ok = ok.allok = 0;
  1288. return ok;
  1289. }
  1290. if((left->op != Ocall || left->left->ty->kind == Tfn) && left->ty->ids != nil && left->ty->cons){
  1291. nerror(n, "too few exception arguments");
  1292. ok.ok = ok.allok = 0;
  1293. return ok;
  1294. }
  1295. break;
  1296. case Ocall:{
  1297. int pure;
  1298. kidsok = echeck(right, 0, isglobal, nil);
  1299. t = left->ty;
  1300. usedty(t);
  1301. pure = 1;
  1302. if(t->kind == Tref){
  1303. pure = 0;
  1304. t = t->tof;
  1305. }
  1306. if(t->kind != Tfn)
  1307. return callcast(n, kidsok.allok, ok.allok);
  1308. n->ty = t->tof;
  1309. if(!kidsok.allok){
  1310. ok.allok = 0;
  1311. break;
  1312. }
  1313. /*
  1314. * get the name to call and any associated module
  1315. */
  1316. mod = nil;
  1317. callee = nil;
  1318. id = nil;
  1319. tt = nil;
  1320. if(left->op == Odot){
  1321. Decl *dd;
  1322. Type *ttt;
  1323. callee = left->right->decl;
  1324. id = callee->dot;
  1325. right = passimplicit(left, right);
  1326. n->right = right;
  1327. tt = left->left->ty;
  1328. if(tt->kind == Tref)
  1329. tt = tt->tof;
  1330. ttt = tt;
  1331. if(tt->kind == Tadtpick)
  1332. ttt = tt->decl->dot->ty;
  1333. dd = ttt->decl;
  1334. while(dd != nil && dd->link != nil)
  1335. dd = dd->link;
  1336. if(dd != nil && dd->timport != nil)
  1337. mod = dd->timport->eimport;
  1338. /*
  1339. * stash the import module under a rock,
  1340. * because we won't be able to get it later
  1341. * after scopes are popped
  1342. */
  1343. left->right->left = mod;
  1344. }else if(left->op == Omdot){
  1345. if(left->right->op == Odot){
  1346. callee = left->right->right->decl;
  1347. right = passimplicit(left->right, right);
  1348. n->right = right;
  1349. tt = left->right->left->ty;
  1350. if(tt->kind == Tref)
  1351. tt = tt->tof;
  1352. }else
  1353. callee = left->right->decl;
  1354. mod = left->left;
  1355. }else if(left->op == Oname){
  1356. callee = left->decl;
  1357. id = callee;
  1358. mod = id->eimport;
  1359. }else if(pure){
  1360. nerror(left, "%V is not a function name", left);
  1361. ok.allok = 0;
  1362. break;
  1363. }
  1364. if(pure && callee == nil)
  1365. fatal("can't find called function: %n", left);
  1366. if(callee != nil && callee->store != Dfn && !isfnref(callee)){
  1367. nerror(left, "%V is not a function or function reference", left);
  1368. ok.allok = 0;
  1369. break;
  1370. }
  1371. if(mod != nil && mod->ty->kind != Tmodule){
  1372. nerror(left, "cannot call %V", left);
  1373. ok.allok = 0;
  1374. break;
  1375. }
  1376. if(mod != nil){
  1377. if(valistype(mod)){
  1378. nerror(left, "cannot call %V because %V is a module interface", left, mod);
  1379. ok.allok = 0;
  1380. break;
  1381. }
  1382. }else if(id != nil && id->dot != nil && !isimpmod(id->dot->sym)){
  1383. nerror(left, "cannot call %V without importing %s from a variable", left, id->sym->name);
  1384. ok.allok = 0;
  1385. break;
  1386. }
  1387. if(mod != nil)
  1388. modrefable(left->ty);
  1389. if(callee != nil && callee->store != Dfn)
  1390. callee = nil;
  1391. if(t->varargs != 0){
  1392. t = mkvarargs(left, right);
  1393. if(left->ty->kind == Tref)
  1394. left->ty = usetype(mktype(&t->src.start, &t->src.stop, Tref, t, nil));
  1395. else
  1396. left->ty = t;
  1397. }
  1398. else if(ispoly(callee) || isfnrefty(left->ty) && left->ty->tof->polys != nil){
  1399. Tpair *tp;
  1400. unifysrc = n->src;
  1401. if(!argncompat(n, t->ids, right))
  1402. ok.allok = 0;
  1403. else if(!tunify(left->ty, calltype(left->ty, right, n->ty), &tp)){
  1404. nerror(n, "function call type mismatch");
  1405. ok.allok = 0;
  1406. }
  1407. else{
  1408. n->ty = usetype(expandtype(n->ty, nil, nil, &tp));
  1409. if(ispoly(callee) && tt != nil && (tt->kind == Tadt || tt->kind == Tadtpick) && (tt->flags&INST))
  1410. callee = rewcall(left, callee);
  1411. n->right = passfns(&n->src, callee, left, right, tt, tp);
  1412. }
  1413. }
  1414. else if(!argcompat(n, t->ids, right))
  1415. ok.allok = 0;
  1416. break;
  1417. }
  1418. case Odot:
  1419. t = left->ty;
  1420. if(t->kind == Tref)
  1421. t = t->tof;
  1422. switch(t->kind){
  1423. case Tadt:
  1424. case Tadtpick:
  1425. case Ttuple:
  1426. case Texception:
  1427. case Tpoly:
  1428. id = namedot(t->ids, right->decl->sym);
  1429. if(id == nil){
  1430. id = namedot(t->tags, right->decl->sym);
  1431. if(id != nil && !valistype(left)){
  1432. nerror(n, "%V is not a type", left);
  1433. ok.ok = ok.allok = 0;
  1434. return ok;
  1435. }
  1436. }
  1437. if(id == nil){
  1438. id = namedot(t->polys, right->decl->sym);
  1439. if(id != nil && !valistype(left)){
  1440. nerror(n, "%V is not a type", left);
  1441. ok.ok = ok.allok = 0;
  1442. return ok;
  1443. }
  1444. }
  1445. if(id == nil && t->kind == Tadtpick)
  1446. id = namedot(t->decl->dot->ty->ids, right->decl->sym);
  1447. if(id == nil){
  1448. for(tg = t->tags; tg != nil; tg = tg->next){
  1449. id = namedot(tg->ty->ids, right->decl->sym);
  1450. if(id != nil)
  1451. break;
  1452. }
  1453. if(id != nil){
  1454. nerror(n, "cannot yet index field %s of %Q", right->decl->sym->name, left);
  1455. ok.ok = ok.allok = 0;
  1456. return ok;
  1457. }
  1458. }
  1459. if(id == nil)
  1460. break;
  1461. if(id->store == Dfield && valistype(left)){
  1462. nerror(n, "%V is not a value", left);
  1463. ok.ok = ok.allok = 0;
  1464. return ok;
  1465. }
  1466. id->ty = validtype(id->ty, t->decl);
  1467. id->ty = usetype(id->ty);
  1468. break;
  1469. default:
  1470. nerror(left, "%Q cannot be qualified with .", left);
  1471. ok.ok = ok.allok = 0;
  1472. return ok;
  1473. }
  1474. if(id == nil){
  1475. nerror(n, "%V is not a member of %Q", right, left);
  1476. ok.ok = ok.allok = 0;
  1477. return ok;
  1478. }
  1479. if(id->ty == tunknown){
  1480. nerror(n, "illegal forward reference to %V", n);
  1481. ok.ok = ok.allok = 0;
  1482. return ok;
  1483. }
  1484. increfs(id);
  1485. right->decl = id;
  1486. n->ty = id->ty;
  1487. if((id->store == Dconst || id->store == Dtag) && hasside(left, 1))
  1488. nwarn(left, "result of expression %Q ignored", left);
  1489. /* function name as function reference */
  1490. if(id->store == Dfn && (par == nil || (par->op != Omdot && par->op != Ocall && par->op != Ofunc)))
  1491. fnref(n, id);
  1492. break;
  1493. case Omdot:
  1494. t = left->ty;
  1495. if(t->kind != Tmodule){
  1496. nerror(left, "%Q cannot be qualified with ->", left);
  1497. ok.ok = ok.allok = 0;
  1498. return ok;
  1499. }
  1500. id = nil;
  1501. if(right->op == Oname){
  1502. id = namedot(t->ids, right->decl->sym);
  1503. }else if(right->op == Odot){
  1504. kidsok = echeck(right, 0, isglobal, n);
  1505. ok.ok = kidsok.ok;
  1506. ok.allok &= kidsok.allok;
  1507. if(!ok.ok){
  1508. ok.allok = 0;
  1509. return ok;
  1510. }
  1511. tt = right->left->ty;
  1512. if(tt->kind == Tref)
  1513. tt = tt->tof;
  1514. if(right->ty->kind == Tfn
  1515. && tt->kind == Tadt
  1516. && tt->decl->dot == t->decl)
  1517. id = right->right->decl;
  1518. }
  1519. if(id == nil){
  1520. nerror(n, "%V is not a member of %Q", right, left);
  1521. ok.ok = ok.allok = 0;
  1522. return ok;
  1523. }
  1524. if(id->store != Dconst && id->store != Dtype && id->store != Dtag){
  1525. if(valistype(left)){
  1526. nerror(n, "%V is not a value", left);
  1527. ok.ok = ok.allok = 0;
  1528. return ok;
  1529. }
  1530. }else if(hasside(left, 1))
  1531. nwarn(left, "result of expression %Q ignored", left);
  1532. if(!typeok && id->store == Dtype){
  1533. nerror(n, "%V is a type, not a value", n);
  1534. ok.ok = ok.allok = 0;
  1535. return ok;
  1536. }
  1537. if(id->ty == tunknown){
  1538. nerror(n, "illegal forward reference to %V", n);
  1539. ok.ok = ok.allok = 0;
  1540. return ok;
  1541. }
  1542. id->refs++;
  1543. right->decl = id;
  1544. n->ty = id->ty = usetype(id->ty);
  1545. if(id->store == Dglobal)
  1546. modrefable(id->ty);
  1547. /* function name as function reference */
  1548. if(id->store == Dfn && (par == nil || (par->op != Ocall && par->op != Ofunc)))
  1549. fnref(n, id);
  1550. break;
  1551. case Otagof:
  1552. n->ty = tint;
  1553. t = left->ty;
  1554. if(t->kind == Tref)
  1555. t = t->tof;
  1556. id = nil;
  1557. switch(left->op){
  1558. case Oname:
  1559. id = left->decl;
  1560. break;
  1561. case Odot:
  1562. id = left->right->decl;
  1563. break;
  1564. case Omdot:
  1565. if(left->right->op == Odot)
  1566. id = left->right->right->decl;
  1567. break;
  1568. }
  1569. if(id != nil && id->store == Dtag
  1570. || id != nil && id->store == Dtype && t->kind == Tadt && t->tags != nil)
  1571. n->decl = id;
  1572. else if(t->kind == Tadt && t->tags != nil || t->kind == Tadtpick)
  1573. n->decl = nil;
  1574. else{
  1575. nerror(n, "cannot get the tag value for %Q", left);
  1576. ok.ok = 1;
  1577. ok.allok = 0;
  1578. return ok;
  1579. }
  1580. break;
  1581. case Oind:
  1582. t = left->ty;
  1583. if(t->kind != Tref || (t->tof->kind != Tadt && t->tof->kind != Tadtpick && t->tof->kind != Ttuple)){
  1584. nerror(n, "cannot * %Q", left);
  1585. ok.ok = ok.allok = 0;
  1586. return ok;
  1587. }
  1588. n->ty = t->tof;
  1589. for(tg = t->tof->tags; tg != nil; tg = tg->next)
  1590. tg->ty->tof = usetype(tg->ty->tof);
  1591. break;
  1592. case Oindex:
  1593. if(valistype(left)){
  1594. tagopt = 1;
  1595. kidsok = echeck(right, 2, isglobal, n);
  1596. tagopt = 0;
  1597. if(!kidsok.allok){
  1598. ok.ok = ok.allok = 0;
  1599. return ok;
  1600. }
  1601. if((t = exptotype(n)) == nil){
  1602. nerror(n, "%V is not a type list", right);
  1603. ok.ok = ok.allok = 0;
  1604. return ok;
  1605. }
  1606. if(!typeok){
  1607. nerror(n, "%Q is not a variable", left);
  1608. ok.ok = ok.allok = 0;
  1609. return ok;
  1610. }
  1611. *n = *(n->left);
  1612. n->ty = usetype(t);
  1613. break;
  1614. }
  1615. if(0 && right->op == Oseq){ /* a[e1, e2, ...] */
  1616. /* array creation to do before we allow this */
  1617. rewind(n);
  1618. return echeck(n, typeok, isglobal, par);
  1619. }
  1620. t = left->ty;
  1621. kidsok = echeck(right, 0, isglobal, n);
  1622. if(t->kind != Tarray && t != tstring){
  1623. nerror(n, "cannot index %Q", left);
  1624. ok.ok = ok.allok = 0;
  1625. return ok;
  1626. }
  1627. if(t == tstring){
  1628. n->op = Oinds;
  1629. n->ty = tint;
  1630. }else{
  1631. n->ty = t->tof;
  1632. }
  1633. if(!kidsok.allok){
  1634. ok.allok = 0;
  1635. break;
  1636. }
  1637. if(right->ty != tint){
  1638. nerror(n, "cannot index %Q with %Q", left, right);
  1639. ok.allok = 0;
  1640. break;
  1641. }
  1642. break;
  1643. case Oslice:
  1644. t = n->ty = left->ty;
  1645. if(t->kind != Tarray && t != tstring){
  1646. nerror(n, "cannot slice %Q with '%v:%v'", left, right->left, right->right);
  1647. ok.ok = ok.allok = 0;
  1648. return ok;
  1649. }
  1650. if(right->left->ty != tint && right->left->op != Onothing
  1651. || right->right->ty != tint && right->right->op != Onothing){
  1652. nerror(n, "cannot slice %Q with '%v:%v'", left, right->left, right->right);
  1653. ok.allok = 0;
  1654. return ok;
  1655. }
  1656. break;
  1657. case Olen:
  1658. t = left->ty;
  1659. n->ty = tint;
  1660. if(t->kind != Tarray && t->kind != Tlist && t != tstring){
  1661. nerror(n, "len requires an array, string or list in %Q", left);
  1662. ok.allok = 0;
  1663. return ok;
  1664. }
  1665. break;
  1666. case Ocomp:
  1667. case Onot:
  1668. case Oneg:
  1669. n->ty = left->ty;
  1670. usedty(n->ty);
  1671. switch(left->ty->kind){
  1672. case Tint:
  1673. return ok;
  1674. case Treal:
  1675. case Tfix:
  1676. if(n->op == Oneg)
  1677. return ok;
  1678. break;
  1679. case Tbig:
  1680. case Tbyte:
  1681. if(n->op == Oneg || n->op == Ocomp)
  1682. return ok;
  1683. break;
  1684. }
  1685. nerror(n, "cannot apply %O to %Q", n->op, left);
  1686. ok.ok = ok.allok = 0;
  1687. return ok;
  1688. case Oinc:
  1689. case Odec:
  1690. case Opreinc:
  1691. case Opredec:
  1692. n->ty = left->ty;
  1693. switch(left->ty->kind){
  1694. case Tint:
  1695. case Tbig:
  1696. case Tbyte:
  1697. case Treal:
  1698. break;
  1699. default:
  1700. nerror(n, "cannot apply %O to %Q", n->op, left);
  1701. ok.ok = ok.allok = 0;
  1702. return ok;
  1703. }
  1704. if(islval(left))
  1705. break;
  1706. ok.ok = ok.allok = 0;
  1707. return ok;
  1708. case Oadd:
  1709. case Odiv:
  1710. case Omul:
  1711. case Osub:
  1712. if(mathchk(n, 1))
  1713. break;
  1714. ok.ok = ok.allok = 0;
  1715. return ok;
  1716. case Oexp:
  1717. case Oexpas:
  1718. n->ty = left->ty;
  1719. if(n->ty != tint && n->ty != tbig && n->ty != treal){
  1720. nerror(n, "exponend %Q is not int, big or real", left);
  1721. ok.ok = ok.allok = 0;
  1722. return ok;
  1723. }
  1724. if(right->ty != tint){
  1725. nerror(n, "exponent %Q is not int", right);
  1726. ok.ok = ok.allok = 0;
  1727. return ok;
  1728. }
  1729. if(n->op == Oexpas && !islval(left)){
  1730. ok.ok = ok.allok = 0;
  1731. return ok;
  1732. }
  1733. break;
  1734. case Olsh:
  1735. case Orsh:
  1736. if(shiftchk(n))
  1737. break;
  1738. ok.ok = ok.allok = 0;
  1739. return ok;
  1740. case Oandand:
  1741. case Ooror:
  1742. if(left->ty != tint){
  1743. nerror(n, "%O's left operand is not an int: %Q", n->op, left);
  1744. ok.allok = 0;
  1745. }
  1746. if(right->ty != tint){
  1747. nerror(n, "%O's right operand is not an int: %Q", n->op, right);
  1748. ok.allok = 0;
  1749. }
  1750. n->ty = tint;
  1751. break;
  1752. case Oand:
  1753. case Omod:
  1754. case Oor:
  1755. case Oxor:
  1756. if(mathchk(n, 0))
  1757. break;
  1758. ok.ok = ok.allok = 0;
  1759. return ok;
  1760. case Oaddas:
  1761. case Odivas:
  1762. case Omulas:
  1763. case Osubas:
  1764. if(mathchk(n, 1) && islval(left))
  1765. break;
  1766. ok.ok = ok.allok = 0;
  1767. return ok;
  1768. case Olshas:
  1769. case Orshas:
  1770. if(shiftchk(n) && islval(left))
  1771. break;
  1772. ok.ok = ok.allok = 0;
  1773. return ok;
  1774. case Oandas:
  1775. case Omodas:
  1776. case Oxoras:
  1777. case Ooras:
  1778. if(mathchk(n, 0) && islval(left))
  1779. break;
  1780. ok.ok = ok.allok = 0;
  1781. return ok;
  1782. case Olt:
  1783. case Oleq:
  1784. case Ogt:
  1785. case Ogeq:
  1786. if(!mathchk(n, 1)){
  1787. ok.ok = ok.allok = 0;
  1788. return ok;
  1789. }
  1790. n->ty = tint;
  1791. break;
  1792. case Oeq:
  1793. case Oneq:
  1794. switch(left->ty->kind){
  1795. case Tint:
  1796. case Tbig:
  1797. case Tbyte:
  1798. case Treal:
  1799. case Tstring:
  1800. case Tref:
  1801. case Tlist:
  1802. case Tarray:
  1803. case Tchan:
  1804. case Tany:
  1805. case Tmodule:
  1806. case Tfix:
  1807. case Tpoly:
  1808. if(!tcompat(left->ty, right->ty, 0) && !tcompat(right->ty, left->ty, 0))
  1809. break;
  1810. t = left->ty;
  1811. if(t == tany)
  1812. t = right->ty;
  1813. if(t == tany)
  1814. t = tint;
  1815. if(left->ty == tany)
  1816. left->ty = t;
  1817. if(right->ty == tany)
  1818. right->ty = t;
  1819. n->ty = tint;
  1820. return ok;
  1821. }
  1822. nerror(n, "cannot compare %Q to %Q", left, right);
  1823. usedty(n->ty);
  1824. ok.ok = ok.allok = 0;
  1825. return ok;
  1826. case Otype:
  1827. if(!typeok){
  1828. nerror(n, "%Q is not a variable", n);
  1829. ok.ok = ok.allok = 0;
  1830. return ok;
  1831. }
  1832. n->ty = usetype(n->ty);
  1833. break;
  1834. default:
  1835. fatal("unknown op in typecheck: %O", n->op);
  1836. }
  1837. usedty(n->ty);
  1838. return ok;
  1839. }
  1840. /*
  1841. * n is syntactically a call, but n->left is not a fn
  1842. * check if it's the contructor for an adt
  1843. */
  1844. Ok
  1845. callcast(Node *n, int kidsok, int allok)
  1846. {
  1847. Node *left, *right;
  1848. Decl *id;
  1849. Type *t, *tt;
  1850. Ok ok;
  1851. left = n->left;
  1852. right = n->right;
  1853. id = nil;
  1854. switch(left->op){
  1855. case Oname:
  1856. id = left->decl;
  1857. break;
  1858. case Omdot:
  1859. if(left->right->op == Odot)
  1860. id = left->right->right->decl;
  1861. else
  1862. id = left->right->decl;
  1863. break;
  1864. case Odot:
  1865. id = left->right->decl;
  1866. break;
  1867. }
  1868. /*
  1869. (chan of int)(nil) looks awkward since both sets of brackets needed
  1870. if(id == nil && right != nil && right->right == nil && (t = exptotype(left)) != nil){
  1871. n->op = Ocast;
  1872. n->left = right->left;
  1873. n->right = nil;
  1874. n->ty = t;
  1875. return echeck(n, 0, 0, nil);
  1876. }
  1877. */
  1878. if(id == nil || (id->store != Dtype && id->store != Dtag && id->ty->kind != Texception)){
  1879. nerror(left, "%V is not a function or type name", left);
  1880. ok.ok = ok.allok = 0;
  1881. return ok;
  1882. }
  1883. if(id->store == Dtag)
  1884. return tagcast(n, left, right, id, kidsok, allok);
  1885. t = left->ty;
  1886. n->ty = t;
  1887. if(!kidsok){
  1888. ok.ok = 1;
  1889. ok.allok = 0;
  1890. return ok;
  1891. }
  1892. if(t->kind == Tref)
  1893. t = t->tof;
  1894. tt = mktype(&n->src.start, &n->src.stop, Ttuple, nil, tuplefields(right));
  1895. if(t->kind == Tadt && tcompat(t, tt, 1)){
  1896. if(right == nil)
  1897. *n = *n->left;
  1898. ok.ok = 1;
  1899. ok.allok = allok;
  1900. return ok;
  1901. }
  1902. /* try an exception with args */
  1903. tt = mktype(&n->src.start, &n->src.stop, Texception, nil, tuplefields(right));
  1904. tt->cons = 1;
  1905. if(t->kind == Texception && t->cons && tcompat(t, tt, 1)){
  1906. if(right == nil)
  1907. *n = *n->left;
  1908. ok.ok = 1;
  1909. ok.allok = allok;
  1910. return ok;
  1911. }
  1912. /* try a cast */
  1913. if(t->kind != Texception && right != nil && right->right == nil){ /* Oseq but single expression */
  1914. right = right->left;
  1915. n->op = Ocast;
  1916. n->left = right;
  1917. n->right = nil;
  1918. n->ty = mkidtype(&n->src, id->sym);
  1919. return echeck(n, 0, 0, nil);
  1920. }
  1921. nerror(left, "cannot make a %V from '(%v)'", left, right);
  1922. ok.ok = ok.allok = 0;
  1923. return ok;
  1924. }
  1925. Ok
  1926. tagcast(Node *n, Node *left, Node *right, Decl *id, int kidsok, int allok)
  1927. {
  1928. Type *tt;
  1929. Ok ok;
  1930. left->ty = id->ty;
  1931. if(left->op == Omdot)
  1932. left->right->ty = id->ty;
  1933. n->ty = id->ty;
  1934. if(!kidsok){
  1935. ok.ok = 1;
  1936. ok.allok = 0;
  1937. return ok;
  1938. }
  1939. id->ty->tof = usetype(id->ty->tof);
  1940. if(right != nil)
  1941. right->ty = id->ty->tof;
  1942. tt = mktype(&n->src.start, &n->src.stop, Ttuple, nil, mkids(&nosrc, nil, tint, tuplefields(right)));
  1943. tt->ids->store = Dfield;
  1944. if(tcompat(id->ty->tof, tt, 1)){
  1945. ok.ok = 1;
  1946. ok.allok = allok;
  1947. return ok;
  1948. }
  1949. nerror(left, "cannot make a %V from '(%v)'", left, right);
  1950. ok.ok = ok.allok = 0;
  1951. return ok;
  1952. }
  1953. int
  1954. valistype(Node *n)
  1955. {
  1956. switch(n->op){
  1957. case Oname:
  1958. if(n->decl->store == Dtype)
  1959. return 1;
  1960. break;
  1961. case Omdot:
  1962. return valistype(n->right);
  1963. }
  1964. return 0;
  1965. }
  1966. int
  1967. islval(Node *n)
  1968. {
  1969. int s;
  1970. s = marklval(n);
  1971. if(s == 1)
  1972. return 1;
  1973. if(s == 0)
  1974. nerror(n, "cannot assign to %V", n);
  1975. else
  1976. circlval(n, n);
  1977. return 0;
  1978. }
  1979. /*
  1980. * check to see if n is an lval
  1981. * mark the lval name as set
  1982. */
  1983. int
  1984. marklval(Node *n)
  1985. {
  1986. Decl *id;
  1987. Node *nn;
  1988. int s;
  1989. if(n == nil)
  1990. return 0;
  1991. switch(n->op){
  1992. case Oname:
  1993. return storespace[n->decl->store] && n->ty->kind != Texception; /*ZZZZ && n->decl->tagged == nil;*/
  1994. case Odot:
  1995. if(n->right->decl->store != Dfield)
  1996. return 0;
  1997. if(n->right->decl->cycle && !n->right->decl->cyc)
  1998. return -1;
  1999. if(n->left->ty->kind != Tref && marklval(n->left) == 0)
  2000. nwarn(n, "assignment to %Q ignored", n);
  2001. return 1;
  2002. case Omdot:
  2003. if(n->right->decl->store == Dglobal)
  2004. return 1;
  2005. return 0;
  2006. case Oind:
  2007. for(id = n->ty->ids; id != nil; id = id->next)
  2008. if(id->cycle && !id->cyc)
  2009. return -1;
  2010. return 1;
  2011. case Oslice:
  2012. if(n->right->right->op != Onothing || n->ty == tstring)
  2013. return 0;
  2014. return 1;
  2015. case Oinds:
  2016. /*
  2017. * make sure we don't change a string constant
  2018. */
  2019. switch(n->left->op){
  2020. case Oconst:
  2021. return 0;
  2022. case Oname:
  2023. return storespace[n->left->decl->store];
  2024. case Odot:
  2025. case Omdot:
  2026. if(n->left->right->decl != nil)
  2027. return storespace[n->left->right->decl->store];
  2028. break;
  2029. }
  2030. return 1;
  2031. case Oindex:
  2032. case Oindx:
  2033. return 1;
  2034. case Otuple:
  2035. for(nn = n->left; nn != nil; nn = nn->right){
  2036. s = marklval(nn->left);
  2037. if(s != 1)
  2038. return s;
  2039. }
  2040. return 1;
  2041. default:
  2042. return 0;
  2043. }
  2044. }
  2045. /*
  2046. * n has a circular field assignment.
  2047. * find it and print an error message.
  2048. */
  2049. int
  2050. circlval(Node *n, Node *lval)
  2051. {
  2052. Decl *id;
  2053. Node *nn;
  2054. int s;
  2055. if(n == nil)
  2056. return 0;
  2057. switch(n->op){
  2058. case Oname:
  2059. break;
  2060. case Odot:
  2061. if(oldcycles && n->right->decl->cycle && !n->right->decl->cyc){
  2062. nerror(lval, "cannot assign to %V because field '%s' of %V could complete a cycle to %V",
  2063. lval, n->right->decl->sym->name, n->left, n->left);
  2064. return -1;
  2065. }
  2066. return 1;
  2067. case Oind:
  2068. if(!oldcycles)
  2069. return 1;
  2070. for(id = n->ty->ids; id != nil; id = id->next){
  2071. if(id->cycle && !id->cyc){
  2072. nerror(lval, "cannot assign to %V because field '%s' of %V could complete a cycle to %V",
  2073. lval, id->sym->name, n, n);
  2074. return -1;
  2075. }
  2076. }
  2077. return 1;
  2078. case Oslice:
  2079. if(n->right->right->op != Onothing || n->ty == tstring)
  2080. return 0;
  2081. return 1;
  2082. case Oindex:
  2083. case Oinds:
  2084. case Oindx:
  2085. return 1;
  2086. case Otuple:
  2087. for(nn = n->left; nn != nil; nn = nn->right){
  2088. s = circlval(nn->left, lval);
  2089. if(s != 1)
  2090. return s;
  2091. }
  2092. return 1;
  2093. default:
  2094. return 0;
  2095. }
  2096. return 0;
  2097. }
  2098. int
  2099. mathchk(Node *n, int realok)
  2100. {
  2101. Type *tr, *tl;
  2102. tl = n->left->ty;
  2103. tr = n->right->ty;
  2104. if(tr != tl && !tequal(tl, tr)){
  2105. nerror(n, "type clash in %Q %O %Q", n->left, n->op, n->right);
  2106. return 0;
  2107. }
  2108. n->ty = tr;
  2109. switch(tr->kind){
  2110. case Tint:
  2111. case Tbig:
  2112. case Tbyte:
  2113. return 1;
  2114. case Tstring:
  2115. switch(n->op){
  2116. case Oadd:
  2117. case Oaddas:
  2118. case Ogt:
  2119. case Ogeq:
  2120. case Olt:
  2121. case Oleq:
  2122. return 1;
  2123. }
  2124. break;
  2125. case Treal:
  2126. case Tfix:
  2127. if(realok)
  2128. return 1;
  2129. break;
  2130. }
  2131. nerror(n, "cannot %O %Q and %Q", n->op, n->left, n->right);
  2132. return 0;
  2133. }
  2134. int
  2135. shiftchk(Node *n)
  2136. {
  2137. Node *left, *right;
  2138. right = n->right;
  2139. left = n->left;
  2140. n->ty = left->ty;
  2141. switch(n->ty->kind){
  2142. case Tint:
  2143. case Tbyte:
  2144. case Tbig:
  2145. if(right->ty->kind != Tint){
  2146. nerror(n, "shift %Q is not an int", right);
  2147. return 0;
  2148. }
  2149. return 1;
  2150. }
  2151. nerror(n, "cannot %Q %O %Q", left, n->op, right);
  2152. return 0;
  2153. }
  2154. /*
  2155. * check for any tany's in t
  2156. */
  2157. int
  2158. specific(Type *t)
  2159. {
  2160. Decl *d;
  2161. if(t == nil)
  2162. return 0;
  2163. switch(t->kind){
  2164. case Terror:
  2165. case Tnone:
  2166. case Tint:
  2167. case Tbig:
  2168. case Tstring:
  2169. case Tbyte:
  2170. case Treal:
  2171. case Tfn:
  2172. case Tadt:
  2173. case Tadtpick:
  2174. case Tmodule:
  2175. case Tfix:
  2176. return 1;
  2177. case Tany:
  2178. return 0;
  2179. case Tpoly:
  2180. return 1;
  2181. case Tref:
  2182. case Tlist:
  2183. case Tarray:
  2184. case Tchan:
  2185. return specific(t->tof);
  2186. case Ttuple:
  2187. case Texception:
  2188. for(d = t->ids; d != nil; d = d->next)
  2189. if(!specific(d->ty))
  2190. return 0;
  2191. return 1;
  2192. }
  2193. fatal("unknown type %T in specific", t);
  2194. return 0;
  2195. }
  2196. /*
  2197. * infer the type of all variable in n from t
  2198. * n is the left-hand exp of a := exp
  2199. */
  2200. int
  2201. declasinfer(Node *n, Type *t)
  2202. {
  2203. Decl *ids;
  2204. int ok;
  2205. if(t->kind == Texception){
  2206. if(t->cons)
  2207. return 0;
  2208. t = mkextuptype(t);
  2209. }
  2210. switch(n->op){
  2211. case Otuple:
  2212. if(t->kind != Ttuple && t->kind != Tadt && t->kind != Tadtpick)
  2213. return 0;
  2214. ok = 1;
  2215. n->ty = t;
  2216. n = n->left;
  2217. ids = t->ids;
  2218. if(t->kind == Tadtpick)
  2219. ids = t->tof->ids->next;
  2220. for(; n != nil && ids != nil; ids = ids->next){
  2221. if(ids->store != Dfield)
  2222. continue;
  2223. ok &= declasinfer(n->left, ids->ty);
  2224. n = n->right;
  2225. }
  2226. for(; ids != nil; ids = ids->next)
  2227. if(ids->store == Dfield)
  2228. break;
  2229. if(n != nil || ids != nil)
  2230. return 0;
  2231. return ok;
  2232. case Oname:
  2233. topvartype(t, n->decl, 0, 0);
  2234. if(n->decl == nildecl)
  2235. return 1;
  2236. n->decl->ty = t;
  2237. n->ty = t;
  2238. shareloc(n->decl);
  2239. return 1;
  2240. }
  2241. fatal("unknown op %n in declasinfer", n);
  2242. return 0;
  2243. }
  2244. /*
  2245. * an error occured in declaring n;
  2246. * set all decl identifiers to Dwundef
  2247. * so further errors are squashed.
  2248. */
  2249. void
  2250. declaserr(Node *n)
  2251. {
  2252. switch(n->op){
  2253. case Otuple:
  2254. for(n = n->left; n != nil; n = n->right)
  2255. declaserr(n->left);
  2256. return;
  2257. case Oname:
  2258. if(n->decl != nildecl)
  2259. n->decl->store = Dwundef;
  2260. return;
  2261. }
  2262. fatal("unknown op %n in declaserr", n);
  2263. }
  2264. int
  2265. argcompat(Node *n, Decl *f, Node *a)
  2266. {
  2267. for(; a != nil; a = a->right){
  2268. if(f == nil){
  2269. nerror(n, "%V: too many function arguments", n->left);
  2270. return 0;
  2271. }
  2272. if(!tcompat(f->ty, a->left->ty, 0)){
  2273. nerror(n, "%V: argument type mismatch: expected %T saw %Q",
  2274. n->left, f->ty, a->left);
  2275. return 0;
  2276. }
  2277. if(a->left->ty == tany)
  2278. a->left->ty = f->ty;
  2279. f = f->next;
  2280. }
  2281. if(f != nil){
  2282. nerror(n, "%V: too few function arguments", n->left);
  2283. return 0;
  2284. }
  2285. return 1;
  2286. }
  2287. /*
  2288. * fn is Odot(adt, methid)
  2289. * pass adt implicitly if needed
  2290. * if not, any side effect of adt will be ignored
  2291. */
  2292. Node*
  2293. passimplicit(Node *fn, Node *args)
  2294. {
  2295. Node *n;
  2296. Type *t;
  2297. t = fn->ty;
  2298. n = fn->left;
  2299. if(t->ids == nil || !t->ids->implicit){
  2300. if(!isfnrefty(t) && hasside(n, 1))
  2301. nwarn(fn, "result of expression %V ignored", n);
  2302. return args;
  2303. }
  2304. if(n->op == Oname && n->decl->store == Dtype){
  2305. nerror(n, "%V is a type and cannot be a self argument", n);
  2306. n = mkn(Onothing, nil, nil);
  2307. n->src = fn->src;
  2308. n->ty = t->ids->ty;
  2309. }
  2310. args = mkn(Oseq, n, args);
  2311. args->src = n->src;
  2312. return args;
  2313. }
  2314. static int
  2315. mem(Type *t, Decl *d)
  2316. {
  2317. for( ; d != nil; d = d->next)
  2318. if(d->ty == t) /* was if(d->ty == t || tequal(d->ty, t)) */
  2319. return 1;
  2320. return 0;
  2321. }
  2322. static int
  2323. memp(Type *t, Decl *f)
  2324. {
  2325. return mem(t, f->ty->polys) || mem(t, encpolys(f));
  2326. }
  2327. static void
  2328. passfns0(Src *src, Decl *fn, Node *args0, Node **args, Node **a, Tpair *tp, Decl *polys)
  2329. {
  2330. Decl *id, *idt, *idf, *dot;
  2331. Type *tt;
  2332. Sym *sym;
  2333. Node *n, *na, *mod;
  2334. Tpair *p;
  2335. if(debug['w']){
  2336. print("polys: ");
  2337. for(id=polys; id!=nil; id=id->next) print("%s ", id->sym->name);
  2338. print("\nmap: ");
  2339. for(p=tp; p!=nil; p=p->nxt) print("%T -> %T ", p->t1, p->t2);
  2340. print("\n");
  2341. }
  2342. for(idt = polys; idt != nil; idt = idt->next){
  2343. tt = valtmap(idt->ty, tp);
  2344. if(tt->kind == Tpoly && fndec != nil && !memp(tt, fndec))
  2345. error(src->start, "cannot determine the instantiated type of %T", tt);
  2346. for(idf = idt->ty->ids; idf != nil; idf = idf->next){
  2347. sym = idf->sym;
  2348. id = fnlookup(sym, tt, &mod);
  2349. while(id != nil && id->link != nil)
  2350. id = id->link;
  2351. if(debug['v']) print("fnlookup: %p\n", id);
  2352. if(id == nil) /* error flagged already */
  2353. continue;
  2354. id->refs++;
  2355. id->caninline = -1;
  2356. if(tt->kind == Tmodule){ /* mod an actual parameter */
  2357. for(;;){
  2358. if(args0 != nil && tequal(tt, args0->left->ty)){
  2359. mod = args0->left;
  2360. break;
  2361. }
  2362. if(args0 == nil)
  2363. break;
  2364. args0 = args0->right;
  2365. }
  2366. }
  2367. if(mod == nil && (dot = module(id)) != nil && !isimpmod(dot->sym))
  2368. error(src->start, "cannot use %s without importing %s from a variable", id->sym->name, id->dot->sym->name);
  2369. if(debug['U']) print("fp: %s %s %s\n", fn->sym->name, mod ? mod->decl->sym->name : "nil", id->sym->name);
  2370. n = mkn(Ofnptr, mod, mkdeclname(src, id));
  2371. n->src = *src;
  2372. n->decl = fn;
  2373. if(tt->kind == Tpoly)
  2374. n->flags = FNPTRA;
  2375. else
  2376. n->flags = 0;
  2377. na = mkn(Oseq, n, nil);
  2378. if(*a == nil)
  2379. *args = na;
  2380. else
  2381. (*a)->right = na;
  2382. n = mkn(Ofnptr, mod, mkdeclname(src, id));
  2383. n->src = *src;
  2384. n->decl = fn;
  2385. if(tt->kind == Tpoly)
  2386. n->flags = FNPTRA|FNPTR2;
  2387. else
  2388. n->flags = FNPTR2;
  2389. *a = na->right = mkn(Oseq, n, nil);
  2390. }
  2391. if(args0 != nil)
  2392. args0 = args0->right;
  2393. }
  2394. }
  2395. Node*
  2396. passfns(Src *src, Decl *fn, Node *left, Node *args, Type *adt, Tpair *tp)
  2397. {
  2398. Node *a, *args0;
  2399. a = nil;
  2400. args0 = args;
  2401. if(args != nil)
  2402. for(a = args; a->right != nil; a = a->right)
  2403. ;
  2404. passfns0(src, fn, args0, &args, &a, tp, ispoly(fn) ? fn->ty->polys : left->ty->tof->polys);
  2405. if(adt != nil)
  2406. passfns0(src, fn, args0, &args, &a, adt->u.tmap, ispoly(fn) ? encpolys(fn) : nil);
  2407. return args;
  2408. }
  2409. /*
  2410. * check the types for a function with a variable number of arguments
  2411. * last typed argument must be a constant string, and must use the
  2412. * print format for describing arguments.
  2413. */
  2414. Type*
  2415. mkvarargs(Node *n, Node *args)
  2416. {
  2417. Node *s, *a;
  2418. Decl *f, *last, *va;
  2419. Type *nt;
  2420. nt = copytypeids(n->ty);
  2421. n->ty = nt;
  2422. f = n->ty->ids;
  2423. last = nil;
  2424. if(f == nil){
  2425. nerror(n, "%V's type is illegal", n);
  2426. return nt;
  2427. }
  2428. s = args;
  2429. for(a = args; a != nil; a = a->right){
  2430. if(f == nil)
  2431. break;
  2432. if(!tcompat(f->ty, a->left->ty, 0)){
  2433. nerror(n, "%V: argument type mismatch: expected %T saw %Q",
  2434. n, f->ty, a->left);
  2435. return nt;
  2436. }
  2437. if(a->left->ty == tany)
  2438. a->left->ty = f->ty;
  2439. last = f;
  2440. f = f->next;
  2441. s = a;
  2442. }
  2443. if(f != nil){
  2444. nerror(n, "%V: too few function arguments", n);
  2445. return nt;
  2446. }
  2447. s->left = fold(s->left);
  2448. s = s->left;
  2449. if(s->ty != tstring || s->op != Oconst){
  2450. nerror(args, "%V: format argument %Q is not a string constant", n, s);
  2451. return nt;
  2452. }
  2453. fmtcheck(n, s, a);
  2454. va = tuplefields(a);
  2455. if(last == nil)
  2456. nt->ids = va;
  2457. else
  2458. last->next = va;
  2459. return nt;
  2460. }
  2461. /*
  2462. * check that a print style format string matches its arguments
  2463. */
  2464. void
  2465. fmtcheck(Node *f, Node *fmtarg, Node *va)
  2466. {
  2467. Sym *fmt;
  2468. Rune r;
  2469. char *s, flags[10];
  2470. int i, c, n1, n2, dot, verb, flag, ns, lens, fmtstart;
  2471. Type *ty;
  2472. fmt = fmtarg->decl->sym;
  2473. s = fmt->name;
  2474. lens = fmt->len;
  2475. ns = 0;
  2476. while(ns < lens){
  2477. c = s[ns++];
  2478. if(c != '%')
  2479. continue;
  2480. verb = -1;
  2481. n1 = 0;
  2482. n2 = 0;
  2483. dot = 0;
  2484. flag = 0;
  2485. fmtstart = ns - 1;
  2486. while(ns < lens && verb < 0){
  2487. c = s[ns++];
  2488. switch(c){
  2489. default:
  2490. chartorune(&r, &s[ns-1]);
  2491. nerror(f, "%V: invalid character %C in format '%.*s'", f, r, ns-fmtstart, &s[fmtstart]);
  2492. return;
  2493. case '.':
  2494. if(dot){
  2495. nerror(f, "%V: invalid format '%.*s'", f, ns-fmtstart, &s[fmtstart]);
  2496. return;
  2497. }
  2498. n1 = 1;
  2499. dot = 1;
  2500. continue;
  2501. case '*':
  2502. if(!n1)
  2503. n1 = 1;
  2504. else if(!n2 && dot)
  2505. n2 = 1;
  2506. else{
  2507. nerror(f, "%V: invalid format '%.*s'", f, ns-fmtstart, &s[fmtstart]);
  2508. return;
  2509. }
  2510. if(va == nil){
  2511. nerror(f, "%V: too few arguments for format '%.*s'",
  2512. f, ns-fmtstart, &s[fmtstart]);
  2513. return;
  2514. }
  2515. if(va->left->ty->kind != Tint){
  2516. nerror(f, "%V: format '%.*s' incompatible with argument %Q",
  2517. f, ns-fmtstart, &s[fmtstart], va->left);
  2518. return;
  2519. }
  2520. va = va->right;
  2521. break;
  2522. case '0': case '1': case '2': case '3': case '4':
  2523. case '5': case '6': case '7': case '8': case '9':
  2524. while(ns < lens && s[ns] >= '0' && s[ns] <= '9')
  2525. ns++;
  2526. if(!n1)
  2527. n1 = 1;
  2528. else if(!n2 && dot)
  2529. n2 = 1;
  2530. else{
  2531. nerror(f, "%V: invalid format '%.*s'", f, ns-fmtstart, &s[fmtstart]);
  2532. return;
  2533. }
  2534. break;
  2535. case '+':
  2536. case '-':
  2537. case '#':
  2538. case ',':
  2539. case 'b':
  2540. case 'u':
  2541. for(i = 0; i < flag; i++){
  2542. if(flags[i] == c){
  2543. nerror(f, "%V: duplicate flag %c in format '%.*s'",
  2544. f, c, ns-fmtstart, &s[fmtstart]);
  2545. return;
  2546. }
  2547. }
  2548. flags[flag++] = c;
  2549. if(flag >= sizeof flags){
  2550. nerror(f, "too many flags in format '%.*s'", ns-fmtstart, &s[fmtstart]);
  2551. return;
  2552. }
  2553. break;
  2554. case '%':
  2555. case 'r':
  2556. verb = Tnone;
  2557. break;
  2558. case 'H':
  2559. verb = Tany;
  2560. break;
  2561. case 'c':
  2562. verb = Tint;
  2563. break;
  2564. case 'd':
  2565. case 'o':
  2566. case 'x':
  2567. case 'X':
  2568. verb = Tint;
  2569. for(i = 0; i < flag; i++){
  2570. if(flags[i] == 'b'){
  2571. verb = Tbig;
  2572. break;
  2573. }
  2574. }
  2575. break;
  2576. case 'e':
  2577. case 'f':
  2578. case 'g':
  2579. case 'E':
  2580. case 'G':
  2581. verb = Treal;
  2582. break;
  2583. case 's':
  2584. case 'q':
  2585. verb = Tstring;
  2586. break;
  2587. }
  2588. }
  2589. if(verb != Tnone){
  2590. if(verb < 0){
  2591. nerror(f, "%V: incomplete format '%.*s'", f, ns-fmtstart, &s[fmtstart]);
  2592. return;
  2593. }
  2594. if(va == nil){
  2595. nerror(f, "%V: too few arguments for format '%.*s'", f, ns-fmtstart, &s[fmtstart]);
  2596. return;
  2597. }
  2598. ty = va->left->ty;
  2599. if(ty->kind == Texception)
  2600. ty = mkextuptype(ty);
  2601. switch(verb){
  2602. case Tint:
  2603. switch(ty->kind){
  2604. case Tstring:
  2605. case Tarray:
  2606. case Tref:
  2607. case Tchan:
  2608. case Tlist:
  2609. case Tmodule:
  2610. if(c == 'x' || c == 'X')
  2611. verb = ty->kind;
  2612. break;
  2613. }
  2614. break;
  2615. case Tany:
  2616. if(tattr[ty->kind].isptr)
  2617. verb = ty->kind;
  2618. break;
  2619. }
  2620. if(verb != ty->kind){
  2621. nerror(f, "%V: format '%.*s' incompatible with argument %Q", f, ns-fmtstart, &s[fmtstart], va->left);
  2622. return;
  2623. }
  2624. va = va->right;
  2625. }
  2626. }
  2627. if(va != nil)
  2628. nerror(f, "%V: more arguments than formats", f);
  2629. }
  2630. Decl*
  2631. tuplefields(Node *n)
  2632. {
  2633. Decl *d, *h, **last;
  2634. h = nil;
  2635. last = &h;
  2636. for(; n != nil; n = n->right){
  2637. d = mkdecl(&n->left->src, Dfield, n->left->ty);
  2638. *last = d;
  2639. last = &d->next;
  2640. }
  2641. return h;
  2642. }
  2643. /*
  2644. * make explicit indices for every element in an array initializer
  2645. * return the maximum index
  2646. * sort the indices and check for duplicates
  2647. */
  2648. int
  2649. assignindices(Node *ar)
  2650. {
  2651. Node *wild, *off, *size, *inits, *n, *q;
  2652. Type *t;
  2653. Case *c;
  2654. int amax, max, last, nlab, ok;
  2655. amax = 0x7fffffff;
  2656. size = dupn(0, &nosrc, ar->left);
  2657. if(size->ty == tint){
  2658. size = fold(size);
  2659. if(size->op == Oconst)
  2660. amax = size->val;
  2661. }
  2662. inits = ar->right;
  2663. max = -1;
  2664. last = -1;
  2665. t = inits->left->ty;
  2666. wild = nil;
  2667. nlab = 0;
  2668. ok = 1;
  2669. for(n = inits; n != nil; n = n->right){
  2670. if(!tcompat(t, n->left->ty, 0)){
  2671. t = tparent(t, n->left->ty);
  2672. if(!tcompat(t, n->left->ty, 0)){
  2673. nerror(n->left, "inconsistent types %T and %T and in array initializer", t, n->left->ty);
  2674. return -1;
  2675. }
  2676. else
  2677. inits->left->ty = t;
  2678. }
  2679. if(t == tany)
  2680. t = n->left->ty;
  2681. /*
  2682. * make up an index if there isn't one
  2683. */
  2684. if(n->left->left == nil)
  2685. n->left->left = mkn(Oseq, mkconst(&n->left->right->src, last + 1), nil);
  2686. for(q = n->left->left; q != nil; q = q->right){
  2687. off = q->left;
  2688. if(off->ty != tint){
  2689. nerror(off, "array index %Q is not an int", off);
  2690. ok = 0;
  2691. continue;
  2692. }
  2693. off = fold(off);
  2694. switch(off->op){
  2695. case Owild:
  2696. if(wild != nil)
  2697. nerror(off, "array index * duplicated on line %L", wild->src.start);
  2698. wild = off;
  2699. continue;
  2700. case Orange:
  2701. if(off->left->op != Oconst || off->right->op != Oconst){
  2702. nerror(off, "range %V is not constant", off);
  2703. off = nil;
  2704. }else if(off->left->val < 0 || off->right->val >= amax){
  2705. nerror(off, "array index %V out of bounds", off);
  2706. off = nil;
  2707. }else
  2708. last = off->right->val;
  2709. break;
  2710. case Oconst:
  2711. last = off->val;
  2712. if(off->val < 0 || off->val >= amax){
  2713. nerror(off, "array index %V out of bounds", off);
  2714. off = nil;
  2715. }
  2716. break;
  2717. case Onothing:
  2718. /* get here from a syntax error */
  2719. off = nil;
  2720. break;
  2721. default:
  2722. nerror(off, "array index %V is not constant", off);
  2723. off = nil;
  2724. break;
  2725. }
  2726. nlab++;
  2727. if(off == nil){
  2728. off = mkconst(&n->left->right->src, last);
  2729. ok = 0;
  2730. }
  2731. if(last > max)
  2732. max = last;
  2733. q->left = off;
  2734. }
  2735. }
  2736. /*
  2737. * fix up types of nil elements
  2738. */
  2739. for(n = inits; n != nil; n = n->right)
  2740. if(n->left->ty == tany)
  2741. n->left->ty = t;
  2742. if(!ok)
  2743. return -1;
  2744. c = checklabels(inits, tint, nlab, "array index");
  2745. t = mktype(&inits->src.start, &inits->src.stop, Tainit, nil, nil);
  2746. inits->ty = t;
  2747. t->cse = c;
  2748. return max + 1;
  2749. }
  2750. /*
  2751. * check the labels of a case statment
  2752. */
  2753. void
  2754. casecheck(Node *cn, Type *ret)
  2755. {
  2756. Node *n, *q, *wild, *left, *arg;
  2757. Type *t;
  2758. Case *c;
  2759. Ok rok;
  2760. int nlab, ok, op;
  2761. rok = echeck(cn->left, 0, 0, nil);
  2762. cn->right = scheck(cn->right, ret, Sother);
  2763. if(!rok.ok)
  2764. return;
  2765. arg = cn->left;
  2766. t = arg->ty;
  2767. if(t != tint && t != tbig && t != tstring){
  2768. nerror(cn, "case argument %Q is not an int or big or string", arg);
  2769. return;
  2770. }
  2771. wild = nil;
  2772. nlab= 0;
  2773. ok = 1;
  2774. for(n = cn->right; n != nil; n = n->right){
  2775. q = n->left->left;
  2776. if(n->left->right->right == nil)
  2777. nwarn(q, "no body for case qualifier %V", q);
  2778. for(; q != nil; q = q->right){
  2779. left = fold(q->left);
  2780. q->left = left;
  2781. switch(left->op){
  2782. case Owild:
  2783. if(wild != nil)
  2784. nerror(left, "case qualifier * duplicated on line %L", wild->src.start);
  2785. wild = left;
  2786. break;
  2787. case Orange:
  2788. if(left->ty != t)
  2789. nerror(left, "case qualifier %Q clashes with %Q", left, arg);
  2790. else if(left->left->op != Oconst || left->right->op != Oconst){
  2791. nerror(left, "case range %V is not constant", left);
  2792. ok = 0;
  2793. }
  2794. nlab++;
  2795. break;
  2796. default:
  2797. if(left->ty != t){
  2798. nerror(left, "case qualifier %Q clashes with %Q", left, arg);
  2799. ok = 0;
  2800. }else if(left->op != Oconst){
  2801. nerror(left, "case qualifier %V is not constant", left);
  2802. ok = 0;
  2803. }
  2804. nlab++;
  2805. break;
  2806. }
  2807. }
  2808. }
  2809. if(!ok)
  2810. return;
  2811. c = checklabels(cn->right, t, nlab, "case qualifier");
  2812. op = Tcase;
  2813. if(t == tbig)
  2814. op = Tcasel;
  2815. else if(t == tstring)
  2816. op = Tcasec;
  2817. t = mktype(&cn->src.start, &cn->src.stop, op, nil, nil);
  2818. cn->ty = t;
  2819. t->cse = c;
  2820. }
  2821. /*
  2822. * check the labels and bodies of a pick statment
  2823. */
  2824. void
  2825. pickcheck(Node *n, Type *ret)
  2826. {
  2827. Node *w, *arg, *qs, *q, *qt, *left, **tags;
  2828. Decl *id, *d;
  2829. Type *t, *argty;
  2830. Case *c;
  2831. Ok rok;
  2832. int ok, nlab;
  2833. arg = n->left->right;
  2834. rok = echeck(arg, 0, 0, nil);
  2835. if(!rok.allok)
  2836. return;
  2837. t = arg->ty;
  2838. if(t->kind == Tref)
  2839. t = t->tof;
  2840. if(arg->ty->kind != Tref || t->kind != Tadt || t->tags == nil){
  2841. nerror(arg, "pick argument %Q is not a ref adt with pick tags", arg);
  2842. return;
  2843. }
  2844. argty = usetype(mktype(&arg->ty->src.start, &arg->ty->src.stop, Tref, t, nil));
  2845. arg = n->left->left;
  2846. pushscope(nil, Sother);
  2847. dasdecl(arg);
  2848. arg->decl->ty = argty;
  2849. arg->ty = argty;
  2850. tags = allocmem(t->decl->tag * sizeof *tags);
  2851. memset(tags, 0, t->decl->tag * sizeof *tags);
  2852. w = nil;
  2853. ok = 1;
  2854. nlab = 0;
  2855. for(qs = n->right; qs != nil; qs = qs->right){
  2856. qt = nil;
  2857. for(q = qs->left->left; q != nil; q = q->right){
  2858. left = q->left;
  2859. switch(left->op){
  2860. case Owild:
  2861. /* left->ty = tnone; */
  2862. left->ty = t;
  2863. if(w != nil)
  2864. nerror(left, "pick qualifier * duplicated on line %L", w->src.start);
  2865. w = left;
  2866. break;
  2867. case Oname:
  2868. id = namedot(t->tags, left->decl->sym);
  2869. if(id == nil){
  2870. nerror(left, "pick qualifier %V is not a member of %Q", left, arg);
  2871. ok = 0;
  2872. continue;
  2873. }
  2874. left->decl = id;
  2875. left->ty = id->ty;
  2876. if(tags[id->tag] != nil){
  2877. nerror(left, "pick qualifier %V duplicated on line %L",
  2878. left, tags[id->tag]->src.start);
  2879. ok = 0;
  2880. }
  2881. tags[id->tag] = left;
  2882. nlab++;
  2883. break;
  2884. default:
  2885. fatal("pickcheck can't handle %n", q);
  2886. break;
  2887. }
  2888. if(qt == nil)
  2889. qt = left;
  2890. else if(!tequal(qt->ty, left->ty))
  2891. nerror(left, "type clash in pick qualifiers %Q and %Q", qt, left);
  2892. }
  2893. argty->tof = t;
  2894. if(qt != nil)
  2895. argty->tof = qt->ty;
  2896. qs->left->right = scheck(qs->left->right, ret, Sother);
  2897. if(qs->left->right == nil)
  2898. nwarn(qs->left->left, "no body for pick qualifier %V", qs->left->left);
  2899. }
  2900. argty->tof = t;
  2901. for(qs = n->right; qs != nil; qs = qs->right)
  2902. for(q = qs->left->left; q != nil; q = q->right)
  2903. q->left = fold(q->left);
  2904. d = popscope();
  2905. d->refs++;
  2906. if(d->next != nil)
  2907. fatal("pickcheck: installing more than one id");
  2908. fndecls = appdecls(fndecls, d);
  2909. if(!ok)
  2910. return;
  2911. c = checklabels(n->right, tint, nlab, "pick qualifier");
  2912. t = mktype(&n->src.start, &n->src.stop, Tcase, nil, nil);
  2913. n->ty = t;
  2914. t->cse = c;
  2915. }
  2916. void
  2917. exccheck(Node *en, Type *ret)
  2918. {
  2919. Decl *ed;
  2920. Node *n, *q, *wild, *left, *oinexcept;
  2921. Type *t, *qt;
  2922. Case *c;
  2923. int nlab, ok;
  2924. Ok rok;
  2925. char buf[32];
  2926. static int nexc;
  2927. pushscope(nil, Sother);
  2928. if(en->left == nil){
  2929. seprint(buf, buf+sizeof(buf), ".ex%d", nexc++);
  2930. en->left = mkdeclname(&en->src, mkids(&en->src, enter(buf, 0), texception, nil));
  2931. }
  2932. oinexcept = inexcept;
  2933. inexcept = en->left;
  2934. dasdecl(en->left);
  2935. en->left->ty = en->left->decl->ty = texception;
  2936. ed = en->left->decl;
  2937. /* en->right = scheck(en->right, ret, Sother); */
  2938. t = tstring;
  2939. wild = nil;
  2940. nlab = 0;
  2941. ok = 1;
  2942. for(n = en->right; n != nil; n = n->right){
  2943. qt = nil;
  2944. for(q = n->left->left; q != nil; q = q->right){
  2945. left = q->left;
  2946. switch(left->op){
  2947. case Owild:
  2948. left->ty = texception;
  2949. if(wild != nil)
  2950. nerror(left, "exception qualifier * duplicated on line %L", wild->src.start);
  2951. wild = left;
  2952. break;
  2953. case Orange:
  2954. left->ty = tnone;
  2955. nerror(left, "exception qualifier %V is illegal", left);
  2956. ok = 0;
  2957. break;
  2958. default:
  2959. rok = echeck(left, 0, 0, nil);
  2960. if(!rok.ok){
  2961. ok = 0;
  2962. break;
  2963. }
  2964. left = q->left = fold(left);
  2965. if(left->ty != t && left->ty->kind != Texception){
  2966. nerror(left, "exception qualifier %Q is not a string or exception", left);
  2967. ok = 0;
  2968. }else if(left->op != Oconst){
  2969. nerror(left, "exception qualifier %V is not constant", left);
  2970. ok = 0;
  2971. }
  2972. else if(left->ty != t)
  2973. left->ty = mkextype(left->ty);
  2974. nlab++;
  2975. break;
  2976. }
  2977. if(qt == nil)
  2978. qt = left->ty;
  2979. else if(!tequal(qt, left->ty))
  2980. qt = texception;
  2981. }
  2982. if(qt != nil)
  2983. ed->ty = qt;
  2984. n->left->right = scheck(n->left->right, ret, Sother);
  2985. if(n->left->right->right == nil)
  2986. nwarn(n->left->left, "no body for exception qualifier %V", n->left->left);
  2987. }
  2988. ed->ty = texception;
  2989. inexcept = oinexcept;
  2990. if(!ok)
  2991. return;
  2992. c = checklabels(en->right, texception, nlab, "exception qualifier");
  2993. t = mktype(&en->src.start, &en->src.stop, Texcept, nil, nil);
  2994. en->ty = t;
  2995. t->cse = c;
  2996. ed = popscope();
  2997. fndecls = appdecls(fndecls, ed);
  2998. }
  2999. /*
  3000. * check array and case labels for validity
  3001. */
  3002. Case *
  3003. checklabels(Node *inits, Type *ctype, int nlab, char *title)
  3004. {
  3005. Node *n, *p, *q, *wild;
  3006. Label *labs, *aux;
  3007. Case *c;
  3008. char buf[256], buf1[256];
  3009. int i, e;
  3010. labs = allocmem(nlab * sizeof *labs);
  3011. i = 0;
  3012. wild = nil;
  3013. for(n = inits; n != nil; n = n->right){
  3014. for(q = n->left->left; q != nil; q = q->right){
  3015. switch(q->left->op){
  3016. case Oconst:
  3017. labs[i].start = q->left;
  3018. labs[i].stop = q->left;
  3019. labs[i++].node = n->left;
  3020. break;
  3021. case Orange:
  3022. labs[i].start = q->left->left;
  3023. labs[i].stop = q->left->right;
  3024. labs[i++].node = n->left;
  3025. break;
  3026. case Owild:
  3027. wild = n->left;
  3028. break;
  3029. default:
  3030. fatal("bogus index in checklabels");
  3031. break;
  3032. }
  3033. }
  3034. }
  3035. if(i != nlab)
  3036. fatal("bad label count: %d then %d", nlab, i);
  3037. aux = allocmem(nlab * sizeof *aux);
  3038. casesort(ctype, aux, labs, 0, nlab);
  3039. for(i = 0; i < nlab; i++){
  3040. p = labs[i].stop;
  3041. if(casecmp(ctype, labs[i].start, p) > 0)
  3042. nerror(labs[i].start, "unmatchable %s %V", title, labs[i].node);
  3043. for(e = i + 1; e < nlab; e++){
  3044. if(casecmp(ctype, labs[e].start, p) <= 0){
  3045. eprintlist(buf, buf+sizeof(buf), labs[e].node->left, " or ");
  3046. eprintlist(buf1, buf1+sizeof(buf1), labs[e-1].node->left, " or ");
  3047. nerror(labs[e].start,"%s '%s' overlaps with '%s' on line %L",
  3048. title, buf, buf1, p->src.start);
  3049. }
  3050. /*
  3051. * check for merging case labels
  3052. */
  3053. if(ctype != tint
  3054. || labs[e].start->val != p->val+1
  3055. || labs[e].node != labs[i].node)
  3056. break;
  3057. p = labs[e].stop;
  3058. }
  3059. if(e != i + 1){
  3060. labs[i].stop = p;
  3061. memmove(&labs[i+1], &labs[e], (nlab-e) * sizeof *labs);
  3062. nlab -= e - (i + 1);
  3063. }
  3064. }
  3065. free(aux);
  3066. c = allocmem(sizeof *c);
  3067. c->nlab = nlab;
  3068. c->nsnd = 0;
  3069. c->labs = labs;
  3070. c->wild = wild;
  3071. return c;
  3072. }
  3073. static int
  3074. matchcmp(Node *na, Node *nb)
  3075. {
  3076. Sym *a, *b;
  3077. int sa, sb;
  3078. a = na->decl->sym;
  3079. b = nb->decl->sym;
  3080. sa = a->len > 0 && a->name[a->len-1] == '*';
  3081. sb = b->len > 0 && b->name[b->len-1] == '*';
  3082. if(sa){
  3083. if(sb){
  3084. if(a->len == b->len)
  3085. return symcmp(a, b);
  3086. return b->len-a->len;
  3087. }
  3088. else
  3089. return 1;
  3090. }
  3091. else{
  3092. if(sb)
  3093. return -1;
  3094. else{
  3095. if(na->ty == tstring){
  3096. if(nb->ty == tstring)
  3097. return symcmp(a, b);
  3098. else
  3099. return 1;
  3100. }
  3101. else{
  3102. if(nb->ty == tstring)
  3103. return -1;
  3104. else
  3105. return symcmp(a, b);
  3106. }
  3107. }
  3108. }
  3109. }
  3110. int
  3111. casecmp(Type *ty, Node *a, Node *b)
  3112. {
  3113. if(ty == tint || ty == tbig){
  3114. if(a->val < b->val)
  3115. return -1;
  3116. if(a->val > b->val)
  3117. return 1;
  3118. return 0;
  3119. }
  3120. if(ty == texception)
  3121. return matchcmp(a, b);
  3122. return symcmp(a->decl->sym, b->decl->sym);
  3123. }
  3124. void
  3125. casesort(Type *t, Label *aux, Label *labs, int start, int stop)
  3126. {
  3127. int n, top, mid, base;
  3128. n = stop - start;
  3129. if(n <= 1)
  3130. return;
  3131. top = mid = start + n / 2;
  3132. casesort(t, aux, labs, start, top);
  3133. casesort(t, aux, labs, mid, stop);
  3134. /*
  3135. * merge together two sorted label arrays, yielding a sorted array
  3136. */
  3137. n = 0;
  3138. base = start;
  3139. while(base < top && mid < stop){
  3140. if(casecmp(t, labs[base].start, labs[mid].start) <= 0)
  3141. aux[n++] = labs[base++];
  3142. else
  3143. aux[n++] = labs[mid++];
  3144. }
  3145. if(base < top)
  3146. memmove(&aux[n], &labs[base], (top-base) * sizeof *aux);
  3147. else if(mid < stop)
  3148. memmove(&aux[n], &labs[mid], (stop-mid) * sizeof *aux);
  3149. memmove(&labs[start], &aux[0], (stop-start) * sizeof *labs);
  3150. }
  3151. /*
  3152. * binary search for the label corresponding to a given value
  3153. */
  3154. int
  3155. findlab(Type *ty, Node *v, Label *labs, int nlab)
  3156. {
  3157. int l, r, m;
  3158. if(nlab <= 1)
  3159. return 0;
  3160. l = 1;
  3161. r = nlab - 1;
  3162. while(l <= r){
  3163. m = (r + l) / 2;
  3164. if(casecmp(ty, labs[m].start, v) <= 0)
  3165. l = m + 1;
  3166. else
  3167. r = m - 1;
  3168. }
  3169. m = l - 1;
  3170. if(casecmp(ty, labs[m].start, v) > 0
  3171. || casecmp(ty, labs[m].stop, v) < 0)
  3172. fatal("findlab out of range");
  3173. return m;
  3174. }
  3175. void
  3176. altcheck(Node *an, Type *ret)
  3177. {
  3178. Node *n, *q, *left, *op, *wild;
  3179. Case *c;
  3180. int ok, nsnd, nrcv;
  3181. an->left = scheck(an->left, ret, Sother);
  3182. ok = 1;
  3183. nsnd = 0;
  3184. nrcv = 0;
  3185. wild = nil;
  3186. for(n = an->left; n != nil; n = n->right){
  3187. q = n->left->right->left;
  3188. if(n->left->right->right == nil)
  3189. nwarn(q, "no body for alt guard %V", q);
  3190. for(; q != nil; q = q->right){
  3191. left = q->left;
  3192. switch(left->op){
  3193. case Owild:
  3194. if(wild != nil)
  3195. nerror(left, "alt guard * duplicated on line %L", wild->src.start);
  3196. wild = left;
  3197. break;
  3198. case Orange:
  3199. nerror(left, "alt guard %V is illegal", left);
  3200. ok = 0;
  3201. break;
  3202. default:
  3203. op = hascomm(left);
  3204. if(op == nil){
  3205. nerror(left, "alt guard %V has no communication", left);
  3206. ok = 0;
  3207. break;
  3208. }
  3209. if(op->op == Osnd)
  3210. nsnd++;
  3211. else
  3212. nrcv++;
  3213. break;
  3214. }
  3215. }
  3216. }
  3217. if(!ok)
  3218. return;
  3219. c = allocmem(sizeof *c);
  3220. c->nlab = nsnd + nrcv;
  3221. c->nsnd = nsnd;
  3222. c->wild = wild;
  3223. an->ty = mktalt(c);
  3224. }
  3225. Node*
  3226. hascomm(Node *n)
  3227. {
  3228. Node *r;
  3229. if(n == nil)
  3230. return nil;
  3231. if(n->op == Osnd || n->op == Orcv)
  3232. return n;
  3233. r = hascomm(n->left);
  3234. if(r != nil)
  3235. return r;
  3236. return hascomm(n->right);
  3237. }
  3238. void
  3239. raisescheck(Type *t)
  3240. {
  3241. Node *n, *nn;
  3242. Ok ok;
  3243. if(t->kind != Tfn)
  3244. return;
  3245. n = t->u.eraises;
  3246. for(nn = n->left; nn != nil; nn = nn->right){
  3247. ok = echeck(nn->left, 0, 0, nil);
  3248. if(ok.ok && nn->left->ty->kind != Texception)
  3249. nerror(n, "%V: illegal raises expression", nn->left);
  3250. }
  3251. }
  3252. typedef struct Elist Elist;
  3253. struct Elist{
  3254. Decl *d;
  3255. Elist *nxt;
  3256. };
  3257. static Elist*
  3258. emerge(Elist *el1, Elist *el2)
  3259. {
  3260. int f;
  3261. Elist *el, *nxt;
  3262. for( ; el1 != nil; el1 = nxt){
  3263. f = 0;
  3264. for(el = el2; el != nil; el = el->nxt){
  3265. if(el1->d == el->d){
  3266. f = 1;
  3267. break;
  3268. }
  3269. }
  3270. nxt = el1->nxt;
  3271. if(!f){
  3272. el1->nxt = el2;
  3273. el2 = el1;
  3274. }
  3275. }
  3276. return el2;
  3277. }
  3278. static Elist*
  3279. equals(Node *n)
  3280. {
  3281. Node *q, *nn;
  3282. Elist *e, *el;
  3283. el = nil;
  3284. for(q = n->left->left; q != nil; q = q->right){
  3285. nn = q->left;
  3286. if(nn->op == Owild)
  3287. return nil;
  3288. if(nn->ty->kind != Texception)
  3289. continue;
  3290. e = (Elist*)malloc(sizeof(Elist));
  3291. e->d = nn->decl;
  3292. e->nxt = el;
  3293. el = e;
  3294. }
  3295. return el;
  3296. }
  3297. static int
  3298. caught(Decl *d, Node *n)
  3299. {
  3300. Node *q, *nn;
  3301. for(n = n->right; n != nil; n = n->right){
  3302. for(q = n->left->left; q != nil; q = q->right){
  3303. nn = q->left;
  3304. if(nn->op == Owild)
  3305. return 1;
  3306. if(nn->ty->kind != Texception)
  3307. continue;
  3308. if(d == nn->decl)
  3309. return 1;
  3310. }
  3311. }
  3312. return 0;
  3313. }
  3314. static Elist*
  3315. raisecheck(Node *n, Elist *ql)
  3316. {
  3317. int exc;
  3318. Node *e;
  3319. Elist *el, *nel, *nxt;
  3320. if(n == nil)
  3321. return nil;
  3322. el = nil;
  3323. for(; n != nil; n = n->right){
  3324. switch(n->op){
  3325. case Oscope:
  3326. return raisecheck(n->right, ql);
  3327. case Olabel:
  3328. case Odo:
  3329. return raisecheck(n->right, ql);
  3330. case Oif:
  3331. case Ofor:
  3332. return emerge(raisecheck(n->right->left, ql),
  3333. raisecheck(n->right->right, ql));
  3334. case Oalt:
  3335. case Ocase:
  3336. case Opick:
  3337. case Oexcept:
  3338. exc = n->op == Oexcept;
  3339. for(n = n->right; n != nil; n = n->right){
  3340. ql = nil;
  3341. if(exc)
  3342. ql = equals(n);
  3343. el = emerge(raisecheck(n->left->right, ql), el);
  3344. }
  3345. return el;
  3346. case Oseq:
  3347. el = emerge(raisecheck(n->left, ql), el);
  3348. break;
  3349. case Oexstmt:
  3350. el = raisecheck(n->left, ql);
  3351. nel = nil;
  3352. for( ; el != nil; el = nxt){
  3353. nxt = el->nxt;
  3354. if(!caught(el->d, n->right)){
  3355. el->nxt = nel;
  3356. nel = el;
  3357. }
  3358. }
  3359. return emerge(nel, raisecheck(n->right, ql));
  3360. case Oraise:
  3361. e = n->left;
  3362. if(e->ty && e->ty->kind == Texception){
  3363. if(!e->ty->cons)
  3364. return ql;
  3365. if(e->op == Ocall)
  3366. e = e->left;
  3367. if(e->op == Omdot)
  3368. e = e->right;
  3369. if(e->op != Oname)
  3370. fatal("exception %n not a name", e);
  3371. el = (Elist*)malloc(sizeof(Elist));
  3372. el->d = e->decl;
  3373. el->nxt = nil;
  3374. return el;
  3375. }
  3376. return nil;
  3377. default:
  3378. return nil;
  3379. }
  3380. }
  3381. return el;
  3382. }
  3383. void
  3384. checkraises(Node *n)
  3385. {
  3386. int f;
  3387. Decl *d;
  3388. Elist *e, *el;
  3389. Node *es, *nn;
  3390. el = raisecheck(n->right, nil);
  3391. es = n->ty->u.eraises;
  3392. if(es != nil){
  3393. for(nn = es->left; nn != nil; nn = nn->right){
  3394. d = nn->left->decl;
  3395. f = 0;
  3396. for(e = el; e != nil; e = e->nxt){
  3397. if(d == e->d){
  3398. f = 1;
  3399. e->d = nil;
  3400. break;
  3401. }
  3402. }
  3403. if(!f)
  3404. nwarn(n, "function %V does not raise %s but declared", n->left, d->sym->name);
  3405. }
  3406. }
  3407. for(e = el; e != nil; e = e->nxt)
  3408. if(e->d != nil)
  3409. nwarn(n, "function %V raises %s but not declared", n->left, e->d->sym->name);
  3410. }
  3411. /* sort all globals in modules now that we've finished with 'last' pointers
  3412. * and before any code generation
  3413. */
  3414. void
  3415. gsort(Node *n)
  3416. {
  3417. for(;;){
  3418. if(n == nil)
  3419. return;
  3420. if(n->op != Oseq)
  3421. break;
  3422. gsort(n->left);
  3423. n = n->right;
  3424. }
  3425. if(n->op == Omoddecl && n->ty->ok & OKverify){
  3426. n->ty->ids = namesort(n->ty->ids);
  3427. sizeids(n->ty->ids, 0);
  3428. }
  3429. }