check_expr.cpp 186 KB

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