check_expr.cpp 174 KB

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