types.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745
  1. #include "limbo.h"
  2. #include "mp.h"
  3. #include "libsec.h"
  4. char *kindname[Tend] =
  5. {
  6. /* Tnone */ "no type",
  7. /* Tadt */ "adt",
  8. /* Tadtpick */ "adt",
  9. /* Tarray */ "array",
  10. /* Tbig */ "big",
  11. /* Tbyte */ "byte",
  12. /* Tchan */ "chan",
  13. /* Treal */ "real",
  14. /* Tfn */ "fn",
  15. /* Tint */ "int",
  16. /* Tlist */ "list",
  17. /* Tmodule */ "module",
  18. /* Tref */ "ref",
  19. /* Tstring */ "string",
  20. /* Ttuple */ "tuple",
  21. /* Texception */ "exception",
  22. /* Tfix */ "fixed point",
  23. /* Tpoly */ "polymorphic",
  24. /* Tainit */ "array initializers",
  25. /* Talt */ "alt channels",
  26. /* Tany */ "polymorphic type",
  27. /* Tarrow */ "->",
  28. /* Tcase */ "case int labels",
  29. /* Tcasel */ "case big labels",
  30. /* Tcasec */ "case string labels",
  31. /* Tdot */ ".",
  32. /* Terror */ "type error",
  33. /* Tgoto */ "goto labels",
  34. /* Tid */ "id",
  35. /* Tiface */ "module interface",
  36. /* Texcept */ "exception handler table",
  37. /* Tinst */ "instantiated type",
  38. };
  39. Tattr tattr[Tend] =
  40. {
  41. /* isptr refable conable big vis */
  42. /* Tnone */ { 0, 0, 0, 0, 0, },
  43. /* Tadt */ { 0, 1, 1, 1, 1, },
  44. /* Tadtpick */ { 0, 1, 0, 1, 1, },
  45. /* Tarray */ { 1, 0, 0, 0, 1, },
  46. /* Tbig */ { 0, 0, 1, 1, 1, },
  47. /* Tbyte */ { 0, 0, 1, 0, 1, },
  48. /* Tchan */ { 1, 0, 0, 0, 1, },
  49. /* Treal */ { 0, 0, 1, 1, 1, },
  50. /* Tfn */ { 0, 1, 0, 0, 1, },
  51. /* Tint */ { 0, 0, 1, 0, 1, },
  52. /* Tlist */ { 1, 0, 0, 0, 1, },
  53. /* Tmodule */ { 1, 0, 0, 0, 1, },
  54. /* Tref */ { 1, 0, 0, 0, 1, },
  55. /* Tstring */ { 1, 0, 1, 0, 1, },
  56. /* Ttuple */ { 0, 1, 1, 1, 1, },
  57. /* Texception */ { 0, 0, 0, 1, 1, },
  58. /* Tfix */ { 0, 0, 1, 0, 1, },
  59. /* Tpoly */ { 1, 0, 0, 0, 1, },
  60. /* Tainit */ { 0, 0, 0, 1, 0, },
  61. /* Talt */ { 0, 0, 0, 1, 0, },
  62. /* Tany */ { 1, 0, 0, 0, 0, },
  63. /* Tarrow */ { 0, 0, 0, 0, 1, },
  64. /* Tcase */ { 0, 0, 0, 1, 0, },
  65. /* Tcasel */ { 0, 0, 0, 1, 0, },
  66. /* Tcasec */ { 0, 0, 0, 1, 0, },
  67. /* Tdot */ { 0, 0, 0, 0, 1, },
  68. /* Terror */ { 0, 1, 1, 0, 0, },
  69. /* Tgoto */ { 0, 0, 0, 1, 0, },
  70. /* Tid */ { 0, 0, 0, 0, 1, },
  71. /* Tiface */ { 0, 0, 0, 1, 0, },
  72. /* Texcept */ { 0, 0, 0, 1, 0, },
  73. /* Tinst */ { 0, 1, 1, 1, 1, },
  74. };
  75. static Teq *eqclass[Tend];
  76. static Type ztype;
  77. static int eqrec;
  78. static int eqset;
  79. static int tcomset;
  80. static int idcompat(Decl*, Decl*, int, int);
  81. static int rtcompat(Type *t1, Type *t2, int any, int);
  82. static int assumeteq(Type *t1, Type *t2);
  83. static int assumetcom(Type *t1, Type *t2);
  84. static int cleartcomrec(Type *t);
  85. static int rtequal(Type*, Type*);
  86. static int cleareqrec(Type*);
  87. static int idequal(Decl*, Decl*, int, int*);
  88. static int pyequal(Type*, Type*);
  89. static int rtsign(Type*, uchar*, int, int);
  90. static int clearrec(Type*);
  91. static int idsign(Decl*, int, uchar*, int, int);
  92. static int idsign1(Decl*, int, uchar*, int, int);
  93. static int raisessign(Node *n, uchar *sig, int lensig, int spos);
  94. static void ckfix(Type*, double);
  95. static int fnunify(Type*, Type*, Tpair**, int);
  96. static int rtunify(Type*, Type*, Tpair**, int);
  97. static int idunify(Decl*, Decl*, Tpair**, int);
  98. static int toccurs(Type*, Tpair**);
  99. static int fncleareqrec(Type*, Type*);
  100. static Type* comtype(Src*, Type*, Decl*);
  101. static Type* duptype(Type*);
  102. static int tpolys(Type*);
  103. static void
  104. addtmap(Type *t1, Type *t2, Tpair **tpp)
  105. {
  106. Tpair *tp;
  107. tp = allocmem(sizeof *tp);
  108. tp->t1 = t1;
  109. tp->t2 = t2;
  110. tp->nxt = *tpp;
  111. *tpp = tp;
  112. }
  113. Type*
  114. valtmap(Type *t, Tpair *tp)
  115. {
  116. for( ; tp != nil; tp = tp->nxt)
  117. if(tp->t1 == t)
  118. return tp->t2;
  119. return t;
  120. }
  121. Typelist*
  122. addtype(Type *t, Typelist *hd)
  123. {
  124. Typelist *tl, *p;
  125. tl = allocmem(sizeof(*tl));
  126. tl->t = t;
  127. tl->nxt = nil;
  128. if(hd == nil)
  129. return tl;
  130. for(p = hd; p->nxt != nil; p = p->nxt)
  131. ;
  132. p->nxt = tl;
  133. return hd;
  134. }
  135. void
  136. typeinit(void)
  137. {
  138. Decl *id;
  139. anontupsym = enter(".tuple", 0);
  140. ztype.sbl = -1;
  141. ztype.ok = 0;
  142. ztype.rec = 0;
  143. tbig = mktype(&noline, &noline, Tbig, nil, nil);
  144. tbig->size = IBY2LG;
  145. tbig->align = IBY2LG;
  146. tbig->ok = OKmask;
  147. tbyte = mktype(&noline, &noline, Tbyte, nil, nil);
  148. tbyte->size = 1;
  149. tbyte->align = 1;
  150. tbyte->ok = OKmask;
  151. tint = mktype(&noline, &noline, Tint, nil, nil);
  152. tint->size = IBY2WD;
  153. tint->align = IBY2WD;
  154. tint->ok = OKmask;
  155. treal = mktype(&noline, &noline, Treal, nil, nil);
  156. treal->size = IBY2FT;
  157. treal->align = IBY2FT;
  158. treal->ok = OKmask;
  159. tstring = mktype(&noline, &noline, Tstring, nil, nil);
  160. tstring->size = IBY2WD;
  161. tstring->align = IBY2WD;
  162. tstring->ok = OKmask;
  163. texception = mktype(&noline, &noline, Texception, nil, nil);
  164. texception->size = IBY2WD;
  165. texception->align = IBY2WD;
  166. texception->ok = OKmask;
  167. tany = mktype(&noline, &noline, Tany, nil, nil);
  168. tany->size = IBY2WD;
  169. tany->align = IBY2WD;
  170. tany->ok = OKmask;
  171. tnone = mktype(&noline, &noline, Tnone, nil, nil);
  172. tnone->size = 0;
  173. tnone->align = 1;
  174. tnone->ok = OKmask;
  175. terror = mktype(&noline, &noline, Terror, nil, nil);
  176. terror->size = 0;
  177. terror->align = 1;
  178. terror->ok = OKmask;
  179. tunknown = mktype(&noline, &noline, Terror, nil, nil);
  180. tunknown->size = 0;
  181. tunknown->align = 1;
  182. tunknown->ok = OKmask;
  183. tfnptr = mktype(&noline, &noline, Ttuple, nil, nil);
  184. id = tfnptr->ids = mkids(&nosrc, nil, tany, nil);
  185. id->store = Dfield;
  186. id->offset = 0;
  187. id->sym = enter("t0", 0);
  188. id->src = nosrc;
  189. id = tfnptr->ids->next = mkids(&nosrc, nil, tint, nil);
  190. id->store = Dfield;
  191. id->offset = IBY2WD;
  192. id->sym = enter("t1", 0);
  193. id->src = nosrc;
  194. rtexception = mktype(&noline, &noline, Tref, texception, nil);
  195. rtexception->size = IBY2WD;
  196. rtexception->align = IBY2WD;
  197. rtexception->ok = OKmask;
  198. }
  199. void
  200. typestart(void)
  201. {
  202. descriptors = nil;
  203. nfns = 0;
  204. nadts = 0;
  205. selfdecl = nil;
  206. if(tfnptr->decl != nil)
  207. tfnptr->decl->desc = nil;
  208. memset(eqclass, 0, sizeof eqclass);
  209. typebuiltin(mkids(&nosrc, enter("int", 0), nil, nil), tint);
  210. typebuiltin(mkids(&nosrc, enter("big", 0), nil, nil), tbig);
  211. typebuiltin(mkids(&nosrc, enter("byte", 0), nil, nil), tbyte);
  212. typebuiltin(mkids(&nosrc, enter("string", 0), nil, nil), tstring);
  213. typebuiltin(mkids(&nosrc, enter("real", 0), nil, nil), treal);
  214. }
  215. Teq*
  216. modclass(void)
  217. {
  218. return eqclass[Tmodule];
  219. }
  220. Type*
  221. mktype(Line *start, Line *stop, int kind, Type *tof, Decl *args)
  222. {
  223. Type *t;
  224. t = allocmem(sizeof *t);
  225. *t = ztype;
  226. t->src.start = *start;
  227. t->src.stop = *stop;
  228. t->kind = kind;
  229. t->tof = tof;
  230. t->ids = args;
  231. return t;
  232. }
  233. Type*
  234. mktalt(Case *c)
  235. {
  236. Type *t;
  237. char buf[32];
  238. static int nalt;
  239. t = mktype(&noline, &noline, Talt, nil, nil);
  240. t->decl = mkdecl(&nosrc, Dtype, t);
  241. seprint(buf, buf+sizeof(buf), ".a%d", nalt++);
  242. t->decl->sym = enter(buf, 0);
  243. t->cse = c;
  244. return usetype(t);
  245. }
  246. /*
  247. * copy t and the top level of ids
  248. */
  249. Type*
  250. copytypeids(Type *t)
  251. {
  252. Type *nt;
  253. Decl *id, *new, *last;
  254. nt = allocmem(sizeof *nt);
  255. *nt = *t;
  256. last = nil;
  257. for(id = t->ids; id != nil; id = id->next){
  258. new = allocmem(sizeof *id);
  259. *new = *id;
  260. if(last == nil)
  261. nt->ids = new;
  262. else
  263. last->next = new;
  264. last = new;
  265. }
  266. return nt;
  267. }
  268. /*
  269. * make each of the ids have type t
  270. */
  271. Decl*
  272. typeids(Decl *ids, Type *t)
  273. {
  274. Decl *id;
  275. if(ids == nil)
  276. return nil;
  277. ids->ty = t;
  278. for(id = ids->next; id != nil; id = id->next){
  279. id->ty = t;
  280. }
  281. return ids;
  282. }
  283. void
  284. typebuiltin(Decl *d, Type *t)
  285. {
  286. d->ty = t;
  287. t->decl = d;
  288. installids(Dtype, d);
  289. }
  290. Node *
  291. fielddecl(int store, Decl *ids)
  292. {
  293. Node *n;
  294. n = mkn(Ofielddecl, nil, nil);
  295. n->decl = ids;
  296. for(; ids != nil; ids = ids->next)
  297. ids->store = store;
  298. return n;
  299. }
  300. Node *
  301. typedecl(Decl *ids, Type *t)
  302. {
  303. Node *n;
  304. if(t->decl == nil)
  305. t->decl = ids;
  306. n = mkn(Otypedecl, nil, nil);
  307. n->decl = ids;
  308. n->ty = t;
  309. for(; ids != nil; ids = ids->next)
  310. ids->ty = t;
  311. return n;
  312. }
  313. void
  314. typedecled(Node *n)
  315. {
  316. installids(Dtype, n->decl);
  317. }
  318. Node *
  319. adtdecl(Decl *ids, Node *fields)
  320. {
  321. Node *n;
  322. Type *t;
  323. n = mkn(Oadtdecl, nil, nil);
  324. t = mktype(&ids->src.start, &ids->src.stop, Tadt, nil, nil);
  325. n->decl = ids;
  326. n->left = fields;
  327. n->ty = t;
  328. t->decl = ids;
  329. for(; ids != nil; ids = ids->next)
  330. ids->ty = t;
  331. return n;
  332. }
  333. void
  334. adtdecled(Node *n)
  335. {
  336. Decl *d, *ids;
  337. d = n->ty->decl;
  338. installids(Dtype, d);
  339. if(n->ty->polys != nil){
  340. pushscope(nil, Sother);
  341. installids(Dtype, n->ty->polys);
  342. }
  343. pushscope(nil, Sother);
  344. fielddecled(n->left);
  345. n->ty->ids = popscope();
  346. if(n->ty->polys != nil)
  347. n->ty->polys = popscope();
  348. for(ids = n->ty->ids; ids != nil; ids = ids->next)
  349. ids->dot = d;
  350. }
  351. void
  352. fielddecled(Node *n)
  353. {
  354. for(; n != nil; n = n->right){
  355. switch(n->op){
  356. case Oseq:
  357. fielddecled(n->left);
  358. break;
  359. case Oadtdecl:
  360. adtdecled(n);
  361. return;
  362. case Otypedecl:
  363. typedecled(n);
  364. return;
  365. case Ofielddecl:
  366. installids(Dfield, n->decl);
  367. return;
  368. case Ocondecl:
  369. condecled(n);
  370. gdasdecl(n->right);
  371. return;
  372. case Oexdecl:
  373. exdecled(n);
  374. return;
  375. case Opickdecl:
  376. pickdecled(n);
  377. return;
  378. default:
  379. fatal("can't deal with %O in fielddecled", n->op);
  380. }
  381. }
  382. }
  383. int
  384. pickdecled(Node *n)
  385. {
  386. Decl *d;
  387. int tag;
  388. if(n == nil)
  389. return 0;
  390. tag = pickdecled(n->left);
  391. pushscope(nil, Sother);
  392. fielddecled(n->right->right);
  393. d = n->right->left->decl;
  394. d->ty->ids = popscope();
  395. installids(Dtag, d);
  396. for(; d != nil; d = d->next)
  397. d->tag = tag++;
  398. return tag;
  399. }
  400. /*
  401. * make the tuple type used to initialize adt t
  402. */
  403. Type*
  404. mkadtcon(Type *t)
  405. {
  406. Decl *id, *new, *last;
  407. Type *nt;
  408. nt = allocmem(sizeof *nt);
  409. *nt = *t;
  410. last = nil;
  411. nt->ids = nil;
  412. nt->kind = Ttuple;
  413. for(id = t->ids; id != nil; id = id->next){
  414. if(id->store != Dfield)
  415. continue;
  416. new = allocmem(sizeof *id);
  417. *new = *id;
  418. new->cyc = 0;
  419. if(last == nil)
  420. nt->ids = new;
  421. else
  422. last->next = new;
  423. last = new;
  424. }
  425. last->next = nil;
  426. return nt;
  427. }
  428. /*
  429. * make the tuple type used to initialize t,
  430. * an adt with pick fields tagged by tg
  431. */
  432. Type*
  433. mkadtpickcon(Type *t, Type *tgt)
  434. {
  435. Decl *id, *new, *last;
  436. Type *nt;
  437. last = mkids(&tgt->decl->src, nil, tint, nil);
  438. last->store = Dfield;
  439. nt = mktype(&t->src.start, &t->src.stop, Ttuple, nil, last);
  440. for(id = t->ids; id != nil; id = id->next){
  441. if(id->store != Dfield)
  442. continue;
  443. new = allocmem(sizeof *id);
  444. *new = *id;
  445. new->cyc = 0;
  446. last->next = new;
  447. last = new;
  448. }
  449. for(id = tgt->ids; id != nil; id = id->next){
  450. if(id->store != Dfield)
  451. continue;
  452. new = allocmem(sizeof *id);
  453. *new = *id;
  454. new->cyc = 0;
  455. last->next = new;
  456. last = new;
  457. }
  458. last->next = nil;
  459. return nt;
  460. }
  461. /*
  462. * make an identifier type
  463. */
  464. Type*
  465. mkidtype(Src *src, Sym *s)
  466. {
  467. Type *t;
  468. t = mktype(&src->start, &src->stop, Tid, nil, nil);
  469. if(s->unbound == nil){
  470. s->unbound = mkdecl(src, Dunbound, nil);
  471. s->unbound->sym = s;
  472. }
  473. t->decl = s->unbound;
  474. return t;
  475. }
  476. /*
  477. * make a qualified type for t->s
  478. */
  479. Type*
  480. mkarrowtype(Line *start, Line *stop, Type *t, Sym *s)
  481. {
  482. Src src;
  483. src.start = *start;
  484. src.stop = *stop;
  485. t = mktype(start, stop, Tarrow, t, nil);
  486. if(s->unbound == nil){
  487. s->unbound = mkdecl(&src, Dunbound, nil);
  488. s->unbound->sym = s;
  489. }
  490. t->decl = s->unbound;
  491. return t;
  492. }
  493. /*
  494. * make a qualified type for t.s
  495. */
  496. Type*
  497. mkdottype(Line *start, Line *stop, Type *t, Sym *s)
  498. {
  499. Src src;
  500. src.start = *start;
  501. src.stop = *stop;
  502. t = mktype(start, stop, Tdot, t, nil);
  503. if(s->unbound == nil){
  504. s->unbound = mkdecl(&src, Dunbound, nil);
  505. s->unbound->sym = s;
  506. }
  507. t->decl = s->unbound;
  508. return t;
  509. }
  510. Type*
  511. mkinsttype(Src* src, Type *tt, Typelist *tl)
  512. {
  513. Type *t;
  514. t = mktype(&src->start, &src->stop, Tinst, tt, nil);
  515. t->u.tlist = tl;
  516. return t;
  517. }
  518. /*
  519. * look up the name f in the fields of a module, adt, or tuple
  520. */
  521. Decl*
  522. namedot(Decl *ids, Sym *s)
  523. {
  524. for(; ids != nil; ids = ids->next)
  525. if(ids->sym == s)
  526. return ids;
  527. return nil;
  528. }
  529. /*
  530. * complete the declaration of an adt
  531. * methods frames get sized in module definition or during function definition
  532. * place the methods at the end of the field list
  533. */
  534. void
  535. adtdefd(Type *t)
  536. {
  537. Decl *d, *id, *next, *aux, *store, *auxhd, *tagnext;
  538. int seentags;
  539. if(debug['x'])
  540. print("adt %T defd\n", t);
  541. d = t->decl;
  542. tagnext = nil;
  543. store = nil;
  544. for(id = t->polys; id != nil; id = id->next){
  545. id->store = Dtype;
  546. id->ty = verifytypes(id->ty, d, nil);
  547. }
  548. for(id = t->ids; id != nil; id = next){
  549. if(id->store == Dtag){
  550. if(t->tags != nil)
  551. error(id->src.start, "only one set of pick fields allowed");
  552. tagnext = pickdefd(t, id);
  553. next = tagnext;
  554. if(store != nil)
  555. store->next = next;
  556. else
  557. t->ids = next;
  558. continue;
  559. }else{
  560. id->dot = d;
  561. next = id->next;
  562. store = id;
  563. }
  564. }
  565. aux = nil;
  566. store = nil;
  567. auxhd = nil;
  568. seentags = 0;
  569. for(id = t->ids; id != nil; id = next){
  570. if(id == tagnext)
  571. seentags = 1;
  572. next = id->next;
  573. id->dot = d;
  574. id->ty = topvartype(verifytypes(id->ty, d, nil), id, 1, 1);
  575. if(id->store == Dfield && id->ty->kind == Tfn)
  576. id->store = Dfn;
  577. if(id->store == Dfn || id->store == Dconst){
  578. if(store != nil)
  579. store->next = next;
  580. else
  581. t->ids = next;
  582. if(aux != nil)
  583. aux->next = id;
  584. else
  585. auxhd = id;
  586. aux = id;
  587. }else{
  588. if(seentags)
  589. error(id->src.start, "pick fields must be the last data fields in an adt");
  590. store = id;
  591. }
  592. }
  593. if(aux != nil)
  594. aux->next = nil;
  595. if(store != nil)
  596. store->next = auxhd;
  597. else
  598. t->ids = auxhd;
  599. for(id = t->tags; id != nil; id = id->next){
  600. id->ty = verifytypes(id->ty, d, nil);
  601. if(id->ty->tof == nil)
  602. id->ty->tof = mkadtpickcon(t, id->ty);
  603. }
  604. }
  605. /*
  606. * assemble the data structure for an adt with a pick clause.
  607. * since the scoping rules for adt pick fields are strange,
  608. * we have a customized check for overlapping definitions.
  609. */
  610. Decl*
  611. pickdefd(Type *t, Decl *tg)
  612. {
  613. Decl *id, *xid, *lasttg, *d;
  614. Type *tt;
  615. int tag;
  616. lasttg = nil;
  617. d = t->decl;
  618. t->tags = tg;
  619. tag = 0;
  620. while(tg != nil){
  621. tt = tg->ty;
  622. if(tt->kind != Tadtpick || tg->tag != tag)
  623. break;
  624. tt->decl = tg;
  625. lasttg = tg;
  626. for(; tg != nil; tg = tg->next){
  627. if(tg->ty != tt)
  628. break;
  629. tag++;
  630. lasttg = tg;
  631. tg->dot = d;
  632. }
  633. for(id = tt->ids; id != nil; id = id->next){
  634. xid = namedot(t->ids, id->sym);
  635. if(xid != nil)
  636. error(id->src.start, "redeclaration of %K, previously declared as %k on line %L",
  637. id, xid, xid->src.start);
  638. id->dot = d;
  639. }
  640. }
  641. if(lasttg == nil){
  642. error(t->src.start, "empty pick field declaration in %T", t);
  643. t->tags = nil;
  644. }else
  645. lasttg->next = nil;
  646. d->tag = tag;
  647. return tg;
  648. }
  649. Node*
  650. moddecl(Decl *ids, Node *fields)
  651. {
  652. Node *n;
  653. Type *t;
  654. n = mkn(Omoddecl, mkn(Oseq, nil, nil), nil);
  655. t = mktype(&ids->src.start, &ids->src.stop, Tmodule, nil, nil);
  656. n->decl = ids;
  657. n->left = fields;
  658. n->ty = t;
  659. return n;
  660. }
  661. void
  662. moddecled(Node *n)
  663. {
  664. Decl *d, *ids, *im, *dot;
  665. Type *t;
  666. Sym *s;
  667. char buf[StrSize];
  668. int isimp;
  669. Dlist *dm, *dl;
  670. d = n->decl;
  671. installids(Dtype, d);
  672. isimp = 0;
  673. for(ids = d; ids != nil; ids = ids->next){
  674. for(im = impmods; im != nil; im = im->next){
  675. if(ids->sym == im->sym){
  676. isimp = 1;
  677. d = ids;
  678. dm = malloc(sizeof(Dlist));
  679. dm->d = ids;
  680. dm->next = nil;
  681. if(impdecls == nil)
  682. impdecls = dm;
  683. else{
  684. for(dl = impdecls; dl->next != nil; dl = dl->next)
  685. ;
  686. dl->next = dm;
  687. }
  688. }
  689. }
  690. ids->ty = n->ty;
  691. }
  692. pushscope(nil, Sother);
  693. fielddecled(n->left);
  694. d->ty->ids = popscope();
  695. /*
  696. * make the current module the -> parent of all contained decls->
  697. */
  698. for(ids = d->ty->ids; ids != nil; ids = ids->next)
  699. ids->dot = d;
  700. t = d->ty;
  701. t->decl = d;
  702. if(debug['m'])
  703. print("declare module %s\n", d->sym->name);
  704. /*
  705. * add the iface declaration in case it's needed later
  706. */
  707. seprint(buf, buf+sizeof(buf), ".m.%s", d->sym->name);
  708. installids(Dglobal, mkids(&d->src, enter(buf, 0), tnone, nil));
  709. if(isimp){
  710. for(ids = d->ty->ids; ids != nil; ids = ids->next){
  711. s = ids->sym;
  712. if(s->decl != nil && s->decl->scope >= scope){
  713. dot = s->decl->dot;
  714. if(s->decl->store != Dwundef && dot != nil && dot != d && isimpmod(dot->sym) && dequal(ids, s->decl, 0))
  715. continue;
  716. redecl(ids);
  717. ids->old = s->decl->old;
  718. }else
  719. ids->old = s->decl;
  720. s->decl = ids;
  721. ids->scope = scope;
  722. }
  723. }
  724. }
  725. /*
  726. * for each module in id,
  727. * link by field ext all of the decls for
  728. * functions needed in external linkage table
  729. * collect globals and make a tuple for all of them
  730. */
  731. Type*
  732. mkiface(Decl *m)
  733. {
  734. Decl *iface, *last, *globals, *glast, *id, *d;
  735. Type *t;
  736. char buf[StrSize];
  737. iface = last = allocmem(sizeof(Decl));
  738. globals = glast = mkdecl(&m->src, Dglobal, mktype(&m->src.start, &m->src.stop, Tadt, nil, nil));
  739. for(id = m->ty->ids; id != nil; id = id->next){
  740. switch(id->store){
  741. case Dglobal:
  742. glast = glast->next = dupdecl(id);
  743. id->iface = globals;
  744. glast->iface = id;
  745. break;
  746. case Dfn:
  747. id->iface = last = last->next = dupdecl(id);
  748. last->iface = id;
  749. break;
  750. case Dtype:
  751. if(id->ty->kind != Tadt)
  752. break;
  753. for(d = id->ty->ids; d != nil; d = d->next){
  754. if(d->store == Dfn){
  755. d->iface = last = last->next = dupdecl(d);
  756. last->iface = d;
  757. }
  758. }
  759. break;
  760. }
  761. }
  762. last->next = nil;
  763. iface = namesort(iface->next);
  764. if(globals->next != nil){
  765. glast->next = nil;
  766. globals->ty->ids = namesort(globals->next);
  767. globals->ty->decl = globals;
  768. globals->sym = enter(".mp", 0);
  769. globals->dot = m;
  770. globals->next = iface;
  771. iface = globals;
  772. }
  773. /*
  774. * make the interface type and install an identifier for it
  775. * the iface has a ref count if it is loaded
  776. */
  777. t = mktype(&m->src.start, &m->src.stop, Tiface, nil, iface);
  778. seprint(buf, buf+sizeof(buf), ".m.%s", m->sym->name);
  779. id = enter(buf, 0)->decl;
  780. t->decl = id;
  781. id->ty = t;
  782. /*
  783. * dummy node so the interface is initialized
  784. */
  785. id->init = mkn(Onothing, nil, nil);
  786. id->init->ty = t;
  787. id->init->decl = id;
  788. return t;
  789. }
  790. void
  791. joiniface(Type *mt, Type *t)
  792. {
  793. Decl *id, *d, *iface, *globals;
  794. iface = t->ids;
  795. globals = iface;
  796. if(iface != nil && iface->store == Dglobal)
  797. iface = iface->next;
  798. for(id = mt->tof->ids; id != nil; id = id->next){
  799. switch(id->store){
  800. case Dglobal:
  801. for(d = id->ty->ids; d != nil; d = d->next)
  802. d->iface->iface = globals;
  803. break;
  804. case Dfn:
  805. id->iface->iface = iface;
  806. iface = iface->next;
  807. break;
  808. default:
  809. fatal("unknown store %k in joiniface", id);
  810. break;
  811. }
  812. }
  813. if(iface != nil)
  814. fatal("join iface not matched");
  815. mt->tof = t;
  816. }
  817. void
  818. addiface(Decl *m, Decl *d)
  819. {
  820. Type *t;
  821. Decl *id, *last, *dd, *lastorig;
  822. Dlist *dl;
  823. if(d == nil || !local(d))
  824. return;
  825. modrefable(d->ty);
  826. if(m == nil){
  827. if(impdecls->next != nil)
  828. for(dl = impdecls; dl != nil; dl = dl->next)
  829. if(dl->d->ty->tof != impdecl->ty->tof) /* impdecl last */
  830. addiface(dl->d, d);
  831. addiface(impdecl, d);
  832. return;
  833. }
  834. t = m->ty->tof;
  835. last = nil;
  836. lastorig = nil;
  837. for(id = t->ids; id != nil; id = id->next){
  838. if(d == id || d == id->iface)
  839. return;
  840. last = id;
  841. if(id->tag == 0)
  842. lastorig = id;
  843. }
  844. dd = dupdecl(d);
  845. if(d->dot == nil)
  846. d->dot = dd->dot = m;
  847. d->iface = dd;
  848. dd->iface = d;
  849. if(debug['v']) print("addiface %p %p\n", d, dd);
  850. if(last == nil)
  851. t->ids = dd;
  852. else
  853. last->next = dd;
  854. dd->tag = 1; /* mark so not signed */
  855. if(lastorig == nil)
  856. t->ids = namesort(t->ids);
  857. else
  858. lastorig->next = namesort(lastorig->next);
  859. }
  860. /*
  861. * eliminate unused declarations from interfaces
  862. * label offset within interface
  863. */
  864. void
  865. narrowmods(void)
  866. {
  867. Teq *eq;
  868. Decl *id, *last;
  869. Type *t;
  870. long offset;
  871. for(eq = modclass(); eq != nil; eq = eq->eq){
  872. t = eq->ty->tof;
  873. if(t->linkall == 0){
  874. last = nil;
  875. for(id = t->ids; id != nil; id = id->next){
  876. if(id->refs == 0){
  877. if(last == nil)
  878. t->ids = id->next;
  879. else
  880. last->next = id->next;
  881. }else
  882. last = id;
  883. }
  884. /*
  885. * need to resize smaller interfaces
  886. */
  887. resizetype(t);
  888. }
  889. offset = 0;
  890. for(id = t->ids; id != nil; id = id->next)
  891. id->offset = offset++;
  892. /*
  893. * rathole to stuff number of entries in interface
  894. */
  895. t->decl->init->val = offset;
  896. }
  897. }
  898. /*
  899. * check to see if any data field of module m if referenced.
  900. * if so, mark all data in m
  901. */
  902. void
  903. moddataref(void)
  904. {
  905. Teq *eq;
  906. Decl *id;
  907. for(eq = modclass(); eq != nil; eq = eq->eq){
  908. id = eq->ty->tof->ids;
  909. if(id != nil && id->store == Dglobal && id->refs)
  910. for(id = eq->ty->ids; id != nil; id = id->next)
  911. if(id->store == Dglobal)
  912. modrefable(id->ty);
  913. }
  914. }
  915. /*
  916. * move the global declarations in interface to the front
  917. */
  918. Decl*
  919. modglobals(Decl *mod, Decl *globals)
  920. {
  921. Decl *id, *head, *last;
  922. /*
  923. * make a copy of all the global declarations
  924. * used for making a type descriptor for globals ONLY
  925. * note we now have two declarations for the same variables,
  926. * which is apt to cause problems if code changes
  927. *
  928. * here we fix up the offsets for the real declarations
  929. */
  930. idoffsets(mod->ty->ids, 0, 1);
  931. last = head = allocmem(sizeof(Decl));
  932. for(id = mod->ty->ids; id != nil; id = id->next)
  933. if(id->store == Dglobal)
  934. last = last->next = dupdecl(id);
  935. last->next = globals;
  936. return head->next;
  937. }
  938. /*
  939. * snap all id type names to the actual type
  940. * check that all types are completely defined
  941. * verify that the types look ok
  942. */
  943. Type*
  944. validtype(Type *t, Decl *inadt)
  945. {
  946. if(t == nil)
  947. return t;
  948. bindtypes(t);
  949. t = verifytypes(t, inadt, nil);
  950. cycsizetype(t);
  951. teqclass(t);
  952. return t;
  953. }
  954. Type*
  955. usetype(Type *t)
  956. {
  957. if(t == nil)
  958. return t;
  959. t = validtype(t, nil);
  960. reftype(t);
  961. return t;
  962. }
  963. Type*
  964. internaltype(Type *t)
  965. {
  966. bindtypes(t);
  967. t->ok = OKverify;
  968. sizetype(t);
  969. t->ok = OKmask;
  970. return t;
  971. }
  972. /*
  973. * checks that t is a valid top-level type
  974. */
  975. Type*
  976. topvartype(Type *t, Decl *id, int tyok, int polyok)
  977. {
  978. if(t->kind == Tadt && t->tags != nil || t->kind == Tadtpick)
  979. error(id->src.start, "cannot declare %s with type %T", id->sym->name, t);
  980. if(!tyok && t->kind == Tfn)
  981. error(id->src.start, "cannot declare %s to be a function", id->sym->name);
  982. if(!polyok && (t->kind == Tadt || t->kind == Tadtpick) && ispolyadt(t))
  983. error(id->src.start, "cannot declare %s of a polymorphic type", id->sym->name);
  984. return t;
  985. }
  986. Type*
  987. toptype(Src *src, Type *t)
  988. {
  989. if(t->kind == Tadt && t->tags != nil || t->kind == Tadtpick)
  990. error(src->start, "%T, an adt with pick fields, must be used with ref", t);
  991. if(t->kind == Tfn)
  992. error(src->start, "data cannot have a fn type like %T", t);
  993. return t;
  994. }
  995. static Type*
  996. comtype(Src *src, Type *t, Decl* adtd)
  997. {
  998. if(adtd == nil && (t->kind == Tadt || t->kind == Tadtpick) && ispolyadt(t))
  999. error(src->start, "polymorphic type %T illegal here", t);
  1000. return t;
  1001. }
  1002. void
  1003. usedty(Type *t)
  1004. {
  1005. if(t != nil && (t->ok | OKmodref) != OKmask)
  1006. fatal("used ty %t %2.2ux", t, t->ok);
  1007. }
  1008. void
  1009. bindtypes(Type *t)
  1010. {
  1011. Decl *id;
  1012. Typelist *tl;
  1013. if(t == nil)
  1014. return;
  1015. if((t->ok & OKbind) == OKbind)
  1016. return;
  1017. t->ok |= OKbind;
  1018. switch(t->kind){
  1019. case Tadt:
  1020. if(t->polys != nil){
  1021. pushscope(nil, Sother);
  1022. installids(Dtype, t->polys);
  1023. }
  1024. if(t->val != nil)
  1025. mergepolydecs(t);
  1026. if(t->polys != nil){
  1027. popscope();
  1028. for(id = t->polys; id != nil; id = id->next)
  1029. bindtypes(id->ty);
  1030. }
  1031. break;
  1032. case Tadtpick:
  1033. case Tmodule:
  1034. case Terror:
  1035. case Tint:
  1036. case Tbig:
  1037. case Tstring:
  1038. case Treal:
  1039. case Tbyte:
  1040. case Tnone:
  1041. case Tany:
  1042. case Tiface:
  1043. case Tainit:
  1044. case Talt:
  1045. case Tcase:
  1046. case Tcasel:
  1047. case Tcasec:
  1048. case Tgoto:
  1049. case Texcept:
  1050. case Tfix:
  1051. case Tpoly:
  1052. break;
  1053. case Tarray:
  1054. case Tarrow:
  1055. case Tchan:
  1056. case Tdot:
  1057. case Tlist:
  1058. case Tref:
  1059. bindtypes(t->tof);
  1060. break;
  1061. case Tid:
  1062. id = t->decl->sym->decl;
  1063. if(id == nil)
  1064. id = undefed(&t->src, t->decl->sym);
  1065. /* save a little space */
  1066. id->sym->unbound = nil;
  1067. t->decl = id;
  1068. break;
  1069. case Ttuple:
  1070. case Texception:
  1071. for(id = t->ids; id != nil; id = id->next)
  1072. bindtypes(id->ty);
  1073. break;
  1074. case Tfn:
  1075. if(t->polys != nil){
  1076. pushscope(nil, Sother);
  1077. installids(Dtype, t->polys);
  1078. }
  1079. for(id = t->ids; id != nil; id = id->next)
  1080. bindtypes(id->ty);
  1081. bindtypes(t->tof);
  1082. if(t->val != nil)
  1083. mergepolydecs(t);
  1084. if(t->polys != nil){
  1085. popscope();
  1086. for(id = t->polys; id != nil; id = id->next)
  1087. bindtypes(id->ty);
  1088. }
  1089. break;
  1090. case Tinst:
  1091. bindtypes(t->tof);
  1092. for(tl = t->u.tlist; tl != nil; tl = tl->nxt)
  1093. bindtypes(tl->t);
  1094. break;
  1095. default:
  1096. fatal("bindtypes: unknown type kind %d", t->kind);
  1097. }
  1098. }
  1099. /*
  1100. * walk the type checking for validity
  1101. */
  1102. Type*
  1103. verifytypes(Type *t, Decl *adtt, Decl *poly)
  1104. {
  1105. Node *n;
  1106. Decl *id, *id1, *last;
  1107. char buf[32];
  1108. int i, cyc;
  1109. Ok ok, ok1;
  1110. double max;
  1111. Typelist *tl;
  1112. if(t == nil)
  1113. return nil;
  1114. if((t->ok & OKverify) == OKverify)
  1115. return t;
  1116. t->ok |= OKverify;
  1117. if((t->ok & (OKverify|OKbind)) != (OKverify|OKbind))
  1118. fatal("verifytypes bogus ok for %t", t);
  1119. cyc = t->flags&CYCLIC;
  1120. switch(t->kind){
  1121. case Terror:
  1122. case Tint:
  1123. case Tbig:
  1124. case Tstring:
  1125. case Treal:
  1126. case Tbyte:
  1127. case Tnone:
  1128. case Tany:
  1129. case Tiface:
  1130. case Tainit:
  1131. case Talt:
  1132. case Tcase:
  1133. case Tcasel:
  1134. case Tcasec:
  1135. case Tgoto:
  1136. case Texcept:
  1137. break;
  1138. case Tfix:
  1139. n = t->val;
  1140. max = 0.0;
  1141. if(n->op == Oseq){
  1142. ok = echeck(n->left, 0, 0, n);
  1143. ok1 = echeck(n->right, 0, 0, n);
  1144. if(!ok.ok || !ok1.ok)
  1145. return terror;
  1146. if(n->left->ty != treal || n->right->ty != treal){
  1147. error(t->src.start, "fixed point scale/maximum not real");
  1148. return terror;
  1149. }
  1150. n->right = fold(n->right);
  1151. if(n->right->op != Oconst){
  1152. error(t->src.start, "fixed point maximum not constant");
  1153. return terror;
  1154. }
  1155. if((max = n->right->rval) <= 0){
  1156. error(t->src.start, "non-positive fixed point maximum");
  1157. return terror;
  1158. }
  1159. n = n->left;
  1160. }
  1161. else{
  1162. ok = echeck(n, 0, 0, nil);
  1163. if(!ok.ok)
  1164. return terror;
  1165. if(n->ty != treal){
  1166. error(t->src.start, "fixed point scale not real");
  1167. return terror;
  1168. }
  1169. }
  1170. n = t->val = fold(n);
  1171. if(n->op != Oconst){
  1172. error(t->src.start, "fixed point scale not constant");
  1173. return terror;
  1174. }
  1175. if(n->rval <= 0){
  1176. error(t->src.start, "non-positive fixed point scale");
  1177. return terror;
  1178. }
  1179. ckfix(t, max);
  1180. break;
  1181. case Tref:
  1182. t->tof = comtype(&t->src, verifytypes(t->tof, adtt, nil), adtt);
  1183. if(t->tof != nil && !tattr[t->tof->kind].refable){
  1184. error(t->src.start, "cannot have a ref %T", t->tof);
  1185. return terror;
  1186. }
  1187. if(0 && t->tof->kind == Tfn && t->tof->ids != nil && t->tof->ids->implicit)
  1188. error(t->src.start, "function references cannot have a self argument");
  1189. if(0 && t->tof->kind == Tfn && t->polys != nil)
  1190. error(t->src.start, "function references cannot be polymorphic");
  1191. break;
  1192. case Tchan:
  1193. case Tarray:
  1194. case Tlist:
  1195. t->tof = comtype(&t->src, toptype(&t->src, verifytypes(t->tof, adtt, nil)), adtt);
  1196. break;
  1197. case Tid:
  1198. t->ok &= ~OKverify;
  1199. t = verifytypes(idtype(t), adtt, nil);
  1200. break;
  1201. case Tarrow:
  1202. t->ok &= ~OKverify;
  1203. t = verifytypes(arrowtype(t, adtt), adtt, nil);
  1204. break;
  1205. case Tdot:
  1206. /*
  1207. * verify the parent adt & lookup the tag fields
  1208. */
  1209. t->ok &= ~OKverify;
  1210. t = verifytypes(dottype(t, adtt), adtt, nil);
  1211. break;
  1212. case Tadt:
  1213. /*
  1214. * this is where Tadt may get tag fields added
  1215. */
  1216. adtdefd(t);
  1217. break;
  1218. case Tadtpick:
  1219. for(id = t->ids; id != nil; id = id->next){
  1220. id->ty = topvartype(verifytypes(id->ty, id->dot, nil), id, 0, 1);
  1221. if(id->store == Dconst)
  1222. error(t->src.start, "pick fields cannot be a con like %s", id->sym->name);
  1223. }
  1224. verifytypes(t->decl->dot->ty, nil, nil);
  1225. break;
  1226. case Tmodule:
  1227. for(id = t->ids; id != nil; id = id->next){
  1228. id->ty = verifytypes(id->ty, nil, nil);
  1229. if(id->store == Dglobal && id->ty->kind == Tfn)
  1230. id->store = Dfn;
  1231. if(id->store != Dtype && id->store != Dfn)
  1232. topvartype(id->ty, id, 0, 0);
  1233. }
  1234. break;
  1235. case Ttuple:
  1236. case Texception:
  1237. if(t->decl == nil){
  1238. t->decl = mkdecl(&t->src, Dtype, t);
  1239. t->decl->sym = enter(".tuple", 0);
  1240. }
  1241. i = 0;
  1242. for(id = t->ids; id != nil; id = id->next){
  1243. id->store = Dfield;
  1244. if(id->sym == nil){
  1245. seprint(buf, buf+sizeof(buf), "t%d", i);
  1246. id->sym = enter(buf, 0);
  1247. }
  1248. i++;
  1249. id->ty = toptype(&id->src, verifytypes(id->ty, adtt, nil));
  1250. /* id->ty = comtype(&id->src, toptype(&id->src, verifytypes(id->ty, adtt, nil)), adtt); */
  1251. }
  1252. break;
  1253. case Tfn:
  1254. last = nil;
  1255. for(id = t->ids; id != nil; id = id->next){
  1256. id->store = Darg;
  1257. id->ty = topvartype(verifytypes(id->ty, adtt, nil), id, 0, 1);
  1258. if(id->implicit){
  1259. Decl *selfd;
  1260. selfd = poly ? poly : adtt;
  1261. if(selfd == nil)
  1262. error(t->src.start, "function is not a member of an adt, so can't use self");
  1263. else if(id != t->ids)
  1264. error(id->src.start, "only the first argument can use self");
  1265. else if(id->ty != selfd->ty && (id->ty->kind != Tref || id->ty->tof != selfd->ty))
  1266. error(id->src.start, "self argument's type must be %s or ref %s",
  1267. selfd->sym->name, selfd->sym->name);
  1268. }
  1269. last = id;
  1270. }
  1271. for(id = t->polys; id != nil; id = id->next){
  1272. if(adtt != nil){
  1273. for(id1 = adtt->ty->polys; id1 != nil; id1 = id1->next){
  1274. if(id1->sym == id->sym)
  1275. id->ty = id1->ty;
  1276. }
  1277. }
  1278. id->store = Dtype;
  1279. id->ty = verifytypes(id->ty, adtt, nil);
  1280. }
  1281. t->tof = comtype(&t->src, toptype(&t->src, verifytypes(t->tof, adtt, nil)), adtt);
  1282. if(t->varargs && (last == nil || last->ty != tstring))
  1283. error(t->src.start, "variable arguments must be preceded by a string");
  1284. if(t->varargs && t->polys != nil)
  1285. error(t->src.start, "polymorphic functions must not have variable arguments");
  1286. break;
  1287. case Tpoly:
  1288. for(id = t->ids; id != nil; id = id->next){
  1289. id->store = Dfn;
  1290. id->ty = verifytypes(id->ty, adtt, t->decl);
  1291. }
  1292. break;
  1293. case Tinst:
  1294. t->ok &= ~OKverify;
  1295. t->tof = verifytypes(t->tof, adtt, nil);
  1296. for(tl = t->u.tlist; tl != nil; tl = tl->nxt)
  1297. tl->t = verifytypes(tl->t, adtt, nil);
  1298. t = verifytypes(insttype(t, adtt, nil), adtt, nil);
  1299. break;
  1300. default:
  1301. fatal("verifytypes: unknown type kind %d", t->kind);
  1302. }
  1303. if(cyc)
  1304. t->flags |= CYCLIC;
  1305. return t;
  1306. }
  1307. /*
  1308. * resolve an id type
  1309. */
  1310. Type*
  1311. idtype(Type *t)
  1312. {
  1313. Decl *id;
  1314. Type *tt;
  1315. id = t->decl;
  1316. if(id->store == Dunbound)
  1317. fatal("idtype: unbound decl");
  1318. tt = id->ty;
  1319. if(id->store != Dtype && id->store != Dtag){
  1320. if(id->store == Dundef){
  1321. id->store = Dwundef;
  1322. error(t->src.start, "%s is not declared", id->sym->name);
  1323. }else if(id->store == Dimport){
  1324. id->store = Dwundef;
  1325. error(t->src.start, "%s's type cannot be determined", id->sym->name);
  1326. }else if(id->store != Dwundef)
  1327. error(t->src.start, "%s is not a type", id->sym->name);
  1328. return terror;
  1329. }
  1330. if(tt == nil){
  1331. error(t->src.start, "%t not fully defined", t);
  1332. return terror;
  1333. }
  1334. return tt;
  1335. }
  1336. /*
  1337. * resolve a -> qualified type
  1338. */
  1339. Type*
  1340. arrowtype(Type *t, Decl *adtt)
  1341. {
  1342. Type *tt;
  1343. Decl *id;
  1344. id = t->decl;
  1345. if(id->ty != nil){
  1346. if(id->store == Dunbound)
  1347. fatal("arrowtype: unbound decl has a type");
  1348. return id->ty;
  1349. }
  1350. /*
  1351. * special hack to allow module variables to derive other types
  1352. */
  1353. tt = t->tof;
  1354. if(tt->kind == Tid){
  1355. id = tt->decl;
  1356. if(id->store == Dunbound)
  1357. fatal("arrowtype: Tid's decl unbound");
  1358. if(id->store == Dimport){
  1359. id->store = Dwundef;
  1360. error(t->src.start, "%s's type cannot be determined", id->sym->name);
  1361. return terror;
  1362. }
  1363. /*
  1364. * forward references to module variables can't be resolved
  1365. */
  1366. if(id->store != Dtype && !(id->ty->ok & OKbind)){
  1367. error(t->src.start, "%s's type cannot be determined", id->sym->name);
  1368. return terror;
  1369. }
  1370. if(id->store == Dwundef)
  1371. return terror;
  1372. tt = id->ty = verifytypes(id->ty, adtt, nil);
  1373. if(tt == nil){
  1374. error(t->tof->src.start, "%T is not a module", t->tof);
  1375. return terror;
  1376. }
  1377. }else
  1378. tt = verifytypes(t->tof, adtt, nil);
  1379. t->tof = tt;
  1380. if(tt == terror)
  1381. return terror;
  1382. if(tt->kind != Tmodule){
  1383. error(t->src.start, "%T is not a module", tt);
  1384. return terror;
  1385. }
  1386. id = namedot(tt->ids, t->decl->sym);
  1387. if(id == nil){
  1388. error(t->src.start, "%s is not a member of %T", t->decl->sym->name, tt);
  1389. return terror;
  1390. }
  1391. if(id->store == Dtype && id->ty != nil){
  1392. t->decl = id;
  1393. return id->ty;
  1394. }
  1395. error(t->src.start, "%T is not a type", t);
  1396. return terror;
  1397. }
  1398. /*
  1399. * resolve a . qualified type
  1400. */
  1401. Type*
  1402. dottype(Type *t, Decl *adtt)
  1403. {
  1404. Type *tt;
  1405. Decl *id;
  1406. if(t->decl->ty != nil){
  1407. if(t->decl->store == Dunbound)
  1408. fatal("dottype: unbound decl has a type");
  1409. return t->decl->ty;
  1410. }
  1411. t->tof = tt = verifytypes(t->tof, adtt, nil);
  1412. if(tt == terror)
  1413. return terror;
  1414. if(tt->kind != Tadt){
  1415. error(t->src.start, "%T is not an adt", tt);
  1416. return terror;
  1417. }
  1418. id = namedot(tt->tags, t->decl->sym);
  1419. if(id != nil && id->ty != nil){
  1420. t->decl = id;
  1421. return id->ty;
  1422. }
  1423. error(t->src.start, "%s is not a pick tag of %T", t->decl->sym->name, tt);
  1424. return terror;
  1425. }
  1426. Type*
  1427. insttype(Type *t, Decl *adtt, Tpair **tp)
  1428. {
  1429. Type *tt;
  1430. Typelist *tl;
  1431. Decl *ids;
  1432. Tpair *tp1, *tp2;
  1433. Src src;
  1434. src = t->src;
  1435. if(tp == nil){
  1436. tp2 = nil;
  1437. tp = &tp2;
  1438. }
  1439. if(t->tof->kind != Tadt && t->tof->kind != Tadtpick){
  1440. error(src.start, "%T is not an adt", t->tof);
  1441. return terror;
  1442. }
  1443. if(t->tof->kind == Tadt)
  1444. ids = t->tof->polys;
  1445. else
  1446. ids = t->tof->decl->dot->ty->polys;
  1447. if(ids == nil){
  1448. error(src.start, "%T is not a polymorphic adt", t->tof);
  1449. return terror;
  1450. }
  1451. for(tl = t->u.tlist; tl != nil && ids != nil; tl = tl->nxt, ids = ids->next){
  1452. tt = tl->t;
  1453. if(!tattr[tt->kind].isptr){
  1454. error(src.start, "%T is not a pointer type", tt);
  1455. return terror;
  1456. }
  1457. unifysrc = src;
  1458. if(!tunify(ids->ty, tt, &tp1)){
  1459. error(src.start, "type %T does not match %T", tt, ids->ty);
  1460. return terror;
  1461. }
  1462. /* usetype(tt); */
  1463. tt = verifytypes(tt, adtt, nil);
  1464. addtmap(ids->ty, tt, tp);
  1465. }
  1466. if(tl != nil){
  1467. error(src.start, "too many actual types in instantiation");
  1468. return terror;
  1469. }
  1470. if(ids != nil){
  1471. error(src.start, "too few actual types in instantiation");
  1472. return terror;
  1473. }
  1474. tp1 = *tp;
  1475. tt = t->tof;
  1476. t = expandtype(tt, t, adtt, tp);
  1477. if(t == tt && adtt == nil)
  1478. t = duptype(t);
  1479. if(t != tt){
  1480. t->u.tmap = tp1;
  1481. if(debug['w']){
  1482. print("tmap for %T: ", t);
  1483. for( ; tp1!=nil; tp1=tp1->nxt)
  1484. print("%T -> %T ", tp1->t1, tp1->t2);
  1485. print("\n");
  1486. }
  1487. }
  1488. t->src = src;
  1489. return t;
  1490. }
  1491. /*
  1492. * walk a type, putting all adts, modules, and tuples into equivalence classes
  1493. */
  1494. void
  1495. teqclass(Type *t)
  1496. {
  1497. Decl *id, *tg;
  1498. Teq *teq;
  1499. if(t == nil || (t->ok & OKclass) == OKclass)
  1500. return;
  1501. t->ok |= OKclass;
  1502. switch(t->kind){
  1503. case Terror:
  1504. case Tint:
  1505. case Tbig:
  1506. case Tstring:
  1507. case Treal:
  1508. case Tbyte:
  1509. case Tnone:
  1510. case Tany:
  1511. case Tiface:
  1512. case Tainit:
  1513. case Talt:
  1514. case Tcase:
  1515. case Tcasel:
  1516. case Tcasec:
  1517. case Tgoto:
  1518. case Texcept:
  1519. case Tfix:
  1520. case Tpoly:
  1521. return;
  1522. case Tref:
  1523. teqclass(t->tof);
  1524. return;
  1525. case Tchan:
  1526. case Tarray:
  1527. case Tlist:
  1528. teqclass(t->tof);
  1529. if(!debug['Z'])
  1530. return;
  1531. break;
  1532. case Tadt:
  1533. case Tadtpick:
  1534. case Ttuple:
  1535. case Texception:
  1536. for(id = t->ids; id != nil; id = id->next)
  1537. teqclass(id->ty);
  1538. for(tg = t->tags; tg != nil; tg = tg->next)
  1539. teqclass(tg->ty);
  1540. for(id = t->polys; id != nil; id = id->next)
  1541. teqclass(id->ty);
  1542. break;
  1543. case Tmodule:
  1544. t->tof = mkiface(t->decl);
  1545. for(id = t->ids; id != nil; id = id->next)
  1546. teqclass(id->ty);
  1547. break;
  1548. case Tfn:
  1549. for(id = t->ids; id != nil; id = id->next)
  1550. teqclass(id->ty);
  1551. for(id = t->polys; id != nil; id = id->next)
  1552. teqclass(id->ty);
  1553. teqclass(t->tof);
  1554. return;
  1555. default:
  1556. fatal("teqclass: unknown type kind %d", t->kind);
  1557. return;
  1558. }
  1559. /*
  1560. * find an equivalent type
  1561. * stupid linear lookup could be made faster
  1562. */
  1563. if((t->ok & OKsized) != OKsized)
  1564. fatal("eqclass type not sized: %t", t);
  1565. for(teq = eqclass[t->kind]; teq != nil; teq = teq->eq){
  1566. if(t->size == teq->ty->size && tequal(t, teq->ty)){
  1567. t->eq = teq;
  1568. if(t->kind == Tmodule)
  1569. joiniface(t, t->eq->ty->tof);
  1570. return;
  1571. }
  1572. }
  1573. /*
  1574. * if no equiv type, make one
  1575. */
  1576. t->eq = allocmem(sizeof(Teq));
  1577. t->eq->id = 0;
  1578. t->eq->ty = t;
  1579. t->eq->eq = eqclass[t->kind];
  1580. eqclass[t->kind] = t->eq;
  1581. }
  1582. /*
  1583. * record that we've used the type
  1584. * using a type uses all types reachable from that type
  1585. */
  1586. void
  1587. reftype(Type *t)
  1588. {
  1589. Decl *id, *tg;
  1590. if(t == nil || (t->ok & OKref) == OKref)
  1591. return;
  1592. t->ok |= OKref;
  1593. if(t->decl != nil && t->decl->refs == 0)
  1594. t->decl->refs++;
  1595. switch(t->kind){
  1596. case Terror:
  1597. case Tint:
  1598. case Tbig:
  1599. case Tstring:
  1600. case Treal:
  1601. case Tbyte:
  1602. case Tnone:
  1603. case Tany:
  1604. case Tiface:
  1605. case Tainit:
  1606. case Talt:
  1607. case Tcase:
  1608. case Tcasel:
  1609. case Tcasec:
  1610. case Tgoto:
  1611. case Texcept:
  1612. case Tfix:
  1613. case Tpoly:
  1614. break;
  1615. case Tref:
  1616. case Tchan:
  1617. case Tarray:
  1618. case Tlist:
  1619. if(t->decl != nil){
  1620. if(nadts >= lenadts){
  1621. lenadts = nadts + 32;
  1622. adts = reallocmem(adts, lenadts * sizeof *adts);
  1623. }
  1624. adts[nadts++] = t->decl;
  1625. }
  1626. reftype(t->tof);
  1627. break;
  1628. case Tadt:
  1629. case Tadtpick:
  1630. case Ttuple:
  1631. case Texception:
  1632. if(t->kind == Tadt || t->kind == Ttuple && t->decl->sym != anontupsym){
  1633. if(nadts >= lenadts){
  1634. lenadts = nadts + 32;
  1635. adts = reallocmem(adts, lenadts * sizeof *adts);
  1636. }
  1637. adts[nadts++] = t->decl;
  1638. }
  1639. for(id = t->ids; id != nil; id = id->next)
  1640. if(id->store != Dfn)
  1641. reftype(id->ty);
  1642. for(tg = t->tags; tg != nil; tg = tg->next)
  1643. reftype(tg->ty);
  1644. for(id = t->polys; id != nil; id = id->next)
  1645. reftype(id->ty);
  1646. if(t->kind == Tadtpick)
  1647. reftype(t->decl->dot->ty);
  1648. break;
  1649. case Tmodule:
  1650. /*
  1651. * a module's elements should get used individually
  1652. * but do the globals for any sbl file
  1653. */
  1654. if(bsym != nil)
  1655. for(id = t->ids; id != nil; id = id->next)
  1656. if(id->store == Dglobal)
  1657. reftype(id->ty);
  1658. break;
  1659. case Tfn:
  1660. for(id = t->ids; id != nil; id = id->next)
  1661. reftype(id->ty);
  1662. for(id = t->polys; id != nil; id = id->next)
  1663. reftype(id->ty);
  1664. reftype(t->tof);
  1665. break;
  1666. default:
  1667. fatal("reftype: unknown type kind %d", t->kind);
  1668. break;
  1669. }
  1670. }
  1671. /*
  1672. * check all reachable types for cycles and illegal forward references
  1673. * find the size of all the types
  1674. */
  1675. void
  1676. cycsizetype(Type *t)
  1677. {
  1678. Decl *id, *tg;
  1679. if(t == nil || (t->ok & (OKcycsize|OKcyc|OKsized)) == (OKcycsize|OKcyc|OKsized))
  1680. return;
  1681. t->ok |= OKcycsize;
  1682. switch(t->kind){
  1683. case Terror:
  1684. case Tint:
  1685. case Tbig:
  1686. case Tstring:
  1687. case Treal:
  1688. case Tbyte:
  1689. case Tnone:
  1690. case Tany:
  1691. case Tiface:
  1692. case Tainit:
  1693. case Talt:
  1694. case Tcase:
  1695. case Tcasel:
  1696. case Tcasec:
  1697. case Tgoto:
  1698. case Texcept:
  1699. case Tfix:
  1700. case Tpoly:
  1701. t->ok |= OKcyc;
  1702. sizetype(t);
  1703. break;
  1704. case Tref:
  1705. case Tchan:
  1706. case Tarray:
  1707. case Tlist:
  1708. cyctype(t);
  1709. sizetype(t);
  1710. cycsizetype(t->tof);
  1711. break;
  1712. case Tadt:
  1713. case Ttuple:
  1714. case Texception:
  1715. cyctype(t);
  1716. sizetype(t);
  1717. for(id = t->ids; id != nil; id = id->next)
  1718. cycsizetype(id->ty);
  1719. for(tg = t->tags; tg != nil; tg = tg->next){
  1720. if((tg->ty->ok & (OKcycsize|OKcyc|OKsized)) == (OKcycsize|OKcyc|OKsized))
  1721. continue;
  1722. tg->ty->ok |= (OKcycsize|OKcyc|OKsized);
  1723. for(id = tg->ty->ids; id != nil; id = id->next)
  1724. cycsizetype(id->ty);
  1725. }
  1726. for(id = t->polys; id != nil; id = id->next)
  1727. cycsizetype(id->ty);
  1728. break;
  1729. case Tadtpick:
  1730. t->ok &= ~OKcycsize;
  1731. cycsizetype(t->decl->dot->ty);
  1732. break;
  1733. case Tmodule:
  1734. cyctype(t);
  1735. sizetype(t);
  1736. for(id = t->ids; id != nil; id = id->next)
  1737. cycsizetype(id->ty);
  1738. sizeids(t->ids, 0);
  1739. break;
  1740. case Tfn:
  1741. cyctype(t);
  1742. sizetype(t);
  1743. for(id = t->ids; id != nil; id = id->next)
  1744. cycsizetype(id->ty);
  1745. for(id = t->polys; id != nil; id = id->next)
  1746. cycsizetype(id->ty);
  1747. cycsizetype(t->tof);
  1748. sizeids(t->ids, MaxTemp);
  1749. break;
  1750. default:
  1751. fatal("cycsizetype: unknown type kind %d", t->kind);
  1752. break;
  1753. }
  1754. }
  1755. /* check for circularity in type declarations
  1756. * - has to be called before verifytypes
  1757. */
  1758. void
  1759. tcycle(Type *t)
  1760. {
  1761. Decl *id;
  1762. Type *tt;
  1763. Typelist *tl;
  1764. if(t == nil)
  1765. return;
  1766. switch(t->kind){
  1767. default:
  1768. break;
  1769. case Tchan:
  1770. case Tarray:
  1771. case Tref:
  1772. case Tlist:
  1773. case Tdot:
  1774. tcycle(t->tof);
  1775. break;
  1776. case Tfn:
  1777. case Ttuple:
  1778. tcycle(t->tof);
  1779. for(id = t->ids; id != nil; id = id->next)
  1780. tcycle(id->ty);
  1781. break;
  1782. case Tarrow:
  1783. if(t->rec&TRvis){
  1784. error(t->src.start, "circularity in definition of %T", t);
  1785. *t = *terror; /* break the cycle */
  1786. return;
  1787. }
  1788. tt = t->tof;
  1789. t->rec |= TRvis;
  1790. tcycle(tt);
  1791. if(tt->kind == Tid)
  1792. tt = tt->decl->ty;
  1793. id = namedot(tt->ids, t->decl->sym);
  1794. if(id != nil)
  1795. tcycle(id->ty);
  1796. t->rec &= ~TRvis;
  1797. break;
  1798. case Tid:
  1799. if(t->rec&TRvis){
  1800. error(t->src.start, "circularity in definition of %T", t);
  1801. *t = *terror; /* break the cycle */
  1802. return;
  1803. }
  1804. t->rec |= TRvis;
  1805. tcycle(t->decl->ty);
  1806. t->rec &= ~TRvis;
  1807. break;
  1808. case Tinst:
  1809. tcycle(t->tof);
  1810. for(tl = t->u.tlist; tl != nil; tl = tl->nxt)
  1811. tcycle(tl->t);
  1812. break;
  1813. }
  1814. }
  1815. /*
  1816. * marks for checking for arcs
  1817. */
  1818. enum
  1819. {
  1820. ArcValue = 1 << 0,
  1821. ArcList = 1 << 1,
  1822. ArcArray = 1 << 2,
  1823. ArcRef = 1 << 3,
  1824. ArcCyc = 1 << 4, /* cycle found */
  1825. ArcPolycyc = 1 << 5,
  1826. };
  1827. void
  1828. cyctype(Type *t)
  1829. {
  1830. Decl *id, *tg;
  1831. if((t->ok & OKcyc) == OKcyc)
  1832. return;
  1833. t->ok |= OKcyc;
  1834. t->rec |= TRcyc;
  1835. switch(t->kind){
  1836. case Terror:
  1837. case Tint:
  1838. case Tbig:
  1839. case Tstring:
  1840. case Treal:
  1841. case Tbyte:
  1842. case Tnone:
  1843. case Tany:
  1844. case Tfn:
  1845. case Tchan:
  1846. case Tarray:
  1847. case Tref:
  1848. case Tlist:
  1849. case Tfix:
  1850. case Tpoly:
  1851. break;
  1852. case Tadt:
  1853. case Tmodule:
  1854. case Ttuple:
  1855. case Texception:
  1856. for(id = t->ids; id != nil; id = id->next)
  1857. cycfield(t, id);
  1858. for(tg = t->tags; tg != nil; tg = tg->next){
  1859. if((tg->ty->ok & OKcyc) == OKcyc)
  1860. continue;
  1861. tg->ty->ok |= OKcyc;
  1862. for(id = tg->ty->ids; id != nil; id = id->next)
  1863. cycfield(t, id);
  1864. }
  1865. break;
  1866. default:
  1867. fatal("checktype: unknown type kind %d", t->kind);
  1868. break;
  1869. }
  1870. t->rec &= ~TRcyc;
  1871. }
  1872. void
  1873. cycfield(Type *base, Decl *id)
  1874. {
  1875. int arc;
  1876. if(!storespace[id->store])
  1877. return;
  1878. arc = cycarc(base, id->ty);
  1879. if((arc & (ArcCyc|ArcValue)) == (ArcCyc|ArcValue)){
  1880. if(id->cycerr == 0)
  1881. error(base->src.start, "illegal type cycle without a reference in field %s of %t",
  1882. id->sym->name, base);
  1883. id->cycerr = 1;
  1884. }else if(arc & ArcCyc){
  1885. if((arc & ArcArray) && id->cyc == 0 && !(arc & ArcPolycyc)){
  1886. if(id->cycerr == 0)
  1887. error(base->src.start, "illegal circular reference to type %T in field %s of %t",
  1888. id->ty, id->sym->name, base);
  1889. id->cycerr = 1;
  1890. }
  1891. id->cycle = 1;
  1892. }else if(id->cyc != 0){
  1893. if(id->cycerr == 0)
  1894. error(id->src.start, "spurious cyclic qualifier for field %s of %t", id->sym->name, base);
  1895. id->cycerr = 1;
  1896. }
  1897. }
  1898. int
  1899. cycarc(Type *base, Type *t)
  1900. {
  1901. Decl *id, *tg;
  1902. int me, arc;
  1903. if(t == nil)
  1904. return 0;
  1905. if(t->rec & TRcyc){
  1906. if(tequal(t, base)){
  1907. if(t->kind == Tmodule)
  1908. return ArcCyc | ArcRef;
  1909. else
  1910. return ArcCyc | ArcValue;
  1911. }
  1912. return 0;
  1913. }
  1914. t->rec |= TRcyc;
  1915. me = 0;
  1916. switch(t->kind){
  1917. case Terror:
  1918. case Tint:
  1919. case Tbig:
  1920. case Tstring:
  1921. case Treal:
  1922. case Tbyte:
  1923. case Tnone:
  1924. case Tany:
  1925. case Tchan:
  1926. case Tfn:
  1927. case Tfix:
  1928. case Tpoly:
  1929. break;
  1930. case Tarray:
  1931. me = cycarc(base, t->tof) & ~ArcValue | ArcArray;
  1932. break;
  1933. case Tref:
  1934. me = cycarc(base, t->tof) & ~ArcValue | ArcRef;
  1935. break;
  1936. case Tlist:
  1937. me = cycarc(base, t->tof) & ~ArcValue | ArcList;
  1938. break;
  1939. case Tadt:
  1940. case Tadtpick:
  1941. case Tmodule:
  1942. case Ttuple:
  1943. case Texception:
  1944. me = 0;
  1945. for(id = t->ids; id != nil; id = id->next){
  1946. if(!storespace[id->store])
  1947. continue;
  1948. arc = cycarc(base, id->ty);
  1949. if((arc & ArcCyc) && id->cycerr == 0)
  1950. me |= arc;
  1951. }
  1952. for(tg = t->tags; tg != nil; tg = tg->next){
  1953. arc = cycarc(base, tg->ty);
  1954. if((arc & ArcCyc) && tg->cycerr == 0)
  1955. me |= arc;
  1956. }
  1957. if(t->kind == Tmodule)
  1958. me = me & ArcCyc | ArcRef | ArcPolycyc;
  1959. else
  1960. me &= ArcCyc | ArcValue | ArcPolycyc;
  1961. break;
  1962. default:
  1963. fatal("cycarc: unknown type kind %d", t->kind);
  1964. break;
  1965. }
  1966. t->rec &= ~TRcyc;
  1967. if(t->flags&CYCLIC)
  1968. me |= ArcPolycyc;
  1969. return me;
  1970. }
  1971. /*
  1972. * set the sizes and field offsets for t
  1973. * look only as deeply as needed to size this type.
  1974. * cycsize type will clean up the rest.
  1975. */
  1976. void
  1977. sizetype(Type *t)
  1978. {
  1979. Decl *id, *tg;
  1980. Szal szal;
  1981. long sz, al, a;
  1982. if(t == nil)
  1983. return;
  1984. if((t->ok & OKsized) == OKsized)
  1985. return;
  1986. t->ok |= OKsized;
  1987. if((t->ok & (OKverify|OKsized)) != (OKverify|OKsized))
  1988. fatal("sizetype bogus ok for %t", t);
  1989. switch(t->kind){
  1990. default:
  1991. fatal("sizetype: unknown type kind %d", t->kind);
  1992. break;
  1993. case Terror:
  1994. case Tnone:
  1995. case Tbyte:
  1996. case Tint:
  1997. case Tbig:
  1998. case Tstring:
  1999. case Tany:
  2000. case Treal:
  2001. fatal("%T should have a size", t);
  2002. break;
  2003. case Tref:
  2004. case Tchan:
  2005. case Tarray:
  2006. case Tlist:
  2007. case Tmodule:
  2008. case Tfix:
  2009. case Tpoly:
  2010. t->size = t->align = IBY2WD;
  2011. break;
  2012. case Ttuple:
  2013. case Tadt:
  2014. case Texception:
  2015. if(t->tags == nil){
  2016. if(!debug['z']){
  2017. szal = sizeids(t->ids, 0);
  2018. t->size = align(szal.size, szal.align);
  2019. t->align = szal.align;
  2020. }else{
  2021. szal = sizeids(t->ids, 0);
  2022. t->align = IBY2LG;
  2023. t->size = align(szal.size, IBY2LG);
  2024. }
  2025. return;
  2026. }
  2027. if(!debug['z']){
  2028. szal = sizeids(t->ids, IBY2WD);
  2029. sz = szal.size;
  2030. al = szal.align;
  2031. if(al < IBY2WD)
  2032. al = IBY2WD;
  2033. }else{
  2034. szal = sizeids(t->ids, IBY2WD);
  2035. sz = szal.size;
  2036. al = IBY2LG;
  2037. }
  2038. for(tg = t->tags; tg != nil; tg = tg->next){
  2039. if((tg->ty->ok & OKsized) == OKsized)
  2040. continue;
  2041. tg->ty->ok |= OKsized;
  2042. if(!debug['z']){
  2043. szal = sizeids(tg->ty->ids, sz);
  2044. a = szal.align;
  2045. if(a < al)
  2046. a = al;
  2047. tg->ty->size = align(szal.size, a);
  2048. tg->ty->align = a;
  2049. }else{
  2050. szal = sizeids(tg->ty->ids, sz);
  2051. tg->ty->size = align(szal.size, IBY2LG);
  2052. tg->ty->align = IBY2LG;
  2053. }
  2054. }
  2055. break;
  2056. case Tfn:
  2057. t->size = 0;
  2058. t->align = 1;
  2059. break;
  2060. case Tainit:
  2061. t->size = 0;
  2062. t->align = 1;
  2063. break;
  2064. case Talt:
  2065. t->size = t->cse->nlab * 2*IBY2WD + 2*IBY2WD;
  2066. t->align = IBY2WD;
  2067. break;
  2068. case Tcase:
  2069. case Tcasec:
  2070. t->size = t->cse->nlab * 3*IBY2WD + 2*IBY2WD;
  2071. t->align = IBY2WD;
  2072. break;
  2073. case Tcasel:
  2074. t->size = t->cse->nlab * 6*IBY2WD + 3*IBY2WD;
  2075. t->align = IBY2LG;
  2076. break;
  2077. case Tgoto:
  2078. t->size = t->cse->nlab * IBY2WD + IBY2WD;
  2079. if(t->cse->iwild != nil)
  2080. t->size += IBY2WD;
  2081. t->align = IBY2WD;
  2082. break;
  2083. case Tiface:
  2084. sz = IBY2WD;
  2085. for(id = t->ids; id != nil; id = id->next){
  2086. sz = align(sz, IBY2WD) + IBY2WD;
  2087. sz += id->sym->len + 1;
  2088. if(id->dot->ty->kind == Tadt)
  2089. sz += id->dot->sym->len + 1;
  2090. }
  2091. t->size = sz;
  2092. t->align = IBY2WD;
  2093. break;
  2094. case Texcept:
  2095. t->size = 0;
  2096. t->align = IBY2WD;
  2097. break;
  2098. }
  2099. }
  2100. Szal
  2101. sizeids(Decl *id, long off)
  2102. {
  2103. Szal szal;
  2104. int a, al;
  2105. al = 1;
  2106. for(; id != nil; id = id->next){
  2107. if(storespace[id->store]){
  2108. sizetype(id->ty);
  2109. /*
  2110. * alignment can be 0 if we have
  2111. * illegal forward declarations.
  2112. * just patch a; other code will flag an error
  2113. */
  2114. a = id->ty->align;
  2115. if(a == 0)
  2116. a = 1;
  2117. if(a > al)
  2118. al = a;
  2119. off = align(off, a);
  2120. id->offset = off;
  2121. off += id->ty->size;
  2122. }
  2123. }
  2124. szal.size = off;
  2125. szal.align = al;
  2126. return szal;
  2127. }
  2128. long
  2129. align(long off, int align)
  2130. {
  2131. if(align == 0)
  2132. fatal("align 0");
  2133. while(off % align)
  2134. off++;
  2135. return off;
  2136. }
  2137. /*
  2138. * recalculate a type's size
  2139. */
  2140. void
  2141. resizetype(Type *t)
  2142. {
  2143. if((t->ok & OKsized) == OKsized){
  2144. t->ok &= ~OKsized;
  2145. cycsizetype(t);
  2146. }
  2147. }
  2148. /*
  2149. * check if a module is accessable from t
  2150. * if so, mark that module interface
  2151. */
  2152. void
  2153. modrefable(Type *t)
  2154. {
  2155. Decl *id, *m, *tg;
  2156. if(t == nil || (t->ok & OKmodref) == OKmodref)
  2157. return;
  2158. if((t->ok & OKverify) != OKverify)
  2159. fatal("modrefable unused type %t", t);
  2160. t->ok |= OKmodref;
  2161. switch(t->kind){
  2162. case Terror:
  2163. case Tint:
  2164. case Tbig:
  2165. case Tstring:
  2166. case Treal:
  2167. case Tbyte:
  2168. case Tnone:
  2169. case Tany:
  2170. case Tfix:
  2171. case Tpoly:
  2172. break;
  2173. case Tchan:
  2174. case Tref:
  2175. case Tarray:
  2176. case Tlist:
  2177. modrefable(t->tof);
  2178. break;
  2179. case Tmodule:
  2180. t->tof->linkall = 1;
  2181. t->decl->refs++;
  2182. for(id = t->ids; id != nil; id = id->next){
  2183. switch(id->store){
  2184. case Dglobal:
  2185. case Dfn:
  2186. modrefable(id->ty);
  2187. break;
  2188. case Dtype:
  2189. if(id->ty->kind != Tadt)
  2190. break;
  2191. for(m = id->ty->ids; m != nil; m = m->next)
  2192. if(m->store == Dfn)
  2193. modrefable(m->ty);
  2194. break;
  2195. }
  2196. }
  2197. break;
  2198. case Tfn:
  2199. case Tadt:
  2200. case Ttuple:
  2201. case Texception:
  2202. for(id = t->ids; id != nil; id = id->next)
  2203. if(id->store != Dfn)
  2204. modrefable(id->ty);
  2205. for(tg = t->tags; tg != nil; tg = tg->next){
  2206. /*
  2207. if((tg->ty->ok & OKmodref) == OKmodref)
  2208. continue;
  2209. */
  2210. tg->ty->ok |= OKmodref;
  2211. for(id = tg->ty->ids; id != nil; id = id->next)
  2212. modrefable(id->ty);
  2213. }
  2214. for(id = t->polys; id != nil; id = id->next)
  2215. modrefable(id->ty);
  2216. modrefable(t->tof);
  2217. break;
  2218. case Tadtpick:
  2219. modrefable(t->decl->dot->ty);
  2220. break;
  2221. default:
  2222. fatal("unknown type kind %d", t->kind);
  2223. break;
  2224. }
  2225. }
  2226. Desc*
  2227. gendesc(Decl *d, long size, Decl *decls)
  2228. {
  2229. Desc *desc;
  2230. if(debug['D'])
  2231. print("generate desc for %D\n", d);
  2232. if(ispoly(d))
  2233. addfnptrs(d, 0);
  2234. desc = usedesc(mkdesc(size, decls));
  2235. return desc;
  2236. }
  2237. Desc*
  2238. mkdesc(long size, Decl *d)
  2239. {
  2240. uchar *pmap;
  2241. long len, n;
  2242. len = (size+8*IBY2WD-1) / (8*IBY2WD);
  2243. pmap = allocmem(len);
  2244. memset(pmap, 0, len);
  2245. n = descmap(d, pmap, 0);
  2246. if(n >= 0)
  2247. n = n / (8*IBY2WD) + 1;
  2248. else
  2249. n = 0;
  2250. if(n > len)
  2251. fatal("wrote off end of decl map: %ld %ld", n, len);
  2252. return enterdesc(pmap, size, n);
  2253. }
  2254. Desc*
  2255. mktdesc(Type *t)
  2256. {
  2257. Desc *d;
  2258. uchar *pmap;
  2259. long len, n;
  2260. usedty(t);
  2261. if(debug['D'])
  2262. print("generate desc for %T\n", t);
  2263. if(t->decl == nil){
  2264. t->decl = mkdecl(&t->src, Dtype, t);
  2265. t->decl->sym = enter("_mktdesc_", 0);
  2266. }
  2267. if(t->decl->desc != nil)
  2268. return t->decl->desc;
  2269. len = (t->size+8*IBY2WD-1) / (8*IBY2WD);
  2270. pmap = allocmem(len);
  2271. memset(pmap, 0, len);
  2272. n = tdescmap(t, pmap, 0);
  2273. if(n >= 0)
  2274. n = n / (8*IBY2WD) + 1;
  2275. else
  2276. n = 0;
  2277. if(n > len)
  2278. fatal("wrote off end of type map for %T: %ld %ld 0x%2.2ux", t, n, len, t->ok);
  2279. d = enterdesc(pmap, t->size, n);
  2280. t->decl->desc = d;
  2281. if(debug['j']){
  2282. uchar *m, *e;
  2283. print("generate desc for %T\n", t);
  2284. print("\tdesc\t$%d,%lud,\"", d->id, d->size);
  2285. e = d->map + d->nmap;
  2286. for(m = d->map; m < e; m++)
  2287. print("%.2x", *m);
  2288. print("\"\n");
  2289. }
  2290. return d;
  2291. }
  2292. Desc*
  2293. enterdesc(uchar *map, long size, long nmap)
  2294. {
  2295. Desc *d, *last;
  2296. int c;
  2297. last = nil;
  2298. for(d = descriptors; d != nil; d = d->next){
  2299. if(d->size > size || d->size == size && d->nmap > nmap)
  2300. break;
  2301. if(d->size == size && d->nmap == nmap){
  2302. c = memcmp(d->map, map, nmap);
  2303. if(c == 0){
  2304. free(map);
  2305. return d;
  2306. }
  2307. if(c > 0)
  2308. break;
  2309. }
  2310. last = d;
  2311. }
  2312. d = allocmem(sizeof *d);
  2313. d->id = -1;
  2314. d->used = 0;
  2315. d->map = map;
  2316. d->size = size;
  2317. d->nmap = nmap;
  2318. if(last == nil){
  2319. d->next = descriptors;
  2320. descriptors = d;
  2321. }else{
  2322. d->next = last->next;
  2323. last->next = d;
  2324. }
  2325. return d;
  2326. }
  2327. Desc*
  2328. usedesc(Desc *d)
  2329. {
  2330. d->used = 1;
  2331. return d;
  2332. }
  2333. /*
  2334. * create the pointer description byte map for every type in decls
  2335. * each bit corresponds to a word, and is 1 if occupied by a pointer
  2336. * the high bit in the byte maps the first word
  2337. */
  2338. long
  2339. descmap(Decl *decls, uchar *map, long start)
  2340. {
  2341. Decl *d;
  2342. long last, m;
  2343. if(debug['D'])
  2344. print("descmap offset %ld\n", start);
  2345. last = -1;
  2346. for(d = decls; d != nil; d = d->next){
  2347. if(d->store == Dtype && d->ty->kind == Tmodule
  2348. || d->store == Dfn
  2349. || d->store == Dconst)
  2350. continue;
  2351. if(d->store == Dlocal && d->link != nil)
  2352. continue;
  2353. m = tdescmap(d->ty, map, d->offset + start);
  2354. if(debug['D']){
  2355. if(d->sym != nil)
  2356. print("descmap %s type %T offset %ld returns %ld\n",
  2357. d->sym->name, d->ty, d->offset+start, m);
  2358. else
  2359. print("descmap type %T offset %ld returns %ld\n", d->ty, d->offset+start, m);
  2360. }
  2361. if(m >= 0)
  2362. last = m;
  2363. }
  2364. return last;
  2365. }
  2366. long
  2367. tdescmap(Type *t, uchar *map, long offset)
  2368. {
  2369. Label *lab;
  2370. long i, e, m;
  2371. int bit;
  2372. if(t == nil)
  2373. return -1;
  2374. m = -1;
  2375. if(t->kind == Talt){
  2376. lab = t->cse->labs;
  2377. e = t->cse->nlab;
  2378. offset += IBY2WD * 2;
  2379. for(i = 0; i < e; i++){
  2380. if(lab[i].isptr){
  2381. bit = offset / IBY2WD % 8;
  2382. map[offset / (8*IBY2WD)] |= 1 << (7 - bit);
  2383. m = offset;
  2384. }
  2385. offset += 2*IBY2WD;
  2386. }
  2387. return m;
  2388. }
  2389. if(t->kind == Tcasec){
  2390. e = t->cse->nlab;
  2391. offset += IBY2WD;
  2392. for(i = 0; i < e; i++){
  2393. bit = offset / IBY2WD % 8;
  2394. map[offset / (8*IBY2WD)] |= 1 << (7 - bit);
  2395. offset += IBY2WD;
  2396. bit = offset / IBY2WD % 8;
  2397. map[offset / (8*IBY2WD)] |= 1 << (7 - bit);
  2398. m = offset;
  2399. offset += 2*IBY2WD;
  2400. }
  2401. return m;
  2402. }
  2403. if(tattr[t->kind].isptr){
  2404. bit = offset / IBY2WD % 8;
  2405. map[offset / (8*IBY2WD)] |= 1 << (7 - bit);
  2406. return offset;
  2407. }
  2408. if(t->kind == Tadtpick)
  2409. t = t->tof;
  2410. if(t->kind == Ttuple || t->kind == Tadt || t->kind == Texception){
  2411. if(debug['D'])
  2412. print("descmap adt offset %ld\n", offset);
  2413. if(t->rec != 0)
  2414. fatal("illegal cyclic type %t in tdescmap", t);
  2415. t->rec = 1;
  2416. offset = descmap(t->ids, map, offset);
  2417. t->rec = 0;
  2418. return offset;
  2419. }
  2420. return -1;
  2421. }
  2422. /*
  2423. * can a t2 be assigned to a t1?
  2424. * any means Tany matches all types,
  2425. * not just references
  2426. */
  2427. int
  2428. tcompat(Type *t1, Type *t2, int any)
  2429. {
  2430. int ok, v;
  2431. if(t1 == t2)
  2432. return 1;
  2433. if(t1 == nil || t2 == nil)
  2434. return 0;
  2435. if(t2->kind == Texception && t1->kind != Texception)
  2436. t2 = mkextuptype(t2);
  2437. tcomset = 0;
  2438. ok = rtcompat(t1, t2, any, 0);
  2439. v = cleartcomrec(t1) + cleartcomrec(t2);
  2440. if(v != tcomset)
  2441. fatal("recid t1 %t and t2 %t not balanced in tcompat: %d v %d", t1, t2, v, tcomset);
  2442. return ok;
  2443. }
  2444. static int
  2445. rtcompat(Type *t1, Type *t2, int any, int inaorc)
  2446. {
  2447. if(t1 == t2)
  2448. return 1;
  2449. if(t1 == nil || t2 == nil)
  2450. return 0;
  2451. if(t1->kind == Terror || t2->kind == Terror)
  2452. return 1;
  2453. if(t2->kind == Texception && t1->kind != Texception)
  2454. t2 = mkextuptype(t2);
  2455. if(debug['x'])
  2456. print("rtcompat: %t and %t\n", t1, t2);
  2457. t1->rec |= TRcom;
  2458. t2->rec |= TRcom;
  2459. switch(t1->kind){
  2460. default:
  2461. fatal("unknown type %t v %t in rtcompat", t1, t2);
  2462. case Tstring:
  2463. return t2->kind == Tstring || t2->kind == Tany;
  2464. case Texception:
  2465. if(t2->kind == Texception && t1->cons == t2->cons){
  2466. if(assumetcom(t1, t2))
  2467. return 1;
  2468. return idcompat(t1->ids, t2->ids, 0, inaorc);
  2469. }
  2470. return 0;
  2471. case Tnone:
  2472. case Tint:
  2473. case Tbig:
  2474. case Tbyte:
  2475. case Treal:
  2476. return t1->kind == t2->kind;
  2477. case Tfix:
  2478. return t1->kind == t2->kind && sametree(t1->val, t2->val);
  2479. case Tany:
  2480. if(tattr[t2->kind].isptr)
  2481. return 1;
  2482. return any;
  2483. case Tref:
  2484. case Tlist:
  2485. case Tarray:
  2486. case Tchan:
  2487. if(t1->kind != t2->kind){
  2488. if(t2->kind == Tany)
  2489. return 1;
  2490. return 0;
  2491. }
  2492. if(t1->kind != Tref && assumetcom(t1, t2))
  2493. return 1;
  2494. return rtcompat(t1->tof, t2->tof, 0, t1->kind == Tarray || t1->kind == Tchan || inaorc);
  2495. case Tfn:
  2496. break;
  2497. case Ttuple:
  2498. if(t2->kind == Tadt && t2->tags == nil
  2499. || t2->kind == Ttuple){
  2500. if(assumetcom(t1, t2))
  2501. return 1;
  2502. return idcompat(t1->ids, t2->ids, any, inaorc);
  2503. }
  2504. if(t2->kind == Tadtpick){
  2505. t2->tof->rec |= TRcom;
  2506. if(assumetcom(t1, t2->tof))
  2507. return 1;
  2508. return idcompat(t1->ids, t2->tof->ids->next, any, inaorc);
  2509. }
  2510. return 0;
  2511. case Tadt:
  2512. if(t2->kind == Ttuple && t1->tags == nil){
  2513. if(assumetcom(t1, t2))
  2514. return 1;
  2515. return idcompat(t1->ids, t2->ids, any, inaorc);
  2516. }
  2517. if(t1->tags != nil && t2->kind == Tadtpick && !inaorc)
  2518. t2 = t2->decl->dot->ty;
  2519. break;
  2520. case Tadtpick:
  2521. /*
  2522. if(t2->kind == Ttuple)
  2523. return idcompat(t1->tof->ids->next, t2->ids, any, inaorc);
  2524. */
  2525. break;
  2526. case Tmodule:
  2527. if(t2->kind == Tany)
  2528. return 1;
  2529. break;
  2530. case Tpoly:
  2531. if(t2->kind == Tany)
  2532. return 1;
  2533. break;
  2534. }
  2535. return tequal(t1, t2);
  2536. }
  2537. /*
  2538. * add the assumption that t1 and t2 are compatable
  2539. */
  2540. static int
  2541. assumetcom(Type *t1, Type *t2)
  2542. {
  2543. Type *r1, *r2;
  2544. if(t1->tcom == nil && t2->tcom == nil){
  2545. tcomset += 2;
  2546. t1->tcom = t2->tcom = t1;
  2547. }else{
  2548. if(t1->tcom == nil){
  2549. r1 = t1;
  2550. t1 = t2;
  2551. t2 = r1;
  2552. }
  2553. for(r1 = t1->tcom; r1 != r1->tcom; r1 = r1->tcom)
  2554. ;
  2555. for(r2 = t2->tcom; r2 != nil && r2 != r2->tcom; r2 = r2->tcom)
  2556. ;
  2557. if(r1 == r2)
  2558. return 1;
  2559. if(r2 == nil)
  2560. tcomset++;
  2561. t2->tcom = t1;
  2562. for(; t2 != r1; t2 = r2){
  2563. r2 = t2->tcom;
  2564. t2->tcom = r1;
  2565. }
  2566. }
  2567. return 0;
  2568. }
  2569. static int
  2570. cleartcomrec(Type *t)
  2571. {
  2572. Decl *id;
  2573. int n;
  2574. n = 0;
  2575. for(; t != nil && (t->rec & TRcom) == TRcom; t = t->tof){
  2576. t->rec &= ~TRcom;
  2577. if(t->tcom != nil){
  2578. t->tcom = nil;
  2579. n++;
  2580. }
  2581. if(t->kind == Tadtpick)
  2582. n += cleartcomrec(t->tof);
  2583. if(t->kind == Tmodule)
  2584. t = t->tof;
  2585. for(id = t->ids; id != nil; id = id->next)
  2586. n += cleartcomrec(id->ty);
  2587. for(id = t->tags; id != nil; id = id->next)
  2588. n += cleartcomrec(id->ty);
  2589. for(id = t->polys; id != nil; id = id->next)
  2590. n += cleartcomrec(id->ty);
  2591. }
  2592. return n;
  2593. }
  2594. /*
  2595. * id1 and id2 are the fields in an adt or tuple
  2596. * simple structural check; ignore names
  2597. */
  2598. static int
  2599. idcompat(Decl *id1, Decl *id2, int any, int inaorc)
  2600. {
  2601. for(; id1 != nil; id1 = id1->next){
  2602. if(id1->store != Dfield)
  2603. continue;
  2604. while(id2 != nil && id2->store != Dfield)
  2605. id2 = id2->next;
  2606. if(id2 == nil
  2607. || id1->store != id2->store
  2608. || !rtcompat(id1->ty, id2->ty, any, inaorc))
  2609. return 0;
  2610. id2 = id2->next;
  2611. }
  2612. while(id2 != nil && id2->store != Dfield)
  2613. id2 = id2->next;
  2614. return id2 == nil;
  2615. }
  2616. int
  2617. tequal(Type *t1, Type *t2)
  2618. {
  2619. int ok, v;
  2620. eqrec = 0;
  2621. eqset = 0;
  2622. ok = rtequal(t1, t2);
  2623. v = cleareqrec(t1) + cleareqrec(t2);
  2624. if(v != eqset && 0)
  2625. fatal("recid t1 %t and t2 %t not balanced in tequal: %d %d", t1, t2, v, eqset);
  2626. eqset = 0;
  2627. return ok;
  2628. }
  2629. /*
  2630. * structural equality on types
  2631. */
  2632. static int
  2633. rtequal(Type *t1, Type *t2)
  2634. {
  2635. /*
  2636. * this is just a shortcut
  2637. */
  2638. if(t1 == t2)
  2639. return 1;
  2640. if(t1 == nil || t2 == nil)
  2641. return 0;
  2642. if(t1->kind == Terror || t2->kind == Terror)
  2643. return 1;
  2644. if(t1->kind != t2->kind)
  2645. return 0;
  2646. if(t1->eq != nil && t2->eq != nil)
  2647. return t1->eq == t2->eq;
  2648. if(debug['x'])
  2649. print("rtequal: %t and %t\n", t1, t2);
  2650. t1->rec |= TReq;
  2651. t2->rec |= TReq;
  2652. switch(t1->kind){
  2653. default:
  2654. fatal("unknown type %t v %t in rtequal", t1, t2);
  2655. case Tnone:
  2656. case Tbig:
  2657. case Tbyte:
  2658. case Treal:
  2659. case Tint:
  2660. case Tstring:
  2661. /*
  2662. * this should always be caught by t1 == t2 check
  2663. */
  2664. fatal("bogus value type %t vs %t in rtequal", t1, t2);
  2665. return 1;
  2666. case Tfix:
  2667. return sametree(t1->val, t2->val);
  2668. case Tref:
  2669. case Tlist:
  2670. case Tarray:
  2671. case Tchan:
  2672. if(t1->kind != Tref && assumeteq(t1, t2))
  2673. return 1;
  2674. return rtequal(t1->tof, t2->tof);
  2675. case Tfn:
  2676. if(t1->varargs != t2->varargs)
  2677. return 0;
  2678. if(!idequal(t1->ids, t2->ids, 0, storespace))
  2679. return 0;
  2680. /* if(!idequal(t1->polys, t2->polys, 1, nil)) */
  2681. if(!pyequal(t1, t2))
  2682. return 0;
  2683. return rtequal(t1->tof, t2->tof);
  2684. case Ttuple:
  2685. case Texception:
  2686. if(t1->kind != t2->kind || t1->cons != t2->cons)
  2687. return 0;
  2688. if(assumeteq(t1, t2))
  2689. return 1;
  2690. return idequal(t1->ids, t2->ids, 0, storespace);
  2691. case Tadt:
  2692. case Tadtpick:
  2693. case Tmodule:
  2694. if(assumeteq(t1, t2))
  2695. return 1;
  2696. /*
  2697. * compare interfaces when comparing modules
  2698. */
  2699. if(t1->kind == Tmodule)
  2700. return idequal(t1->tof->ids, t2->tof->ids, 1, nil);
  2701. /*
  2702. * picked adts; check parent,
  2703. * assuming equiv picked fields,
  2704. * then check picked fields are equiv
  2705. */
  2706. if(t1->kind == Tadtpick && !rtequal(t1->decl->dot->ty, t2->decl->dot->ty))
  2707. return 0;
  2708. /*
  2709. * adts with pick tags: check picked fields for equality
  2710. */
  2711. if(!idequal(t1->tags, t2->tags, 1, nil))
  2712. return 0;
  2713. /* if(!idequal(t1->polys, t2->polys, 1, nil)) */
  2714. if(!pyequal(t1, t2))
  2715. return 0;
  2716. return idequal(t1->ids, t2->ids, 1, storespace);
  2717. case Tpoly:
  2718. if(assumeteq(t1, t2))
  2719. return 1;
  2720. if(t1->decl->sym != t2->decl->sym)
  2721. return 0;
  2722. return idequal(t1->ids, t2->ids, 1, nil);
  2723. }
  2724. }
  2725. static int
  2726. assumeteq(Type *t1, Type *t2)
  2727. {
  2728. Type *r1, *r2;
  2729. if(t1->teq == nil && t2->teq == nil){
  2730. eqrec++;
  2731. eqset += 2;
  2732. t1->teq = t2->teq = t1;
  2733. }else{
  2734. if(t1->teq == nil){
  2735. r1 = t1;
  2736. t1 = t2;
  2737. t2 = r1;
  2738. }
  2739. for(r1 = t1->teq; r1 != r1->teq; r1 = r1->teq)
  2740. ;
  2741. for(r2 = t2->teq; r2 != nil && r2 != r2->teq; r2 = r2->teq)
  2742. ;
  2743. if(r1 == r2)
  2744. return 1;
  2745. if(r2 == nil)
  2746. eqset++;
  2747. t2->teq = t1;
  2748. for(; t2 != r1; t2 = r2){
  2749. r2 = t2->teq;
  2750. t2->teq = r1;
  2751. }
  2752. }
  2753. return 0;
  2754. }
  2755. /*
  2756. * checking structural equality for adts, tuples, and fns
  2757. */
  2758. static int
  2759. idequal(Decl *id1, Decl *id2, int usenames, int *storeok)
  2760. {
  2761. /*
  2762. * this is just a shortcut
  2763. */
  2764. if(id1 == id2)
  2765. return 1;
  2766. for(; id1 != nil; id1 = id1->next){
  2767. if(storeok != nil && !storeok[id1->store])
  2768. continue;
  2769. while(id2 != nil && storeok != nil && !storeok[id2->store])
  2770. id2 = id2->next;
  2771. if(id2 == nil
  2772. || usenames && id1->sym != id2->sym
  2773. || id1->store != id2->store
  2774. || id1->implicit != id2->implicit
  2775. || id1->cyc != id2->cyc
  2776. || (id1->dot == nil) != (id2->dot == nil)
  2777. || id1->dot != nil && id2->dot != nil && id1->dot->ty->kind != id2->dot->ty->kind
  2778. || !rtequal(id1->ty, id2->ty))
  2779. return 0;
  2780. id2 = id2->next;
  2781. }
  2782. while(id2 != nil && storeok != nil && !storeok[id2->store])
  2783. id2 = id2->next;
  2784. return id1 == nil && id2 == nil;
  2785. }
  2786. static int
  2787. pyequal(Type *t1, Type *t2)
  2788. {
  2789. Type *pt1, *pt2;
  2790. Decl *id1, *id2;
  2791. if(t1 == t2)
  2792. return 1;
  2793. id1 = t1->polys;
  2794. id2 = t2->polys;
  2795. for(; id1 != nil; id1 = id1->next){
  2796. if(id2 == nil)
  2797. return 0;
  2798. pt1 = id1->ty;
  2799. pt2 = id2->ty;
  2800. if(!rtequal(pt1, pt2)){
  2801. if(t1->u.tmap != nil)
  2802. pt1 = valtmap(pt1, t1->u.tmap);
  2803. if(t2->u.tmap != nil)
  2804. pt2 = valtmap(pt2, t2->u.tmap);
  2805. if(!rtequal(pt1, pt2))
  2806. return 0;
  2807. }
  2808. id2 = id2->next;
  2809. }
  2810. return id1 == nil && id2 == nil;
  2811. }
  2812. static int
  2813. cleareqrec(Type *t)
  2814. {
  2815. Decl *id;
  2816. int n;
  2817. n = 0;
  2818. for(; t != nil && (t->rec & TReq) == TReq; t = t->tof){
  2819. t->rec &= ~TReq;
  2820. if(t->teq != nil){
  2821. t->teq = nil;
  2822. n++;
  2823. }
  2824. if(t->kind == Tadtpick)
  2825. n += cleareqrec(t->decl->dot->ty);
  2826. if(t->kind == Tmodule)
  2827. t = t->tof;
  2828. for(id = t->ids; id != nil; id = id->next)
  2829. n += cleareqrec(id->ty);
  2830. for(id = t->tags; id != nil; id = id->next)
  2831. n += cleareqrec(id->ty);
  2832. for(id = t->polys; id != nil; id = id->next)
  2833. n += cleareqrec(id->ty);
  2834. }
  2835. return n;
  2836. }
  2837. int
  2838. raisescompat(Node *n1, Node *n2)
  2839. {
  2840. if(n1 == n2)
  2841. return 1;
  2842. if(n2 == nil)
  2843. return 1; /* no need to repeat in definition if given in declaration */
  2844. if(n1 == nil)
  2845. return 0;
  2846. for(n1 = n1->left, n2 = n2->left; n1 != nil && n2 != nil; n1 = n1->right, n2 = n2->right){
  2847. if(n1->left->decl != n2->left->decl)
  2848. return 0;
  2849. }
  2850. return n1 == n2;
  2851. }
  2852. /* t1 a polymorphic type */
  2853. static int
  2854. fnunify(Type *t1, Type *t2, Tpair **tp, int swapped)
  2855. {
  2856. Decl *id, *ids;
  2857. Sym *sym;
  2858. for(ids = t1->ids; ids != nil; ids = ids->next){
  2859. sym = ids->sym;
  2860. id = fnlookup(sym, t2, nil);
  2861. if(id != nil)
  2862. usetype(id->ty);
  2863. if(id == nil){
  2864. if(dowarn)
  2865. error(unifysrc.start, "type %T does not have a '%s' function", t2, sym->name);
  2866. return 0;
  2867. }
  2868. else if(id->ty->kind != Tfn){
  2869. if(dowarn)
  2870. error(unifysrc.start, "%T is not a function", id->ty);
  2871. return 0;
  2872. }
  2873. else if(!rtunify(ids->ty, id->ty, tp, !swapped)){
  2874. if(dowarn)
  2875. error(unifysrc.start, "%T and %T are not compatible wrt %s", ids->ty, id->ty, sym->name);
  2876. return 0;
  2877. }
  2878. }
  2879. return 1;
  2880. }
  2881. static int
  2882. fncleareqrec(Type *t1, Type *t2)
  2883. {
  2884. Decl *id, *ids;
  2885. int n;
  2886. n = 0;
  2887. n += cleareqrec(t1);
  2888. n += cleareqrec(t2);
  2889. for(ids = t1->ids; ids != nil; ids = ids->next){
  2890. id = fnlookup(ids->sym, t2, nil);
  2891. if(id == nil)
  2892. continue;
  2893. else{
  2894. n += cleareqrec(ids->ty);
  2895. n += cleareqrec(id->ty);
  2896. }
  2897. }
  2898. return n;
  2899. }
  2900. int
  2901. tunify(Type *t1, Type *t2, Tpair **tp)
  2902. {
  2903. int ok, v;
  2904. Tpair *p;
  2905. *tp = nil;
  2906. eqrec = 0;
  2907. eqset = 0;
  2908. ok = rtunify(t1, t2, tp, 0);
  2909. v = cleareqrec(t1) + cleareqrec(t2);
  2910. for(p = *tp; p != nil; p = p->nxt)
  2911. v += fncleareqrec(p->t1, p->t2);
  2912. if(0 && v != eqset)
  2913. fatal("recid t1 %t and t2 %t not balanced in tunify: %d %d", t1, t2, v, eqset);
  2914. return ok;
  2915. }
  2916. static int
  2917. rtunify(Type *t1, Type *t2, Tpair **tp, int swapped)
  2918. {
  2919. Type *tmp;
  2920. if(debug['w']) print("rtunifya - %T %T\n", t1, t2);
  2921. t1 = valtmap(t1, *tp);
  2922. t2 = valtmap(t2, *tp);
  2923. if(debug['w']) print("rtunifyb - %T %T\n", t1, t2);
  2924. if(t1 == t2)
  2925. return 1;
  2926. if(t1 == nil || t2 == nil)
  2927. return 0;
  2928. if(t1->kind == Terror || t2->kind == Terror)
  2929. return 1;
  2930. if(t1->kind != Tpoly && t2->kind == Tpoly){
  2931. tmp = t1;
  2932. t1 = t2;
  2933. t2 = tmp;
  2934. swapped = !swapped;
  2935. }
  2936. if(t1->kind == Tpoly){
  2937. /*
  2938. if(typein(t1, t2))
  2939. return 0;
  2940. */
  2941. if(!tattr[t2->kind].isptr)
  2942. return 0;
  2943. if(t2->kind != Tany)
  2944. addtmap(t1, t2, tp);
  2945. return fnunify(t1, t2, tp, swapped);
  2946. }
  2947. if(t1->kind != Tany && t2->kind == Tany){
  2948. tmp = t1;
  2949. t1 = t2;
  2950. t2 = tmp;
  2951. swapped = !swapped;
  2952. }
  2953. if(t1->kind == Tadt && t1->tags != nil && t2->kind == Tadtpick && !swapped)
  2954. t2 = t2->decl->dot->ty;
  2955. if(t2->kind == Tadt && t2->tags != nil && t1->kind == Tadtpick && swapped)
  2956. t1 = t1->decl->dot->ty;
  2957. if(t1->kind != Tany && t1->kind != t2->kind)
  2958. return 0;
  2959. t1->rec |= TReq;
  2960. t2->rec |= TReq;
  2961. switch(t1->kind){
  2962. default:
  2963. return tequal(t1, t2);
  2964. case Tany:
  2965. return tattr[t2->kind].isptr;
  2966. case Tref:
  2967. case Tlist:
  2968. case Tarray:
  2969. case Tchan:
  2970. if(t1->kind != Tref && assumeteq(t1, t2))
  2971. return 1;
  2972. return rtunify(t1->tof, t2->tof, tp, swapped);
  2973. case Tfn:
  2974. if(!idunify(t1->ids, t2->ids, tp, swapped))
  2975. return 0;
  2976. if(!idunify(t1->polys, t2->polys, tp, swapped))
  2977. return 0;
  2978. return rtunify(t1->tof, t2->tof, tp, swapped);
  2979. case Ttuple:
  2980. if(assumeteq(t1, t2))
  2981. return 1;
  2982. return idunify(t1->ids, t2->ids, tp, swapped);
  2983. case Tadt:
  2984. case Tadtpick:
  2985. if(assumeteq(t1, t2))
  2986. return 1;
  2987. if(!idunify(t1->polys, t2->polys, tp, swapped))
  2988. return 0;
  2989. if(!idunify(t1->tags, t2->tags, tp, swapped))
  2990. return 0;
  2991. return idunify(t1->ids, t2->ids, tp, swapped);
  2992. case Tmodule:
  2993. if(assumeteq(t1, t2))
  2994. return 1;
  2995. return idunify(t1->tof->ids, t2->tof->ids, tp, swapped);
  2996. case Tpoly:
  2997. return t1 == t2;
  2998. }
  2999. }
  3000. static int
  3001. idunify(Decl *id1, Decl *id2, Tpair **tp, int swapped)
  3002. {
  3003. if(id1 == id2)
  3004. return 1;
  3005. for(; id1 != nil; id1 = id1->next){
  3006. if(id2 == nil || !rtunify(id1->ty, id2->ty, tp, swapped))
  3007. return 0;
  3008. id2 = id2->next;
  3009. }
  3010. return id1 == nil && id2 == nil;
  3011. }
  3012. int
  3013. polyequal(Decl *id1, Decl *id2)
  3014. {
  3015. int ck2;
  3016. Decl *d;
  3017. /* allow id2 list to have an optional for clause */
  3018. ck2 = 0;
  3019. for(d = id2; d != nil; d = d->next)
  3020. if(d->ty->ids != nil)
  3021. ck2 = 1;
  3022. for( ; id1 != nil; id1 = id1->next){
  3023. if(id2 == nil
  3024. || id1->sym != id2->sym
  3025. || id1->ty->decl != nil && id2->ty->decl != nil && id1->ty->decl->sym != id2->ty->decl->sym)
  3026. return 0;
  3027. if(ck2 && !idequal(id1->ty->ids, id2->ty->ids, 1, nil))
  3028. return 0;
  3029. id2 = id2->next;
  3030. }
  3031. return id1 == nil && id2 == nil;
  3032. }
  3033. Type*
  3034. calltype(Type *f, Node *a, Type *rt)
  3035. {
  3036. Type *t;
  3037. Decl *id, *first, *last;
  3038. first = last = nil;
  3039. t = mktype(&f->src.start, &f->src.stop, Tfn, rt, nil);
  3040. t->polys = f->kind == Tref ? f->tof->polys : f->polys;
  3041. for( ; a != nil; a = a->right){
  3042. id = mkdecl(&f->src, Darg, a->left->ty);
  3043. if(last == nil)
  3044. first = id;
  3045. else
  3046. last->next = id;
  3047. last = id;
  3048. }
  3049. t->ids = first;
  3050. if(f->kind == Tref)
  3051. t = mktype(&f->src.start, &f->src.stop, Tref, t, nil);
  3052. return t;
  3053. }
  3054. static Type*
  3055. duptype(Type *t)
  3056. {
  3057. Type *nt;
  3058. nt = allocmem(sizeof(*nt));
  3059. *nt = *t;
  3060. nt->ok &= ~(OKverify|OKref|OKclass|OKsized|OKcycsize|OKcyc);
  3061. nt->flags |= INST;
  3062. nt->eq = nil;
  3063. nt->sbl = -1;
  3064. if(t->decl != nil && (nt->kind == Tadt || nt->kind == Tadtpick || nt->kind == Ttuple)){
  3065. nt->decl = dupdecl(t->decl);
  3066. nt->decl->ty = nt;
  3067. nt->decl->link = t->decl;
  3068. if(t->decl->dot != nil){
  3069. nt->decl->dot = dupdecl(t->decl->dot);
  3070. nt->decl->dot->link = t->decl->dot;
  3071. }
  3072. }
  3073. else
  3074. nt->decl = nil;
  3075. return nt;
  3076. }
  3077. static int
  3078. dpolys(Decl *ids)
  3079. {
  3080. Decl *p;
  3081. for(p = ids; p != nil; p = p->next)
  3082. if(tpolys(p->ty))
  3083. return 1;
  3084. return 0;
  3085. }
  3086. static int
  3087. tpolys(Type *t)
  3088. {
  3089. int v;
  3090. Typelist *tl;
  3091. if(t == nil)
  3092. return 0;
  3093. if(t->flags&(POLY|NOPOLY))
  3094. return t->flags&POLY;
  3095. switch(t->kind){
  3096. default:
  3097. v = 0;
  3098. break;
  3099. case Tarrow:
  3100. case Tdot:
  3101. case Tpoly:
  3102. v = 1;
  3103. break;
  3104. case Tref:
  3105. case Tlist:
  3106. case Tarray:
  3107. case Tchan:
  3108. v = tpolys(t->tof);
  3109. break;
  3110. case Tid:
  3111. v = tpolys(t->decl->ty);
  3112. break;
  3113. case Tinst:
  3114. v = 0;
  3115. for(tl = t->u.tlist; tl != nil; tl = tl->nxt)
  3116. if(tpolys(tl->t)){
  3117. v = 1;
  3118. break;
  3119. }
  3120. if(v == 0)
  3121. v = tpolys(t->tof);
  3122. break;
  3123. case Tfn:
  3124. case Tadt:
  3125. case Tadtpick:
  3126. case Ttuple:
  3127. case Texception:
  3128. if(t->polys != nil){
  3129. v = 1;
  3130. break;
  3131. }
  3132. if(t->rec&TRvis)
  3133. return 0;
  3134. t->rec |= TRvis;
  3135. v = tpolys(t->tof) || dpolys(t->polys) || dpolys(t->ids) || dpolys(t->tags);
  3136. t->rec &= ~TRvis;
  3137. if(t->kind == Tadtpick && v == 0)
  3138. v = tpolys(t->decl->dot->ty);
  3139. break;
  3140. }
  3141. if(v)
  3142. t->flags |= POLY;
  3143. else
  3144. t->flags |= NOPOLY;
  3145. return v;
  3146. }
  3147. static int
  3148. doccurs(Decl *ids, Tpair **tp)
  3149. {
  3150. Decl *p;
  3151. for(p = ids; p != nil; p = p->next)
  3152. if(toccurs(p->ty, tp))
  3153. return 1;
  3154. return 0;
  3155. }
  3156. static int
  3157. toccurs(Type *t, Tpair **tp)
  3158. {
  3159. int o;
  3160. Typelist *tl;
  3161. if(t == nil)
  3162. return 0;
  3163. if(!(t->flags&(POLY|NOPOLY)))
  3164. tpolys(t);
  3165. if(t->flags&NOPOLY)
  3166. return 0;
  3167. switch(t->kind){
  3168. default:
  3169. fatal("unknown type %t in toccurs", t);
  3170. case Tnone:
  3171. case Tbig:
  3172. case Tbyte:
  3173. case Treal:
  3174. case Tint:
  3175. case Tstring:
  3176. case Tfix:
  3177. case Tmodule:
  3178. case Terror:
  3179. return 0;
  3180. case Tarrow:
  3181. case Tdot:
  3182. return 1;
  3183. case Tpoly:
  3184. return valtmap(t, *tp) != t;
  3185. case Tref:
  3186. case Tlist:
  3187. case Tarray:
  3188. case Tchan:
  3189. return toccurs(t->tof, tp);
  3190. case Tid:
  3191. return toccurs(t->decl->ty, tp);
  3192. case Tinst:
  3193. for(tl = t->u.tlist; tl != nil; tl = tl->nxt)
  3194. if(toccurs(tl->t, tp))
  3195. return 1;
  3196. return toccurs(t->tof, tp);
  3197. case Tfn:
  3198. case Tadt:
  3199. case Tadtpick:
  3200. case Ttuple:
  3201. case Texception:
  3202. if(t->rec&TRvis)
  3203. return 0;
  3204. t->rec |= TRvis;
  3205. o = toccurs(t->tof, tp) || doccurs(t->polys, tp) || doccurs(t->ids, tp) || doccurs(t->tags, tp);
  3206. t->rec &= ~TRvis;
  3207. if(t->kind == Tadtpick && o == 0)
  3208. o = toccurs(t->decl->dot->ty, tp);
  3209. return o;
  3210. }
  3211. }
  3212. static Decl*
  3213. expandids(Decl *ids, Decl *adtt, Tpair **tp, int sym)
  3214. {
  3215. Decl *p, *q, *nids, *last;
  3216. nids = last = nil;
  3217. for(p = ids; p != nil; p = p->next){
  3218. q = dupdecl(p);
  3219. q->ty = expandtype(p->ty, nil, adtt, tp);
  3220. if(sym && q->ty->decl != nil)
  3221. q->sym = q->ty->decl->sym;
  3222. if(q->store == Dfn){
  3223. if(debug['v']) print("%p->link = %p\n", q, p);
  3224. q->link = p;
  3225. }
  3226. if(nids == nil)
  3227. nids = q;
  3228. else
  3229. last->next = q;
  3230. last = q;
  3231. }
  3232. return nids;
  3233. }
  3234. Type*
  3235. expandtype(Type *t, Type *instt, Decl *adtt, Tpair **tp)
  3236. {
  3237. Type *nt;
  3238. Decl *ids;
  3239. if(t == nil)
  3240. return nil;
  3241. if(debug['w']) print("expandtype %d %lux %T\n", t->kind, (ulong)t, t);
  3242. if(!toccurs(t, tp))
  3243. return t;
  3244. if(debug['w']) print("\texpanding\n");
  3245. switch(t->kind){
  3246. default:
  3247. fatal("unknown type %t in expandtype", t);
  3248. case Tpoly:
  3249. return valtmap(t, *tp);
  3250. case Tref:
  3251. case Tlist:
  3252. case Tarray:
  3253. case Tchan:
  3254. nt = duptype(t);
  3255. nt->tof = expandtype(t->tof, nil, adtt, tp);
  3256. return nt;
  3257. case Tid:
  3258. return expandtype(idtype(t), nil, adtt, tp);
  3259. case Tdot:
  3260. return expandtype(dottype(t, adtt), nil, adtt, tp);
  3261. case Tarrow:
  3262. return expandtype(arrowtype(t, adtt), nil, adtt, tp);
  3263. case Tinst:
  3264. if((nt = valtmap(t, *tp)) != t)
  3265. return nt;
  3266. return expandtype(insttype(t, adtt, tp), nil, adtt, tp);
  3267. case Tfn:
  3268. case Tadt:
  3269. case Tadtpick:
  3270. case Ttuple:
  3271. case Texception:
  3272. if((nt = valtmap(t, *tp)) != t)
  3273. return nt;
  3274. if(t->kind == Tadt)
  3275. adtt = t->decl;
  3276. nt = duptype(t);
  3277. addtmap(t, nt, tp);
  3278. if(instt != nil)
  3279. addtmap(instt, nt, tp);
  3280. nt->tof = expandtype(t->tof, nil, adtt, tp);
  3281. nt->polys = expandids(t->polys, adtt, tp, 1);
  3282. nt->ids = expandids(t->ids, adtt, tp, 0);
  3283. nt->tags = expandids(t->tags, adtt, tp, 0);
  3284. if(t->kind == Tadt){
  3285. for(ids = nt->tags; ids != nil; ids = ids->next)
  3286. ids->ty->decl->dot = nt->decl;
  3287. }
  3288. if(t->kind == Tadtpick){
  3289. nt->decl->dot->ty = expandtype(t->decl->dot->ty, nil, adtt, tp);
  3290. }
  3291. if((t->kind == Tadt || t->kind == Tadtpick) && t->u.tmap != nil){
  3292. Tpair *p;
  3293. nt->u.tmap = nil;
  3294. for(p = t->u.tmap; p != nil; p = p->nxt)
  3295. addtmap(valtmap(p->t1, *tp), valtmap(p->t2, *tp), &nt->u.tmap);
  3296. if(debug['w']){
  3297. print("new tmap for %T->%T: ", t, nt);
  3298. for(p=nt->u.tmap;p!=nil;p=p->nxt)print("%T -> %T ", p->t1, p->t2);
  3299. print("\n");
  3300. }
  3301. }
  3302. return nt;
  3303. }
  3304. }
  3305. /*
  3306. * create type signatures
  3307. * sign the same information used
  3308. * for testing type equality
  3309. */
  3310. ulong
  3311. sign(Decl *d)
  3312. {
  3313. Type *t;
  3314. uchar *sig, md5sig[MD5dlen];
  3315. char buf[StrSize];
  3316. int i, sigend, sigalloc, v;
  3317. t = d->ty;
  3318. if(t->sig != 0)
  3319. return t->sig;
  3320. if(ispoly(d))
  3321. rmfnptrs(d);
  3322. sig = 0;
  3323. sigend = -1;
  3324. sigalloc = 1024;
  3325. while(sigend < 0 || sigend >= sigalloc){
  3326. sigalloc *= 2;
  3327. sig = reallocmem(sig, sigalloc);
  3328. eqrec = 0;
  3329. sigend = rtsign(t, sig, sigalloc, 0);
  3330. v = clearrec(t);
  3331. if(v != eqrec)
  3332. fatal("recid not balanced in sign: %d %d", v, eqrec);
  3333. eqrec = 0;
  3334. }
  3335. sig[sigend] = '\0';
  3336. if(signdump != nil){
  3337. seprint(buf, buf+sizeof(buf), "%D", d);
  3338. if(strcmp(buf, signdump) == 0){
  3339. print("sign %D len %d\n", d, sigend);
  3340. print("%s\n", (char*)sig);
  3341. }
  3342. }
  3343. md5(sig, sigend, md5sig, nil);
  3344. for(i = 0; i < MD5dlen; i += 4)
  3345. t->sig ^= md5sig[i+0] | (md5sig[i+1]<<8) | (md5sig[i+2]<<16) | (md5sig[i+3]<<24);
  3346. if(debug['S'])
  3347. print("signed %D type %T len %d sig %#lux\n", d, t, sigend, t->sig);
  3348. free(sig);
  3349. return t->sig;
  3350. }
  3351. enum
  3352. {
  3353. SIGSELF = 'S',
  3354. SIGVARARGS = '*',
  3355. SIGCYC = 'y',
  3356. SIGREC = '@'
  3357. };
  3358. static int sigkind[Tend] =
  3359. {
  3360. /* Tnone */ 'n',
  3361. /* Tadt */ 'a',
  3362. /* Tadtpick */ 'p',
  3363. /* Tarray */ 'A',
  3364. /* Tbig */ 'B',
  3365. /* Tbyte */ 'b',
  3366. /* Tchan */ 'C',
  3367. /* Treal */ 'r',
  3368. /* Tfn */ 'f',
  3369. /* Tint */ 'i',
  3370. /* Tlist */ 'L',
  3371. /* Tmodule */ 'm',
  3372. /* Tref */ 'R',
  3373. /* Tstring */ 's',
  3374. /* Ttuple */ 't',
  3375. /* Texception */ 'e',
  3376. /* Tfix */ 'x',
  3377. /* Tpoly */ 'P',
  3378. };
  3379. static int
  3380. rtsign(Type *t, uchar *sig, int lensig, int spos)
  3381. {
  3382. Decl *id, *tg;
  3383. char name[32];
  3384. int kind, lenname;
  3385. if(t == nil)
  3386. return spos;
  3387. if(spos < 0 || spos + 8 >= lensig)
  3388. return -1;
  3389. if(t->eq != nil && t->eq->id){
  3390. if(t->eq->id < 0 || t->eq->id > eqrec)
  3391. fatal("sign rec %T %d %d", t, t->eq->id, eqrec);
  3392. sig[spos++] = SIGREC;
  3393. seprint(name, name+sizeof(name), "%d", t->eq->id);
  3394. lenname = strlen(name);
  3395. if(spos + lenname > lensig)
  3396. return -1;
  3397. strcpy((char*)&sig[spos], name);
  3398. spos += lenname;
  3399. return spos;
  3400. }
  3401. if(t->eq != nil){
  3402. eqrec++;
  3403. t->eq->id = eqrec;
  3404. }
  3405. kind = sigkind[t->kind];
  3406. sig[spos++] = kind;
  3407. if(kind == 0)
  3408. fatal("no sigkind for %t", t);
  3409. t->rec = 1;
  3410. switch(t->kind){
  3411. default:
  3412. fatal("bogus type %t in rtsign", t);
  3413. return -1;
  3414. case Tnone:
  3415. case Tbig:
  3416. case Tbyte:
  3417. case Treal:
  3418. case Tint:
  3419. case Tstring:
  3420. case Tpoly:
  3421. return spos;
  3422. case Tfix:
  3423. seprint(name, name+sizeof(name), "%g", t->val->rval);
  3424. lenname = strlen(name);
  3425. if(spos+lenname-1 >= lensig)
  3426. return -1;
  3427. strcpy((char*)&sig[spos], name);
  3428. spos += lenname;
  3429. return spos;
  3430. case Tref:
  3431. case Tlist:
  3432. case Tarray:
  3433. case Tchan:
  3434. return rtsign(t->tof, sig, lensig, spos);
  3435. case Tfn:
  3436. if(t->varargs != 0)
  3437. sig[spos++] = SIGVARARGS;
  3438. if(t->polys != nil)
  3439. spos = idsign(t->polys, 0, sig, lensig, spos);
  3440. spos = idsign(t->ids, 0, sig, lensig, spos);
  3441. if(t->u.eraises)
  3442. spos = raisessign(t->u.eraises, sig, lensig, spos);
  3443. return rtsign(t->tof, sig, lensig, spos);
  3444. case Ttuple:
  3445. return idsign(t->ids, 0, sig, lensig, spos);
  3446. case Tadt:
  3447. /*
  3448. * this is a little different than in rtequal,
  3449. * since we flatten the adt we used to represent the globals
  3450. */
  3451. if(t->eq == nil){
  3452. if(strcmp(t->decl->sym->name, ".mp") != 0)
  3453. fatal("no t->eq field for %t", t);
  3454. spos--;
  3455. for(id = t->ids; id != nil; id = id->next){
  3456. spos = idsign1(id, 1, sig, lensig, spos);
  3457. if(spos < 0 || spos >= lensig)
  3458. return -1;
  3459. sig[spos++] = ';';
  3460. }
  3461. return spos;
  3462. }
  3463. if(t->polys != nil)
  3464. spos = idsign(t->polys, 0, sig, lensig, spos);
  3465. spos = idsign(t->ids, 1, sig, lensig, spos);
  3466. if(spos < 0 || t->tags == nil)
  3467. return spos;
  3468. /*
  3469. * convert closing ')' to a ',', then sign any tags
  3470. */
  3471. sig[spos-1] = ',';
  3472. for(tg = t->tags; tg != nil; tg = tg->next){
  3473. lenname = tg->sym->len;
  3474. if(spos + lenname + 2 >= lensig)
  3475. return -1;
  3476. strcpy((char*)&sig[spos], tg->sym->name);
  3477. spos += lenname;
  3478. sig[spos++] = '=';
  3479. sig[spos++] = '>';
  3480. spos = rtsign(tg->ty, sig, lensig, spos);
  3481. if(spos < 0 || spos >= lensig)
  3482. return -1;
  3483. if(tg->next != nil)
  3484. sig[spos++] = ',';
  3485. }
  3486. if(spos >= lensig)
  3487. return -1;
  3488. sig[spos++] = ')';
  3489. return spos;
  3490. case Tadtpick:
  3491. spos = idsign(t->ids, 1, sig, lensig, spos);
  3492. if(spos < 0)
  3493. return spos;
  3494. return rtsign(t->decl->dot->ty, sig, lensig, spos);
  3495. case Tmodule:
  3496. if(t->tof->linkall == 0)
  3497. fatal("signing a narrowed module");
  3498. if(spos >= lensig)
  3499. return -1;
  3500. sig[spos++] = '{';
  3501. for(id = t->tof->ids; id != nil; id = id->next){
  3502. if(id->tag)
  3503. continue;
  3504. if(strcmp(id->sym->name, ".mp") == 0){
  3505. spos = rtsign(id->ty, sig, lensig, spos);
  3506. if(spos < 0)
  3507. return -1;
  3508. continue;
  3509. }
  3510. spos = idsign1(id, 1, sig, lensig, spos);
  3511. if(spos < 0 || spos >= lensig)
  3512. return -1;
  3513. sig[spos++] = ';';
  3514. }
  3515. if(spos >= lensig)
  3516. return -1;
  3517. sig[spos++] = '}';
  3518. return spos;
  3519. }
  3520. }
  3521. static int
  3522. idsign(Decl *id, int usenames, uchar *sig, int lensig, int spos)
  3523. {
  3524. int first;
  3525. if(spos >= lensig)
  3526. return -1;
  3527. sig[spos++] = '(';
  3528. first = 1;
  3529. for(; id != nil; id = id->next){
  3530. if(id->store == Dlocal)
  3531. fatal("local %s in idsign", id->sym->name);
  3532. if(!storespace[id->store])
  3533. continue;
  3534. if(!first){
  3535. if(spos >= lensig)
  3536. return -1;
  3537. sig[spos++] = ',';
  3538. }
  3539. spos = idsign1(id, usenames, sig, lensig, spos);
  3540. if(spos < 0)
  3541. return -1;
  3542. first = 0;
  3543. }
  3544. if(spos >= lensig)
  3545. return -1;
  3546. sig[spos++] = ')';
  3547. return spos;
  3548. }
  3549. static int
  3550. idsign1(Decl *id, int usenames, uchar *sig, int lensig, int spos)
  3551. {
  3552. char *name;
  3553. int lenname;
  3554. if(usenames){
  3555. name = id->sym->name;
  3556. lenname = id->sym->len;
  3557. if(spos + lenname + 1 >= lensig)
  3558. return -1;
  3559. strcpy((char*)&sig[spos], name);
  3560. spos += lenname;
  3561. sig[spos++] = ':';
  3562. }
  3563. if(spos + 2 >= lensig)
  3564. return -1;
  3565. if(id->implicit != 0)
  3566. sig[spos++] = SIGSELF;
  3567. if(id->cyc != 0)
  3568. sig[spos++] = SIGCYC;
  3569. return rtsign(id->ty, sig, lensig, spos);
  3570. }
  3571. static int
  3572. raisessign(Node *n, uchar *sig, int lensig, int spos)
  3573. {
  3574. int m;
  3575. char *s;
  3576. Node *nn;
  3577. if(spos >= lensig)
  3578. return -1;
  3579. sig[spos++] = '(';
  3580. for(nn = n->left; nn != nil; nn = nn->right){
  3581. s = nn->left->decl->sym->name;
  3582. m = nn->left->decl->sym->len;
  3583. if(spos+m-1 >= lensig)
  3584. return -1;
  3585. strcpy((char*)&sig[spos], s);
  3586. spos += m;
  3587. if(nn->right != nil){
  3588. if(spos >= lensig)
  3589. return -1;
  3590. sig[spos++] = ',';
  3591. }
  3592. }
  3593. if(spos >= lensig)
  3594. return -1;
  3595. sig[spos++] = ')';
  3596. return spos;
  3597. }
  3598. static int
  3599. clearrec(Type *t)
  3600. {
  3601. Decl *id;
  3602. int n;
  3603. n = 0;
  3604. for(; t != nil && t->rec; t = t->tof){
  3605. t->rec = 0;
  3606. if(t->eq != nil && t->eq->id != 0){
  3607. t->eq->id = 0;
  3608. n++;
  3609. }
  3610. if(t->kind == Tmodule){
  3611. for(id = t->tof->ids; id != nil; id = id->next)
  3612. n += clearrec(id->ty);
  3613. return n;
  3614. }
  3615. if(t->kind == Tadtpick)
  3616. n += clearrec(t->decl->dot->ty);
  3617. for(id = t->ids; id != nil; id = id->next)
  3618. n += clearrec(id->ty);
  3619. for(id = t->tags; id != nil; id = id->next)
  3620. n += clearrec(id->ty);
  3621. for(id = t->polys; id != nil; id = id->next)
  3622. n += clearrec(id->ty);
  3623. }
  3624. return n;
  3625. }
  3626. /* must a variable of the given type be zeroed ? (for uninitialized declarations inside loops) */
  3627. int
  3628. tmustzero(Type *t)
  3629. {
  3630. if(t==nil)
  3631. return 0;
  3632. if(tattr[t->kind].isptr)
  3633. return 1;
  3634. if(t->kind == Tadtpick)
  3635. t = t->tof;
  3636. if(t->kind == Ttuple || t->kind == Tadt)
  3637. return mustzero(t->ids);
  3638. return 0;
  3639. }
  3640. int
  3641. mustzero(Decl *decls)
  3642. {
  3643. Decl *d;
  3644. for (d = decls; d != nil; d = d->next)
  3645. if (tmustzero(d->ty))
  3646. return 1;
  3647. return 0;
  3648. }
  3649. int
  3650. typeconv(Fmt *f)
  3651. {
  3652. Type *t;
  3653. char *p, buf[1024];
  3654. t = va_arg(f->args, Type*);
  3655. if(t == nil){
  3656. p = "nothing";
  3657. }else{
  3658. p = buf;
  3659. buf[0] = 0;
  3660. tprint(buf, buf+sizeof(buf), t);
  3661. }
  3662. return fmtstrcpy(f, p);
  3663. }
  3664. int
  3665. stypeconv(Fmt *f)
  3666. {
  3667. Type *t;
  3668. char *p, buf[1024];
  3669. t = va_arg(f->args, Type*);
  3670. if(t == nil){
  3671. p = "nothing";
  3672. }else{
  3673. p = buf;
  3674. buf[0] = 0;
  3675. stprint(buf, buf+sizeof(buf), t);
  3676. }
  3677. return fmtstrcpy(f, p);
  3678. }
  3679. int
  3680. ctypeconv(Fmt *f)
  3681. {
  3682. Type *t;
  3683. char buf[1024];
  3684. t = va_arg(f->args, Type*);
  3685. buf[0] = 0;
  3686. ctprint(buf, buf+sizeof(buf), t);
  3687. return fmtstrcpy(f, buf);
  3688. }
  3689. char*
  3690. tprint(char *buf, char *end, Type *t)
  3691. {
  3692. Decl *id;
  3693. Typelist *tl;
  3694. if(t == nil)
  3695. return buf;
  3696. if(t->kind >= Tend)
  3697. return seprint(buf, end, "kind %d", t->kind);
  3698. switch(t->kind){
  3699. case Tarrow:
  3700. buf = seprint(buf, end, "%T->%s", t->tof, t->decl->sym->name);
  3701. break;
  3702. case Tdot:
  3703. buf = seprint(buf, end, "%T.%s", t->tof, t->decl->sym->name);
  3704. break;
  3705. case Tid:
  3706. case Tpoly:
  3707. buf = seprint(buf, end, "%s", t->decl->sym->name);
  3708. break;
  3709. case Tinst:
  3710. buf = tprint(buf, end, t->tof);
  3711. buf = secpy(buf ,end, "[");
  3712. for(tl = t->u.tlist; tl != nil; tl = tl->nxt){
  3713. buf = tprint(buf, end, tl->t);
  3714. if(tl->nxt != nil)
  3715. buf = secpy(buf, end, ", ");
  3716. }
  3717. buf = secpy(buf, end, "]");
  3718. break;
  3719. case Tint:
  3720. case Tbig:
  3721. case Tstring:
  3722. case Treal:
  3723. case Tbyte:
  3724. case Tany:
  3725. case Tnone:
  3726. case Terror:
  3727. case Tainit:
  3728. case Talt:
  3729. case Tcase:
  3730. case Tcasel:
  3731. case Tcasec:
  3732. case Tgoto:
  3733. case Tiface:
  3734. case Texception:
  3735. case Texcept:
  3736. buf = secpy(buf, end, kindname[t->kind]);
  3737. break;
  3738. case Tfix:
  3739. buf = seprint(buf, end, "%s(%v)", kindname[t->kind], t->val);
  3740. break;
  3741. case Tref:
  3742. buf = secpy(buf, end, "ref ");
  3743. buf = tprint(buf, end, t->tof);
  3744. break;
  3745. case Tchan:
  3746. case Tarray:
  3747. case Tlist:
  3748. buf = seprint(buf, end, "%s of ", kindname[t->kind]);
  3749. buf = tprint(buf, end, t->tof);
  3750. break;
  3751. case Tadtpick:
  3752. buf = seprint(buf, end, "%s.%s", t->decl->dot->sym->name, t->decl->sym->name);
  3753. break;
  3754. case Tadt:
  3755. if(t->decl->dot != nil && !isimpmod(t->decl->dot->sym))
  3756. buf = seprint(buf, end, "%s->%s", t->decl->dot->sym->name, t->decl->sym->name);
  3757. else
  3758. buf = seprint(buf, end, "%s", t->decl->sym->name);
  3759. if(t->polys != nil){
  3760. buf = secpy(buf ,end, "[");
  3761. for(id = t->polys; id != nil; id = id->next){
  3762. if(t->u.tmap != nil)
  3763. buf = tprint(buf, end, valtmap(id->ty, t->u.tmap));
  3764. else
  3765. buf = seprint(buf, end, "%s", id->sym->name);
  3766. if(id->next != nil)
  3767. buf = secpy(buf, end, ", ");
  3768. }
  3769. buf = secpy(buf, end, "]");
  3770. }
  3771. break;
  3772. case Tmodule:
  3773. buf = seprint(buf, end, "%s", t->decl->sym->name);
  3774. break;
  3775. case Ttuple:
  3776. buf = secpy(buf, end, "(");
  3777. for(id = t->ids; id != nil; id = id->next){
  3778. buf = tprint(buf, end, id->ty);
  3779. if(id->next != nil)
  3780. buf = secpy(buf, end, ", ");
  3781. }
  3782. buf = secpy(buf, end, ")");
  3783. break;
  3784. case Tfn:
  3785. buf = secpy(buf, end, "fn");
  3786. if(t->polys != nil){
  3787. buf = secpy(buf, end, "[");
  3788. for(id = t->polys; id != nil; id = id->next){
  3789. buf = seprint(buf, end, "%s", id->sym->name);
  3790. if(id->next != nil)
  3791. buf = secpy(buf, end, ", ");
  3792. }
  3793. buf = secpy(buf, end, "]");
  3794. }
  3795. buf = secpy(buf, end, "(");
  3796. for(id = t->ids; id != nil; id = id->next){
  3797. if(id->sym == nil)
  3798. buf = secpy(buf, end, "nil: ");
  3799. else
  3800. buf = seprint(buf, end, "%s: ", id->sym->name);
  3801. if(id->implicit)
  3802. buf = secpy(buf, end, "self ");
  3803. buf = tprint(buf, end, id->ty);
  3804. if(id->next != nil)
  3805. buf = secpy(buf, end, ", ");
  3806. }
  3807. if(t->varargs && t->ids != nil)
  3808. buf = secpy(buf, end, ", *");
  3809. else if(t->varargs)
  3810. buf = secpy(buf, end, "*");
  3811. if(t->tof != nil && t->tof->kind != Tnone){
  3812. buf = secpy(buf, end, "): ");
  3813. buf = tprint(buf, end, t->tof);
  3814. break;
  3815. }
  3816. buf = secpy(buf, end, ")");
  3817. break;
  3818. default:
  3819. yyerror("tprint: unknown type kind %d", t->kind);
  3820. break;
  3821. }
  3822. return buf;
  3823. }
  3824. char*
  3825. stprint(char *buf, char *end, Type *t)
  3826. {
  3827. if(t == nil)
  3828. return buf;
  3829. switch(t->kind){
  3830. case Tid:
  3831. return seprint(buf, end, "id %s", t->decl->sym->name);
  3832. case Tadt:
  3833. case Tadtpick:
  3834. case Tmodule:
  3835. buf = secpy(buf, end, kindname[t->kind]);
  3836. buf = secpy(buf, end, " ");
  3837. return tprint(buf, end, t);
  3838. }
  3839. return tprint(buf, end, t);
  3840. }
  3841. /* generalize ref P.A, ref P.B to ref P */
  3842. /*
  3843. Type*
  3844. tparentx(Type *t1, Type* t2)
  3845. {
  3846. if(t1 == nil || t2 == nil || t1->kind != Tref || t2->kind != Tref)
  3847. return t1;
  3848. t1 = t1->tof;
  3849. t2 = t2->tof;
  3850. if(t1 == nil || t2 == nil || t1->kind != Tadtpick || t2->kind != Tadtpick)
  3851. return t1;
  3852. t1 = t1->decl->dot->ty;
  3853. t2 = t2->decl->dot->ty;
  3854. if(tequal(t1, t2))
  3855. return mktype(&t1->src.start, &t1->src.stop, Tref, t1, nil);
  3856. return t1;
  3857. }
  3858. */
  3859. static int
  3860. tparent0(Type *t1, Type *t2)
  3861. {
  3862. Decl *id1, *id2;
  3863. if(t1 == t2)
  3864. return 1;
  3865. if(t1 == nil || t2 == nil)
  3866. return 0;
  3867. if(t1->kind == Tadt && t2->kind == Tadtpick)
  3868. t2 = t2->decl->dot->ty;
  3869. if(t1->kind == Tadtpick && t2->kind == Tadt)
  3870. t1 = t1->decl->dot->ty;
  3871. if(t1->kind != t2->kind)
  3872. return 0;
  3873. switch(t1->kind){
  3874. default:
  3875. fatal("unknown type %t v %t in tparent", t1, t2);
  3876. break;
  3877. case Terror:
  3878. case Tstring:
  3879. case Tnone:
  3880. case Tint:
  3881. case Tbig:
  3882. case Tbyte:
  3883. case Treal:
  3884. case Tany:
  3885. return 1;
  3886. case Texception:
  3887. case Tfix:
  3888. case Tfn:
  3889. case Tadt:
  3890. case Tmodule:
  3891. case Tpoly:
  3892. return tcompat(t1, t2, 0);
  3893. case Tref:
  3894. case Tlist:
  3895. case Tarray:
  3896. case Tchan:
  3897. return tparent0(t1->tof, t2->tof);
  3898. case Ttuple:
  3899. for(id1 = t1->ids, id2 = t2->ids; id1 != nil && id2 != nil; id1 = id1->next, id2 = id2->next)
  3900. if(!tparent0(id1->ty, id2->ty))
  3901. return 0;
  3902. return id1 == nil && id2 == nil;
  3903. case Tadtpick:
  3904. return tequal(t1->decl->dot->ty, t2->decl->dot->ty);
  3905. }
  3906. return 0;
  3907. }
  3908. static Type*
  3909. tparent1(Type *t1, Type *t2)
  3910. {
  3911. Type *t, *nt;
  3912. Decl *id, *id1, *id2, *idt;
  3913. if(t1->kind == Tadt && t2->kind == Tadtpick)
  3914. t2 = t2->decl->dot->ty;
  3915. if(t1->kind == Tadtpick && t2->kind == Tadt)
  3916. t1 = t1->decl->dot->ty;
  3917. switch(t1->kind){
  3918. default:
  3919. return t1;
  3920. case Tref:
  3921. case Tlist:
  3922. case Tarray:
  3923. case Tchan:
  3924. t = tparent1(t1->tof, t2->tof);
  3925. if(t == t1->tof)
  3926. return t1;
  3927. return mktype(&t1->src.start, &t1->src.stop, t1->kind, t, nil);
  3928. case Ttuple:
  3929. nt = nil;
  3930. id = nil;
  3931. for(id1 = t1->ids, id2 = t2->ids; id1 != nil && id2 != nil; id1 = id1->next, id2 = id2->next){
  3932. t = tparent1(id1->ty, id2->ty);
  3933. if(t != id1->ty){
  3934. if(nt == nil){
  3935. nt = mktype(&t1->src.start, &t1->src.stop, Ttuple, nil, dupdecls(t1->ids));
  3936. for(id = nt->ids, idt = t1->ids; idt != id1; id = id->next, idt = idt->next)
  3937. ;
  3938. }
  3939. id->ty = t;
  3940. }
  3941. if(id != nil)
  3942. id = id->next;
  3943. }
  3944. if(nt == nil)
  3945. return t1;
  3946. return nt;
  3947. case Tadtpick:
  3948. if(tequal(t1, t2))
  3949. return t1;
  3950. return t1->decl->dot->ty;
  3951. }
  3952. }
  3953. Type*
  3954. tparent(Type *t1, Type *t2)
  3955. {
  3956. if(tparent0(t1, t2))
  3957. return tparent1(t1, t2);
  3958. return t1;
  3959. }
  3960. /*
  3961. * make the tuple type used to initialize an exception type
  3962. */
  3963. Type*
  3964. mkexbasetype(Type *t)
  3965. {
  3966. Decl *id, *new, *last;
  3967. Type *nt;
  3968. if(!t->cons)
  3969. fatal("mkexbasetype on non-constant");
  3970. last = mkids(&t->decl->src, nil, tstring, nil);
  3971. last->store = Dfield;
  3972. nt = mktype(&t->src.start, &t->src.stop, Texception, nil, last);
  3973. nt->cons = 0;
  3974. new = mkids(&t->decl->src, nil, tint, nil);
  3975. new->store = Dfield;
  3976. last->next = new;
  3977. last = new;
  3978. for(id = t->ids; id != nil; id = id->next){
  3979. new = allocmem(sizeof *id);
  3980. *new = *id;
  3981. new->cyc = 0;
  3982. last->next = new;
  3983. last = new;
  3984. }
  3985. last->next = nil;
  3986. return usetype(nt);
  3987. }
  3988. /*
  3989. * make an instantiated exception type
  3990. */
  3991. Type*
  3992. mkextype(Type *t)
  3993. {
  3994. Type *nt;
  3995. if(!t->cons)
  3996. fatal("mkextype on non-constant");
  3997. if(t->tof != nil)
  3998. return t->tof;
  3999. nt = copytypeids(t);
  4000. nt->cons = 0;
  4001. t->tof = usetype(nt);
  4002. return t->tof;
  4003. }
  4004. /*
  4005. * convert an instantiated exception type to its underlying type
  4006. */
  4007. Type*
  4008. mkextuptype(Type *t)
  4009. {
  4010. Decl *id;
  4011. Type *nt;
  4012. if(t->cons)
  4013. return t;
  4014. if(t->tof != nil)
  4015. return t->tof;
  4016. id = t->ids;
  4017. if(id == nil)
  4018. nt = t;
  4019. else if(id->next == nil)
  4020. nt = id->ty;
  4021. else{
  4022. nt = copytypeids(t);
  4023. nt->cons = 0;
  4024. nt->kind = Ttuple;
  4025. }
  4026. t->tof = usetype(nt);
  4027. return t->tof;
  4028. }
  4029. static void
  4030. ckfix(Type *t, double max)
  4031. {
  4032. int p;
  4033. vlong k, x;
  4034. double s;
  4035. s = t->val->rval;
  4036. if(max == 0.0)
  4037. k = ((vlong)1<<32)-1;
  4038. else
  4039. k = 2*(vlong)(max/s+0.5)+1;
  4040. x = 1;
  4041. for(p = 0; k > x; p++)
  4042. x *= 2;
  4043. if(p == 0 || p > 32){
  4044. error(t->src.start, "cannot fit fixed type into an int");
  4045. return;
  4046. }
  4047. if(p < 32)
  4048. t->val->rval /= (double)(1<<(32-p));
  4049. }
  4050. double
  4051. scale(Type *t)
  4052. {
  4053. Node *n;
  4054. if(t->kind == Tint || t->kind == Treal)
  4055. return 1.0;
  4056. if(t->kind != Tfix)
  4057. fatal("scale() on non fixed point type");
  4058. n = t->val;
  4059. if(n->op != Oconst)
  4060. fatal("non constant scale");
  4061. if(n->ty != treal)
  4062. fatal("non real scale");
  4063. return n->rval;
  4064. }
  4065. double
  4066. scale2(Type *f, Type *t)
  4067. {
  4068. return scale(f)/scale(t);
  4069. }
  4070. #define I(x) ((int)(x))
  4071. #define V(x) ((Long)(x))
  4072. #define D(x) ((double)(x))
  4073. /* put x in normal form */
  4074. static int
  4075. nf(double x, int *mant)
  4076. {
  4077. int p;
  4078. double m;
  4079. p = 0;
  4080. m = x;
  4081. while(m >= 1){
  4082. p++;
  4083. m /= 2;
  4084. }
  4085. while(m < 0.5){
  4086. p--;
  4087. m *= 2;
  4088. }
  4089. m *= D(1<<16)*D(1<<15);
  4090. if(m >= D(0x7fffffff) - 0.5){
  4091. *mant = 0x7fffffff;
  4092. return p;
  4093. }
  4094. *mant = I(m+0.5);
  4095. return p;
  4096. }
  4097. static int
  4098. ispow2(double x)
  4099. {
  4100. int m;
  4101. nf(x, &m);
  4102. if(m != 1<<30)
  4103. return 0;
  4104. return 1;
  4105. }
  4106. static int
  4107. fround(double x, int n, int *m)
  4108. {
  4109. if(n != 31)
  4110. fatal("not 31 in fround");
  4111. return nf(x, m);
  4112. }
  4113. static int
  4114. fixmul2(double sx, double sy, double sr, int *rp, int *ra)
  4115. {
  4116. int k, n, a;
  4117. double alpha;
  4118. alpha = (sx*sy)/sr;
  4119. n = 31;
  4120. k = fround(1/alpha, n, &a);
  4121. *rp = 1-k;
  4122. *ra = 0;
  4123. return IMULX;
  4124. }
  4125. static int
  4126. fixdiv2(double sx, double sy, double sr, int *rp, int *ra)
  4127. {
  4128. int k, n, b;
  4129. double beta;
  4130. beta = sx/(sy*sr);
  4131. n = 31;
  4132. k = fround(beta, n, &b);
  4133. *rp = k-1;
  4134. *ra = 0;
  4135. return IDIVX;
  4136. }
  4137. static int
  4138. fixmul(double sx, double sy, double sr, int *rp, int *ra)
  4139. {
  4140. int k, m, n, a, v;
  4141. vlong W;
  4142. double alpha, eps;
  4143. alpha = (sx*sy)/sr;
  4144. if(ispow2(alpha))
  4145. return fixmul2(sx, sy, sr, rp, ra);
  4146. n = 31;
  4147. k = fround(1/alpha, n, &a);
  4148. m = n-k;
  4149. if(m < -n-1)
  4150. return IMOVW; /* result is zero whatever the values */
  4151. v = 0;
  4152. W = 0;
  4153. eps = D(1<<m)/(alpha*D(a)) - 1;
  4154. if(eps < 0){
  4155. v = a-1;
  4156. eps = -eps;
  4157. }
  4158. if(m < 0 && D(1<<n)*eps*D(a) >= D(a)-1+D(1<<m))
  4159. W = (V(1)<<(-m)) - 1;
  4160. if(v != 0 || W != 0)
  4161. m = m<<2|(v != 0)<<1|(W != 0);
  4162. *rp = m;
  4163. *ra = a;
  4164. return v == 0 && W == 0 ? IMULX0: IMULX1;
  4165. }
  4166. static int
  4167. fixdiv(double sx, double sy, double sr, int *rp, int *ra)
  4168. {
  4169. int k, m, n, b, v;
  4170. vlong W;
  4171. double beta, eps;
  4172. beta = sx/(sy*sr);
  4173. if(ispow2(beta))
  4174. return fixdiv2(sx, sy, sr, rp, ra);
  4175. n = 31;
  4176. k = fround(beta, n, &b);
  4177. m = k-n;
  4178. if(m <= -2*n)
  4179. return IMOVW; /* result is zero whatever the values */
  4180. v = 0;
  4181. W = 0;
  4182. eps = (D(1<<m)*D(b))/beta - 1;
  4183. if(eps < 0)
  4184. v = 1;
  4185. if(m < 0)
  4186. W = (V(1)<<(-m)) - 1;
  4187. if(v != 0 || W != 0)
  4188. m = m<<2|(v != 0)<<1|(W != 0);
  4189. *rp = m;
  4190. *ra = b;
  4191. return v == 0 && W == 0 ? IDIVX0: IDIVX1;
  4192. }
  4193. static int
  4194. fixcast(double sx, double sr, int *rp, int *ra)
  4195. {
  4196. int op;
  4197. op = fixmul(sx, 1.0, sr, rp, ra);
  4198. return op-IMULX+ICVTXX;
  4199. }
  4200. int
  4201. fixop(int op, Type *tx, Type *ty, Type *tr, int *rp, int *ra)
  4202. {
  4203. double sx, sy, sr;
  4204. sx = scale(tx);
  4205. sy = scale(ty);
  4206. sr = scale(tr);
  4207. if(op == IMULX)
  4208. op = fixmul(sx, sy, sr, rp, ra);
  4209. else if(op == IDIVX)
  4210. op = fixdiv(sx, sy, sr, rp, ra);
  4211. else
  4212. op = fixcast(sx, sr, rp, ra);
  4213. return op;
  4214. }
  4215. int
  4216. ispoly(Decl *d)
  4217. {
  4218. Type *t;
  4219. if(d == nil)
  4220. return 0;
  4221. t = d->ty;
  4222. if(t->kind == Tfn){
  4223. if(t->polys != nil)
  4224. return 1;
  4225. if((d = d->dot) == nil)
  4226. return 0;
  4227. t = d->ty;
  4228. return t->kind == Tadt && t->polys != nil;
  4229. }
  4230. return 0;
  4231. }
  4232. int
  4233. ispolyadt(Type *t)
  4234. {
  4235. return (t->kind == Tadt || t->kind == Tadtpick) && t->polys != nil && !(t->flags & INST);
  4236. }
  4237. Decl*
  4238. polydecl(Decl *ids)
  4239. {
  4240. Decl *id;
  4241. Type *t;
  4242. for(id = ids; id != nil; id = id->next){
  4243. t = mktype(&id->src.start, &id->src.stop, Tpoly, nil, nil);
  4244. id->ty = t;
  4245. t->decl = id;
  4246. }
  4247. return ids;
  4248. }
  4249. /* try to convert an expression tree to a type */
  4250. Type*
  4251. exptotype(Node *n)
  4252. {
  4253. Type *t, *tt;
  4254. Decl *d;
  4255. Typelist *tl;
  4256. Src *src;
  4257. if(n == nil)
  4258. return nil;
  4259. t = nil;
  4260. switch(n->op){
  4261. case Oname:
  4262. if((d = n->decl) != nil && d->store == Dtype)
  4263. t = d->ty;
  4264. break;
  4265. case Otype:
  4266. case Ochan:
  4267. t = n->ty;
  4268. break;
  4269. case Oref:
  4270. t = exptotype(n->left);
  4271. if(t != nil)
  4272. t = mktype(&n->src.start, &n->src.stop, Tref, t, nil);
  4273. break;
  4274. case Odot:
  4275. t = exptotype(n->left);
  4276. if(t != nil){
  4277. d = namedot(t->tags, n->right->decl->sym);
  4278. if(d == nil)
  4279. t = nil;
  4280. else
  4281. t = d->ty;
  4282. }
  4283. if(t == nil)
  4284. t = exptotype(n->right);
  4285. break;
  4286. case Omdot:
  4287. t = exptotype(n->right);
  4288. break;
  4289. case Oindex:
  4290. t = exptotype(n->left);
  4291. if(t != nil){
  4292. src = &n->src;
  4293. tl = nil;
  4294. for(n = n->right; n != nil; n = n->right){
  4295. if(n->op == Oseq)
  4296. tt = exptotype(n->left);
  4297. else
  4298. tt = exptotype(n);
  4299. if(tt == nil)
  4300. return nil;
  4301. tl = addtype(tt, tl);
  4302. if(n->op != Oseq)
  4303. break;
  4304. }
  4305. t = mkinsttype(src, t, tl);
  4306. }
  4307. break;
  4308. }
  4309. return t;
  4310. }
  4311. static char*
  4312. uname(Decl *im)
  4313. {
  4314. Decl *p;
  4315. int n;
  4316. char *s;
  4317. n = 0;
  4318. for(p = im; p != nil; p = p->next)
  4319. n += strlen(p->sym->name)+1;
  4320. s = allocmem(n);
  4321. strcpy(s, "");
  4322. for(p = im; p != nil; p = p->next){
  4323. strcat(s, p->sym->name);
  4324. if(p->next != nil)
  4325. strcat(s, "+");
  4326. }
  4327. return s;
  4328. }
  4329. /* check all implementation modules have consistent declarations
  4330. * and create their union if needed
  4331. */
  4332. Decl*
  4333. modimp(Dlist *dl, Decl *im)
  4334. {
  4335. Decl *u, *d, *dd, *ids, *dot, *last;
  4336. Sym *s;
  4337. Dlist *dl0;
  4338. long sg, sg0;
  4339. char buf[StrSize], *un;
  4340. if(dl->next == nil)
  4341. return dl->d;
  4342. dl0 = dl;
  4343. sg0 = 0;
  4344. un = uname(im);
  4345. seprint(buf, buf+sizeof(buf), ".m.%s", un);
  4346. installids(Dglobal, mkids(&dl->d->src, enter(buf, 0), tnone, nil));
  4347. u = dupdecl(dl->d);
  4348. u->sym = enter(un, 0);
  4349. u->sym->decl = u;
  4350. u->ty = mktype(&u->src.start, &u->src.stop, Tmodule, nil, nil);
  4351. u->ty->decl = u;
  4352. last = nil;
  4353. for( ; dl != nil; dl = dl->next){
  4354. d = dl->d;
  4355. ids = d->ty->tof->ids; /* iface */
  4356. if(ids != nil && ids->store == Dglobal) /* .mp */
  4357. sg = sign(ids);
  4358. else
  4359. sg = 0;
  4360. if(dl == dl0)
  4361. sg0 = sg;
  4362. else if(sg != sg0)
  4363. error(d->src.start, "%s's module data not consistent with that of %s\n", d->sym->name, dl0->d->sym->name);
  4364. for(ids = d->ty->ids; ids != nil; ids = ids->next){
  4365. s = ids->sym;
  4366. if(s->decl != nil && s->decl->scope >= scope){
  4367. if(ids == s->decl){
  4368. dd = dupdecl(ids);
  4369. if(u->ty->ids == nil)
  4370. u->ty->ids = dd;
  4371. else
  4372. last->next = dd;
  4373. last = dd;
  4374. continue;
  4375. }
  4376. dot = s->decl->dot;
  4377. if(s->decl->store != Dwundef && dot != nil && dot != d && isimpmod(dot->sym) && dequal(ids, s->decl, 1))
  4378. ids->refs = s->decl->refs;
  4379. else
  4380. redecl(ids);
  4381. ids->init = s->decl->init;
  4382. }
  4383. }
  4384. }
  4385. u->ty = usetype(u->ty);
  4386. return u;
  4387. }
  4388. static void
  4389. modres(Decl *d)
  4390. {
  4391. Decl *ids, *id, *n, *i;
  4392. Type *t;
  4393. for(ids = d->ty->ids; ids != nil; ids = ids->next){
  4394. id = ids->sym->decl;
  4395. if(ids != id){
  4396. n = ids->next;
  4397. i = ids->iface;
  4398. t = ids->ty;
  4399. *ids = *id;
  4400. ids->next = n;
  4401. ids->iface = i;
  4402. ids->ty = t;
  4403. }
  4404. }
  4405. }
  4406. /* update the fields of duplicate declarations in other implementation modules
  4407. * and their union
  4408. */
  4409. void
  4410. modresolve(void)
  4411. {
  4412. Dlist *dl;
  4413. dl = impdecls;
  4414. if(dl->next == nil)
  4415. return;
  4416. for( ; dl != nil; dl = dl->next)
  4417. modres(dl->d);
  4418. modres(impdecl);
  4419. }