check_expr.cpp 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115
  1. enum CallArgumentError {
  2. CallArgumentError_None,
  3. CallArgumentError_NoneProcedureType,
  4. CallArgumentError_WrongTypes,
  5. CallArgumentError_NonVariadicExpand,
  6. CallArgumentError_VariadicTuple,
  7. CallArgumentError_MultipleVariadicExpand,
  8. CallArgumentError_ArgumentCount,
  9. CallArgumentError_TooFewArguments,
  10. CallArgumentError_TooManyArguments,
  11. CallArgumentError_InvalidFieldValue,
  12. CallArgumentError_ParameterNotFound,
  13. CallArgumentError_ParameterMissing,
  14. CallArgumentError_DuplicateParameter,
  15. CallArgumentError_NoneConstantParameter,
  16. };
  17. enum CallArgumentErrorMode {
  18. CallArgumentMode_NoErrors,
  19. CallArgumentMode_ShowErrors,
  20. };
  21. struct CallArgumentData {
  22. Entity *gen_entity;
  23. i64 score;
  24. Type * result_type;
  25. };
  26. struct PolyProcData {
  27. Entity * gen_entity;
  28. ProcedureInfo proc_info;
  29. };
  30. #define CALL_ARGUMENT_CHECKER(name) CallArgumentError name(Checker *c, AstNode *call, Type *proc_type, Entity *entity, Array<Operand> operands, CallArgumentErrorMode show_error_mode, CallArgumentData *data)
  31. typedef CALL_ARGUMENT_CHECKER(CallArgumentCheckerType);
  32. void check_expr (Checker *c, Operand *operand, AstNode *expression);
  33. void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
  34. void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression, Type *type_hint = nullptr);
  35. ExprKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint);
  36. void check_expr_with_type_hint (Checker *c, Operand *o, AstNode *e, Type *t);
  37. Type * check_type (Checker *c, AstNode *expression, Type *named_type = nullptr);
  38. void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def);
  39. Entity * check_selector (Checker *c, Operand *operand, AstNode *node, Type *type_hint);
  40. void check_not_tuple (Checker *c, Operand *operand);
  41. void convert_to_typed (Checker *c, Operand *operand, Type *target_type, i32 level);
  42. gbString expr_to_string (AstNode *expression);
  43. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type);
  44. void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr, Type *named_type);
  45. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  46. void update_expr_type (Checker *c, AstNode *e, Type *type, bool final);
  47. bool check_is_terminating (AstNode *node);
  48. bool check_has_break (AstNode *stmt, bool implicit);
  49. void check_stmt (Checker *c, AstNode *node, u32 flags);
  50. void check_stmt_list (Checker *c, Array<AstNode *> stmts, u32 flags);
  51. void check_init_constant (Checker *c, Entity *e, Operand *operand);
  52. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
  53. bool check_procedure_type (Checker *c, Type *type, AstNode *proc_type_node, Array<Operand> *operands = nullptr);
  54. CallArgumentData check_call_arguments (Checker *c, Operand *operand, Type *proc_type, AstNode *call);
  55. void error_operand_not_expression(Operand *o) {
  56. if (o->mode == Addressing_Type) {
  57. gbString err = expr_to_string(o->expr);
  58. error(o->expr, "`%s` is not an expression but a type", err);
  59. gb_string_free(err);
  60. o->mode = Addressing_Invalid;
  61. }
  62. }
  63. void error_operand_no_value(Operand *o) {
  64. if (o->mode == Addressing_NoValue) {
  65. gbString err = expr_to_string(o->expr);
  66. error(o->expr, "`%s` used as value", err);
  67. gb_string_free(err);
  68. o->mode = Addressing_Invalid;
  69. }
  70. }
  71. void check_scope_decls(Checker *c, Array<AstNode *> nodes, isize reserve_size) {
  72. Scope *s = c->context.scope;
  73. GB_ASSERT(!s->is_file);
  74. check_collect_entities(c, nodes, false);
  75. for_array(i, s->elements.entries) {
  76. Entity *e = s->elements.entries[i].value;
  77. switch (e->kind) {
  78. case Entity_Constant:
  79. case Entity_TypeName:
  80. case Entity_Procedure:
  81. break;
  82. default:
  83. continue;
  84. }
  85. DeclInfo *d = decl_info_of_entity(&c->info, e);
  86. if (d != nullptr) {
  87. check_entity_decl(c, e, d, nullptr);
  88. }
  89. }
  90. for_array(i, s->elements.entries) {
  91. Entity *e = s->elements.entries[i].value;
  92. if (e->kind != Entity_Procedure) {
  93. continue;
  94. }
  95. check_procedure_overloading(c, e);
  96. }
  97. }
  98. bool check_is_assignable_to_using_subtype(Type *src, Type *dst) {
  99. bool src_is_ptr = false;
  100. Type *prev_src = src;
  101. src = type_deref(src);
  102. src_is_ptr = src != prev_src;
  103. src = base_type(src);
  104. if (!is_type_struct(src)) {
  105. return false;
  106. }
  107. for (isize i = 0; i < src->Record.field_count; i++) {
  108. Entity *f = src->Record.fields[i];
  109. if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
  110. continue;
  111. }
  112. if (are_types_identical(f->type, dst)) {
  113. return true;
  114. }
  115. if (src_is_ptr && is_type_pointer(dst)) {
  116. if (are_types_identical(f->type, type_deref(dst))) {
  117. return true;
  118. }
  119. }
  120. bool ok = check_is_assignable_to_using_subtype(f->type, dst);
  121. if (ok) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. bool find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity, Type *type,
  128. Array<Operand> *param_operands, PolyProcData *poly_proc_data,
  129. bool check_later) {
  130. ///////////////////////////////////////////////////////////////////////////////
  131. // //
  132. // TODO CLEANUP(bill): This procedure is very messy and hacky. Clean this!!! //
  133. // //
  134. ///////////////////////////////////////////////////////////////////////////////
  135. if (base_entity == nullptr) {
  136. return false;
  137. }
  138. if (!is_type_proc(base_entity->type)) {
  139. return false;
  140. }
  141. Type *src = base_type(base_entity->type);
  142. Type *dst = nullptr;
  143. if (type != nullptr) dst = base_type(type);
  144. if (param_operands == nullptr) {
  145. GB_ASSERT(dst != nullptr);
  146. }
  147. if (param_operands != nullptr) {
  148. GB_ASSERT(dst == nullptr);
  149. }
  150. if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
  151. return false;
  152. }
  153. if (dst != nullptr) {
  154. if (dst->Proc.is_polymorphic) {
  155. return false;
  156. }
  157. if (dst->Proc.param_count != src->Proc.param_count ||
  158. dst->Proc.result_count != src->Proc.result_count) {
  159. return false;
  160. }
  161. }
  162. DeclInfo *old_decl = decl_info_of_entity(&c->info, base_entity);
  163. if (old_decl == nullptr) {
  164. return false;
  165. }
  166. gbAllocator a = heap_allocator();
  167. Array<Operand> operands = {};
  168. if (param_operands) {
  169. operands = *param_operands;
  170. } else {
  171. array_init(&operands, a, dst->Proc.param_count);
  172. for (isize i = 0; i < dst->Proc.param_count; i++) {
  173. Entity *param = dst->Proc.params->Tuple.variables[i];
  174. Operand o = {Addressing_Value};
  175. o.type = param->type;
  176. array_add(&operands, o);
  177. }
  178. }
  179. defer (if (param_operands == nullptr) {
  180. array_free(&operands);
  181. });
  182. CheckerContext prev_context = c->context;
  183. defer (c->context = prev_context);
  184. Scope *scope = make_scope(base_entity->scope, a);
  185. scope->is_proc = true;
  186. c->context.scope = scope;
  187. c->context.allow_polymorphic_types = true;
  188. if (c->context.polymorphic_scope == nullptr) {
  189. c->context.polymorphic_scope = scope;
  190. }
  191. if (param_operands == nullptr) {
  192. // c->context.no_polymorphic_errors = false;
  193. }
  194. bool generate_type_again = c->context.no_polymorphic_errors;
  195. auto *pt = &src->Proc;
  196. // NOTE(bill): This is slightly memory leaking if the type already exists
  197. // Maybe it's better to check with the previous types first?
  198. Type *final_proc_type = make_type_proc(c->allocator, scope, nullptr, 0, nullptr, 0, false, pt->calling_convention);
  199. bool success = check_procedure_type(c, final_proc_type, pt->node, &operands);
  200. if (!success) {
  201. return false;
  202. }
  203. auto *found_gen_procs = map_get(&c->info.gen_procs, hash_pointer(base_entity->identifier));
  204. if (found_gen_procs) {
  205. auto procs = *found_gen_procs;
  206. for_array(i, procs) {
  207. Entity *other = procs[i];
  208. Type *pt = base_type(other->type);
  209. if (are_types_identical(pt, final_proc_type)) {
  210. if (poly_proc_data) {
  211. poly_proc_data->gen_entity = other;
  212. }
  213. return true;
  214. }
  215. }
  216. }
  217. if (generate_type_again) {
  218. // LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice
  219. bool prev_no_polymorphic_errors = c->context.no_polymorphic_errors;
  220. defer (c->context.no_polymorphic_errors = prev_no_polymorphic_errors);
  221. c->context.no_polymorphic_errors = false;
  222. // NOTE(bill): Reset scope from the failed procedure type
  223. scope_reset(scope);
  224. success = check_procedure_type(c, final_proc_type, pt->node, &operands);
  225. if (!success) {
  226. return false;
  227. }
  228. if (found_gen_procs) {
  229. auto procs = *found_gen_procs;
  230. for_array(i, procs) {
  231. Entity *other = procs[i];
  232. Type *pt = base_type(other->type);
  233. if (are_types_identical(pt, final_proc_type)) {
  234. if (poly_proc_data) {
  235. poly_proc_data->gen_entity = other;
  236. }
  237. return true;
  238. }
  239. }
  240. }
  241. }
  242. AstNode *proc_lit = clone_ast_node(a, old_decl->proc_lit);
  243. ast_node(pl, ProcLit, proc_lit);
  244. // NOTE(bill): Associate the scope declared above with this procedure declaration's type
  245. add_scope(c, pl->type, final_proc_type->Proc.scope);
  246. final_proc_type->Proc.is_poly_specialized = true;
  247. final_proc_type->Proc.is_polymorphic = true;
  248. u64 tags = base_entity->Procedure.tags;
  249. AstNode *ident = clone_ast_node(a, base_entity->identifier);
  250. Token token = ident->Ident.token;
  251. DeclInfo *d = make_declaration_info(c->allocator, scope, old_decl->parent);
  252. d->gen_proc_type = final_proc_type;
  253. d->type_expr = pl->type;
  254. d->proc_lit = proc_lit;
  255. Entity *entity = make_entity_procedure(c->allocator, nullptr, token, final_proc_type, tags);
  256. entity->identifier = ident;
  257. add_entity_and_decl_info(c, ident, entity, d);
  258. // NOTE(bill): Set the scope afterwards as this is not real overloading
  259. entity->scope = scope->parent;
  260. AstFile *file = nullptr;
  261. {
  262. Scope *s = entity->scope;
  263. while (s != nullptr && s->file == nullptr) {
  264. s = s->parent;
  265. }
  266. file = s->file;
  267. }
  268. ProcedureInfo proc_info = {};
  269. proc_info.file = file;
  270. proc_info.token = token;
  271. proc_info.decl = d;
  272. proc_info.type = final_proc_type;
  273. proc_info.body = pl->body;
  274. proc_info.tags = tags;
  275. proc_info.generated_from_polymorphic = true;
  276. if (found_gen_procs) {
  277. array_add(found_gen_procs, entity);
  278. } else {
  279. Array<Entity *> array = {};
  280. array_init(&array, heap_allocator());
  281. array_add(&array, entity);
  282. map_set(&c->info.gen_procs, hash_pointer(base_entity->identifier), array);
  283. }
  284. GB_ASSERT(entity != nullptr);
  285. if (poly_proc_data) {
  286. poly_proc_data->gen_entity = entity;
  287. poly_proc_data->proc_info = proc_info;
  288. }
  289. if (check_later) {
  290. // NOTE(bill): Check the newly generated procedure body
  291. check_procedure_later(c, proc_info);
  292. }
  293. return true;
  294. }
  295. bool check_polymorphic_procedure_assignment(Checker *c, Operand *operand, Type *type, PolyProcData *poly_proc_data) {
  296. if (operand->expr == NULL) return false;
  297. Entity *base_entity = entity_of_ident(&c->info, operand->expr);
  298. if (base_entity == nullptr) return false;
  299. return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_proc_data, true);
  300. }
  301. bool find_or_generate_polymorphic_procedure_from_parameters(Checker *c, Entity *base_entity, Array<Operand> *operands, PolyProcData *poly_proc_data) {
  302. return find_or_generate_polymorphic_procedure(c, base_entity, nullptr, operands, poly_proc_data, false);
  303. }
  304. i64 check_distance_between_types(Checker *c, Operand *operand, Type *type) {
  305. if (operand->mode == Addressing_Invalid ||
  306. type == t_invalid) {
  307. return 0;
  308. }
  309. if (operand->mode == Addressing_Builtin) {
  310. return -1;
  311. }
  312. if (operand->mode == Addressing_Type) {
  313. return -1;
  314. }
  315. Type *s = operand->type;
  316. if (are_types_identical(s, type)) {
  317. return 0;
  318. }
  319. Type *src = base_type(s);
  320. Type *dst = base_type(type);
  321. if (is_type_untyped_undef(src)) {
  322. if (type_has_undef(dst)) {
  323. return 1;
  324. }
  325. return -1;
  326. }
  327. if (is_type_untyped_nil(src)) {
  328. if (type_has_nil(dst)) {
  329. return 1;
  330. }
  331. return -1;
  332. }
  333. if (is_type_untyped(src)) {
  334. if (is_type_any(dst)) {
  335. // NOTE(bill): Anything can cast to `Any`
  336. add_type_info_type(c, s);
  337. return 10;
  338. }
  339. if (dst->kind == Type_Basic) {
  340. if (operand->mode == Addressing_Constant) {
  341. if (check_representable_as_constant(c, operand->value, dst, nullptr)) {
  342. if (is_type_typed(dst) && src->kind == Type_Basic) {
  343. switch (src->Basic.kind) {
  344. case Basic_UntypedRune:
  345. if (is_type_integer(dst) || is_type_rune(dst)) {
  346. return 1;
  347. }
  348. break;
  349. case Basic_UntypedInteger:
  350. if (is_type_integer(dst) || is_type_rune(dst)) {
  351. return 1;
  352. }
  353. break;
  354. case Basic_UntypedFloat:
  355. if (is_type_float(dst)) {
  356. return 1;
  357. }
  358. break;
  359. case Basic_UntypedComplex:
  360. if (is_type_complex(dst)) {
  361. return 1;
  362. }
  363. break;
  364. }
  365. }
  366. return 2;
  367. }
  368. return -1;
  369. }
  370. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedRune) {
  371. if (is_type_integer(dst) || is_type_rune(dst)) {
  372. if (is_type_typed(type)) {
  373. return 2;
  374. }
  375. return 1;
  376. }
  377. return -1;
  378. }
  379. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  380. if (is_type_boolean(dst)) {
  381. if (is_type_typed(type)) {
  382. return 2;
  383. }
  384. return 1;
  385. }
  386. return -1;
  387. }
  388. }
  389. }
  390. if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
  391. return 1;
  392. }
  393. if (is_type_bit_field_value(operand->type) && is_type_integer(type)) {
  394. Type *bfv = base_type(operand->type);
  395. i32 bits = bfv->BitFieldValue.bits;
  396. i32 size = next_pow2((bits+7)/8);
  397. i32 dst_size = type_size_of(c->allocator, type);
  398. i32 diff = gb_abs(dst_size - size);
  399. // TODO(bill): figure out a decent rule here
  400. return 1;
  401. }
  402. if (check_is_assignable_to_using_subtype(operand->type, type)) {
  403. return 4;
  404. }
  405. // ^T <- rawptr
  406. #if 0
  407. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  408. if (is_type_pointer(dst) && is_type_rawptr(src)) {
  409. return true;
  410. }
  411. #endif
  412. #if 1
  413. // TODO(bill): Should I allow this implicit conversion at all?!
  414. // rawptr <- ^T
  415. if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
  416. return 5;
  417. }
  418. #endif
  419. if (is_type_union(dst)) {
  420. for (isize i = 0; i < dst->Union.variant_count; i++) {
  421. Type *vt = dst->Union.variants[i];
  422. if (are_types_identical(vt, s)) {
  423. return 1;
  424. }
  425. }
  426. }
  427. if (is_type_proc(dst)) {
  428. if (are_types_identical(src, dst)) {
  429. return 3;
  430. }
  431. PolyProcData poly_proc_data = {};
  432. if (check_polymorphic_procedure_assignment(c, operand, type, &poly_proc_data)) {
  433. add_entity_use(c, operand->expr, poly_proc_data.gen_entity);
  434. return 4;
  435. }
  436. }
  437. if (is_type_vector(dst)) {
  438. Type *elem = base_vector_type(dst);
  439. i64 distance = check_distance_between_types(c, operand, elem);
  440. if (distance >= 0) {
  441. return distance + 5;
  442. }
  443. }
  444. if (is_type_any(dst)) {
  445. if (!is_type_polymorphic(src)) {
  446. // NOTE(bill): Anything can cast to `Any`
  447. add_type_info_type(c, s);
  448. return 10;
  449. }
  450. }
  451. return -1;
  452. }
  453. i64 assign_score_function(i64 distance) {
  454. // TODO(bill): A decent score function
  455. return gb_max(1000000 - distance*distance, 0);
  456. }
  457. bool check_is_assignable_to_with_score(Checker *c, Operand *operand, Type *type, i64 *score_) {
  458. i64 score = 0;
  459. i64 distance = check_distance_between_types(c, operand, type);
  460. bool ok = distance >= 0;
  461. if (ok) {
  462. score = assign_score_function(distance);
  463. }
  464. if (score_) *score_ = score;
  465. return ok;
  466. }
  467. bool check_is_assignable_to(Checker *c, Operand *operand, Type *type) {
  468. i64 score = 0;
  469. return check_is_assignable_to_with_score(c, operand, type, &score);
  470. }
  471. // NOTE(bill): `content_name` is for debugging and error messages
  472. void check_assignment(Checker *c, Operand *operand, Type *type, String context_name) {
  473. check_not_tuple(c, operand);
  474. if (operand->mode == Addressing_Invalid) {
  475. return;
  476. }
  477. #if 0
  478. if (operand->mode == Addressing_Type) {
  479. Type *t = base_type(type);
  480. if (t->kind == Type_Pointer &&
  481. t->Pointer.elem == t_type_info) {
  482. add_type_info_type(c, type);
  483. return;
  484. }
  485. }
  486. #endif
  487. if (is_type_untyped(operand->type)) {
  488. Type *target_type = type;
  489. if (type == nullptr || is_type_any(type)) {
  490. if (type == nullptr && is_type_untyped_nil(operand->type)) {
  491. error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
  492. operand->mode = Addressing_Invalid;
  493. return;
  494. }
  495. if (type == nullptr && is_type_untyped_undef(operand->type)) {
  496. error(operand->expr, "Use of --- in %.*s", LIT(context_name));
  497. operand->mode = Addressing_Invalid;
  498. return;
  499. }
  500. target_type = default_type(operand->type);
  501. if (type != nullptr && !is_type_any(type)) {
  502. GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
  503. }
  504. add_type_info_type(c, type);
  505. add_type_info_type(c, target_type);
  506. }
  507. if (target_type != nullptr && is_type_vector(target_type)) {
  508. // NOTE(bill): continue to below
  509. } else {
  510. convert_to_typed(c, operand, target_type, 0);
  511. if (operand->mode == Addressing_Invalid) {
  512. return;
  513. }
  514. }
  515. }
  516. if (type == nullptr) {
  517. return;
  518. }
  519. if (operand->mode == Addressing_Overload) {
  520. // GB_PANIC("HERE!\n");
  521. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  522. defer (gb_temp_arena_memory_end(tmp));
  523. Entity **procs = operand->overload_entities;
  524. isize overload_count = operand->overload_count;
  525. bool good = false;
  526. // NOTE(bill): These should be done
  527. for (isize i = 0; i < overload_count; i++) {
  528. Type *t = base_type(procs[i]->type);
  529. if (t == t_invalid) {
  530. continue;
  531. }
  532. Operand x = {};
  533. x.mode = Addressing_Value;
  534. x.type = t;
  535. if (check_is_assignable_to(c, &x, type)) {
  536. Entity *e = procs[i];
  537. add_entity_use(c, operand->expr, e);
  538. good = true;
  539. break;
  540. }
  541. }
  542. if (!good) {
  543. gbString expr_str = expr_to_string(operand->expr);
  544. gbString op_type_str = type_to_string(operand->type);
  545. gbString type_str = type_to_string(type);
  546. defer (gb_string_free(type_str));
  547. defer (gb_string_free(op_type_str));
  548. defer (gb_string_free(expr_str));
  549. // TODO(bill): is this a good enough error message?
  550. error(operand->expr,
  551. "Cannot assign overloaded procedure `%s` to `%s` in %.*s",
  552. expr_str,
  553. op_type_str,
  554. LIT(context_name));
  555. operand->mode = Addressing_Invalid;
  556. }
  557. return;
  558. }
  559. if (!check_is_assignable_to(c, operand, type)) {
  560. gbString expr_str = expr_to_string(operand->expr);
  561. gbString op_type_str = type_to_string(operand->type);
  562. gbString type_str = type_to_string(type);
  563. defer (gb_string_free(type_str));
  564. defer (gb_string_free(op_type_str));
  565. defer (gb_string_free(expr_str));
  566. if (operand->mode == Addressing_Builtin) {
  567. // TODO(bill): is this a good enough error message?
  568. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  569. error(operand->expr,
  570. "Cannot assign built-in procedure `%s` in %.*s",
  571. expr_str,
  572. LIT(context_name));
  573. } else if (operand->mode == Addressing_Type) {
  574. // TODO(bill): is this a good enough error message?
  575. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  576. error(operand->expr,
  577. "Cannot assign `%s` which is a type in %.*s",
  578. op_type_str,
  579. LIT(context_name));
  580. } else {
  581. // TODO(bill): is this a good enough error message?
  582. error(operand->expr,
  583. "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
  584. expr_str,
  585. op_type_str,
  586. type_str,
  587. LIT(context_name));
  588. }
  589. operand->mode = Addressing_Invalid;
  590. return;
  591. }
  592. }
  593. void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *> *entity_map) {
  594. t = base_type(type_deref(t));
  595. gbString str = nullptr;
  596. defer (gb_string_free(str));
  597. if (node != nullptr) {
  598. str = expr_to_string(node);
  599. }
  600. if (t->kind == Type_Record) {
  601. for (isize i = 0; i < t->Record.field_count; i++) {
  602. Entity *f = t->Record.fields[i];
  603. GB_ASSERT(f->kind == Entity_Variable);
  604. String name = f->token.string;
  605. HashKey key = hash_string(name);
  606. Entity **found = map_get(entity_map, key);
  607. if (found != nullptr) {
  608. Entity *e = *found;
  609. // TODO(bill): Better type error
  610. if (str != nullptr) {
  611. error(e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
  612. } else {
  613. error(e->token, "`%.*s` is already declared`", LIT(name));
  614. }
  615. } else {
  616. map_set(entity_map, key, f);
  617. add_entity(c, c->context.scope, nullptr, f);
  618. if (f->flags & EntityFlag_Using) {
  619. populate_using_entity_map(c, node, f->type, entity_map);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *record_node, String context) {
  626. if (decl->kind == AstNode_WhenStmt) {
  627. ast_node(ws, WhenStmt, decl);
  628. Operand operand = {Addressing_Invalid};
  629. check_expr(c, &operand, ws->cond);
  630. if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
  631. error(ws->cond, "Non-constant boolean `when` condition");
  632. return;
  633. }
  634. if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
  635. error(ws->cond, "Invalid body for `when` statement");
  636. return;
  637. }
  638. if (operand.value.kind == ExactValue_Bool &&
  639. operand.value.value_bool) {
  640. for_array(i, ws->body->BlockStmt.stmts) {
  641. AstNode *stmt = ws->body->BlockStmt.stmts[i];
  642. check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
  643. }
  644. } else if (ws->else_stmt) {
  645. switch (ws->else_stmt->kind) {
  646. case AstNode_BlockStmt:
  647. for_array(i, ws->else_stmt->BlockStmt.stmts) {
  648. AstNode *stmt = ws->else_stmt->BlockStmt.stmts[i];
  649. check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
  650. }
  651. break;
  652. case AstNode_WhenStmt:
  653. check_record_field_decl(c, ws->else_stmt, fields, entity_map, record_node, context);
  654. break;
  655. default:
  656. error(ws->else_stmt, "Invalid `else` statement in `when` statement");
  657. break;
  658. }
  659. }
  660. }
  661. if (decl->kind != AstNode_ValueDecl) {
  662. return;
  663. }
  664. ast_node(vd, ValueDecl, decl);
  665. if (!vd->is_mutable) return;
  666. Type *type = nullptr;
  667. if (vd->type != nullptr) {
  668. type = check_type(c, vd->type);
  669. } else {
  670. error(vd->names[0], "Expected a type for this field");
  671. type = t_invalid;
  672. }
  673. bool is_using = (vd->flags&VarDeclFlag_using) != 0;
  674. if (is_using && vd->names.count > 1) {
  675. error(vd->names[0], "Cannot apply `using` to more than one of the same type");
  676. is_using = false;
  677. }
  678. if (!vd->is_mutable) {
  679. error(vd->names[0], "Immutable values in a %.*s are not yet supported", LIT(context));
  680. return;
  681. }
  682. if (vd->values.count) {
  683. error(vd->values[0], "Default values are not allowed within a %.*s", LIT(context));
  684. }
  685. for_array(name_index, vd->names) {
  686. AstNode *name = vd->names[name_index];
  687. if (!ast_node_expect(name, AstNode_Ident)) {
  688. return;
  689. }
  690. Token name_token = name->Ident.token;
  691. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)fields->count);
  692. e->identifier = name;
  693. if (is_blank_ident(name_token)) {
  694. array_add(fields, e);
  695. } else if (name_token.string == "__tag") {
  696. error(name, "`__tag` is a reserved identifier for fields");
  697. } else {
  698. HashKey key = hash_string(name_token.string);
  699. Entity **found = map_get(entity_map, key);
  700. if (found != nullptr) {
  701. Entity *e = *found;
  702. // NOTE(bill): Scope checking already checks the declaration but in many cases, this can happen so why not?
  703. // This may be a little janky but it's not really that much of a problem
  704. error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
  705. error(e->token, "\tpreviously declared");
  706. } else {
  707. map_set(entity_map, key, e);
  708. array_add(fields, e);
  709. add_entity(c, c->context.scope, name, e);
  710. }
  711. add_entity_use(c, name, e);
  712. }
  713. }
  714. Entity *using_index_expr = nullptr;
  715. if (is_using) {
  716. Type *t = base_type(type_deref(type));
  717. if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
  718. vd->names.count >= 1 &&
  719. vd->names[0]->kind == AstNode_Ident) {
  720. Token name_token = vd->names[0]->Ident.token;
  721. if (is_type_indexable(t)) {
  722. bool ok = true;
  723. for_array(emi, entity_map->entries) {
  724. Entity *e = entity_map->entries[emi].value;
  725. if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
  726. if (is_type_indexable(e->type)) {
  727. if (e->identifier != vd->names[0]) {
  728. ok = false;
  729. using_index_expr = e;
  730. break;
  731. }
  732. }
  733. }
  734. }
  735. if (ok) {
  736. using_index_expr = (*fields)[fields->count-1];
  737. } else {
  738. (*fields)[fields->count-1]->flags &= ~EntityFlag_Using;
  739. error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
  740. }
  741. } else {
  742. gbString type_str = type_to_string(type);
  743. error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
  744. gb_string_free(type_str);
  745. return;
  746. }
  747. }
  748. populate_using_entity_map(c, record_node, type, entity_map);
  749. }
  750. }
  751. // Returns filled field_count
  752. Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
  753. isize init_field_capacity, String context) {
  754. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  755. defer (gb_temp_arena_memory_end(tmp));
  756. Array<Entity *> fields = {};
  757. array_init(&fields, heap_allocator(), init_field_capacity);
  758. Map<Entity *> entity_map = {};
  759. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*init_field_capacity);
  760. if (node != nullptr) {
  761. GB_ASSERT(node->kind != AstNode_UnionType);
  762. }
  763. check_collect_entities(c, decls, false);
  764. for_array(i, c->context.scope->elements.entries) {
  765. Entity *e = c->context.scope->elements.entries[i].value;
  766. DeclInfo *d = nullptr;
  767. switch (e->kind) {
  768. default: continue;
  769. case Entity_Constant:
  770. case Entity_TypeName:
  771. d = decl_info_of_entity(&c->info, e);
  772. if (d != nullptr) {
  773. check_entity_decl(c, e, d, nullptr);
  774. }
  775. break;
  776. }
  777. }
  778. for_array(decl_index, decls) {
  779. check_record_field_decl(c, decls[decl_index], &fields, &entity_map, node, str_lit("struct"));
  780. }
  781. return fields;
  782. }
  783. // TODO(bill): Cleanup struct field reordering
  784. // TODO(bill): Inline sorting procedure?
  785. gb_global gbAllocator __checker_allocator = {};
  786. GB_COMPARE_PROC(cmp_reorder_struct_fields) {
  787. // Rule:
  788. // `using` over non-`using`
  789. // Biggest to smallest alignment
  790. // if same alignment: biggest to smallest size
  791. // if same size: order by source order
  792. Entity *x = *(Entity **)a;
  793. Entity *y = *(Entity **)b;
  794. GB_ASSERT(x != nullptr);
  795. GB_ASSERT(y != nullptr);
  796. GB_ASSERT(x->kind == Entity_Variable);
  797. GB_ASSERT(y->kind == Entity_Variable);
  798. bool xu = (x->flags & EntityFlag_Using) != 0;
  799. bool yu = (y->flags & EntityFlag_Using) != 0;
  800. i64 xa = type_align_of(__checker_allocator, x->type);
  801. i64 ya = type_align_of(__checker_allocator, y->type);
  802. i64 xs = type_size_of(__checker_allocator, x->type);
  803. i64 ys = type_size_of(__checker_allocator, y->type);
  804. if (xu != yu) {
  805. return xu ? -1 : +1;
  806. }
  807. if (xa != ya) {
  808. return xa > ya ? -1 : xa < ya;
  809. }
  810. if (xs != ys) {
  811. return xs > ys ? -1 : xs < ys;
  812. }
  813. i32 diff = x->Variable.field_index - y->Variable.field_index;
  814. return diff < 0 ? -1 : diff > 0;
  815. }
  816. Entity *make_names_field_for_record(Checker *c, Scope *scope) {
  817. Entity *e = make_entity_field(c->allocator, scope,
  818. make_token_ident(str_lit("names")), t_string_slice, false, 0);
  819. e->Variable.is_immutable = true;
  820. e->flags |= EntityFlag_TypeField;
  821. return e;
  822. }
  823. void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Operand> *poly_operands) {
  824. GB_ASSERT(is_type_struct(struct_type));
  825. ast_node(st, StructType, node);
  826. isize min_field_count = 0;
  827. for_array(field_index, st->fields) {
  828. AstNode *field = st->fields[field_index];
  829. switch (field->kind) {
  830. case_ast_node(f, ValueDecl, field);
  831. min_field_count += f->names.count;
  832. case_end;
  833. }
  834. }
  835. struct_type->Record.names = make_names_field_for_record(c, c->context.scope);
  836. Type *polymorphic_params = nullptr;
  837. bool is_polymorphic = false;
  838. if (st->polymorphic_params != nullptr) {
  839. ast_node(field_list, FieldList, st->polymorphic_params);
  840. Array<AstNode *> params = field_list->list;
  841. if (params.count != 0) {
  842. isize variable_count = 0;
  843. for_array(i, params) {
  844. AstNode *field = params[i];
  845. if (ast_node_expect(field, AstNode_Field)) {
  846. ast_node(f, Field, field);
  847. variable_count += gb_max(f->names.count, 1);
  848. }
  849. }
  850. Array<Entity *> entities = {};
  851. array_init(&entities, c->allocator, variable_count);
  852. for_array(i, params) {
  853. AstNode *param = params[i];
  854. if (param->kind != AstNode_Field) {
  855. continue;
  856. }
  857. ast_node(p, Field, param);
  858. AstNode *type_expr = p->type;
  859. Type *type = nullptr;
  860. bool is_type_param = false;
  861. bool is_type_polymorphic_type = false;
  862. if (type_expr == nullptr) {
  863. error(param, "Expected a type for this parameter");
  864. continue;
  865. }
  866. if (type_expr->kind == AstNode_Ellipsis) {
  867. type_expr = type_expr->Ellipsis.expr;
  868. error(param, "A polymorphic parameter cannot be variadic");
  869. }
  870. if (type_expr->kind == AstNode_TypeType) {
  871. is_type_param = true;
  872. type = make_type_generic(c->allocator, 0, str_lit(""));
  873. } else {
  874. type = check_type(c, type_expr);
  875. if (is_type_polymorphic(type)) {
  876. is_type_polymorphic_type = true;
  877. }
  878. }
  879. if (type == nullptr) {
  880. error(params[i], "Invalid parameter type");
  881. type = t_invalid;
  882. }
  883. if (is_type_untyped(type)) {
  884. if (is_type_untyped_undef(type)) {
  885. error(params[i], "Cannot determine parameter type from ---");
  886. } else {
  887. error(params[i], "Cannot determine parameter type from a nil");
  888. }
  889. type = t_invalid;
  890. }
  891. if (is_type_polymorphic_type) {
  892. gbString str = type_to_string(type);
  893. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  894. gb_string_free(str);
  895. type = t_invalid;
  896. }
  897. if (!is_type_param && !is_type_constant_type(type)) {
  898. gbString str = type_to_string(type);
  899. error(params[i], "A parameter must be a valid constant type, got %s", str);
  900. gb_string_free(str);
  901. }
  902. Scope *scope = c->context.scope;
  903. for_array(j, p->names) {
  904. AstNode *name = p->names[j];
  905. if (!ast_node_expect(name, AstNode_Ident)) {
  906. continue;
  907. }
  908. Entity *e = nullptr;
  909. Token token = name->Ident.token;
  910. if (poly_operands != nullptr) {
  911. Operand operand = (*poly_operands)[entities.count];
  912. if (is_type_param) {
  913. GB_ASSERT(operand.mode == Addressing_Type);
  914. e = make_entity_type_name(c->allocator, scope, token, operand.type);
  915. e->TypeName.is_type_alias = true;
  916. } else {
  917. GB_ASSERT(operand.mode == Addressing_Constant);
  918. e = make_entity_constant(c->allocator, scope, token, operand.type, operand.value);
  919. }
  920. } else {
  921. if (is_type_param) {
  922. e = make_entity_type_name(c->allocator, scope, token, type);
  923. e->TypeName.is_type_alias = true;
  924. } else {
  925. e = make_entity_constant(c->allocator, scope, token, type, empty_exact_value);
  926. }
  927. }
  928. add_entity(c, scope, name, e);
  929. array_add(&entities, e);
  930. }
  931. }
  932. if (entities.count > 0) {
  933. Type *tuple = make_type_tuple(c->allocator);
  934. tuple->Tuple.variables = entities.data;
  935. tuple->Tuple.variable_count = entities.count;
  936. polymorphic_params = tuple;
  937. }
  938. }
  939. }
  940. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  941. Array<Entity *> fields = {};
  942. if (!is_polymorphic) {
  943. fields = check_fields(c, node, st->fields, min_field_count, str_lit("struct"));
  944. }
  945. struct_type->Record.scope = c->context.scope;
  946. struct_type->Record.is_packed = st->is_packed;
  947. struct_type->Record.is_ordered = st->is_ordered;
  948. struct_type->Record.fields = fields.data;
  949. struct_type->Record.fields_in_src_order = fields.data;
  950. struct_type->Record.field_count = fields.count;
  951. struct_type->Record.polymorphic_params = polymorphic_params;
  952. struct_type->Record.is_polymorphic = is_polymorphic;
  953. type_set_offsets(c->allocator, struct_type);
  954. if (!struct_type->failure && !st->is_packed && !st->is_ordered) {
  955. struct_type->failure = false;
  956. struct_type->Record.are_offsets_set = false;
  957. struct_type->Record.offsets = nullptr;
  958. // NOTE(bill): Reorder fields for reduced size/performance
  959. Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, fields.count);
  960. for (isize i = 0; i < fields.count; i++) {
  961. reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
  962. }
  963. // NOTE(bill): Hacky thing
  964. // TODO(bill): Probably make an inline sorting procedure rather than use global variables
  965. __checker_allocator = c->allocator;
  966. // NOTE(bill): compound literal order must match source not layout
  967. gb_sort_array(reordered_fields, fields.count, cmp_reorder_struct_fields);
  968. for (isize i = 0; i < fields.count; i++) {
  969. reordered_fields[i]->Variable.field_index = i;
  970. }
  971. struct_type->Record.fields = reordered_fields;
  972. }
  973. type_set_offsets(c->allocator, struct_type);
  974. if (st->align != nullptr) {
  975. if (st->is_packed) {
  976. syntax_error(st->align, "`#align` cannot be applied with `#packed`");
  977. return;
  978. }
  979. Operand o = {};
  980. check_expr(c, &o, st->align);
  981. if (o.mode != Addressing_Constant) {
  982. if (o.mode != Addressing_Invalid) {
  983. error(st->align, "#align must be a constant");
  984. }
  985. return;
  986. }
  987. Type *type = base_type(o.type);
  988. if (is_type_untyped(type) || is_type_integer(type)) {
  989. if (o.value.kind == ExactValue_Integer) {
  990. i64 align = i128_to_i64(o.value.value_integer);
  991. if (align < 1 || !gb_is_power_of_two(align)) {
  992. error(st->align, "#align must be a power of 2, got %lld", align);
  993. return;
  994. }
  995. // NOTE(bill): Success!!!
  996. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  997. if (custom_align < align) {
  998. warning(st->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  999. }
  1000. struct_type->Record.custom_align = custom_align;
  1001. return;
  1002. }
  1003. }
  1004. error(st->align, "#align must be an integer");
  1005. return;
  1006. }
  1007. }
  1008. void check_union_type(Checker *c, Type *named_type, Type *union_type, AstNode *node) {
  1009. GB_ASSERT(is_type_union(union_type));
  1010. ast_node(ut, UnionType, node);
  1011. isize variant_count = ut->variants.count+1;
  1012. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1013. defer (gb_temp_arena_memory_end(tmp));
  1014. Entity *using_index_expr = nullptr;
  1015. Array<Type *> variants = {};
  1016. array_init(&variants, c->allocator, variant_count);
  1017. array_add(&variants, t_invalid);
  1018. union_type->Union.scope = c->context.scope;
  1019. {
  1020. Entity *__tag = make_entity_field(c->allocator, nullptr, make_token_ident(str_lit("__tag")), t_int, false, -1);
  1021. union_type->Union.union__tag = __tag;
  1022. }
  1023. for_array(i, ut->variants) {
  1024. AstNode *node = ut->variants[i];
  1025. Type *t = check_type(c, node);
  1026. if (t != nullptr && t != t_invalid) {
  1027. bool ok = true;
  1028. t = default_type(t);
  1029. if (is_type_untyped(t)) {
  1030. ok = false;
  1031. gbString str = type_to_string(t);
  1032. error(node, "Invalid type in union `%s`", str);
  1033. gb_string_free(str);
  1034. } else {
  1035. for_array(j, variants) {
  1036. if (are_types_identical(t, variants[j])) {
  1037. ok = false;
  1038. gbString str = type_to_string(t);
  1039. error(node, "Duplicate variant type `%s`", str);
  1040. gb_string_free(str);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. if (ok) array_add(&variants, t);
  1046. }
  1047. }
  1048. union_type->Union.variants = variants.data;
  1049. union_type->Union.variant_count = variants.count;
  1050. }
  1051. void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) {
  1052. GB_ASSERT(node->kind == AstNode_RawUnionType);
  1053. GB_ASSERT(is_type_raw_union(union_type));
  1054. ast_node(ut, RawUnionType, node);
  1055. isize min_field_count = 0;
  1056. for_array(i, ut->fields) {
  1057. AstNode *field = ut->fields[i];
  1058. switch (field->kind) {
  1059. case_ast_node(f, ValueDecl, field);
  1060. min_field_count += f->names.count;
  1061. case_end;
  1062. }
  1063. }
  1064. union_type->Record.names = make_names_field_for_record(c, c->context.scope);
  1065. auto fields = check_fields(c, node, ut->fields, min_field_count, str_lit("raw_union"));
  1066. union_type->Record.scope = c->context.scope;
  1067. union_type->Record.fields = fields.data;
  1068. union_type->Record.field_count = fields.count;
  1069. }
  1070. void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *node) {
  1071. ast_node(et, EnumType, node);
  1072. GB_ASSERT(is_type_enum(enum_type));
  1073. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1074. defer (gb_temp_arena_memory_end(tmp));
  1075. Type *base_type = t_int;
  1076. if (et->base_type != nullptr) {
  1077. base_type = check_type(c, et->base_type);
  1078. }
  1079. if (base_type == nullptr || !(is_type_integer(base_type) || is_type_float(base_type))) {
  1080. error(node, "Base type for enumeration must be numeric");
  1081. return;
  1082. }
  1083. if (is_type_enum(base_type)) {
  1084. error(node, "Base type for enumeration cannot be another enumeration");
  1085. return;
  1086. }
  1087. // NOTE(bill): Must be up here for the `check_init_constant` system
  1088. enum_type->Enum.base_type = base_type;
  1089. Map<Entity *> entity_map = {}; // Key: String
  1090. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*(et->fields.count));
  1091. Array<Entity *> fields = {};
  1092. array_init(&fields, c->allocator, et->fields.count);
  1093. Type *constant_type = enum_type;
  1094. if (named_type != nullptr) {
  1095. constant_type = named_type;
  1096. }
  1097. ExactValue iota = exact_value_i64(-1);
  1098. ExactValue min_value = exact_value_i64(0);
  1099. ExactValue max_value = exact_value_i64(0);
  1100. for_array(i, et->fields) {
  1101. AstNode *field = et->fields[i];
  1102. AstNode *ident = nullptr;
  1103. AstNode *init = nullptr;
  1104. if (field->kind == AstNode_FieldValue) {
  1105. ast_node(fv, FieldValue, field);
  1106. if (fv->field == nullptr || fv->field->kind != AstNode_Ident) {
  1107. error(field, "An enum field's name must be an identifier");
  1108. continue;
  1109. }
  1110. ident = fv->field;
  1111. init = fv->value;
  1112. } else if (field->kind == AstNode_Ident) {
  1113. ident = field;
  1114. } else {
  1115. error(field, "An enum field's name must be an identifier");
  1116. continue;
  1117. }
  1118. String name = ident->Ident.token.string;
  1119. if (init != nullptr) {
  1120. Operand o = {};
  1121. check_expr(c, &o, init);
  1122. if (o.mode != Addressing_Constant) {
  1123. error(init, "Enumeration value must be a constant");
  1124. o.mode = Addressing_Invalid;
  1125. }
  1126. if (o.mode != Addressing_Invalid) {
  1127. check_assignment(c, &o, constant_type, str_lit("enumeration"));
  1128. }
  1129. if (o.mode != Addressing_Invalid) {
  1130. iota = o.value;
  1131. } else {
  1132. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  1133. }
  1134. } else {
  1135. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  1136. }
  1137. // NOTE(bill): Skip blank identifiers
  1138. if (is_blank_ident(name)) {
  1139. continue;
  1140. } else if (name == "count") {
  1141. error(field, "`count` is a reserved identifier for enumerations");
  1142. continue;
  1143. } else if (name == "min_value") {
  1144. error(field, "`min_value` is a reserved identifier for enumerations");
  1145. continue;
  1146. } else if (name == "max_value") {
  1147. error(field, "`max_value` is a reserved identifier for enumerations");
  1148. continue;
  1149. } else if (name == "names") {
  1150. error(field, "`names` is a reserved identifier for enumerations");
  1151. continue;
  1152. }/* else if (name == "base_type") {
  1153. error(field, "`base_type` is a reserved identifier for enumerations");
  1154. continue;
  1155. } */
  1156. if (compare_exact_values(Token_Gt, min_value, iota)) {
  1157. min_value = iota;
  1158. }
  1159. if (compare_exact_values(Token_Lt, max_value, iota)) {
  1160. max_value = iota;
  1161. }
  1162. Entity *e = make_entity_constant(c->allocator, c->context.scope, ident->Ident.token, constant_type, iota);
  1163. e->identifier = ident;
  1164. e->flags |= EntityFlag_Visited;
  1165. HashKey key = hash_string(name);
  1166. if (map_get(&entity_map, key) != nullptr) {
  1167. error(ident, "`%.*s` is already declared in this enumeration", LIT(name));
  1168. } else {
  1169. map_set(&entity_map, key, e);
  1170. add_entity(c, c->context.scope, nullptr, e);
  1171. array_add(&fields, e);
  1172. add_entity_use(c, field, e);
  1173. }
  1174. }
  1175. GB_ASSERT(fields.count <= et->fields.count);
  1176. enum_type->Enum.fields = fields.data;
  1177. enum_type->Enum.field_count = fields.count;
  1178. enum_type->Enum.count = make_entity_constant(c->allocator, c->context.scope,
  1179. make_token_ident(str_lit("count")), t_int, exact_value_i64(fields.count));
  1180. enum_type->Enum.min_value = make_entity_constant(c->allocator, c->context.scope,
  1181. make_token_ident(str_lit("min_value")), constant_type, min_value);
  1182. enum_type->Enum.max_value = make_entity_constant(c->allocator, c->context.scope,
  1183. make_token_ident(str_lit("max_value")), constant_type, max_value);
  1184. enum_type->Enum.names = make_names_field_for_record(c, c->context.scope);
  1185. }
  1186. void check_bit_field_type(Checker *c, Type *bit_field_type, Type *named_type, AstNode *node) {
  1187. ast_node(bft, BitFieldType, node);
  1188. GB_ASSERT(is_type_bit_field(bit_field_type));
  1189. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1190. defer (gb_temp_arena_memory_end(tmp));
  1191. Map<Entity *> entity_map = {}; // Key: String
  1192. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*(bft->fields.count));
  1193. isize field_count = 0;
  1194. Entity **fields = gb_alloc_array(c->allocator, Entity *, bft->fields.count);
  1195. u32 * sizes = gb_alloc_array(c->allocator, u32, bft->fields.count);
  1196. u32 * offsets = gb_alloc_array(c->allocator, u32, bft->fields.count);
  1197. u32 curr_offset = 0;
  1198. for_array(i, bft->fields) {
  1199. AstNode *field = bft->fields[i];
  1200. GB_ASSERT(field->kind == AstNode_FieldValue);
  1201. AstNode *ident = field->FieldValue.field;
  1202. AstNode *value = field->FieldValue.value;
  1203. if (ident->kind != AstNode_Ident) {
  1204. error(field, "A bit field value's name must be an identifier");
  1205. continue;
  1206. }
  1207. String name = ident->Ident.token.string;
  1208. Operand o = {};
  1209. check_expr(c, &o, value);
  1210. if (o.mode != Addressing_Constant) {
  1211. error(value, "Bit field bit size must be a constant");
  1212. continue;
  1213. }
  1214. ExactValue v = exact_value_to_integer(o.value);
  1215. if (v.kind != ExactValue_Integer) {
  1216. error(value, "Bit field bit size must be a constant integer");
  1217. continue;
  1218. }
  1219. i64 bits = i128_to_i64(v.value_integer);
  1220. if (bits < 0 || bits > 128) {
  1221. error(value, "Bit field's bit size must be within the range 1..<128, got %lld", cast(long long)bits);
  1222. continue;
  1223. }
  1224. Type *value_type = make_type_bit_field_value(c->allocator, bits);
  1225. Entity *e = make_entity_variable(c->allocator, bit_field_type->BitField.scope, ident->Ident.token, value_type, false);
  1226. e->identifier = ident;
  1227. e->flags |= EntityFlag_BitFieldValue;
  1228. HashKey key = hash_string(name);
  1229. if (!is_blank_ident(name) &&
  1230. map_get(&entity_map, key) != nullptr) {
  1231. error(ident, "`%.*s` is already declared in this bit field", LIT(name));
  1232. } else {
  1233. map_set(&entity_map, key, e);
  1234. add_entity(c, c->context.scope, nullptr, e);
  1235. add_entity_use(c, field, e);
  1236. fields [field_count] = e;
  1237. offsets[field_count] = curr_offset;
  1238. sizes [field_count] = bits;
  1239. field_count++;
  1240. curr_offset += bits;
  1241. }
  1242. }
  1243. GB_ASSERT(field_count <= bft->fields.count);
  1244. bit_field_type->BitField.fields = fields;
  1245. bit_field_type->BitField.field_count = field_count;
  1246. bit_field_type->BitField.sizes = sizes;
  1247. bit_field_type->BitField.offsets = offsets;
  1248. if (bft->align != nullptr) {
  1249. Operand o = {};
  1250. check_expr(c, &o, bft->align);
  1251. if (o.mode != Addressing_Constant) {
  1252. if (o.mode != Addressing_Invalid) {
  1253. error(bft->align, "#align must be a constant");
  1254. }
  1255. return;
  1256. }
  1257. Type *type = base_type(o.type);
  1258. if (is_type_untyped(type) || is_type_integer(type)) {
  1259. if (o.value.kind == ExactValue_Integer) {
  1260. i64 align = i128_to_i64(o.value.value_integer);
  1261. if (align < 1 || !gb_is_power_of_two(align)) {
  1262. error(bft->align, "#align must be a power of 2, got %lld", align);
  1263. return;
  1264. }
  1265. // NOTE(bill): Success!!!
  1266. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  1267. if (custom_align < align) {
  1268. warning(bft->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  1269. }
  1270. bit_field_type->BitField.custom_align = custom_align;
  1271. return;
  1272. }
  1273. }
  1274. error(bft->align, "#align must be an integer");
  1275. return;
  1276. }
  1277. }
  1278. bool is_polymorphic_type_assignable(Checker *c, Type *poly, Type *source, bool compound, bool modify_type) {
  1279. Operand o = {Addressing_Value};
  1280. o.type = source;
  1281. switch (poly->kind) {
  1282. case Type_Basic:
  1283. if (compound) return are_types_identical(poly, source);
  1284. return check_is_assignable_to(c, &o, poly);
  1285. case Type_Named:
  1286. if (compound) return are_types_identical(poly, source);
  1287. return check_is_assignable_to(c, &o, poly);
  1288. case Type_Generic: {
  1289. if (modify_type) {
  1290. Type *ds = default_type(source);
  1291. gb_memmove(poly, ds, gb_size_of(Type));
  1292. }
  1293. return true;
  1294. }
  1295. case Type_Pointer:
  1296. if (source->kind == Type_Pointer) {
  1297. return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->Atomic.elem, true, modify_type);
  1298. }
  1299. return false;
  1300. case Type_Atomic:
  1301. if (source->kind == Type_Atomic) {
  1302. return is_polymorphic_type_assignable(c, poly->Atomic.elem, source->Atomic.elem, true, modify_type);
  1303. }
  1304. return false;
  1305. case Type_Array:
  1306. if (source->kind == Type_Array &&
  1307. poly->Array.count == source->Array.count) {
  1308. return is_polymorphic_type_assignable(c, poly->Array.elem, source->Array.elem, true, modify_type);
  1309. }
  1310. return false;
  1311. case Type_DynamicArray:
  1312. if (source->kind == Type_DynamicArray) {
  1313. return is_polymorphic_type_assignable(c, poly->DynamicArray.elem, source->DynamicArray.elem, true, modify_type);
  1314. }
  1315. return false;
  1316. case Type_Vector:
  1317. if (source->kind == Type_Vector &&
  1318. poly->Vector.count == source->Vector.count) {
  1319. return is_polymorphic_type_assignable(c, poly->Vector.elem, source->Vector.elem, true, modify_type);
  1320. }
  1321. return false;
  1322. case Type_Slice:
  1323. if (source->kind == Type_Slice) {
  1324. return is_polymorphic_type_assignable(c, poly->Slice.elem, source->Slice.elem, true, modify_type);
  1325. }
  1326. return false;
  1327. case Type_Enum:
  1328. return false;
  1329. case Type_Union:
  1330. if (source->kind == Type_Union) {
  1331. TypeUnion *x = &poly->Union;
  1332. TypeUnion *y = &source->Union;
  1333. if (x->variant_count != y->variant_count) {
  1334. return false;
  1335. }
  1336. for (isize i = 1; i < x->variant_count; i++) {
  1337. Type *a = x->variants[i];
  1338. Type *b = y->variants[i];
  1339. bool ok = is_polymorphic_type_assignable(c, a, b, false, modify_type);
  1340. if (!ok) return false;
  1341. }
  1342. return true;
  1343. }
  1344. return false;
  1345. case Type_Record:
  1346. if (source->kind == Type_Record) {
  1347. // TODO(bill): Polymorphic type assignment
  1348. // return check_is_assignable_to(c, &o, poly);
  1349. }
  1350. return false;
  1351. case Type_Tuple:
  1352. GB_PANIC("This should never happen");
  1353. return false;
  1354. case Type_Proc:
  1355. if (source->kind == Type_Proc) {
  1356. // return check_is_assignable_to(c, &o, poly);
  1357. // TODO(bill): Polymorphic type assignment
  1358. #if 1
  1359. TypeProc *x = &poly->Proc;
  1360. TypeProc *y = &source->Proc;
  1361. if (x->calling_convention != y->calling_convention) {
  1362. return false;
  1363. }
  1364. if (x->c_vararg != y->c_vararg) {
  1365. return false;
  1366. }
  1367. if (x->variadic != y->variadic) {
  1368. return false;
  1369. }
  1370. if (x->param_count != y->param_count) {
  1371. return false;
  1372. }
  1373. if (x->result_count != y->result_count) {
  1374. return false;
  1375. }
  1376. for (isize i = 0; i < x->param_count; i++) {
  1377. Entity *a = x->params->Tuple.variables[i];
  1378. Entity *b = y->params->Tuple.variables[i];
  1379. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  1380. if (!ok) return false;
  1381. }
  1382. for (isize i = 0; i < x->result_count; i++) {
  1383. Entity *a = x->results->Tuple.variables[i];
  1384. Entity *b = y->results->Tuple.variables[i];
  1385. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  1386. if (!ok) return false;
  1387. }
  1388. // TODO(bill): Polymorphic type assignment
  1389. return true;
  1390. #endif
  1391. }
  1392. return false;
  1393. case Type_Map:
  1394. if (source->kind == Type_Map) {
  1395. bool key = is_polymorphic_type_assignable(c, poly->Map.key, source->Map.key, true, modify_type);
  1396. bool value = is_polymorphic_type_assignable(c, poly->Map.value, source->Map.value, true, modify_type);
  1397. return key || value;
  1398. }
  1399. return false;
  1400. }
  1401. return false;
  1402. }
  1403. Type *determine_type_from_polymorphic(Checker *c, Type *poly_type, Operand operand) {
  1404. bool modify_type = !c->context.no_polymorphic_errors;
  1405. if (!is_operand_value(operand)) {
  1406. if (modify_type) {
  1407. error(operand.expr, "Cannot determine polymorphic type from parameter");
  1408. }
  1409. return t_invalid;
  1410. }
  1411. if (is_polymorphic_type_assignable(c, poly_type, operand.type, false, modify_type)) {
  1412. return poly_type;
  1413. }
  1414. if (modify_type) {
  1415. gbString pts = type_to_string(poly_type);
  1416. gbString ots = type_to_string(operand.type);
  1417. defer (gb_string_free(pts));
  1418. defer (gb_string_free(ots));
  1419. error(operand.expr,
  1420. "Cannot determine polymorphic type from parameter: `%s` to `%s`\n"
  1421. "\tNote: Record and procedure types are not yet supported",
  1422. ots, pts);
  1423. }
  1424. return t_invalid;
  1425. }
  1426. Type *check_get_params(Checker *c, Scope *scope, AstNode *_params, bool *is_variadic_, bool *success_, Array<Operand> *operands) {
  1427. if (_params == nullptr) {
  1428. return nullptr;
  1429. }
  1430. bool allow_polymorphic_types = c->context.allow_polymorphic_types;
  1431. bool success = true;
  1432. ast_node(field_list, FieldList, _params);
  1433. Array<AstNode *> params = field_list->list;
  1434. if (params.count == 0) {
  1435. if (success_) *success_ = success;
  1436. return nullptr;
  1437. }
  1438. isize variable_count = 0;
  1439. for_array(i, params) {
  1440. AstNode *field = params[i];
  1441. if (ast_node_expect(field, AstNode_Field)) {
  1442. ast_node(f, Field, field);
  1443. variable_count += gb_max(f->names.count, 1);
  1444. }
  1445. }
  1446. if (operands != nullptr) {
  1447. GB_ASSERT_MSG(operands->count >= variable_count, "%td vs %td", operands->count, variable_count);
  1448. }
  1449. bool is_variadic = false;
  1450. bool is_c_vararg = false;
  1451. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  1452. isize variable_index = 0;
  1453. for_array(i, params) {
  1454. AstNode *param = params[i];
  1455. if (param->kind != AstNode_Field) {
  1456. continue;
  1457. }
  1458. ast_node(p, Field, param);
  1459. AstNode *type_expr = p->type;
  1460. Type *type = nullptr;
  1461. AstNode *default_value = unparen_expr(p->default_value);
  1462. ExactValue value = {};
  1463. bool default_is_nil = false;
  1464. bool default_is_location = false;
  1465. bool is_type_param = false;
  1466. bool is_type_polymorphic_type = false;
  1467. bool detemine_type_from_operand = false;
  1468. if (type_expr == nullptr) {
  1469. if (default_value->kind == AstNode_BasicDirective &&
  1470. default_value->BasicDirective.name == "caller_location") {
  1471. init_preload(c);
  1472. default_is_location = true;
  1473. type = t_source_code_location;
  1474. } else {
  1475. Operand o = {};
  1476. check_expr_or_type(c, &o, default_value);
  1477. if (is_operand_nil(o)) {
  1478. default_is_nil = true;
  1479. } else if (o.mode != Addressing_Constant) {
  1480. error(default_value, "Default parameter must be a constant");
  1481. } else {
  1482. value = o.value;
  1483. }
  1484. type = default_type(o.type);
  1485. }
  1486. } else {
  1487. if (type_expr->kind == AstNode_Ellipsis) {
  1488. type_expr = type_expr->Ellipsis.expr;
  1489. if (i+1 == params.count) {
  1490. is_variadic = true;
  1491. } else {
  1492. error(param, "Invalid AST: Invalid variadic parameter");
  1493. success = false;
  1494. }
  1495. }
  1496. if (type_expr->kind == AstNode_TypeType) {
  1497. is_type_param = true;
  1498. if (operands != nullptr) {
  1499. detemine_type_from_operand = true;
  1500. type = t_invalid;
  1501. } else {
  1502. type = make_type_generic(c->allocator, 0, str_lit(""));
  1503. }
  1504. } else {
  1505. bool prev = c->context.allow_polymorphic_types;
  1506. if (operands != nullptr) {
  1507. c->context.allow_polymorphic_types = true;
  1508. }
  1509. type = check_type(c, type_expr);
  1510. c->context.allow_polymorphic_types = prev;
  1511. if (is_type_polymorphic(type)) {
  1512. is_type_polymorphic_type = true;
  1513. }
  1514. }
  1515. if (default_value != nullptr) {
  1516. if (type_expr->kind == AstNode_TypeType) {
  1517. error(default_value, "A type parameter may not have a default value");
  1518. } else {
  1519. Operand o = {};
  1520. if (default_value->kind == AstNode_BasicDirective &&
  1521. default_value->BasicDirective.name == "caller_location") {
  1522. init_preload(c);
  1523. default_is_location = true;
  1524. o.type = t_source_code_location;
  1525. o.mode = Addressing_Value;
  1526. } else {
  1527. check_expr_with_type_hint(c, &o, default_value, type);
  1528. if (is_operand_nil(o)) {
  1529. default_is_nil = true;
  1530. } else if (o.mode != Addressing_Constant) {
  1531. error(default_value, "Default parameter must be a constant");
  1532. } else {
  1533. value = o.value;
  1534. }
  1535. }
  1536. check_is_assignable_to(c, &o, type);
  1537. }
  1538. }
  1539. }
  1540. if (type == nullptr) {
  1541. error(params[i], "Invalid parameter type");
  1542. type = t_invalid;
  1543. }
  1544. if (is_type_untyped(type)) {
  1545. if (is_type_untyped_undef(type)) {
  1546. error(params[i], "Cannot determine parameter type from ---");
  1547. } else {
  1548. error(params[i], "Cannot determine parameter type from a nil");
  1549. }
  1550. type = t_invalid;
  1551. }
  1552. if (p->flags&FieldFlag_c_vararg) {
  1553. if (p->type == nullptr ||
  1554. p->type->kind != AstNode_Ellipsis) {
  1555. error(params[i], "`#c_vararg` can only be applied to variadic type fields");
  1556. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1557. } else {
  1558. is_c_vararg = true;
  1559. }
  1560. }
  1561. for_array(j, p->names) {
  1562. AstNode *name = p->names[j];
  1563. if (!ast_node_expect(name, AstNode_Ident)) {
  1564. continue;
  1565. }
  1566. Entity *param = nullptr;
  1567. if (is_type_param) {
  1568. if (operands != nullptr) {
  1569. Operand o = (*operands)[variable_index];
  1570. if (o.mode == Addressing_Type) {
  1571. type = o.type;
  1572. } else {
  1573. if (!c->context.no_polymorphic_errors) {
  1574. error(o.expr, "Expected a type to assign to the type parameter");
  1575. }
  1576. success = false;
  1577. type = t_invalid;
  1578. }
  1579. }
  1580. param = make_entity_type_name(c->allocator, scope, name->Ident.token, type);
  1581. param->TypeName.is_type_alias = true;
  1582. } else {
  1583. if (operands != nullptr && is_type_polymorphic_type) {
  1584. Operand op = (*operands)[variable_index];
  1585. type = determine_type_from_polymorphic(c, type, op);
  1586. if (type == t_invalid) {
  1587. success = false;
  1588. }
  1589. }
  1590. if (p->flags&FieldFlag_no_alias) {
  1591. if (!is_type_pointer(type)) {
  1592. error(params[i], "`#no_alias` can only be applied to fields of pointer type");
  1593. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1594. }
  1595. }
  1596. param = make_entity_param(c->allocator, scope, name->Ident.token, type,
  1597. (p->flags&FieldFlag_using) != 0, false);
  1598. param->Variable.default_value = value;
  1599. param->Variable.default_is_nil = default_is_nil;
  1600. param->Variable.default_is_location = default_is_location;
  1601. }
  1602. if (p->flags&FieldFlag_no_alias) {
  1603. param->flags |= EntityFlag_NoAlias;
  1604. }
  1605. add_entity(c, scope, name, param);
  1606. variables[variable_index++] = param;
  1607. }
  1608. }
  1609. variable_count = variable_index;
  1610. if (is_variadic) {
  1611. GB_ASSERT(params.count > 0);
  1612. // NOTE(bill): Change last variadic parameter to be a slice
  1613. // Custom Calling convention for variadic parameters
  1614. Entity *end = variables[variable_count-1];
  1615. end->type = make_type_slice(c->allocator, end->type);
  1616. end->flags |= EntityFlag_Ellipsis;
  1617. if (is_c_vararg) {
  1618. end->flags |= EntityFlag_CVarArg;
  1619. }
  1620. }
  1621. Type *tuple = make_type_tuple(c->allocator);
  1622. tuple->Tuple.variables = variables;
  1623. tuple->Tuple.variable_count = variable_count;
  1624. if (success_) *success_ = success;
  1625. if (is_variadic_) *is_variadic_ = is_variadic;
  1626. return tuple;
  1627. }
  1628. Type *check_get_results(Checker *c, Scope *scope, AstNode *_results) {
  1629. if (_results == nullptr) {
  1630. return nullptr;
  1631. }
  1632. ast_node(field_list, FieldList, _results);
  1633. Array<AstNode *> results = field_list->list;
  1634. if (results.count == 0) {
  1635. return nullptr;
  1636. }
  1637. Type *tuple = make_type_tuple(c->allocator);
  1638. isize variable_count = 0;
  1639. for_array(i, results) {
  1640. AstNode *field = results[i];
  1641. if (ast_node_expect(field, AstNode_Field)) {
  1642. ast_node(f, Field, field);
  1643. variable_count += gb_max(f->names.count, 1);
  1644. }
  1645. }
  1646. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  1647. isize variable_index = 0;
  1648. for_array(i, results) {
  1649. ast_node(field, Field, results[i]);
  1650. AstNode *default_value = unparen_expr(field->default_value);
  1651. ExactValue value = {};
  1652. bool default_is_nil = false;
  1653. Type *type = nullptr;
  1654. if (field->type == nullptr) {
  1655. Operand o = {};
  1656. check_expr(c, &o, default_value);
  1657. if (is_operand_nil(o)) {
  1658. default_is_nil = true;
  1659. } else if (o.mode != Addressing_Constant) {
  1660. error(default_value, "Default parameter must be a constant");
  1661. } else {
  1662. value = o.value;
  1663. }
  1664. type = default_type(o.type);
  1665. } else {
  1666. type = check_type(c, field->type);
  1667. if (default_value != nullptr) {
  1668. Operand o = {};
  1669. check_expr_with_type_hint(c, &o, default_value, type);
  1670. if (is_operand_nil(o)) {
  1671. default_is_nil = true;
  1672. } else if (o.mode != Addressing_Constant) {
  1673. error(default_value, "Default parameter must be a constant");
  1674. } else {
  1675. value = o.value;
  1676. }
  1677. check_is_assignable_to(c, &o, type);
  1678. }
  1679. }
  1680. if (type == nullptr) {
  1681. error(results[i], "Invalid parameter type");
  1682. type = t_invalid;
  1683. }
  1684. if (is_type_untyped(type)) {
  1685. error(results[i], "Cannot determine parameter type from a nil");
  1686. type = t_invalid;
  1687. }
  1688. if (field->names.count == 0) {
  1689. Token token = ast_node_token(field->type);
  1690. token.string = str_lit("");
  1691. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  1692. param->Variable.default_value = value;
  1693. param->Variable.default_is_nil = default_is_nil;
  1694. variables[variable_index++] = param;
  1695. } else {
  1696. for_array(j, field->names) {
  1697. Token token = ast_node_token(results[i]);
  1698. if (field->type != nullptr) {
  1699. token = ast_node_token(field->type);
  1700. }
  1701. token.string = str_lit("");
  1702. AstNode *name = field->names[j];
  1703. if (name->kind != AstNode_Ident) {
  1704. error(name, "Expected an identifer for as the field name");
  1705. } else {
  1706. token = name->Ident.token;
  1707. }
  1708. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  1709. param->Variable.default_value = value;
  1710. param->Variable.default_is_nil = default_is_nil;
  1711. variables[variable_index++] = param;
  1712. }
  1713. }
  1714. }
  1715. for (isize i = 0; i < variable_index; i++) {
  1716. String x = variables[i]->token.string;
  1717. if (x.len == 0 || is_blank_ident(x)) {
  1718. continue;
  1719. }
  1720. for (isize j = i+1; j < variable_index; j++) {
  1721. String y = variables[j]->token.string;
  1722. if (y.len == 0 || is_blank_ident(y)) {
  1723. continue;
  1724. }
  1725. if (x == y) {
  1726. error(variables[j]->token, "Duplicate return value name `%.*s`", LIT(y));
  1727. }
  1728. }
  1729. }
  1730. tuple->Tuple.variables = variables;
  1731. tuple->Tuple.variable_count = variable_index;
  1732. return tuple;
  1733. }
  1734. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
  1735. Type *new_type = original_type;
  1736. if (build_context.ODIN_ARCH == "x86") {
  1737. return new_type;
  1738. }
  1739. if (build_context.ODIN_OS == "windows") {
  1740. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  1741. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  1742. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  1743. Type *bt = core_type(original_type);
  1744. switch (bt->kind) {
  1745. // Okay to pass by value (usually)
  1746. // Especially the only Odin types
  1747. case Type_Basic: {
  1748. i64 sz = bt->Basic.size;
  1749. if (sz > 8 && build_context.word_size < 8) {
  1750. new_type = make_type_pointer(a, original_type);
  1751. }
  1752. } break;
  1753. case Type_Pointer: break;
  1754. case Type_Proc: break; // NOTE(bill): Just a pointer
  1755. // Odin only types
  1756. case Type_Slice:
  1757. case Type_DynamicArray:
  1758. case Type_Map:
  1759. break;
  1760. // Odin specific
  1761. case Type_Array:
  1762. case Type_Vector:
  1763. // Could be in C too
  1764. case Type_Record: {
  1765. i64 align = type_align_of(a, original_type);
  1766. i64 size = type_size_of(a, original_type);
  1767. switch (8*size) {
  1768. case 8: new_type = t_u8; break;
  1769. case 16: new_type = t_u16; break;
  1770. case 32: new_type = t_u32; break;
  1771. case 64: new_type = t_u64; break;
  1772. default:
  1773. new_type = make_type_pointer(a, original_type);
  1774. break;
  1775. }
  1776. } break;
  1777. }
  1778. } else if (build_context.ODIN_OS == "linux" ||
  1779. build_context.ODIN_OS == "osx") {
  1780. Type *bt = core_type(original_type);
  1781. switch (bt->kind) {
  1782. // Okay to pass by value (usually)
  1783. // Especially the only Odin types
  1784. case Type_Basic: {
  1785. i64 sz = bt->Basic.size;
  1786. if (sz > 8 && build_context.word_size < 8) {
  1787. new_type = make_type_pointer(a, original_type);
  1788. }
  1789. } break;
  1790. case Type_Pointer: break;
  1791. case Type_Proc: break; // NOTE(bill): Just a pointer
  1792. // Odin only types
  1793. case Type_Slice:
  1794. case Type_DynamicArray:
  1795. case Type_Map:
  1796. break;
  1797. // Odin specific
  1798. case Type_Array:
  1799. case Type_Vector:
  1800. // Could be in C too
  1801. case Type_Record: {
  1802. i64 align = type_align_of(a, original_type);
  1803. i64 size = type_size_of(a, original_type);
  1804. if (8*size > 16) {
  1805. new_type = make_type_pointer(a, original_type);
  1806. }
  1807. } break;
  1808. }
  1809. } else {
  1810. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1811. // their architectures
  1812. }
  1813. return new_type;
  1814. }
  1815. Type *reduce_tuple_to_single_type(Type *original_type) {
  1816. if (original_type != nullptr) {
  1817. Type *t = core_type(original_type);
  1818. if (t->kind == Type_Tuple && t->Tuple.variable_count == 1) {
  1819. return t->Tuple.variables[0]->type;
  1820. }
  1821. }
  1822. return original_type;
  1823. }
  1824. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type) {
  1825. Type *new_type = original_type;
  1826. if (new_type == nullptr) {
  1827. return nullptr;
  1828. }
  1829. GB_ASSERT(is_type_tuple(original_type));
  1830. if (build_context.ODIN_OS == "windows") {
  1831. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  1832. // NOTE(bill): This is just reversed engineered from LLVM IR output
  1833. switch (bt->kind) {
  1834. // Okay to pass by value
  1835. // Especially the only Odin types
  1836. case Type_Pointer: break;
  1837. case Type_Proc: break; // NOTE(bill): Just a pointer
  1838. case Type_Basic: break;
  1839. default: {
  1840. i64 align = type_align_of(a, original_type);
  1841. i64 size = type_size_of(a, original_type);
  1842. switch (8*size) {
  1843. #if 1
  1844. case 8: new_type = t_u8; break;
  1845. case 16: new_type = t_u16; break;
  1846. case 32: new_type = t_u32; break;
  1847. case 64: new_type = t_u64; break;
  1848. #endif
  1849. }
  1850. } break;
  1851. }
  1852. } else if (build_context.ODIN_OS == "linux") {
  1853. } else {
  1854. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1855. // their architectures
  1856. }
  1857. if (new_type != original_type) {
  1858. Type *tuple = make_type_tuple(a);
  1859. tuple->Tuple.variable_count = 1;
  1860. tuple->Tuple.variables = gb_alloc_array(a, Entity *, 1);
  1861. tuple->Tuple.variables[0] = make_entity_param(a, original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false);
  1862. new_type = tuple;
  1863. }
  1864. // return reduce_tuple_to_single_type(new_type);
  1865. return new_type;
  1866. }
  1867. bool abi_compat_return_by_value(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  1868. if (abi_return_type == nullptr) {
  1869. return false;
  1870. }
  1871. switch (cc) {
  1872. case ProcCC_Odin:
  1873. case ProcCC_Contextless:
  1874. return false;
  1875. }
  1876. if (build_context.ODIN_OS == "windows") {
  1877. i64 size = 8*type_size_of(a, abi_return_type);
  1878. switch (size) {
  1879. case 0:
  1880. case 8:
  1881. case 16:
  1882. case 32:
  1883. case 64:
  1884. return false;
  1885. default:
  1886. return true;
  1887. }
  1888. }
  1889. return false;
  1890. }
  1891. // NOTE(bill): `operands` is for generating non generic procedure type
  1892. bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array<Operand> *operands) {
  1893. ast_node(pt, ProcType, proc_type_node);
  1894. if (c->context.polymorphic_scope == nullptr && c->context.allow_polymorphic_types) {
  1895. c->context.polymorphic_scope = c->context.scope;
  1896. }
  1897. bool variadic = false;
  1898. bool success = true;
  1899. Type *params = check_get_params(c, c->context.scope, pt->params, &variadic, &success, operands);
  1900. Type *results = check_get_results(c, c->context.scope, pt->results);
  1901. isize param_count = 0;
  1902. isize result_count = 0;
  1903. if (params) param_count = params ->Tuple.variable_count;
  1904. if (results) result_count = results->Tuple.variable_count;
  1905. type->Proc.node = proc_type_node;
  1906. type->Proc.scope = c->context.scope;
  1907. type->Proc.params = params;
  1908. type->Proc.param_count = param_count;
  1909. type->Proc.results = results;
  1910. type->Proc.result_count = result_count;
  1911. type->Proc.variadic = variadic;
  1912. type->Proc.calling_convention = pt->calling_convention;
  1913. type->Proc.is_polymorphic = pt->generic;
  1914. if (param_count > 0) {
  1915. Entity *end = params->Tuple.variables[param_count-1];
  1916. if (end->flags&EntityFlag_CVarArg) {
  1917. if (pt->calling_convention == ProcCC_Odin) {
  1918. error(end->token, "Odin calling convention does not support #c_vararg");
  1919. } else if (pt->calling_convention == ProcCC_Contextless) {
  1920. error(end->token, "Odin's contextless calling convention does not support #c_vararg");
  1921. } else if (pt->calling_convention == ProcCC_Fast) {
  1922. error(end->token, "Fast calling convention does not support #c_vararg");
  1923. } else {
  1924. type->Proc.c_vararg = true;
  1925. }
  1926. }
  1927. }
  1928. bool is_polymorphic = false;
  1929. for (isize i = 0; i < param_count; i++) {
  1930. Entity *e = params->Tuple.variables[i];
  1931. if (e->kind != Entity_Variable) {
  1932. is_polymorphic = true;
  1933. break;
  1934. } else if (is_type_polymorphic(e->type)) {
  1935. is_polymorphic = true;
  1936. break;
  1937. }
  1938. }
  1939. type->Proc.is_polymorphic = is_polymorphic;
  1940. type->Proc.abi_compat_params = gb_alloc_array(c->allocator, Type *, param_count);
  1941. for (isize i = 0; i < param_count; i++) {
  1942. Entity *e = type->Proc.params->Tuple.variables[i];
  1943. if (e->kind == Entity_Variable) {
  1944. Type *original_type = e->type;
  1945. Type *new_type = type_to_abi_compat_param_type(c->allocator, original_type);
  1946. type->Proc.abi_compat_params[i] = new_type;
  1947. }
  1948. }
  1949. // NOTE(bill): The types are the same
  1950. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(c->allocator, type->Proc.results);
  1951. type->Proc.return_by_pointer = abi_compat_return_by_value(c->allocator, pt->calling_convention, type->Proc.abi_compat_result_type);
  1952. return success;
  1953. }
  1954. Entity *check_ident(Checker *c, Operand *o, AstNode *n, Type *named_type, Type *type_hint, bool allow_import_name) {
  1955. GB_ASSERT(n->kind == AstNode_Ident);
  1956. o->mode = Addressing_Invalid;
  1957. o->expr = n;
  1958. String name = n->Ident.token.string;
  1959. Entity *e = scope_lookup_entity(c->context.scope, name);
  1960. if (e == nullptr) {
  1961. if (is_blank_ident(name)) {
  1962. error(n, "`_` cannot be used as a value type");
  1963. } else {
  1964. error(n, "Undeclared name: %.*s", LIT(name));
  1965. }
  1966. o->type = t_invalid;
  1967. o->mode = Addressing_Invalid;
  1968. if (named_type != nullptr) {
  1969. set_base_type(named_type, t_invalid);
  1970. }
  1971. return nullptr;
  1972. }
  1973. if (e->parent_proc_decl != nullptr &&
  1974. e->parent_proc_decl != c->context.curr_proc_decl) {
  1975. if (e->kind == Entity_Variable) {
  1976. error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
  1977. return nullptr;
  1978. } else if (e->kind == Entity_Label) {
  1979. error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
  1980. return nullptr;
  1981. }
  1982. }
  1983. bool is_overloaded = false;
  1984. isize overload_count = 0;
  1985. bool is_alias = false;
  1986. while (e->kind == Entity_Alias) {
  1987. GB_ASSERT(e->Alias.base != nullptr);
  1988. e = e->Alias.base;
  1989. is_alias = true;
  1990. }
  1991. HashKey key = hash_string(e->token.string);
  1992. if (e->kind == Entity_Procedure) {
  1993. // NOTE(bill): Overloads are only allowed with the same scope
  1994. Scope *s = e->scope;
  1995. overload_count = multi_map_count(&s->elements, key);
  1996. if (overload_count > 1) {
  1997. is_overloaded = true;
  1998. }
  1999. }
  2000. if (is_overloaded) {
  2001. Scope *s = e->scope;
  2002. bool skip = false;
  2003. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  2004. multi_map_get_all(&s->elements, key, procs);
  2005. if (type_hint != nullptr) {
  2006. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  2007. defer (gb_temp_arena_memory_end(tmp));
  2008. // NOTE(bill): These should be done
  2009. for (isize i = 0; i < overload_count; i++) {
  2010. Type *t = base_type(procs[i]->type);
  2011. if (t == t_invalid) {
  2012. continue;
  2013. }
  2014. Operand x = {};
  2015. x.mode = Addressing_Value;
  2016. x.type = t;
  2017. if (check_is_assignable_to(c, &x, type_hint)) {
  2018. e = procs[i];
  2019. add_entity_use(c, n, e);
  2020. skip = true;
  2021. break;
  2022. }
  2023. }
  2024. }
  2025. if (!skip) {
  2026. o->mode = Addressing_Overload;
  2027. o->type = t_invalid;
  2028. o->overload_count = overload_count;
  2029. o->overload_entities = procs;
  2030. return nullptr;
  2031. }
  2032. gb_free(heap_allocator(), procs);
  2033. }
  2034. add_entity_use(c, n, e);
  2035. check_entity_decl(c, e, nullptr, named_type);
  2036. if (e->type == nullptr) {
  2037. compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
  2038. // return nullptr;
  2039. }
  2040. e->flags |= EntityFlag_Used;
  2041. Type *type = e->type;
  2042. switch (e->kind) {
  2043. case Entity_Constant:
  2044. if (type == t_invalid) {
  2045. o->type = t_invalid;
  2046. return e;
  2047. }
  2048. o->value = e->Constant.value;
  2049. if (o->value.kind == ExactValue_Invalid) {
  2050. return e;
  2051. }
  2052. o->mode = Addressing_Constant;
  2053. break;
  2054. case Entity_Variable:
  2055. e->flags |= EntityFlag_Used;
  2056. if (type == t_invalid) {
  2057. o->type = t_invalid;
  2058. return e;
  2059. }
  2060. o->mode = Addressing_Variable;
  2061. if (e->flags & EntityFlag_Value) {
  2062. o->mode = Addressing_Value;
  2063. }
  2064. if (e->Variable.is_immutable) {
  2065. o->mode = Addressing_Immutable;
  2066. }
  2067. break;
  2068. case Entity_Procedure:
  2069. o->mode = Addressing_Value;
  2070. break;
  2071. case Entity_Builtin:
  2072. o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
  2073. o->mode = Addressing_Builtin;
  2074. break;
  2075. case Entity_TypeName:
  2076. o->mode = Addressing_Type;
  2077. break;
  2078. case Entity_ImportName:
  2079. if (!allow_import_name) {
  2080. error(n, "Use of import `%.*s` not in selector", LIT(name));
  2081. }
  2082. return e;
  2083. case Entity_LibraryName:
  2084. error(n, "Use of library `%.*s` not in foreign block", LIT(name));
  2085. return e;
  2086. case Entity_Label:
  2087. o->mode = Addressing_NoValue;
  2088. break;
  2089. case Entity_Nil:
  2090. o->mode = Addressing_Value;
  2091. break;
  2092. default:
  2093. compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
  2094. break;
  2095. }
  2096. o->type = type;
  2097. return e;
  2098. }
  2099. i64 check_array_or_map_count(Checker *c, AstNode *e, bool is_map) {
  2100. if (e == nullptr) {
  2101. return 0;
  2102. }
  2103. Operand o = {};
  2104. if (e->kind == AstNode_UnaryExpr &&
  2105. e->UnaryExpr.op.kind == Token_Ellipsis) {
  2106. return -1;
  2107. }
  2108. check_expr(c, &o, e);
  2109. if (o.mode != Addressing_Constant) {
  2110. if (o.mode != Addressing_Invalid) {
  2111. if (is_map) {
  2112. error(e, "Fixed map count must be a constant");
  2113. } else {
  2114. error(e, "Array count must be a constant");
  2115. }
  2116. }
  2117. return 0;
  2118. }
  2119. Type *type = base_type(o.type);
  2120. if (is_type_untyped(type) || is_type_integer(type)) {
  2121. if (o.value.kind == ExactValue_Integer) {
  2122. i64 count = i128_to_i64(o.value.value_integer);
  2123. if (is_map) {
  2124. if (count > 0) {
  2125. return count;
  2126. }
  2127. error(e, "Invalid fixed map count");
  2128. } else {
  2129. if (count >= 0) {
  2130. return count;
  2131. }
  2132. error(e, "Invalid negative array count %lld", cast(long long)count);
  2133. }
  2134. return 0;
  2135. }
  2136. }
  2137. if (is_map) {
  2138. error(e, "Fixed map count must be an integer");
  2139. } else {
  2140. error(e, "Array count must be an integer");
  2141. }
  2142. return 0;
  2143. }
  2144. Type *make_optional_ok_type(gbAllocator a, Type *value) {
  2145. bool typed = true;
  2146. Type *t = make_type_tuple(a);
  2147. t->Tuple.variables = gb_alloc_array(a, Entity *, 2);
  2148. t->Tuple.variable_count = 2;
  2149. t->Tuple.variables[0] = make_entity_field(a, nullptr, blank_token, value, false, 0);
  2150. t->Tuple.variables[1] = make_entity_field(a, nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  2151. return t;
  2152. }
  2153. void check_map_type(Checker *c, Type *type, AstNode *node) {
  2154. GB_ASSERT(type->kind == Type_Map);
  2155. ast_node(mt, MapType, node);
  2156. i64 count = check_array_or_map_count(c, mt->count, true);
  2157. Type *key = check_type(c, mt->key);
  2158. Type *value = check_type(c, mt->value);
  2159. if (!is_type_valid_for_keys(key)) {
  2160. if (is_type_boolean(key)) {
  2161. error(node, "A boolean cannot be used as a key for a map");
  2162. } else {
  2163. gbString str = type_to_string(key);
  2164. error(node, "Invalid type of a key for a map, got `%s`", str);
  2165. gb_string_free(str);
  2166. }
  2167. }
  2168. if (count > 0) {
  2169. count = 0;
  2170. error(node, "Fixed map types are not yet implemented");
  2171. }
  2172. type->Map.count = count;
  2173. type->Map.key = key;
  2174. type->Map.value = value;
  2175. gbAllocator a = c->allocator;
  2176. {
  2177. // NOTE(bill): The preload types may have not been set yet
  2178. init_preload(c);
  2179. GB_ASSERT(t_map_key != nullptr);
  2180. Type *entry_type = make_type_struct(a);
  2181. /*
  2182. struct {
  2183. hash: Map_Key,
  2184. next: int,
  2185. key: Key_Type,
  2186. value: Value_Type,
  2187. }
  2188. */
  2189. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  2190. dummy_node->kind = AstNode_Invalid;
  2191. check_open_scope(c, dummy_node);
  2192. isize field_count = 3;
  2193. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  2194. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("key")), t_map_key, false, 0);
  2195. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("next")), t_int, false, 1);
  2196. fields[2] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("value")), value, false, 2);
  2197. check_close_scope(c);
  2198. entry_type->Record.fields = fields;
  2199. entry_type->Record.fields_in_src_order = fields;
  2200. entry_type->Record.field_count = field_count;
  2201. type_set_offsets(a, entry_type);
  2202. type->Map.entry_type = entry_type;
  2203. }
  2204. {
  2205. Type *generated_struct_type = make_type_struct(a);
  2206. /*
  2207. struct {
  2208. hashes: [dynamic]int,
  2209. entries; [dynamic]Entry_Type,
  2210. }
  2211. */
  2212. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  2213. dummy_node->kind = AstNode_Invalid;
  2214. check_open_scope(c, dummy_node);
  2215. Type *hashes_type = make_type_dynamic_array(a, t_int);
  2216. Type *entries_type = make_type_dynamic_array(a, type->Map.entry_type);
  2217. isize field_count = 2;
  2218. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  2219. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("hashes")), hashes_type, false, 0);
  2220. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("entries")), entries_type, false, 1);
  2221. check_close_scope(c);
  2222. generated_struct_type->Record.fields = fields;
  2223. generated_struct_type->Record.fields_in_src_order = fields;
  2224. generated_struct_type->Record.field_count = field_count;
  2225. type_set_offsets(a, generated_struct_type);
  2226. type->Map.generated_struct_type = generated_struct_type;
  2227. }
  2228. type->Map.lookup_result_type = make_optional_ok_type(a, value);
  2229. // error(node, "`map` types are not yet implemented");
  2230. }
  2231. bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type) {
  2232. GB_ASSERT_NOT_NULL(type);
  2233. if (e == nullptr) {
  2234. *type = t_invalid;
  2235. return true;
  2236. }
  2237. switch (e->kind) {
  2238. case_ast_node(i, Ident, e);
  2239. Operand o = {};
  2240. check_ident(c, &o, e, named_type, nullptr, false);
  2241. switch (o.mode) {
  2242. case Addressing_Invalid:
  2243. break;
  2244. case Addressing_Type: {
  2245. *type = o.type;
  2246. return true;
  2247. } break;
  2248. case Addressing_NoValue: {
  2249. gbString err_str = expr_to_string(e);
  2250. error(e, "`%s` used as a type", err_str);
  2251. gb_string_free(err_str);
  2252. } break;
  2253. default: {
  2254. gbString err_str = expr_to_string(e);
  2255. error(e, "`%s` used as a type when not a type", err_str);
  2256. gb_string_free(err_str);
  2257. } break;
  2258. }
  2259. case_end;
  2260. case_ast_node(ht, HelperType, e);
  2261. return check_type_internal(c, ht->type, type, named_type);
  2262. case_end;
  2263. case_ast_node(pt, PolyType, e);
  2264. AstNode *ident = pt->type;
  2265. if (ident->kind != AstNode_Ident) {
  2266. error(ident, "Expected an identifier after the $");
  2267. *type = t_invalid;
  2268. return false;
  2269. }
  2270. Token token = ident->Ident.token;
  2271. Type *t = make_type_generic(c->allocator, 0, token.string);
  2272. if (c->context.allow_polymorphic_types) {
  2273. Scope *ps = c->context.polymorphic_scope;
  2274. Scope *s = c->context.scope;
  2275. Scope *entity_scope = s;
  2276. if (ps != nullptr && ps != s) {
  2277. GB_ASSERT(is_scope_an_ancestor(ps, s) >= 0);
  2278. entity_scope = ps;
  2279. }
  2280. Entity *e = make_entity_type_name(c->allocator, entity_scope, token, t);
  2281. e->TypeName.is_type_alias = true;
  2282. add_entity(c, ps, ident, e);
  2283. add_entity(c, s, ident, e);
  2284. }
  2285. *type = t;
  2286. return true;
  2287. case_end;
  2288. case_ast_node(se, SelectorExpr, e);
  2289. Operand o = {};
  2290. check_selector(c, &o, e, nullptr);
  2291. switch (o.mode) {
  2292. case Addressing_Invalid:
  2293. break;
  2294. case Addressing_Type:
  2295. GB_ASSERT(o.type != nullptr);
  2296. *type = o.type;
  2297. return true;
  2298. case Addressing_NoValue: {
  2299. gbString err_str = expr_to_string(e);
  2300. error(e, "`%s` used as a type", err_str);
  2301. gb_string_free(err_str);
  2302. } break;
  2303. default: {
  2304. gbString err_str = expr_to_string(e);
  2305. error(e, "`%s` is not a type", err_str);
  2306. gb_string_free(err_str);
  2307. } break;
  2308. }
  2309. case_end;
  2310. case_ast_node(pe, ParenExpr, e);
  2311. *type = check_type(c, pe->expr, named_type);
  2312. return true;
  2313. case_end;
  2314. case_ast_node(ue, UnaryExpr, e);
  2315. if (ue->op.kind == Token_Pointer) {
  2316. *type = make_type_pointer(c->allocator, check_type(c, ue->expr));
  2317. return true;
  2318. } /* else if (ue->op.kind == Token_Maybe) {
  2319. *type = make_type_maybe(c->allocator, check_type(c, ue->expr));
  2320. return true;
  2321. } */
  2322. case_end;
  2323. case_ast_node(pt, PointerType, e);
  2324. Type *elem = check_type(c, pt->type);
  2325. i64 esz = type_size_of(c->allocator, elem);
  2326. *type = make_type_pointer(c->allocator, elem);
  2327. return true;
  2328. case_end;
  2329. case_ast_node(at, AtomicType, e);
  2330. Type *elem = check_type(c, at->type);
  2331. i64 esz = type_size_of(c->allocator, elem);
  2332. *type = make_type_atomic(c->allocator, elem);
  2333. return true;
  2334. case_end;
  2335. case_ast_node(at, ArrayType, e);
  2336. if (at->count != nullptr) {
  2337. Type *elem = check_type(c, at->elem, nullptr);
  2338. i64 count = check_array_or_map_count(c, at->count, false);
  2339. if (count < 0) {
  2340. error(at->count, "... can only be used in conjuction with compound literals");
  2341. count = 0;
  2342. }
  2343. #if 0
  2344. i64 esz = type_size_of(c->allocator, elem);
  2345. if (esz == 0) {
  2346. gbString str = type_to_string(elem);
  2347. error(at->elem, "Zero sized element type `%s` is not allowed", str);
  2348. gb_string_free(str);
  2349. }
  2350. #endif
  2351. *type = make_type_array(c->allocator, elem, count);
  2352. } else {
  2353. Type *elem = check_type(c, at->elem);
  2354. #if 0
  2355. i64 esz = type_size_of(c->allocator, elem);
  2356. if (esz == 0) {
  2357. gbString str = type_to_string(elem);
  2358. error(at->elem, "Zero sized element type `%s` is not allowed", str);
  2359. gb_string_free(str);
  2360. }
  2361. #endif
  2362. *type = make_type_slice(c->allocator, elem);
  2363. }
  2364. return true;
  2365. case_end;
  2366. case_ast_node(dat, DynamicArrayType, e);
  2367. Type *elem = check_type(c, dat->elem);
  2368. i64 esz = type_size_of(c->allocator, elem);
  2369. #if 0
  2370. if (esz == 0) {
  2371. gbString str = type_to_string(elem);
  2372. error(dat->elem, "Zero sized element type `%s` is not allowed", str);
  2373. gb_string_free(str);
  2374. }
  2375. #endif
  2376. *type = make_type_dynamic_array(c->allocator, elem);
  2377. return true;
  2378. case_end;
  2379. case_ast_node(vt, VectorType, e);
  2380. Type *elem = check_type(c, vt->elem);
  2381. Type *be = base_type(elem);
  2382. i64 count = check_array_or_map_count(c, vt->count, false);
  2383. if (is_type_vector(be) || (!is_type_boolean(be) && !is_type_numeric(be))) {
  2384. gbString err_str = type_to_string(elem);
  2385. error(vt->elem, "Vector element type must be numerical or a boolean, got `%s`", err_str);
  2386. gb_string_free(err_str);
  2387. }
  2388. *type = make_type_vector(c->allocator, elem, count);
  2389. return true;
  2390. case_end;
  2391. case_ast_node(st, StructType, e);
  2392. *type = make_type_struct(c->allocator);
  2393. set_base_type(named_type, *type);
  2394. check_open_scope(c, e);
  2395. check_struct_type(c, *type, e, nullptr);
  2396. check_close_scope(c);
  2397. (*type)->Record.node = e;
  2398. return true;
  2399. case_end;
  2400. case_ast_node(ut, UnionType, e);
  2401. *type = make_type_union(c->allocator);
  2402. set_base_type(named_type, *type);
  2403. check_open_scope(c, e);
  2404. check_union_type(c, named_type, *type, e);
  2405. check_close_scope(c);
  2406. (*type)->Union.node = e;
  2407. return true;
  2408. case_end;
  2409. case_ast_node(rut, RawUnionType, e);
  2410. *type = make_type_raw_union(c->allocator);
  2411. set_base_type(named_type, *type);
  2412. check_open_scope(c, e);
  2413. check_raw_union_type(c, *type, e);
  2414. check_close_scope(c);
  2415. (*type)->Record.node = e;
  2416. return true;
  2417. case_end;
  2418. case_ast_node(et, EnumType, e);
  2419. *type = make_type_enum(c->allocator);
  2420. set_base_type(named_type, *type);
  2421. check_open_scope(c, e);
  2422. check_enum_type(c, *type, named_type, e);
  2423. check_close_scope(c);
  2424. (*type)->Enum.node = e;
  2425. return true;
  2426. case_end;
  2427. case_ast_node(et, BitFieldType, e);
  2428. *type = make_type_bit_field(c->allocator);
  2429. set_base_type(named_type, *type);
  2430. check_open_scope(c, e);
  2431. check_bit_field_type(c, *type, named_type, e);
  2432. check_close_scope(c);
  2433. return true;
  2434. case_end;
  2435. case_ast_node(pt, ProcType, e);
  2436. *type = alloc_type(c->allocator, Type_Proc);
  2437. set_base_type(named_type, *type);
  2438. check_open_scope(c, e);
  2439. check_procedure_type(c, *type, e);
  2440. check_close_scope(c);
  2441. return true;
  2442. case_end;
  2443. case_ast_node(mt, MapType, e);
  2444. *type = alloc_type(c->allocator, Type_Map);
  2445. set_base_type(named_type, *type);
  2446. check_map_type(c, *type, e);
  2447. return true;
  2448. case_end;
  2449. case_ast_node(ce, CallExpr, e);
  2450. Operand o = {};
  2451. check_expr_or_type(c, &o, e);
  2452. if (o.mode == Addressing_Type) {
  2453. *type = o.type;
  2454. return true;
  2455. }
  2456. case_end;
  2457. }
  2458. *type = t_invalid;
  2459. return false;
  2460. }
  2461. Type *check_type(Checker *c, AstNode *e, Type *named_type) {
  2462. Type *type = nullptr;
  2463. bool ok = check_type_internal(c, e, &type, named_type);
  2464. if (!ok) {
  2465. gbString err_str = expr_to_string(e);
  2466. error(e, "`%s` is not a type", err_str);
  2467. gb_string_free(err_str);
  2468. type = t_invalid;
  2469. }
  2470. if (type == nullptr) {
  2471. type = t_invalid;
  2472. }
  2473. if (type->kind == Type_Named) {
  2474. if (type->Named.base == nullptr) {
  2475. gbString name = type_to_string(type);
  2476. error(e, "Invalid type definition of %s", name);
  2477. gb_string_free(name);
  2478. type->Named.base = t_invalid;
  2479. }
  2480. }
  2481. #if 0
  2482. if (!c->context.allow_polymorphic_types && is_type_polymorphic(type)) {
  2483. gbString str = type_to_string(type);
  2484. error(e, "Invalid use of a polymorphic type `%s`", str);
  2485. gb_string_free(str);
  2486. type = t_invalid;
  2487. }
  2488. #endif
  2489. if (is_type_typed(type)) {
  2490. add_type_and_value(&c->info, e, Addressing_Type, type, empty_exact_value);
  2491. } else {
  2492. gbString name = type_to_string(type);
  2493. error(e, "Invalid type definition of %s", name);
  2494. gb_string_free(name);
  2495. type = t_invalid;
  2496. }
  2497. set_base_type(named_type, type);
  2498. return type;
  2499. }
  2500. bool check_unary_op(Checker *c, Operand *o, Token op) {
  2501. if (o->type == nullptr) {
  2502. gbString str = expr_to_string(o->expr);
  2503. error(o->expr, "Expression has no value `%s`", str);
  2504. gb_string_free(str);
  2505. return false;
  2506. }
  2507. // TODO(bill): Handle errors correctly
  2508. Type *type = base_type(base_vector_type(o->type));
  2509. gbString str = nullptr;
  2510. switch (op.kind) {
  2511. case Token_Add:
  2512. case Token_Sub:
  2513. if (!is_type_numeric(type)) {
  2514. str = expr_to_string(o->expr);
  2515. error(op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  2516. gb_string_free(str);
  2517. }
  2518. break;
  2519. case Token_Xor:
  2520. if (!is_type_integer(type) && !is_type_boolean(type)) {
  2521. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  2522. }
  2523. break;
  2524. case Token_Not:
  2525. if (!is_type_boolean(type)) {
  2526. str = expr_to_string(o->expr);
  2527. error(op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  2528. gb_string_free(str);
  2529. }
  2530. break;
  2531. default:
  2532. error(op, "Unknown operator `%.*s`", LIT(op.string));
  2533. return false;
  2534. }
  2535. return true;
  2536. }
  2537. bool check_binary_op(Checker *c, Operand *o, Token op) {
  2538. // TODO(bill): Handle errors correctly
  2539. Type *type = base_type(base_vector_type(o->type));
  2540. switch (op.kind) {
  2541. case Token_Sub:
  2542. case Token_SubEq:
  2543. if (!is_type_numeric(type) && !is_type_pointer(type)) {
  2544. error(op, "Operator `%.*s` is only allowed with numeric or pointer expressions", LIT(op.string));
  2545. return false;
  2546. }
  2547. if (is_type_pointer(type)) {
  2548. o->type = t_int;
  2549. }
  2550. if (base_type(type) == t_rawptr) {
  2551. gbString str = type_to_string(type);
  2552. error(o->expr, "Invalid pointer type for pointer arithmetic: `%s`", str);
  2553. gb_string_free(str);
  2554. return false;
  2555. }
  2556. break;
  2557. case Token_Add:
  2558. case Token_Mul:
  2559. case Token_Quo:
  2560. case Token_AddEq:
  2561. case Token_MulEq:
  2562. case Token_QuoEq:
  2563. if (!is_type_numeric(type)) {
  2564. error(op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  2565. return false;
  2566. }
  2567. break;
  2568. case Token_And:
  2569. case Token_Or:
  2570. case Token_AndEq:
  2571. case Token_OrEq:
  2572. case Token_Xor:
  2573. case Token_XorEq:
  2574. if (!is_type_integer(type) && !is_type_boolean(type)) {
  2575. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  2576. return false;
  2577. }
  2578. break;
  2579. case Token_Mod:
  2580. case Token_ModMod:
  2581. case Token_AndNot:
  2582. case Token_ModEq:
  2583. case Token_ModModEq:
  2584. case Token_AndNotEq:
  2585. if (!is_type_integer(type)) {
  2586. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  2587. return false;
  2588. }
  2589. break;
  2590. case Token_CmpAnd:
  2591. case Token_CmpOr:
  2592. case Token_CmpAndEq:
  2593. case Token_CmpOrEq:
  2594. if (!is_type_boolean(type)) {
  2595. error(op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  2596. return false;
  2597. }
  2598. break;
  2599. default:
  2600. error(op, "Unknown operator `%.*s`", LIT(op.string));
  2601. return false;
  2602. }
  2603. return true;
  2604. }
  2605. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  2606. if (in_value.kind == ExactValue_Invalid) {
  2607. // NOTE(bill): There's already been an error
  2608. return true;
  2609. }
  2610. type = core_type(type);
  2611. if (is_type_boolean(type)) {
  2612. return in_value.kind == ExactValue_Bool;
  2613. } else if (is_type_string(type)) {
  2614. return in_value.kind == ExactValue_String;
  2615. } else if (is_type_integer(type) || is_type_rune(type)) {
  2616. ExactValue v = exact_value_to_integer(in_value);
  2617. if (v.kind != ExactValue_Integer) {
  2618. return false;
  2619. }
  2620. if (out_value) *out_value = v;
  2621. if (is_type_untyped(type)) {
  2622. return true;
  2623. }
  2624. i128 i = v.value_integer;
  2625. u128 u = *cast(u128 *)&i;
  2626. i64 s = 8*type_size_of(c->allocator, type);
  2627. u128 umax = U128_NEG_ONE;
  2628. if (s < 128) {
  2629. umax = u128_sub(u128_shl(U128_ONE, s), U128_ONE);
  2630. } else {
  2631. // IMPORTANT TODO(bill): I NEED A PROPER BIG NUMBER LIBRARY THAT CAN SUPPORT 128 bit floats
  2632. s = 128;
  2633. }
  2634. i128 imax = i128_shl(I128_ONE, s-1ll);
  2635. switch (type->Basic.kind) {
  2636. case Basic_rune:
  2637. case Basic_i8:
  2638. case Basic_i16:
  2639. case Basic_i32:
  2640. case Basic_i64:
  2641. case Basic_i128:
  2642. case Basic_int:
  2643. return i128_le(i128_neg(imax), i) && i128_le(i, i128_sub(imax, I128_ONE));
  2644. case Basic_u8:
  2645. case Basic_u16:
  2646. case Basic_u32:
  2647. case Basic_u64:
  2648. case Basic_u128:
  2649. case Basic_uint:
  2650. return !(u128_lt(u, U128_ZERO) || u128_gt(u, umax));
  2651. case Basic_UntypedInteger:
  2652. return true;
  2653. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  2654. }
  2655. } else if (is_type_float(type)) {
  2656. ExactValue v = exact_value_to_float(in_value);
  2657. if (v.kind != ExactValue_Float) {
  2658. return false;
  2659. }
  2660. if (out_value) *out_value = v;
  2661. switch (type->Basic.kind) {
  2662. // case Basic_f16:
  2663. case Basic_f32:
  2664. case Basic_f64:
  2665. return true;
  2666. case Basic_UntypedFloat:
  2667. return true;
  2668. }
  2669. } else if (is_type_complex(type)) {
  2670. ExactValue v = exact_value_to_complex(in_value);
  2671. if (v.kind != ExactValue_Complex) {
  2672. return false;
  2673. }
  2674. switch (type->Basic.kind) {
  2675. case Basic_complex64:
  2676. case Basic_complex128: {
  2677. ExactValue real = exact_value_real(v);
  2678. ExactValue imag = exact_value_imag(v);
  2679. if (real.kind != ExactValue_Invalid &&
  2680. imag.kind != ExactValue_Invalid) {
  2681. if (out_value) *out_value = exact_binary_operator_value(Token_Add, real, exact_value_make_imag(imag));
  2682. return true;
  2683. }
  2684. } break;
  2685. case Basic_UntypedComplex:
  2686. return true;
  2687. }
  2688. return false;
  2689. }else if (is_type_pointer(type)) {
  2690. if (in_value.kind == ExactValue_Pointer) {
  2691. return true;
  2692. }
  2693. if (in_value.kind == ExactValue_Integer) {
  2694. return false;
  2695. // return true;
  2696. }
  2697. if (out_value) *out_value = in_value;
  2698. }
  2699. return false;
  2700. }
  2701. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  2702. GB_ASSERT(is_type_constant_type(type));
  2703. GB_ASSERT(o->mode == Addressing_Constant);
  2704. if (!check_representable_as_constant(c, o->value, type, &o->value)) {
  2705. gbString a = expr_to_string(o->expr);
  2706. gbString b = type_to_string(type);
  2707. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  2708. if (!is_type_integer(o->type) && is_type_integer(type)) {
  2709. error(o->expr, "`%s` truncated to `%s`", a, b);
  2710. } else {
  2711. char buf[127] = {};
  2712. String str = {};
  2713. i128 i = o->value.value_integer;
  2714. if (is_type_unsigned(o->type)) {
  2715. str = u128_to_string(*cast(u128 *)&i, buf, gb_size_of(buf));
  2716. } else {
  2717. str = i128_to_string(i, buf, gb_size_of(buf));
  2718. }
  2719. error(o->expr, "`%s = %.*s` overflows `%s`", a, str, b);
  2720. }
  2721. } else {
  2722. error(o->expr, "Cannot convert `%s` to `%s`", a, b);
  2723. }
  2724. gb_string_free(b);
  2725. gb_string_free(a);
  2726. o->mode = Addressing_Invalid;
  2727. }
  2728. }
  2729. bool check_is_expr_vector_index(Checker *c, AstNode *expr) {
  2730. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  2731. expr = unparen_expr(expr);
  2732. if (expr->kind == AstNode_IndexExpr) {
  2733. ast_node(ie, IndexExpr, expr);
  2734. Type *t = type_deref(type_of_expr(&c->info, ie->expr));
  2735. if (t != nullptr) {
  2736. return is_type_vector(t);
  2737. }
  2738. }
  2739. return false;
  2740. }
  2741. bool check_is_vector_elem(Checker *c, AstNode *expr) {
  2742. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  2743. expr = unparen_expr(expr);
  2744. if (expr->kind == AstNode_SelectorExpr) {
  2745. ast_node(se, SelectorExpr, expr);
  2746. Type *t = type_deref(type_of_expr(&c->info, se->expr));
  2747. if (t != nullptr && is_type_vector(t)) {
  2748. return true;
  2749. }
  2750. }
  2751. return false;
  2752. }
  2753. bool check_is_not_addressable(Checker *c, Operand *o) {
  2754. if (o->mode != Addressing_Variable) {
  2755. return true;
  2756. }
  2757. if (is_type_bit_field_value(o->type)) {
  2758. return true;
  2759. }
  2760. if (check_is_expr_vector_index(c, o->expr)) {
  2761. return true;
  2762. }
  2763. if (check_is_vector_elem(c, o->expr)) {
  2764. return true;
  2765. }
  2766. return false;
  2767. }
  2768. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  2769. switch (op.kind) {
  2770. case Token_And: { // Pointer address
  2771. if (o->mode == Addressing_Type) {
  2772. o->type = make_type_pointer(c->allocator, o->type);
  2773. return;
  2774. }
  2775. if (check_is_not_addressable(c, o)) {
  2776. if (ast_node_expect(node, AstNode_UnaryExpr)) {
  2777. ast_node(ue, UnaryExpr, node);
  2778. gbString str = expr_to_string(ue->expr);
  2779. error(op, "Cannot take the pointer address of `%s`", str);
  2780. gb_string_free(str);
  2781. }
  2782. o->mode = Addressing_Invalid;
  2783. return;
  2784. }
  2785. o->mode = Addressing_Value;
  2786. o->type = make_type_pointer(c->allocator, o->type);
  2787. return;
  2788. }
  2789. }
  2790. if (!check_unary_op(c, o, op)) {
  2791. o->mode = Addressing_Invalid;
  2792. return;
  2793. }
  2794. if (o->mode == Addressing_Constant && !is_type_vector(o->type)) {
  2795. Type *type = base_type(o->type);
  2796. if (!is_type_constant_type(o->type)) {
  2797. gbString xt = type_to_string(o->type);
  2798. gbString err_str = expr_to_string(node);
  2799. error(op, "Invalid type, `%s`, for constant unary expression `%s`", xt, err_str);
  2800. gb_string_free(err_str);
  2801. gb_string_free(xt);
  2802. o->mode = Addressing_Invalid;
  2803. return;
  2804. }
  2805. i32 precision = 0;
  2806. if (is_type_unsigned(type)) {
  2807. precision = cast(i32)(8 * type_size_of(c->allocator, type));
  2808. }
  2809. o->value = exact_unary_operator_value(op.kind, o->value, precision);
  2810. if (is_type_typed(type)) {
  2811. if (node != nullptr) {
  2812. o->expr = node;
  2813. }
  2814. check_is_expressible(c, o, type);
  2815. }
  2816. return;
  2817. }
  2818. o->mode = Addressing_Value;
  2819. }
  2820. void check_comparison(Checker *c, Operand *x, Operand *y, TokenKind op) {
  2821. if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
  2822. bool comp = are_types_identical(x->type, y->type);
  2823. switch (op) {
  2824. case Token_CmpEq: comp = comp; break;
  2825. case Token_NotEq: comp = !comp; break;
  2826. }
  2827. x->mode = Addressing_Constant;
  2828. x->type = t_untyped_bool;
  2829. x->value = exact_value_bool(comp);
  2830. return;
  2831. }
  2832. gbString err_str = nullptr;
  2833. defer (if (err_str != nullptr) {
  2834. gb_string_free(err_str);
  2835. });
  2836. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  2837. defer (gb_temp_arena_memory_end(tmp));
  2838. if (check_is_assignable_to(c, x, y->type) ||
  2839. check_is_assignable_to(c, y, x->type)) {
  2840. Type *err_type = x->type;
  2841. bool defined = false;
  2842. switch (op) {
  2843. case Token_CmpEq:
  2844. case Token_NotEq:
  2845. defined = is_type_comparable(x->type) ||
  2846. (is_operand_nil(*x) && type_has_nil(y->type)) ||
  2847. (is_operand_nil(*y) && type_has_nil(x->type));
  2848. break;
  2849. case Token_Lt:
  2850. case Token_Gt:
  2851. case Token_LtEq:
  2852. case Token_GtEq: {
  2853. defined = is_type_ordered(x->type);
  2854. } break;
  2855. }
  2856. if (!defined) {
  2857. if (x->type == err_type && is_operand_nil(*x)) {
  2858. err_type = y->type;
  2859. }
  2860. gbString type_string = type_to_string(err_type);
  2861. err_str = gb_string_make(c->tmp_allocator,
  2862. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(token_strings[op]), type_string));
  2863. gb_string_free(type_string);
  2864. }
  2865. } else {
  2866. gbString xt = type_to_string(x->type);
  2867. gbString yt = type_to_string(y->type);
  2868. err_str = gb_string_make(c->tmp_allocator,
  2869. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  2870. gb_string_free(yt);
  2871. gb_string_free(xt);
  2872. }
  2873. if (err_str != nullptr) {
  2874. error(x->expr, "Cannot compare expression, %s", err_str);
  2875. x->type = t_untyped_bool;
  2876. } else {
  2877. if (x->mode == Addressing_Constant &&
  2878. y->mode == Addressing_Constant) {
  2879. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  2880. } else {
  2881. x->mode = Addressing_Value;
  2882. update_expr_type(c, x->expr, default_type(x->type), true);
  2883. update_expr_type(c, y->expr, default_type(y->type), true);
  2884. }
  2885. if (is_type_vector(base_type(y->type))) {
  2886. x->type = make_type_vector(c->allocator, t_bool, base_type(y->type)->Vector.count);
  2887. } else {
  2888. x->type = t_untyped_bool;
  2889. }
  2890. }
  2891. }
  2892. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  2893. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  2894. ast_node(be, BinaryExpr, node);
  2895. ExactValue x_val = {};
  2896. if (x->mode == Addressing_Constant) {
  2897. x_val = exact_value_to_integer(x->value);
  2898. }
  2899. bool x_is_untyped = is_type_untyped(x->type);
  2900. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  2901. gbString err_str = expr_to_string(x->expr);
  2902. error(node, "Shifted operand `%s` must be an integer", err_str);
  2903. gb_string_free(err_str);
  2904. x->mode = Addressing_Invalid;
  2905. return;
  2906. }
  2907. if (is_type_unsigned(y->type)) {
  2908. } else if (is_type_untyped(y->type)) {
  2909. convert_to_typed(c, y, t_untyped_integer, 0);
  2910. if (y->mode == Addressing_Invalid) {
  2911. x->mode = Addressing_Invalid;
  2912. return;
  2913. }
  2914. } else {
  2915. gbString err_str = expr_to_string(y->expr);
  2916. error(node, "Shift amount `%s` must be an unsigned integer", err_str);
  2917. gb_string_free(err_str);
  2918. x->mode = Addressing_Invalid;
  2919. return;
  2920. }
  2921. if (x->mode == Addressing_Constant) {
  2922. if (y->mode == Addressing_Constant) {
  2923. ExactValue y_val = exact_value_to_integer(y->value);
  2924. if (y_val.kind != ExactValue_Integer) {
  2925. gbString err_str = expr_to_string(y->expr);
  2926. error(node, "Shift amount `%s` must be an unsigned integer", err_str);
  2927. gb_string_free(err_str);
  2928. x->mode = Addressing_Invalid;
  2929. return;
  2930. }
  2931. i64 amount = i128_to_i64(y_val.value_integer);
  2932. if (amount > 128) {
  2933. gbString err_str = expr_to_string(y->expr);
  2934. error(node, "Shift amount too large: `%s`", err_str);
  2935. gb_string_free(err_str);
  2936. x->mode = Addressing_Invalid;
  2937. return;
  2938. }
  2939. if (!is_type_integer(x->type)) {
  2940. // NOTE(bill): It could be an untyped float but still representable
  2941. // as an integer
  2942. x->type = t_untyped_integer;
  2943. }
  2944. x->value = exact_value_shift(be->op.kind, x_val, exact_value_i64(amount));
  2945. if (is_type_typed(x->type)) {
  2946. check_is_expressible(c, x, base_type(x->type));
  2947. }
  2948. return;
  2949. }
  2950. TokenPos pos = ast_node_token(x->expr).pos;
  2951. if (x_is_untyped) {
  2952. ExprInfo *info = check_get_expr_info(&c->info, x->expr);
  2953. if (info != nullptr) {
  2954. info->is_lhs = true;
  2955. }
  2956. x->mode = Addressing_Value;
  2957. // x->value = x_val;
  2958. return;
  2959. }
  2960. }
  2961. if (y->mode == Addressing_Constant && i128_lt(y->value.value_integer, I128_ZERO)) {
  2962. gbString err_str = expr_to_string(y->expr);
  2963. error(node, "Shift amount cannot be negative: `%s`", err_str);
  2964. gb_string_free(err_str);
  2965. }
  2966. if (!is_type_integer(x->type)) {
  2967. gbString err_str = expr_to_string(y->expr);
  2968. error(node, "Shift operand `%s` must be an integer", err_str);
  2969. gb_string_free(err_str);
  2970. x->mode = Addressing_Invalid;
  2971. return;
  2972. }
  2973. x->mode = Addressing_Value;
  2974. }
  2975. String check_down_cast_name(Type *dst_, Type *src_) {
  2976. String result = {};
  2977. Type *dst = type_deref(dst_);
  2978. Type *src = type_deref(src_);
  2979. Type *dst_s = base_type(dst);
  2980. GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
  2981. for (isize i = 0; i < dst_s->Record.field_count; i++) {
  2982. Entity *f = dst_s->Record.fields[i];
  2983. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  2984. if (f->flags & EntityFlag_Using) {
  2985. if (are_types_identical(f->type, src_)) {
  2986. return f->token.string;
  2987. }
  2988. if (are_types_identical(type_deref(f->type), src_)) {
  2989. return f->token.string;
  2990. }
  2991. if (!is_type_pointer(f->type)) {
  2992. result = check_down_cast_name(f->type, src_);
  2993. if (result.len > 0) {
  2994. return result;
  2995. }
  2996. }
  2997. }
  2998. }
  2999. return result;
  3000. }
  3001. Operand check_ptr_addition(Checker *c, TokenKind op, Operand *ptr, Operand *offset, AstNode *node) {
  3002. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  3003. ast_node(be, BinaryExpr, node);
  3004. GB_ASSERT(is_type_pointer(ptr->type));
  3005. GB_ASSERT(is_type_integer(offset->type));
  3006. GB_ASSERT(op == Token_Add || op == Token_Sub);
  3007. Operand operand = {};
  3008. operand.mode = Addressing_Value;
  3009. operand.type = ptr->type;
  3010. operand.expr = node;
  3011. if (base_type(ptr->type) == t_rawptr) {
  3012. gbString str = type_to_string(ptr->type);
  3013. error(node, "Invalid pointer type for pointer arithmetic: `%s`", str);
  3014. gb_string_free(str);
  3015. operand.mode = Addressing_Invalid;
  3016. return operand;
  3017. }
  3018. Type *base_ptr = base_type(ptr->type); GB_ASSERT(base_ptr->kind == Type_Pointer);
  3019. Type *elem = base_ptr->Pointer.elem;
  3020. i64 elem_size = type_size_of(c->allocator, elem);
  3021. if (elem_size <= 0) {
  3022. gbString str = type_to_string(elem);
  3023. error(node, "Size of pointer's element type `%s` is zero and cannot be used for pointer arithmetic", str);
  3024. gb_string_free(str);
  3025. operand.mode = Addressing_Invalid;
  3026. return operand;
  3027. }
  3028. if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
  3029. i64 ptr_val = ptr->value.value_pointer;
  3030. i64 offset_val = i128_to_i64(exact_value_to_integer(offset->value).value_integer);
  3031. i64 new_ptr_val = ptr_val;
  3032. if (op == Token_Add) {
  3033. new_ptr_val += elem_size*offset_val;
  3034. } else {
  3035. new_ptr_val -= elem_size*offset_val;
  3036. }
  3037. operand.mode = Addressing_Constant;
  3038. operand.value = exact_value_pointer(new_ptr_val);
  3039. }
  3040. return operand;
  3041. }
  3042. bool check_is_castable_to(Checker *c, Operand *operand, Type *y) {
  3043. if (check_is_assignable_to(c, operand, y)) {
  3044. return true;
  3045. }
  3046. Type *x = operand->type;
  3047. Type *src = core_type(x);
  3048. Type *dst = core_type(y);
  3049. if (are_types_identical(src, dst)) {
  3050. return true;
  3051. }
  3052. if (dst->kind == Type_Array && src->kind == Type_Array) {
  3053. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  3054. return dst->Array.count == src->Array.count;
  3055. }
  3056. }
  3057. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  3058. return are_types_identical(dst->Slice.elem, src->Slice.elem);
  3059. }
  3060. // Cast between booleans and integers
  3061. if (is_type_boolean(src) || is_type_integer(src)) {
  3062. if (is_type_boolean(dst) || is_type_integer(dst)) {
  3063. return true;
  3064. }
  3065. }
  3066. // Cast between numbers
  3067. if (is_type_integer(src) || is_type_float(src)) {
  3068. if (is_type_integer(dst) || is_type_float(dst)) {
  3069. return true;
  3070. }
  3071. }
  3072. if (is_type_integer(src) && is_type_rune(dst)) {
  3073. return true;
  3074. }
  3075. if (is_type_rune(src) && is_type_integer(dst)) {
  3076. return true;
  3077. }
  3078. if (is_type_complex(src) && is_type_complex(dst)) {
  3079. return true;
  3080. }
  3081. if (is_type_bit_field_value(src) && is_type_integer(dst)) {
  3082. return true;
  3083. }
  3084. if (is_type_bit_field_value(src) && is_type_boolean(dst)) {
  3085. return src->BitFieldValue.bits == 1;
  3086. }
  3087. // Cast between pointers
  3088. if (is_type_pointer(src) && is_type_pointer(dst)) {
  3089. #if 0
  3090. Type *s = base_type(type_deref(src));
  3091. if (is_type_union(s)) {
  3092. // NOTE(bill): Should the error be here?!
  3093. // NOTE(bill): This error should suppress the next casting error as it's at the same position
  3094. gbString xs = type_to_string(x);
  3095. gbString ys = type_to_string(y);
  3096. error(operand->expr, "Cannot cast from a union pointer `%s` to `%s`, try using `union_cast` or cast to a `rawptr`", xs, ys);
  3097. gb_string_free(ys);
  3098. gb_string_free(xs);
  3099. return false;
  3100. }
  3101. #endif
  3102. return true;
  3103. }
  3104. // (u)int <-> rawptr
  3105. if (is_type_int_or_uint(src) && is_type_rawptr(dst)) {
  3106. return true;
  3107. }
  3108. if (is_type_rawptr(src) && is_type_int_or_uint(dst)) {
  3109. return true;
  3110. }
  3111. // []byte/[]u8 <-> string
  3112. if (is_type_u8_slice(src) && is_type_string(dst)) {
  3113. return true;
  3114. }
  3115. if (is_type_string(src) && is_type_u8_slice(dst)) {
  3116. // if (is_type_typed(src)) {
  3117. return true;
  3118. // }
  3119. }
  3120. // proc <-> proc
  3121. if (is_type_proc(src) && is_type_proc(dst)) {
  3122. return true;
  3123. }
  3124. // proc -> rawptr
  3125. if (is_type_proc(src) && is_type_rawptr(dst)) {
  3126. return true;
  3127. }
  3128. // rawptr -> proc
  3129. if (is_type_rawptr(src) && is_type_proc(dst)) {
  3130. return true;
  3131. }
  3132. return false;
  3133. }
  3134. void check_cast(Checker *c, Operand *x, Type *type) {
  3135. bool is_const_expr = x->mode == Addressing_Constant;
  3136. bool can_convert = false;
  3137. Type *bt = base_type(type);
  3138. if (is_const_expr && is_type_constant_type(bt)) {
  3139. if (core_type(bt)->kind == Type_Basic) {
  3140. if (check_representable_as_constant(c, x->value, bt, &x->value)) {
  3141. can_convert = true;
  3142. } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
  3143. can_convert = true;
  3144. }
  3145. }
  3146. } else if (check_is_castable_to(c, x, type)) {
  3147. if (x->mode != Addressing_Constant) {
  3148. x->mode = Addressing_Value;
  3149. } else if (is_type_slice(type) && is_type_string(x->type)) {
  3150. x->mode = Addressing_Value;
  3151. } else if (!is_type_vector(x->type) && is_type_vector(type)) {
  3152. x->mode = Addressing_Value;
  3153. }
  3154. can_convert = true;
  3155. }
  3156. if (!can_convert) {
  3157. gbString expr_str = expr_to_string(x->expr);
  3158. gbString to_type = type_to_string(type);
  3159. gbString from_type = type_to_string(x->type);
  3160. error(x->expr, "Cannot cast `%s` as `%s` from `%s`", expr_str, to_type, from_type);
  3161. gb_string_free(from_type);
  3162. gb_string_free(to_type);
  3163. gb_string_free(expr_str);
  3164. x->mode = Addressing_Invalid;
  3165. return;
  3166. }
  3167. if (is_type_untyped(x->type)) {
  3168. Type *final_type = type;
  3169. if (is_const_expr && !is_type_constant_type(type)) {
  3170. final_type = default_type(x->type);
  3171. }
  3172. update_expr_type(c, x->expr, final_type, true);
  3173. }
  3174. x->type = type;
  3175. }
  3176. bool check_binary_vector_expr(Checker *c, Token op, Operand *x, Operand *y) {
  3177. if (is_type_vector(x->type) && !is_type_vector(y->type)) {
  3178. if (check_is_assignable_to(c, y, x->type)) {
  3179. if (check_binary_op(c, x, op)) {
  3180. return true;
  3181. }
  3182. }
  3183. }
  3184. return false;
  3185. }
  3186. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  3187. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  3188. Operand y_ = {}, *y = &y_;
  3189. ast_node(be, BinaryExpr, node);
  3190. Token op = be->op;
  3191. switch (op.kind) {
  3192. case Token_CmpEq:
  3193. case Token_NotEq: {
  3194. // NOTE(bill): Allow comparisons between types
  3195. check_expr_or_type(c, x, be->left);
  3196. check_expr_or_type(c, y, be->right);
  3197. bool xt = x->mode == Addressing_Type;
  3198. bool yt = y->mode == Addressing_Type;
  3199. // If only one is a type, this is an error
  3200. if (xt ^ yt) {
  3201. GB_ASSERT(xt != yt);
  3202. if (xt) error_operand_not_expression(x);
  3203. if (yt) error_operand_not_expression(y);
  3204. }
  3205. } break;
  3206. default:
  3207. check_expr(c, x, be->left);
  3208. check_expr(c, y, be->right);
  3209. break;
  3210. }
  3211. if (x->mode == Addressing_Invalid) {
  3212. return;
  3213. }
  3214. if (y->mode == Addressing_Invalid) {
  3215. x->mode = Addressing_Invalid;
  3216. x->expr = y->expr;
  3217. return;
  3218. }
  3219. if (token_is_shift(op.kind)) {
  3220. check_shift(c, x, y, node);
  3221. return;
  3222. }
  3223. if (op.kind == Token_Add || op.kind == Token_Sub) {
  3224. if (is_type_pointer(x->type) && is_type_integer(y->type)) {
  3225. *x = check_ptr_addition(c, op.kind, x, y, node);
  3226. return;
  3227. } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
  3228. if (op.kind == Token_Sub) {
  3229. gbString lhs = expr_to_string(x->expr);
  3230. gbString rhs = expr_to_string(y->expr);
  3231. error(node, "Invalid pointer arithmetic, did you mean `%s %.*s %s`?", rhs, LIT(op.string), lhs);
  3232. gb_string_free(rhs);
  3233. gb_string_free(lhs);
  3234. x->mode = Addressing_Invalid;
  3235. return;
  3236. }
  3237. *x = check_ptr_addition(c, op.kind, y, x, node);
  3238. return;
  3239. }
  3240. }
  3241. convert_to_typed(c, x, y->type, 0);
  3242. if (x->mode == Addressing_Invalid) {
  3243. return;
  3244. }
  3245. convert_to_typed(c, y, x->type, 0);
  3246. if (y->mode == Addressing_Invalid) {
  3247. x->mode = Addressing_Invalid;
  3248. return;
  3249. }
  3250. if (token_is_comparison(op.kind)) {
  3251. check_comparison(c, x, y, op.kind);
  3252. return;
  3253. }
  3254. if (check_binary_vector_expr(c, op, x, y)) {
  3255. x->mode = Addressing_Value;
  3256. x->type = x->type;
  3257. return;
  3258. }
  3259. if (check_binary_vector_expr(c, op, y, x)) {
  3260. x->mode = Addressing_Value;
  3261. x->type = y->type;
  3262. return;
  3263. }
  3264. if (!are_types_identical(x->type, y->type)) {
  3265. if (x->type != t_invalid &&
  3266. y->type != t_invalid) {
  3267. gbString xt = type_to_string(x->type);
  3268. gbString yt = type_to_string(y->type);
  3269. gbString expr_str = expr_to_string(x->expr);
  3270. error(op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", expr_str, xt, yt);
  3271. gb_string_free(expr_str);
  3272. gb_string_free(yt);
  3273. gb_string_free(xt);
  3274. }
  3275. x->mode = Addressing_Invalid;
  3276. return;
  3277. }
  3278. if (!check_binary_op(c, x, op)) {
  3279. x->mode = Addressing_Invalid;
  3280. return;
  3281. }
  3282. switch (op.kind) {
  3283. case Token_Quo:
  3284. case Token_Mod:
  3285. case Token_ModMod:
  3286. case Token_QuoEq:
  3287. case Token_ModEq:
  3288. case Token_ModModEq:
  3289. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  3290. y->mode == Addressing_Constant) {
  3291. bool fail = false;
  3292. switch (y->value.kind) {
  3293. case ExactValue_Integer:
  3294. if (i128_eq(y->value.value_integer, I128_ZERO)) {
  3295. fail = true;
  3296. }
  3297. break;
  3298. case ExactValue_Float:
  3299. if (y->value.value_float == 0.0) {
  3300. fail = true;
  3301. }
  3302. break;
  3303. }
  3304. if (fail) {
  3305. error(y->expr, "Division by zero not allowed");
  3306. x->mode = Addressing_Invalid;
  3307. return;
  3308. }
  3309. }
  3310. }
  3311. if (x->mode == Addressing_Constant &&
  3312. y->mode == Addressing_Constant) {
  3313. ExactValue a = x->value;
  3314. ExactValue b = y->value;
  3315. Type *type = base_type(x->type);
  3316. if (is_type_pointer(type)) {
  3317. GB_ASSERT(op.kind == Token_Sub);
  3318. i64 bytes = a.value_pointer - b.value_pointer;
  3319. i64 diff = bytes/type_size_of(c->allocator, type);
  3320. x->value = exact_value_pointer(diff);
  3321. return;
  3322. }
  3323. if (!is_type_constant_type(type)) {
  3324. gbString xt = type_to_string(x->type);
  3325. gbString err_str = expr_to_string(node);
  3326. error(op, "Invalid type, `%s`, for constant binary expression `%s`", xt, err_str);
  3327. gb_string_free(err_str);
  3328. gb_string_free(xt);
  3329. x->mode = Addressing_Invalid;
  3330. return;
  3331. }
  3332. if (op.kind == Token_Quo && is_type_integer(type)) {
  3333. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  3334. }
  3335. x->value = exact_binary_operator_value(op.kind, a, b);
  3336. if (is_type_typed(type)) {
  3337. if (node != nullptr) {
  3338. x->expr = node;
  3339. }
  3340. check_is_expressible(c, x, type);
  3341. }
  3342. return;
  3343. }
  3344. x->mode = Addressing_Value;
  3345. }
  3346. void update_expr_type(Checker *c, AstNode *e, Type *type, bool final) {
  3347. ExprInfo *found = check_get_expr_info(&c->info, e);
  3348. if (found == nullptr) {
  3349. return;
  3350. }
  3351. ExprInfo old = *found;
  3352. switch (e->kind) {
  3353. case_ast_node(ue, UnaryExpr, e);
  3354. if (old.value.kind != ExactValue_Invalid) {
  3355. // NOTE(bill): if `e` is constant, the operands will be constant too.
  3356. // They don't need to be updated as they will be updated later and
  3357. // checked at the end of general checking stage.
  3358. break;
  3359. }
  3360. update_expr_type(c, ue->expr, type, final);
  3361. case_end;
  3362. case_ast_node(be, BinaryExpr, e);
  3363. if (old.value.kind != ExactValue_Invalid) {
  3364. // See above note in UnaryExpr case
  3365. break;
  3366. }
  3367. if (token_is_comparison(be->op.kind)) {
  3368. // NOTE(bill): Do nothing as the types are fine
  3369. } else if (token_is_shift(be->op.kind)) {
  3370. update_expr_type(c, be->left, type, final);
  3371. } else {
  3372. update_expr_type(c, be->left, type, final);
  3373. update_expr_type(c, be->right, type, final);
  3374. }
  3375. case_end;
  3376. case_ast_node(pe, ParenExpr, e);
  3377. update_expr_type(c, pe->expr, type, final);
  3378. case_end;
  3379. }
  3380. if (!final && is_type_untyped(type)) {
  3381. old.type = base_type(type);
  3382. check_set_expr_info(&c->info, e, old);
  3383. return;
  3384. }
  3385. // We need to remove it and then give it a new one
  3386. check_remove_expr_info(&c->info, e);
  3387. if (old.is_lhs && !is_type_integer(type)) {
  3388. gbString expr_str = expr_to_string(e);
  3389. gbString type_str = type_to_string(type);
  3390. error(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  3391. gb_string_free(type_str);
  3392. gb_string_free(expr_str);
  3393. return;
  3394. }
  3395. add_type_and_value(&c->info, e, old.mode, type, old.value);
  3396. }
  3397. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  3398. ExprInfo *found = check_get_expr_info(&c->info, e);
  3399. if (found) {
  3400. found->value = value;
  3401. }
  3402. }
  3403. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  3404. gbString expr_str = expr_to_string(operand->expr);
  3405. gbString type_str = type_to_string(target_type);
  3406. char *extra_text = "";
  3407. if (operand->mode == Addressing_Constant) {
  3408. if (i128_eq(operand->value.value_integer, I128_ZERO)) {
  3409. if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
  3410. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  3411. extra_text = " - Did you want `nil`?";
  3412. }
  3413. }
  3414. }
  3415. error(operand->expr, "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  3416. gb_string_free(type_str);
  3417. gb_string_free(expr_str);
  3418. operand->mode = Addressing_Invalid;
  3419. }
  3420. ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
  3421. Type *t = core_type(type);
  3422. if (is_type_boolean(t)) {
  3423. // v = exact_value_to_boolean(v);
  3424. } else if (is_type_float(t)) {
  3425. v = exact_value_to_float(v);
  3426. } else if (is_type_integer(t)) {
  3427. v = exact_value_to_integer(v);
  3428. } else if (is_type_pointer(t)) {
  3429. v = exact_value_to_integer(v);
  3430. } else if (is_type_complex(t)) {
  3431. v = exact_value_to_complex(v);
  3432. }
  3433. return v;
  3434. }
  3435. // NOTE(bill): Set initial level to 0
  3436. void convert_to_typed(Checker *c, Operand *operand, Type *target_type, i32 level) {
  3437. GB_ASSERT_NOT_NULL(target_type);
  3438. if (operand->mode == Addressing_Invalid ||
  3439. operand->mode == Addressing_Type ||
  3440. is_type_typed(operand->type) ||
  3441. target_type == t_invalid) {
  3442. return;
  3443. }
  3444. if (is_type_untyped(target_type)) {
  3445. GB_ASSERT(operand->type->kind == Type_Basic);
  3446. GB_ASSERT(target_type->kind == Type_Basic);
  3447. BasicKind x_kind = operand->type->Basic.kind;
  3448. BasicKind y_kind = target_type->Basic.kind;
  3449. if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
  3450. if (x_kind < y_kind) {
  3451. operand->type = target_type;
  3452. update_expr_type(c, operand->expr, target_type, false);
  3453. }
  3454. } else if (x_kind != y_kind) {
  3455. operand->mode = Addressing_Invalid;
  3456. convert_untyped_error(c, operand, target_type);
  3457. return;
  3458. }
  3459. return;
  3460. }
  3461. Type *t = core_type(target_type);
  3462. switch (t->kind) {
  3463. case Type_Basic:
  3464. if (operand->mode == Addressing_Constant) {
  3465. check_is_expressible(c, operand, t);
  3466. if (operand->mode == Addressing_Invalid) {
  3467. return;
  3468. }
  3469. update_expr_value(c, operand->expr, operand->value);
  3470. } else {
  3471. switch (operand->type->Basic.kind) {
  3472. case Basic_UntypedBool:
  3473. if (!is_type_boolean(target_type)) {
  3474. operand->mode = Addressing_Invalid;
  3475. convert_untyped_error(c, operand, target_type);
  3476. return;
  3477. }
  3478. break;
  3479. case Basic_UntypedInteger:
  3480. case Basic_UntypedFloat:
  3481. case Basic_UntypedComplex:
  3482. case Basic_UntypedRune:
  3483. if (!is_type_numeric(target_type)) {
  3484. operand->mode = Addressing_Invalid;
  3485. convert_untyped_error(c, operand, target_type);
  3486. return;
  3487. }
  3488. break;
  3489. case Basic_UntypedNil:
  3490. if (is_type_any(target_type)) {
  3491. target_type = t_untyped_nil;
  3492. } else if (!type_has_nil(target_type)) {
  3493. operand->mode = Addressing_Invalid;
  3494. convert_untyped_error(c, operand, target_type);
  3495. return;
  3496. }
  3497. break;
  3498. }
  3499. }
  3500. break;
  3501. case Type_Vector: {
  3502. Type *elem = base_vector_type(t);
  3503. if (check_is_assignable_to(c, operand, elem)) {
  3504. operand->mode = Addressing_Value;
  3505. } else {
  3506. operand->mode = Addressing_Invalid;
  3507. convert_untyped_error(c, operand, target_type);
  3508. return;
  3509. }
  3510. } break;
  3511. default:
  3512. if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
  3513. target_type = t_untyped_undef;
  3514. } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  3515. operand->mode = Addressing_Invalid;
  3516. convert_untyped_error(c, operand, target_type);
  3517. return;
  3518. }
  3519. target_type = t_untyped_nil;
  3520. break;
  3521. }
  3522. operand->type = target_type;
  3523. update_expr_type(c, operand->expr, target_type, true);
  3524. }
  3525. bool check_index_value(Checker *c, bool open_range, AstNode *index_value, i64 max_count, i64 *value) {
  3526. Operand operand = {Addressing_Invalid};
  3527. check_expr(c, &operand, index_value);
  3528. if (operand.mode == Addressing_Invalid) {
  3529. if (value) *value = 0;
  3530. return false;
  3531. }
  3532. convert_to_typed(c, &operand, t_int, 0);
  3533. if (operand.mode == Addressing_Invalid) {
  3534. if (value) *value = 0;
  3535. return false;
  3536. }
  3537. if (!is_type_integer(operand.type)) {
  3538. gbString expr_str = expr_to_string(operand.expr);
  3539. error(operand.expr, "Index `%s` must be an integer", expr_str);
  3540. gb_string_free(expr_str);
  3541. if (value) *value = 0;
  3542. return false;
  3543. }
  3544. if (operand.mode == Addressing_Constant &&
  3545. (c->context.stmt_state_flags & StmtStateFlag_no_bounds_check) == 0) {
  3546. i64 i = i128_to_i64(exact_value_to_integer(operand.value).value_integer);
  3547. if (i < 0) {
  3548. gbString expr_str = expr_to_string(operand.expr);
  3549. error(operand.expr, "Index `%s` cannot be a negative value", expr_str);
  3550. gb_string_free(expr_str);
  3551. if (value) *value = 0;
  3552. return false;
  3553. }
  3554. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  3555. if (value) *value = i;
  3556. bool out_of_bounds = false;
  3557. if (open_range) {
  3558. out_of_bounds = i >= max_count;
  3559. } else {
  3560. out_of_bounds = i > max_count;
  3561. }
  3562. if (out_of_bounds) {
  3563. gbString expr_str = expr_to_string(operand.expr);
  3564. error(operand.expr, "Index `%s` is out of bounds range 0..<%lld", expr_str, max_count);
  3565. gb_string_free(expr_str);
  3566. return false;
  3567. }
  3568. return true;
  3569. }
  3570. }
  3571. // NOTE(bill): It's alright :D
  3572. if (value) *value = -1;
  3573. return true;
  3574. }
  3575. isize entity_overload_count(Scope *s, String name) {
  3576. Entity *e = scope_lookup_entity(s, name);
  3577. if (e == nullptr) {
  3578. return 0;
  3579. }
  3580. if (e->kind == Entity_Procedure) {
  3581. // NOTE(bill): Overloads are only allowed with the same scope
  3582. return multi_map_count(&s->elements, hash_string(e->token.string));
  3583. }
  3584. return 1;
  3585. }
  3586. bool check_is_field_exported(Checker *c, Entity *field) {
  3587. if (field == nullptr) {
  3588. // NOTE(bill): Just incase
  3589. return true;
  3590. }
  3591. if (field->kind != Entity_Variable) {
  3592. return true;
  3593. }
  3594. Scope *file_scope = field->scope;
  3595. if (file_scope == nullptr) {
  3596. return true;
  3597. }
  3598. while (!file_scope->is_file) {
  3599. file_scope = file_scope->parent;
  3600. }
  3601. if (!is_entity_exported(field) && file_scope != c->context.file_scope) {
  3602. return false;
  3603. }
  3604. return true;
  3605. }
  3606. Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_hint) {
  3607. ast_node(se, SelectorExpr, node);
  3608. bool check_op_expr = true;
  3609. Entity *expr_entity = nullptr;
  3610. Entity *entity = nullptr;
  3611. Selection sel = {}; // NOTE(bill): Not used if it's an import name
  3612. operand->expr = node;
  3613. AstNode *op_expr = se->expr;
  3614. AstNode *selector = unparen_expr(se->selector);
  3615. if (selector == nullptr) {
  3616. operand->mode = Addressing_Invalid;
  3617. operand->expr = node;
  3618. return nullptr;
  3619. }
  3620. if (selector->kind != AstNode_Ident && selector->kind != AstNode_BasicLit) {
  3621. // if (selector->kind != AstNode_Ident) {
  3622. error(selector, "Illegal selector kind: `%.*s`", LIT(ast_node_strings[selector->kind]));
  3623. operand->mode = Addressing_Invalid;
  3624. operand->expr = node;
  3625. return nullptr;
  3626. }
  3627. if (op_expr->kind == AstNode_Ident) {
  3628. String op_name = op_expr->Ident.token.string;
  3629. Entity *e = scope_lookup_entity(c->context.scope, op_name);
  3630. bool is_alias = false;
  3631. while (e->kind == Entity_Alias) {
  3632. GB_ASSERT(e->Alias.base != nullptr);
  3633. e = e->Alias.base;
  3634. is_alias = true;
  3635. }
  3636. add_entity_use(c, op_expr, e);
  3637. expr_entity = e;
  3638. Entity *original_e = e;
  3639. if (e != nullptr && e->kind == Entity_ImportName && selector->kind == AstNode_Ident) {
  3640. // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
  3641. // It pretty much needs to be in this order and this way
  3642. // If you can clean this up, please do but be really careful
  3643. String import_name = op_name;
  3644. Scope *import_scope = e->ImportName.scope;
  3645. String entity_name = selector->Ident.token.string;
  3646. check_op_expr = false;
  3647. entity = scope_lookup_entity(import_scope, entity_name);
  3648. bool is_declared = entity != nullptr;
  3649. if (is_declared) {
  3650. if (entity->kind == Entity_Builtin) {
  3651. // NOTE(bill): Builtin's are in the universe scope which is part of every scopes hierarchy
  3652. // This means that we should just ignore the found result through it
  3653. is_declared = false;
  3654. } else if (entity->scope->is_global && !import_scope->is_global) {
  3655. is_declared = false;
  3656. }
  3657. }
  3658. if (!is_declared) {
  3659. error(op_expr, "`%.*s` is not declared by `%.*s`", LIT(entity_name), LIT(import_name));
  3660. operand->mode = Addressing_Invalid;
  3661. operand->expr = node;
  3662. return nullptr;
  3663. }
  3664. bool is_alias = false;
  3665. while (entity->kind == Entity_Alias) {
  3666. GB_ASSERT(e->Alias.base != nullptr);
  3667. entity = entity->Alias.base;
  3668. is_alias = true;
  3669. }
  3670. check_entity_decl(c, entity, nullptr, nullptr);
  3671. GB_ASSERT(entity->type != nullptr);
  3672. if (is_alias) {
  3673. // TODO(bill): Which scope do you search for for an alias?
  3674. // import_scope = entity->scope;
  3675. entity_name = entity->token.string;
  3676. }
  3677. isize overload_count = entity_overload_count(import_scope, entity_name);
  3678. bool is_overloaded = overload_count > 1;
  3679. bool implicit_is_found = is_entity_implicitly_imported(e, entity);
  3680. bool is_not_exported = !is_entity_exported(entity);
  3681. if (!implicit_is_found) {
  3682. is_not_exported = false;
  3683. } else if (entity->kind == Entity_ImportName) {
  3684. is_not_exported = true;
  3685. }
  3686. if (is_not_exported) {
  3687. gbString sel_str = expr_to_string(selector);
  3688. error(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
  3689. gb_string_free(sel_str);
  3690. operand->mode = Addressing_Invalid;
  3691. operand->expr = node;
  3692. return nullptr;
  3693. }
  3694. if (is_overloaded) {
  3695. HashKey key = hash_string(entity_name);
  3696. bool skip = false;
  3697. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  3698. multi_map_get_all(&import_scope->elements, key, procs);
  3699. for (isize i = 0; i < overload_count; i++) {
  3700. Type *t = base_type(procs[i]->type);
  3701. if (t == t_invalid) {
  3702. continue;
  3703. }
  3704. // NOTE(bill): Check to see if it's imported
  3705. if (map_get(&import_scope->implicit, hash_entity(procs[i]))) {
  3706. gb_swap(Entity *, procs[i], procs[overload_count-1]);
  3707. overload_count--;
  3708. i--; // NOTE(bill): Counteract the post event
  3709. continue;
  3710. }
  3711. Operand x = {};
  3712. x.mode = Addressing_Value;
  3713. x.type = t;
  3714. if (type_hint != nullptr) {
  3715. if (check_is_assignable_to(c, &x, type_hint)) {
  3716. entity = procs[i];
  3717. skip = true;
  3718. break;
  3719. }
  3720. }
  3721. }
  3722. if (overload_count > 0 && !skip) {
  3723. operand->mode = Addressing_Overload;
  3724. operand->type = t_invalid;
  3725. operand->expr = node;
  3726. operand->overload_count = overload_count;
  3727. operand->overload_entities = procs;
  3728. return procs[0];
  3729. }
  3730. }
  3731. }
  3732. }
  3733. if (check_op_expr) {
  3734. check_expr_base(c, operand, op_expr, nullptr);
  3735. if (operand->mode == Addressing_Invalid) {
  3736. operand->mode = Addressing_Invalid;
  3737. operand->expr = node;
  3738. return nullptr;
  3739. }
  3740. }
  3741. if (entity == nullptr && selector->kind == AstNode_Ident) {
  3742. String field_name = selector->Ident.token.string;
  3743. sel = lookup_field(c->allocator, operand->type, field_name, operand->mode == Addressing_Type);
  3744. if (operand->mode != Addressing_Type && !check_is_field_exported(c, sel.entity)) {
  3745. error(op_expr, "`%.*s` is an unexported field", LIT(field_name));
  3746. operand->mode = Addressing_Invalid;
  3747. operand->expr = node;
  3748. return nullptr;
  3749. }
  3750. entity = sel.entity;
  3751. // NOTE(bill): Add type info needed for fields like `names`
  3752. if (entity != nullptr && (entity->flags&EntityFlag_TypeField)) {
  3753. add_type_info_type(c, operand->type);
  3754. }
  3755. }
  3756. if (entity == nullptr && selector->kind == AstNode_BasicLit) {
  3757. if (is_type_struct(operand->type) || is_type_tuple(operand->type)) {
  3758. Type *type = base_type(operand->type);
  3759. Operand o = {};
  3760. check_expr(c, &o, selector);
  3761. if (o.mode != Addressing_Constant ||
  3762. !is_type_integer(o.type)) {
  3763. error(op_expr, "Indexed based selectors must be a constant integer %s");
  3764. operand->mode = Addressing_Invalid;
  3765. operand->expr = node;
  3766. return nullptr;
  3767. }
  3768. i64 index = i128_to_i64(o.value.value_integer);
  3769. if (index < 0) {
  3770. error(o.expr, "Index %lld cannot be a negative value", index);
  3771. operand->mode = Addressing_Invalid;
  3772. operand->expr = node;
  3773. return nullptr;
  3774. }
  3775. i64 max_count = 0;
  3776. switch (type->kind) {
  3777. case Type_Record: max_count = type->Record.field_count; break;
  3778. case Type_Tuple: max_count = type->Tuple.variable_count; break;
  3779. }
  3780. if (index >= max_count) {
  3781. error(o.expr, "Index %lld is out of bounds range 0..<%lld", index, max_count);
  3782. operand->mode = Addressing_Invalid;
  3783. operand->expr = node;
  3784. return nullptr;
  3785. }
  3786. sel = lookup_field_from_index(heap_allocator(), type, index);
  3787. entity = sel.entity;
  3788. GB_ASSERT(entity != nullptr);
  3789. } else {
  3790. error(op_expr, "Indexed based selectors may only be used on structs or tuples");
  3791. operand->mode = Addressing_Invalid;
  3792. operand->expr = node;
  3793. return nullptr;
  3794. }
  3795. }
  3796. if (entity == nullptr &&
  3797. operand->type != nullptr && is_type_untyped(operand->type) && is_type_string(operand->type)) {
  3798. String s = operand->value.value_string;
  3799. operand->mode = Addressing_Constant;
  3800. operand->value = exact_value_i64(s.len);
  3801. operand->type = t_untyped_integer;
  3802. return nullptr;
  3803. }
  3804. if (entity == nullptr) {
  3805. gbString op_str = expr_to_string(op_expr);
  3806. gbString type_str = type_to_string(operand->type);
  3807. gbString sel_str = expr_to_string(selector);
  3808. error(op_expr, "`%s` of type `%s` has no field `%s`", op_str, type_str, sel_str);
  3809. gb_string_free(sel_str);
  3810. gb_string_free(type_str);
  3811. gb_string_free(op_str);
  3812. operand->mode = Addressing_Invalid;
  3813. operand->expr = node;
  3814. return nullptr;
  3815. }
  3816. if (expr_entity != nullptr && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
  3817. gbString op_str = expr_to_string(op_expr);
  3818. gbString type_str = type_to_string(operand->type);
  3819. gbString sel_str = expr_to_string(selector);
  3820. error(op_expr, "Cannot access non-constant field `%s` from `%s`", sel_str, op_str);
  3821. gb_string_free(sel_str);
  3822. gb_string_free(type_str);
  3823. gb_string_free(op_str);
  3824. operand->mode = Addressing_Invalid;
  3825. operand->expr = node;
  3826. return nullptr;
  3827. }
  3828. add_entity_use(c, selector, entity);
  3829. switch (entity->kind) {
  3830. case Entity_Constant:
  3831. operand->mode = Addressing_Constant;
  3832. operand->value = entity->Constant.value;
  3833. break;
  3834. case Entity_Variable:
  3835. // TODO(bill): Is this the rule I need?
  3836. if (operand->mode == Addressing_Immutable) {
  3837. // Okay
  3838. } else if (sel.indirect || operand->mode != Addressing_Value) {
  3839. operand->mode = Addressing_Variable;
  3840. } else {
  3841. operand->mode = Addressing_Value;
  3842. }
  3843. break;
  3844. case Entity_TypeName:
  3845. operand->mode = Addressing_Type;
  3846. break;
  3847. case Entity_Procedure:
  3848. operand->mode = Addressing_Value;
  3849. break;
  3850. case Entity_Builtin:
  3851. operand->mode = Addressing_Builtin;
  3852. operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
  3853. break;
  3854. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  3855. case Entity_Nil:
  3856. operand->mode = Addressing_Value;
  3857. break;
  3858. }
  3859. operand->type = entity->type;
  3860. operand->expr = node;
  3861. return entity;
  3862. }
  3863. bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  3864. GB_ASSERT(call->kind == AstNode_CallExpr);
  3865. ast_node(ce, CallExpr, call);
  3866. BuiltinProc *bp = &builtin_procs[id];
  3867. {
  3868. char *err = nullptr;
  3869. if (ce->args.count < bp->arg_count) {
  3870. err = "Too few";
  3871. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  3872. err = "Too many";
  3873. }
  3874. if (err != nullptr) {
  3875. gbString expr = expr_to_string(ce->proc);
  3876. error(ce->close, "%s arguments for `%s`, expected %td, got %td",
  3877. err, expr,
  3878. bp->arg_count, ce->args.count);
  3879. gb_string_free(expr);
  3880. return false;
  3881. }
  3882. }
  3883. if (ce->args.count > 0) {
  3884. if (ce->args[0]->kind == AstNode_FieldValue) {
  3885. error(call, "`field = value` calling is not allowed on built-in procedures");
  3886. return false;
  3887. }
  3888. }
  3889. bool vari_expand = (ce->ellipsis.pos.line != 0);
  3890. // if (vari_expand && id != BuiltinProc_append) {
  3891. // error(ce->ellipsis, "Invalid use of `..` with built-in procedure `append`");
  3892. // return false;
  3893. // }
  3894. switch (id) {
  3895. // case BuiltinProc_new:
  3896. case BuiltinProc_make:
  3897. case BuiltinProc_size_of:
  3898. case BuiltinProc_align_of:
  3899. case BuiltinProc_offset_of:
  3900. case BuiltinProc_type_info:
  3901. case BuiltinProc_transmute:
  3902. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  3903. break;
  3904. default:
  3905. if (ce->args.count > 0) {
  3906. check_multi_expr(c, operand, ce->args[0]);
  3907. }
  3908. break;
  3909. }
  3910. switch (id) {
  3911. default:
  3912. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_procs[id].name));
  3913. break;
  3914. case BuiltinProc_DIRECTIVE: {
  3915. ast_node(bd, BasicDirective, ce->proc);
  3916. String name = bd->name;
  3917. GB_ASSERT(name == "location");
  3918. if (ce->args.count > 1) {
  3919. error(ce->args[0], "`#location` expects either 0 or 1 arguments, got %td", ce->args.count);
  3920. }
  3921. if (ce->args.count > 0) {
  3922. AstNode *arg = ce->args[0];
  3923. Entity *e = nullptr;
  3924. Operand o = {};
  3925. if (arg->kind == AstNode_Ident) {
  3926. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  3927. } else if (arg->kind == AstNode_SelectorExpr) {
  3928. e = check_selector(c, &o, arg, nullptr);
  3929. }
  3930. if (e == nullptr) {
  3931. error(ce->args[0], "`#location` expected a valid entity name");
  3932. }
  3933. }
  3934. operand->type = t_source_code_location;
  3935. operand->mode = Addressing_Value;
  3936. } break;
  3937. case BuiltinProc_len:
  3938. case BuiltinProc_cap: {
  3939. // proc len(Type) -> int
  3940. // proc cap(Type) -> int
  3941. Type *op_type = type_deref(operand->type);
  3942. Type *type = t_int;
  3943. AddressingMode mode = Addressing_Invalid;
  3944. ExactValue value = {};
  3945. if (is_type_string(op_type) && id == BuiltinProc_len) {
  3946. if (operand->mode == Addressing_Constant) {
  3947. mode = Addressing_Constant;
  3948. String str = operand->value.value_string;
  3949. value = exact_value_i64(str.len);
  3950. type = t_untyped_integer;
  3951. } else {
  3952. mode = Addressing_Value;
  3953. }
  3954. } else if (is_type_array(op_type)) {
  3955. Type *at = core_type(op_type);
  3956. mode = Addressing_Constant;
  3957. value = exact_value_i64(at->Array.count);
  3958. type = t_untyped_integer;
  3959. } else if (is_type_vector(op_type) && id == BuiltinProc_len) {
  3960. Type *at = core_type(op_type);
  3961. mode = Addressing_Constant;
  3962. value = exact_value_i64(at->Vector.count);
  3963. type = t_untyped_integer;
  3964. } else if (is_type_slice(op_type)) {
  3965. mode = Addressing_Value;
  3966. } else if (is_type_dynamic_array(op_type)) {
  3967. mode = Addressing_Value;
  3968. } else if (is_type_map(op_type)) {
  3969. mode = Addressing_Value;
  3970. }
  3971. if (mode == Addressing_Invalid) {
  3972. String name = builtin_procs[id].name;
  3973. gbString t = type_to_string(operand->type);
  3974. error(call, "`%.*s` is not supported for `%s`", LIT(name), t);
  3975. return false;
  3976. }
  3977. operand->mode = mode;
  3978. operand->value = value;
  3979. operand->type = type;
  3980. } break;
  3981. #if 0
  3982. case BuiltinProc_new: {
  3983. // proc new(Type) -> ^Type
  3984. Operand op = {};
  3985. check_expr_or_type(c, &op, ce->args[0]);
  3986. Type *type = op.type;
  3987. if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
  3988. error(ce->args[0], "Expected a type for `new`");
  3989. return false;
  3990. }
  3991. operand->mode = Addressing_Value;
  3992. operand->type = make_type_pointer(c->allocator, type);
  3993. } break;
  3994. #endif
  3995. #if 0
  3996. case BuiltinProc_new_slice: {
  3997. // proc new_slice(Type, len: int) -> []Type
  3998. // proc new_slice(Type, len, cap: int) -> []Type
  3999. Operand op = {};
  4000. check_expr_or_type(c, &op, ce->args[0]);
  4001. Type *type = op.type;
  4002. if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
  4003. error(ce->args[0], "Expected a type for `new_slice`");
  4004. return false;
  4005. }
  4006. isize arg_count = ce->args.count;
  4007. if (arg_count < 2 || 3 < arg_count) {
  4008. error(ce->args[0], "`new_slice` expects 2 or 3 arguments, found %td", arg_count);
  4009. // NOTE(bill): Return the correct type to reduce errors
  4010. } else {
  4011. // If any are constant
  4012. i64 sizes[2] = {};
  4013. isize size_count = 0;
  4014. for (isize i = 1; i < arg_count; i++) {
  4015. i64 val = 0;
  4016. bool ok = check_index_value(c, ce->args[i], -1, &val);
  4017. if (ok && val >= 0) {
  4018. GB_ASSERT(size_count < gb_count_of(sizes));
  4019. sizes[size_count++] = val;
  4020. }
  4021. }
  4022. if (size_count == 2 && sizes[0] > sizes[1]) {
  4023. error(ce->args[1], "`new_slice` count and capacity are swapped");
  4024. // No need quit
  4025. }
  4026. }
  4027. operand->mode = Addressing_Value;
  4028. operand->type = make_type_slice(c->allocator, type);
  4029. } break;
  4030. #endif
  4031. case BuiltinProc_make: {
  4032. // proc make(Type, len: int) -> Type
  4033. // proc make(Type, len, cap: int) -> Type
  4034. Operand op = {};
  4035. check_expr_or_type(c, &op, ce->args[0]);
  4036. Type *type = op.type;
  4037. if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
  4038. error(ce->args[0], "Expected a type for `make`");
  4039. return false;
  4040. }
  4041. isize min_args = 0;
  4042. isize max_args = 1;
  4043. if (is_type_slice(type)) {
  4044. min_args = 2;
  4045. max_args = 3;
  4046. } else if (is_type_dynamic_map(type)) {
  4047. min_args = 1;
  4048. max_args = 2;
  4049. } else if (is_type_dynamic_array(type)) {
  4050. min_args = 1;
  4051. max_args = 3;
  4052. } else {
  4053. gbString str = type_to_string(type);
  4054. error(call, "Cannot `make` %s; type must be a slice, map, or dynamic array", str);
  4055. gb_string_free(str);
  4056. return false;
  4057. }
  4058. isize arg_count = ce->args.count;
  4059. if (arg_count < min_args || max_args < arg_count) {
  4060. error(ce->args[0], "`make` expects %td or %d argument, found %td", min_args, max_args, arg_count);
  4061. return false;
  4062. }
  4063. // If any are constant
  4064. i64 sizes[4] = {};
  4065. isize size_count = 0;
  4066. for (isize i = 1; i < arg_count; i++) {
  4067. i64 val = 0;
  4068. bool ok = check_index_value(c, false, ce->args[i], -1, &val);
  4069. if (ok && val >= 0) {
  4070. GB_ASSERT(size_count < gb_count_of(sizes));
  4071. sizes[size_count++] = val;
  4072. }
  4073. }
  4074. if (size_count == 2 && sizes[0] > sizes[1]) {
  4075. error(ce->args[1], "`make` count and capacity are swapped");
  4076. // No need quit
  4077. }
  4078. operand->mode = Addressing_Value;
  4079. operand->type = type;
  4080. } break;
  4081. #if 0
  4082. case BuiltinProc_free: {
  4083. // proc free(^Type)
  4084. // proc free([]Type)
  4085. // proc free(string)
  4086. // proc free(map[K]T)
  4087. Type *type = operand->type;
  4088. bool ok = false;
  4089. if (is_type_pointer(type)) {
  4090. ok = true;
  4091. } else if (is_type_slice(type)) {
  4092. ok = true;
  4093. } else if (is_type_string(type)) {
  4094. ok = true;
  4095. } else if (is_type_dynamic_array(type)) {
  4096. ok = true;
  4097. } else if (is_type_dynamic_map(type)) {
  4098. ok = true;
  4099. }
  4100. if (!ok) {
  4101. gbString type_str = type_to_string(type);
  4102. error(operand->expr, "Invalid type for `free`, got `%s`", type_str);
  4103. gb_string_free(type_str);
  4104. return false;
  4105. }
  4106. operand->mode = Addressing_NoValue;
  4107. } break;
  4108. #endif
  4109. #if 0
  4110. case BuiltinProc_reserve: {
  4111. // proc reserve([dynamic]Type, count: int) {
  4112. // proc reserve(map[Key]Type, count: int) {
  4113. Type *type = operand->type;
  4114. if (!is_type_dynamic_array(type) && !is_type_dynamic_map(type)) {
  4115. gbString str = type_to_string(type);
  4116. error(operand->expr, "Expected a dynamic array or dynamic map, got `%s`", str);
  4117. gb_string_free(str);
  4118. return false;
  4119. }
  4120. AstNode *capacity = ce->args[1];
  4121. Operand op = {};
  4122. check_expr(c, &op, capacity);
  4123. if (op.mode == Addressing_Invalid) {
  4124. return false;
  4125. }
  4126. Type *arg_type = base_type(op.type);
  4127. if (!is_type_integer(arg_type)) {
  4128. error(operand->expr, "`reserve` capacities must be an integer");
  4129. return false;
  4130. }
  4131. operand->type = nullptr;
  4132. operand->mode = Addressing_NoValue;
  4133. } break;
  4134. #endif
  4135. #if 0
  4136. case BuiltinProc_clear: {
  4137. Type *type = operand->type;
  4138. bool is_pointer = is_type_pointer(type);
  4139. type = base_type(type_deref(type));
  4140. if (!is_type_dynamic_array(type) && !is_type_map(type) && !is_type_slice(type)) {
  4141. gbString str = type_to_string(type);
  4142. error(operand->expr, "Invalid type for `clear`, got `%s`", str);
  4143. gb_string_free(str);
  4144. return false;
  4145. }
  4146. operand->type = nullptr;
  4147. operand->mode = Addressing_NoValue;
  4148. } break;
  4149. #endif
  4150. #if 0
  4151. case BuiltinProc_append: {
  4152. // proc append([dynamic]Type, item: ..Type)
  4153. // proc append([]Type, item: ..Type)
  4154. Operand prev_operand = *operand;
  4155. Type *type = operand->type;
  4156. bool is_pointer = is_type_pointer(type);
  4157. type = base_type(type_deref(type));
  4158. if (!is_type_dynamic_array(type) && !is_type_slice(type)) {
  4159. gbString str = type_to_string(type);
  4160. error(operand->expr, "Expected a slice or dynamic array, got `%s`", str);
  4161. gb_string_free(str);
  4162. return false;
  4163. }
  4164. bool is_addressable = operand->mode == Addressing_Variable;
  4165. if (is_pointer) {
  4166. is_addressable = true;
  4167. }
  4168. if (!is_addressable) {
  4169. error(operand->expr, "`append` can only operate on addressable values");
  4170. return false;
  4171. }
  4172. Type *elem = nullptr;
  4173. if (is_type_dynamic_array(type)) {
  4174. elem = type->DynamicArray.elem;
  4175. } else {
  4176. elem = type->Slice.elem;
  4177. }
  4178. Type *slice_elem = make_type_slice(c->allocator, elem);
  4179. Type *proc_type_params = make_type_tuple(c->allocator);
  4180. proc_type_params->Tuple.variables = gb_alloc_array(c->allocator, Entity *, 2);
  4181. proc_type_params->Tuple.variable_count = 2;
  4182. proc_type_params->Tuple.variables[0] = make_entity_param(c->allocator, nullptr, blank_token, operand->type, false, false);
  4183. proc_type_params->Tuple.variables[1] = make_entity_param(c->allocator, nullptr, blank_token, slice_elem, false, false);
  4184. Type *proc_type = make_type_proc(c->allocator, nullptr, proc_type_params, 2, nullptr, false, true, ProcCC_Odin);
  4185. check_call_arguments(c, &prev_operand, proc_type, call);
  4186. if (prev_operand.mode == Addressing_Invalid) {
  4187. return false;
  4188. }
  4189. operand->mode = Addressing_Value;
  4190. operand->type = t_int;
  4191. } break;
  4192. #endif
  4193. #if 0
  4194. case BuiltinProc_delete: {
  4195. // proc delete(map[Key]Value, key: Key)
  4196. Type *type = operand->type;
  4197. if (!is_type_map(type)) {
  4198. gbString str = type_to_string(type);
  4199. error(operand->expr, "Expected a map, got `%s`", str);
  4200. gb_string_free(str);
  4201. return false;
  4202. }
  4203. Type *key = base_type(type)->Map.key;
  4204. Operand x = {Addressing_Invalid};
  4205. AstNode *key_node = ce->args[1];
  4206. Operand op = {};
  4207. check_expr(c, &op, key_node);
  4208. if (op.mode == Addressing_Invalid) {
  4209. return false;
  4210. }
  4211. if (!check_is_assignable_to(c, &op, key)) {
  4212. gbString kt = type_to_string(key);
  4213. gbString ot = type_to_string(op.type);
  4214. error(operand->expr, "Expected a key of type `%s`, got `%s`", key, ot);
  4215. gb_string_free(ot);
  4216. gb_string_free(kt);
  4217. return false;
  4218. }
  4219. operand->mode = Addressing_NoValue;
  4220. } break;
  4221. #endif
  4222. case BuiltinProc_size_of: {
  4223. // proc size_of(Type or expr) -> untyped int
  4224. Operand o = {};
  4225. check_expr_or_type(c, &o, ce->args[0]);
  4226. if (o.mode == Addressing_Invalid) {
  4227. return false;
  4228. }
  4229. Type *t = o.type;
  4230. if (t == nullptr || t == t_invalid) {
  4231. error(ce->args[0], "Invalid argument for `size_of`");
  4232. return false;
  4233. }
  4234. t = default_type(t);
  4235. operand->mode = Addressing_Constant;
  4236. operand->value = exact_value_i64(type_size_of(c->allocator, t));
  4237. operand->type = t_untyped_integer;
  4238. } break;
  4239. case BuiltinProc_align_of: {
  4240. // proc align_of(Type or expr) -> untyped int
  4241. Operand o = {};
  4242. check_expr_or_type(c, &o, ce->args[0]);
  4243. if (o.mode == Addressing_Invalid) {
  4244. return false;
  4245. }
  4246. Type *t = o.type;
  4247. if (t == nullptr || t == t_invalid) {
  4248. error(ce->args[0], "Invalid argument for `align_of`");
  4249. return false;
  4250. }
  4251. t = default_type(t);
  4252. operand->mode = Addressing_Constant;
  4253. operand->value = exact_value_i64(type_align_of(c->allocator, t));
  4254. operand->type = t_untyped_integer;
  4255. } break;
  4256. case BuiltinProc_offset_of: {
  4257. // proc offset_of(Type, field) -> untyped int
  4258. Operand op = {};
  4259. Type *bt = check_type(c, ce->args[0]);
  4260. Type *type = base_type(bt);
  4261. if (type == nullptr || type == t_invalid) {
  4262. error(ce->args[0], "Expected a type for `offset_of`");
  4263. return false;
  4264. }
  4265. AstNode *field_arg = unparen_expr(ce->args[1]);
  4266. if (field_arg == nullptr ||
  4267. field_arg->kind != AstNode_Ident) {
  4268. error(field_arg, "Expected an identifier for field argument");
  4269. return false;
  4270. }
  4271. if (is_type_array(type) || is_type_vector(type)) {
  4272. error(field_arg, "Invalid type for `offset_of`");
  4273. return false;
  4274. }
  4275. ast_node(arg, Ident, field_arg);
  4276. Selection sel = lookup_field(c->allocator, type, arg->token.string, operand->mode == Addressing_Type);
  4277. if (sel.entity == nullptr) {
  4278. gbString type_str = type_to_string(bt);
  4279. error(ce->args[0],
  4280. "`%s` has no field named `%.*s`", type_str, LIT(arg->token.string));
  4281. gb_string_free(type_str);
  4282. return false;
  4283. }
  4284. if (sel.indirect) {
  4285. gbString type_str = type_to_string(bt);
  4286. error(ce->args[0],
  4287. "Field `%.*s` is embedded via a pointer in `%s`", LIT(arg->token.string), type_str);
  4288. gb_string_free(type_str);
  4289. return false;
  4290. }
  4291. operand->mode = Addressing_Constant;
  4292. operand->value = exact_value_i64(type_offset_of_from_selection(c->allocator, type, sel));
  4293. operand->type = t_untyped_integer;
  4294. } break;
  4295. case BuiltinProc_type_of:
  4296. // proc type_of(val: Type) -> type(Type)
  4297. check_assignment(c, operand, nullptr, str_lit("argument of `type_of`"));
  4298. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin) {
  4299. return false;
  4300. }
  4301. if (operand->type == nullptr || operand->type == t_invalid) {
  4302. error(operand->expr, "Invalid argument to `type_of`");
  4303. return false;
  4304. }
  4305. if (is_type_polymorphic(operand->type)) {
  4306. error(operand->expr, "`type_of` of polymorphic type cannot be determined");
  4307. return false;
  4308. }
  4309. operand->mode = Addressing_Type;
  4310. break;
  4311. case BuiltinProc_type_info: {
  4312. // proc type_info(Type) -> ^Type_Info
  4313. if (c->context.scope->is_global) {
  4314. compiler_error("`type_info` Cannot be declared within a #shared_global_scope due to how the internals of the compiler works");
  4315. }
  4316. // NOTE(bill): The type information may not be setup yet
  4317. init_preload(c);
  4318. AstNode *expr = ce->args[0];
  4319. Operand o = {};
  4320. check_expr_or_type(c, &o, ce->args[0]);
  4321. if (o.mode == Addressing_Invalid) {
  4322. return false;
  4323. }
  4324. Type *t = o.type;
  4325. if (t == nullptr || t == t_invalid || is_type_polymorphic(operand->type)) {
  4326. error(ce->args[0], "Invalid argument for `type_info`");
  4327. return false;
  4328. }
  4329. t = default_type(t);
  4330. add_type_info_type(c, t);
  4331. operand->mode = Addressing_Value;
  4332. operand->type = t_type_info_ptr;
  4333. } break;
  4334. case BuiltinProc_compile_assert:
  4335. // proc compile_assert(cond: bool) -> bool
  4336. if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
  4337. gbString str = expr_to_string(ce->args[0]);
  4338. error(call, "`%s` is not a constant boolean", str);
  4339. gb_string_free(str);
  4340. return false;
  4341. }
  4342. if (!operand->value.value_bool) {
  4343. gbString str = expr_to_string(ce->args[0]);
  4344. error(call, "Compile time assertion: `%s`", str);
  4345. gb_string_free(str);
  4346. }
  4347. operand->mode = Addressing_Constant;
  4348. operand->type = t_untyped_bool;
  4349. break;
  4350. case BuiltinProc_swizzle: {
  4351. // proc swizzle(v: {N}T, T..) -> {M}T
  4352. Type *vector_type = base_type(operand->type);
  4353. if (!is_type_vector(vector_type)) {
  4354. gbString type_str = type_to_string(operand->type);
  4355. error(call,
  4356. "You can only `swizzle` a vector, got `%s`",
  4357. type_str);
  4358. gb_string_free(type_str);
  4359. return false;
  4360. }
  4361. isize max_count = vector_type->Vector.count;
  4362. i128 max_count128 = i128_from_i64(max_count);
  4363. isize arg_count = 0;
  4364. for_array(i, ce->args) {
  4365. if (i == 0) {
  4366. continue;
  4367. }
  4368. AstNode *arg = ce->args[i];
  4369. Operand op = {};
  4370. check_expr(c, &op, arg);
  4371. if (op.mode == Addressing_Invalid) {
  4372. return false;
  4373. }
  4374. Type *arg_type = base_type(op.type);
  4375. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  4376. error(op.expr, "Indices to `swizzle` must be constant integers");
  4377. return false;
  4378. }
  4379. if (i128_lt(op.value.value_integer, I128_ZERO)) {
  4380. error(op.expr, "Negative `swizzle` index");
  4381. return false;
  4382. }
  4383. if (i128_le(max_count128, op.value.value_integer)) {
  4384. error(op.expr, "`swizzle` index exceeds vector length");
  4385. return false;
  4386. }
  4387. arg_count++;
  4388. }
  4389. if (arg_count > max_count) {
  4390. error(call, "Too many `swizzle` indices, %td > %td", arg_count, max_count);
  4391. return false;
  4392. }
  4393. Type *elem_type = vector_type->Vector.elem;
  4394. operand->type = make_type_vector(c->allocator, elem_type, arg_count);
  4395. operand->mode = Addressing_Value;
  4396. } break;
  4397. case BuiltinProc_complex: {
  4398. // proc complex(real, imag: float_type) -> complex_type
  4399. Operand x = *operand;
  4400. Operand y = {};
  4401. // NOTE(bill): Invalid will be the default till fixed
  4402. operand->type = t_invalid;
  4403. operand->mode = Addressing_Invalid;
  4404. check_expr(c, &y, ce->args[1]);
  4405. if (y.mode == Addressing_Invalid) {
  4406. return false;
  4407. }
  4408. convert_to_typed(c, &x, y.type, 0); if (x.mode == Addressing_Invalid) return false;
  4409. convert_to_typed(c, &y, x.type, 0); if (y.mode == Addressing_Invalid) return false;
  4410. if (x.mode == Addressing_Constant &&
  4411. y.mode == Addressing_Constant) {
  4412. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  4413. x.type = t_untyped_float;
  4414. }
  4415. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  4416. y.type = t_untyped_float;
  4417. }
  4418. }
  4419. if (!are_types_identical(x.type, y.type)) {
  4420. gbString tx = type_to_string(x.type);
  4421. gbString ty = type_to_string(y.type);
  4422. error(call, "Mismatched types to `complex`, `%s` vs `%s`", tx, ty);
  4423. gb_string_free(ty);
  4424. gb_string_free(tx);
  4425. return false;
  4426. }
  4427. if (!is_type_float(x.type)) {
  4428. gbString s = type_to_string(x.type);
  4429. error(call, "Arguments have type `%s`, expected a floating point", s);
  4430. gb_string_free(s);
  4431. return false;
  4432. }
  4433. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  4434. operand->value = exact_binary_operator_value(Token_Add, x.value, y.value);
  4435. operand->mode = Addressing_Constant;
  4436. } else {
  4437. operand->mode = Addressing_Value;
  4438. }
  4439. BasicKind kind = core_type(x.type)->Basic.kind;
  4440. switch (kind) {
  4441. // case Basic_f16: operand->type = t_complex32; break;
  4442. case Basic_f32: operand->type = t_complex64; break;
  4443. case Basic_f64: operand->type = t_complex128; break;
  4444. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  4445. default: GB_PANIC("Invalid type"); break;
  4446. }
  4447. } break;
  4448. case BuiltinProc_real:
  4449. case BuiltinProc_imag: {
  4450. // proc real(x: type) -> float_type
  4451. // proc imag(x: type) -> float_type
  4452. Operand *x = operand;
  4453. if (is_type_untyped(x->type)) {
  4454. if (x->mode == Addressing_Constant) {
  4455. if (is_type_numeric(x->type)) {
  4456. x->type = t_untyped_complex;
  4457. }
  4458. } else {
  4459. convert_to_typed(c, x, t_complex128, 0);
  4460. if (x->mode == Addressing_Invalid) {
  4461. return false;
  4462. }
  4463. }
  4464. }
  4465. if (!is_type_complex(x->type)) {
  4466. gbString s = type_to_string(x->type);
  4467. error(call, "Argument has type `%s`, expected a complex type", s);
  4468. gb_string_free(s);
  4469. return false;
  4470. }
  4471. if (x->mode == Addressing_Constant) {
  4472. switch (id) {
  4473. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  4474. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  4475. }
  4476. } else {
  4477. x->mode = Addressing_Value;
  4478. }
  4479. BasicKind kind = core_type(x->type)->Basic.kind;
  4480. switch (kind) {
  4481. case Basic_complex64: x->type = t_f32; break;
  4482. case Basic_complex128: x->type = t_f64; break;
  4483. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  4484. default: GB_PANIC("Invalid type"); break;
  4485. }
  4486. } break;
  4487. case BuiltinProc_conj: {
  4488. // proc conj(x: type) -> type
  4489. Operand *x = operand;
  4490. if (is_type_complex(x->type)) {
  4491. if (x->mode == Addressing_Constant) {
  4492. ExactValue v = exact_value_to_complex(x->value);
  4493. f64 r = v.value_complex.real;
  4494. f64 i = v.value_complex.imag;
  4495. x->value = exact_value_complex(r, i);
  4496. x->mode = Addressing_Constant;
  4497. } else {
  4498. x->mode = Addressing_Value;
  4499. }
  4500. } else {
  4501. gbString s = type_to_string(x->type);
  4502. error(call, "Expected a complex or quaternion, got `%s`", s);
  4503. gb_string_free(s);
  4504. return false;
  4505. }
  4506. } break;
  4507. #if 0
  4508. case BuiltinProc_slice_ptr: {
  4509. // proc slice_ptr(a: ^T, len: int) -> []T
  4510. // proc slice_ptr(a: ^T, len, cap: int) -> []T
  4511. // ^T cannot be rawptr
  4512. Type *ptr_type = base_type(operand->type);
  4513. if (!is_type_pointer(ptr_type)) {
  4514. gbString type_str = type_to_string(operand->type);
  4515. error(call, "Expected a pointer to `slice_ptr`, got `%s`", type_str);
  4516. gb_string_free(type_str);
  4517. return false;
  4518. }
  4519. if (ptr_type == t_rawptr) {
  4520. error(call, "`rawptr` cannot have pointer arithmetic");
  4521. return false;
  4522. }
  4523. isize arg_count = ce->args.count;
  4524. if (arg_count < 2 || 3 < arg_count) {
  4525. error(ce->args[0], "`slice_ptr` expects 2 or 3 arguments, found %td", arg_count);
  4526. // NOTE(bill): Return the correct type to reduce errors
  4527. } else {
  4528. // If any are constant
  4529. i64 sizes[2] = {};
  4530. isize size_count = 0;
  4531. for (isize i = 1; i < arg_count; i++) {
  4532. i64 val = 0;
  4533. bool ok = check_index_value(c, false, ce->args[i], -1, &val);
  4534. if (ok && val >= 0) {
  4535. GB_ASSERT(size_count < gb_count_of(sizes));
  4536. sizes[size_count++] = val;
  4537. }
  4538. }
  4539. if (size_count == 2 && sizes[0] > sizes[1]) {
  4540. error(ce->args[1], "`slice_ptr` count and capacity are swapped");
  4541. // No need quit
  4542. }
  4543. }
  4544. operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
  4545. operand->mode = Addressing_Value;
  4546. } break;
  4547. case BuiltinProc_slice_to_bytes: {
  4548. // proc slice_to_bytes(a: []T) -> []u8
  4549. Type *slice_type = base_type(operand->type);
  4550. if (!is_type_slice(slice_type)) {
  4551. gbString type_str = type_to_string(operand->type);
  4552. error(call, "Expected a slice type, got `%s`", type_str);
  4553. gb_string_free(type_str);
  4554. return false;
  4555. }
  4556. operand->type = t_u8_slice;
  4557. operand->mode = Addressing_Value;
  4558. } break;
  4559. #endif
  4560. case BuiltinProc_expand_to_tuple: {
  4561. Type *type = base_type(operand->type);
  4562. if (!is_type_struct(type) &
  4563. !is_type_union(type)) {
  4564. gbString type_str = type_to_string(operand->type);
  4565. error(call, "Expected a struct or union type, got `%s`", type_str);
  4566. gb_string_free(type_str);
  4567. return false;
  4568. }
  4569. gbAllocator a = c->allocator;
  4570. Type *tuple = make_type_tuple(a);
  4571. i32 variable_count = type->Record.field_count;
  4572. tuple->Tuple.variables = gb_alloc_array(a, Entity *, variable_count);
  4573. tuple->Tuple.variable_count = variable_count;
  4574. // TODO(bill): Should I copy each of the entities or is this good enough?
  4575. gb_memcopy_array(tuple->Tuple.variables, type->Record.fields_in_src_order, variable_count);
  4576. operand->type = tuple;
  4577. operand->mode = Addressing_Value;
  4578. } break;
  4579. case BuiltinProc_min: {
  4580. // proc min(a, b: ordered) -> ordered
  4581. Type *type = base_type(operand->type);
  4582. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4583. gbString type_str = type_to_string(operand->type);
  4584. error(call, "Expected a ordered numeric type to `min`, got `%s`", type_str);
  4585. gb_string_free(type_str);
  4586. return false;
  4587. }
  4588. AstNode *other_arg = ce->args[1];
  4589. Operand a = *operand;
  4590. Operand b = {};
  4591. check_expr(c, &b, other_arg);
  4592. if (b.mode == Addressing_Invalid) {
  4593. return false;
  4594. }
  4595. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4596. gbString type_str = type_to_string(b.type);
  4597. error(call,
  4598. "Expected a ordered numeric type to `min`, got `%s`",
  4599. type_str);
  4600. gb_string_free(type_str);
  4601. return false;
  4602. }
  4603. if (a.mode == Addressing_Constant &&
  4604. b.mode == Addressing_Constant) {
  4605. ExactValue x = a.value;
  4606. ExactValue y = b.value;
  4607. operand->mode = Addressing_Constant;
  4608. if (compare_exact_values(Token_Lt, x, y)) {
  4609. operand->value = x;
  4610. operand->type = a.type;
  4611. } else {
  4612. operand->value = y;
  4613. operand->type = b.type;
  4614. }
  4615. } else {
  4616. operand->mode = Addressing_Value;
  4617. operand->type = type;
  4618. convert_to_typed(c, &a, b.type, 0);
  4619. if (a.mode == Addressing_Invalid) {
  4620. return false;
  4621. }
  4622. convert_to_typed(c, &b, a.type, 0);
  4623. if (b.mode == Addressing_Invalid) {
  4624. return false;
  4625. }
  4626. if (!are_types_identical(a.type, b.type)) {
  4627. gbString type_a = type_to_string(a.type);
  4628. gbString type_b = type_to_string(b.type);
  4629. error(call,
  4630. "Mismatched types to `min`, `%s` vs `%s`",
  4631. type_a, type_b);
  4632. gb_string_free(type_b);
  4633. gb_string_free(type_a);
  4634. return false;
  4635. }
  4636. }
  4637. } break;
  4638. case BuiltinProc_max: {
  4639. // proc min(a, b: ordered) -> ordered
  4640. Type *type = base_type(operand->type);
  4641. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4642. gbString type_str = type_to_string(operand->type);
  4643. error(call,
  4644. "Expected a ordered numeric or string type to `max`, got `%s`",
  4645. type_str);
  4646. gb_string_free(type_str);
  4647. return false;
  4648. }
  4649. AstNode *other_arg = ce->args[1];
  4650. Operand a = *operand;
  4651. Operand b = {};
  4652. check_expr(c, &b, other_arg);
  4653. if (b.mode == Addressing_Invalid) {
  4654. return false;
  4655. }
  4656. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4657. gbString type_str = type_to_string(b.type);
  4658. error(call,
  4659. "Expected a ordered numeric or string type to `max`, got `%s`",
  4660. type_str);
  4661. gb_string_free(type_str);
  4662. return false;
  4663. }
  4664. if (a.mode == Addressing_Constant &&
  4665. b.mode == Addressing_Constant) {
  4666. ExactValue x = a.value;
  4667. ExactValue y = b.value;
  4668. operand->mode = Addressing_Constant;
  4669. if (compare_exact_values(Token_Gt, x, y)) {
  4670. operand->value = x;
  4671. operand->type = a.type;
  4672. } else {
  4673. operand->value = y;
  4674. operand->type = b.type;
  4675. }
  4676. } else {
  4677. operand->mode = Addressing_Value;
  4678. operand->type = type;
  4679. convert_to_typed(c, &a, b.type, 0);
  4680. if (a.mode == Addressing_Invalid) {
  4681. return false;
  4682. }
  4683. convert_to_typed(c, &b, a.type, 0);
  4684. if (b.mode == Addressing_Invalid) {
  4685. return false;
  4686. }
  4687. if (!are_types_identical(a.type, b.type)) {
  4688. gbString type_a = type_to_string(a.type);
  4689. gbString type_b = type_to_string(b.type);
  4690. error(call,
  4691. "Mismatched types to `max`, `%s` vs `%s`",
  4692. type_a, type_b);
  4693. gb_string_free(type_b);
  4694. gb_string_free(type_a);
  4695. return false;
  4696. }
  4697. }
  4698. } break;
  4699. case BuiltinProc_abs: {
  4700. // proc abs(n: numeric) -> numeric
  4701. if (!is_type_numeric(operand->type) && !is_type_vector(operand->type)) {
  4702. gbString type_str = type_to_string(operand->type);
  4703. error(call, "Expected a numeric type to `abs`, got `%s`", type_str);
  4704. gb_string_free(type_str);
  4705. return false;
  4706. }
  4707. if (operand->mode == Addressing_Constant) {
  4708. switch (operand->value.kind) {
  4709. case ExactValue_Integer:
  4710. operand->value.value_integer = i128_abs(operand->value.value_integer);
  4711. break;
  4712. case ExactValue_Float:
  4713. operand->value.value_float = gb_abs(operand->value.value_float);
  4714. break;
  4715. case ExactValue_Complex: {
  4716. f64 r = operand->value.value_complex.real;
  4717. f64 i = operand->value.value_complex.imag;
  4718. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  4719. } break;
  4720. default:
  4721. GB_PANIC("Invalid numeric constant");
  4722. break;
  4723. }
  4724. } else {
  4725. operand->mode = Addressing_Value;
  4726. }
  4727. if (is_type_complex(operand->type)) {
  4728. operand->type = base_complex_elem_type(operand->type);
  4729. }
  4730. GB_ASSERT(!is_type_complex(operand->type));
  4731. } break;
  4732. case BuiltinProc_clamp: {
  4733. // proc clamp(a, min, max: ordered) -> ordered
  4734. Type *type = base_type(operand->type);
  4735. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4736. gbString type_str = type_to_string(operand->type);
  4737. error(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  4738. gb_string_free(type_str);
  4739. return false;
  4740. }
  4741. AstNode *min_arg = ce->args[1];
  4742. AstNode *max_arg = ce->args[2];
  4743. Operand x = *operand;
  4744. Operand y = {};
  4745. Operand z = {};
  4746. check_expr(c, &y, min_arg);
  4747. if (y.mode == Addressing_Invalid) {
  4748. return false;
  4749. }
  4750. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  4751. gbString type_str = type_to_string(y.type);
  4752. error(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  4753. gb_string_free(type_str);
  4754. return false;
  4755. }
  4756. check_expr(c, &z, max_arg);
  4757. if (z.mode == Addressing_Invalid) {
  4758. return false;
  4759. }
  4760. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  4761. gbString type_str = type_to_string(z.type);
  4762. error(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  4763. gb_string_free(type_str);
  4764. return false;
  4765. }
  4766. if (x.mode == Addressing_Constant &&
  4767. y.mode == Addressing_Constant &&
  4768. z.mode == Addressing_Constant) {
  4769. ExactValue a = x.value;
  4770. ExactValue b = y.value;
  4771. ExactValue c = z.value;
  4772. operand->mode = Addressing_Constant;
  4773. if (compare_exact_values(Token_Lt, a, b)) {
  4774. operand->value = b;
  4775. operand->type = y.type;
  4776. } else if (compare_exact_values(Token_Gt, a, c)) {
  4777. operand->value = c;
  4778. operand->type = z.type;
  4779. } else {
  4780. operand->value = a;
  4781. operand->type = x.type;
  4782. }
  4783. } else {
  4784. operand->mode = Addressing_Value;
  4785. operand->type = type;
  4786. convert_to_typed(c, &x, y.type, 0);
  4787. if (x.mode == Addressing_Invalid) { return false; }
  4788. convert_to_typed(c, &y, x.type, 0);
  4789. if (y.mode == Addressing_Invalid) { return false; }
  4790. convert_to_typed(c, &x, z.type, 0);
  4791. if (x.mode == Addressing_Invalid) { return false; }
  4792. convert_to_typed(c, &z, x.type, 0);
  4793. if (z.mode == Addressing_Invalid) { return false; }
  4794. convert_to_typed(c, &y, z.type, 0);
  4795. if (y.mode == Addressing_Invalid) { return false; }
  4796. convert_to_typed(c, &z, y.type, 0);
  4797. if (z.mode == Addressing_Invalid) { return false; }
  4798. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  4799. gbString type_x = type_to_string(x.type);
  4800. gbString type_y = type_to_string(y.type);
  4801. gbString type_z = type_to_string(z.type);
  4802. error(call,
  4803. "Mismatched types to `clamp`, `%s`, `%s`, `%s`",
  4804. type_x, type_y, type_z);
  4805. gb_string_free(type_z);
  4806. gb_string_free(type_y);
  4807. gb_string_free(type_x);
  4808. return false;
  4809. }
  4810. }
  4811. } break;
  4812. case BuiltinProc_transmute: {
  4813. Operand op = {};
  4814. check_expr_or_type(c, &op, ce->args[0]);
  4815. Type *t = op.type;
  4816. if ((op.mode != Addressing_Type && t == nullptr) || t == t_invalid) {
  4817. error(ce->args[0], "Expected a type for `transmute`");
  4818. return false;
  4819. }
  4820. AstNode *expr = ce->args[1];
  4821. Operand *o = operand;
  4822. check_expr(c, o, expr);
  4823. if (o->mode == Addressing_Invalid) {
  4824. return false;
  4825. }
  4826. if (o->mode == Addressing_Constant) {
  4827. gbString expr_str = expr_to_string(o->expr);
  4828. error(o->expr, "Cannot transmute a constant expression: `%s`", expr_str);
  4829. gb_string_free(expr_str);
  4830. o->mode = Addressing_Invalid;
  4831. o->expr = expr;
  4832. return false;
  4833. }
  4834. if (is_type_untyped(o->type)) {
  4835. gbString expr_str = expr_to_string(o->expr);
  4836. error(o->expr, "Cannot transmute untyped expression: `%s`", expr_str);
  4837. gb_string_free(expr_str);
  4838. o->mode = Addressing_Invalid;
  4839. o->expr = expr;
  4840. return false;
  4841. }
  4842. i64 srcz = type_size_of(c->allocator, o->type);
  4843. i64 dstz = type_size_of(c->allocator, t);
  4844. if (srcz != dstz) {
  4845. gbString expr_str = expr_to_string(o->expr);
  4846. gbString type_str = type_to_string(t);
  4847. error(o->expr, "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  4848. gb_string_free(type_str);
  4849. gb_string_free(expr_str);
  4850. o->mode = Addressing_Invalid;
  4851. o->expr = expr;
  4852. return false;
  4853. }
  4854. o->mode = Addressing_Value;
  4855. o->type = t;
  4856. } break;
  4857. }
  4858. return true;
  4859. }
  4860. struct ValidProcAndScore {
  4861. isize index;
  4862. i64 score;
  4863. };
  4864. int valid_proc_and_score_cmp(void const *a, void const *b) {
  4865. i64 si = (cast(ValidProcAndScore const *)a)->score;
  4866. i64 sj = (cast(ValidProcAndScore const *)b)->score;
  4867. return sj < si ? -1 : sj > si;
  4868. }
  4869. bool check_unpack_arguments(Checker *c, isize lhs_count, Array<Operand> *operands, Array<AstNode *> rhs, bool allow_ok) {
  4870. bool optional_ok = false;
  4871. for_array(i, rhs) {
  4872. Operand o = {};
  4873. check_expr_base(c, &o, rhs[i], nullptr);
  4874. if (o.mode == Addressing_NoValue) {
  4875. error_operand_no_value(&o);
  4876. o.mode = Addressing_Invalid;
  4877. }
  4878. // check_multi_expr(c, &o, rhs[i]);
  4879. if (o.type == nullptr || o.type->kind != Type_Tuple) {
  4880. if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
  4881. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  4882. Type *tuple = make_optional_ok_type(c->allocator, o.type);
  4883. add_type_and_value(&c->info, o.expr, o.mode, tuple, o.value);
  4884. Operand val = o;
  4885. Operand ok = o;
  4886. val.mode = Addressing_Value;
  4887. ok.mode = Addressing_Value;
  4888. ok.type = t_bool;
  4889. array_add(operands, val);
  4890. array_add(operands, ok);
  4891. optional_ok = true;
  4892. } else {
  4893. array_add(operands, o);
  4894. }
  4895. } else {
  4896. TypeTuple *tuple = &o.type->Tuple;
  4897. for (isize j = 0; j < tuple->variable_count; j++) {
  4898. o.type = tuple->variables[j]->type;
  4899. array_add(operands, o);
  4900. }
  4901. }
  4902. }
  4903. return optional_ok;
  4904. }
  4905. CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
  4906. ast_node(ce, CallExpr, call);
  4907. GB_ASSERT(is_type_proc(proc_type));
  4908. proc_type = base_type(proc_type);
  4909. TypeProc *pt = &proc_type->Proc;
  4910. isize param_count = 0;
  4911. isize param_count_excluding_defaults = 0;
  4912. bool variadic = pt->variadic;
  4913. bool vari_expand = (ce->ellipsis.pos.line != 0);
  4914. i64 score = 0;
  4915. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  4916. TypeTuple *param_tuple = nullptr;
  4917. if (pt->params != nullptr) {
  4918. param_tuple = &pt->params->Tuple;
  4919. param_count = param_tuple->variable_count;
  4920. if (variadic) {
  4921. param_count--;
  4922. }
  4923. }
  4924. param_count_excluding_defaults = param_count;
  4925. if (param_tuple != nullptr) {
  4926. for (isize i = param_count-1; i >= 0; i--) {
  4927. Entity *e = param_tuple->variables[i];
  4928. if (e->kind == Entity_TypeName) {
  4929. break;
  4930. }
  4931. GB_ASSERT(e->kind == Entity_Variable);
  4932. if (e->Variable.default_value.kind != ExactValue_Invalid ||
  4933. e->Variable.default_is_nil ||
  4934. e->Variable.default_is_location) {
  4935. param_count_excluding_defaults--;
  4936. continue;
  4937. }
  4938. break;
  4939. }
  4940. }
  4941. CallArgumentError err = CallArgumentError_None;
  4942. Type *final_proc_type = proc_type;
  4943. Entity *gen_entity = nullptr;
  4944. if (vari_expand && !variadic) {
  4945. if (show_error) {
  4946. error(ce->ellipsis,
  4947. "Cannot use `..` in call to a non-variadic procedure: `%.*s`",
  4948. LIT(ce->proc->Ident.token.string));
  4949. }
  4950. err = CallArgumentError_NonVariadicExpand;
  4951. } else if (vari_expand && pt->c_vararg) {
  4952. if (show_error) {
  4953. error(ce->ellipsis,
  4954. "Cannot use `..` in call to a `#c_vararg` variadic procedure: `%.*s`",
  4955. LIT(ce->proc->Ident.token.string));
  4956. }
  4957. err = CallArgumentError_NonVariadicExpand;
  4958. } else if (operands.count == 0 && param_count_excluding_defaults == 0) {
  4959. err = CallArgumentError_None;
  4960. } else {
  4961. i32 error_code = 0;
  4962. if (operands.count < param_count_excluding_defaults) {
  4963. error_code = -1;
  4964. } else if (!variadic && operands.count > param_count) {
  4965. error_code = +1;
  4966. }
  4967. if (error_code != 0) {
  4968. err = CallArgumentError_TooManyArguments;
  4969. char *err_fmt = "Too many arguments for `%s`, expected %td arguments";
  4970. if (error_code < 0) {
  4971. err = CallArgumentError_TooFewArguments;
  4972. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  4973. }
  4974. if (show_error) {
  4975. gbString proc_str = expr_to_string(ce->proc);
  4976. error(call, err_fmt, proc_str, param_count_excluding_defaults);
  4977. gb_string_free(proc_str);
  4978. }
  4979. } else {
  4980. // NOTE(bill): Generate the procedure type for this generic instance
  4981. PolyProcData poly_proc_data = {};
  4982. if (pt->is_polymorphic && !pt->is_poly_specialized) {
  4983. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &operands, &poly_proc_data)) {
  4984. gen_entity = poly_proc_data.gen_entity;
  4985. GB_ASSERT(is_type_proc(gen_entity->type));
  4986. final_proc_type = gen_entity->type;
  4987. }
  4988. }
  4989. GB_ASSERT(is_type_proc(final_proc_type));
  4990. TypeProc *pt = &final_proc_type->Proc;
  4991. GB_ASSERT(pt->params != nullptr);
  4992. Entity **sig_params = pt->params->Tuple.variables;
  4993. isize operand_index = 0;
  4994. isize max_operand_count = gb_min(param_count, operands.count);
  4995. for (; operand_index < max_operand_count; operand_index++) {
  4996. Entity *e = sig_params[operand_index];
  4997. Type *t = e->type;
  4998. Operand o = operands[operand_index];
  4999. if (e->kind == Entity_TypeName) {
  5000. // GB_ASSERT(!variadic);
  5001. if (o.mode == Addressing_Invalid) {
  5002. continue;
  5003. } else if (o.mode != Addressing_Type) {
  5004. if (show_error) {
  5005. error(o.expr, "Expected a type for the argument `%.*s`", LIT(e->token.string));
  5006. }
  5007. err = CallArgumentError_WrongTypes;
  5008. }
  5009. if (are_types_identical(e->type, o.type)) {
  5010. score += assign_score_function(1);
  5011. } else {
  5012. score += assign_score_function(10);
  5013. }
  5014. continue;
  5015. }
  5016. if (variadic) {
  5017. o = operands[operand_index];
  5018. }
  5019. i64 s = 0;
  5020. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  5021. if (show_error) {
  5022. check_assignment(c, &o, t, str_lit("argument"));
  5023. }
  5024. err = CallArgumentError_WrongTypes;
  5025. }
  5026. score += s;
  5027. }
  5028. if (variadic) {
  5029. bool variadic_expand = false;
  5030. Type *slice = sig_params[param_count]->type;
  5031. GB_ASSERT(is_type_slice(slice));
  5032. Type *elem = base_type(slice)->Slice.elem;
  5033. Type *t = elem;
  5034. for (; operand_index < operands.count; operand_index++) {
  5035. Operand o = operands[operand_index];
  5036. if (vari_expand) {
  5037. variadic_expand = true;
  5038. t = slice;
  5039. if (operand_index != param_count) {
  5040. if (show_error) {
  5041. error(o.expr, "`..` in a variadic procedure can only have one variadic argument at the end");
  5042. }
  5043. if (data) {
  5044. data->score = score;
  5045. data->result_type = final_proc_type->Proc.results;
  5046. data->gen_entity = gen_entity;
  5047. }
  5048. return CallArgumentError_MultipleVariadicExpand;
  5049. }
  5050. }
  5051. i64 s = 0;
  5052. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  5053. if (show_error) {
  5054. check_assignment(c, &o, t, str_lit("argument"));
  5055. }
  5056. err = CallArgumentError_WrongTypes;
  5057. }
  5058. score += s;
  5059. }
  5060. }
  5061. if (gen_entity != nullptr && gen_entity->token.string == "append" && err != CallArgumentError_None) {
  5062. gb_printf_err("append %s with score %lld %d\n", type_to_string(final_proc_type), score, err);
  5063. }
  5064. if (err == CallArgumentError_None && poly_proc_data.proc_info.decl != nullptr) {
  5065. // NOTE(bill): Check the newly generated procedure body
  5066. check_procedure_later(c, poly_proc_data.proc_info);
  5067. }
  5068. }
  5069. }
  5070. if (data) {
  5071. data->score = score;
  5072. data->result_type = final_proc_type->Proc.results;
  5073. data->gen_entity = gen_entity;
  5074. }
  5075. return err;
  5076. }
  5077. bool is_call_expr_field_value(AstNodeCallExpr *ce) {
  5078. GB_ASSERT(ce != nullptr);
  5079. if (ce->args.count == 0) {
  5080. return false;
  5081. }
  5082. return ce->args[0]->kind == AstNode_FieldValue;
  5083. }
  5084. isize lookup_procedure_parameter(TypeProc *pt, String parameter_name) {
  5085. isize param_count = pt->param_count;
  5086. for (isize i = 0; i < param_count; i++) {
  5087. Entity *e = pt->params->Tuple.variables[i];
  5088. String name = e->token.string;
  5089. if (is_blank_ident(name)) {
  5090. continue;
  5091. }
  5092. if (name == parameter_name) {
  5093. return i;
  5094. }
  5095. }
  5096. return -1;
  5097. }
  5098. isize lookup_procedure_result(TypeProc *pt, String result_name) {
  5099. isize result_count = pt->result_count;
  5100. for (isize i = 0; i < result_count; i++) {
  5101. Entity *e = pt->results->Tuple.variables[i];
  5102. String name = e->token.string;
  5103. if (is_blank_ident(name)) {
  5104. continue;
  5105. }
  5106. if (name == result_name) {
  5107. return i;
  5108. }
  5109. }
  5110. return -1;
  5111. }
  5112. CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
  5113. ast_node(ce, CallExpr, call);
  5114. GB_ASSERT(is_type_proc(proc_type));
  5115. TypeProc *pt = &base_type(proc_type)->Proc;
  5116. i64 score = 0;
  5117. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  5118. CallArgumentError err = CallArgumentError_None;
  5119. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  5120. defer (gb_temp_arena_memory_end(tmp));
  5121. isize param_count = pt->param_count;
  5122. bool *visited = gb_alloc_array(c->tmp_allocator, bool, param_count);
  5123. Array<Operand> ordered_operands = {};
  5124. array_init_count(&ordered_operands, c->tmp_allocator, param_count);
  5125. for_array(i, ce->args) {
  5126. AstNode *arg = ce->args[i];
  5127. ast_node(fv, FieldValue, arg);
  5128. if (fv->field->kind != AstNode_Ident) {
  5129. if (show_error) {
  5130. gbString expr_str = expr_to_string(fv->field);
  5131. error(arg, "Invalid parameter name `%s` in procedure call", expr_str);
  5132. gb_string_free(expr_str);
  5133. }
  5134. err = CallArgumentError_InvalidFieldValue;
  5135. continue;
  5136. }
  5137. String name = fv->field->Ident.token.string;
  5138. isize index = lookup_procedure_parameter(pt, name);
  5139. if (index < 0) {
  5140. if (show_error) {
  5141. error(arg, "No parameter named `%.*s` for this procedure type", LIT(name));
  5142. }
  5143. err = CallArgumentError_ParameterNotFound;
  5144. continue;
  5145. }
  5146. if (visited[index]) {
  5147. if (show_error) {
  5148. error(arg, "Duplicate parameter `%.*s` in procedure call", LIT(name));
  5149. }
  5150. err = CallArgumentError_DuplicateParameter;
  5151. continue;
  5152. }
  5153. visited[index] = true;
  5154. ordered_operands[index] = operands[i];
  5155. }
  5156. // NOTE(bill): Check for default values and missing parameters
  5157. isize param_count_to_check = param_count;
  5158. if (pt->variadic) {
  5159. param_count_to_check--;
  5160. }
  5161. for (isize i = 0; i < param_count_to_check; i++) {
  5162. if (!visited[i]) {
  5163. Entity *e = pt->params->Tuple.variables[i];
  5164. if (is_blank_ident(e->token)) {
  5165. continue;
  5166. }
  5167. if (e->kind == Entity_Variable) {
  5168. if (e->Variable.default_value.kind != ExactValue_Invalid) {
  5169. score += assign_score_function(1);
  5170. continue;
  5171. } else if (e->Variable.default_is_nil) {
  5172. score += assign_score_function(1);
  5173. continue;
  5174. }
  5175. }
  5176. if (show_error) {
  5177. if (e->kind == Entity_TypeName) {
  5178. error(call, "Type parameter `%.*s` is missing in procedure call",
  5179. LIT(e->token.string));
  5180. } else {
  5181. gbString str = type_to_string(e->type);
  5182. error(call, "Parameter `%.*s` of type `%s` is missing in procedure call",
  5183. LIT(e->token.string), str);
  5184. gb_string_free(str);
  5185. }
  5186. }
  5187. err = CallArgumentError_ParameterMissing;
  5188. }
  5189. }
  5190. Entity *gen_entity = nullptr;
  5191. if (pt->is_polymorphic && !pt->is_poly_specialized && err == CallArgumentError_None) {
  5192. PolyProcData poly_proc_data = {};
  5193. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &ordered_operands, &poly_proc_data)) {
  5194. gen_entity = poly_proc_data.gen_entity;
  5195. if (poly_proc_data.proc_info.decl != nullptr) {
  5196. check_procedure_later(c, poly_proc_data.proc_info);
  5197. }
  5198. Type *gept = base_type(gen_entity->type);
  5199. GB_ASSERT(is_type_proc(gept));
  5200. pt = &gept->Proc;
  5201. }
  5202. }
  5203. for (isize i = 0; i < param_count; i++) {
  5204. Operand *o = &ordered_operands[i];
  5205. if (o->mode == Addressing_Invalid) {
  5206. continue;
  5207. }
  5208. Entity *e = pt->params->Tuple.variables[i];
  5209. if (e->kind == Entity_TypeName) {
  5210. GB_ASSERT(pt->is_polymorphic);
  5211. if (o->mode != Addressing_Type) {
  5212. if (show_error) {
  5213. error(o->expr, "Expected a type for the argument `%.*s`", LIT(e->token.string));
  5214. }
  5215. err = CallArgumentError_WrongTypes;
  5216. }
  5217. if (are_types_identical(e->type, o->type)) {
  5218. score += assign_score_function(1);
  5219. } else {
  5220. score += assign_score_function(10);
  5221. }
  5222. } else {
  5223. i64 s = 0;
  5224. if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
  5225. if (show_error) {
  5226. check_assignment(c, o, e->type, str_lit("procedure argument"));
  5227. }
  5228. err = CallArgumentError_WrongTypes;
  5229. }
  5230. score += s;
  5231. }
  5232. }
  5233. if (data) {
  5234. data->score = score;
  5235. data->result_type = pt->results;
  5236. data->gen_entity = gen_entity;
  5237. }
  5238. return err;
  5239. }
  5240. CallArgumentData check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  5241. ast_node(ce, CallExpr, call);
  5242. CallArgumentCheckerType *call_checker = check_call_arguments_internal;
  5243. Array<Operand> operands = {};
  5244. defer (array_free(&operands));
  5245. Type *result_type = t_invalid;
  5246. if (is_call_expr_field_value(ce)) {
  5247. call_checker = check_named_call_arguments;
  5248. array_init_count(&operands, heap_allocator(), ce->args.count);
  5249. for_array(i, ce->args) {
  5250. AstNode *arg = ce->args[i];
  5251. ast_node(fv, FieldValue, arg);
  5252. check_expr_or_type(c, &operands[i], fv->value);
  5253. }
  5254. bool vari_expand = (ce->ellipsis.pos.line != 0);
  5255. if (vari_expand) {
  5256. // error(ce->ellipsis, "Invalid use of `..` with `field = value` call`");
  5257. }
  5258. } else {
  5259. array_init(&operands, heap_allocator(), 2*ce->args.count);
  5260. check_unpack_arguments(c, -1, &operands, ce->args, false);
  5261. }
  5262. if (operand->mode == Addressing_Overload) {
  5263. GB_ASSERT(operand->overload_entities != nullptr &&
  5264. operand->overload_count > 0);
  5265. isize overload_count = operand->overload_count;
  5266. Entity ** procs = operand->overload_entities;
  5267. ValidProcAndScore *valids = gb_alloc_array(heap_allocator(), ValidProcAndScore, overload_count);
  5268. isize valid_count = 0;
  5269. defer (gb_free(heap_allocator(), procs));
  5270. defer (gb_free(heap_allocator(), valids));
  5271. String name = procs[0]->token.string;
  5272. for (isize i = 0; i < overload_count; i++) {
  5273. Entity *e = procs[i];
  5274. GB_ASSERT(e->token.string == name);
  5275. DeclInfo *d = decl_info_of_entity(&c->info, e);
  5276. GB_ASSERT(d != nullptr);
  5277. check_entity_decl(c, e, d, nullptr);
  5278. }
  5279. for (isize i = 0; i < overload_count; i++) {
  5280. Entity *p = procs[i];
  5281. Type *pt = base_type(p->type);
  5282. if (pt != nullptr && is_type_proc(pt)) {
  5283. CallArgumentError err = CallArgumentError_None;
  5284. CallArgumentData data = {};
  5285. CheckerContext prev_context = c->context;
  5286. defer (c->context = prev_context);
  5287. c->context.no_polymorphic_errors = true;
  5288. c->context.allow_polymorphic_types = is_type_polymorphic(pt);
  5289. err = call_checker(c, call, pt, p, operands, CallArgumentMode_NoErrors, &data);
  5290. if (err == CallArgumentError_None) {
  5291. valids[valid_count].index = i;
  5292. valids[valid_count].score = data.score;
  5293. valid_count++;
  5294. }
  5295. }
  5296. }
  5297. if (valid_count > 1) {
  5298. gb_sort_array(valids, valid_count, valid_proc_and_score_cmp);
  5299. i64 best_score = valids[0].score;
  5300. Entity *best_entity = procs[valids[0].index];
  5301. for (isize i = 0; i < valid_count; i++) {
  5302. if (best_score > valids[i].score) {
  5303. valid_count = i;
  5304. break;
  5305. }
  5306. if (best_entity == procs[valids[i].index]) {
  5307. valid_count = i;
  5308. break;
  5309. }
  5310. best_score = valids[i].score;
  5311. }
  5312. }
  5313. if (valid_count == 0) {
  5314. error(operand->expr, "No overloads or ambiguous call for `%.*s` that match with the given arguments", LIT(name));
  5315. gb_printf_err("\tGiven argument types -> (");
  5316. for_array(i, operands) {
  5317. Operand o = operands[i];
  5318. if (i > 0) gb_printf_err(", ");
  5319. gbString type = type_to_string(o.type);
  5320. defer (gb_string_free(type));
  5321. gb_printf_err("%s", type);
  5322. }
  5323. gb_printf_err(")\n");
  5324. if (overload_count > 0) {
  5325. gb_printf_err("Did you mean to use one of the following:\n");
  5326. }
  5327. for (isize i = 0; i < overload_count; i++) {
  5328. Entity *proc = procs[i];
  5329. TokenPos pos = proc->token.pos;
  5330. Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
  5331. gbString pt;
  5332. if (t->Proc.node != NULL) {
  5333. pt = expr_to_string(t->Proc.node);
  5334. } else {
  5335. pt = type_to_string(t);
  5336. }
  5337. gb_printf_err("\t%.*s :: %s at %.*s(%td:%td) with score %lld\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column, cast(long long)valids[i].score);
  5338. // gb_printf_err("\t%.*s :: %s at %.*s(%td:%td)\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column);
  5339. gb_string_free(pt);
  5340. }
  5341. result_type = t_invalid;
  5342. } else if (valid_count > 1) {
  5343. error(operand->expr, "Ambiguous procedure call `%.*s` tha match with the given arguments", LIT(name));
  5344. gb_printf_err("\tGiven argument types -> (");
  5345. for_array(i, operands) {
  5346. Operand o = operands[i];
  5347. if (i > 0) gb_printf_err(", ");
  5348. gbString type = type_to_string(o.type);
  5349. defer (gb_string_free(type));
  5350. gb_printf_err("%s", type);
  5351. }
  5352. gb_printf_err(")\n");
  5353. for (isize i = 0; i < valid_count; i++) {
  5354. Entity *proc = procs[valids[i].index];
  5355. TokenPos pos = proc->token.pos;
  5356. Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
  5357. gbString pt;
  5358. if (t->Proc.node != NULL) {
  5359. pt = expr_to_string(t->Proc.node);
  5360. } else {
  5361. pt = type_to_string(t);
  5362. }
  5363. // gb_printf_err("\t%.*s :: %s at %.*s(%td:%td) with score %lld\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column, cast(long long)valids[i].score);
  5364. gb_printf_err("\t%.*s :: %s at %.*s(%td:%td)\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column);
  5365. gb_string_free(pt);
  5366. }
  5367. result_type = t_invalid;
  5368. } else {
  5369. AstNode *ident = operand->expr;
  5370. while (ident->kind == AstNode_SelectorExpr) {
  5371. AstNode *s = ident->SelectorExpr.selector;
  5372. ident = s;
  5373. }
  5374. Entity *e = procs[valids[0].index];
  5375. proc_type = e->type;
  5376. CallArgumentData data = {};
  5377. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  5378. if (data.gen_entity != nullptr) {
  5379. add_entity_use(c, ident, data.gen_entity);
  5380. } else {
  5381. add_entity_use(c, ident, e);
  5382. }
  5383. return data;
  5384. }
  5385. } else {
  5386. AstNode *ident = operand->expr;
  5387. while (ident->kind == AstNode_SelectorExpr) {
  5388. AstNode *s = ident->SelectorExpr.selector;
  5389. ident = s;
  5390. }
  5391. Entity *e = entity_of_ident(&c->info, ident);
  5392. CallArgumentData data = {};
  5393. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  5394. if (data.gen_entity != nullptr) {
  5395. add_entity_use(c, ident, data.gen_entity);
  5396. } else {
  5397. add_entity_use(c, ident, e);
  5398. }
  5399. return data;
  5400. }
  5401. CallArgumentData data = {};
  5402. data.result_type = t_invalid;
  5403. return data;
  5404. }
  5405. Entity *find_using_index_expr(Type *t) {
  5406. t = base_type(t);
  5407. if (t->kind != Type_Record) {
  5408. return nullptr;
  5409. }
  5410. for (isize i = 0; i < t->Record.field_count; i++) {
  5411. Entity *f = t->Record.fields[i];
  5412. if (f->kind == Entity_Variable &&
  5413. (f->flags & EntityFlag_Field) != 0 &&
  5414. (f->flags & EntityFlag_Using) != 0) {
  5415. if (is_type_indexable(f->type)) {
  5416. return f;
  5417. }
  5418. Entity *res = find_using_index_expr(f->type);
  5419. if (res != nullptr) {
  5420. return res;
  5421. }
  5422. }
  5423. }
  5424. return nullptr;
  5425. }
  5426. isize lookup_polymorphic_struct_parameter(TypeRecord *st, String parameter_name) {
  5427. if (!st->is_polymorphic) return -1;
  5428. TypeTuple *params = &st->polymorphic_params->Tuple;
  5429. isize param_count = params->variable_count;
  5430. for (isize i = 0; i < param_count; i++) {
  5431. Entity *e = params->variables[i];
  5432. String name = e->token.string;
  5433. if (is_blank_ident(name)) {
  5434. continue;
  5435. }
  5436. if (name == parameter_name) {
  5437. return i;
  5438. }
  5439. }
  5440. return -1;
  5441. }
  5442. CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, AstNode *call) {
  5443. ast_node(ce, CallExpr, call);
  5444. Type *original_type = operand->type;
  5445. Type *struct_type = base_type(operand->type);
  5446. GB_ASSERT(is_type_struct(struct_type));
  5447. TypeRecord *st = &struct_type->Record;
  5448. GB_ASSERT(st->is_polymorphic);
  5449. bool show_error = true;
  5450. Array<Operand> operands = {};
  5451. defer (array_free(&operands));
  5452. bool named_fields = false;
  5453. if (is_call_expr_field_value(ce)) {
  5454. named_fields = true;
  5455. array_init_count(&operands, heap_allocator(), ce->args.count);
  5456. for_array(i, ce->args) {
  5457. AstNode *arg = ce->args[i];
  5458. ast_node(fv, FieldValue, arg);
  5459. check_expr_or_type(c, &operands[i], fv->value);
  5460. }
  5461. bool vari_expand = (ce->ellipsis.pos.line != 0);
  5462. if (vari_expand) {
  5463. error(ce->ellipsis, "Invalid use of `..` in a polymorphic type call`");
  5464. }
  5465. } else {
  5466. array_init(&operands, heap_allocator(), 2*ce->args.count);
  5467. check_unpack_arguments(c, -1, &operands, ce->args, false);
  5468. }
  5469. CallArgumentError err = CallArgumentError_None;
  5470. TypeTuple *tuple = &st->polymorphic_params->Tuple;
  5471. isize param_count = tuple->variable_count;
  5472. Array<Operand> ordered_operands = operands;
  5473. if (named_fields) {
  5474. bool *visited = gb_alloc_array(c->allocator, bool, param_count);
  5475. array_init_count(&ordered_operands, c->tmp_allocator, param_count);
  5476. for_array(i, ce->args) {
  5477. AstNode *arg = ce->args[i];
  5478. ast_node(fv, FieldValue, arg);
  5479. if (fv->field->kind != AstNode_Ident) {
  5480. if (show_error) {
  5481. gbString expr_str = expr_to_string(fv->field);
  5482. error(arg, "Invalid parameter name `%s` in polymorphic type call", expr_str);
  5483. gb_string_free(expr_str);
  5484. }
  5485. err = CallArgumentError_InvalidFieldValue;
  5486. continue;
  5487. }
  5488. String name = fv->field->Ident.token.string;
  5489. isize index = lookup_polymorphic_struct_parameter(st, name);
  5490. if (index < 0) {
  5491. if (show_error) {
  5492. error(arg, "No parameter named `%.*s` for this polymorphic type", LIT(name));
  5493. }
  5494. err = CallArgumentError_ParameterNotFound;
  5495. continue;
  5496. }
  5497. if (visited[index]) {
  5498. if (show_error) {
  5499. error(arg, "Duplicate parameter `%.*s` in polymorphic type", LIT(name));
  5500. }
  5501. err = CallArgumentError_DuplicateParameter;
  5502. continue;
  5503. }
  5504. visited[index] = true;
  5505. ordered_operands[index] = operands[i];
  5506. }
  5507. for (isize i = 0; i < param_count; i++) {
  5508. if (!visited[i]) {
  5509. Entity *e = tuple->variables[i];
  5510. if (is_blank_ident(e->token)) {
  5511. continue;
  5512. }
  5513. if (show_error) {
  5514. if (e->kind == Entity_TypeName) {
  5515. error(call, "Type parameter `%.*s` is missing in polymorphic type call",
  5516. LIT(e->token.string));
  5517. } else {
  5518. gbString str = type_to_string(e->type);
  5519. error(call, "Parameter `%.*s` of type `%s` is missing in polymorphic type call",
  5520. LIT(e->token.string), str);
  5521. gb_string_free(str);
  5522. }
  5523. }
  5524. err = CallArgumentError_ParameterMissing;
  5525. }
  5526. }
  5527. }
  5528. if (err != 0) {
  5529. operand->mode = Addressing_Invalid;
  5530. return err;
  5531. }
  5532. i64 score = 0;
  5533. for (isize i = 0; i < param_count; i++) {
  5534. Operand *o = &ordered_operands[i];
  5535. if (o->mode == Addressing_Invalid) {
  5536. continue;
  5537. }
  5538. Entity *e = tuple->variables[i];
  5539. if (e->kind == Entity_TypeName) {
  5540. if (o->mode != Addressing_Type) {
  5541. if (show_error) {
  5542. error(o->expr, "Expected a type for the argument `%.*s`", LIT(e->token.string));
  5543. }
  5544. err = CallArgumentError_WrongTypes;
  5545. }
  5546. if (are_types_identical(e->type, o->type)) {
  5547. score += assign_score_function(1);
  5548. } else {
  5549. score += assign_score_function(10);
  5550. }
  5551. } else {
  5552. i64 s = 0;
  5553. if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
  5554. if (show_error) {
  5555. check_assignment(c, o, e->type, str_lit("polymorphic type argument"));
  5556. }
  5557. err = CallArgumentError_WrongTypes;
  5558. }
  5559. o->type = e->type;
  5560. if (o->mode != Addressing_Constant) {
  5561. if (show_error) {
  5562. error(o->expr, "Expected a constant value for this polymorphic type argument");
  5563. }
  5564. err = CallArgumentError_NoneConstantParameter;
  5565. }
  5566. score += s;
  5567. }
  5568. }
  5569. if (param_count < ordered_operands.count) {
  5570. error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  5571. err = CallArgumentError_TooManyArguments;
  5572. } else if (param_count > ordered_operands.count) {
  5573. error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  5574. err = CallArgumentError_TooFewArguments;
  5575. }
  5576. if (err == 0) {
  5577. // TODO(bill): Check for previous types
  5578. gbAllocator a = c->allocator;
  5579. auto *found_gen_types = map_get(&c->info.gen_types, hash_pointer(original_type));
  5580. if (found_gen_types != nullptr) {
  5581. for_array(i, *found_gen_types) {
  5582. Entity *e = (*found_gen_types)[i];
  5583. Type *t = base_type(e->type);
  5584. TypeTuple *tuple = &t->Record.polymorphic_params->Tuple;
  5585. bool ok = true;
  5586. GB_ASSERT(param_count == tuple->variable_count);
  5587. for (isize j = 0; j < param_count; j++) {
  5588. Entity *p = tuple->variables[j];
  5589. Operand o = ordered_operands[j];
  5590. if (p->kind == Entity_TypeName) {
  5591. if (!are_types_identical(o.type, p->type)) {
  5592. ok = false;
  5593. }
  5594. } else if (p->kind == Entity_Constant) {
  5595. if (!are_types_identical(o.type, p->type)) {
  5596. ok = false;
  5597. }
  5598. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  5599. ok = false;
  5600. }
  5601. } else {
  5602. GB_PANIC("Unknown entity kind");
  5603. }
  5604. }
  5605. if (ok) {
  5606. operand->mode = Addressing_Type;
  5607. operand->type = e->type;
  5608. return err;
  5609. }
  5610. }
  5611. }
  5612. String generated_name = make_string_c(expr_to_string(call));
  5613. Type *named_type = make_type_named(a, generated_name, nullptr, nullptr);
  5614. Type *struct_type = make_type_struct(a);
  5615. AstNode *node = clone_ast_node(a, st->node);
  5616. set_base_type(named_type, struct_type);
  5617. check_open_scope(c, node);
  5618. check_struct_type(c, struct_type, node, &ordered_operands);
  5619. check_close_scope(c);
  5620. struct_type->Record.node = node;
  5621. Entity *e = nullptr;
  5622. {
  5623. Token token = ast_node_token(node);
  5624. token.kind = Token_String;
  5625. token.string = generated_name;
  5626. AstNode *node = gb_alloc_item(a, AstNode);
  5627. node->kind = AstNode_Ident;
  5628. node->Ident.token = token;
  5629. e = make_entity_type_name(a, st->scope->parent, token, named_type);
  5630. add_entity(c, st->scope->parent, node, e);
  5631. add_entity_use(c, node, e);
  5632. }
  5633. named_type->Named.type_name = e;
  5634. if (found_gen_types) {
  5635. array_add(found_gen_types, e);
  5636. } else {
  5637. Array<Entity *> array = {};
  5638. array_init(&array, heap_allocator());
  5639. array_add(&array, e);
  5640. map_set(&c->info.gen_types, hash_pointer(original_type), array);
  5641. }
  5642. operand->mode = Addressing_Type;
  5643. operand->type = named_type;
  5644. }
  5645. return err;
  5646. }
  5647. ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  5648. ast_node(ce, CallExpr, call);
  5649. if (ce->proc != nullptr &&
  5650. ce->proc->kind == AstNode_BasicDirective) {
  5651. ast_node(bd, BasicDirective, ce->proc);
  5652. String name = bd->name;
  5653. GB_ASSERT(name == "location");
  5654. operand->mode = Addressing_Builtin;
  5655. operand->builtin_id = BuiltinProc_DIRECTIVE;
  5656. operand->expr = ce->proc;
  5657. operand->type = t_invalid;
  5658. add_type_and_value(&c->info, ce->proc, operand->mode, operand->type, operand->value);
  5659. } else {
  5660. check_expr_or_type(c, operand, ce->proc);
  5661. }
  5662. if (ce->args.count > 0) {
  5663. bool fail = false;
  5664. bool first_is_field_value = (ce->args[0]->kind == AstNode_FieldValue);
  5665. for_array(i, ce->args) {
  5666. AstNode *arg = ce->args[i];
  5667. bool mix = false;
  5668. if (first_is_field_value) {
  5669. mix = arg->kind != AstNode_FieldValue;
  5670. } else {
  5671. mix = arg->kind == AstNode_FieldValue;
  5672. }
  5673. if (mix) {
  5674. error(arg, "Mixture of `field = value` and value elements in a procedure all is not allowed");
  5675. fail = true;
  5676. }
  5677. }
  5678. if (fail) {
  5679. operand->mode = Addressing_Invalid;
  5680. operand->expr = call;
  5681. return Expr_Stmt;
  5682. }
  5683. }
  5684. if (operand->mode == Addressing_Invalid) {
  5685. for_array(i, ce->args) {
  5686. AstNode *arg = ce->args[i];
  5687. if (arg->kind == AstNode_FieldValue) {
  5688. arg = arg->FieldValue.value;
  5689. }
  5690. check_expr_base(c, operand, arg, nullptr);
  5691. }
  5692. operand->mode = Addressing_Invalid;
  5693. operand->expr = call;
  5694. return Expr_Stmt;
  5695. }
  5696. if (operand->mode == Addressing_Type) {
  5697. Type *t = operand->type;
  5698. if (is_type_polymorphic_struct(t)) {
  5699. auto err = check_polymorphic_struct_type(c, operand, call);
  5700. if (err == 0) {
  5701. AstNode *ident = operand->expr;
  5702. while (ident->kind == AstNode_SelectorExpr) {
  5703. AstNode *s = ident->SelectorExpr.selector;
  5704. ident = s;
  5705. }
  5706. add_entity_use(c, ident, entity_of_ident(&c->info, ident));
  5707. add_type_and_value(&c->info, call, Addressing_Type, operand->type, empty_exact_value);
  5708. } else {
  5709. operand->mode = Addressing_Invalid;
  5710. operand->type = t_invalid;
  5711. }
  5712. } else {
  5713. gbString str = type_to_string(t);
  5714. defer (gb_string_free(str));
  5715. operand->mode = Addressing_Invalid;
  5716. isize arg_count = ce->args.count;
  5717. switch (arg_count) {
  5718. case 0: error(call, "Missing argument in conversion to `%s`", str); break;
  5719. default: error(call, "Too many arguments in conversion to `%s`", str); break;
  5720. case 1: {
  5721. AstNode *arg = ce->args[0];
  5722. if (arg->kind == AstNode_FieldValue) {
  5723. error(call, "`field = value` cannot be used in a type conversion");
  5724. arg = arg->FieldValue.value;
  5725. // NOTE(bill): Carry on the cast regardless
  5726. }
  5727. check_expr(c, operand, arg);
  5728. if (operand->mode != Addressing_Invalid) {
  5729. check_cast(c, operand, t);
  5730. }
  5731. } break;
  5732. }
  5733. }
  5734. return Expr_Expr;
  5735. }
  5736. if (operand->mode == Addressing_Builtin) {
  5737. i32 id = operand->builtin_id;
  5738. if (!check_builtin_procedure(c, operand, call, id)) {
  5739. operand->mode = Addressing_Invalid;
  5740. }
  5741. operand->expr = call;
  5742. return builtin_procs[id].kind;
  5743. }
  5744. Type *proc_type = base_type(operand->type);
  5745. if (operand->mode != Addressing_Overload) {
  5746. bool valid_type = (proc_type != nullptr) && is_type_proc(proc_type);
  5747. bool valid_mode = is_operand_value(*operand);
  5748. if (!valid_type || !valid_mode) {
  5749. AstNode *e = operand->expr;
  5750. gbString str = expr_to_string(e);
  5751. gbString type_str = type_to_string(operand->type);
  5752. error(e, "Cannot call a non-procedure: `%s` of type `%s`", str, type_str);
  5753. gb_string_free(type_str);
  5754. gb_string_free(str);
  5755. operand->mode = Addressing_Invalid;
  5756. operand->expr = call;
  5757. return Expr_Stmt;
  5758. }
  5759. }
  5760. CallArgumentData data = check_call_arguments(c, operand, proc_type, call);
  5761. Type *result_type = data.result_type;
  5762. gb_zero_item(operand);
  5763. operand->expr = call;
  5764. if (result_type == t_invalid) {
  5765. operand->mode = Addressing_Invalid;
  5766. operand->type = t_invalid;
  5767. return Expr_Stmt;
  5768. }
  5769. Type *pt = base_type(proc_type);
  5770. if (result_type == nullptr) {
  5771. operand->mode = Addressing_NoValue;
  5772. } else {
  5773. GB_ASSERT(is_type_tuple(result_type));
  5774. switch (result_type->Tuple.variable_count) {
  5775. case 0:
  5776. operand->mode = Addressing_NoValue;
  5777. break;
  5778. case 1:
  5779. operand->mode = Addressing_Value;
  5780. operand->type = result_type->Tuple.variables[0]->type;
  5781. break;
  5782. default:
  5783. operand->mode = Addressing_Value;
  5784. operand->type = result_type;
  5785. break;
  5786. }
  5787. }
  5788. operand->expr = call;
  5789. return Expr_Expr;
  5790. }
  5791. ExprKind check_macro_call_expr(Checker *c, Operand *operand, AstNode *call) {
  5792. GB_ASSERT(call->kind == AstNode_MacroCallExpr);
  5793. ast_node(mce, MacroCallExpr, call);
  5794. error(call, "Macro call expressions are not yet supported");
  5795. operand->mode = Addressing_Invalid;
  5796. operand->expr = call;
  5797. return Expr_Stmt;
  5798. }
  5799. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  5800. check_expr_base(c, o, e, t);
  5801. check_not_tuple(c, o);
  5802. char *err_str = nullptr;
  5803. switch (o->mode) {
  5804. case Addressing_NoValue:
  5805. err_str = "used as a value";
  5806. break;
  5807. case Addressing_Type:
  5808. err_str = "is not an expression";
  5809. break;
  5810. case Addressing_Builtin:
  5811. err_str = "must be called";
  5812. break;
  5813. }
  5814. if (err_str != nullptr) {
  5815. gbString str = expr_to_string(e);
  5816. error(e, "`%s` %s", str, err_str);
  5817. gb_string_free(str);
  5818. o->mode = Addressing_Invalid;
  5819. }
  5820. }
  5821. void check_set_mode_with_indirection(Operand *o, bool indirection) {
  5822. if (o->mode != Addressing_Immutable) {
  5823. if (indirection) {
  5824. o->mode = Addressing_Variable;
  5825. } else if (o->mode != Addressing_Variable &&
  5826. o->mode != Addressing_Constant) {
  5827. o->mode = Addressing_Value;
  5828. }
  5829. }
  5830. }
  5831. bool check_set_index_data(Operand *o, Type *type, bool indirection, i64 *max_count) {
  5832. Type *t = base_type(type_deref(type));
  5833. switch (t->kind) {
  5834. case Type_Basic:
  5835. if (is_type_string(t)) {
  5836. if (o->mode == Addressing_Constant) {
  5837. *max_count = o->value.value_string.len;
  5838. }
  5839. check_set_mode_with_indirection(o, indirection);
  5840. o->type = t_u8;
  5841. return true;
  5842. }
  5843. break;
  5844. case Type_Array:
  5845. *max_count = t->Array.count;
  5846. check_set_mode_with_indirection(o, indirection);
  5847. o->type = t->Array.elem;
  5848. return true;
  5849. case Type_Vector:
  5850. *max_count = t->Vector.count;
  5851. check_set_mode_with_indirection(o, indirection);
  5852. o->type = t->Vector.elem;
  5853. return true;
  5854. case Type_Slice:
  5855. o->type = t->Slice.elem;
  5856. if (o->mode != Addressing_Immutable) {
  5857. o->mode = Addressing_Variable;
  5858. }
  5859. return true;
  5860. case Type_DynamicArray:
  5861. o->type = t->DynamicArray.elem;
  5862. check_set_mode_with_indirection(o, indirection);
  5863. return true;
  5864. }
  5865. return false;
  5866. }
  5867. ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  5868. ExprKind kind = Expr_Stmt;
  5869. o->mode = Addressing_Invalid;
  5870. o->type = t_invalid;
  5871. switch (node->kind) {
  5872. default:
  5873. return kind;
  5874. case_ast_node(be, BadExpr, node)
  5875. return kind;
  5876. case_end;
  5877. case_ast_node(i, Implicit, node)
  5878. switch (i->kind) {
  5879. case Token_context:
  5880. if (c->context.proc_name.len == 0) {
  5881. error(node, "`context` is only allowed within procedures");
  5882. return kind;
  5883. }
  5884. init_preload(c);
  5885. o->mode = Addressing_Value;
  5886. o->type = t_context;
  5887. break;
  5888. case Token_size_of:
  5889. o->mode = Addressing_Builtin;
  5890. o->builtin_id = BuiltinProc_size_of;
  5891. break;
  5892. case Token_align_of:
  5893. o->mode = Addressing_Builtin;
  5894. o->builtin_id = BuiltinProc_align_of;
  5895. break;
  5896. case Token_offset_of:
  5897. o->mode = Addressing_Builtin;
  5898. o->builtin_id = BuiltinProc_offset_of;
  5899. break;
  5900. case Token_type_of:
  5901. o->mode = Addressing_Builtin;
  5902. o->builtin_id = BuiltinProc_type_of;
  5903. break;
  5904. default:
  5905. error(node, "Illegal implicit name `%.*s`", LIT(i->string));
  5906. return kind;
  5907. }
  5908. case_end;
  5909. case_ast_node(i, Ident, node);
  5910. check_ident(c, o, node, nullptr, type_hint, false);
  5911. case_end;
  5912. case_ast_node(u, Undef, node);
  5913. o->mode = Addressing_Value;
  5914. o->type = t_untyped_undef;
  5915. case_end;
  5916. case_ast_node(bl, BasicLit, node);
  5917. Type *t = t_invalid;
  5918. switch (bl->kind) {
  5919. case Token_Integer: t = t_untyped_integer; break;
  5920. case Token_Float: t = t_untyped_float; break;
  5921. case Token_String: t = t_untyped_string; break;
  5922. case Token_Rune: t = t_untyped_rune; break;
  5923. case Token_Imag: {
  5924. String s = bl->string;
  5925. Rune r = s[s.len-1];
  5926. switch (r) {
  5927. case 'i': t = t_untyped_complex; break;
  5928. }
  5929. } break;
  5930. default: GB_PANIC("Unknown literal"); break;
  5931. }
  5932. o->mode = Addressing_Constant;
  5933. o->type = t;
  5934. o->value = exact_value_from_basic_literal(*bl);
  5935. case_end;
  5936. case_ast_node(bd, BasicDirective, node);
  5937. if (bd->name == "file") {
  5938. o->type = t_untyped_string;
  5939. o->value = exact_value_string(bd->token.pos.file);
  5940. } else if (bd->name == "line") {
  5941. o->type = t_untyped_integer;
  5942. o->value = exact_value_i64(bd->token.pos.line);
  5943. } else if (bd->name == "procedure") {
  5944. if (c->proc_stack.count == 0) {
  5945. error(node, "#procedure may only be used within procedures");
  5946. o->type = t_untyped_string;
  5947. o->value = exact_value_string(str_lit(""));
  5948. } else {
  5949. o->type = t_untyped_string;
  5950. o->value = exact_value_string(c->context.proc_name);
  5951. }
  5952. } else if (bd->name == "caller_location") {
  5953. init_preload(c);
  5954. error(node, "#caller_location may only be used as a default argument parameter");
  5955. o->type = t_source_code_location;
  5956. o->mode = Addressing_Value;
  5957. } else {
  5958. GB_PANIC("Unknown basic directive");
  5959. }
  5960. o->mode = Addressing_Constant;
  5961. case_end;
  5962. case_ast_node(pl, ProcLit, node);
  5963. CheckerContext prev_context = c->context;
  5964. DeclInfo *decl = nullptr;
  5965. Type *type = alloc_type(c->allocator, Type_Proc);
  5966. check_open_scope(c, pl->type);
  5967. {
  5968. decl = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
  5969. decl->proc_lit = node;
  5970. c->context.decl = decl;
  5971. if (pl->tags != 0) {
  5972. error(node, "A procedure literal cannot have tags");
  5973. pl->tags = 0; // TODO(bill): Should I zero this?!
  5974. }
  5975. check_procedure_type(c, type, pl->type);
  5976. if (!is_type_proc(type)) {
  5977. gbString str = expr_to_string(node);
  5978. error(node, "Invalid procedure literal `%s`", str);
  5979. gb_string_free(str);
  5980. check_close_scope(c);
  5981. return kind;
  5982. }
  5983. if (pl->body == nullptr) {
  5984. error(node, "A procedure literal must have a body");
  5985. return kind;
  5986. }
  5987. check_procedure_later(c, c->curr_ast_file, empty_token, decl, type, pl->body, pl->tags);
  5988. }
  5989. check_close_scope(c);
  5990. c->context = prev_context;
  5991. o->mode = Addressing_Value;
  5992. o->type = type;
  5993. case_end;
  5994. case_ast_node(te, TernaryExpr, node);
  5995. Operand cond = {Addressing_Invalid};
  5996. check_expr(c, &cond, te->cond);
  5997. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  5998. error(te->cond, "Non-boolean condition in if expression");
  5999. }
  6000. Operand x = {Addressing_Invalid};
  6001. Operand y = {Addressing_Invalid};
  6002. check_expr_with_type_hint(c, &x, te->x, type_hint);
  6003. if (te->y != nullptr) {
  6004. check_expr_with_type_hint(c, &y, te->y, type_hint);
  6005. } else {
  6006. error(node, "A ternary expression must have an else clause");
  6007. return kind;
  6008. }
  6009. if (x.type == nullptr || x.type == t_invalid ||
  6010. y.type == nullptr || y.type == t_invalid) {
  6011. return kind;
  6012. }
  6013. convert_to_typed(c, &x, y.type, 0);
  6014. if (x.mode == Addressing_Invalid) {
  6015. return kind;
  6016. }
  6017. convert_to_typed(c, &y, x.type, 0);
  6018. if (y.mode == Addressing_Invalid) {
  6019. x.mode = Addressing_Invalid;
  6020. return kind;
  6021. }
  6022. if (!are_types_identical(x.type, y.type)) {
  6023. gbString its = type_to_string(x.type);
  6024. gbString ets = type_to_string(y.type);
  6025. error(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
  6026. gb_string_free(ets);
  6027. gb_string_free(its);
  6028. return kind;
  6029. }
  6030. o->type = x.type;
  6031. o->mode = Addressing_Value;
  6032. if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  6033. x.mode == Addressing_Constant &&
  6034. y.mode == Addressing_Constant) {
  6035. o->mode = Addressing_Constant;
  6036. if (cond.value.value_bool) {
  6037. o->value = x.value;
  6038. } else {
  6039. o->value = y.value;
  6040. }
  6041. }
  6042. case_end;
  6043. case_ast_node(cl, CompoundLit, node);
  6044. Type *type = type_hint;
  6045. bool is_to_be_determined_array_count = false;
  6046. bool is_constant = true;
  6047. if (cl->type != nullptr) {
  6048. type = nullptr;
  6049. // [..]Type
  6050. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != nullptr) {
  6051. AstNode *count = cl->type->ArrayType.count;
  6052. if (count->kind == AstNode_UnaryExpr &&
  6053. count->UnaryExpr.op.kind == Token_Ellipsis) {
  6054. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  6055. is_to_be_determined_array_count = true;
  6056. }
  6057. }
  6058. if (type == nullptr) {
  6059. type = check_type(c, cl->type);
  6060. }
  6061. }
  6062. if (type == nullptr) {
  6063. error(node, "Missing type in compound literal");
  6064. return kind;
  6065. }
  6066. Type *t = base_type(type);
  6067. switch (t->kind) {
  6068. case Type_Record: {
  6069. if (is_type_union(t)) {
  6070. is_constant = false;
  6071. }
  6072. if (cl->elems.count == 0) {
  6073. break; // NOTE(bill): No need to init
  6074. }
  6075. if (!is_type_struct(t)) {
  6076. if (cl->elems.count != 0) {
  6077. gbString type_str = type_to_string(type);
  6078. error(node, "Illegal compound literal type `%s`", type_str);
  6079. gb_string_free(type_str);
  6080. }
  6081. break;
  6082. }
  6083. { // Checker values
  6084. isize field_count = t->Record.field_count;
  6085. if (cl->elems[0]->kind == AstNode_FieldValue) {
  6086. bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
  6087. for_array(i, cl->elems) {
  6088. AstNode *elem = cl->elems[i];
  6089. if (elem->kind != AstNode_FieldValue) {
  6090. error(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  6091. continue;
  6092. }
  6093. ast_node(fv, FieldValue, elem);
  6094. if (fv->field->kind != AstNode_Ident) {
  6095. gbString expr_str = expr_to_string(fv->field);
  6096. error(elem, "Invalid field name `%s` in structure literal", expr_str);
  6097. gb_string_free(expr_str);
  6098. continue;
  6099. }
  6100. String name = fv->field->Ident.token.string;
  6101. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  6102. bool is_unknown = sel.entity == nullptr;
  6103. if (is_unknown) {
  6104. error(elem, "Unknown field `%.*s` in structure literal", LIT(name));
  6105. continue;
  6106. }
  6107. if (!is_unknown && !check_is_field_exported(c, sel.entity)) {
  6108. error(elem, "Cannot assign to an unexported field `%.*s` in structure literal", LIT(name));
  6109. continue;
  6110. }
  6111. if (sel.index.count > 1) {
  6112. error(elem, "Cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
  6113. continue;
  6114. }
  6115. Entity *field = t->Record.fields[sel.index[0]];
  6116. add_entity_use(c, fv->field, field);
  6117. if (fields_visited[sel.index[0]]) {
  6118. error(elem, "Duplicate field `%.*s` in structure literal", LIT(name));
  6119. continue;
  6120. }
  6121. fields_visited[sel.index[0]] = true;
  6122. check_expr(c, o, fv->value);
  6123. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
  6124. is_constant = false;
  6125. }
  6126. if (is_constant) {
  6127. is_constant = o->mode == Addressing_Constant;
  6128. }
  6129. check_assignment(c, o, field->type, str_lit("structure literal"));
  6130. }
  6131. } else {
  6132. bool all_fields_are_blank = true;
  6133. for (isize i = 0; i < t->Record.field_count; i++) {
  6134. Entity *field = t->Record.fields_in_src_order[i];
  6135. if (!is_blank_ident(field->token)) {
  6136. all_fields_are_blank = false;
  6137. break;
  6138. }
  6139. }
  6140. for_array(index, cl->elems) {
  6141. AstNode *elem = cl->elems[index];
  6142. if (elem->kind == AstNode_FieldValue) {
  6143. error(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  6144. continue;
  6145. }
  6146. if (index >= field_count) {
  6147. error(o->expr, "Too many values in structure literal, expected %td", field_count);
  6148. break;
  6149. }
  6150. Entity *field = t->Record.fields_in_src_order[index];
  6151. if (!all_fields_are_blank && is_blank_ident(field->token)) {
  6152. // NOTE(bill): Ignore blank identifiers
  6153. continue;
  6154. }
  6155. check_expr(c, o, elem);
  6156. if (!check_is_field_exported(c, field)) {
  6157. gbString t = type_to_string(type);
  6158. error(o->expr, "Implicit assignment to an unexported field `%.*s` in `%s` literal",
  6159. LIT(field->token.string), t);
  6160. gb_string_free(t);
  6161. continue;
  6162. }
  6163. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
  6164. is_constant = false;
  6165. }
  6166. if (is_constant) {
  6167. is_constant = o->mode == Addressing_Constant;
  6168. }
  6169. check_assignment(c, o, field->type, str_lit("structure literal"));
  6170. }
  6171. if (cl->elems.count < field_count) {
  6172. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  6173. }
  6174. }
  6175. }
  6176. } break;
  6177. case Type_Slice:
  6178. case Type_Array:
  6179. case Type_Vector:
  6180. case Type_DynamicArray:
  6181. {
  6182. Type *elem_type = nullptr;
  6183. String context_name = {};
  6184. i64 max_type_count = -1;
  6185. if (t->kind == Type_Slice) {
  6186. elem_type = t->Slice.elem;
  6187. context_name = str_lit("slice literal");
  6188. } else if (t->kind == Type_Vector) {
  6189. elem_type = t->Vector.elem;
  6190. context_name = str_lit("vector literal");
  6191. max_type_count = t->Vector.count;
  6192. } else if (t->kind == Type_Array) {
  6193. elem_type = t->Array.elem;
  6194. context_name = str_lit("array literal");
  6195. max_type_count = t->Array.count;
  6196. } else if (t->kind == Type_DynamicArray) {
  6197. elem_type = t->DynamicArray.elem;
  6198. context_name = str_lit("dynamic array literal");
  6199. is_constant = false;
  6200. } else {
  6201. GB_PANIC("unreachable");
  6202. }
  6203. i64 max = 0;
  6204. isize index = 0;
  6205. isize elem_count = cl->elems.count;
  6206. if (is_type_any(base_type(elem_type))) {
  6207. is_constant = false;
  6208. }
  6209. for (; index < elem_count; index++) {
  6210. GB_ASSERT(cl->elems.data != nullptr);
  6211. AstNode *e = cl->elems[index];
  6212. if (e == nullptr) {
  6213. error(node, "Invalid literal element");
  6214. continue;
  6215. }
  6216. if (e->kind == AstNode_FieldValue) {
  6217. error(e, "`field = value` is only allowed in struct literals");
  6218. continue;
  6219. }
  6220. if (0 <= max_type_count && max_type_count <= index) {
  6221. error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  6222. }
  6223. Operand operand = {};
  6224. check_expr_with_type_hint(c, &operand, e, elem_type);
  6225. check_assignment(c, &operand, elem_type, context_name);
  6226. if (is_constant) {
  6227. is_constant = operand.mode == Addressing_Constant;
  6228. }
  6229. }
  6230. if (max < index) {
  6231. max = index;
  6232. }
  6233. if (t->kind == Type_Vector) {
  6234. if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
  6235. error(cl->elems[0], "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
  6236. }
  6237. }
  6238. if (t->kind == Type_Array && is_to_be_determined_array_count) {
  6239. t->Array.count = max;
  6240. }
  6241. } break;
  6242. case Type_Basic: {
  6243. if (!is_type_any(t)) {
  6244. if (cl->elems.count != 0) {
  6245. error(node, "Illegal compound literal");
  6246. }
  6247. break;
  6248. }
  6249. if (cl->elems.count == 0) {
  6250. break; // NOTE(bill): No need to init
  6251. }
  6252. { // Checker values
  6253. Type *field_types[2] = {t_rawptr, t_type_info_ptr};
  6254. isize field_count = 2;
  6255. if (cl->elems[0]->kind == AstNode_FieldValue) {
  6256. bool fields_visited[2] = {};
  6257. for_array(i, cl->elems) {
  6258. AstNode *elem = cl->elems[i];
  6259. if (elem->kind != AstNode_FieldValue) {
  6260. error(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  6261. continue;
  6262. }
  6263. ast_node(fv, FieldValue, elem);
  6264. if (fv->field->kind != AstNode_Ident) {
  6265. gbString expr_str = expr_to_string(fv->field);
  6266. error(elem, "Invalid field name `%s` in `any` literal", expr_str);
  6267. gb_string_free(expr_str);
  6268. continue;
  6269. }
  6270. String name = fv->field->Ident.token.string;
  6271. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  6272. if (sel.entity == nullptr) {
  6273. error(elem, "Unknown field `%.*s` in `any` literal", LIT(name));
  6274. continue;
  6275. }
  6276. isize index = sel.index[0];
  6277. if (fields_visited[index]) {
  6278. error(elem, "Duplicate field `%.*s` in `any` literal", LIT(name));
  6279. continue;
  6280. }
  6281. fields_visited[index] = true;
  6282. check_expr(c, o, fv->value);
  6283. // NOTE(bill): `any` literals can never be constant
  6284. is_constant = false;
  6285. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  6286. }
  6287. } else {
  6288. for_array(index, cl->elems) {
  6289. AstNode *elem = cl->elems[index];
  6290. if (elem->kind == AstNode_FieldValue) {
  6291. error(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  6292. continue;
  6293. }
  6294. check_expr(c, o, elem);
  6295. if (index >= field_count) {
  6296. error(o->expr, "Too many values in `any` literal, expected %td", field_count);
  6297. break;
  6298. }
  6299. // NOTE(bill): `any` literals can never be constant
  6300. is_constant = false;
  6301. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  6302. }
  6303. if (cl->elems.count < field_count) {
  6304. error(cl->close, "Too few values in `any` literal, expected %td, got %td", field_count, cl->elems.count);
  6305. }
  6306. }
  6307. }
  6308. } break;
  6309. case Type_Map: {
  6310. if (cl->elems.count == 0) {
  6311. break;
  6312. }
  6313. is_constant = false;
  6314. { // Checker values
  6315. for_array(i, cl->elems) {
  6316. AstNode *elem = cl->elems[i];
  6317. if (elem->kind != AstNode_FieldValue) {
  6318. error(elem, "Only `field = value` elements are allowed in a map literal");
  6319. continue;
  6320. }
  6321. ast_node(fv, FieldValue, elem);
  6322. check_expr_with_type_hint(c, o, fv->field, t->Map.key);
  6323. check_assignment(c, o, t->Map.key, str_lit("map literal"));
  6324. if (o->mode == Addressing_Invalid) {
  6325. continue;
  6326. }
  6327. check_expr_with_type_hint(c, o, fv->value, t->Map.value);
  6328. check_assignment(c, o, t->Map.value, str_lit("map literal"));
  6329. }
  6330. }
  6331. } break;
  6332. default: {
  6333. if (cl->elems.count == 0) {
  6334. break; // NOTE(bill): No need to init
  6335. }
  6336. gbString str = type_to_string(type);
  6337. error(node, "Invalid compound literal type `%s`", str);
  6338. gb_string_free(str);
  6339. return kind;
  6340. } break;
  6341. }
  6342. if (is_constant) {
  6343. o->mode = Addressing_Constant;
  6344. o->value = exact_value_compound(node);
  6345. } else {
  6346. o->mode = Addressing_Value;
  6347. }
  6348. o->type = type;
  6349. case_end;
  6350. case_ast_node(pe, ParenExpr, node);
  6351. kind = check_expr_base(c, o, pe->expr, type_hint);
  6352. o->expr = node;
  6353. case_end;
  6354. case_ast_node(te, TagExpr, node);
  6355. String name = te->name.string;
  6356. error(node, "Unknown tag expression, #%.*s", LIT(name));
  6357. if (te->expr) {
  6358. kind = check_expr_base(c, o, te->expr, type_hint);
  6359. }
  6360. o->expr = node;
  6361. case_end;
  6362. case_ast_node(re, RunExpr, node);
  6363. // TODO(bill): Tag expressions
  6364. kind = check_expr_base(c, o, re->expr, type_hint);
  6365. o->expr = node;
  6366. case_end;
  6367. case_ast_node(ta, TypeAssertion, node);
  6368. check_expr(c, o, ta->expr);
  6369. if (o->mode == Addressing_Invalid) {
  6370. o->expr = node;
  6371. return kind;
  6372. }
  6373. Type *t = check_type(c, ta->type);
  6374. if (o->mode == Addressing_Constant) {
  6375. gbString expr_str = expr_to_string(o->expr);
  6376. error(o->expr, "A type assertion cannot be applied to a constant expression: `%s`", expr_str);
  6377. gb_string_free(expr_str);
  6378. o->mode = Addressing_Invalid;
  6379. o->expr = node;
  6380. return kind;
  6381. }
  6382. if (is_type_untyped(o->type)) {
  6383. gbString expr_str = expr_to_string(o->expr);
  6384. error(o->expr, "A type assertion cannot be applied to an untyped expression: `%s`", expr_str);
  6385. gb_string_free(expr_str);
  6386. o->mode = Addressing_Invalid;
  6387. o->expr = node;
  6388. return kind;
  6389. }
  6390. bool src_is_ptr = is_type_pointer(o->type);
  6391. Type *src = type_deref(o->type);
  6392. Type *dst = t;
  6393. Type *bsrc = base_type(src);
  6394. Type *bdst = base_type(dst);
  6395. if (is_type_union(src)) {
  6396. bool ok = false;
  6397. for (isize i = 1; i < bsrc->Union.variant_count; i++) {
  6398. Type *vt = bsrc->Union.variants[i];
  6399. if (are_types_identical(vt, dst)) {
  6400. ok = true;
  6401. break;
  6402. }
  6403. }
  6404. if (!ok) {
  6405. gbString expr_str = expr_to_string(o->expr);
  6406. gbString dst_type_str = type_to_string(t);
  6407. error(o->expr, "Cannot type assert `%s` to `%s` as it is not a variant of that union", expr_str, dst_type_str);
  6408. gb_string_free(dst_type_str);
  6409. gb_string_free(expr_str);
  6410. o->mode = Addressing_Invalid;
  6411. o->expr = node;
  6412. return kind;
  6413. }
  6414. add_type_info_type(c, o->type);
  6415. add_type_info_type(c, t);
  6416. o->type = t;
  6417. o->mode = Addressing_OptionalOk;
  6418. } else if (is_type_any(src)) {
  6419. o->type = t;
  6420. o->mode = Addressing_OptionalOk;
  6421. add_type_info_type(c, o->type);
  6422. add_type_info_type(c, t);
  6423. } else {
  6424. gbString str = type_to_string(o->type);
  6425. error(o->expr, "Type assertions can only operate on unions and `any`, got %s", str);
  6426. gb_string_free(str);
  6427. o->mode = Addressing_Invalid;
  6428. o->expr = node;
  6429. return kind;
  6430. }
  6431. case_end;
  6432. case_ast_node(tc, TypeCast, node);
  6433. check_expr_or_type(c, o, tc->type);
  6434. if (o->mode != Addressing_Type) {
  6435. gbString str = expr_to_string(tc->type);
  6436. error(tc->type, "Expected a type, got %s", str);
  6437. gb_string_free(str);
  6438. o->mode = Addressing_Invalid;
  6439. }
  6440. if (o->mode == Addressing_Invalid) {
  6441. o->expr = node;
  6442. return kind;
  6443. }
  6444. Type *type = o->type;
  6445. check_expr_base(c, o, tc->expr, type);
  6446. if (o->mode != Addressing_Invalid) {
  6447. check_cast(c, o, type);
  6448. }
  6449. return Expr_Expr;
  6450. case_end;
  6451. case_ast_node(ue, UnaryExpr, node);
  6452. check_expr_base(c, o, ue->expr, type_hint);
  6453. if (o->mode == Addressing_Invalid) {
  6454. o->expr = node;
  6455. return kind;
  6456. }
  6457. check_unary_expr(c, o, ue->op, node);
  6458. if (o->mode == Addressing_Invalid) {
  6459. o->expr = node;
  6460. return kind;
  6461. }
  6462. case_end;
  6463. case_ast_node(be, BinaryExpr, node);
  6464. check_binary_expr(c, o, node);
  6465. if (o->mode == Addressing_Invalid) {
  6466. o->expr = node;
  6467. return kind;
  6468. }
  6469. case_end;
  6470. case_ast_node(se, SelectorExpr, node);
  6471. check_selector(c, o, node, type_hint);
  6472. case_end;
  6473. case_ast_node(ie, IndexExpr, node);
  6474. check_expr(c, o, ie->expr);
  6475. if (o->mode == Addressing_Invalid) {
  6476. o->expr = node;
  6477. return kind;
  6478. }
  6479. Type *t = base_type(type_deref(o->type));
  6480. bool is_ptr = is_type_pointer(o->type);
  6481. bool is_const = o->mode == Addressing_Constant;
  6482. if (is_type_map(t)) {
  6483. Operand key = {};
  6484. check_expr(c, &key, ie->index);
  6485. check_assignment(c, &key, t->Map.key, str_lit("map index"));
  6486. if (key.mode == Addressing_Invalid) {
  6487. o->mode = Addressing_Invalid;
  6488. o->expr = node;
  6489. return kind;
  6490. }
  6491. o->mode = Addressing_MapIndex;
  6492. o->type = t->Map.value;
  6493. o->expr = node;
  6494. return Expr_Expr;
  6495. }
  6496. i64 max_count = -1;
  6497. bool valid = check_set_index_data(o, t, is_ptr, &max_count);
  6498. if (is_const) {
  6499. valid = false;
  6500. }
  6501. if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
  6502. Entity *found = find_using_index_expr(t);
  6503. if (found != nullptr) {
  6504. valid = check_set_index_data(o, found->type, is_type_pointer(found->type), &max_count);
  6505. }
  6506. }
  6507. if (!valid) {
  6508. gbString str = expr_to_string(o->expr);
  6509. if (is_const) {
  6510. error(o->expr, "Cannot index a constant `%s`", str);
  6511. } else {
  6512. error(o->expr, "Cannot index `%s`", str);
  6513. }
  6514. gb_string_free(str);
  6515. o->mode = Addressing_Invalid;
  6516. o->expr = node;
  6517. return kind;
  6518. }
  6519. if (ie->index == nullptr) {
  6520. gbString str = expr_to_string(o->expr);
  6521. error(o->expr, "Missing index for `%s`", str);
  6522. gb_string_free(str);
  6523. o->mode = Addressing_Invalid;
  6524. o->expr = node;
  6525. return kind;
  6526. }
  6527. i64 index = 0;
  6528. bool ok = check_index_value(c, false, ie->index, max_count, &index);
  6529. case_end;
  6530. case_ast_node(se, SliceExpr, node);
  6531. check_expr(c, o, se->expr);
  6532. if (o->mode == Addressing_Invalid) {
  6533. o->mode = Addressing_Invalid;
  6534. o->expr = node;
  6535. return kind;
  6536. }
  6537. bool valid = false;
  6538. i64 max_count = -1;
  6539. Type *t = base_type(type_deref(o->type));
  6540. switch (t->kind) {
  6541. case Type_Basic:
  6542. if (is_type_string(t)) {
  6543. if (se->index3) {
  6544. error(node, "3-index slice on a string in not needed");
  6545. o->mode = Addressing_Invalid;
  6546. o->expr = node;
  6547. return kind;
  6548. }
  6549. valid = true;
  6550. if (o->mode == Addressing_Constant) {
  6551. max_count = o->value.value_string.len;
  6552. }
  6553. o->type = t_string;
  6554. }
  6555. break;
  6556. case Type_Array:
  6557. valid = true;
  6558. max_count = t->Array.count;
  6559. if (o->mode != Addressing_Variable) {
  6560. gbString str = expr_to_string(node);
  6561. error(node, "Cannot slice array `%s`, value is not addressable", str);
  6562. gb_string_free(str);
  6563. o->mode = Addressing_Invalid;
  6564. o->expr = node;
  6565. return kind;
  6566. }
  6567. o->type = make_type_slice(c->allocator, t->Array.elem);
  6568. break;
  6569. case Type_Slice:
  6570. valid = true;
  6571. o->type = type_deref(o->type);
  6572. break;
  6573. case Type_DynamicArray:
  6574. valid = true;
  6575. o->type = make_type_slice(c->allocator, t->DynamicArray.elem);
  6576. break;
  6577. }
  6578. if (!valid) {
  6579. gbString str = expr_to_string(o->expr);
  6580. error(o->expr, "Cannot slice `%s`", str);
  6581. gb_string_free(str);
  6582. o->mode = Addressing_Invalid;
  6583. o->expr = node;
  6584. return kind;
  6585. }
  6586. if (o->mode != Addressing_Immutable) {
  6587. o->mode = Addressing_Value;
  6588. }
  6589. if (se->low == nullptr && se->high != nullptr) {
  6590. // error(se->interval0, "1st index is required if a 2nd index is specified");
  6591. // It is okay to continue as it will assume the 1st index is zero
  6592. }
  6593. if (se->index3 && (se->high == nullptr || se->max == nullptr)) {
  6594. error(se->close, "2nd and 3rd indices are required in a 3-index slice");
  6595. o->mode = Addressing_Invalid;
  6596. o->expr = node;
  6597. return kind;
  6598. }
  6599. if (se->index3 && se->interval0.kind != se->interval1.kind) {
  6600. error(se->close, "The interval separators for in a 3-index slice must be the same");
  6601. o->mode = Addressing_Invalid;
  6602. o->expr = node;
  6603. return kind;
  6604. }
  6605. TokenKind interval_kind = se->interval0.kind;
  6606. i64 indices[3] = {};
  6607. AstNode *nodes[3] = {se->low, se->high, se->max};
  6608. for (isize i = 0; i < gb_count_of(nodes); i++) {
  6609. i64 index = max_count;
  6610. if (nodes[i] != nullptr) {
  6611. i64 capacity = -1;
  6612. if (max_count >= 0) {
  6613. capacity = max_count;
  6614. }
  6615. i64 j = 0;
  6616. if (check_index_value(c, interval_kind == Token_Ellipsis, nodes[i], capacity, &j)) {
  6617. index = j;
  6618. }
  6619. } else if (i == 0) {
  6620. index = 0;
  6621. }
  6622. indices[i] = index;
  6623. }
  6624. for (isize i = 0; i < gb_count_of(indices); i++) {
  6625. i64 a = indices[i];
  6626. for (isize j = i+1; j < gb_count_of(indices); j++) {
  6627. i64 b = indices[j];
  6628. if (a > b && b >= 0) {
  6629. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  6630. }
  6631. }
  6632. }
  6633. case_end;
  6634. case_ast_node(ce, CallExpr, node);
  6635. return check_call_expr(c, o, node);
  6636. case_end;
  6637. case_ast_node(ce, MacroCallExpr, node);
  6638. return check_macro_call_expr(c, o, node);
  6639. case_end;
  6640. case_ast_node(de, DerefExpr, node);
  6641. check_expr_or_type(c, o, de->expr);
  6642. if (o->mode == Addressing_Invalid) {
  6643. o->mode = Addressing_Invalid;
  6644. o->expr = node;
  6645. return kind;
  6646. } else {
  6647. Type *t = base_type(o->type);
  6648. if (t->kind == Type_Pointer) {
  6649. if (o->mode != Addressing_Immutable) {
  6650. o->mode = Addressing_Variable;
  6651. }
  6652. o->type = t->Pointer.elem;
  6653. } else {
  6654. gbString str = expr_to_string(o->expr);
  6655. error(o->expr, "Cannot dereference `%s`", str);
  6656. gb_string_free(str);
  6657. o->mode = Addressing_Invalid;
  6658. o->expr = node;
  6659. return kind;
  6660. }
  6661. }
  6662. case_end;
  6663. case AstNode_TypeType:
  6664. case AstNode_PolyType:
  6665. case AstNode_ProcType:
  6666. case AstNode_PointerType:
  6667. case AstNode_ArrayType:
  6668. case AstNode_DynamicArrayType:
  6669. case AstNode_VectorType:
  6670. case AstNode_StructType:
  6671. case AstNode_UnionType:
  6672. case AstNode_RawUnionType:
  6673. case AstNode_EnumType:
  6674. case AstNode_MapType:
  6675. o->mode = Addressing_Type;
  6676. o->type = check_type(c, node);
  6677. break;
  6678. }
  6679. kind = Expr_Expr;
  6680. o->expr = node;
  6681. return kind;
  6682. }
  6683. ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  6684. ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
  6685. Type *type = nullptr;
  6686. ExactValue value = {ExactValue_Invalid};
  6687. switch (o->mode) {
  6688. case Addressing_Invalid:
  6689. type = t_invalid;
  6690. break;
  6691. case Addressing_NoValue:
  6692. type = nullptr;
  6693. break;
  6694. case Addressing_Constant:
  6695. type = o->type;
  6696. value = o->value;
  6697. break;
  6698. default:
  6699. type = o->type;
  6700. break;
  6701. }
  6702. if (type != nullptr && is_type_untyped(type)) {
  6703. add_untyped(&c->info, node, false, o->mode, type, value);
  6704. } else {
  6705. add_type_and_value(&c->info, node, o->mode, type, value);
  6706. }
  6707. return kind;
  6708. }
  6709. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  6710. check_expr_base(c, o, e, nullptr);
  6711. switch (o->mode) {
  6712. default:
  6713. return; // NOTE(bill): Valid
  6714. case Addressing_NoValue:
  6715. error_operand_no_value(o);
  6716. break;
  6717. case Addressing_Type:
  6718. error_operand_not_expression(o);
  6719. break;
  6720. }
  6721. o->mode = Addressing_Invalid;
  6722. }
  6723. void check_not_tuple(Checker *c, Operand *o) {
  6724. if (o->mode == Addressing_Value) {
  6725. // NOTE(bill): Tuples are not first class thus never named
  6726. if (o->type->kind == Type_Tuple) {
  6727. isize count = o->type->Tuple.variable_count;
  6728. GB_ASSERT(count != 1);
  6729. error(o->expr,
  6730. "%td-valued tuple found where single value expected", count);
  6731. o->mode = Addressing_Invalid;
  6732. }
  6733. }
  6734. }
  6735. void check_expr(Checker *c, Operand *o, AstNode *e) {
  6736. check_multi_expr(c, o, e);
  6737. check_not_tuple(c, o);
  6738. }
  6739. void check_expr_or_type(Checker *c, Operand *o, AstNode *e, Type *type_hint) {
  6740. check_expr_base(c, o, e, type_hint);
  6741. check_not_tuple(c, o);
  6742. error_operand_no_value(o);
  6743. }
  6744. gbString write_expr_to_string(gbString str, AstNode *node);
  6745. gbString write_record_fields_to_string(gbString str, Array<AstNode *> params) {
  6746. for_array(i, params) {
  6747. if (i > 0) {
  6748. str = gb_string_appendc(str, ", ");
  6749. }
  6750. str = write_expr_to_string(str, params[i]);
  6751. }
  6752. return str;
  6753. }
  6754. gbString string_append_token(gbString str, Token token) {
  6755. if (token.string.len > 0) {
  6756. return gb_string_append_length(str, &token.string[0], token.string.len);
  6757. }
  6758. return str;
  6759. }
  6760. gbString write_expr_to_string(gbString str, AstNode *node) {
  6761. if (node == nullptr)
  6762. return str;
  6763. if (is_ast_node_stmt(node)) {
  6764. GB_ASSERT("stmt passed to write_expr_to_string");
  6765. }
  6766. switch (node->kind) {
  6767. default:
  6768. str = gb_string_appendc(str, "(BadExpr)");
  6769. break;
  6770. case_ast_node(i, Ident, node);
  6771. str = string_append_token(str, i->token);
  6772. case_end;
  6773. case_ast_node(i, Implicit, node);
  6774. str = string_append_token(str, *i);
  6775. case_end;
  6776. case_ast_node(bl, BasicLit, node);
  6777. str = string_append_token(str, *bl);
  6778. case_end;
  6779. case_ast_node(bd, BasicDirective, node);
  6780. str = gb_string_appendc(str, "#");
  6781. str = gb_string_append_length(str, &bd->name[0], bd->name.len);
  6782. case_end;
  6783. case_ast_node(pl, ProcLit, node);
  6784. str = write_expr_to_string(str, pl->type);
  6785. case_end;
  6786. case_ast_node(cl, CompoundLit, node);
  6787. str = write_expr_to_string(str, cl->type);
  6788. str = gb_string_appendc(str, "{");
  6789. for_array(i, cl->elems) {
  6790. if (i > 0) {
  6791. str = gb_string_appendc(str, ", ");
  6792. }
  6793. str = write_expr_to_string(str, cl->elems[i]);
  6794. }
  6795. str = gb_string_appendc(str, "}");
  6796. case_end;
  6797. case_ast_node(te, TagExpr, node);
  6798. str = gb_string_appendc(str, "#");
  6799. str = string_append_token(str, te->name);
  6800. str = write_expr_to_string(str, te->expr);
  6801. case_end;
  6802. case_ast_node(ue, UnaryExpr, node);
  6803. str = string_append_token(str, ue->op);
  6804. str = write_expr_to_string(str, ue->expr);
  6805. case_end;
  6806. case_ast_node(de, DerefExpr, node);
  6807. str = write_expr_to_string(str, de->expr);
  6808. str = gb_string_appendc(str, "^");
  6809. case_end;
  6810. case_ast_node(be, BinaryExpr, node);
  6811. str = write_expr_to_string(str, be->left);
  6812. str = gb_string_appendc(str, " ");
  6813. str = string_append_token(str, be->op);
  6814. str = gb_string_appendc(str, " ");
  6815. str = write_expr_to_string(str, be->right);
  6816. case_end;
  6817. case_ast_node(te, TernaryExpr, node);
  6818. str = write_expr_to_string(str, te->cond);
  6819. str = gb_string_appendc(str, " ? ");
  6820. str = write_expr_to_string(str, te->x);
  6821. str = gb_string_appendc(str, " : ");
  6822. str = write_expr_to_string(str, te->y);
  6823. case_end;
  6824. case_ast_node(pe, ParenExpr, node);
  6825. str = gb_string_appendc(str, "(");
  6826. str = write_expr_to_string(str, pe->expr);
  6827. str = gb_string_appendc(str, ")");
  6828. case_end;
  6829. case_ast_node(se, SelectorExpr, node);
  6830. str = write_expr_to_string(str, se->expr);
  6831. str = gb_string_appendc(str, ".");
  6832. str = write_expr_to_string(str, se->selector);
  6833. case_end;
  6834. case_ast_node(ta, TypeAssertion, node);
  6835. str = write_expr_to_string(str, ta->expr);
  6836. str = gb_string_appendc(str, ".(");
  6837. str = write_expr_to_string(str, ta->type);
  6838. str = gb_string_appendc(str, ")");
  6839. case_end;
  6840. case_ast_node(ie, IndexExpr, node);
  6841. str = write_expr_to_string(str, ie->expr);
  6842. str = gb_string_appendc(str, "[");
  6843. str = write_expr_to_string(str, ie->index);
  6844. str = gb_string_appendc(str, "]");
  6845. case_end;
  6846. case_ast_node(se, SliceExpr, node);
  6847. str = write_expr_to_string(str, se->expr);
  6848. str = gb_string_appendc(str, "[");
  6849. str = write_expr_to_string(str, se->low);
  6850. str = gb_string_appendc(str, "..");
  6851. str = write_expr_to_string(str, se->high);
  6852. if (se->index3) {
  6853. str = gb_string_appendc(str, "..");
  6854. str = write_expr_to_string(str, se->max);
  6855. }
  6856. str = gb_string_appendc(str, "]");
  6857. case_end;
  6858. case_ast_node(e, Ellipsis, node);
  6859. str = gb_string_appendc(str, "..");
  6860. str = write_expr_to_string(str, e->expr);
  6861. case_end;
  6862. case_ast_node(fv, FieldValue, node);
  6863. str = write_expr_to_string(str, fv->field);
  6864. str = gb_string_appendc(str, " = ");
  6865. str = write_expr_to_string(str, fv->value);
  6866. case_end;
  6867. case_ast_node(ht, HelperType, node);
  6868. str = gb_string_appendc(str, "#type ");
  6869. str = write_expr_to_string(str, ht->type);
  6870. case_end;
  6871. case_ast_node(pt, PolyType, node);
  6872. str = gb_string_appendc(str, "$");
  6873. str = write_expr_to_string(str, pt->type);
  6874. case_end;
  6875. case_ast_node(pt, PointerType, node);
  6876. str = gb_string_appendc(str, "^");
  6877. str = write_expr_to_string(str, pt->type);
  6878. case_end;
  6879. case_ast_node(at, ArrayType, node);
  6880. str = gb_string_appendc(str, "[");
  6881. if (at->count != nullptr &&
  6882. at->count->kind == AstNode_UnaryExpr &&
  6883. at->count->UnaryExpr.op.kind == Token_Ellipsis) {
  6884. str = gb_string_appendc(str, "..");
  6885. } else {
  6886. str = write_expr_to_string(str, at->count);
  6887. }
  6888. str = gb_string_appendc(str, "]");
  6889. str = write_expr_to_string(str, at->elem);
  6890. case_end;
  6891. case_ast_node(at, DynamicArrayType, node);
  6892. str = gb_string_appendc(str, "[dynamic]");
  6893. str = write_expr_to_string(str, at->elem);
  6894. case_end;
  6895. case_ast_node(vt, VectorType, node);
  6896. str = gb_string_appendc(str, "[vector ");
  6897. str = write_expr_to_string(str, vt->count);
  6898. str = gb_string_appendc(str, "]");
  6899. str = write_expr_to_string(str, vt->elem);
  6900. case_end;
  6901. case_ast_node(f, Field, node);
  6902. if (f->flags&FieldFlag_using) {
  6903. str = gb_string_appendc(str, "using ");
  6904. }
  6905. if (f->flags&FieldFlag_no_alias) {
  6906. str = gb_string_appendc(str, "#no_alias ");
  6907. }
  6908. if (f->flags&FieldFlag_c_vararg) {
  6909. str = gb_string_appendc(str, "#c_vararg ");
  6910. }
  6911. for_array(i, f->names) {
  6912. AstNode *name = f->names[i];
  6913. if (i > 0) {
  6914. str = gb_string_appendc(str, ", ");
  6915. }
  6916. str = write_expr_to_string(str, name);
  6917. }
  6918. if (f->names.count > 0) {
  6919. str = gb_string_appendc(str, ": ");
  6920. }
  6921. str = write_expr_to_string(str, f->type);
  6922. case_end;
  6923. case_ast_node(f, FieldList, node);
  6924. bool has_name = false;
  6925. for_array(i, f->list) {
  6926. ast_node(field, Field, f->list[i]);
  6927. if (field->names.count > 1) {
  6928. has_name = true;
  6929. break;
  6930. }
  6931. if (field->names.count == 0) {
  6932. continue;
  6933. }
  6934. if (!is_blank_ident(field->names[0])) {
  6935. has_name = true;
  6936. break;
  6937. }
  6938. }
  6939. for_array(i, f->list) {
  6940. if (i > 0) {
  6941. str = gb_string_appendc(str, ", ");
  6942. }
  6943. if (has_name) {
  6944. str = write_expr_to_string(str, f->list[i]);
  6945. } else {
  6946. ast_node(field, Field, f->list[i]);
  6947. if (field->flags&FieldFlag_using) {
  6948. str = gb_string_appendc(str, "using ");
  6949. }
  6950. if (field->flags&FieldFlag_no_alias) {
  6951. str = gb_string_appendc(str, "#no_alias ");
  6952. }
  6953. if (field->flags&FieldFlag_c_vararg) {
  6954. str = gb_string_appendc(str, "#c_vararg ");
  6955. }
  6956. str = write_expr_to_string(str, field->type);
  6957. }
  6958. }
  6959. case_end;
  6960. case_ast_node(f, UnionField, node);
  6961. str = write_expr_to_string(str, f->name);
  6962. str = gb_string_appendc(str, "{");
  6963. str = write_expr_to_string(str, f->list);
  6964. str = gb_string_appendc(str, "}");
  6965. case_end;
  6966. case_ast_node(ce, CallExpr, node);
  6967. str = write_expr_to_string(str, ce->proc);
  6968. str = gb_string_appendc(str, "(");
  6969. for_array(i, ce->args) {
  6970. AstNode *arg = ce->args[i];
  6971. if (i > 0) {
  6972. str = gb_string_appendc(str, ", ");
  6973. }
  6974. str = write_expr_to_string(str, arg);
  6975. }
  6976. str = gb_string_appendc(str, ")");
  6977. case_end;
  6978. case_ast_node(ht, TypeType, node);
  6979. str = gb_string_appendc(str, "type");
  6980. case_end;
  6981. case_ast_node(pt, ProcType, node);
  6982. str = gb_string_appendc(str, "proc(");
  6983. str = write_expr_to_string(str, pt->params);
  6984. str = gb_string_appendc(str, ")");
  6985. if (pt->results != nullptr) {
  6986. str = gb_string_appendc(str, " -> ");
  6987. str = write_expr_to_string(str, pt->results);
  6988. }
  6989. case_end;
  6990. case_ast_node(st, StructType, node);
  6991. str = gb_string_appendc(str, "struct ");
  6992. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  6993. if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
  6994. str = gb_string_appendc(str, "{");
  6995. str = write_record_fields_to_string(str, st->fields);
  6996. str = gb_string_appendc(str, "}");
  6997. case_end;
  6998. case_ast_node(st, RawUnionType, node);
  6999. str = gb_string_appendc(str, "raw_union ");
  7000. str = gb_string_appendc(str, "{");
  7001. str = write_record_fields_to_string(str, st->fields);
  7002. str = gb_string_appendc(str, "}");
  7003. case_end;
  7004. case_ast_node(st, UnionType, node);
  7005. str = gb_string_appendc(str, "union ");
  7006. str = gb_string_appendc(str, "{");
  7007. str = write_record_fields_to_string(str, st->variants);
  7008. str = gb_string_appendc(str, "}");
  7009. case_end;
  7010. case_ast_node(et, EnumType, node);
  7011. str = gb_string_appendc(str, "enum ");
  7012. if (et->base_type != nullptr) {
  7013. str = write_expr_to_string(str, et->base_type);
  7014. str = gb_string_appendc(str, " ");
  7015. }
  7016. str = gb_string_appendc(str, "{");
  7017. for_array(i, et->fields) {
  7018. if (i > 0) {
  7019. str = gb_string_appendc(str, ", ");
  7020. }
  7021. str = write_expr_to_string(str, et->fields[i]);
  7022. }
  7023. str = gb_string_appendc(str, "}");
  7024. case_end;
  7025. case_ast_node(at, AtomicType, node);
  7026. str = gb_string_appendc(str, "atomic ");
  7027. str = write_expr_to_string(str, at->type);
  7028. case_end;
  7029. }
  7030. return str;
  7031. }
  7032. gbString expr_to_string(AstNode *expression) {
  7033. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
  7034. }