check_expr.cpp 274 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744
  1. enum CallArgumentError {
  2. CallArgumentError_None,
  3. CallArgumentError_NoneProcedureType,
  4. CallArgumentError_WrongTypes,
  5. CallArgumentError_NonVariadicExpand,
  6. CallArgumentError_VariadicTuple,
  7. CallArgumentError_MultipleVariadicExpand,
  8. CallArgumentError_AmbiguousPolymorphicVariadic,
  9. CallArgumentError_ArgumentCount,
  10. CallArgumentError_TooFewArguments,
  11. CallArgumentError_TooManyArguments,
  12. CallArgumentError_InvalidFieldValue,
  13. CallArgumentError_ParameterNotFound,
  14. CallArgumentError_ParameterMissing,
  15. CallArgumentError_DuplicateParameter,
  16. CallArgumentError_NoneConstantParameter,
  17. };
  18. enum CallArgumentErrorMode {
  19. CallArgumentMode_NoErrors,
  20. CallArgumentMode_ShowErrors,
  21. };
  22. struct CallArgumentData {
  23. Entity *gen_entity;
  24. i64 score;
  25. Type * result_type;
  26. };
  27. struct PolyProcData {
  28. Entity * gen_entity;
  29. ProcInfo proc_info;
  30. };
  31. struct ValidIndexAndScore {
  32. isize index;
  33. i64 score;
  34. };
  35. int valid_index_and_score_cmp(void const *a, void const *b) {
  36. i64 si = (cast(ValidIndexAndScore const *)a)->score;
  37. i64 sj = (cast(ValidIndexAndScore const *)b)->score;
  38. return sj < si ? -1 : sj > si;
  39. }
  40. #define CALL_ARGUMENT_CHECKER(name) CallArgumentError name(CheckerContext *c, Ast *call, Type *proc_type, Entity *entity, Array<Operand> operands, CallArgumentErrorMode show_error_mode, CallArgumentData *data)
  41. typedef CALL_ARGUMENT_CHECKER(CallArgumentCheckerType);
  42. void check_expr (CheckerContext *c, Operand *operand, Ast *expression);
  43. void check_multi_expr (CheckerContext *c, Operand *operand, Ast *expression);
  44. void check_multi_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression);
  45. void check_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint = nullptr);
  46. ExprKind check_expr_base (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
  47. void check_expr_with_type_hint (CheckerContext *c, Operand *o, Ast *e, Type *t);
  48. Type * check_type (CheckerContext *c, Ast *expression);
  49. Type * check_type_expr (CheckerContext *c, Ast *expression, Type *named_type);
  50. Type * make_optional_ok_type (Type *value);
  51. void check_type_decl (CheckerContext *c, Entity *e, Ast *type_expr, Type *def);
  52. Entity * check_selector (CheckerContext *c, Operand *operand, Ast *node, Type *type_hint);
  53. Entity * check_ident (CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name);
  54. Entity * find_polymorphic_record_entity (CheckerContext *c, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure);
  55. void check_not_tuple (CheckerContext *c, Operand *operand);
  56. void convert_to_typed (CheckerContext *c, Operand *operand, Type *target_type);
  57. gbString expr_to_string (Ast *expression);
  58. void check_entity_decl (CheckerContext *c, Entity *e, DeclInfo *decl, Type *named_type);
  59. void check_const_decl (CheckerContext *c, Entity *e, Ast *type_expr, Ast *init_expr, Type *named_type);
  60. void check_proc_body (CheckerContext *c, Token token, DeclInfo *decl, Type *type, Ast *body);
  61. void update_expr_type (CheckerContext *c, Ast *e, Type *type, bool final);
  62. bool check_is_terminating (Ast *node);
  63. bool check_has_break (Ast *stmt, bool implicit);
  64. void check_stmt (CheckerContext *c, Ast *node, u32 flags);
  65. void check_stmt_list (CheckerContext *c, Array<Ast *> const &stmts, u32 flags);
  66. void check_init_constant (CheckerContext *c, Entity *e, Operand *operand);
  67. bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value);
  68. bool check_procedure_type (CheckerContext *c, Type *type, Ast *proc_type_node, Array<Operand> *operands = nullptr);
  69. void check_struct_type (CheckerContext *c, Type *struct_type, Ast *node, Array<Operand> *poly_operands,
  70. Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
  71. void check_union_type (CheckerContext *c, Type *union_type, Ast *node, Array<Operand> *poly_operands,
  72. Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
  73. CallArgumentData check_call_arguments (CheckerContext *c, Operand *operand, Type *proc_type, Ast *call);
  74. Type * check_init_variable (CheckerContext *c, Entity *e, Operand *operand, String context_name);
  75. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
  76. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
  77. bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type);
  78. void set_procedure_abi_types(gbAllocator a, Type *type);
  79. void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type);
  80. Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
  81. Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
  82. Entity *entity_from_expr(Ast *expr) {
  83. expr = unparen_expr(expr);
  84. switch (expr->kind) {
  85. case Ast_Ident:
  86. return expr->Ident.entity;
  87. case Ast_SelectorExpr:
  88. return entity_from_expr(expr->SelectorExpr.selector);
  89. }
  90. return nullptr;
  91. }
  92. void error_operand_not_expression(Operand *o) {
  93. if (o->mode == Addressing_Type) {
  94. gbString err = expr_to_string(o->expr);
  95. error(o->expr, "'%s' is not an expression but a type", err);
  96. gb_string_free(err);
  97. o->mode = Addressing_Invalid;
  98. }
  99. }
  100. void error_operand_no_value(Operand *o) {
  101. if (o->mode == Addressing_NoValue) {
  102. gbString err = expr_to_string(o->expr);
  103. Ast *x = unparen_expr(o->expr);
  104. if (x->kind == Ast_CallExpr) {
  105. error(o->expr, "'%s' call does not return a value and cannot be used as a value", err);
  106. } else {
  107. error(o->expr, "'%s' used as a value", err);
  108. }
  109. gb_string_free(err);
  110. o->mode = Addressing_Invalid;
  111. }
  112. }
  113. void check_scope_decls(CheckerContext *c, Array<Ast *> const &nodes, isize reserve_size) {
  114. Scope *s = c->scope;
  115. check_collect_entities(c, nodes);
  116. for_array(i, s->elements.entries) {
  117. Entity *e = s->elements.entries[i].value;
  118. switch (e->kind) {
  119. case Entity_Constant:
  120. case Entity_TypeName:
  121. case Entity_Procedure:
  122. break;
  123. default:
  124. continue;
  125. }
  126. DeclInfo *d = decl_info_of_entity(e);
  127. if (d != nullptr) {
  128. check_entity_decl(c, e, d, nullptr);
  129. }
  130. }
  131. }
  132. isize check_is_assignable_to_using_subtype(Type *src, Type *dst, isize level = 0, bool src_is_ptr = false) {
  133. Type *prev_src = src;
  134. src = type_deref(src);
  135. if (!src_is_ptr) {
  136. src_is_ptr = src != prev_src;
  137. }
  138. src = base_type(src);
  139. if (!is_type_struct(src)) {
  140. return 0;
  141. }
  142. for_array(i, src->Struct.fields) {
  143. Entity *f = src->Struct.fields[i];
  144. if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
  145. continue;
  146. }
  147. if (are_types_identical(f->type, dst)) {
  148. return level+1;
  149. }
  150. if (src_is_ptr && is_type_pointer(dst)) {
  151. if (are_types_identical(f->type, type_deref(dst))) {
  152. return level+1;
  153. }
  154. }
  155. isize nested_level = check_is_assignable_to_using_subtype(f->type, dst, level+1, src_is_ptr);
  156. if (nested_level > 0) {
  157. return nested_level;
  158. }
  159. }
  160. return 0;
  161. }
  162. bool find_or_generate_polymorphic_procedure(CheckerContext *c, Entity *base_entity, Type *type,
  163. Array<Operand> *param_operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  164. ///////////////////////////////////////////////////////////////////////////////
  165. // //
  166. // TODO CLEANUP(bill): This procedure is very messy and hacky. Clean this!!! //
  167. // //
  168. ///////////////////////////////////////////////////////////////////////////////
  169. if (base_entity == nullptr) {
  170. return false;
  171. }
  172. if (!is_type_proc(base_entity->type)) {
  173. return false;
  174. }
  175. String name = base_entity->token.string;
  176. Type *src = base_type(base_entity->type);
  177. Type *dst = nullptr;
  178. if (type != nullptr) dst = base_type(type);
  179. if (param_operands == nullptr) {
  180. GB_ASSERT(dst != nullptr);
  181. }
  182. if (param_operands != nullptr) {
  183. GB_ASSERT(dst == nullptr);
  184. }
  185. if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
  186. return false;
  187. }
  188. if (dst != nullptr) {
  189. if (dst->Proc.is_polymorphic) {
  190. return false;
  191. }
  192. if (dst->Proc.param_count != src->Proc.param_count ||
  193. dst->Proc.result_count != src->Proc.result_count) {
  194. return false;
  195. }
  196. }
  197. DeclInfo *old_decl = decl_info_of_entity(base_entity);
  198. if (old_decl == nullptr) {
  199. return false;
  200. }
  201. gbAllocator a = heap_allocator();
  202. Array<Operand> operands = {};
  203. if (param_operands) {
  204. operands = *param_operands;
  205. } else {
  206. operands = array_make<Operand>(a, 0, dst->Proc.param_count);
  207. for (isize i = 0; i < dst->Proc.param_count; i++) {
  208. Entity *param = dst->Proc.params->Tuple.variables[i];
  209. Operand o = {Addressing_Value};
  210. o.type = param->type;
  211. array_add(&operands, o);
  212. }
  213. }
  214. defer (if (param_operands == nullptr) {
  215. array_free(&operands);
  216. });
  217. CheckerContext nctx = *c;
  218. Scope *scope = create_scope(base_entity->scope, a);
  219. scope->flags |= ScopeFlag_Proc;
  220. nctx.scope = scope;
  221. nctx.allow_polymorphic_types = true;
  222. if (nctx.polymorphic_scope == nullptr) {
  223. nctx.polymorphic_scope = scope;
  224. }
  225. if (param_operands == nullptr) {
  226. // c->no_polymorphic_errors = false;
  227. }
  228. auto *pt = &src->Proc;
  229. // NOTE(bill): This is slightly memory leaking if the type already exists
  230. // Maybe it's better to check with the previous types first?
  231. Type *final_proc_type = alloc_type_proc(scope, nullptr, 0, nullptr, 0, false, pt->calling_convention);
  232. bool success = check_procedure_type(&nctx, final_proc_type, pt->node, &operands);
  233. if (!success) {
  234. return false;
  235. }
  236. auto *found_gen_procs = map_get(&nctx.info->gen_procs, hash_pointer(base_entity->identifier));
  237. if (found_gen_procs) {
  238. auto procs = *found_gen_procs;
  239. for_array(i, procs) {
  240. Entity *other = procs[i];
  241. Type *pt = base_type(other->type);
  242. if (are_types_identical(pt, final_proc_type)) {
  243. if (poly_proc_data) {
  244. poly_proc_data->gen_entity = other;
  245. }
  246. return true;
  247. }
  248. }
  249. }
  250. #if 0
  251. bool generate_type_again = nctx.no_polymorphic_errors;
  252. if (generate_type_again) {
  253. #else
  254. {
  255. #endif
  256. // LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice
  257. bool prev_no_polymorphic_errors = nctx.no_polymorphic_errors;
  258. defer (nctx.no_polymorphic_errors = prev_no_polymorphic_errors);
  259. nctx.no_polymorphic_errors = false;
  260. // NOTE(bill): Reset scope from the failed procedure type
  261. scope_reset(scope);
  262. // LEAK TODO(bill): Cloning this AST may be leaky
  263. Ast *cloned_proc_type_node = clone_ast(pt->node);
  264. success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands);
  265. if (!success) {
  266. return false;
  267. }
  268. if (found_gen_procs) {
  269. auto procs = *found_gen_procs;
  270. for_array(i, procs) {
  271. Entity *other = procs[i];
  272. Type *pt = base_type(other->type);
  273. if (are_types_identical(pt, final_proc_type)) {
  274. if (poly_proc_data) {
  275. poly_proc_data->gen_entity = other;
  276. }
  277. return true;
  278. }
  279. }
  280. }
  281. }
  282. Ast *proc_lit = clone_ast(old_decl->proc_lit);
  283. ast_node(pl, ProcLit, proc_lit);
  284. // NOTE(bill): Associate the scope declared above withinth this procedure declaration's type
  285. add_scope(&nctx, pl->type, final_proc_type->Proc.scope);
  286. final_proc_type->Proc.is_poly_specialized = true;
  287. final_proc_type->Proc.is_polymorphic = true;
  288. for (isize i = 0; i < operands.count; i++) {
  289. Operand o = operands[i];
  290. if (final_proc_type == o.type ||
  291. base_entity->type == o.type) {
  292. // NOTE(bill): Cycle
  293. final_proc_type->Proc.is_poly_specialized = false;
  294. break;
  295. }
  296. }
  297. u64 tags = base_entity->Procedure.tags;
  298. Ast *ident = clone_ast(base_entity->identifier);
  299. Token token = ident->Ident.token;
  300. DeclInfo *d = make_decl_info(nctx.allocator, scope, old_decl->parent);
  301. d->gen_proc_type = final_proc_type;
  302. d->type_expr = pl->type;
  303. d->proc_lit = proc_lit;
  304. Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags);
  305. entity->identifier = ident;
  306. add_entity_and_decl_info(&nctx, ident, entity, d);
  307. // NOTE(bill): Set the scope afterwards as this is not real overloading
  308. entity->scope = scope->parent;
  309. entity->file = base_entity->file;
  310. entity->pkg = base_entity->pkg;
  311. AstFile *file = nullptr;
  312. {
  313. Scope *s = entity->scope;
  314. while (s != nullptr && s->file == nullptr) {
  315. file = s->file;
  316. s = s->parent;
  317. }
  318. }
  319. ProcInfo proc_info = {};
  320. proc_info.file = file;
  321. proc_info.token = token;
  322. proc_info.decl = d;
  323. proc_info.type = final_proc_type;
  324. proc_info.body = pl->body;
  325. proc_info.tags = tags;
  326. proc_info.generated_from_polymorphic = true;
  327. proc_info.poly_def_node = poly_def_node;
  328. if (found_gen_procs) {
  329. array_add(found_gen_procs, entity);
  330. } else {
  331. auto array = array_make<Entity *>(heap_allocator());
  332. array_add(&array, entity);
  333. map_set(&nctx.checker->info.gen_procs, hash_pointer(base_entity->identifier), array);
  334. }
  335. GB_ASSERT(entity != nullptr);
  336. if (poly_proc_data) {
  337. poly_proc_data->gen_entity = entity;
  338. poly_proc_data->proc_info = proc_info;
  339. }
  340. // NOTE(bill): Check the newly generated procedure body
  341. check_procedure_later(nctx.checker, proc_info);
  342. return true;
  343. }
  344. bool check_polymorphic_procedure_assignment(CheckerContext *c, Operand *operand, Type *type, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  345. if (operand->expr == nullptr) return false;
  346. Entity *base_entity = entity_of_ident(operand->expr);
  347. if (base_entity == nullptr) return false;
  348. return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_def_node, poly_proc_data);
  349. }
  350. bool find_or_generate_polymorphic_procedure_from_parameters(CheckerContext *c, Entity *base_entity, Array<Operand> *operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  351. return find_or_generate_polymorphic_procedure(c, base_entity, nullptr, operands, poly_def_node, poly_proc_data);
  352. }
  353. bool check_type_specialization_to(CheckerContext *c, Type *specialization, Type *type, bool compound, bool modify_type);
  354. bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type);
  355. bool check_cast_internal(CheckerContext *c, Operand *x, Type *type);
  356. #define MAXIMUM_TYPE_DISTANCE 10
  357. i64 check_distance_between_types(CheckerContext *c, Operand *operand, Type *type) {
  358. if (operand->mode == Addressing_Invalid ||
  359. type == t_invalid) {
  360. return -1;
  361. }
  362. if (operand->mode == Addressing_Builtin) {
  363. return -1;
  364. }
  365. if (operand->mode == Addressing_Type) {
  366. if (is_type_typeid(type)) {
  367. add_type_info_type(c, operand->type);
  368. return 4;
  369. }
  370. return -1;
  371. }
  372. Type *s = operand->type;
  373. if (are_types_identical(s, type)) {
  374. return 0;
  375. }
  376. Type *src = base_type(s);
  377. Type *dst = base_type(type);
  378. if (is_type_untyped_undef(src)) {
  379. if (type_has_undef(dst)) {
  380. return 1;
  381. }
  382. return -1;
  383. }
  384. if (is_type_untyped_nil(src)) {
  385. if (type_has_nil(dst)) {
  386. return 1;
  387. }
  388. return -1;
  389. }
  390. if (is_type_untyped(src)) {
  391. if (is_type_any(dst)) {
  392. // NOTE(bill): Anything can cast to 'Any'
  393. add_type_info_type(c, s);
  394. return MAXIMUM_TYPE_DISTANCE;
  395. }
  396. if (dst->kind == Type_Basic) {
  397. if (operand->mode == Addressing_Constant) {
  398. if (check_representable_as_constant(c, operand->value, dst, nullptr)) {
  399. if (is_type_typed(dst) && src->kind == Type_Basic) {
  400. switch (src->Basic.kind) {
  401. case Basic_UntypedRune:
  402. if (is_type_integer(dst) || is_type_rune(dst)) {
  403. return 1;
  404. }
  405. break;
  406. case Basic_UntypedInteger:
  407. if (is_type_integer(dst) || is_type_rune(dst)) {
  408. return 1;
  409. }
  410. break;
  411. case Basic_UntypedFloat:
  412. if (is_type_float(dst)) {
  413. return 1;
  414. }
  415. break;
  416. case Basic_UntypedComplex:
  417. if (is_type_complex(dst)) {
  418. return 1;
  419. }
  420. if (is_type_quaternion(dst)) {
  421. return 2;
  422. }
  423. break;
  424. case Basic_UntypedQuaternion:
  425. if (is_type_quaternion(dst)) {
  426. return 1;
  427. }
  428. break;
  429. }
  430. }
  431. return 2;
  432. }
  433. return -1;
  434. }
  435. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedRune) {
  436. if (is_type_integer(dst) || is_type_rune(dst)) {
  437. if (is_type_typed(type)) {
  438. return 2;
  439. }
  440. return 1;
  441. }
  442. return -1;
  443. }
  444. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  445. if (is_type_boolean(dst)) {
  446. if (is_type_typed(type)) {
  447. return 2;
  448. }
  449. return 1;
  450. }
  451. return -1;
  452. }
  453. }
  454. }
  455. if (is_type_enum(dst) && are_types_identical(dst->Enum.base_type, operand->type)) {
  456. if (c->in_enum_type) {
  457. return 3;
  458. }
  459. }
  460. if (is_type_bit_field_value(operand->type) && is_type_integer(type)) {
  461. return 1;
  462. }
  463. if (is_type_bit_field_value(operand->type) && is_type_bit_field_value(type)) {
  464. return 1;
  465. }
  466. {
  467. isize subtype_level = check_is_assignable_to_using_subtype(operand->type, type);
  468. if (subtype_level > 0) {
  469. return 4 + subtype_level;
  470. }
  471. }
  472. // ^T <- rawptr
  473. #if 0
  474. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  475. if (is_type_pointer(dst) && is_type_rawptr(src)) {
  476. return true;
  477. }
  478. #endif
  479. #if 1
  480. // TODO(bill): Should I allow this implicit conversion at all?!
  481. // rawptr <- ^T
  482. if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
  483. return 5;
  484. }
  485. #endif
  486. if (is_type_polymorphic(dst) && !is_type_polymorphic(src)) {
  487. bool modify_type = !c->no_polymorphic_errors;
  488. if (is_polymorphic_type_assignable(c, type, s, false, modify_type)) {
  489. return 2;
  490. }
  491. }
  492. if (is_type_union(dst)) {
  493. for_array(i, dst->Union.variants) {
  494. Type *vt = dst->Union.variants[i];
  495. if (are_types_identical(vt, s)) {
  496. return 1;
  497. }
  498. }
  499. }
  500. if (is_type_proc(dst)) {
  501. if (are_types_identical(src, dst)) {
  502. return 3;
  503. }
  504. PolyProcData poly_proc_data = {};
  505. if (check_polymorphic_procedure_assignment(c, operand, type, operand->expr, &poly_proc_data)) {
  506. add_entity_use(c, operand->expr, poly_proc_data.gen_entity);
  507. return 4;
  508. }
  509. }
  510. if (is_type_array(dst)) {
  511. Type *elem = base_array_type(dst);
  512. i64 distance = check_distance_between_types(c, operand, elem);
  513. if (distance >= 0) {
  514. return distance + 6;
  515. }
  516. }
  517. if (is_type_any(dst)) {
  518. if (!is_type_polymorphic(src)) {
  519. if (operand->mode == Addressing_Context && operand->type == t_context) {
  520. return -1;
  521. } else {
  522. // NOTE(bill): Anything can cast to 'Any'
  523. add_type_info_type(c, s);
  524. return MAXIMUM_TYPE_DISTANCE;
  525. }
  526. }
  527. }
  528. Ast *expr = unparen_expr(operand->expr);
  529. if (expr != nullptr && expr->kind == Ast_AutoCast) {
  530. Operand x = *operand;
  531. x.expr = expr->AutoCast.expr;
  532. bool ok = check_cast_internal(c, &x, type);
  533. if (ok) {
  534. return MAXIMUM_TYPE_DISTANCE;
  535. }
  536. }
  537. return -1;
  538. }
  539. i64 assign_score_function(i64 distance, bool is_variadic=false) {
  540. // 3*x^2 + 1 > x^2 + x + 1 (for positive x)
  541. i64 const c = 3*MAXIMUM_TYPE_DISTANCE*MAXIMUM_TYPE_DISTANCE + 1;
  542. // TODO(bill): A decent score function
  543. i64 d = distance*distance; // x^2
  544. if (is_variadic && d >= 0) {
  545. d += distance + 1; // x^2 + x + 1
  546. }
  547. return gb_max(c - d, 0);
  548. }
  549. bool check_is_assignable_to_with_score(CheckerContext *c, Operand *operand, Type *type, i64 *score_, bool is_variadic=false) {
  550. i64 score = 0;
  551. i64 distance = check_distance_between_types(c, operand, type);
  552. bool ok = distance >= 0;
  553. if (ok) {
  554. score = assign_score_function(distance, is_variadic);
  555. }
  556. if (score_) *score_ = score;
  557. return ok;
  558. }
  559. bool check_is_assignable_to(CheckerContext *c, Operand *operand, Type *type) {
  560. i64 score = 0;
  561. return check_is_assignable_to_with_score(c, operand, type, &score);
  562. }
  563. // NOTE(bill): 'content_name' is for debugging and error messages
  564. void check_assignment(CheckerContext *c, Operand *operand, Type *type, String context_name) {
  565. check_not_tuple(c, operand);
  566. if (operand->mode == Addressing_Invalid) {
  567. return;
  568. }
  569. if (is_type_untyped(operand->type)) {
  570. Type *target_type = type;
  571. if (type == nullptr || is_type_any(type)) {
  572. if (type == nullptr && is_type_untyped_nil(operand->type)) {
  573. error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
  574. operand->mode = Addressing_Invalid;
  575. return;
  576. }
  577. if (type == nullptr && is_type_untyped_undef(operand->type)) {
  578. error(operand->expr, "Use of --- in %.*s", LIT(context_name));
  579. operand->mode = Addressing_Invalid;
  580. return;
  581. }
  582. target_type = default_type(operand->type);
  583. if (type != nullptr && !is_type_any(type)) {
  584. GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
  585. }
  586. add_type_info_type(c, type);
  587. add_type_info_type(c, target_type);
  588. }
  589. convert_to_typed(c, operand, target_type);
  590. if (operand->mode == Addressing_Invalid) {
  591. return;
  592. }
  593. }
  594. if (type == nullptr) {
  595. return;
  596. }
  597. if (operand->mode == Addressing_ProcGroup) {
  598. Array<Entity *> procs = proc_group_entities(c, *operand);
  599. bool good = false;
  600. // NOTE(bill): These should be done
  601. for_array(i, procs) {
  602. Type *t = base_type(procs[i]->type);
  603. if (t == t_invalid) {
  604. continue;
  605. }
  606. Operand x = {};
  607. x.mode = Addressing_Value;
  608. x.type = t;
  609. if (check_is_assignable_to(c, &x, type)) {
  610. Entity *e = procs[i];
  611. add_entity_use(c, operand->expr, e);
  612. good = true;
  613. break;
  614. }
  615. }
  616. if (!good) {
  617. gbString expr_str = expr_to_string(operand->expr);
  618. gbString op_type_str = type_to_string(operand->type);
  619. gbString type_str = type_to_string(type);
  620. defer (gb_string_free(type_str));
  621. defer (gb_string_free(op_type_str));
  622. defer (gb_string_free(expr_str));
  623. // TODO(bill): is this a good enough error message?
  624. error(operand->expr,
  625. "Cannot assign overloaded procedure '%s' to '%s' in %.*s",
  626. expr_str,
  627. op_type_str,
  628. LIT(context_name));
  629. operand->mode = Addressing_Invalid;
  630. }
  631. return;
  632. }
  633. if (check_is_assignable_to(c, operand, type)) {
  634. if (operand->mode == Addressing_Type && is_type_typeid(type)) {
  635. add_type_info_type(c, operand->type);
  636. add_type_and_value(c->info, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
  637. }
  638. } else {
  639. gbString expr_str = expr_to_string(operand->expr);
  640. gbString op_type_str = type_to_string(operand->type);
  641. gbString type_str = type_to_string(type);
  642. defer (gb_string_free(type_str));
  643. defer (gb_string_free(op_type_str));
  644. defer (gb_string_free(expr_str));
  645. switch (operand->mode) {
  646. case Addressing_Builtin:
  647. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  648. error(operand->expr,
  649. "Cannot assign built-in procedure '%s' in %.*s",
  650. expr_str,
  651. LIT(context_name));
  652. break;
  653. case Addressing_Type:
  654. error(operand->expr,
  655. "Cannot assign '%s' which is a type in %.*s",
  656. op_type_str,
  657. LIT(context_name));
  658. break;
  659. default:
  660. // TODO(bill): is this a good enough error message?
  661. error(operand->expr,
  662. "Cannot assign value '%s' of type '%s' to '%s' in %.*s",
  663. expr_str,
  664. op_type_str,
  665. type_str,
  666. LIT(context_name));
  667. check_assignment_error_suggestion(c, operand, type);
  668. break;
  669. }
  670. operand->mode = Addressing_Invalid;
  671. return;
  672. }
  673. }
  674. bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type) {
  675. Operand o = {Addressing_Value};
  676. o.type = source;
  677. switch (poly->kind) {
  678. case Type_Basic:
  679. if (compound) return are_types_identical(poly, source);
  680. return check_is_assignable_to(c, &o, poly);
  681. case Type_Named: {
  682. if (check_type_specialization_to(c, poly, source, compound, modify_type)) {
  683. return true;
  684. }
  685. if (compound || !is_type_generic(poly)) {
  686. return are_types_identical(poly, source);
  687. }
  688. return check_is_assignable_to(c, &o, poly);
  689. }
  690. case Type_Generic: {
  691. if (poly->Generic.specialized != nullptr) {
  692. Type *s = poly->Generic.specialized;
  693. if (!check_type_specialization_to(c, s, source, compound, modify_type)) {
  694. return false;
  695. }
  696. }
  697. if (modify_type) {
  698. Type *ds = default_type(source); // IMPORTANT TODO(bill): IS THIS CORRECT?
  699. gb_memmove(poly, ds, gb_size_of(Type));
  700. }
  701. return true;
  702. }
  703. case Type_Opaque:
  704. if (source->kind == Type_Opaque) {
  705. return is_polymorphic_type_assignable(c, poly->Opaque.elem, source->Opaque.elem, true, modify_type);
  706. }
  707. return false;
  708. case Type_Pointer:
  709. if (source->kind == Type_Pointer) {
  710. return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->Pointer.elem, true, modify_type);
  711. }
  712. return false;
  713. case Type_Array:
  714. if (source->kind == Type_Array) {
  715. // IMPORTANT TODO(bill): Which is correct?
  716. // if (poly->Array.generic_count != nullptr && modify_type) {
  717. if (poly->Array.generic_count != nullptr) {
  718. Type *gt = poly->Array.generic_count;
  719. GB_ASSERT(gt->kind == Type_Generic);
  720. Entity *e = scope_lookup(gt->Generic.scope, gt->Generic.name);
  721. GB_ASSERT(e != nullptr);
  722. if (e->kind == Entity_TypeName) {
  723. poly->Array.generic_count = nullptr;
  724. poly->Array.count = source->Array.count;
  725. e->kind = Entity_Constant;
  726. e->Constant.value = exact_value_i64(source->Array.count);
  727. e->type = t_untyped_integer;
  728. } else if (e->kind == Entity_Constant) {
  729. poly->Array.generic_count = nullptr;
  730. if (e->Constant.value.kind != ExactValue_Integer) {
  731. return false;
  732. }
  733. i64 count = big_int_to_i64(&e->Constant.value.value_integer);
  734. if (count != source->Array.count) {
  735. return false;
  736. }
  737. poly->Array.count = source->Array.count;
  738. } else {
  739. return false;
  740. }
  741. }
  742. if (poly->Array.count == source->Array.count) {
  743. return is_polymorphic_type_assignable(c, poly->Array.elem, source->Array.elem, true, modify_type);
  744. }
  745. }
  746. return false;
  747. case Type_DynamicArray:
  748. if (source->kind == Type_DynamicArray) {
  749. return is_polymorphic_type_assignable(c, poly->DynamicArray.elem, source->DynamicArray.elem, true, modify_type);
  750. }
  751. return false;
  752. case Type_Slice:
  753. if (source->kind == Type_Slice) {
  754. return is_polymorphic_type_assignable(c, poly->Slice.elem, source->Slice.elem, true, modify_type);
  755. }
  756. return false;
  757. case Type_Enum:
  758. return false;
  759. case Type_BitSet:
  760. if (source->kind == Type_BitSet) {
  761. if (!is_polymorphic_type_assignable(c, poly->BitSet.elem, source->BitSet.elem, true, modify_type)) {
  762. return false;
  763. }
  764. if (poly->BitSet.underlying == nullptr) {
  765. if (modify_type) {
  766. poly->BitSet.underlying = source->BitSet.underlying;
  767. }
  768. } else if (!is_polymorphic_type_assignable(c, poly->BitSet.underlying, source->BitSet.underlying, true, modify_type)) {
  769. return false;
  770. }
  771. return true;
  772. }
  773. return false;
  774. case Type_Union:
  775. if (source->kind == Type_Union) {
  776. TypeUnion *x = &poly->Union;
  777. TypeUnion *y = &source->Union;
  778. if (x->variants.count != y->variants.count) {
  779. return false;
  780. }
  781. for_array(i, x->variants) {
  782. Type *a = x->variants[i];
  783. Type *b = y->variants[i];
  784. bool ok = is_polymorphic_type_assignable(c, a, b, false, modify_type);
  785. if (!ok) return false;
  786. }
  787. return true;
  788. }
  789. return false;
  790. case Type_Struct:
  791. if (source->kind == Type_Struct) {
  792. if (poly->Struct.soa_kind == source->Struct.soa_kind &&
  793. poly->Struct.soa_kind != StructSoa_None) {
  794. bool ok = is_polymorphic_type_assignable(c, poly->Struct.soa_elem, source->Struct.soa_elem, true, modify_type);
  795. if (ok) switch (source->Struct.soa_kind) {
  796. case StructSoa_Fixed:
  797. default:
  798. GB_PANIC("Unhandled SOA Kind");
  799. break;
  800. case StructSoa_Slice:
  801. if (modify_type) {
  802. Type *type = make_soa_struct_slice(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
  803. gb_memmove(poly, type, gb_size_of(*type));
  804. }
  805. break;
  806. case StructSoa_Dynamic:
  807. if (modify_type) {
  808. Type *type = make_soa_struct_dynamic_array(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
  809. gb_memmove(poly, type, gb_size_of(*type));
  810. }
  811. break;
  812. }
  813. return ok;
  814. }
  815. // return check_is_assignable_to(c, &o, poly);
  816. }
  817. return false;
  818. case Type_Tuple:
  819. GB_PANIC("This should never happen");
  820. return false;
  821. case Type_Proc:
  822. if (source->kind == Type_Proc) {
  823. // return check_is_assignable_to(c, &o, poly);
  824. // TODO(bill): Polymorphic type assignment
  825. #if 1
  826. TypeProc *x = &poly->Proc;
  827. TypeProc *y = &source->Proc;
  828. if (x->calling_convention != y->calling_convention) {
  829. return false;
  830. }
  831. if (x->c_vararg != y->c_vararg) {
  832. return false;
  833. }
  834. if (x->variadic != y->variadic) {
  835. return false;
  836. }
  837. if (x->param_count != y->param_count) {
  838. return false;
  839. }
  840. if (x->result_count != y->result_count) {
  841. return false;
  842. }
  843. for (isize i = 0; i < x->param_count; i++) {
  844. Entity *a = x->params->Tuple.variables[i];
  845. Entity *b = y->params->Tuple.variables[i];
  846. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  847. if (!ok) return false;
  848. }
  849. for (isize i = 0; i < x->result_count; i++) {
  850. Entity *a = x->results->Tuple.variables[i];
  851. Entity *b = y->results->Tuple.variables[i];
  852. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  853. if (!ok) return false;
  854. }
  855. if (modify_type) {
  856. set_procedure_abi_types(c->allocator, source);
  857. }
  858. return true;
  859. #endif
  860. }
  861. return false;
  862. case Type_Map:
  863. if (source->kind == Type_Map) {
  864. bool key = is_polymorphic_type_assignable(c, poly->Map.key, source->Map.key, true, modify_type);
  865. bool value = is_polymorphic_type_assignable(c, poly->Map.value, source->Map.value, true, modify_type);
  866. return key || value;
  867. }
  868. return false;
  869. }
  870. return false;
  871. }
  872. bool check_cycle(CheckerContext *c, Entity *curr, bool report) {
  873. if (curr->state != EntityState_InProgress) {
  874. return false;
  875. }
  876. for_array(i, *c->type_path) {
  877. Entity *prev = (*c->type_path)[i];
  878. if (prev == curr) {
  879. if (report) {
  880. error(curr->token, "Illegal declaration cycle of `%.*s`", LIT(curr->token.string));
  881. for (isize j = i; j < c->type_path->count; j++) {
  882. Entity *curr = (*c->type_path)[j];
  883. error(curr->token, "\t%.*s refers to", LIT(curr->token.string));
  884. }
  885. error(curr->token, "\t%.*s", LIT(curr->token.string));
  886. }
  887. return true;
  888. }
  889. }
  890. return false;
  891. }
  892. Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) {
  893. GB_ASSERT(n->kind == Ast_Ident);
  894. o->mode = Addressing_Invalid;
  895. o->expr = n;
  896. String name = n->Ident.token.string;
  897. Entity *e = scope_lookup(c->scope, name);
  898. if (e == nullptr) {
  899. if (is_blank_ident(name)) {
  900. error(n, "'_' cannot be used as a value type");
  901. } else {
  902. error(n, "Undeclared name: %.*s", LIT(name));
  903. }
  904. o->type = t_invalid;
  905. o->mode = Addressing_Invalid;
  906. if (named_type != nullptr) {
  907. set_base_type(named_type, t_invalid);
  908. }
  909. return nullptr;
  910. }
  911. if (e->parent_proc_decl != nullptr &&
  912. e->parent_proc_decl != c->curr_proc_decl) {
  913. if (e->kind == Entity_Variable) {
  914. error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
  915. return nullptr;
  916. } else if (e->kind == Entity_Label) {
  917. error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
  918. return nullptr;
  919. }
  920. }
  921. HashKey key = hash_string(e->token.string);
  922. if (e->kind == Entity_ProcGroup) {
  923. auto *pge = &e->ProcGroup;
  924. DeclInfo *d = decl_info_of_entity(e);
  925. check_entity_decl(c, e, d, nullptr);
  926. Array<Entity *> procs = pge->entities;
  927. bool skip = false;
  928. if (type_hint != nullptr) {
  929. // NOTE(bill): These should be done
  930. for_array(i, procs) {
  931. Type *t = base_type(procs[i]->type);
  932. if (t == t_invalid) {
  933. continue;
  934. }
  935. Operand x = {};
  936. x.mode = Addressing_Value;
  937. x.type = t;
  938. if (check_is_assignable_to(c, &x, type_hint)) {
  939. e = procs[i];
  940. add_entity_use(c, n, e);
  941. skip = true;
  942. break;
  943. }
  944. }
  945. }
  946. if (!skip) {
  947. o->mode = Addressing_ProcGroup;
  948. o->type = t_invalid;
  949. o->proc_group = e;
  950. return nullptr;
  951. }
  952. }
  953. add_entity_use(c, n, e);
  954. if (e->state == EntityState_Unresolved) {
  955. check_entity_decl(c, e, nullptr, named_type);
  956. }
  957. if (e->type == nullptr) {
  958. // TODO(bill): Which is correct? return or compiler_error?
  959. // compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
  960. return nullptr;
  961. }
  962. e->flags |= EntityFlag_Used;
  963. Type *type = e->type;
  964. switch (e->kind) {
  965. case Entity_Constant:
  966. if (type == t_invalid) {
  967. o->type = t_invalid;
  968. return e;
  969. }
  970. o->value = e->Constant.value;
  971. if (o->value.kind == ExactValue_Invalid) {
  972. return e;
  973. }
  974. o->mode = Addressing_Constant;
  975. break;
  976. case Entity_Variable:
  977. e->flags |= EntityFlag_Used;
  978. if (type == t_invalid) {
  979. o->type = t_invalid;
  980. return e;
  981. }
  982. o->mode = Addressing_Variable;
  983. if (e->flags & EntityFlag_Value) {
  984. o->mode = Addressing_Value;
  985. }
  986. break;
  987. case Entity_Procedure:
  988. o->mode = Addressing_Value;
  989. break;
  990. case Entity_Builtin:
  991. o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
  992. o->mode = Addressing_Builtin;
  993. break;
  994. case Entity_TypeName:
  995. o->mode = Addressing_Type;
  996. if (check_cycle(c, e, true)) {
  997. type = t_invalid;
  998. }
  999. break;
  1000. case Entity_ImportName:
  1001. if (!allow_import_name) {
  1002. error(n, "Use of import '%.*s' not in selector", LIT(name));
  1003. }
  1004. return e;
  1005. case Entity_LibraryName:
  1006. error(n, "Use of library '%.*s' not in foreign block", LIT(name));
  1007. return e;
  1008. case Entity_Label:
  1009. o->mode = Addressing_NoValue;
  1010. break;
  1011. case Entity_Nil:
  1012. o->mode = Addressing_Value;
  1013. break;
  1014. default:
  1015. compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
  1016. break;
  1017. }
  1018. o->type = type;
  1019. return e;
  1020. }
  1021. bool check_unary_op(CheckerContext *c, Operand *o, Token op) {
  1022. if (o->type == nullptr) {
  1023. gbString str = expr_to_string(o->expr);
  1024. error(o->expr, "Expression has no value '%s'", str);
  1025. gb_string_free(str);
  1026. return false;
  1027. }
  1028. // TODO(bill): Handle errors correctly
  1029. Type *type = base_type(core_array_type(o->type));
  1030. gbString str = nullptr;
  1031. switch (op.kind) {
  1032. case Token_Add:
  1033. case Token_Sub:
  1034. if (!is_type_numeric(type)) {
  1035. str = expr_to_string(o->expr);
  1036. error(op, "Operator '%.*s' is not allowed with '%s'", LIT(op.string), str);
  1037. gb_string_free(str);
  1038. }
  1039. break;
  1040. case Token_Xor:
  1041. if (!is_type_integer(type) && !is_type_boolean(type) && !is_type_bit_set(type)) {
  1042. error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
  1043. }
  1044. break;
  1045. case Token_Not:
  1046. if (!is_type_boolean(type)) {
  1047. str = expr_to_string(o->expr);
  1048. error(op, "Operator '%.*s' is only allowed on boolean expression", LIT(op.string));
  1049. gb_string_free(str);
  1050. }
  1051. break;
  1052. default:
  1053. error(op, "Unknown operator '%.*s'", LIT(op.string));
  1054. return false;
  1055. }
  1056. return true;
  1057. }
  1058. bool check_binary_op(CheckerContext *c, Operand *o, Token op) {
  1059. // TODO(bill): Handle errors correctly
  1060. Type *type = base_type(core_array_type(o->type));
  1061. Type *ct = core_type(type);
  1062. switch (op.kind) {
  1063. case Token_Sub:
  1064. case Token_SubEq:
  1065. if (!is_type_numeric(type)) {
  1066. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1067. return false;
  1068. }
  1069. break;
  1070. case Token_Mul:
  1071. case Token_Quo:
  1072. case Token_MulEq:
  1073. case Token_QuoEq:
  1074. case Token_AddEq:
  1075. if (!is_type_numeric(type)) {
  1076. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1077. return false;
  1078. }
  1079. break;
  1080. case Token_Add:
  1081. if (is_type_string(type)) {
  1082. if (o->mode == Addressing_Constant) {
  1083. return true;
  1084. }
  1085. error(op, "String concatenation is only allowed with constant strings");
  1086. return false;
  1087. } else if (!is_type_numeric(type)) {
  1088. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1089. return false;
  1090. }
  1091. break;
  1092. case Token_And:
  1093. case Token_Or:
  1094. case Token_AndEq:
  1095. case Token_OrEq:
  1096. case Token_Xor:
  1097. case Token_XorEq:
  1098. if (!is_type_integer(ct) && !is_type_boolean(ct) && !is_type_bit_set(ct)) {
  1099. error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
  1100. return false;
  1101. }
  1102. break;
  1103. case Token_Mod:
  1104. case Token_ModMod:
  1105. case Token_ModEq:
  1106. case Token_ModModEq:
  1107. if (!is_type_integer(type)) {
  1108. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1109. return false;
  1110. }
  1111. if (is_type_simd_vector(o->type)) {
  1112. switch (op.kind) {
  1113. case Token_ModMod:
  1114. case Token_ModModEq:
  1115. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1116. return false;
  1117. }
  1118. }
  1119. break;
  1120. case Token_AndNot:
  1121. case Token_AndNotEq:
  1122. if (!is_type_integer(ct) && !is_type_bit_set(ct)) {
  1123. error(op, "Operator '%.*s' is only allowed with integers and bit sets", LIT(op.string));
  1124. return false;
  1125. }
  1126. if (is_type_simd_vector(o->type)) {
  1127. switch (op.kind) {
  1128. case Token_AndNot:
  1129. case Token_AndNotEq:
  1130. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1131. return false;
  1132. }
  1133. }
  1134. break;
  1135. case Token_CmpAnd:
  1136. case Token_CmpOr:
  1137. case Token_CmpAndEq:
  1138. case Token_CmpOrEq:
  1139. if (!is_type_boolean(type)) {
  1140. error(op, "Operator '%.*s' is only allowed with boolean expressions", LIT(op.string));
  1141. return false;
  1142. }
  1143. break;
  1144. default:
  1145. error(op, "Unknown operator '%.*s'", LIT(op.string));
  1146. return false;
  1147. }
  1148. return true;
  1149. }
  1150. bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  1151. if (in_value.kind == ExactValue_Invalid) {
  1152. // NOTE(bill): There's already been an error
  1153. return true;
  1154. }
  1155. type = core_type(type);
  1156. if (type == t_invalid) {
  1157. return false;
  1158. } else if (is_type_boolean(type)) {
  1159. return in_value.kind == ExactValue_Bool;
  1160. } else if (is_type_string(type)) {
  1161. return in_value.kind == ExactValue_String;
  1162. } else if (is_type_integer(type) || is_type_rune(type)) {
  1163. if (in_value.kind == ExactValue_Bool) {
  1164. return false;
  1165. }
  1166. ExactValue v = exact_value_to_integer(in_value);
  1167. if (v.kind != ExactValue_Integer) {
  1168. return false;
  1169. }
  1170. if (out_value) *out_value = v;
  1171. if (is_type_untyped(type)) {
  1172. return true;
  1173. }
  1174. BigInt i = v.value_integer;
  1175. i64 bit_size = type_size_of(type);
  1176. BigInt umax = {};
  1177. BigInt imin = {};
  1178. BigInt imax = {};
  1179. if (bit_size < 16) {
  1180. big_int_from_u64(&umax, unsigned_integer_maxs[bit_size]);
  1181. big_int_from_i64(&imin, signed_integer_mins[bit_size]);
  1182. big_int_from_i64(&imax, signed_integer_maxs[bit_size]);
  1183. } else {
  1184. big_int_from_u64(&umax, 1);
  1185. big_int_from_i64(&imin, 1);
  1186. big_int_from_i64(&imax, 1);
  1187. BigInt bi128 = {};
  1188. BigInt bi127 = {};
  1189. big_int_from_i64(&bi128, 128);
  1190. big_int_from_i64(&bi127, 127);
  1191. big_int_shl_eq(&umax, &bi128);
  1192. big_int_sub_eq(&umax, &BIG_INT_ONE);
  1193. big_int_shl_eq(&imin, &bi127);
  1194. big_int_neg(&imin, &imin);
  1195. big_int_shl_eq(&imax, &bi127);
  1196. big_int_sub_eq(&imax, &BIG_INT_ONE);
  1197. }
  1198. switch (type->Basic.kind) {
  1199. case Basic_rune:
  1200. case Basic_i8:
  1201. case Basic_i16:
  1202. case Basic_i32:
  1203. case Basic_i64:
  1204. case Basic_i128:
  1205. case Basic_int:
  1206. case Basic_i16le:
  1207. case Basic_i32le:
  1208. case Basic_i64le:
  1209. case Basic_i128le:
  1210. case Basic_i16be:
  1211. case Basic_i32be:
  1212. case Basic_i64be:
  1213. case Basic_i128be:
  1214. {
  1215. // return imin <= i && i <= imax;
  1216. int a = big_int_cmp(&imin, &i);
  1217. int b = big_int_cmp(&i, &imax);
  1218. return (a <= 0) && (b <= 0);
  1219. }
  1220. case Basic_u8:
  1221. case Basic_u16:
  1222. case Basic_u32:
  1223. case Basic_u64:
  1224. case Basic_u128:
  1225. case Basic_uint:
  1226. case Basic_uintptr:
  1227. case Basic_u16le:
  1228. case Basic_u32le:
  1229. case Basic_u64le:
  1230. case Basic_u128le:
  1231. case Basic_u16be:
  1232. case Basic_u32be:
  1233. case Basic_u64be:
  1234. case Basic_u128be:
  1235. {
  1236. // return 0ull <= i && i <= umax;
  1237. int b = big_int_cmp(&i, &umax);
  1238. return !i.neg && (b <= 0);
  1239. }
  1240. case Basic_UntypedInteger:
  1241. return true;
  1242. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  1243. }
  1244. } else if (is_type_float(type)) {
  1245. ExactValue v = exact_value_to_float(in_value);
  1246. if (v.kind != ExactValue_Float) {
  1247. return false;
  1248. }
  1249. if (out_value) *out_value = v;
  1250. switch (type->Basic.kind) {
  1251. // case Basic_f16:
  1252. case Basic_f32:
  1253. case Basic_f64:
  1254. return true;
  1255. case Basic_UntypedFloat:
  1256. return true;
  1257. default: GB_PANIC("Compiler error: Unknown float type!"); break;
  1258. }
  1259. } else if (is_type_complex(type)) {
  1260. ExactValue v = exact_value_to_complex(in_value);
  1261. if (v.kind != ExactValue_Complex) {
  1262. return false;
  1263. }
  1264. switch (type->Basic.kind) {
  1265. case Basic_complex64:
  1266. case Basic_complex128: {
  1267. ExactValue real = exact_value_real(v);
  1268. ExactValue imag = exact_value_imag(v);
  1269. if (real.kind != ExactValue_Invalid &&
  1270. imag.kind != ExactValue_Invalid) {
  1271. if (out_value) *out_value = exact_value_complex(exact_value_to_f64(real), exact_value_to_f64(imag));
  1272. return true;
  1273. }
  1274. break;
  1275. }
  1276. case Basic_UntypedComplex:
  1277. return true;
  1278. default: GB_PANIC("Compiler error: Unknown complex type!"); break;
  1279. }
  1280. return false;
  1281. } else if (is_type_quaternion(type)) {
  1282. ExactValue v = exact_value_to_quaternion(in_value);
  1283. if (v.kind != ExactValue_Quaternion) {
  1284. return false;
  1285. }
  1286. switch (type->Basic.kind) {
  1287. case Basic_quaternion128:
  1288. case Basic_quaternion256: {
  1289. ExactValue real = exact_value_real(v);
  1290. ExactValue imag = exact_value_imag(v);
  1291. ExactValue jmag = exact_value_jmag(v);
  1292. ExactValue kmag = exact_value_kmag(v);
  1293. if (real.kind != ExactValue_Invalid &&
  1294. imag.kind != ExactValue_Invalid) {
  1295. if (out_value) *out_value = exact_value_quaternion(exact_value_to_f64(real), exact_value_to_f64(imag), exact_value_to_f64(jmag), exact_value_to_f64(kmag));
  1296. return true;
  1297. }
  1298. break;
  1299. }
  1300. case Basic_UntypedComplex:
  1301. if (out_value) *out_value = exact_value_to_quaternion(*out_value);
  1302. return true;
  1303. case Basic_UntypedQuaternion:
  1304. return true;
  1305. default: GB_PANIC("Compiler error: Unknown complex type!"); break;
  1306. }
  1307. return false;
  1308. } else if (is_type_pointer(type)) {
  1309. if (in_value.kind == ExactValue_Pointer) {
  1310. return true;
  1311. }
  1312. if (in_value.kind == ExactValue_Integer) {
  1313. return false;
  1314. // return true;
  1315. }
  1316. if (in_value.kind == ExactValue_String) {
  1317. return false;
  1318. }
  1319. if (out_value) *out_value = in_value;
  1320. } else if (is_type_bit_set(type)) {
  1321. if (in_value.kind == ExactValue_Integer) {
  1322. return true;
  1323. }
  1324. }
  1325. return false;
  1326. }
  1327. void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type) {
  1328. gbString a = expr_to_string(o->expr);
  1329. gbString b = type_to_string(type);
  1330. defer(
  1331. gb_string_free(b);
  1332. gb_string_free(a);
  1333. );
  1334. Type *src = base_type(o->type);
  1335. Type *dst = base_type(type);
  1336. if (is_type_array(src) && is_type_slice(dst)) {
  1337. Type *s = src->Array.elem;
  1338. Type *d = dst->Slice.elem;
  1339. if (are_types_identical(s, d)) {
  1340. error_line("\tSuggestion: the array expression may be sliced with %s[:]\n", a);
  1341. }
  1342. } else if (are_types_identical(src, dst)) {
  1343. error_line("\tSuggestion: the expression may be directly casted to type %s\n", b);
  1344. } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
  1345. error_line("\tSuggestion: a string may be transmuted to %s\n", b);
  1346. } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string)) {
  1347. error_line("\tSuggestion: the expression may be casted to %s\n", b);
  1348. }
  1349. }
  1350. void check_cast_error_suggestion(CheckerContext *c, Operand *o, Type *type) {
  1351. gbString a = expr_to_string(o->expr);
  1352. gbString b = type_to_string(type);
  1353. defer(
  1354. gb_string_free(b);
  1355. gb_string_free(a);
  1356. );
  1357. Type *src = base_type(o->type);
  1358. Type *dst = base_type(type);
  1359. if (is_type_array(src) && is_type_slice(dst)) {
  1360. Type *s = src->Array.elem;
  1361. Type *d = dst->Slice.elem;
  1362. if (are_types_identical(s, d)) {
  1363. error_line("\tSuggestion: the array expression may be sliced with %s[:]\n", a);
  1364. }
  1365. } else if (is_type_pointer(o->type) && is_type_integer(type)) {
  1366. if (is_type_uintptr(type)) {
  1367. error_line("\tSuggestion: a pointer may be directly casted to %s\n", b);
  1368. } else {
  1369. error_line("\tSuggestion: for a pointer to be casted to an integer, it must be converted to 'uintptr' first\n");
  1370. i64 x = type_size_of(o->type);
  1371. i64 y = type_size_of(type);
  1372. if (x != y) {
  1373. error_line("\tNote: the type of expression and the type of the cast have a different size in bytes, %lld vs %lld\n", x, y);
  1374. }
  1375. }
  1376. } else if (is_type_integer(o->type) && is_type_pointer(type)) {
  1377. if (is_type_uintptr(o->type)) {
  1378. error_line("\tSuggestion: %a may be directly casted to %s\n", a, b);
  1379. } else {
  1380. error_line("\tSuggestion: for an integer to be casted to a pointer, it must be converted to 'uintptr' first\n");
  1381. }
  1382. } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
  1383. error_line("\tSuggestion: a string may be transmuted to %s\n", b);
  1384. } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string)) {
  1385. error_line("\tSuggestion: the expression may be casted to %s\n", b);
  1386. }
  1387. }
  1388. void check_is_expressible(CheckerContext *c, Operand *o, Type *type) {
  1389. GB_ASSERT(o->mode == Addressing_Constant);
  1390. if (!is_type_constant_type(type) || !check_representable_as_constant(c, o->value, type, &o->value)) {
  1391. gbString a = expr_to_string(o->expr);
  1392. gbString b = type_to_string(type);
  1393. defer(
  1394. gb_string_free(b);
  1395. gb_string_free(a);
  1396. o->mode = Addressing_Invalid;
  1397. );
  1398. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1399. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1400. error(o->expr, "'%s' truncated to '%s'", a, b);
  1401. } else {
  1402. error(o->expr, "Cannot convert '%s' to '%s'", a, b);
  1403. check_assignment_error_suggestion(c, o, type);
  1404. }
  1405. } else {
  1406. error(o->expr, "Cannot convert '%s' to '%s'", a, b);
  1407. check_assignment_error_suggestion(c, o, type);
  1408. }
  1409. }
  1410. }
  1411. bool check_is_not_addressable(CheckerContext *c, Operand *o) {
  1412. if (o->mode == Addressing_OptionalOk) {
  1413. Ast *expr = unselector_expr(o->expr);
  1414. if (expr->kind != Ast_TypeAssertion) {
  1415. return true;
  1416. }
  1417. ast_node(ta, TypeAssertion, expr);
  1418. TypeAndValue tv = ta->expr->tav;
  1419. if (is_type_pointer(tv.type)) {
  1420. return false;
  1421. }
  1422. if (is_type_union(tv.type) && tv.mode == Addressing_Variable) {
  1423. return false;
  1424. }
  1425. if (is_type_any(tv.type)) {
  1426. return false;
  1427. }
  1428. return true;
  1429. }
  1430. if (o->mode != Addressing_Variable) {
  1431. return true;
  1432. }
  1433. if (is_type_bit_field_value(o->type)) {
  1434. return true;
  1435. }
  1436. return false;
  1437. }
  1438. void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) {
  1439. switch (op.kind) {
  1440. case Token_And: { // Pointer address
  1441. if (check_is_not_addressable(c, o)) {
  1442. if (ast_node_expect(node, Ast_UnaryExpr)) {
  1443. ast_node(ue, UnaryExpr, node);
  1444. gbString str = expr_to_string(ue->expr);
  1445. defer (gb_string_free(str));
  1446. Entity *e = entity_of_ident(o->expr);
  1447. if (e != nullptr && (e->flags & EntityFlag_Param) != 0) {
  1448. error(op, "Cannot take the pointer address of '%s' which is a procedure parameter", str);
  1449. } else {
  1450. switch (o->mode) {
  1451. case Addressing_SoaVariable:
  1452. error(op, "Cannot take the pointer address of '%s' as it is an indirect index of an SOA struct", str);
  1453. break;
  1454. case Addressing_Constant:
  1455. error(op, "Cannot take the pointer address of '%s' which is a constant", str);
  1456. break;
  1457. default:
  1458. error(op, "Cannot take the pointer address of '%s'", str);
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. o->mode = Addressing_Invalid;
  1464. return;
  1465. }
  1466. o->mode = Addressing_Value;
  1467. o->type = alloc_type_pointer(o->type);
  1468. return;
  1469. }
  1470. }
  1471. if (!check_unary_op(c, o, op)) {
  1472. o->mode = Addressing_Invalid;
  1473. return;
  1474. }
  1475. if (o->mode == Addressing_Constant) {
  1476. Type *type = base_type(o->type);
  1477. if (!is_type_constant_type(o->type)) {
  1478. gbString xt = type_to_string(o->type);
  1479. gbString err_str = expr_to_string(node);
  1480. error(op, "Invalid type, '%s', for constant unary expression '%s'", xt, err_str);
  1481. gb_string_free(err_str);
  1482. gb_string_free(xt);
  1483. o->mode = Addressing_Invalid;
  1484. return;
  1485. }
  1486. if (op.kind == Token_Xor && is_type_untyped(type)) {
  1487. gbString err_str = expr_to_string(node);
  1488. error(op, "Bitwise not cannot be applied to untyped constants '%s'", err_str);
  1489. gb_string_free(err_str);
  1490. o->mode = Addressing_Invalid;
  1491. return;
  1492. }
  1493. if (op.kind == Token_Sub && is_type_unsigned(type)) {
  1494. gbString err_str = expr_to_string(node);
  1495. error(op, "A unsigned constant cannot be negated '%s'", err_str);
  1496. gb_string_free(err_str);
  1497. o->mode = Addressing_Invalid;
  1498. return;
  1499. }
  1500. i32 precision = 0;
  1501. if (is_type_typed(type)) {
  1502. precision = cast(i32)(8 * type_size_of(type));
  1503. }
  1504. bool is_unsigned = is_type_unsigned(type);
  1505. if (is_type_rune(type)) {
  1506. GB_ASSERT(!is_unsigned);
  1507. }
  1508. o->value = exact_unary_operator_value(op.kind, o->value, precision, is_unsigned);
  1509. if (is_type_typed(type)) {
  1510. if (node != nullptr) {
  1511. o->expr = node;
  1512. }
  1513. check_is_expressible(c, o, type);
  1514. }
  1515. return;
  1516. }
  1517. o->mode = Addressing_Value;
  1518. }
  1519. void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
  1520. if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
  1521. bool comp = are_types_identical(x->type, y->type);
  1522. switch (op) {
  1523. case Token_CmpEq: /* comp = comp; */ break;
  1524. case Token_NotEq: comp = !comp; break;
  1525. }
  1526. x->mode = Addressing_Constant;
  1527. x->type = t_untyped_bool;
  1528. x->value = exact_value_bool(comp);
  1529. return;
  1530. }
  1531. if (x->mode == Addressing_Type && is_type_typeid(y->type)) {
  1532. add_type_info_type(c, x->type);
  1533. add_type_info_type(c, y->type);
  1534. add_type_and_value(c->info, x->expr, Addressing_Value, y->type, exact_value_typeid(x->type));
  1535. x->mode = Addressing_Value;
  1536. x->type = t_untyped_bool;
  1537. return;
  1538. } else if (is_type_typeid(x->type) && y->mode == Addressing_Type) {
  1539. add_type_info_type(c, x->type);
  1540. add_type_info_type(c, y->type);
  1541. add_type_and_value(c->info, y->expr, Addressing_Value, x->type, exact_value_typeid(y->type));
  1542. x->mode = Addressing_Value;
  1543. x->type = t_untyped_bool;
  1544. return;
  1545. }
  1546. gbString err_str = nullptr;
  1547. defer (if (err_str != nullptr) {
  1548. gb_string_free(err_str);
  1549. });
  1550. if (check_is_assignable_to(c, x, y->type) ||
  1551. check_is_assignable_to(c, y, x->type)) {
  1552. Type *err_type = x->type;
  1553. bool defined = false;
  1554. switch (op) {
  1555. case Token_CmpEq:
  1556. case Token_NotEq:
  1557. defined = (is_type_comparable(x->type) && is_type_comparable(y->type)) ||
  1558. (is_operand_nil(*x) && type_has_nil(y->type)) ||
  1559. (is_operand_nil(*y) && type_has_nil(x->type));
  1560. break;
  1561. case Token_Lt:
  1562. case Token_Gt:
  1563. case Token_LtEq:
  1564. case Token_GtEq:
  1565. if (are_types_identical(x->type, y->type) && is_type_bit_set(x->type)) {
  1566. defined = true;
  1567. } else {
  1568. defined = is_type_ordered(x->type) && is_type_ordered(y->type);
  1569. }
  1570. break;
  1571. }
  1572. if (!defined) {
  1573. if (x->type == err_type && is_operand_nil(*x)) {
  1574. err_type = y->type;
  1575. }
  1576. gbString type_string = type_to_string(err_type);
  1577. defer (gb_string_free(type_string));
  1578. err_str = gb_string_make(c->allocator,
  1579. gb_bprintf("operator '%.*s' not defined for type '%s'", LIT(token_strings[op]), type_string));
  1580. }
  1581. } else {
  1582. gbString xt, yt;
  1583. if (x->mode == Addressing_ProcGroup) {
  1584. xt = gb_string_make(heap_allocator(), "procedure group");
  1585. } else {
  1586. xt = type_to_string(x->type);
  1587. }
  1588. if (y->mode == Addressing_ProcGroup) {
  1589. yt = gb_string_make(heap_allocator(), "procedure group");
  1590. } else {
  1591. yt = type_to_string(y->type);
  1592. }
  1593. err_str = gb_string_make(c->allocator,
  1594. gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
  1595. gb_string_free(yt);
  1596. gb_string_free(xt);
  1597. }
  1598. if (err_str != nullptr) {
  1599. error(x->expr, "Cannot compare expression, %s", err_str);
  1600. x->type = t_untyped_bool;
  1601. } else {
  1602. if (x->mode == Addressing_Constant &&
  1603. y->mode == Addressing_Constant) {
  1604. if (is_type_constant_type(x->type)) {
  1605. if (is_type_bit_set(x->type)) {
  1606. switch (op) {
  1607. case Token_CmpEq:
  1608. case Token_NotEq:
  1609. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  1610. break;
  1611. case Token_Lt:
  1612. case Token_LtEq:
  1613. {
  1614. ExactValue lhs = x->value;
  1615. ExactValue rhs = y->value;
  1616. ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
  1617. res = exact_value_bool(compare_exact_values(op, res, lhs));
  1618. if (op == Token_Lt) {
  1619. res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
  1620. }
  1621. x->value = res;
  1622. break;
  1623. }
  1624. case Token_Gt:
  1625. case Token_GtEq:
  1626. {
  1627. ExactValue lhs = x->value;
  1628. ExactValue rhs = y->value;
  1629. ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
  1630. res = exact_value_bool(compare_exact_values(op, res, rhs));
  1631. if (op == Token_Gt) {
  1632. res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
  1633. }
  1634. x->value = res;
  1635. break;
  1636. }
  1637. }
  1638. } else {
  1639. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  1640. }
  1641. } else {
  1642. x->mode = Addressing_Value;
  1643. }
  1644. } else {
  1645. x->mode = Addressing_Value;
  1646. update_expr_type(c, x->expr, default_type(x->type), true);
  1647. update_expr_type(c, y->expr, default_type(y->type), true);
  1648. i64 size = 0;
  1649. if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
  1650. if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));
  1651. if (is_type_string(x->type) || is_type_string(y->type)) {
  1652. switch (op) {
  1653. case Token_CmpEq: add_package_dependency(c, "runtime", "string_eq"); break;
  1654. case Token_NotEq: add_package_dependency(c, "runtime", "string_ne"); break;
  1655. case Token_Lt: add_package_dependency(c, "runtime", "string_lt"); break;
  1656. case Token_Gt: add_package_dependency(c, "runtime", "string_gt"); break;
  1657. case Token_LtEq: add_package_dependency(c, "runtime", "string_le"); break;
  1658. case Token_GtEq: add_package_dependency(c, "runtime", "string_gt"); break;
  1659. }
  1660. } else if (is_type_complex(x->type) || is_type_complex(y->type)) {
  1661. switch (op) {
  1662. case Token_CmpEq:
  1663. switch (8*size) {
  1664. case 64: add_package_dependency(c, "runtime", "complex64_eq"); break;
  1665. case 128: add_package_dependency(c, "runtime", "complex128_eq"); break;
  1666. }
  1667. break;
  1668. case Token_NotEq:
  1669. switch (8*size) {
  1670. case 64: add_package_dependency(c, "runtime", "complex64_ne"); break;
  1671. case 128: add_package_dependency(c, "runtime", "complex128_ne"); break;
  1672. }
  1673. break;
  1674. }
  1675. } else if (is_type_quaternion(x->type) || is_type_quaternion(y->type)) {
  1676. switch (op) {
  1677. case Token_CmpEq:
  1678. switch (8*size) {
  1679. case 128: add_package_dependency(c, "runtime", "quaternion128_eq"); break;
  1680. case 256: add_package_dependency(c, "runtime", "quaternion256_eq"); break;
  1681. }
  1682. break;
  1683. case Token_NotEq:
  1684. switch (8*size) {
  1685. case 128: add_package_dependency(c, "runtime", "quaternion128_ne"); break;
  1686. case 256: add_package_dependency(c, "runtime", "quaternion256_ne"); break;
  1687. }
  1688. break;
  1689. }
  1690. }
  1691. }
  1692. x->type = t_untyped_bool;
  1693. }
  1694. }
  1695. void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *type_hint) {
  1696. GB_ASSERT(node->kind == Ast_BinaryExpr);
  1697. ast_node(be, BinaryExpr, node);
  1698. ExactValue x_val = {};
  1699. if (x->mode == Addressing_Constant) {
  1700. x_val = exact_value_to_integer(x->value);
  1701. }
  1702. bool x_is_untyped = is_type_untyped(x->type);
  1703. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  1704. gbString err_str = expr_to_string(x->expr);
  1705. error(node, "Shifted operand '%s' must be an integer", err_str);
  1706. gb_string_free(err_str);
  1707. x->mode = Addressing_Invalid;
  1708. return;
  1709. }
  1710. if (is_type_unsigned(y->type)) {
  1711. } else if (is_type_untyped(y->type)) {
  1712. convert_to_typed(c, y, t_untyped_integer);
  1713. if (y->mode == Addressing_Invalid) {
  1714. x->mode = Addressing_Invalid;
  1715. return;
  1716. }
  1717. } else {
  1718. gbString err_str = expr_to_string(y->expr);
  1719. error(node, "Shift amount '%s' must be an unsigned integer", err_str);
  1720. gb_string_free(err_str);
  1721. x->mode = Addressing_Invalid;
  1722. return;
  1723. }
  1724. if (x->mode == Addressing_Constant) {
  1725. if (y->mode == Addressing_Constant) {
  1726. ExactValue y_val = exact_value_to_integer(y->value);
  1727. if (y_val.kind != ExactValue_Integer) {
  1728. gbString err_str = expr_to_string(y->expr);
  1729. error(node, "Shift amount '%s' must be an unsigned integer", err_str);
  1730. gb_string_free(err_str);
  1731. x->mode = Addressing_Invalid;
  1732. return;
  1733. }
  1734. BigInt max_shift = {};
  1735. big_int_from_u64(&max_shift, 128);
  1736. if (big_int_cmp(&y_val.value_integer, &max_shift) > 0) {
  1737. gbString err_str = expr_to_string(y->expr);
  1738. error(node, "Shift amount too large: '%s'", err_str);
  1739. gb_string_free(err_str);
  1740. x->mode = Addressing_Invalid;
  1741. return;
  1742. }
  1743. if (!is_type_integer(x->type)) {
  1744. // NOTE(bill): It could be an untyped float but still representable
  1745. // as an integer
  1746. x->type = t_untyped_integer;
  1747. }
  1748. x->value = exact_value_shift(be->op.kind, x_val, y_val);
  1749. if (is_type_typed(x->type)) {
  1750. check_is_expressible(c, x, base_type(x->type));
  1751. }
  1752. return;
  1753. }
  1754. TokenPos pos = ast_token(x->expr).pos;
  1755. if (x_is_untyped) {
  1756. ExprInfo *info = check_get_expr_info(&c->checker->info, x->expr);
  1757. if (info != nullptr) {
  1758. info->is_lhs = true;
  1759. }
  1760. x->mode = Addressing_Value;
  1761. if (type_hint && is_type_integer(type_hint)) {
  1762. x->type = type_hint;
  1763. }
  1764. // x->value = x_val;
  1765. return;
  1766. }
  1767. }
  1768. if (y->mode == Addressing_Constant && y->value.value_integer.neg) {
  1769. gbString err_str = expr_to_string(y->expr);
  1770. error(node, "Shift amount cannot be negative: '%s'", err_str);
  1771. gb_string_free(err_str);
  1772. }
  1773. if (!is_type_integer(x->type)) {
  1774. gbString err_str = expr_to_string(y->expr);
  1775. error(node, "Shift operand '%s' must be an integer", err_str);
  1776. gb_string_free(err_str);
  1777. x->mode = Addressing_Invalid;
  1778. return;
  1779. }
  1780. x->mode = Addressing_Value;
  1781. }
  1782. // Operand check_ptr_addition(CheckerContext *c, TokenKind op, Operand *ptr, Operand *offset, Ast *node) {
  1783. // GB_ASSERT(node->kind == Ast_BinaryExpr);
  1784. // ast_node(be, BinaryExpr, node);
  1785. // GB_ASSERT(is_type_pointer(ptr->type));
  1786. // GB_ASSERT(is_type_integer(offset->type));
  1787. // GB_ASSERT(op == Token_Add || op == Token_Sub);
  1788. // Operand operand = {};
  1789. // operand.mode = Addressing_Value;
  1790. // operand.type = ptr->type;
  1791. // operand.expr = node;
  1792. // if (base_type(ptr->type) == t_rawptr) {
  1793. // gbString str = type_to_string(ptr->type);
  1794. // error(node, "Invalid pointer type for pointer arithmetic: '%s'", str);
  1795. // gb_string_free(str);
  1796. // operand.mode = Addressing_Invalid;
  1797. // return operand;
  1798. // }
  1799. // #if defined(NO_POINTER_ARITHMETIC)
  1800. // operand.mode = Addressing_Invalid;
  1801. // error(operand.expr, "Pointer arithmetic is not supported");
  1802. // return operand;
  1803. // #else
  1804. // Type *base_ptr = base_type(ptr->type); GB_ASSERT(base_ptr->kind == Type_Pointer);
  1805. // Type *elem = base_ptr->Pointer.elem;
  1806. // i64 elem_size = type_size_of(elem);
  1807. // if (elem_size <= 0) {
  1808. // gbString str = type_to_string(elem);
  1809. // error(node, "Size of pointer's element type '%s' is zero and cannot be used for pointer arithmetic", str);
  1810. // gb_string_free(str);
  1811. // operand.mode = Addressing_Invalid;
  1812. // return operand;
  1813. // }
  1814. // if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
  1815. // i64 ptr_val = ptr->value.value_pointer;
  1816. // i64 offset_val = exact_value_to_integer(offset->value).value_integer;
  1817. // i64 new_ptr_val = ptr_val;
  1818. // if (op == Token_Add) {
  1819. // new_ptr_val += elem_size*offset_val;
  1820. // } else {
  1821. // new_ptr_val -= elem_size*offset_val;
  1822. // }
  1823. // operand.mode = Addressing_Constant;
  1824. // operand.value = exact_value_pointer(new_ptr_val);
  1825. // }
  1826. // return operand;
  1827. // #endif
  1828. // }
  1829. bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
  1830. if (check_is_assignable_to(c, operand, y)) {
  1831. return true;
  1832. }
  1833. bool is_constant = operand->mode == Addressing_Constant;
  1834. Type *x = operand->type;
  1835. Type *src = core_type(x);
  1836. Type *dst = core_type(y);
  1837. if (are_types_identical(src, dst)) {
  1838. return true;
  1839. }
  1840. if (dst->kind == Type_Array && src->kind == Type_Array) {
  1841. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  1842. return dst->Array.count == src->Array.count;
  1843. }
  1844. }
  1845. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  1846. return are_types_identical(dst->Slice.elem, src->Slice.elem);
  1847. }
  1848. // Cast between booleans and integers
  1849. if (is_type_boolean(src) || is_type_integer(src)) {
  1850. if (is_type_boolean(dst) || is_type_integer(dst)) {
  1851. return true;
  1852. }
  1853. }
  1854. // Cast between numbers
  1855. if (is_type_integer(src) || is_type_float(src)) {
  1856. if (is_type_integer(dst) || is_type_float(dst)) {
  1857. return true;
  1858. }
  1859. }
  1860. if (is_type_integer(src) && is_type_rune(dst)) {
  1861. return true;
  1862. }
  1863. if (is_type_rune(src) && is_type_integer(dst)) {
  1864. return true;
  1865. }
  1866. if (is_type_complex(src) && is_type_complex(dst)) {
  1867. return true;
  1868. }
  1869. if (is_type_complex(src) && is_type_quaternion(dst)) {
  1870. return true;
  1871. }
  1872. if (is_type_quaternion(src) && is_type_quaternion(dst)) {
  1873. return true;
  1874. }
  1875. if (is_type_bit_field_value(src) && is_type_integer(dst)) {
  1876. return true;
  1877. }
  1878. if (is_type_bit_field_value(src) && is_type_boolean(dst)) {
  1879. return src->BitFieldValue.bits == 1;
  1880. }
  1881. // Cast between pointers
  1882. if (is_type_pointer(src) && is_type_pointer(dst)) {
  1883. return true;
  1884. }
  1885. // uintptr <-> pointer
  1886. if (is_type_uintptr(src) && is_type_pointer(dst)) {
  1887. return true;
  1888. }
  1889. if (is_type_pointer(src) && is_type_uintptr(dst)) {
  1890. return true;
  1891. }
  1892. // []byte/[]u8 <-> string (not cstring)
  1893. if (is_type_u8_slice(src) && (is_type_string(dst) && !is_type_cstring(dst))) {
  1894. return true;
  1895. }
  1896. if ((is_type_string(src) && !is_type_cstring(src)) && is_type_u8_slice(dst)) {
  1897. // if (is_type_typed(src)) {
  1898. // return true;
  1899. // }
  1900. }
  1901. // cstring -> string
  1902. if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
  1903. if (operand->mode != Addressing_Constant) {
  1904. add_package_dependency(c, "runtime", "cstring_to_string");
  1905. }
  1906. return true;
  1907. }
  1908. // cstring -> ^u8
  1909. if (are_types_identical(src, t_cstring) && is_type_u8_ptr(dst)) {
  1910. return !is_constant;
  1911. }
  1912. // cstring -> rawptr
  1913. if (are_types_identical(src, t_cstring) && is_type_rawptr(dst)) {
  1914. return !is_constant;
  1915. }
  1916. // ^u8 -> cstring
  1917. if (is_type_u8_ptr(src) && are_types_identical(dst, t_cstring)) {
  1918. return !is_constant;
  1919. }
  1920. // rawptr -> cstring
  1921. if (is_type_rawptr(src) && are_types_identical(dst, t_cstring)) {
  1922. return !is_constant;
  1923. }
  1924. // proc <-> proc
  1925. if (is_type_proc(src) && is_type_proc(dst)) {
  1926. return true;
  1927. }
  1928. // proc -> rawptr
  1929. if (is_type_proc(src) && is_type_rawptr(dst)) {
  1930. return true;
  1931. }
  1932. // rawptr -> proc
  1933. if (is_type_rawptr(src) && is_type_proc(dst)) {
  1934. return true;
  1935. }
  1936. if (is_type_opaque(src)) {
  1937. return are_types_identical(dst, src->Opaque.elem);
  1938. }
  1939. if (is_type_opaque(dst)) {
  1940. return are_types_identical(dst->Opaque.elem, src);
  1941. }
  1942. return false;
  1943. }
  1944. bool check_cast_internal(CheckerContext *c, Operand *x, Type *type) {
  1945. bool is_const_expr = x->mode == Addressing_Constant;
  1946. bool can_convert = false;
  1947. Type *bt = base_type(type);
  1948. if (is_const_expr && is_type_constant_type(bt)) {
  1949. if (core_type(bt)->kind == Type_Basic) {
  1950. if (check_representable_as_constant(c, x->value, bt, &x->value)) {
  1951. return true;
  1952. } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
  1953. return true;
  1954. }
  1955. }
  1956. } else if (check_is_castable_to(c, x, type)) {
  1957. if (x->mode != Addressing_Constant) {
  1958. x->mode = Addressing_Value;
  1959. } else if (is_type_slice(type) && is_type_string(x->type)) {
  1960. x->mode = Addressing_Value;
  1961. } else if (is_type_union(type)) {
  1962. x->mode = Addressing_Value;
  1963. }
  1964. return true;
  1965. }
  1966. return false;
  1967. }
  1968. void check_cast(CheckerContext *c, Operand *x, Type *type) {
  1969. if (!is_operand_value(*x)) {
  1970. error(x->expr, "Only values can be casted");
  1971. x->mode = Addressing_Invalid;
  1972. return;
  1973. }
  1974. bool is_const_expr = x->mode == Addressing_Constant;
  1975. bool can_convert = check_cast_internal(c, x, type);
  1976. if (!can_convert) {
  1977. gbString expr_str = expr_to_string(x->expr);
  1978. gbString to_type = type_to_string(type);
  1979. gbString from_type = type_to_string(x->type);
  1980. error(x->expr, "Cannot cast '%s' as '%s' from '%s'", expr_str, to_type, from_type);
  1981. gb_string_free(from_type);
  1982. gb_string_free(to_type);
  1983. gb_string_free(expr_str);
  1984. check_cast_error_suggestion(c, x, type);
  1985. x->mode = Addressing_Invalid;
  1986. return;
  1987. }
  1988. if (is_type_untyped(x->type)) {
  1989. Type *final_type = type;
  1990. if (is_const_expr && !is_type_constant_type(type)) {
  1991. final_type = default_type(x->type);
  1992. }
  1993. update_expr_type(c, x->expr, final_type, true);
  1994. }
  1995. x->type = type;
  1996. }
  1997. bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type *t) {
  1998. if (!is_operand_value(*o)) {
  1999. error(o->expr, "'transmute' can only be applied to values");
  2000. o->mode = Addressing_Invalid;
  2001. return false;
  2002. }
  2003. if (o->mode == Addressing_Constant) {
  2004. gbString expr_str = expr_to_string(o->expr);
  2005. error(o->expr, "Cannot transmute a constant expression: '%s'", expr_str);
  2006. gb_string_free(expr_str);
  2007. o->mode = Addressing_Invalid;
  2008. o->expr = node;
  2009. return false;
  2010. }
  2011. if (is_type_untyped(o->type)) {
  2012. gbString expr_str = expr_to_string(o->expr);
  2013. error(o->expr, "Cannot transmute untyped expression: '%s'", expr_str);
  2014. gb_string_free(expr_str);
  2015. o->mode = Addressing_Invalid;
  2016. o->expr = node;
  2017. return false;
  2018. }
  2019. i64 srcz = type_size_of(o->type);
  2020. i64 dstz = type_size_of(t);
  2021. if (srcz != dstz) {
  2022. gbString expr_str = expr_to_string(o->expr);
  2023. gbString type_str = type_to_string(t);
  2024. error(o->expr, "Cannot transmute '%s' to '%s', %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  2025. gb_string_free(type_str);
  2026. gb_string_free(expr_str);
  2027. o->mode = Addressing_Invalid;
  2028. o->expr = node;
  2029. return false;
  2030. }
  2031. o->mode = Addressing_Value;
  2032. o->type = t;
  2033. return true;
  2034. }
  2035. bool check_binary_array_expr(CheckerContext *c, Token op, Operand *x, Operand *y) {
  2036. if (is_type_array(x->type) && !is_type_array(y->type)) {
  2037. if (check_is_assignable_to(c, y, x->type)) {
  2038. if (check_binary_op(c, x, op)) {
  2039. return true;
  2040. }
  2041. }
  2042. }
  2043. return false;
  2044. }
  2045. void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Type *type_hint, bool use_lhs_as_type_hint=false) {
  2046. GB_ASSERT(node->kind == Ast_BinaryExpr);
  2047. Operand y_ = {}, *y = &y_;
  2048. ast_node(be, BinaryExpr, node);
  2049. defer({
  2050. node->viral_state_flags |= be->left->viral_state_flags;
  2051. node->viral_state_flags |= be->right->viral_state_flags;
  2052. });
  2053. Token op = be->op;
  2054. switch (op.kind) {
  2055. case Token_CmpEq:
  2056. case Token_NotEq: {
  2057. // NOTE(bill): Allow comparisons between types
  2058. check_expr_or_type(c, x, be->left, type_hint);
  2059. check_expr_or_type(c, y, be->right, x->type);
  2060. bool xt = x->mode == Addressing_Type;
  2061. bool yt = y->mode == Addressing_Type;
  2062. // If only one is a type, this is an error
  2063. if (xt ^ yt) {
  2064. GB_ASSERT(xt != yt);
  2065. if (xt) {
  2066. if (!is_type_typeid(y->type)) {
  2067. error_operand_not_expression(x);
  2068. }
  2069. }
  2070. if (yt) {
  2071. if (!is_type_typeid(x->type)) {
  2072. error_operand_not_expression(y);
  2073. }
  2074. }
  2075. }
  2076. break;
  2077. }
  2078. case Token_in:
  2079. case Token_not_in:
  2080. // IMPORTANT NOTE(bill): This uses right-left evaluation in type checking only no in
  2081. check_expr(c, y, be->right);
  2082. if (is_type_bit_set(y->type)) {
  2083. Type *elem = base_type(y->type)->BitSet.elem;
  2084. check_expr_with_type_hint(c, x, be->left, elem);
  2085. } else if (is_type_map(y->type)) {
  2086. Type *key = base_type(y->type)->Map.key;
  2087. check_expr_with_type_hint(c, x, be->left, key);
  2088. } else {
  2089. check_expr(c, x, be->left);
  2090. }
  2091. if (x->mode == Addressing_Invalid) {
  2092. return;
  2093. }
  2094. if (y->mode == Addressing_Invalid) {
  2095. x->mode = Addressing_Invalid;
  2096. x->expr = y->expr;
  2097. return;
  2098. }
  2099. if (is_type_map(y->type)) {
  2100. Type *yt = base_type(y->type);
  2101. if (op.kind == Token_in) {
  2102. check_assignment(c, x, yt->Map.key, str_lit("map 'in'"));
  2103. } else {
  2104. check_assignment(c, x, yt->Map.key, str_lit("map 'not_in'"));
  2105. }
  2106. add_package_dependency(c, "runtime", "__dynamic_map_get");
  2107. } else if (is_type_bit_set(y->type)) {
  2108. Type *yt = base_type(y->type);
  2109. if (op.kind == Token_in) {
  2110. check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'"));
  2111. } else {
  2112. check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'not_in'"));
  2113. }
  2114. if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) {
  2115. ExactValue k = exact_value_to_integer(x->value);
  2116. ExactValue v = exact_value_to_integer(y->value);
  2117. GB_ASSERT(k.kind == ExactValue_Integer);
  2118. GB_ASSERT(v.kind == ExactValue_Integer);
  2119. i64 key = big_int_to_i64(&k.value_integer);
  2120. i64 lower = yt->BitSet.lower;
  2121. i64 upper = yt->BitSet.upper;
  2122. if (lower <= key && key <= upper) {
  2123. i64 bit = 1ll<<key;
  2124. i64 bits = big_int_to_i64(&v.value_integer);
  2125. x->mode = Addressing_Constant;
  2126. x->type = t_untyped_bool;
  2127. if (op.kind == Token_in) {
  2128. x->value = exact_value_bool((bit & bits) != 0);
  2129. } else {
  2130. x->value = exact_value_bool((bit & bits) == 0);
  2131. }
  2132. x->expr = node;
  2133. return;
  2134. } else {
  2135. error(x->expr, "key '%lld' out of range of bit set, %lld..%lld", key, lower, upper);
  2136. x->mode = Addressing_Invalid;
  2137. }
  2138. }
  2139. } else {
  2140. gbString t = type_to_string(y->type);
  2141. error(x->expr, "expected either a map or bitset for 'in', got %s", t);
  2142. gb_string_free(t);
  2143. x->expr = node;
  2144. x->mode = Addressing_Invalid;
  2145. return;
  2146. }
  2147. if (x->mode != Addressing_Invalid) {
  2148. x->mode = Addressing_Value;
  2149. x->type = t_untyped_bool;
  2150. }
  2151. x->expr = node;
  2152. return;
  2153. default:
  2154. check_expr_with_type_hint(c, x, be->left, type_hint);
  2155. if (use_lhs_as_type_hint) {
  2156. check_expr_with_type_hint(c, y, be->right, x->type);
  2157. } else {
  2158. check_expr_with_type_hint(c, y, be->right, type_hint);
  2159. }
  2160. break;
  2161. }
  2162. if (x->mode == Addressing_Invalid) {
  2163. return;
  2164. }
  2165. if (y->mode == Addressing_Invalid) {
  2166. x->mode = Addressing_Invalid;
  2167. x->expr = y->expr;
  2168. return;
  2169. }
  2170. if (x->mode == Addressing_Builtin) {
  2171. x->mode = Addressing_Invalid;
  2172. error(x->expr, "built-in expression in binary expression");
  2173. return;
  2174. }
  2175. if (y->mode == Addressing_Builtin) {
  2176. x->mode = Addressing_Invalid;
  2177. error(y->expr, "built-in expression in binary expression");
  2178. return;
  2179. }
  2180. if (token_is_shift(op.kind)) {
  2181. check_shift(c, x, y, node, type_hint);
  2182. return;
  2183. }
  2184. // if (op.kind == Token_Add || op.kind == Token_Sub) {
  2185. // if (is_type_pointer(x->type) && is_type_integer(y->type)) {
  2186. // *x = check_ptr_addition(c, op.kind, x, y, node);
  2187. // return;
  2188. // } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
  2189. // if (op.kind == Token_Sub) {
  2190. // gbString lhs = expr_to_string(x->expr);
  2191. // gbString rhs = expr_to_string(y->expr);
  2192. // error(node, "Invalid pointer arithmetic, did you mean '%s %.*s %s'?", rhs, LIT(op.string), lhs);
  2193. // gb_string_free(rhs);
  2194. // gb_string_free(lhs);
  2195. // x->mode = Addressing_Invalid;
  2196. // return;
  2197. // }
  2198. // *x = check_ptr_addition(c, op.kind, y, x, node);
  2199. // return;
  2200. // }
  2201. // }
  2202. convert_to_typed(c, x, y->type);
  2203. if (x->mode == Addressing_Invalid) {
  2204. return;
  2205. }
  2206. convert_to_typed(c, y, x->type);
  2207. if (y->mode == Addressing_Invalid) {
  2208. x->mode = Addressing_Invalid;
  2209. return;
  2210. }
  2211. if (token_is_comparison(op.kind)) {
  2212. check_comparison(c, x, y, op.kind);
  2213. return;
  2214. }
  2215. if (check_binary_array_expr(c, op, x, y)) {
  2216. x->mode = Addressing_Value;
  2217. x->type = x->type;
  2218. return;
  2219. }
  2220. if (check_binary_array_expr(c, op, y, x)) {
  2221. x->mode = Addressing_Value;
  2222. x->type = y->type;
  2223. return;
  2224. }
  2225. if (!are_types_identical(x->type, y->type)) {
  2226. if (x->type != t_invalid &&
  2227. y->type != t_invalid) {
  2228. gbString xt = type_to_string(x->type);
  2229. gbString yt = type_to_string(y->type);
  2230. gbString expr_str = expr_to_string(x->expr);
  2231. error(op, "Mismatched types in binary expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
  2232. gb_string_free(expr_str);
  2233. gb_string_free(yt);
  2234. gb_string_free(xt);
  2235. }
  2236. x->mode = Addressing_Invalid;
  2237. return;
  2238. }
  2239. if (!check_binary_op(c, x, op)) {
  2240. x->mode = Addressing_Invalid;
  2241. return;
  2242. }
  2243. switch (op.kind) {
  2244. case Token_Quo:
  2245. case Token_Mod:
  2246. case Token_ModMod:
  2247. case Token_QuoEq:
  2248. case Token_ModEq:
  2249. case Token_ModModEq:
  2250. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  2251. y->mode == Addressing_Constant) {
  2252. bool fail = false;
  2253. switch (y->value.kind) {
  2254. case ExactValue_Integer:
  2255. if (big_int_is_zero(&y->value.value_integer)) {
  2256. fail = true;
  2257. }
  2258. break;
  2259. case ExactValue_Float:
  2260. if (y->value.value_float == 0.0) {
  2261. fail = true;
  2262. }
  2263. break;
  2264. }
  2265. if (fail) {
  2266. error(y->expr, "Division by zero not allowed");
  2267. x->mode = Addressing_Invalid;
  2268. return;
  2269. }
  2270. }
  2271. break;
  2272. case Token_CmpAnd:
  2273. case Token_CmpOr:
  2274. if (be->left->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
  2275. error(be->left, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
  2276. }
  2277. if (be->right->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
  2278. error(be->right, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
  2279. }
  2280. break;
  2281. }
  2282. if (x->mode == Addressing_Constant &&
  2283. y->mode == Addressing_Constant) {
  2284. ExactValue a = x->value;
  2285. ExactValue b = y->value;
  2286. // Type *type = base_type(x->type);
  2287. Type *type = x->type;
  2288. if (is_type_pointer(type)) {
  2289. GB_ASSERT(op.kind == Token_Sub);
  2290. i64 bytes = a.value_pointer - b.value_pointer;
  2291. i64 diff = bytes/type_size_of(type);
  2292. x->value = exact_value_pointer(diff);
  2293. return;
  2294. }
  2295. if (!is_type_constant_type(type)) {
  2296. gbString xt = type_to_string(x->type);
  2297. gbString err_str = expr_to_string(node);
  2298. error(op, "Invalid type, '%s', for constant binary expression '%s'", xt, err_str);
  2299. gb_string_free(err_str);
  2300. gb_string_free(xt);
  2301. x->mode = Addressing_Invalid;
  2302. return;
  2303. }
  2304. if (op.kind == Token_Quo && is_type_integer(type)) {
  2305. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  2306. }
  2307. x->value = exact_binary_operator_value(op.kind, a, b);
  2308. if (is_type_typed(type)) {
  2309. if (node != nullptr) {
  2310. x->expr = node;
  2311. }
  2312. check_is_expressible(c, x, type);
  2313. }
  2314. return;
  2315. } else if (is_type_string(x->type)) {
  2316. error(node, "String concatenation is only allowed with constant strings");
  2317. x->mode = Addressing_Invalid;
  2318. return;
  2319. }
  2320. if (op.kind == Token_Quo || op.kind == Token_QuoEq) {
  2321. Type *bt = base_type(x->type);
  2322. if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
  2323. case Basic_complex64: add_package_dependency(c, "runtime", "quo_complex64"); break;
  2324. case Basic_complex128: add_package_dependency(c, "runtime", "quo_complex128"); break;
  2325. case Basic_quaternion128: add_package_dependency(c, "runtime", "quo_quaternion128"); break;
  2326. case Basic_quaternion256: add_package_dependency(c, "runtime", "quo_quaternion256"); break;
  2327. }
  2328. } else if (op.kind == Token_Mul || op.kind == Token_MulEq) {
  2329. Type *bt = base_type(x->type);
  2330. if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
  2331. case Basic_quaternion128: add_package_dependency(c, "runtime", "mul_quaternion128"); break;
  2332. case Basic_quaternion256: add_package_dependency(c, "runtime", "mul_quaternion256"); break;
  2333. }
  2334. }
  2335. x->mode = Addressing_Value;
  2336. }
  2337. void update_expr_type(CheckerContext *c, Ast *e, Type *type, bool final) {
  2338. ExprInfo *found = check_get_expr_info(&c->checker->info, e);
  2339. if (found == nullptr) {
  2340. return;
  2341. }
  2342. ExprInfo old = *found;
  2343. switch (e->kind) {
  2344. case_ast_node(ue, UnaryExpr, e);
  2345. if (old.value.kind != ExactValue_Invalid) {
  2346. // NOTE(bill): if 'e' is constant, the operands will be constant too.
  2347. // They don't need to be updated as they will be updated later and
  2348. // checked at the end of general checking stage.
  2349. break;
  2350. }
  2351. update_expr_type(c, ue->expr, type, final);
  2352. case_end;
  2353. case_ast_node(be, BinaryExpr, e);
  2354. if (old.value.kind != ExactValue_Invalid) {
  2355. // See above note in UnaryExpr case
  2356. break;
  2357. }
  2358. if (token_is_comparison(be->op.kind)) {
  2359. // NOTE(bill): Do nothing as the types are fine
  2360. } else if (token_is_shift(be->op.kind)) {
  2361. update_expr_type(c, be->left, type, final);
  2362. } else {
  2363. update_expr_type(c, be->left, type, final);
  2364. update_expr_type(c, be->right, type, final);
  2365. }
  2366. case_end;
  2367. case_ast_node(pe, ParenExpr, e);
  2368. update_expr_type(c, pe->expr, type, final);
  2369. case_end;
  2370. }
  2371. if (!final && is_type_untyped(type)) {
  2372. old.type = base_type(type);
  2373. check_set_expr_info(&c->checker->info, e, old);
  2374. return;
  2375. }
  2376. // We need to remove it and then give it a new one
  2377. check_remove_expr_info(&c->checker->info, e);
  2378. if (old.is_lhs && !is_type_integer(type)) {
  2379. gbString expr_str = expr_to_string(e);
  2380. gbString type_str = type_to_string(type);
  2381. error(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  2382. gb_string_free(type_str);
  2383. gb_string_free(expr_str);
  2384. return;
  2385. }
  2386. add_type_and_value(&c->checker->info, e, old.mode, type, old.value);
  2387. }
  2388. void update_expr_value(CheckerContext *c, Ast *e, ExactValue value) {
  2389. ExprInfo *found = check_get_expr_info(&c->checker->info, e);
  2390. if (found) {
  2391. found->value = value;
  2392. }
  2393. }
  2394. void convert_untyped_error(CheckerContext *c, Operand *operand, Type *target_type) {
  2395. gbString expr_str = expr_to_string(operand->expr);
  2396. gbString type_str = type_to_string(target_type);
  2397. char const *extra_text = "";
  2398. if (operand->mode == Addressing_Constant) {
  2399. if (big_int_is_zero(&operand->value.value_integer)) {
  2400. if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
  2401. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  2402. extra_text = " - Did you want 'nil'?";
  2403. }
  2404. }
  2405. }
  2406. error(operand->expr, "Cannot convert '%s' to '%s'%s", expr_str, type_str, extra_text);
  2407. gb_string_free(type_str);
  2408. gb_string_free(expr_str);
  2409. operand->mode = Addressing_Invalid;
  2410. }
  2411. ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
  2412. Type *t = core_type(type);
  2413. if (is_type_boolean(t)) {
  2414. // v = exact_value_to_boolean(v);
  2415. } else if (is_type_float(t)) {
  2416. v = exact_value_to_float(v);
  2417. } else if (is_type_integer(t)) {
  2418. v = exact_value_to_integer(v);
  2419. } else if (is_type_pointer(t)) {
  2420. v = exact_value_to_integer(v);
  2421. } else if (is_type_complex(t)) {
  2422. v = exact_value_to_complex(v);
  2423. } else if (is_type_quaternion(t)) {
  2424. v = exact_value_to_quaternion(v);
  2425. }
  2426. return v;
  2427. }
  2428. Type *check_assignment_bit_field(CheckerContext *ctx, Operand *operand, Type *target_type) {
  2429. if (is_type_bit_field_value(target_type)) {
  2430. Type *lt = base_type(target_type);
  2431. i64 lhs_bits = lt->BitFieldValue.bits;
  2432. if (operand->mode == Addressing_Constant) {
  2433. ExactValue v = exact_value_to_integer(operand->value);
  2434. if (v.kind == ExactValue_Integer) {
  2435. BigInt i = v.value_integer;
  2436. if (!i.neg) {
  2437. u64 imax_ = ~cast(u64)0ull;
  2438. if (lhs_bits < 64) {
  2439. imax_ = (1ull << cast(u64)lhs_bits) - 1ull;
  2440. }
  2441. BigInt imax = big_int_make_u64(imax_);
  2442. if (big_int_cmp(&i, &imax) <= 0) {
  2443. return operand->type;
  2444. }
  2445. }
  2446. } else if (operand->value.kind == ExactValue_Bool) {
  2447. bool b = operand->value.value_bool;
  2448. if (lhs_bits == 1) {
  2449. return operand->type;
  2450. }
  2451. }
  2452. } else if (is_type_integer(operand->type)) {
  2453. // TODO(bill): Any other checks?
  2454. return operand->type;
  2455. } else if (is_type_boolean(operand->type)) {
  2456. if (lhs_bits == 1) {
  2457. return operand->type;
  2458. }
  2459. }
  2460. return nullptr;
  2461. }
  2462. return nullptr;
  2463. }
  2464. void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
  2465. GB_ASSERT_NOT_NULL(target_type);
  2466. if (operand->mode == Addressing_Invalid ||
  2467. operand->mode == Addressing_Type ||
  2468. is_type_typed(operand->type) ||
  2469. target_type == t_invalid) {
  2470. return;
  2471. }
  2472. if (is_type_untyped(target_type)) {
  2473. GB_ASSERT(operand->type->kind == Type_Basic);
  2474. GB_ASSERT(target_type->kind == Type_Basic);
  2475. BasicKind x_kind = operand->type->Basic.kind;
  2476. BasicKind y_kind = target_type->Basic.kind;
  2477. if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
  2478. if (x_kind < y_kind) {
  2479. operand->type = target_type;
  2480. update_expr_type(c, operand->expr, target_type, false);
  2481. }
  2482. } else if (x_kind != y_kind) {
  2483. operand->mode = Addressing_Invalid;
  2484. convert_untyped_error(c, operand, target_type);
  2485. return;
  2486. }
  2487. return;
  2488. }
  2489. Type *t = base_type(target_type);
  2490. if (c->in_enum_type) {
  2491. t = core_type(target_type);
  2492. }
  2493. switch (t->kind) {
  2494. case Type_Basic:
  2495. if (operand->mode == Addressing_Constant) {
  2496. check_is_expressible(c, operand, t);
  2497. if (operand->mode == Addressing_Invalid) {
  2498. return;
  2499. }
  2500. update_expr_value(c, operand->expr, operand->value);
  2501. } else {
  2502. switch (operand->type->Basic.kind) {
  2503. case Basic_UntypedBool:
  2504. if (!is_type_boolean(target_type)) {
  2505. operand->mode = Addressing_Invalid;
  2506. convert_untyped_error(c, operand, target_type);
  2507. return;
  2508. }
  2509. break;
  2510. case Basic_UntypedInteger:
  2511. case Basic_UntypedFloat:
  2512. case Basic_UntypedComplex:
  2513. case Basic_UntypedQuaternion:
  2514. case Basic_UntypedRune:
  2515. if (!is_type_numeric(target_type)) {
  2516. operand->mode = Addressing_Invalid;
  2517. convert_untyped_error(c, operand, target_type);
  2518. return;
  2519. }
  2520. break;
  2521. case Basic_UntypedNil:
  2522. if (is_type_any(target_type)) {
  2523. target_type = t_untyped_nil;
  2524. } else if (is_type_cstring(target_type)) {
  2525. target_type = t_untyped_nil;
  2526. } else if (!type_has_nil(target_type)) {
  2527. operand->mode = Addressing_Invalid;
  2528. convert_untyped_error(c, operand, target_type);
  2529. return;
  2530. }
  2531. break;
  2532. }
  2533. }
  2534. break;
  2535. case Type_Array: {
  2536. Type *elem = base_array_type(t);
  2537. if (check_is_assignable_to(c, operand, elem)) {
  2538. operand->mode = Addressing_Value;
  2539. } else {
  2540. operand->mode = Addressing_Invalid;
  2541. convert_untyped_error(c, operand, target_type);
  2542. return;
  2543. }
  2544. break;
  2545. }
  2546. case Type_BitFieldValue: {
  2547. Type *res = check_assignment_bit_field(c, operand, target_type);
  2548. if (res == nullptr) {
  2549. convert_untyped_error(c, operand, target_type);
  2550. }
  2551. break;
  2552. }
  2553. case Type_Union:
  2554. if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) {
  2555. isize count = t->Union.variants.count;
  2556. ValidIndexAndScore *valids = gb_alloc_array(c->allocator, ValidIndexAndScore, count);
  2557. defer (gb_free(c->allocator, valids));
  2558. isize valid_count = 0;
  2559. isize first_success_index = -1;
  2560. for_array(i, t->Union.variants) {
  2561. Type *vt = t->Union.variants[i];
  2562. i64 score = 0;
  2563. if (check_is_assignable_to_with_score(c, operand, vt, &score)) {
  2564. valids[valid_count].index = i;
  2565. valids[valid_count].score = score;
  2566. valid_count += 1;
  2567. if (first_success_index < 0) {
  2568. first_success_index = i;
  2569. }
  2570. }
  2571. }
  2572. if (valid_count > 1) {
  2573. gb_sort_array(valids, valid_count, valid_index_and_score_cmp);
  2574. i64 best_score = valids[0].score;
  2575. Type *best_type = t->Union.variants[valids[0].index];
  2576. for (isize i = 1; i < valid_count; i++) {
  2577. auto v = valids[i];
  2578. Type *vt = t->Union.variants[v.index];
  2579. if (best_score > v.score) {
  2580. valid_count = i;
  2581. break;
  2582. }
  2583. best_score = v.score;
  2584. }
  2585. first_success_index = valids[0].index;
  2586. }
  2587. gbString type_str = type_to_string(target_type);
  2588. defer (gb_string_free(type_str));
  2589. if (valid_count == 1) {
  2590. operand->mode = Addressing_Value;
  2591. operand->type = t->Union.variants[first_success_index];
  2592. target_type = t->Union.variants[first_success_index];
  2593. break;
  2594. } else if (valid_count > 1) {
  2595. begin_error_block();
  2596. defer (end_error_block());
  2597. GB_ASSERT(first_success_index >= 0);
  2598. operand->mode = Addressing_Invalid;
  2599. convert_untyped_error(c, operand, target_type);
  2600. error_line("Ambiguous type conversion to '%s', which variant did you mean:\n\t", type_str);
  2601. i32 j = 0;
  2602. for (i32 i = 0; i < valid_count; i++) {
  2603. ValidIndexAndScore valid = valids[i];
  2604. if (j > 0 && valid_count > 2) error_line(", ");
  2605. if (j == valid_count-1) {
  2606. if (valid_count == 2) error_line(" ");
  2607. error_line("or ");
  2608. }
  2609. gbString str = type_to_string(t->Union.variants[valid.index]);
  2610. error_line("'%s'", str);
  2611. gb_string_free(str);
  2612. j++;
  2613. }
  2614. error_line("\n\n");
  2615. return;
  2616. } else if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
  2617. target_type = t_untyped_undef;
  2618. } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  2619. begin_error_block();
  2620. defer (end_error_block());
  2621. operand->mode = Addressing_Invalid;
  2622. convert_untyped_error(c, operand, target_type);
  2623. if (count > 0) {
  2624. error_line("'%s' is a union which only excepts the following types:\n", type_str);
  2625. error_line("\t");
  2626. for (i32 i = 0; i < count; i++) {
  2627. Type *v = t->Union.variants[i];
  2628. if (i > 0 && count > 2) error_line(", ");
  2629. if (i == count-1) {
  2630. if (count == 2) error_line(" ");
  2631. error_line("or ");
  2632. }
  2633. gbString str = type_to_string(v);
  2634. error_line("'%s'", str);
  2635. gb_string_free(str);
  2636. }
  2637. error_line("\n\n");
  2638. }
  2639. return;
  2640. }
  2641. }
  2642. /* fallthrough */
  2643. default:
  2644. if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
  2645. target_type = t_untyped_undef;
  2646. } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) {
  2647. target_type = t_untyped_nil;
  2648. } else {
  2649. operand->mode = Addressing_Invalid;
  2650. convert_untyped_error(c, operand, target_type);
  2651. return;
  2652. }
  2653. break;
  2654. }
  2655. operand->type = target_type;
  2656. update_expr_type(c, operand->expr, target_type, true);
  2657. }
  2658. bool check_index_value(CheckerContext *c, bool open_range, Ast *index_value, i64 max_count, i64 *value, Type *type_hint=nullptr) {
  2659. Operand operand = {Addressing_Invalid};
  2660. check_expr_with_type_hint(c, &operand, index_value, type_hint);
  2661. if (operand.mode == Addressing_Invalid) {
  2662. if (value) *value = 0;
  2663. return false;
  2664. }
  2665. Type *index_type = t_int;
  2666. if (type_hint != nullptr) {
  2667. index_type = type_hint;
  2668. }
  2669. convert_to_typed(c, &operand, index_type);
  2670. if (operand.mode == Addressing_Invalid) {
  2671. if (value) *value = 0;
  2672. return false;
  2673. }
  2674. if (type_hint != nullptr) {
  2675. if (!check_is_assignable_to(c, &operand, type_hint)) {
  2676. gbString expr_str = expr_to_string(operand.expr);
  2677. gbString index_type_str = type_to_string(type_hint);
  2678. error(operand.expr, "Index '%s' must be an enum of type '%s'", expr_str, index_type_str);
  2679. gb_string_free(index_type_str);
  2680. gb_string_free(expr_str);
  2681. if (value) *value = 0;
  2682. return false;
  2683. }
  2684. } else if (!is_type_integer(operand.type) && !is_type_enum(operand.type)) {
  2685. gbString expr_str = expr_to_string(operand.expr);
  2686. error(operand.expr, "Index '%s' must be an integer", expr_str);
  2687. gb_string_free(expr_str);
  2688. if (value) *value = 0;
  2689. return false;
  2690. }
  2691. if (operand.mode == Addressing_Constant &&
  2692. (c->state_flags & StateFlag_no_bounds_check) == 0) {
  2693. BigInt i = exact_value_to_integer(operand.value).value_integer;
  2694. if (i.neg && !is_type_enum(index_type)) {
  2695. gbString expr_str = expr_to_string(operand.expr);
  2696. error(operand.expr, "Index '%s' cannot be a negative value", expr_str);
  2697. gb_string_free(expr_str);
  2698. if (value) *value = 0;
  2699. return false;
  2700. }
  2701. if (max_count >= 0) {
  2702. if (is_type_enum(index_type)) {
  2703. Type *bt = base_type(index_type);
  2704. GB_ASSERT(bt->kind == Type_Enum);
  2705. ExactValue lo = bt->Enum.min_value;
  2706. ExactValue hi = bt->Enum.max_value;
  2707. String lo_str = {};
  2708. String hi_str = {};
  2709. if (bt->Enum.fields.count > 0) {
  2710. lo_str = bt->Enum.fields[bt->Enum.min_value_index]->token.string;
  2711. hi_str = bt->Enum.fields[bt->Enum.max_value_index]->token.string;
  2712. }
  2713. bool out_of_bounds = false;
  2714. if (compare_exact_values(Token_Lt, operand.value, lo) || compare_exact_values(Token_Gt, operand.value, hi)) {
  2715. out_of_bounds = true;
  2716. }
  2717. if (out_of_bounds) {
  2718. gbString expr_str = expr_to_string(operand.expr);
  2719. if (lo_str.len > 0) {
  2720. error(operand.expr, "Index '%s' is out of bounds range %.*s .. %.*s", expr_str, LIT(lo_str), LIT(hi_str));
  2721. } else {
  2722. gbString index_type_str = type_to_string(index_type);
  2723. error(operand.expr, "Index '%s' is out of bounds range of enum type %s", expr_str, index_type_str);
  2724. gb_string_free(index_type_str);
  2725. }
  2726. gb_string_free(expr_str);
  2727. return false;
  2728. }
  2729. if (value) *value = exact_value_to_i64(exact_value_sub(operand.value, lo));
  2730. return true;
  2731. } else { // NOTE(bill): Do array bound checking
  2732. i64 v = -1;
  2733. if (i.len <= 1) {
  2734. v = big_int_to_i64(&i);
  2735. }
  2736. if (value) *value = v;
  2737. bool out_of_bounds = false;
  2738. if (v < 0) {
  2739. out_of_bounds = true;
  2740. } else if (open_range) {
  2741. out_of_bounds = v > max_count;
  2742. } else {
  2743. out_of_bounds = v >= max_count;
  2744. }
  2745. if (out_of_bounds) {
  2746. gbString expr_str = expr_to_string(operand.expr);
  2747. error(operand.expr, "Index '%s' is out of bounds range 0..<%lld", expr_str, max_count);
  2748. gb_string_free(expr_str);
  2749. return false;
  2750. }
  2751. return true;
  2752. }
  2753. }
  2754. }
  2755. // NOTE(bill): It's alright :D
  2756. if (value) *value = -1;
  2757. return true;
  2758. }
  2759. ExactValue get_constant_field_single(CheckerContext *c, ExactValue value, i32 index, bool *success_, bool *finish_) {
  2760. if (value.kind == ExactValue_String) {
  2761. GB_ASSERT(0 <= index && index < value.value_string.len);
  2762. u8 val = value.value_string[index];
  2763. if (success_) *success_ = true;
  2764. if (finish_) *finish_ = true;
  2765. return exact_value_u64(val);
  2766. }
  2767. if (value.kind != ExactValue_Compound) {
  2768. if (success_) *success_ = true;
  2769. if (finish_) *finish_ = true;
  2770. return value;
  2771. }
  2772. Ast *node = value.value_compound;
  2773. switch (node->kind) {
  2774. case_ast_node(cl, CompoundLit, node);
  2775. if (cl->elems.count == 0) {
  2776. if (success_) *success_ = true;
  2777. if (finish_) *finish_ = true;
  2778. return empty_exact_value;
  2779. }
  2780. if (cl->elems[0]->kind == Ast_FieldValue) {
  2781. if (is_type_struct(node->tav.type)) {
  2782. for_array(i, cl->elems) {
  2783. Ast *elem = cl->elems[i];
  2784. if (elem->kind != Ast_FieldValue) {
  2785. continue;
  2786. }
  2787. ast_node(fv, FieldValue, elem);
  2788. String name = fv->field->Ident.token.string;
  2789. Selection sub_sel = lookup_field(node->tav.type, name, false);
  2790. defer (array_free(&sub_sel.index));
  2791. if (sub_sel.index[0] == index) {
  2792. value = fv->value->tav.value;
  2793. break;
  2794. }
  2795. }
  2796. } else if (is_type_array(node->tav.type) || is_type_enumerated_array(node->tav.type)) {
  2797. for_array(i, cl->elems) {
  2798. Ast *elem = cl->elems[i];
  2799. if (elem->kind != Ast_FieldValue) {
  2800. continue;
  2801. }
  2802. ast_node(fv, FieldValue, elem);
  2803. if (is_ast_range(fv->field)) {
  2804. ast_node(ie, BinaryExpr, fv->field);
  2805. TypeAndValue lo_tav = ie->left->tav;
  2806. TypeAndValue hi_tav = ie->right->tav;
  2807. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  2808. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  2809. TokenKind op = ie->op.kind;
  2810. i64 lo = exact_value_to_i64(lo_tav.value);
  2811. i64 hi = exact_value_to_i64(hi_tav.value);
  2812. i64 corrected_index = index;
  2813. if (is_type_enumerated_array(node->tav.type)) {
  2814. Type *bt = base_type(node->tav.type);
  2815. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2816. corrected_index = index + exact_value_to_i64(bt->EnumeratedArray.min_value);
  2817. }
  2818. if (op == Token_Ellipsis) {
  2819. if (lo <= corrected_index && corrected_index <= hi) {
  2820. TypeAndValue tav = fv->value->tav;
  2821. if (success_) *success_ = true;
  2822. if (finish_) *finish_ = false;
  2823. return tav.value;
  2824. }
  2825. } else {
  2826. if (lo <= corrected_index && corrected_index < hi) {
  2827. TypeAndValue tav = fv->value->tav;
  2828. if (success_) *success_ = true;
  2829. if (finish_) *finish_ = false;
  2830. return tav.value;
  2831. }
  2832. }
  2833. } else {
  2834. TypeAndValue index_tav = fv->field->tav;
  2835. GB_ASSERT(index_tav.mode == Addressing_Constant);
  2836. ExactValue index_value = index_tav.value;
  2837. if (is_type_enumerated_array(node->tav.type)) {
  2838. Type *bt = base_type(node->tav.type);
  2839. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2840. index_value = exact_value_sub(index_value, bt->EnumeratedArray.min_value);
  2841. }
  2842. i64 field_index = exact_value_to_i64(index_value);
  2843. if (index == field_index) {
  2844. TypeAndValue tav = fv->value->tav;
  2845. value = tav.value;
  2846. break;
  2847. }
  2848. }
  2849. }
  2850. }
  2851. } else {
  2852. i32 count = (i32)cl->elems.count;
  2853. if (count < index) {
  2854. if (success_) *success_ = false;
  2855. if (finish_) *finish_ = true;
  2856. return empty_exact_value;
  2857. }
  2858. TypeAndValue tav = cl->elems[index]->tav;
  2859. if (tav.mode == Addressing_Constant) {
  2860. if (success_) *success_ = true;
  2861. if (finish_) *finish_ = false;
  2862. return tav.value;
  2863. } else {
  2864. GB_ASSERT(is_type_untyped_nil(tav.type));
  2865. if (success_) *success_ = true;
  2866. if (finish_) *finish_ = false;
  2867. return tav.value;
  2868. }
  2869. }
  2870. case_end;
  2871. default:
  2872. // TODO(bill): Should this be a general fallback?
  2873. if (success_) *success_ = true;
  2874. if (finish_) *finish_ = true;
  2875. return empty_exact_value;
  2876. }
  2877. if (finish_) *finish_ = false;
  2878. return value;
  2879. }
  2880. ExactValue get_constant_field(CheckerContext *c, Operand const *operand, Selection sel, bool *success_) {
  2881. if (operand->mode != Addressing_Constant) {
  2882. if (success_) *success_ = false;
  2883. return empty_exact_value;
  2884. }
  2885. if (sel.indirect) {
  2886. if (success_) *success_ = false;
  2887. return empty_exact_value;
  2888. }
  2889. if (sel.index.count == 0) {
  2890. if (success_) *success_ = false;
  2891. return empty_exact_value;
  2892. }
  2893. ExactValue value = operand->value;
  2894. if (value.kind == ExactValue_Compound) {
  2895. i32 depth = 0;
  2896. while (sel.index.count > 0) {
  2897. i32 index = sel.index[0];
  2898. sel = sub_selection(sel, 1);
  2899. bool finish = false;
  2900. value = get_constant_field_single(c, value, index, success_, &finish);
  2901. if (finish) {
  2902. return value;
  2903. }
  2904. }
  2905. if (success_) *success_ = true;
  2906. return value;
  2907. } else if (value.kind == ExactValue_Quaternion) {
  2908. // @QuaternionLayout
  2909. Quaternion256 q = value.value_quaternion;
  2910. GB_ASSERT(sel.index.count == 1);
  2911. switch (sel.index[0]) {
  2912. case 3: // w
  2913. if (success_) *success_ = true;
  2914. return exact_value_float(q.real);
  2915. case 0: // x
  2916. if (success_) *success_ = true;
  2917. return exact_value_float(q.imag);
  2918. case 1: // y
  2919. if (success_) *success_ = true;
  2920. return exact_value_float(q.jmag);
  2921. case 2: // z
  2922. if (success_) *success_ = true;
  2923. return exact_value_float(q.kmag);
  2924. }
  2925. if (success_) *success_ = false;
  2926. return empty_exact_value;
  2927. } else if (value.kind == ExactValue_Complex) {
  2928. // @QuaternionLayout
  2929. Complex128 c = value.value_complex;
  2930. GB_ASSERT(sel.index.count == 1);
  2931. switch (sel.index[0]) {
  2932. case 0: // real
  2933. if (success_) *success_ = true;
  2934. return exact_value_float(c.real);
  2935. case 1: // imag
  2936. if (success_) *success_ = true;
  2937. return exact_value_float(c.imag);
  2938. }
  2939. if (success_) *success_ = false;
  2940. return empty_exact_value;
  2941. }
  2942. if (success_) *success_ = true;
  2943. return empty_exact_value;
  2944. }
  2945. Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *type_hint) {
  2946. ast_node(se, SelectorExpr, node);
  2947. bool check_op_expr = true;
  2948. Entity *expr_entity = nullptr;
  2949. Entity *entity = nullptr;
  2950. Selection sel = {}; // NOTE(bill): Not used if it's an import name
  2951. operand->expr = node;
  2952. Ast *op_expr = se->expr;
  2953. Ast *selector = unparen_expr(se->selector);
  2954. if (selector == nullptr) {
  2955. operand->mode = Addressing_Invalid;
  2956. operand->expr = node;
  2957. return nullptr;
  2958. }
  2959. if (selector->kind != Ast_Ident) {
  2960. error(selector, "Illegal selector kind: '%.*s'", LIT(ast_strings[selector->kind]));
  2961. operand->mode = Addressing_Invalid;
  2962. operand->expr = node;
  2963. return nullptr;
  2964. }
  2965. if (op_expr->kind == Ast_Ident) {
  2966. String op_name = op_expr->Ident.token.string;
  2967. Entity *e = scope_lookup(c->scope, op_name);
  2968. add_entity_use(c, op_expr, e);
  2969. expr_entity = e;
  2970. Entity *original_e = e;
  2971. if (e != nullptr && e->kind == Entity_ImportName && selector->kind == Ast_Ident) {
  2972. // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
  2973. // It pretty much needs to be in this order and this way
  2974. // If you can clean this up, please do but be really careful
  2975. String import_name = op_name;
  2976. Scope *import_scope = e->ImportName.scope;
  2977. String entity_name = selector->Ident.token.string;
  2978. check_op_expr = false;
  2979. entity = scope_lookup_current(import_scope, entity_name);
  2980. bool is_declared = entity != nullptr;
  2981. bool allow_builtin = false;
  2982. if (is_declared) {
  2983. if (entity->kind == Entity_Builtin) {
  2984. // NOTE(bill): Builtin's are in the universal scope which is part of every scopes hierarchy
  2985. // This means that we should just ignore the found result through it
  2986. allow_builtin = entity->scope == import_scope;
  2987. } else if ((entity->scope->flags&ScopeFlag_Global) == ScopeFlag_Global && (import_scope->flags&ScopeFlag_Global) == 0) {
  2988. is_declared = false;
  2989. }
  2990. }
  2991. if (!is_declared) {
  2992. error(op_expr, "'%.*s' is not declared by '%.*s'", LIT(entity_name), LIT(import_name));
  2993. operand->mode = Addressing_Invalid;
  2994. operand->expr = node;
  2995. return nullptr;
  2996. }
  2997. check_entity_decl(c, entity, nullptr, nullptr);
  2998. GB_ASSERT(entity->type != nullptr);
  2999. if (!is_entity_exported(entity, allow_builtin)) {
  3000. gbString sel_str = expr_to_string(selector);
  3001. error(op_expr, "'%s' is not exported by '%.*s'", sel_str, LIT(import_name));
  3002. gb_string_free(sel_str);
  3003. operand->mode = Addressing_Invalid;
  3004. operand->expr = node;
  3005. return nullptr;
  3006. }
  3007. if (entity->kind == Entity_ProcGroup) {
  3008. Array<Entity *> procs = entity->ProcGroup.entities;
  3009. bool skip = false;
  3010. for_array(i, procs) {
  3011. Entity *p = procs[i];
  3012. Type *t = base_type(p->type);
  3013. if (t == t_invalid) {
  3014. continue;
  3015. }
  3016. Operand x = {};
  3017. x.mode = Addressing_Value;
  3018. x.type = t;
  3019. if (type_hint != nullptr) {
  3020. if (check_is_assignable_to(c, &x, type_hint)) {
  3021. entity = p;
  3022. skip = true;
  3023. break;
  3024. }
  3025. }
  3026. }
  3027. if (!skip) {
  3028. GB_ASSERT(entity != nullptr);
  3029. operand->mode = Addressing_ProcGroup;
  3030. operand->type = t_invalid;
  3031. operand->expr = node;
  3032. operand->proc_group = entity;
  3033. return entity;
  3034. }
  3035. }
  3036. }
  3037. }
  3038. if (check_op_expr) {
  3039. check_expr_base(c, operand, op_expr, nullptr);
  3040. if (operand->mode == Addressing_Invalid) {
  3041. operand->mode = Addressing_Invalid;
  3042. operand->expr = node;
  3043. return nullptr;
  3044. }
  3045. }
  3046. if (entity == nullptr && selector->kind == Ast_Ident) {
  3047. String field_name = selector->Ident.token.string;
  3048. if (is_type_dynamic_array(type_deref(operand->type))) {
  3049. init_mem_allocator(c->checker);
  3050. }
  3051. sel = lookup_field(operand->type, field_name, operand->mode == Addressing_Type);
  3052. entity = sel.entity;
  3053. // NOTE(bill): Add type info needed for fields like 'names'
  3054. if (entity != nullptr && (entity->flags&EntityFlag_TypeField)) {
  3055. add_type_info_type(c, operand->type);
  3056. }
  3057. if (is_type_enum(operand->type)) {
  3058. add_type_info_type(c, operand->type);
  3059. }
  3060. }
  3061. if (entity == nullptr) {
  3062. gbString op_str = expr_to_string(op_expr);
  3063. gbString type_str = type_to_string(operand->type);
  3064. gbString sel_str = expr_to_string(selector);
  3065. error(op_expr, "'%s' of type '%s' has no field '%s'", op_str, type_str, sel_str);
  3066. gb_string_free(sel_str);
  3067. gb_string_free(type_str);
  3068. gb_string_free(op_str);
  3069. operand->mode = Addressing_Invalid;
  3070. operand->expr = node;
  3071. return nullptr;
  3072. }
  3073. if (expr_entity != nullptr && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
  3074. bool success = false;
  3075. ExactValue field_value = get_constant_field(c, operand, sel, &success);
  3076. if (success) {
  3077. operand->mode = Addressing_Constant;
  3078. operand->expr = node;
  3079. operand->value = field_value;
  3080. operand->type = entity->type;
  3081. add_entity_use(c, selector, entity);
  3082. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3083. return entity;
  3084. }
  3085. gbString op_str = expr_to_string(op_expr);
  3086. gbString type_str = type_to_string(operand->type);
  3087. gbString sel_str = expr_to_string(selector);
  3088. error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
  3089. gb_string_free(sel_str);
  3090. gb_string_free(type_str);
  3091. gb_string_free(op_str);
  3092. operand->mode = Addressing_Invalid;
  3093. operand->expr = node;
  3094. return nullptr;
  3095. }
  3096. if (operand->mode == Addressing_Constant && entity->kind != Entity_Constant) {
  3097. bool success = false;
  3098. ExactValue field_value = get_constant_field(c, operand, sel, &success);
  3099. if (success) {
  3100. operand->mode = Addressing_Constant;
  3101. operand->expr = node;
  3102. operand->value = field_value;
  3103. operand->type = entity->type;
  3104. add_entity_use(c, selector, entity);
  3105. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3106. return entity;
  3107. }
  3108. gbString op_str = expr_to_string(op_expr);
  3109. gbString type_str = type_to_string(operand->type);
  3110. gbString sel_str = expr_to_string(selector);
  3111. error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
  3112. gb_string_free(sel_str);
  3113. gb_string_free(type_str);
  3114. gb_string_free(op_str);
  3115. operand->mode = Addressing_Invalid;
  3116. operand->expr = node;
  3117. return nullptr;
  3118. }
  3119. if (expr_entity != nullptr && is_type_polymorphic(expr_entity->type)) {
  3120. gbString op_str = expr_to_string(op_expr);
  3121. gbString type_str = type_to_string(operand->type);
  3122. gbString sel_str = expr_to_string(selector);
  3123. error(op_expr, "Cannot access field '%s' from non-specialized polymorphic type '%s'", sel_str, op_str);
  3124. gb_string_free(sel_str);
  3125. gb_string_free(type_str);
  3126. gb_string_free(op_str);
  3127. operand->mode = Addressing_Invalid;
  3128. operand->expr = node;
  3129. return nullptr;
  3130. }
  3131. add_entity_use(c, selector, entity);
  3132. switch (entity->kind) {
  3133. case Entity_Constant:
  3134. operand->mode = Addressing_Constant;
  3135. operand->value = entity->Constant.value;
  3136. break;
  3137. case Entity_Variable:
  3138. // TODO(bill): Is this the rule I need?
  3139. if (operand->mode == Addressing_Context) {
  3140. if (sel.indirect) {
  3141. operand->mode = Addressing_Variable;
  3142. }
  3143. } else if (operand->mode == Addressing_MapIndex) {
  3144. operand->mode = Addressing_Value;
  3145. } else if (entity->flags & EntityFlag_SoaPtrField) {
  3146. operand->mode = Addressing_SoaVariable;
  3147. } else if (sel.indirect || operand->mode != Addressing_Value || operand->mode == Addressing_SoaVariable) {
  3148. operand->mode = Addressing_Variable;
  3149. } else {
  3150. operand->mode = Addressing_Value;
  3151. }
  3152. break;
  3153. case Entity_TypeName:
  3154. operand->mode = Addressing_Type;
  3155. break;
  3156. case Entity_Procedure:
  3157. operand->mode = Addressing_Value;
  3158. break;
  3159. case Entity_Builtin:
  3160. operand->mode = Addressing_Builtin;
  3161. operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
  3162. break;
  3163. case Entity_ProcGroup:
  3164. operand->mode = Addressing_ProcGroup;
  3165. operand->proc_group = entity;
  3166. break;
  3167. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  3168. case Entity_Nil:
  3169. operand->mode = Addressing_Value;
  3170. break;
  3171. }
  3172. operand->type = entity->type;
  3173. operand->expr = node;
  3174. return entity;
  3175. }
  3176. bool is_type_normal_pointer(Type *ptr, Type **elem) {
  3177. ptr = base_type(ptr);
  3178. if (is_type_pointer(ptr)) {
  3179. if (is_type_rawptr(ptr)) {
  3180. return false;
  3181. }
  3182. if (elem) *elem = ptr->Pointer.elem;
  3183. return true;
  3184. }
  3185. return false;
  3186. }
  3187. bool check_identifier_exists(Scope *s, Ast *node, bool nested = false, Scope **out_scope = nullptr) {
  3188. switch (node->kind) {
  3189. case_ast_node(i, Ident, node);
  3190. String name = i->token.string;
  3191. if (nested) {
  3192. Entity *e = scope_lookup_current(s, name);
  3193. if (e != nullptr) {
  3194. if (out_scope) *out_scope = e->scope;
  3195. return true;
  3196. }
  3197. } else {
  3198. Entity *e = scope_lookup(s, name);
  3199. if (e != nullptr) {
  3200. if (out_scope) *out_scope = e->scope;
  3201. return true;
  3202. }
  3203. }
  3204. case_end;
  3205. case_ast_node(se, SelectorExpr, node);
  3206. Ast *lhs = se->expr;
  3207. Ast *rhs = se->selector;
  3208. Scope *lhs_scope = nullptr;
  3209. if (check_identifier_exists(s, lhs, nested, &lhs_scope)) {
  3210. return check_identifier_exists(lhs_scope, rhs, true);
  3211. }
  3212. case_end;
  3213. }
  3214. return false;
  3215. }
  3216. typedef bool (BuiltinTypeIsProc)(Type *t);
  3217. BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
  3218. nullptr, // BuiltinProc__type_simple_boolean_begin
  3219. is_type_boolean,
  3220. is_type_integer,
  3221. is_type_rune,
  3222. is_type_float,
  3223. is_type_complex,
  3224. is_type_quaternion,
  3225. is_type_string,
  3226. is_type_typeid,
  3227. is_type_any,
  3228. is_type_endian_little,
  3229. is_type_endian_big,
  3230. is_type_unsigned,
  3231. is_type_numeric,
  3232. is_type_ordered,
  3233. is_type_ordered_numeric,
  3234. is_type_indexable,
  3235. is_type_sliceable,
  3236. is_type_simple_compare,
  3237. is_type_dereferenceable,
  3238. is_type_valid_for_keys,
  3239. is_type_named,
  3240. is_type_pointer,
  3241. is_type_opaque,
  3242. is_type_array,
  3243. is_type_enumerated_array,
  3244. is_type_slice,
  3245. is_type_dynamic_array,
  3246. is_type_map,
  3247. is_type_struct,
  3248. is_type_union,
  3249. is_type_enum,
  3250. is_type_proc,
  3251. is_type_bit_field,
  3252. is_type_bit_field_value,
  3253. is_type_bit_set,
  3254. is_type_simd_vector,
  3255. is_type_polymorphic_record_specialized,
  3256. is_type_polymorphic_record_unspecialized,
  3257. type_has_nil,
  3258. };
  3259. bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  3260. ast_node(ce, CallExpr, call);
  3261. if (ce->inlining != ProcInlining_none) {
  3262. error(call, "Inlining operators are not allowed on built-in procedures");
  3263. }
  3264. BuiltinProc *bp = &builtin_procs[id];
  3265. {
  3266. char const *err = nullptr;
  3267. if (ce->args.count < bp->arg_count) {
  3268. err = "Too few";
  3269. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  3270. err = "Too many";
  3271. }
  3272. if (err != nullptr) {
  3273. gbString expr = expr_to_string(ce->proc);
  3274. error(ce->close, "%s arguments for '%s', expected %td, got %td",
  3275. err, expr,
  3276. bp->arg_count, ce->args.count);
  3277. gb_string_free(expr);
  3278. return false;
  3279. }
  3280. }
  3281. switch (id) {
  3282. case BuiltinProc_size_of:
  3283. case BuiltinProc_align_of:
  3284. case BuiltinProc_offset_of:
  3285. case BuiltinProc_type_info_of:
  3286. case BuiltinProc_typeid_of:
  3287. case BuiltinProc_len:
  3288. case BuiltinProc_min:
  3289. case BuiltinProc_max:
  3290. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  3291. break;
  3292. case BuiltinProc_DIRECTIVE: {
  3293. ast_node(bd, BasicDirective, ce->proc);
  3294. String name = bd->name;
  3295. if (name == "defined") {
  3296. break;
  3297. }
  3298. /*fallthrough*/
  3299. }
  3300. default:
  3301. if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
  3302. check_expr_or_type(c, operand, ce->args[0]);
  3303. } else if (ce->args.count > 0) {
  3304. check_multi_expr(c, operand, ce->args[0]);
  3305. }
  3306. break;
  3307. }
  3308. String builtin_name = builtin_procs[id].name;
  3309. if (ce->args.count > 0) {
  3310. if (ce->args[0]->kind == Ast_FieldValue) {
  3311. error(call, "'field = value' calling is not allowed on built-in procedures");
  3312. return false;
  3313. }
  3314. }
  3315. switch (id) {
  3316. default:
  3317. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
  3318. break;
  3319. case BuiltinProc_DIRECTIVE: {
  3320. ast_node(bd, BasicDirective, ce->proc);
  3321. String name = bd->name;
  3322. if (name == "location") {
  3323. if (ce->args.count > 1) {
  3324. error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
  3325. }
  3326. if (ce->args.count > 0) {
  3327. Ast *arg = ce->args[0];
  3328. Entity *e = nullptr;
  3329. Operand o = {};
  3330. if (arg->kind == Ast_Ident) {
  3331. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  3332. } else if (arg->kind == Ast_SelectorExpr) {
  3333. e = check_selector(c, &o, arg, nullptr);
  3334. }
  3335. if (e == nullptr) {
  3336. error(ce->args[0], "'#location' expected a valid entity name");
  3337. }
  3338. }
  3339. operand->type = t_source_code_location;
  3340. operand->mode = Addressing_Value;
  3341. } else if (name == "load") {
  3342. if (ce->args.count != 1) {
  3343. error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
  3344. return false;
  3345. }
  3346. Ast *arg = ce->args[0];
  3347. Operand o = {};
  3348. check_expr(c, &o, arg);
  3349. if (o.mode != Addressing_Constant) {
  3350. error(arg, "'#load' expected a constant string argument");
  3351. return false;
  3352. }
  3353. if (!is_type_string(o.type)) {
  3354. gbString str = type_to_string(o.type);
  3355. error(arg, "'#load' expected a constant string, got %s", str);
  3356. gb_string_free(str);
  3357. return false;
  3358. }
  3359. gbAllocator a = heap_allocator();
  3360. GB_ASSERT(o.value.kind == ExactValue_String);
  3361. String base_dir = dir_from_path(bd->token.pos.file);
  3362. String original_string = o.value.value_string;
  3363. gbMutex *ignore_mutex = nullptr;
  3364. String path = {};
  3365. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  3366. char *c_str = alloc_cstring(a, path);
  3367. defer (gb_free(a, c_str));
  3368. gbFile f = {};
  3369. gbFileError file_err = gb_file_open(&f, c_str);
  3370. defer (gb_file_close(&f));
  3371. switch (file_err) {
  3372. default:
  3373. case gbFileError_Invalid:
  3374. error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
  3375. return false;
  3376. case gbFileError_NotExists:
  3377. error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
  3378. return false;
  3379. case gbFileError_Permission:
  3380. error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
  3381. return false;
  3382. case gbFileError_None:
  3383. // Okay
  3384. break;
  3385. }
  3386. String result = {};
  3387. isize file_size = cast(isize)gb_file_size(&f);
  3388. if (file_size > 0) {
  3389. u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
  3390. gb_file_read_at(&f, data, file_size, 0);
  3391. data[file_size] = '\0';
  3392. result.text = data;
  3393. result.len = file_size;
  3394. }
  3395. operand->type = t_u8_slice;
  3396. operand->mode = Addressing_Constant;
  3397. operand->value = exact_value_string(result);
  3398. } else if (name == "assert") {
  3399. if (ce->args.count != 1) {
  3400. error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
  3401. return false;
  3402. }
  3403. if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
  3404. gbString str = expr_to_string(ce->args[0]);
  3405. error(call, "'%s' is not a constant boolean", str);
  3406. gb_string_free(str);
  3407. return false;
  3408. }
  3409. if (!operand->value.value_bool) {
  3410. gbString arg = expr_to_string(ce->args[0]);
  3411. error(call, "Compile time assertion: %s", arg);
  3412. if (c->proc_name != "") {
  3413. gbString str = type_to_string(c->curr_proc_sig);
  3414. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  3415. gb_string_free(str);
  3416. }
  3417. gb_string_free(arg);
  3418. }
  3419. operand->type = t_untyped_bool;
  3420. operand->mode = Addressing_Constant;
  3421. } else if (name == "panic") {
  3422. if (ce->args.count != 1) {
  3423. error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
  3424. return false;
  3425. }
  3426. if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
  3427. gbString str = expr_to_string(ce->args[0]);
  3428. error(call, "'%s' is not a constant string", str);
  3429. gb_string_free(str);
  3430. return false;
  3431. }
  3432. error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
  3433. if (c->proc_name != "") {
  3434. gbString str = type_to_string(c->curr_proc_sig);
  3435. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  3436. gb_string_free(str);
  3437. }
  3438. operand->type = t_invalid;
  3439. operand->mode = Addressing_NoValue;
  3440. } else if (name == "defined") {
  3441. if (ce->args.count != 1) {
  3442. error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
  3443. return false;
  3444. }
  3445. Ast *arg = unparen_expr(ce->args[0]);
  3446. if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
  3447. error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
  3448. return false;
  3449. }
  3450. bool is_defined = check_identifier_exists(c->scope, arg);
  3451. operand->type = t_untyped_bool;
  3452. operand->mode = Addressing_Constant;
  3453. operand->value = exact_value_bool(is_defined);
  3454. } else {
  3455. GB_PANIC("Unhandled #%.*s", LIT(name));
  3456. }
  3457. break;
  3458. }
  3459. case BuiltinProc_len:
  3460. check_expr_or_type(c, operand, ce->args[0]);
  3461. if (operand->mode == Addressing_Invalid) {
  3462. return false;
  3463. }
  3464. /* fallthrough */
  3465. case BuiltinProc_cap:
  3466. {
  3467. // len :: proc(Type) -> int
  3468. // cap :: proc(Type) -> int
  3469. Type *op_type = type_deref(operand->type);
  3470. Type *type = t_int;
  3471. AddressingMode mode = Addressing_Invalid;
  3472. ExactValue value = {};
  3473. if (is_type_string(op_type) && id == BuiltinProc_len) {
  3474. if (operand->mode == Addressing_Constant) {
  3475. mode = Addressing_Constant;
  3476. String str = operand->value.value_string;
  3477. value = exact_value_i64(str.len);
  3478. type = t_untyped_integer;
  3479. } else {
  3480. mode = Addressing_Value;
  3481. if (is_type_cstring(op_type)) {
  3482. add_package_dependency(c, "runtime", "cstring_len");
  3483. }
  3484. }
  3485. } else if (is_type_array(op_type)) {
  3486. Type *at = core_type(op_type);
  3487. mode = Addressing_Constant;
  3488. value = exact_value_i64(at->Array.count);
  3489. type = t_untyped_integer;
  3490. } else if (is_type_enumerated_array(op_type) && id == BuiltinProc_len) {
  3491. Type *at = core_type(op_type);
  3492. mode = Addressing_Constant;
  3493. value = exact_value_i64(at->EnumeratedArray.count);
  3494. type = t_untyped_integer;
  3495. } else if (is_type_slice(op_type) && id == BuiltinProc_len) {
  3496. mode = Addressing_Value;
  3497. } else if (is_type_dynamic_array(op_type)) {
  3498. mode = Addressing_Value;
  3499. } else if (is_type_map(op_type)) {
  3500. mode = Addressing_Value;
  3501. } else if (operand->mode == Addressing_Type && is_type_enum(op_type) && id == BuiltinProc_len) {
  3502. Type *bt = base_type(op_type);
  3503. mode = Addressing_Constant;
  3504. value = exact_value_i64(bt->Enum.fields.count);
  3505. type = t_untyped_integer;
  3506. } else if (is_type_struct(op_type)) {
  3507. Type *bt = base_type(op_type);
  3508. if (bt->Struct.soa_kind == StructSoa_Fixed) {
  3509. mode = Addressing_Constant;
  3510. value = exact_value_i64(bt->Struct.soa_count);
  3511. type = t_untyped_integer;
  3512. } else if ((bt->Struct.soa_kind == StructSoa_Slice && id == BuiltinProc_len) ||
  3513. bt->Struct.soa_kind == StructSoa_Dynamic) {
  3514. mode = Addressing_Value;
  3515. }
  3516. }
  3517. if (operand->mode == Addressing_Type && mode != Addressing_Constant) {
  3518. mode = Addressing_Invalid;
  3519. }
  3520. if (mode == Addressing_Invalid) {
  3521. gbString t = type_to_string(operand->type);
  3522. error(call, "'%.*s' is not supported for '%s'", LIT(builtin_name), t);
  3523. return false;
  3524. }
  3525. operand->mode = mode;
  3526. operand->value = value;
  3527. operand->type = type;
  3528. break;
  3529. }
  3530. case BuiltinProc_size_of: {
  3531. // size_of :: proc(Type or expr) -> untyped int
  3532. Operand o = {};
  3533. check_expr_or_type(c, &o, ce->args[0]);
  3534. if (o.mode == Addressing_Invalid) {
  3535. return false;
  3536. }
  3537. Type *t = o.type;
  3538. if (t == nullptr || t == t_invalid) {
  3539. error(ce->args[0], "Invalid argument for 'size_of'");
  3540. return false;
  3541. }
  3542. t = default_type(t);
  3543. operand->mode = Addressing_Constant;
  3544. operand->value = exact_value_i64(type_size_of(t));
  3545. operand->type = t_untyped_integer;
  3546. break;
  3547. }
  3548. case BuiltinProc_align_of: {
  3549. // align_of :: proc(Type or expr) -> untyped int
  3550. Operand o = {};
  3551. check_expr_or_type(c, &o, ce->args[0]);
  3552. if (o.mode == Addressing_Invalid) {
  3553. return false;
  3554. }
  3555. Type *t = o.type;
  3556. if (t == nullptr || t == t_invalid) {
  3557. error(ce->args[0], "Invalid argument for 'align_of'");
  3558. return false;
  3559. }
  3560. t = default_type(t);
  3561. operand->mode = Addressing_Constant;
  3562. operand->value = exact_value_i64(type_align_of(t));
  3563. operand->type = t_untyped_integer;
  3564. break;
  3565. }
  3566. case BuiltinProc_offset_of: {
  3567. // offset_of :: proc(Type, field) -> uintptr
  3568. Operand op = {};
  3569. Type *bt = check_type(c, ce->args[0]);
  3570. Type *type = base_type(bt);
  3571. if (type == nullptr || type == t_invalid) {
  3572. error(ce->args[0], "Expected a type for 'offset_of'");
  3573. return false;
  3574. }
  3575. Ast *field_arg = unparen_expr(ce->args[1]);
  3576. if (field_arg == nullptr ||
  3577. field_arg->kind != Ast_Ident) {
  3578. error(field_arg, "Expected an identifier for field argument");
  3579. return false;
  3580. }
  3581. if (is_type_array(type)) {
  3582. error(field_arg, "Invalid type for 'offset_of'");
  3583. return false;
  3584. }
  3585. ast_node(arg, Ident, field_arg);
  3586. Selection sel = lookup_field(type, arg->token.string, operand->mode == Addressing_Type);
  3587. if (sel.entity == nullptr) {
  3588. gbString type_str = type_to_string(bt);
  3589. error(ce->args[0],
  3590. "'%s' has no field named '%.*s'", type_str, LIT(arg->token.string));
  3591. gb_string_free(type_str);
  3592. return false;
  3593. }
  3594. if (sel.indirect) {
  3595. gbString type_str = type_to_string(bt);
  3596. error(ce->args[0],
  3597. "Field '%.*s' is embedded via a pointer in '%s'", LIT(arg->token.string), type_str);
  3598. gb_string_free(type_str);
  3599. return false;
  3600. }
  3601. operand->mode = Addressing_Constant;
  3602. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  3603. operand->type = t_uintptr;
  3604. break;
  3605. }
  3606. case BuiltinProc_type_of: {
  3607. // type_of :: proc(val: Type) -> type(Type)
  3608. Ast *expr = ce->args[0];
  3609. Operand o = {};
  3610. check_expr_or_type(c, &o, expr);
  3611. // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
  3612. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  3613. return false;
  3614. }
  3615. if (o.type == nullptr || o.type == t_invalid) {
  3616. error(o.expr, "Invalid argument to 'type_of'");
  3617. return false;
  3618. }
  3619. if (o.type == nullptr || o.type == t_invalid) {
  3620. error(o.expr, "Invalid argument to 'type_of'");
  3621. return false;
  3622. }
  3623. // NOTE(bill): Prevent type cycles for procedure declarations
  3624. if (c->curr_proc_sig == o.type) {
  3625. gbString s = expr_to_string(o.expr);
  3626. error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
  3627. gb_string_free(s);
  3628. return false;
  3629. }
  3630. if (is_type_polymorphic(o.type)) {
  3631. error(o.expr, "'type_of' of polymorphic type cannot be determined");
  3632. return false;
  3633. }
  3634. operand->mode = Addressing_Type;
  3635. operand->type = o.type;
  3636. break;
  3637. }
  3638. case BuiltinProc_type_info_of: {
  3639. // type_info_of :: proc(Type) -> ^Type_Info
  3640. if (c->scope->flags&ScopeFlag_Global) {
  3641. compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  3642. }
  3643. // NOTE(bill): The type information may not be setup yet
  3644. init_core_type_info(c->checker);
  3645. Ast *expr = ce->args[0];
  3646. Operand o = {};
  3647. check_expr_or_type(c, &o, expr);
  3648. if (o.mode == Addressing_Invalid) {
  3649. return false;
  3650. }
  3651. Type *t = o.type;
  3652. if (t == nullptr || t == t_invalid || is_type_polymorphic(t)) {
  3653. if (is_type_polymorphic(t)) {
  3654. error(ce->args[0], "Invalid argument for 'type_info_of', unspecialized polymorphic type");
  3655. } else {
  3656. error(ce->args[0], "Invalid argument for 'type_info_of'");
  3657. }
  3658. return false;
  3659. }
  3660. t = default_type(t);
  3661. add_type_info_type(c, t);
  3662. if (is_operand_value(o) && is_type_typeid(t)) {
  3663. add_package_dependency(c, "runtime", "__type_info_of");
  3664. } else if (o.mode != Addressing_Type) {
  3665. error(expr, "Expected a type or typeid for 'type_info_of'");
  3666. return false;
  3667. }
  3668. operand->mode = Addressing_Value;
  3669. operand->type = t_type_info_ptr;
  3670. break;
  3671. }
  3672. case BuiltinProc_typeid_of: {
  3673. // typeid_of :: proc(Type) -> typeid
  3674. if (c->scope->flags&ScopeFlag_Global) {
  3675. compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  3676. }
  3677. // NOTE(bill): The type information may not be setup yet
  3678. init_core_type_info(c->checker);
  3679. Ast *expr = ce->args[0];
  3680. Operand o = {};
  3681. check_expr_or_type(c, &o, expr);
  3682. if (o.mode == Addressing_Invalid) {
  3683. return false;
  3684. }
  3685. Type *t = o.type;
  3686. if (t == nullptr || t == t_invalid || is_type_polymorphic(operand->type)) {
  3687. error(ce->args[0], "Invalid argument for 'typeid_of'");
  3688. return false;
  3689. }
  3690. t = default_type(t);
  3691. add_type_info_type(c, t);
  3692. t = base_type(t);
  3693. if (o.mode != Addressing_Type) {
  3694. error(expr, "Expected a type for 'typeid_of'");
  3695. return false;
  3696. }
  3697. operand->mode = Addressing_Value;
  3698. operand->type = t_typeid;
  3699. break;
  3700. }
  3701. case BuiltinProc_swizzle: {
  3702. // swizzle :: proc(v: [N]T, ..int) -> [M]T
  3703. Type *type = base_type(operand->type);
  3704. if (!is_type_array(type)) {
  3705. gbString type_str = type_to_string(operand->type);
  3706. error(call,
  3707. "You can only 'swizzle' an array, got '%s'",
  3708. type_str);
  3709. gb_string_free(type_str);
  3710. return false;
  3711. }
  3712. i64 max_count = type->Array.count;
  3713. Type *elem_type = type->Array.elem;
  3714. i64 arg_count = 0;
  3715. for_array(i, ce->args) {
  3716. if (i == 0) {
  3717. continue;
  3718. }
  3719. Ast *arg = ce->args[i];
  3720. Operand op = {};
  3721. check_expr(c, &op, arg);
  3722. if (op.mode == Addressing_Invalid) {
  3723. return false;
  3724. }
  3725. Type *arg_type = base_type(op.type);
  3726. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  3727. error(op.expr, "Indices to 'swizzle' must be constant integers");
  3728. return false;
  3729. }
  3730. if (op.value.value_integer.neg) {
  3731. error(op.expr, "Negative 'swizzle' index");
  3732. return false;
  3733. }
  3734. BigInt mc = {};
  3735. big_int_from_i64(&mc, max_count);
  3736. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  3737. error(op.expr, "'swizzle' index exceeds length");
  3738. return false;
  3739. }
  3740. arg_count++;
  3741. }
  3742. if (arg_count > max_count) {
  3743. error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
  3744. return false;
  3745. }
  3746. if (arg_count < max_count) {
  3747. operand->type = alloc_type_array(elem_type, arg_count);
  3748. }
  3749. operand->mode = Addressing_Value;
  3750. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3751. operand->type = type_hint;
  3752. }
  3753. break;
  3754. }
  3755. case BuiltinProc_complex: {
  3756. // complex :: proc(real, imag: float_type) -> complex_type
  3757. Operand x = *operand;
  3758. Operand y = {};
  3759. // NOTE(bill): Invalid will be the default till fixed
  3760. operand->type = t_invalid;
  3761. operand->mode = Addressing_Invalid;
  3762. check_expr(c, &y, ce->args[1]);
  3763. if (y.mode == Addressing_Invalid) {
  3764. return false;
  3765. }
  3766. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  3767. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3768. if (x.mode == Addressing_Constant &&
  3769. y.mode == Addressing_Constant) {
  3770. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  3771. x.type = t_untyped_float;
  3772. }
  3773. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  3774. y.type = t_untyped_float;
  3775. }
  3776. }
  3777. if (!are_types_identical(x.type, y.type)) {
  3778. gbString tx = type_to_string(x.type);
  3779. gbString ty = type_to_string(y.type);
  3780. error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
  3781. gb_string_free(ty);
  3782. gb_string_free(tx);
  3783. return false;
  3784. }
  3785. if (!is_type_float(x.type)) {
  3786. gbString s = type_to_string(x.type);
  3787. error(call, "Arguments have type '%s', expected a floating point", s);
  3788. gb_string_free(s);
  3789. return false;
  3790. }
  3791. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  3792. f64 r = exact_value_to_float(x.value).value_float;
  3793. f64 i = exact_value_to_float(y.value).value_float;
  3794. operand->value = exact_value_complex(r, i);
  3795. operand->mode = Addressing_Constant;
  3796. } else {
  3797. operand->mode = Addressing_Value;
  3798. }
  3799. BasicKind kind = core_type(x.type)->Basic.kind;
  3800. switch (kind) {
  3801. // case Basic_f16: operand->type = t_complex32; break;
  3802. case Basic_f32: operand->type = t_complex64; break;
  3803. case Basic_f64: operand->type = t_complex128; break;
  3804. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  3805. default: GB_PANIC("Invalid type"); break;
  3806. }
  3807. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3808. operand->type = type_hint;
  3809. }
  3810. break;
  3811. }
  3812. case BuiltinProc_quaternion: {
  3813. // quaternion :: proc(real, imag, jmag, kmag: float_type) -> complex_type
  3814. Operand x = *operand;
  3815. Operand y = {};
  3816. Operand z = {};
  3817. Operand w = {};
  3818. // NOTE(bill): Invalid will be the default till fixed
  3819. operand->type = t_invalid;
  3820. operand->mode = Addressing_Invalid;
  3821. check_expr(c, &y, ce->args[1]);
  3822. if (y.mode == Addressing_Invalid) {
  3823. return false;
  3824. }
  3825. check_expr(c, &z, ce->args[2]);
  3826. if (y.mode == Addressing_Invalid) {
  3827. return false;
  3828. }
  3829. check_expr(c, &w, ce->args[3]);
  3830. if (y.mode == Addressing_Invalid) {
  3831. return false;
  3832. }
  3833. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  3834. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3835. convert_to_typed(c, &z, x.type); if (z.mode == Addressing_Invalid) return false;
  3836. convert_to_typed(c, &w, x.type); if (w.mode == Addressing_Invalid) return false;
  3837. if (x.mode == Addressing_Constant &&
  3838. y.mode == Addressing_Constant &&
  3839. z.mode == Addressing_Constant &&
  3840. w.mode == Addressing_Constant) {
  3841. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  3842. x.type = t_untyped_float;
  3843. }
  3844. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  3845. y.type = t_untyped_float;
  3846. }
  3847. if (is_type_numeric(z.type) && exact_value_imag(z.value).value_float == 0) {
  3848. z.type = t_untyped_float;
  3849. }
  3850. if (is_type_numeric(w.type) && exact_value_imag(w.value).value_float == 0) {
  3851. w.type = t_untyped_float;
  3852. }
  3853. }
  3854. if (!(are_types_identical(x.type, y.type) && are_types_identical(x.type, z.type) && are_types_identical(x.type, w.type))) {
  3855. gbString tx = type_to_string(x.type);
  3856. gbString ty = type_to_string(y.type);
  3857. gbString tz = type_to_string(z.type);
  3858. gbString tw = type_to_string(w.type);
  3859. error(call, "Mismatched types to 'quaternion', '%s' vs '%s' vs '%s' vs '%s'", tx, ty, tz, tw);
  3860. gb_string_free(tw);
  3861. gb_string_free(tz);
  3862. gb_string_free(ty);
  3863. gb_string_free(tx);
  3864. return false;
  3865. }
  3866. if (!is_type_float(x.type)) {
  3867. gbString s = type_to_string(x.type);
  3868. error(call, "Arguments have type '%s', expected a floating point", s);
  3869. gb_string_free(s);
  3870. return false;
  3871. }
  3872. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant && z.mode == Addressing_Constant && w.mode == Addressing_Constant) {
  3873. f64 r = exact_value_to_float(x.value).value_float;
  3874. f64 i = exact_value_to_float(y.value).value_float;
  3875. f64 j = exact_value_to_float(z.value).value_float;
  3876. f64 k = exact_value_to_float(w.value).value_float;
  3877. operand->value = exact_value_quaternion(r, i, j, k);
  3878. operand->mode = Addressing_Constant;
  3879. } else {
  3880. operand->mode = Addressing_Value;
  3881. }
  3882. BasicKind kind = core_type(x.type)->Basic.kind;
  3883. switch (kind) {
  3884. case Basic_f32: operand->type = t_quaternion128; break;
  3885. case Basic_f64: operand->type = t_quaternion256; break;
  3886. case Basic_UntypedFloat: operand->type = t_untyped_quaternion; break;
  3887. default: GB_PANIC("Invalid type"); break;
  3888. }
  3889. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3890. operand->type = type_hint;
  3891. }
  3892. break;
  3893. }
  3894. case BuiltinProc_real:
  3895. case BuiltinProc_imag: {
  3896. // real :: proc(x: type) -> float_type
  3897. // imag :: proc(x: type) -> float_type
  3898. Operand *x = operand;
  3899. if (is_type_untyped(x->type)) {
  3900. if (x->mode == Addressing_Constant) {
  3901. if (is_type_numeric(x->type)) {
  3902. x->type = t_untyped_complex;
  3903. }
  3904. } else if (is_type_quaternion(x->type)) {
  3905. convert_to_typed(c, x, t_quaternion256);
  3906. if (x->mode == Addressing_Invalid) {
  3907. return false;
  3908. }
  3909. } else{
  3910. convert_to_typed(c, x, t_complex128);
  3911. if (x->mode == Addressing_Invalid) {
  3912. return false;
  3913. }
  3914. }
  3915. }
  3916. if (!is_type_complex(x->type) && !is_type_quaternion(x->type)) {
  3917. gbString s = type_to_string(x->type);
  3918. error(call, "Argument has type '%s', expected a complex or quaternion type", s);
  3919. gb_string_free(s);
  3920. return false;
  3921. }
  3922. if (x->mode == Addressing_Constant) {
  3923. switch (id) {
  3924. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  3925. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  3926. }
  3927. } else {
  3928. x->mode = Addressing_Value;
  3929. }
  3930. BasicKind kind = core_type(x->type)->Basic.kind;
  3931. switch (kind) {
  3932. case Basic_complex64: x->type = t_f32; break;
  3933. case Basic_complex128: x->type = t_f64; break;
  3934. case Basic_quaternion128: x->type = t_f32; break;
  3935. case Basic_quaternion256: x->type = t_f64; break;
  3936. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  3937. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  3938. default: GB_PANIC("Invalid type"); break;
  3939. }
  3940. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3941. operand->type = type_hint;
  3942. }
  3943. break;
  3944. }
  3945. case BuiltinProc_jmag:
  3946. case BuiltinProc_kmag: {
  3947. // jmag :: proc(x: type) -> float_type
  3948. // kmag :: proc(x: type) -> float_type
  3949. Operand *x = operand;
  3950. if (is_type_untyped(x->type)) {
  3951. if (x->mode == Addressing_Constant) {
  3952. if (is_type_numeric(x->type)) {
  3953. x->type = t_untyped_complex;
  3954. }
  3955. } else{
  3956. convert_to_typed(c, x, t_quaternion256);
  3957. if (x->mode == Addressing_Invalid) {
  3958. return false;
  3959. }
  3960. }
  3961. }
  3962. if (!is_type_quaternion(x->type)) {
  3963. gbString s = type_to_string(x->type);
  3964. error(call, "Argument has type '%s', expected a quaternion type", s);
  3965. gb_string_free(s);
  3966. return false;
  3967. }
  3968. if (x->mode == Addressing_Constant) {
  3969. switch (id) {
  3970. case BuiltinProc_jmag: x->value = exact_value_jmag(x->value); break;
  3971. case BuiltinProc_kmag: x->value = exact_value_kmag(x->value); break;
  3972. }
  3973. } else {
  3974. x->mode = Addressing_Value;
  3975. }
  3976. BasicKind kind = core_type(x->type)->Basic.kind;
  3977. switch (kind) {
  3978. case Basic_quaternion128: x->type = t_f32; break;
  3979. case Basic_quaternion256: x->type = t_f64; break;
  3980. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  3981. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  3982. default: GB_PANIC("Invalid type"); break;
  3983. }
  3984. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3985. operand->type = type_hint;
  3986. }
  3987. break;
  3988. }
  3989. case BuiltinProc_conj: {
  3990. // conj :: proc(x: type) -> type
  3991. Operand *x = operand;
  3992. if (is_type_complex(x->type)) {
  3993. if (x->mode == Addressing_Constant) {
  3994. ExactValue v = exact_value_to_complex(x->value);
  3995. f64 r = v.value_complex.real;
  3996. f64 i = -v.value_complex.imag;
  3997. x->value = exact_value_complex(r, i);
  3998. x->mode = Addressing_Constant;
  3999. } else {
  4000. x->mode = Addressing_Value;
  4001. }
  4002. } else if (is_type_quaternion(x->type)) {
  4003. if (x->mode == Addressing_Constant) {
  4004. ExactValue v = exact_value_to_quaternion(x->value);
  4005. f64 r = v.value_quaternion.real;
  4006. f64 i = -v.value_quaternion.imag;
  4007. f64 j = -v.value_quaternion.jmag;
  4008. f64 k = -v.value_quaternion.kmag;
  4009. x->value = exact_value_quaternion(r, i, j, k);
  4010. x->mode = Addressing_Constant;
  4011. } else {
  4012. x->mode = Addressing_Value;
  4013. }
  4014. } else {
  4015. gbString s = type_to_string(x->type);
  4016. error(call, "Expected a complex or quaternion, got '%s'", s);
  4017. gb_string_free(s);
  4018. return false;
  4019. }
  4020. break;
  4021. }
  4022. case BuiltinProc_expand_to_tuple: {
  4023. Type *type = base_type(operand->type);
  4024. if (!is_type_struct(type) && !is_type_array(type)) {
  4025. gbString type_str = type_to_string(operand->type);
  4026. error(call, "Expected a struct or array type, got '%s'", type_str);
  4027. gb_string_free(type_str);
  4028. return false;
  4029. }
  4030. gbAllocator a = c->allocator;
  4031. Type *tuple = alloc_type_tuple();
  4032. if (is_type_struct(type)) {
  4033. isize variable_count = type->Struct.fields.count;
  4034. array_init(&tuple->Tuple.variables, a, variable_count);
  4035. // TODO(bill): Should I copy each of the entities or is this good enough?
  4036. gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
  4037. } else if (is_type_array(type)) {
  4038. isize variable_count = type->Array.count;
  4039. array_init(&tuple->Tuple.variables, a, variable_count);
  4040. for (isize i = 0; i < variable_count; i++) {
  4041. tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
  4042. }
  4043. }
  4044. operand->type = tuple;
  4045. operand->mode = Addressing_Value;
  4046. if (tuple->Tuple.variables.count == 1) {
  4047. operand->type = tuple->Tuple.variables[0]->type;
  4048. }
  4049. break;
  4050. }
  4051. case BuiltinProc_min: {
  4052. // min :: proc($T: typeid) -> ordered
  4053. // min :: proc(a: ..ordered) -> ordered
  4054. check_multi_expr_or_type(c, operand, ce->args[0]);
  4055. Type *original_type = operand->type;
  4056. Type *type = base_type(operand->type);
  4057. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  4058. // Okay
  4059. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4060. gbString type_str = type_to_string(original_type);
  4061. error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
  4062. gb_string_free(type_str);
  4063. return false;
  4064. }
  4065. if (operand->mode == Addressing_Type) {
  4066. if (is_type_boolean(type)) {
  4067. operand->mode = Addressing_Constant;
  4068. operand->type = original_type;
  4069. operand->value = exact_value_bool(false);
  4070. return true;
  4071. } else if (is_type_integer(type)) {
  4072. operand->mode = Addressing_Constant;
  4073. operand->type = original_type;
  4074. if (is_type_unsigned(type)) {
  4075. operand->value = exact_value_u64(0);
  4076. return true;
  4077. } else {
  4078. i64 sz = 8*type_size_of(type);
  4079. ExactValue a = exact_value_i64(1);
  4080. ExactValue b = exact_value_i64(sz-1);
  4081. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4082. v = exact_unary_operator_value(Token_Sub, v, cast(i32)sz, false);
  4083. operand->value = v;
  4084. return true;
  4085. }
  4086. } else if (is_type_float(type)) {
  4087. operand->mode = Addressing_Constant;
  4088. operand->type = original_type;
  4089. switch (type_size_of(type)) {
  4090. case 4:
  4091. operand->value = exact_value_float(-3.402823466e+38f);
  4092. break;
  4093. case 8:
  4094. operand->value = exact_value_float(-1.7976931348623158e+308);
  4095. break;
  4096. default:
  4097. GB_PANIC("Unhandled float type");
  4098. break;
  4099. }
  4100. return true;
  4101. } else if (is_type_enum(type)) {
  4102. operand->mode = Addressing_Constant;
  4103. operand->type = original_type;
  4104. operand->value = type->Enum.min_value;
  4105. return true;
  4106. } else if (is_type_enumerated_array(type)) {
  4107. Type *bt = base_type(type);
  4108. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  4109. operand->mode = Addressing_Constant;
  4110. operand->type = bt->EnumeratedArray.index;
  4111. operand->value = bt->EnumeratedArray.min_value;
  4112. return true;
  4113. }
  4114. gbString type_str = type_to_string(original_type);
  4115. error(call, "Invalid type for 'min', got %s", type_str);
  4116. gb_string_free(type_str);
  4117. return false;
  4118. }
  4119. bool all_constant = operand->mode == Addressing_Constant;
  4120. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  4121. defer (array_free(&operands));
  4122. array_add(&operands, *operand);
  4123. for (isize i = 1; i < ce->args.count; i++) {
  4124. Ast *other_arg = ce->args[i];
  4125. Operand b = {};
  4126. check_expr(c, &b, other_arg);
  4127. if (b.mode == Addressing_Invalid) {
  4128. return false;
  4129. }
  4130. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4131. gbString type_str = type_to_string(b.type);
  4132. error(call,
  4133. "Expected a ordered numeric type to 'min', got '%s'",
  4134. type_str);
  4135. gb_string_free(type_str);
  4136. return false;
  4137. }
  4138. array_add(&operands, b);
  4139. if (all_constant) {
  4140. all_constant = b.mode == Addressing_Constant;
  4141. }
  4142. }
  4143. if (all_constant) {
  4144. ExactValue value = operands[0].value;
  4145. Type *type = operands[0].type;
  4146. for (isize i = 1; i < operands.count; i++) {
  4147. Operand y = operands[i];
  4148. if (compare_exact_values(Token_Lt, value, y.value)) {
  4149. // okay
  4150. } else {
  4151. value = y.value;
  4152. type = y.type;
  4153. }
  4154. }
  4155. operand->value = value;
  4156. operand->type = type;
  4157. } else {
  4158. operand->mode = Addressing_Value;
  4159. operand->type = original_type;
  4160. for_array(i, operands) {
  4161. Operand *a = &operands[i];
  4162. for_array(j, operands) {
  4163. if (i == j) {
  4164. continue;
  4165. }
  4166. Operand *b = &operands[j];
  4167. convert_to_typed(c, a, b->type);
  4168. if (a->mode == Addressing_Invalid) {
  4169. return false;
  4170. }
  4171. convert_to_typed(c, b, a->type);
  4172. if (b->mode == Addressing_Invalid) {
  4173. return false;
  4174. }
  4175. }
  4176. }
  4177. for (isize i = 0; i < operands.count-1; i++) {
  4178. Operand *a = &operands[i];
  4179. Operand *b = &operands[i+1];
  4180. if (!are_types_identical(a->type, b->type)) {
  4181. gbString type_a = type_to_string(a->type);
  4182. gbString type_b = type_to_string(b->type);
  4183. error(a->expr,
  4184. "Mismatched types to 'min', '%s' vs '%s'",
  4185. type_a, type_b);
  4186. gb_string_free(type_b);
  4187. gb_string_free(type_a);
  4188. return false;
  4189. }
  4190. }
  4191. {
  4192. Type *bt = base_type(operands[0].type);
  4193. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "min_f32");
  4194. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "min_f64");
  4195. operand->type = operands[0].type;
  4196. }
  4197. }
  4198. break;
  4199. }
  4200. case BuiltinProc_max: {
  4201. // max :: proc($T: typeid) -> ordered
  4202. // max :: proc(a: ..ordered) -> ordered
  4203. check_multi_expr_or_type(c, operand, ce->args[0]);
  4204. Type *original_type = operand->type;
  4205. Type *type = base_type(operand->type);
  4206. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  4207. // Okay
  4208. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4209. gbString type_str = type_to_string(original_type);
  4210. error(call, "Expected a ordered numeric type to 'max', got '%s'", type_str);
  4211. gb_string_free(type_str);
  4212. return false;
  4213. }
  4214. if (operand->mode == Addressing_Type) {
  4215. if (is_type_boolean(type)) {
  4216. operand->mode = Addressing_Constant;
  4217. operand->type = original_type;
  4218. operand->value = exact_value_bool(true);
  4219. return true;
  4220. } else if (is_type_integer(type)) {
  4221. operand->mode = Addressing_Constant;
  4222. operand->type = original_type;
  4223. if (is_type_unsigned(type)) {
  4224. i64 sz = 8*type_size_of(type);
  4225. ExactValue a = exact_value_i64(1);
  4226. ExactValue b = exact_value_i64(sz);
  4227. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4228. v = exact_binary_operator_value(Token_Sub, v, a);
  4229. operand->value = v;
  4230. return true;
  4231. } else {
  4232. i64 sz = 8*type_size_of(type);
  4233. ExactValue a = exact_value_i64(1);
  4234. ExactValue b = exact_value_i64(sz-1);
  4235. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4236. v = exact_binary_operator_value(Token_Sub, v, a);
  4237. operand->value = v;
  4238. return true;
  4239. }
  4240. } else if (is_type_float(type)) {
  4241. operand->mode = Addressing_Constant;
  4242. operand->type = original_type;
  4243. switch (type_size_of(type)) {
  4244. case 4:
  4245. operand->value = exact_value_float(3.402823466e+38f);
  4246. break;
  4247. case 8:
  4248. operand->value = exact_value_float(1.7976931348623158e+308);
  4249. break;
  4250. default:
  4251. GB_PANIC("Unhandled float type");
  4252. break;
  4253. }
  4254. return true;
  4255. } else if (is_type_enum(type)) {
  4256. operand->mode = Addressing_Constant;
  4257. operand->type = original_type;
  4258. operand->value = type->Enum.max_value;
  4259. return true;
  4260. } else if (is_type_enumerated_array(type)) {
  4261. Type *bt = base_type(type);
  4262. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  4263. operand->mode = Addressing_Constant;
  4264. operand->type = bt->EnumeratedArray.index;
  4265. operand->value = bt->EnumeratedArray.max_value;
  4266. return true;
  4267. }
  4268. gbString type_str = type_to_string(original_type);
  4269. error(call, "Invalid type for 'max', got %s", type_str);
  4270. gb_string_free(type_str);
  4271. return false;
  4272. }
  4273. bool all_constant = operand->mode == Addressing_Constant;
  4274. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  4275. defer (array_free(&operands));
  4276. array_add(&operands, *operand);
  4277. for (isize i = 1; i < ce->args.count; i++) {
  4278. Ast *arg = ce->args[i];
  4279. Operand b = {};
  4280. check_expr(c, &b, arg);
  4281. if (b.mode == Addressing_Invalid) {
  4282. return false;
  4283. }
  4284. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4285. gbString type_str = type_to_string(b.type);
  4286. error(arg,
  4287. "Expected a ordered numeric type to 'max', got '%s'",
  4288. type_str);
  4289. gb_string_free(type_str);
  4290. return false;
  4291. }
  4292. array_add(&operands, b);
  4293. if (all_constant) {
  4294. all_constant = b.mode == Addressing_Constant;
  4295. }
  4296. }
  4297. if (all_constant) {
  4298. ExactValue value = operands[0].value;
  4299. Type *type = operands[0].type;
  4300. for (isize i = 1; i < operands.count; i++) {
  4301. Operand y = operands[i];
  4302. if (compare_exact_values(Token_Gt, value, y.value)) {
  4303. // okay
  4304. } else {
  4305. type = y.type;
  4306. value = y.value;
  4307. }
  4308. }
  4309. operand->value = value;
  4310. operand->type = type;
  4311. } else {
  4312. operand->mode = Addressing_Value;
  4313. operand->type = original_type;
  4314. for_array(i, operands) {
  4315. Operand *a = &operands[i];
  4316. for_array(j, operands) {
  4317. if (i == j) {
  4318. continue;
  4319. }
  4320. Operand *b = &operands[j];
  4321. convert_to_typed(c, a, b->type);
  4322. if (a->mode == Addressing_Invalid) {
  4323. return false;
  4324. }
  4325. convert_to_typed(c, b, a->type);
  4326. if (b->mode == Addressing_Invalid) {
  4327. return false;
  4328. }
  4329. }
  4330. }
  4331. for (isize i = 0; i < operands.count-1; i++) {
  4332. Operand *a = &operands[i];
  4333. Operand *b = &operands[i+1];
  4334. if (!are_types_identical(a->type, b->type)) {
  4335. gbString type_a = type_to_string(a->type);
  4336. gbString type_b = type_to_string(b->type);
  4337. error(a->expr,
  4338. "Mismatched types to 'max', '%s' vs '%s'",
  4339. type_a, type_b);
  4340. gb_string_free(type_b);
  4341. gb_string_free(type_a);
  4342. return false;
  4343. }
  4344. }
  4345. {
  4346. Type *bt = base_type(operands[0].type);
  4347. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "max_f32");
  4348. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "max_f64");
  4349. operand->type = operands[0].type;
  4350. }
  4351. }
  4352. break;
  4353. }
  4354. case BuiltinProc_abs: {
  4355. // abs :: proc(n: numeric) -> numeric
  4356. if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) {
  4357. gbString type_str = type_to_string(operand->type);
  4358. error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
  4359. gb_string_free(type_str);
  4360. return false;
  4361. }
  4362. if (operand->mode == Addressing_Constant) {
  4363. switch (operand->value.kind) {
  4364. case ExactValue_Integer:
  4365. operand->value.value_integer.neg = false;
  4366. break;
  4367. case ExactValue_Float:
  4368. operand->value.value_float = gb_abs(operand->value.value_float);
  4369. break;
  4370. case ExactValue_Complex: {
  4371. f64 r = operand->value.value_complex.real;
  4372. f64 i = operand->value.value_complex.imag;
  4373. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  4374. break;
  4375. }
  4376. default:
  4377. GB_PANIC("Invalid numeric constant");
  4378. break;
  4379. }
  4380. } else {
  4381. operand->mode = Addressing_Value;
  4382. {
  4383. Type *bt = base_type(operand->type);
  4384. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "abs_f32");
  4385. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "abs_f64");
  4386. if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64");
  4387. if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128");
  4388. if (are_types_identical(bt, t_quaternion128)) add_package_dependency(c, "runtime", "abs_quaternion128");
  4389. if (are_types_identical(bt, t_quaternion256)) add_package_dependency(c, "runtime", "abs_quaternion256");
  4390. }
  4391. }
  4392. if (is_type_complex(operand->type)) {
  4393. operand->type = base_complex_elem_type(operand->type);
  4394. }
  4395. GB_ASSERT(!is_type_complex(operand->type));
  4396. break;
  4397. }
  4398. case BuiltinProc_clamp: {
  4399. // clamp :: proc(a, min, max: ordered) -> ordered
  4400. Type *type = operand->type;
  4401. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4402. gbString type_str = type_to_string(operand->type);
  4403. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4404. gb_string_free(type_str);
  4405. return false;
  4406. }
  4407. Ast *min_arg = ce->args[1];
  4408. Ast *max_arg = ce->args[2];
  4409. Operand x = *operand;
  4410. Operand y = {};
  4411. Operand z = {};
  4412. check_expr(c, &y, min_arg);
  4413. if (y.mode == Addressing_Invalid) {
  4414. return false;
  4415. }
  4416. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  4417. gbString type_str = type_to_string(y.type);
  4418. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4419. gb_string_free(type_str);
  4420. return false;
  4421. }
  4422. check_expr(c, &z, max_arg);
  4423. if (z.mode == Addressing_Invalid) {
  4424. return false;
  4425. }
  4426. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  4427. gbString type_str = type_to_string(z.type);
  4428. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4429. gb_string_free(type_str);
  4430. return false;
  4431. }
  4432. if (x.mode == Addressing_Constant &&
  4433. y.mode == Addressing_Constant &&
  4434. z.mode == Addressing_Constant) {
  4435. ExactValue a = x.value;
  4436. ExactValue b = y.value;
  4437. ExactValue c = z.value;
  4438. operand->mode = Addressing_Constant;
  4439. if (compare_exact_values(Token_Lt, a, b)) {
  4440. operand->value = b;
  4441. operand->type = y.type;
  4442. } else if (compare_exact_values(Token_Gt, a, c)) {
  4443. operand->value = c;
  4444. operand->type = z.type;
  4445. } else {
  4446. operand->value = a;
  4447. operand->type = x.type;
  4448. }
  4449. } else {
  4450. operand->mode = Addressing_Value;
  4451. operand->type = type;
  4452. Operand *ops[3] = {&x, &y, &z};
  4453. for (isize i = 0; i < 3; i++) {
  4454. Operand *a = ops[i];
  4455. for (isize j = 0; j < 3; j++) {
  4456. if (i == j) continue;
  4457. Operand *b = ops[j];
  4458. convert_to_typed(c, a, b->type);
  4459. if (a->mode == Addressing_Invalid) { return false; }
  4460. }
  4461. }
  4462. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  4463. gbString type_x = type_to_string(x.type);
  4464. gbString type_y = type_to_string(y.type);
  4465. gbString type_z = type_to_string(z.type);
  4466. error(call,
  4467. "Mismatched types to 'clamp', '%s', '%s', '%s'",
  4468. type_x, type_y, type_z);
  4469. gb_string_free(type_z);
  4470. gb_string_free(type_y);
  4471. gb_string_free(type_x);
  4472. return false;
  4473. }
  4474. {
  4475. Type *bt = base_type(x.type);
  4476. if (are_types_identical(bt, t_f32)) {
  4477. add_package_dependency(c, "runtime", "min_f32");
  4478. add_package_dependency(c, "runtime", "max_f32");
  4479. }
  4480. if (are_types_identical(bt, t_f64)) {
  4481. add_package_dependency(c, "runtime", "min_f64");
  4482. add_package_dependency(c, "runtime", "max_f64");
  4483. }
  4484. operand->type = ops[0]->type;
  4485. }
  4486. }
  4487. break;
  4488. }
  4489. case BuiltinProc_simd_vector: {
  4490. Operand x = {};
  4491. Operand y = {};
  4492. x = *operand;
  4493. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  4494. error(call, "Expected a constant integer for 'intrinsics.simd_vector'");
  4495. operand->mode = Addressing_Type;
  4496. operand->type = t_invalid;
  4497. return false;
  4498. }
  4499. if (x.value.value_integer.neg) {
  4500. error(call, "Negative vector element length");
  4501. operand->mode = Addressing_Type;
  4502. operand->type = t_invalid;
  4503. return false;
  4504. }
  4505. i64 count = big_int_to_i64(&x.value.value_integer);
  4506. check_expr_or_type(c, &y, ce->args[1]);
  4507. if (y.mode != Addressing_Type) {
  4508. error(call, "Expected a type 'intrinsics.simd_vector'");
  4509. operand->mode = Addressing_Type;
  4510. operand->type = t_invalid;
  4511. return false;
  4512. }
  4513. Type *elem = y.type;
  4514. if (!is_type_valid_vector_elem(elem)) {
  4515. gbString str = type_to_string(elem);
  4516. error(call, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  4517. gb_string_free(str);
  4518. operand->mode = Addressing_Type;
  4519. operand->type = t_invalid;
  4520. return false;
  4521. }
  4522. operand->mode = Addressing_Type;
  4523. operand->type = alloc_type_simd_vector(count, elem);
  4524. break;
  4525. }
  4526. case BuiltinProc_soa_struct: {
  4527. Operand x = {};
  4528. Operand y = {};
  4529. x = *operand;
  4530. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  4531. error(call, "Expected a constant integer for 'intrinsics.soa_struct'");
  4532. operand->mode = Addressing_Type;
  4533. operand->type = t_invalid;
  4534. return false;
  4535. }
  4536. if (x.value.value_integer.neg) {
  4537. error(call, "Negative array element length");
  4538. operand->mode = Addressing_Type;
  4539. operand->type = t_invalid;
  4540. return false;
  4541. }
  4542. i64 count = big_int_to_i64(&x.value.value_integer);
  4543. check_expr_or_type(c, &y, ce->args[1]);
  4544. if (y.mode != Addressing_Type) {
  4545. error(call, "Expected a type 'intrinsics.soa_struct'");
  4546. operand->mode = Addressing_Type;
  4547. operand->type = t_invalid;
  4548. return false;
  4549. }
  4550. Type *elem = y.type;
  4551. Type *bt_elem = base_type(elem);
  4552. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  4553. gbString str = type_to_string(elem);
  4554. error(call, "Invalid type for 'intrinsics.soa_struct', expected a struct or array of length 4 or below, got '%s'", str);
  4555. gb_string_free(str);
  4556. operand->mode = Addressing_Type;
  4557. operand->type = t_invalid;
  4558. return false;
  4559. }
  4560. operand->mode = Addressing_Type;
  4561. Type *soa_struct = nullptr;
  4562. Scope *scope = nullptr;
  4563. if (is_type_array(elem)) {
  4564. Type *old_array = base_type(elem);
  4565. soa_struct = alloc_type_struct();
  4566. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
  4567. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
  4568. soa_struct->Struct.node = operand->expr;
  4569. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  4570. soa_struct->Struct.soa_elem = elem;
  4571. soa_struct->Struct.soa_count = count;
  4572. scope = create_scope(c->scope, c->allocator);
  4573. soa_struct->Struct.scope = scope;
  4574. String params_xyzw[4] = {
  4575. str_lit("x"),
  4576. str_lit("y"),
  4577. str_lit("z"),
  4578. str_lit("w")
  4579. };
  4580. for (i64 i = 0; i < old_array->Array.count; i++) {
  4581. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  4582. Token token = {};
  4583. token.string = params_xyzw[i];
  4584. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  4585. soa_struct->Struct.fields[i] = new_field;
  4586. add_entity(c->checker, scope, nullptr, new_field);
  4587. add_entity_use(c, nullptr, new_field);
  4588. }
  4589. } else {
  4590. GB_ASSERT(is_type_struct(elem));
  4591. Type *old_struct = base_type(elem);
  4592. soa_struct = alloc_type_struct();
  4593. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  4594. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count);
  4595. soa_struct->Struct.node = operand->expr;
  4596. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  4597. soa_struct->Struct.soa_elem = elem;
  4598. soa_struct->Struct.soa_count = count;
  4599. scope = create_scope(old_struct->Struct.scope->parent, c->allocator);
  4600. soa_struct->Struct.scope = scope;
  4601. for_array(i, old_struct->Struct.fields) {
  4602. Entity *old_field = old_struct->Struct.fields[i];
  4603. if (old_field->kind == Entity_Variable) {
  4604. Type *array_type = alloc_type_array(old_field->type, count);
  4605. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  4606. soa_struct->Struct.fields[i] = new_field;
  4607. add_entity(c->checker, scope, nullptr, new_field);
  4608. } else {
  4609. soa_struct->Struct.fields[i] = old_field;
  4610. }
  4611. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  4612. }
  4613. }
  4614. Token token = {};
  4615. token.string = str_lit("Base_Type");
  4616. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  4617. add_entity(c->checker, scope, nullptr, base_type_entity);
  4618. add_type_info_type(c, soa_struct);
  4619. operand->type = soa_struct;
  4620. break;
  4621. }
  4622. case BuiltinProc_cpu_relax:
  4623. operand->mode = Addressing_NoValue;
  4624. break;
  4625. case BuiltinProc_atomic_fence:
  4626. case BuiltinProc_atomic_fence_acq:
  4627. case BuiltinProc_atomic_fence_rel:
  4628. case BuiltinProc_atomic_fence_acqrel:
  4629. operand->mode = Addressing_NoValue;
  4630. break;
  4631. case BuiltinProc_atomic_store:
  4632. case BuiltinProc_atomic_store_rel:
  4633. case BuiltinProc_atomic_store_relaxed:
  4634. case BuiltinProc_atomic_store_unordered:
  4635. {
  4636. Type *elem = nullptr;
  4637. if (!is_type_normal_pointer(operand->type, &elem)) {
  4638. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4639. return false;
  4640. }
  4641. Operand x = {};
  4642. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4643. check_assignment(c, &x, elem, builtin_name);
  4644. operand->type = nullptr;
  4645. operand->mode = Addressing_NoValue;
  4646. break;
  4647. }
  4648. case BuiltinProc_atomic_load:
  4649. case BuiltinProc_atomic_load_acq:
  4650. case BuiltinProc_atomic_load_relaxed:
  4651. case BuiltinProc_atomic_load_unordered:
  4652. {
  4653. Type *elem = nullptr;
  4654. if (!is_type_normal_pointer(operand->type, &elem)) {
  4655. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4656. return false;
  4657. }
  4658. operand->type = elem;
  4659. operand->mode = Addressing_Value;
  4660. break;
  4661. }
  4662. case BuiltinProc_atomic_add:
  4663. case BuiltinProc_atomic_add_acq:
  4664. case BuiltinProc_atomic_add_rel:
  4665. case BuiltinProc_atomic_add_acqrel:
  4666. case BuiltinProc_atomic_add_relaxed:
  4667. case BuiltinProc_atomic_sub:
  4668. case BuiltinProc_atomic_sub_acq:
  4669. case BuiltinProc_atomic_sub_rel:
  4670. case BuiltinProc_atomic_sub_acqrel:
  4671. case BuiltinProc_atomic_sub_relaxed:
  4672. case BuiltinProc_atomic_and:
  4673. case BuiltinProc_atomic_and_acq:
  4674. case BuiltinProc_atomic_and_rel:
  4675. case BuiltinProc_atomic_and_acqrel:
  4676. case BuiltinProc_atomic_and_relaxed:
  4677. case BuiltinProc_atomic_nand:
  4678. case BuiltinProc_atomic_nand_acq:
  4679. case BuiltinProc_atomic_nand_rel:
  4680. case BuiltinProc_atomic_nand_acqrel:
  4681. case BuiltinProc_atomic_nand_relaxed:
  4682. case BuiltinProc_atomic_or:
  4683. case BuiltinProc_atomic_or_acq:
  4684. case BuiltinProc_atomic_or_rel:
  4685. case BuiltinProc_atomic_or_acqrel:
  4686. case BuiltinProc_atomic_or_relaxed:
  4687. case BuiltinProc_atomic_xor:
  4688. case BuiltinProc_atomic_xor_acq:
  4689. case BuiltinProc_atomic_xor_rel:
  4690. case BuiltinProc_atomic_xor_acqrel:
  4691. case BuiltinProc_atomic_xor_relaxed:
  4692. case BuiltinProc_atomic_xchg:
  4693. case BuiltinProc_atomic_xchg_acq:
  4694. case BuiltinProc_atomic_xchg_rel:
  4695. case BuiltinProc_atomic_xchg_acqrel:
  4696. case BuiltinProc_atomic_xchg_relaxed:
  4697. {
  4698. Type *elem = nullptr;
  4699. if (!is_type_normal_pointer(operand->type, &elem)) {
  4700. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4701. return false;
  4702. }
  4703. Operand x = {};
  4704. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4705. check_assignment(c, &x, elem, builtin_name);
  4706. operand->type = elem;
  4707. operand->mode = Addressing_Value;
  4708. break;
  4709. }
  4710. case BuiltinProc_atomic_cxchg:
  4711. case BuiltinProc_atomic_cxchg_acq:
  4712. case BuiltinProc_atomic_cxchg_rel:
  4713. case BuiltinProc_atomic_cxchg_acqrel:
  4714. case BuiltinProc_atomic_cxchg_relaxed:
  4715. case BuiltinProc_atomic_cxchg_failrelaxed:
  4716. case BuiltinProc_atomic_cxchg_failacq:
  4717. case BuiltinProc_atomic_cxchg_acq_failrelaxed:
  4718. case BuiltinProc_atomic_cxchg_acqrel_failrelaxed:
  4719. case BuiltinProc_atomic_cxchgweak:
  4720. case BuiltinProc_atomic_cxchgweak_acq:
  4721. case BuiltinProc_atomic_cxchgweak_rel:
  4722. case BuiltinProc_atomic_cxchgweak_acqrel:
  4723. case BuiltinProc_atomic_cxchgweak_relaxed:
  4724. case BuiltinProc_atomic_cxchgweak_failrelaxed:
  4725. case BuiltinProc_atomic_cxchgweak_failacq:
  4726. case BuiltinProc_atomic_cxchgweak_acq_failrelaxed:
  4727. case BuiltinProc_atomic_cxchgweak_acqrel_failrelaxed:
  4728. {
  4729. Type *elem = nullptr;
  4730. if (!is_type_normal_pointer(operand->type, &elem)) {
  4731. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4732. return false;
  4733. }
  4734. Operand x = {};
  4735. Operand y = {};
  4736. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4737. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  4738. check_assignment(c, &x, elem, builtin_name);
  4739. check_assignment(c, &y, elem, builtin_name);
  4740. operand->mode = Addressing_Value;
  4741. operand->type = make_optional_ok_type(elem);
  4742. break;
  4743. }
  4744. break;
  4745. case BuiltinProc_type_base_type:
  4746. if (operand->mode != Addressing_Type) {
  4747. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4748. } else {
  4749. operand->type = base_type(operand->type);
  4750. }
  4751. operand->mode = Addressing_Type;
  4752. break;
  4753. case BuiltinProc_type_core_type:
  4754. if (operand->mode != Addressing_Type) {
  4755. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4756. } else {
  4757. operand->type = core_type(operand->type);
  4758. }
  4759. operand->mode = Addressing_Type;
  4760. break;
  4761. case BuiltinProc_type_elem_type:
  4762. if (operand->mode != Addressing_Type) {
  4763. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4764. } else {
  4765. Type *bt = base_type(operand->type);
  4766. switch (bt->kind) {
  4767. case Type_Basic:
  4768. switch (bt->Basic.kind) {
  4769. case Basic_complex64: operand->type = t_f32; break;
  4770. case Basic_complex128: operand->type = t_f64; break;
  4771. }
  4772. break;
  4773. case Type_Pointer: operand->type = bt->Pointer.elem; break;
  4774. case Type_Opaque: operand->type = bt->Opaque.elem; break;
  4775. case Type_Array: operand->type = bt->Array.elem; break;
  4776. case Type_Slice: operand->type = bt->Slice.elem; break;
  4777. case Type_DynamicArray: operand->type = bt->DynamicArray.elem; break;
  4778. }
  4779. }
  4780. operand->mode = Addressing_Type;
  4781. break;
  4782. case BuiltinProc_type_is_boolean:
  4783. case BuiltinProc_type_is_integer:
  4784. case BuiltinProc_type_is_rune:
  4785. case BuiltinProc_type_is_float:
  4786. case BuiltinProc_type_is_complex:
  4787. case BuiltinProc_type_is_quaternion:
  4788. case BuiltinProc_type_is_string:
  4789. case BuiltinProc_type_is_typeid:
  4790. case BuiltinProc_type_is_any:
  4791. case BuiltinProc_type_is_endian_little:
  4792. case BuiltinProc_type_is_endian_big:
  4793. case BuiltinProc_type_is_unsigned:
  4794. case BuiltinProc_type_is_numeric:
  4795. case BuiltinProc_type_is_ordered:
  4796. case BuiltinProc_type_is_ordered_numeric:
  4797. case BuiltinProc_type_is_indexable:
  4798. case BuiltinProc_type_is_sliceable:
  4799. case BuiltinProc_type_is_simple_compare:
  4800. case BuiltinProc_type_is_dereferenceable:
  4801. case BuiltinProc_type_is_valid_map_key:
  4802. case BuiltinProc_type_is_named:
  4803. case BuiltinProc_type_is_pointer:
  4804. case BuiltinProc_type_is_opaque:
  4805. case BuiltinProc_type_is_array:
  4806. case BuiltinProc_type_is_slice:
  4807. case BuiltinProc_type_is_dynamic_array:
  4808. case BuiltinProc_type_is_map:
  4809. case BuiltinProc_type_is_struct:
  4810. case BuiltinProc_type_is_union:
  4811. case BuiltinProc_type_is_enum:
  4812. case BuiltinProc_type_is_proc:
  4813. case BuiltinProc_type_is_bit_field:
  4814. case BuiltinProc_type_is_bit_field_value:
  4815. case BuiltinProc_type_is_bit_set:
  4816. case BuiltinProc_type_is_simd_vector:
  4817. case BuiltinProc_type_is_specialized_polymorphic_record:
  4818. case BuiltinProc_type_is_unspecialized_polymorphic_record:
  4819. case BuiltinProc_type_has_nil:
  4820. GB_ASSERT(BuiltinProc__type_simple_boolean_begin < id && id < BuiltinProc__type_simple_boolean_end);
  4821. operand->value = exact_value_bool(false);
  4822. if (operand->mode != Addressing_Type) {
  4823. gbString str = expr_to_string(ce->args[0]);
  4824. error(operand->expr, "Expected a type for '%.*s', got '%s'", LIT(builtin_name), str);
  4825. gb_string_free(str);
  4826. } else {
  4827. i32 i = id - cast(i32)BuiltinProc__type_simple_boolean_begin;
  4828. auto procedure = builtin_type_is_procs[i];
  4829. GB_ASSERT_MSG(procedure != nullptr, "%.*s", LIT(builtin_name));
  4830. operand->value = exact_value_bool(procedure(operand->type));
  4831. }
  4832. operand->mode = Addressing_Constant;
  4833. operand->type = t_untyped_bool;
  4834. break;
  4835. case BuiltinProc_type_is_specialization_of:
  4836. {
  4837. if (operand->mode != Addressing_Type) {
  4838. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4839. operand->mode = Addressing_Invalid;
  4840. operand->type = t_invalid;
  4841. return false;
  4842. }
  4843. Type *t = operand->type;
  4844. Type *s = nullptr;
  4845. bool prev_ips = c->in_polymorphic_specialization;
  4846. c->in_polymorphic_specialization = true;
  4847. s = check_type(c, ce->args[1]);
  4848. c->in_polymorphic_specialization = prev_ips;
  4849. if (s == t_invalid) {
  4850. error(ce->args[1], "Invalid specialization type for '%.*s'", LIT(builtin_name));
  4851. operand->mode = Addressing_Invalid;
  4852. operand->type = t_invalid;
  4853. return false;
  4854. }
  4855. operand->mode = Addressing_Constant;
  4856. operand->type = t_untyped_bool;
  4857. operand->value = exact_value_bool(check_type_specialization_to(c, s, t, false, false));
  4858. }
  4859. break;
  4860. case BuiltinProc_type_proc_parameter_count:
  4861. operand->value = exact_value_i64(0);
  4862. if (operand->mode != Addressing_Type) {
  4863. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  4864. } else if (!is_type_proc(operand->type)) {
  4865. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  4866. } else {
  4867. Type *bt = base_type(operand->type);
  4868. operand->value = exact_value_i64(bt->Proc.param_count);
  4869. }
  4870. operand->mode = Addressing_Constant;
  4871. operand->type = t_untyped_integer;
  4872. break;
  4873. case BuiltinProc_type_proc_return_count:
  4874. operand->value = exact_value_i64(0);
  4875. if (operand->mode != Addressing_Type) {
  4876. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  4877. } else if (!is_type_proc(operand->type)) {
  4878. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  4879. } else {
  4880. Type *bt = base_type(operand->type);
  4881. operand->value = exact_value_i64(bt->Proc.result_count);
  4882. }
  4883. operand->mode = Addressing_Constant;
  4884. operand->type = t_untyped_integer;
  4885. break;
  4886. case BuiltinProc_type_polymorphic_record_parameter_count:
  4887. operand->value = exact_value_i64(0);
  4888. if (operand->mode != Addressing_Type) {
  4889. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  4890. } else {
  4891. Type *bt = base_type(operand->type);
  4892. if (bt->kind == Type_Struct) {
  4893. if (bt->Struct.polymorphic_params != nullptr) {
  4894. operand->value = exact_value_i64(bt->Struct.polymorphic_params->Tuple.variables.count);
  4895. }
  4896. } else if (bt->kind == Type_Union) {
  4897. if (bt->Union.polymorphic_params != nullptr) {
  4898. operand->value = exact_value_i64(bt->Union.polymorphic_params->Tuple.variables.count);
  4899. }
  4900. } else {
  4901. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  4902. }
  4903. }
  4904. operand->mode = Addressing_Constant;
  4905. operand->type = t_untyped_integer;
  4906. break;
  4907. case BuiltinProc_type_polymorphic_record_parameter_value:
  4908. if (operand->mode != Addressing_Type) {
  4909. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  4910. return false;
  4911. } else if (!is_type_polymorphic_record_specialized(operand->type)) {
  4912. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  4913. return false;
  4914. } else {
  4915. Operand op = {};
  4916. check_expr(c, &op, ce->args[1]);
  4917. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  4918. error(op.expr, "Execpted a constant integer for the index of record parameter value");
  4919. return false;
  4920. }
  4921. i64 index = exact_value_to_i64(op.value);
  4922. if (index < 0) {
  4923. error(op.expr, "Execpted a non-negative integer for the index of record parameter value, got %lld", cast(long long)index);
  4924. return false;
  4925. }
  4926. Entity *param = nullptr;
  4927. i64 count = 0;
  4928. Type *bt = base_type(operand->type);
  4929. if (bt->kind == Type_Struct) {
  4930. if (bt->Struct.polymorphic_params != nullptr) {
  4931. count = bt->Struct.polymorphic_params->Tuple.variables.count;
  4932. if (index < count) {
  4933. param = bt->Struct.polymorphic_params->Tuple.variables[cast(isize)index];
  4934. }
  4935. }
  4936. } else if (bt->kind == Type_Union) {
  4937. if (bt->Union.polymorphic_params != nullptr) {
  4938. count = bt->Union.polymorphic_params->Tuple.variables.count;
  4939. if (index < count) {
  4940. param = bt->Union.polymorphic_params->Tuple.variables[cast(isize)index];
  4941. }
  4942. }
  4943. } else {
  4944. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  4945. return false;
  4946. }
  4947. if (index >= count) {
  4948. error(op.expr, "Index of record parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  4949. return false;
  4950. }
  4951. GB_ASSERT(param != nullptr);
  4952. switch (param->kind) {
  4953. case Entity_Constant:
  4954. operand->mode = Addressing_Constant;
  4955. operand->type = param->type;
  4956. operand->value = param->Constant.value;
  4957. break;
  4958. case Entity_TypeName:
  4959. operand->mode = Addressing_Type;
  4960. operand->type = param->type;
  4961. break;
  4962. default:
  4963. GB_PANIC("Unhandle polymorphic record type");
  4964. break;
  4965. }
  4966. }
  4967. break;
  4968. }
  4969. return true;
  4970. }
  4971. isize add_dependencies_from_unpacking(CheckerContext *c, Entity **lhs, isize lhs_count, isize tuple_index, isize tuple_count) {
  4972. if (lhs != nullptr) {
  4973. for (isize j = 0; (tuple_index + j) < lhs_count && j < tuple_count; j++) {
  4974. Entity *e = lhs[tuple_index + j];
  4975. if (e != nullptr) {
  4976. DeclInfo *decl = decl_info_of_entity(e);
  4977. if (decl != nullptr) {
  4978. c->decl = decl; // will be reset by the 'defer' any way
  4979. for_array(k, decl->deps.entries) {
  4980. Entity *dep = decl->deps.entries[k].ptr;
  4981. add_declaration_dependency(c, dep); // TODO(bill): Should this be here?
  4982. }
  4983. }
  4984. }
  4985. }
  4986. }
  4987. return tuple_count;
  4988. }
  4989. bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs, Array<Operand> *operands, Array<Ast *> const &rhs) {
  4990. bool optional_ok = false;
  4991. isize tuple_index = 0;
  4992. for_array(i, rhs) {
  4993. CheckerContext c_ = *ctx;
  4994. CheckerContext *c = &c_;
  4995. Operand o = {};
  4996. Type *type_hint = nullptr;
  4997. if (tuple_index < lhs.count) {
  4998. type_hint = lhs[tuple_index].type;
  4999. }
  5000. check_expr_base(c, &o, rhs[i], type_hint);
  5001. if (o.mode == Addressing_NoValue) {
  5002. error_operand_no_value(&o);
  5003. o.mode = Addressing_Invalid;
  5004. }
  5005. if (o.type == nullptr || o.type->kind != Type_Tuple) {
  5006. if (lhs.count == 2 && rhs.count == 1 &&
  5007. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  5008. Type *tuple = make_optional_ok_type(o.type);
  5009. add_type_and_value(&c->checker->info, o.expr, o.mode, tuple, o.value);
  5010. Operand val = o;
  5011. Operand ok = o;
  5012. val.mode = Addressing_Value;
  5013. ok.mode = Addressing_Value;
  5014. ok.type = t_untyped_bool;
  5015. array_add(operands, val);
  5016. array_add(operands, ok);
  5017. optional_ok = true;
  5018. tuple_index += 2;
  5019. } else {
  5020. array_add(operands, o);
  5021. tuple_index += 1;
  5022. }
  5023. } else {
  5024. TypeTuple *tuple = &o.type->Tuple;
  5025. for_array(j, tuple->variables) {
  5026. o.type = tuple->variables[j]->type;
  5027. array_add(operands, o);
  5028. }
  5029. isize count = tuple->variables.count;
  5030. tuple_index += 2;
  5031. }
  5032. }
  5033. return optional_ok;
  5034. }
  5035. bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Array<Ast *> const &rhs, bool allow_ok, bool is_variadic) {
  5036. bool optional_ok = false;
  5037. isize tuple_index = 0;
  5038. for_array(i, rhs) {
  5039. CheckerContext c_ = *ctx;
  5040. CheckerContext *c = &c_;
  5041. Operand o = {};
  5042. Type *type_hint = nullptr;
  5043. if (lhs != nullptr && tuple_index < lhs_count) {
  5044. // NOTE(bill): override DeclInfo for dependency
  5045. Entity *e = lhs[tuple_index];
  5046. if (e != nullptr) {
  5047. // DeclInfo *decl = decl_info_of_entity(e);
  5048. // if (decl) c->decl = decl;
  5049. type_hint = e->type;
  5050. if (e->flags & EntityFlag_Ellipsis) {
  5051. GB_ASSERT(is_type_slice(e->type));
  5052. GB_ASSERT(e->type->kind == Type_Slice);
  5053. type_hint = e->type->Slice.elem;
  5054. }
  5055. }
  5056. } else if (lhs != nullptr && tuple_index >= lhs_count && is_variadic) {
  5057. // NOTE(bill): override DeclInfo for dependency
  5058. Entity *e = lhs[lhs_count-1];
  5059. if (e != nullptr) {
  5060. // DeclInfo *decl = decl_info_of_entity(e);
  5061. // if (decl) c->decl = decl;
  5062. type_hint = e->type;
  5063. if (e->flags & EntityFlag_Ellipsis) {
  5064. GB_ASSERT(is_type_slice(e->type));
  5065. GB_ASSERT(e->type->kind == Type_Slice);
  5066. type_hint = e->type->Slice.elem;
  5067. }
  5068. }
  5069. }
  5070. check_expr_base(c, &o, rhs[i], type_hint);
  5071. if (o.mode == Addressing_NoValue) {
  5072. error_operand_no_value(&o);
  5073. o.mode = Addressing_Invalid;
  5074. }
  5075. if (o.type == nullptr || o.type->kind != Type_Tuple) {
  5076. if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
  5077. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  5078. Type *tuple = make_optional_ok_type(o.type);
  5079. add_type_and_value(&c->checker->info, o.expr, o.mode, tuple, o.value);
  5080. Operand val = o;
  5081. Operand ok = o;
  5082. val.mode = Addressing_Value;
  5083. ok.mode = Addressing_Value;
  5084. // ok.type = t_bool;
  5085. ok.type = t_untyped_bool;
  5086. array_add(operands, val);
  5087. array_add(operands, ok);
  5088. optional_ok = true;
  5089. tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, 2);
  5090. } else {
  5091. array_add(operands, o);
  5092. tuple_index += 1;
  5093. }
  5094. } else {
  5095. TypeTuple *tuple = &o.type->Tuple;
  5096. for_array(j, tuple->variables) {
  5097. o.type = tuple->variables[j]->type;
  5098. array_add(operands, o);
  5099. }
  5100. isize count = tuple->variables.count;
  5101. tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, count);
  5102. }
  5103. }
  5104. return optional_ok;
  5105. }
  5106. CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
  5107. ast_node(ce, CallExpr, call);
  5108. GB_ASSERT(is_type_proc(proc_type));
  5109. proc_type = base_type(proc_type);
  5110. TypeProc *pt = &proc_type->Proc;
  5111. isize param_count = 0;
  5112. isize param_count_excluding_defaults = 0;
  5113. bool variadic = pt->variadic;
  5114. bool vari_expand = (ce->ellipsis.pos.line != 0);
  5115. i64 score = 0;
  5116. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  5117. TypeTuple *param_tuple = nullptr;
  5118. if (pt->params != nullptr) {
  5119. param_tuple = &pt->params->Tuple;
  5120. param_count = param_tuple->variables.count;
  5121. if (variadic) {
  5122. for (isize i = param_count-1; i >= 0; i--) {
  5123. Entity *e = param_tuple->variables[i];
  5124. if (e->kind == Entity_TypeName) {
  5125. break;
  5126. }
  5127. if (e->kind == Entity_Variable) {
  5128. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5129. param_count--;
  5130. continue;
  5131. }
  5132. }
  5133. break;
  5134. }
  5135. param_count--;
  5136. }
  5137. }
  5138. param_count_excluding_defaults = param_count;
  5139. if (param_tuple != nullptr) {
  5140. for (isize i = param_count-1; i >= 0; i--) {
  5141. Entity *e = param_tuple->variables[i];
  5142. if (e->kind == Entity_TypeName) {
  5143. break;
  5144. }
  5145. if (e->kind == Entity_Variable) {
  5146. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5147. param_count_excluding_defaults--;
  5148. continue;
  5149. }
  5150. }
  5151. break;
  5152. }
  5153. }
  5154. CallArgumentError err = CallArgumentError_None;
  5155. Type *final_proc_type = proc_type;
  5156. Entity *gen_entity = nullptr;
  5157. if (vari_expand && !variadic) {
  5158. if (show_error) {
  5159. error(ce->ellipsis,
  5160. "Cannot use '..' in call to a non-variadic procedure: '%.*s'",
  5161. LIT(ce->proc->Ident.token.string));
  5162. }
  5163. err = CallArgumentError_NonVariadicExpand;
  5164. } else if (vari_expand && pt->c_vararg) {
  5165. if (show_error) {
  5166. error(ce->ellipsis,
  5167. "Cannot use '..' in call to a '#c_vararg' variadic procedure: '%.*s'",
  5168. LIT(ce->proc->Ident.token.string));
  5169. }
  5170. err = CallArgumentError_NonVariadicExpand;
  5171. } else if (operands.count == 0 && param_count_excluding_defaults == 0) {
  5172. err = CallArgumentError_None;
  5173. if (variadic) {
  5174. GB_ASSERT(param_tuple != nullptr && param_tuple->variables.count > 0);
  5175. Type *t = param_tuple->variables[0]->type;
  5176. if (is_type_polymorphic(t)) {
  5177. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5178. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5179. }
  5180. }
  5181. } else {
  5182. i32 error_code = 0;
  5183. if (operands.count < param_count_excluding_defaults) {
  5184. error_code = -1;
  5185. } else if (!variadic && operands.count > param_count) {
  5186. error_code = +1;
  5187. }
  5188. if (error_code != 0) {
  5189. err = CallArgumentError_TooManyArguments;
  5190. char const *err_fmt = "Too many arguments for '%s', expected %td arguments";
  5191. if (error_code < 0) {
  5192. err = CallArgumentError_TooFewArguments;
  5193. err_fmt = "Too few arguments for '%s', expected %td arguments";
  5194. }
  5195. if (show_error) {
  5196. gbString proc_str = expr_to_string(ce->proc);
  5197. error(call, err_fmt, proc_str, param_count_excluding_defaults);
  5198. gb_string_free(proc_str);
  5199. }
  5200. } else {
  5201. // NOTE(bill): Generate the procedure type for this generic instance
  5202. PolyProcData poly_proc_data = {};
  5203. if (pt->is_polymorphic && !pt->is_poly_specialized) {
  5204. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &operands, call, &poly_proc_data)) {
  5205. gen_entity = poly_proc_data.gen_entity;
  5206. GB_ASSERT(is_type_proc(gen_entity->type));
  5207. final_proc_type = gen_entity->type;
  5208. } else {
  5209. err = CallArgumentError_WrongTypes;
  5210. }
  5211. }
  5212. GB_ASSERT(is_type_proc(final_proc_type));
  5213. TypeProc *pt = &final_proc_type->Proc;
  5214. GB_ASSERT(pt->params != nullptr);
  5215. auto sig_params = pt->params->Tuple.variables;
  5216. isize operand_index = 0;
  5217. isize max_operand_count = gb_min(param_count, operands.count);
  5218. for (; operand_index < max_operand_count; operand_index++) {
  5219. Entity *e = sig_params[operand_index];
  5220. Type *t = e->type;
  5221. Operand o = operands[operand_index];
  5222. if (o.expr != nullptr) {
  5223. call->viral_state_flags |= o.expr->viral_state_flags;
  5224. }
  5225. if (e->kind == Entity_TypeName) {
  5226. // GB_ASSERT(!variadic);
  5227. if (o.mode == Addressing_Invalid) {
  5228. continue;
  5229. } else if (o.mode != Addressing_Type) {
  5230. if (show_error) {
  5231. error(o.expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  5232. }
  5233. err = CallArgumentError_WrongTypes;
  5234. }
  5235. if (are_types_identical(e->type, o.type)) {
  5236. score += assign_score_function(1);
  5237. } else {
  5238. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5239. }
  5240. continue;
  5241. }
  5242. bool param_is_variadic = pt->variadic && pt->variadic_index == operand_index;
  5243. i64 s = 0;
  5244. if (!check_is_assignable_to_with_score(c, &o, t, &s, param_is_variadic)) {
  5245. bool ok = false;
  5246. if (e->flags & EntityFlag_AutoCast) {
  5247. ok = check_is_castable_to(c, &o, t);
  5248. }
  5249. if (ok) {
  5250. s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5251. } else {
  5252. if (show_error) {
  5253. check_assignment(c, &o, t, str_lit("argument"));
  5254. }
  5255. err = CallArgumentError_WrongTypes;
  5256. }
  5257. }
  5258. score += s;
  5259. if (e->flags & EntityFlag_ConstInput) {
  5260. if (o.mode != Addressing_Constant) {
  5261. if (show_error) {
  5262. error(o.expr, "Expected a constant value for the argument '%.*s'", LIT(e->token.string));
  5263. }
  5264. err = CallArgumentError_NoneConstantParameter;
  5265. }
  5266. }
  5267. if (o.mode == Addressing_Type && is_type_typeid(e->type)) {
  5268. add_type_info_type(c, o.type);
  5269. add_type_and_value(c->info, o.expr, Addressing_Value, e->type, exact_value_typeid(o.type));
  5270. }
  5271. }
  5272. if (variadic) {
  5273. bool variadic_expand = false;
  5274. Type *slice = sig_params[param_count]->type;
  5275. GB_ASSERT(is_type_slice(slice));
  5276. Type *elem = base_type(slice)->Slice.elem;
  5277. Type *t = elem;
  5278. if (is_type_polymorphic(t)) {
  5279. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5280. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5281. }
  5282. for (; operand_index < operands.count; operand_index++) {
  5283. Operand o = operands[operand_index];
  5284. if (vari_expand) {
  5285. variadic_expand = true;
  5286. t = slice;
  5287. if (operand_index != param_count) {
  5288. if (show_error) {
  5289. error(o.expr, "'..' in a variadic procedure can only have one variadic argument at the end");
  5290. }
  5291. if (data) {
  5292. data->score = score;
  5293. data->result_type = final_proc_type->Proc.results;
  5294. data->gen_entity = gen_entity;
  5295. }
  5296. return CallArgumentError_MultipleVariadicExpand;
  5297. }
  5298. }
  5299. i64 s = 0;
  5300. if (!check_is_assignable_to_with_score(c, &o, t, &s, true)) {
  5301. if (show_error) {
  5302. check_assignment(c, &o, t, str_lit("argument"));
  5303. }
  5304. err = CallArgumentError_WrongTypes;
  5305. }
  5306. score += s;
  5307. if (is_type_any(elem)) {
  5308. add_type_info_type(c, o.type);
  5309. }
  5310. if (o.mode == Addressing_Type && is_type_typeid(t)) {
  5311. add_type_info_type(c, o.type);
  5312. add_type_and_value(c->info, o.expr, Addressing_Value, t, exact_value_typeid(o.type));
  5313. }
  5314. }
  5315. }
  5316. }
  5317. }
  5318. if (data) {
  5319. data->score = score;
  5320. data->result_type = final_proc_type->Proc.results;
  5321. data->gen_entity = gen_entity;
  5322. }
  5323. return err;
  5324. }
  5325. bool is_call_expr_field_value(AstCallExpr *ce) {
  5326. GB_ASSERT(ce != nullptr);
  5327. if (ce->args.count == 0) {
  5328. return false;
  5329. }
  5330. return ce->args[0]->kind == Ast_FieldValue;
  5331. }
  5332. isize lookup_procedure_parameter(TypeProc *pt, String parameter_name) {
  5333. isize param_count = pt->param_count;
  5334. for (isize i = 0; i < param_count; i++) {
  5335. Entity *e = pt->params->Tuple.variables[i];
  5336. String name = e->token.string;
  5337. if (is_blank_ident(name)) {
  5338. continue;
  5339. }
  5340. if (name == parameter_name) {
  5341. return i;
  5342. }
  5343. }
  5344. return -1;
  5345. }
  5346. isize lookup_procedure_result(TypeProc *pt, String result_name) {
  5347. isize result_count = pt->result_count;
  5348. for (isize i = 0; i < result_count; i++) {
  5349. Entity *e = pt->results->Tuple.variables[i];
  5350. String name = e->token.string;
  5351. if (is_blank_ident(name)) {
  5352. continue;
  5353. }
  5354. if (name == result_name) {
  5355. return i;
  5356. }
  5357. }
  5358. return -1;
  5359. }
  5360. CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
  5361. ast_node(ce, CallExpr, call);
  5362. GB_ASSERT(is_type_proc(proc_type));
  5363. proc_type = base_type(proc_type);
  5364. TypeProc *pt = &proc_type->Proc;
  5365. i64 score = 0;
  5366. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  5367. CallArgumentError err = CallArgumentError_None;
  5368. isize param_count = pt->param_count;
  5369. bool *visited = gb_alloc_array(c->allocator, bool, param_count);
  5370. defer (gb_free(c->allocator, visited));
  5371. auto ordered_operands = array_make<Operand>(c->allocator, param_count);
  5372. defer (array_free(&ordered_operands));
  5373. defer ({
  5374. for_array(i, ordered_operands) {
  5375. Operand const &o = ordered_operands[i];
  5376. if (o.expr != nullptr) {
  5377. call->viral_state_flags |= o.expr->viral_state_flags;
  5378. }
  5379. }
  5380. });
  5381. for_array(i, ce->args) {
  5382. Ast *arg = ce->args[i];
  5383. ast_node(fv, FieldValue, arg);
  5384. if (fv->field->kind != Ast_Ident) {
  5385. if (show_error) {
  5386. gbString expr_str = expr_to_string(fv->field);
  5387. error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
  5388. gb_string_free(expr_str);
  5389. }
  5390. err = CallArgumentError_InvalidFieldValue;
  5391. continue;
  5392. }
  5393. String name = fv->field->Ident.token.string;
  5394. isize index = lookup_procedure_parameter(pt, name);
  5395. if (index < 0) {
  5396. if (show_error) {
  5397. error(arg, "No parameter named '%.*s' for this procedure type", LIT(name));
  5398. }
  5399. err = CallArgumentError_ParameterNotFound;
  5400. continue;
  5401. }
  5402. if (visited[index]) {
  5403. if (show_error) {
  5404. error(arg, "Duplicate parameter '%.*s' in procedure call", LIT(name));
  5405. }
  5406. err = CallArgumentError_DuplicateParameter;
  5407. continue;
  5408. }
  5409. visited[index] = true;
  5410. ordered_operands[index] = operands[i];
  5411. }
  5412. // NOTE(bill): Check for default values and missing parameters
  5413. isize param_count_to_check = param_count;
  5414. if (pt->variadic) {
  5415. param_count_to_check--;
  5416. }
  5417. for (isize i = 0; i < param_count_to_check; i++) {
  5418. if (!visited[i]) {
  5419. Entity *e = pt->params->Tuple.variables[i];
  5420. if (is_blank_ident(e->token)) {
  5421. continue;
  5422. }
  5423. if (e->kind == Entity_Variable) {
  5424. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5425. score += assign_score_function(1);
  5426. continue;
  5427. }
  5428. }
  5429. if (show_error) {
  5430. if (e->kind == Entity_TypeName) {
  5431. error(call, "Type parameter '%.*s' is missing in procedure call",
  5432. LIT(e->token.string));
  5433. } else if (e->kind == Entity_Constant && e->Constant.value.kind != ExactValue_Invalid) {
  5434. // Ignore
  5435. } else {
  5436. gbString str = type_to_string(e->type);
  5437. error(call, "Parameter '%.*s' of type '%s' is missing in procedure call",
  5438. LIT(e->token.string), str);
  5439. gb_string_free(str);
  5440. }
  5441. }
  5442. err = CallArgumentError_ParameterMissing;
  5443. }
  5444. }
  5445. Entity *gen_entity = nullptr;
  5446. if (pt->is_polymorphic && !pt->is_poly_specialized && err == CallArgumentError_None) {
  5447. PolyProcData poly_proc_data = {};
  5448. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &ordered_operands, call, &poly_proc_data)) {
  5449. gen_entity = poly_proc_data.gen_entity;
  5450. Type *gept = base_type(gen_entity->type);
  5451. GB_ASSERT(is_type_proc(gept));
  5452. proc_type = gept;
  5453. pt = &gept->Proc;
  5454. }
  5455. }
  5456. for (isize i = 0; i < param_count; i++) {
  5457. Entity *e = pt->params->Tuple.variables[i];
  5458. Operand *o = &ordered_operands[i];
  5459. bool param_is_variadic = pt->variadic && pt->variadic_index == i;
  5460. if (o->mode == Addressing_Invalid) {
  5461. if (param_is_variadic) {
  5462. Type *slice = e->type;
  5463. GB_ASSERT(is_type_slice(slice));
  5464. Type *elem = base_type(slice)->Slice.elem;
  5465. if (is_type_polymorphic(elem)) {
  5466. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5467. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5468. return err;
  5469. }
  5470. }
  5471. continue;
  5472. }
  5473. if (e->kind == Entity_TypeName) {
  5474. GB_ASSERT(pt->is_polymorphic);
  5475. if (o->mode != Addressing_Type) {
  5476. if (show_error) {
  5477. error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  5478. }
  5479. err = CallArgumentError_WrongTypes;
  5480. }
  5481. if (are_types_identical(e->type, o->type)) {
  5482. score += assign_score_function(1);
  5483. } else {
  5484. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5485. }
  5486. } else {
  5487. i64 s = 0;
  5488. if (!check_is_assignable_to_with_score(c, o, e->type, &s, param_is_variadic)) {
  5489. bool ok = false;
  5490. if (e->flags & EntityFlag_AutoCast) {
  5491. ok = check_is_castable_to(c, o, e->type);
  5492. }
  5493. if (ok) {
  5494. s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5495. } else {
  5496. if (show_error) {
  5497. check_assignment(c, o, e->type, str_lit("procedure argument"));
  5498. }
  5499. err = CallArgumentError_WrongTypes;
  5500. }
  5501. if (e->flags & EntityFlag_ConstInput) {
  5502. if (o->mode != Addressing_Constant) {
  5503. if (show_error) {
  5504. error(o->expr, "Expected a constant value for the argument '%.*s'", LIT(e->token.string));
  5505. }
  5506. err = CallArgumentError_NoneConstantParameter;
  5507. }
  5508. }
  5509. }
  5510. score += s;
  5511. }
  5512. if (o->mode == Addressing_Type && is_type_typeid(e->type)) {
  5513. add_type_info_type(c, o->type);
  5514. add_type_and_value(c->info, o->expr, Addressing_Value, e->type, exact_value_typeid(o->type));
  5515. }
  5516. }
  5517. if (data) {
  5518. data->score = score;
  5519. data->result_type = pt->results;
  5520. data->gen_entity = gen_entity;
  5521. }
  5522. return err;
  5523. }
  5524. Entity **populate_proc_parameter_list(CheckerContext *c, Type *proc_type, isize *lhs_count_, bool *is_variadic) {
  5525. Entity **lhs = nullptr;
  5526. isize lhs_count = -1;
  5527. if (proc_type == nullptr) {
  5528. return nullptr;
  5529. }
  5530. GB_ASSERT(is_type_proc(proc_type));
  5531. TypeProc *pt = &base_type(proc_type)->Proc;
  5532. *is_variadic = pt->variadic;
  5533. if (!pt->is_polymorphic || pt->is_poly_specialized) {
  5534. if (pt->params != nullptr) {
  5535. lhs = pt->params->Tuple.variables.data;
  5536. lhs_count = pt->params->Tuple.variables.count;
  5537. }
  5538. } else {
  5539. // NOTE(bill): Create 'lhs' list in order to ignore parameters which are polymorphic
  5540. if (pt->params == nullptr) {
  5541. lhs_count = 0;
  5542. } else {
  5543. lhs_count = pt->params->Tuple.variables.count;
  5544. }
  5545. lhs = gb_alloc_array(heap_allocator(), Entity *, lhs_count);
  5546. for (isize i = 0; i < lhs_count; i++) {
  5547. Entity *e = pt->params->Tuple.variables[i];
  5548. if (!is_type_polymorphic(e->type)) {
  5549. lhs[i] = e;
  5550. }
  5551. }
  5552. }
  5553. if (lhs_count_) *lhs_count_ = lhs_count;
  5554. return lhs;
  5555. }
  5556. bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, Array<Ast *> *clauses, bool print_err) {
  5557. if (clauses != nullptr) {
  5558. for_array(i, *clauses) {
  5559. Ast *clause = (*clauses)[i];
  5560. Operand o = {};
  5561. check_expr(ctx, &o, clause);
  5562. if (o.mode != Addressing_Constant) {
  5563. if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
  5564. if (print_err && call_expr) error(call_expr, "at caller location");
  5565. return false;
  5566. } else if (o.value.kind != ExactValue_Bool) {
  5567. if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
  5568. if (print_err && call_expr) error(call_expr, "at caller location");
  5569. return false;
  5570. } else if (!o.value.value_bool) {
  5571. if (print_err) {
  5572. gbString str = expr_to_string(clause);
  5573. error(clause, "'where' clause evaluated to false:\n\t%s", str);
  5574. gb_string_free(str);
  5575. if (scope != nullptr) {
  5576. isize print_count = 0;
  5577. for_array(j, scope->elements.entries) {
  5578. Entity *e = scope->elements.entries[j].value;
  5579. switch (e->kind) {
  5580. case Entity_TypeName: {
  5581. if (print_count == 0) error_line("\n\tWith the following definitions:\n");
  5582. gbString str = type_to_string(e->type);
  5583. error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
  5584. gb_string_free(str);
  5585. print_count += 1;
  5586. break;
  5587. }
  5588. case Entity_Constant: {
  5589. if (print_count == 0) error_line("\n\tWith the following definitions:\n");
  5590. gbString str = exact_value_to_string(e->Constant.value);
  5591. if (is_type_untyped(e->type)) {
  5592. error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
  5593. } else {
  5594. gbString t = type_to_string(e->type);
  5595. error_line("\t\t%.*s : %s : %s;\n", LIT(e->token.string), t, str);
  5596. gb_string_free(t);
  5597. }
  5598. gb_string_free(str);
  5599. print_count += 1;
  5600. break;
  5601. }
  5602. }
  5603. }
  5604. }
  5605. if (call_expr) error(call_expr, "at caller location");
  5606. }
  5607. return false;
  5608. }
  5609. }
  5610. }
  5611. return true;
  5612. }
  5613. CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Type *proc_type, Ast *call) {
  5614. ast_node(ce, CallExpr, call);
  5615. CallArgumentCheckerType *call_checker = check_call_arguments_internal;
  5616. Array<Operand> operands = {};
  5617. defer (array_free(&operands));
  5618. Type *result_type = t_invalid;
  5619. if (is_call_expr_field_value(ce)) {
  5620. call_checker = check_named_call_arguments;
  5621. operands = array_make<Operand>(heap_allocator(), ce->args.count);
  5622. // NOTE(bill): This is give type hints for the named parameters
  5623. // in order to improve the type inference system
  5624. Map<Type *> type_hint_map = {}; // Key: String
  5625. map_init(&type_hint_map, heap_allocator(), 2*ce->args.count);
  5626. defer (map_destroy(&type_hint_map));
  5627. Type *ptype = nullptr;
  5628. bool single_case = true;
  5629. if (operand->mode == Addressing_ProcGroup) {
  5630. single_case = false;
  5631. Array<Entity *> procs = proc_group_entities(c, *operand);
  5632. if (procs.count == 1) {
  5633. ptype = procs[0]->type;
  5634. single_case = true;
  5635. }
  5636. } else {
  5637. ptype = proc_type;
  5638. }
  5639. if (single_case) {
  5640. Type *bptype = base_type(ptype);
  5641. if (is_type_proc(bptype)) {
  5642. TypeProc *pt = &bptype->Proc;
  5643. TypeTuple *param_tuple = nullptr;
  5644. if (pt->params != nullptr) {
  5645. param_tuple = &pt->params->Tuple;
  5646. }
  5647. if (param_tuple != nullptr) {
  5648. for_array(i, param_tuple->variables) {
  5649. Entity *e = param_tuple->variables[i];
  5650. if (is_blank_ident(e->token)) {
  5651. continue;
  5652. }
  5653. map_set(&type_hint_map, hash_string(e->token.string), e->type);
  5654. }
  5655. }
  5656. }
  5657. } else {
  5658. Array<Entity *> procs = proc_group_entities(c, *operand);
  5659. for_array(j, procs) {
  5660. Type *proc_type = base_type(procs[j]->type);
  5661. if (is_type_proc(proc_type)) {
  5662. TypeProc *pt = &proc_type->Proc;
  5663. TypeTuple *param_tuple = nullptr;
  5664. if (pt->params != nullptr) {
  5665. param_tuple = &pt->params->Tuple;
  5666. }
  5667. if (param_tuple == nullptr) {
  5668. continue;
  5669. }
  5670. for_array(i, param_tuple->variables) {
  5671. Entity *e = param_tuple->variables[i];
  5672. if (is_blank_ident(e->token)) {
  5673. continue;
  5674. }
  5675. HashKey key = hash_string(e->token.string);
  5676. Type **found = map_get(&type_hint_map, key);
  5677. if (found) {
  5678. Type *t = *found;
  5679. if (t == nullptr) {
  5680. // NOTE(bill): Ambiguous named parameter across all types
  5681. continue;
  5682. }
  5683. if (are_types_identical(t, e->type)) {
  5684. // NOTE(bill): No need to set again
  5685. } else {
  5686. // NOTE(bill): Ambiguous named parameter across all types so set it to a nullptr
  5687. map_set(&type_hint_map, key, cast(Type *)nullptr);
  5688. }
  5689. } else {
  5690. map_set(&type_hint_map, key, e->type);
  5691. }
  5692. }
  5693. }
  5694. }
  5695. }
  5696. for_array(i, ce->args) {
  5697. Ast *arg = ce->args[i];
  5698. ast_node(fv, FieldValue, arg);
  5699. Ast *field = fv->field;
  5700. Type *type_hint = nullptr;
  5701. if (field != nullptr && field->kind == Ast_Ident) {
  5702. String key = field->Ident.token.string;
  5703. Type **found = map_get(&type_hint_map, hash_string(key));
  5704. if (found) {
  5705. type_hint = *found;
  5706. }
  5707. }
  5708. check_expr_or_type(c, &operands[i], fv->value, type_hint);
  5709. }
  5710. } else {
  5711. operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
  5712. Entity **lhs = nullptr;
  5713. isize lhs_count = -1;
  5714. bool is_variadic = false;
  5715. if (proc_type != nullptr && is_type_proc(proc_type)) {
  5716. lhs = populate_proc_parameter_list(c, proc_type, &lhs_count, &is_variadic);
  5717. }
  5718. if (operand->mode != Addressing_ProcGroup) {
  5719. check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, false, is_variadic);
  5720. }
  5721. }
  5722. if (operand->mode == Addressing_ProcGroup) {
  5723. check_entity_decl(c, operand->proc_group, nullptr, nullptr);
  5724. Array<Entity *> procs = proc_group_entities(c, *operand);
  5725. if (procs.count == 1) {
  5726. Ast *ident = operand->expr;
  5727. while (ident->kind == Ast_SelectorExpr) {
  5728. Ast *s = ident->SelectorExpr.selector;
  5729. ident = s;
  5730. }
  5731. Entity *e = procs[0];
  5732. Entity **lhs = nullptr;
  5733. isize lhs_count = -1;
  5734. bool is_variadic = false;
  5735. lhs = populate_proc_parameter_list(c, e->type, &lhs_count, &is_variadic);
  5736. check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, false, is_variadic);
  5737. CallArgumentData data = {};
  5738. CallArgumentError err = call_checker(c, call, e->type, e, operands, CallArgumentMode_ShowErrors, &data);
  5739. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  5740. add_entity_use(c, ident, entity_to_use);
  5741. return data;
  5742. }
  5743. Entity **lhs = nullptr;
  5744. isize lhs_count = -1;
  5745. {
  5746. // NOTE(bill, 2019-07-13): This code is used to improve the type inference for procedure groups
  5747. // where the same positional parameter has the same type value (and ellipsis)
  5748. bool proc_arg_count_all_equal = true;
  5749. isize proc_arg_count = -1;
  5750. for_array(i, procs) {
  5751. Entity *p = procs[i];
  5752. Type *pt = base_type(p->type);
  5753. if (pt != nullptr && is_type_proc(pt)) {
  5754. if (proc_arg_count < 0) {
  5755. proc_arg_count = pt->Proc.param_count;
  5756. } else {
  5757. if (proc_arg_count != pt->Proc.param_count) {
  5758. proc_arg_count_all_equal = false;
  5759. break;
  5760. }
  5761. }
  5762. }
  5763. }
  5764. if (proc_arg_count >= 0 && proc_arg_count_all_equal) {
  5765. lhs_count = proc_arg_count;
  5766. if (lhs_count > 0) {
  5767. lhs = gb_alloc_array(heap_allocator(), Entity *, lhs_count);
  5768. for (isize param_index = 0; param_index < lhs_count; param_index++) {
  5769. Entity *e = nullptr;
  5770. for_array(j, procs) {
  5771. Entity *p = procs[j];
  5772. Type *pt = base_type(p->type);
  5773. if (pt != nullptr && is_type_proc(pt)) {
  5774. if (e == nullptr) {
  5775. e = pt->Proc.params->Tuple.variables[param_index];
  5776. } else {
  5777. Entity *f = pt->Proc.params->Tuple.variables[param_index];
  5778. if (e == f) {
  5779. continue;
  5780. }
  5781. if (are_types_identical(e->type, f->type)) {
  5782. bool ee = (e->flags & EntityFlag_Ellipsis) != 0;
  5783. bool fe = (f->flags & EntityFlag_Ellipsis) != 0;
  5784. if (ee == fe) {
  5785. continue;
  5786. }
  5787. }
  5788. // NOTE(bill): Entities are not close enough to be used
  5789. e = nullptr;
  5790. break;
  5791. }
  5792. }
  5793. }
  5794. lhs[param_index] = e;
  5795. }
  5796. }
  5797. }
  5798. }
  5799. check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, false, false);
  5800. if (lhs != nullptr) {
  5801. gb_free(heap_allocator(), lhs);
  5802. }
  5803. auto valids = array_make<ValidIndexAndScore>(heap_allocator(), 0, procs.count);
  5804. defer (array_free(&valids));
  5805. gbString expr_name = expr_to_string(operand->expr);
  5806. defer (gb_string_free(expr_name));
  5807. for_array(i, procs) {
  5808. Entity *p = procs[i];
  5809. Type *pt = base_type(p->type);
  5810. if (pt != nullptr && is_type_proc(pt)) {
  5811. CallArgumentError err = CallArgumentError_None;
  5812. CallArgumentData data = {};
  5813. CheckerContext ctx = *c;
  5814. ctx.no_polymorphic_errors = true;
  5815. ctx.allow_polymorphic_types = is_type_polymorphic(pt);
  5816. ctx.hide_polymorphic_errors = true;
  5817. err = call_checker(&ctx, call, pt, p, operands, CallArgumentMode_NoErrors, &data);
  5818. if (err != CallArgumentError_None) {
  5819. continue;
  5820. }
  5821. if (data.gen_entity != nullptr) {
  5822. Entity *e = data.gen_entity;
  5823. DeclInfo *decl = data.gen_entity->decl_info;
  5824. ctx.scope = decl->scope;
  5825. ctx.decl = decl;
  5826. ctx.proc_name = e->token.string;
  5827. ctx.curr_proc_decl = decl;
  5828. ctx.curr_proc_sig = e->type;
  5829. GB_ASSERT(decl->proc_lit->kind == Ast_ProcLit);
  5830. if (!evaluate_where_clauses(&ctx, operand->expr, decl->scope, &decl->proc_lit->ProcLit.where_clauses, false)) {
  5831. continue;
  5832. }
  5833. }
  5834. ValidIndexAndScore item = {};
  5835. item.index = i;
  5836. item.score = data.score;
  5837. array_add(&valids, item);
  5838. }
  5839. }
  5840. if (valids.count > 1) {
  5841. gb_sort_array(valids.data, valids.count, valid_index_and_score_cmp);
  5842. i64 best_score = valids[0].score;
  5843. Entity *best_entity = procs[valids[0].index];
  5844. for (isize i = 1; i < valids.count; i++) {
  5845. if (best_score > valids[i].score) {
  5846. valids.count = i;
  5847. break;
  5848. }
  5849. if (best_entity == procs[valids[i].index]) {
  5850. valids.count = i;
  5851. break;
  5852. }
  5853. }
  5854. }
  5855. if (valids.count == 0) {
  5856. begin_error_block();
  5857. defer (end_error_block());
  5858. error(operand->expr, "No procedures or ambiguous call for procedure group '%s' that match with the given arguments", expr_name);
  5859. if (operands.count == 0) {
  5860. error_line("\tNo given arguments\n");
  5861. } else {
  5862. error_line("\tGiven argument types: (");
  5863. for_array(i, operands) {
  5864. Operand o = operands[i];
  5865. if (i > 0) error_line(", ");
  5866. gbString type = type_to_string(o.type);
  5867. defer (gb_string_free(type));
  5868. error_line("%s", type);
  5869. }
  5870. error_line(")\n");
  5871. }
  5872. if (procs.count > 0) {
  5873. error_line("Did you mean to use one of the following:\n");
  5874. }
  5875. for_array(i, procs) {
  5876. Entity *proc = procs[i];
  5877. TokenPos pos = proc->token.pos;
  5878. Type *t = base_type(proc->type);
  5879. if (t == t_invalid) continue;
  5880. GB_ASSERT(t->kind == Type_Proc);
  5881. gbString pt;
  5882. defer (gb_string_free(pt));
  5883. if (t->Proc.node != nullptr) {
  5884. pt = expr_to_string(t->Proc.node);
  5885. } else {
  5886. pt = type_to_string(t);
  5887. }
  5888. String prefix = {};
  5889. String prefix_sep = {};
  5890. if (proc->pkg) {
  5891. prefix = proc->pkg->name;
  5892. prefix_sep = str_lit(".");
  5893. }
  5894. String name = proc->token.string;
  5895. char const *sep = "::";
  5896. if (proc->kind == Entity_Variable) {
  5897. sep = ":=";
  5898. }
  5899. error_line("\t%.*s%.*s%.*s %s %s at %.*s(%td:%td)\n", LIT(prefix), LIT(prefix_sep), LIT(name), sep, pt, LIT(pos.file), pos.line, pos.column);
  5900. }
  5901. if (procs.count > 0) {
  5902. error_line("\n");
  5903. }
  5904. result_type = t_invalid;
  5905. } else if (valids.count > 1) {
  5906. begin_error_block();
  5907. defer (end_error_block());
  5908. error(operand->expr, "Ambiguous procedure group call '%s' that match with the given arguments", expr_name);
  5909. error_line("\tGiven argument types: (");
  5910. for_array(i, operands) {
  5911. Operand o = operands[i];
  5912. if (i > 0) error_line(", ");
  5913. gbString type = type_to_string(o.type);
  5914. defer (gb_string_free(type));
  5915. error_line("%s", type);
  5916. }
  5917. error_line(")\n");
  5918. for (isize i = 0; i < valids.count; i++) {
  5919. Entity *proc = procs[valids[i].index];
  5920. TokenPos pos = proc->token.pos;
  5921. Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
  5922. gbString pt = nullptr;
  5923. defer (gb_string_free(pt));
  5924. if (t->Proc.node != nullptr) {
  5925. pt = expr_to_string(t->Proc.node);
  5926. } else {
  5927. pt = type_to_string(t);
  5928. }
  5929. String name = proc->token.string;
  5930. char const *sep = "::";
  5931. if (proc->kind == Entity_Variable) {
  5932. sep = ":=";
  5933. }
  5934. error_line("\t%.*s %s %s ", LIT(name), sep, pt);
  5935. if (proc->decl_info->proc_lit != nullptr) {
  5936. GB_ASSERT(proc->decl_info->proc_lit->kind == Ast_ProcLit);
  5937. auto *pl = &proc->decl_info->proc_lit->ProcLit;
  5938. if (pl->where_token.kind != Token_Invalid) {
  5939. error_line("\n\t\twhere ");
  5940. for_array(j, pl->where_clauses) {
  5941. Ast *clause = pl->where_clauses[j];
  5942. if (j != 0) {
  5943. error_line("\t\t ");
  5944. }
  5945. gbString str = expr_to_string(clause);
  5946. error_line("%s", str);
  5947. gb_string_free(str);
  5948. if (j != pl->where_clauses.count-1) {
  5949. error_line(",");
  5950. }
  5951. }
  5952. error_line("\n\t");
  5953. }
  5954. }
  5955. error_line("at %.*s(%td:%td)\n", LIT(pos.file), pos.line, pos.column);
  5956. // error_line("\t%.*s %s %s at %.*s(%td:%td) %lld\n", LIT(name), sep, pt, LIT(pos.file), pos.line, pos.column, valids[i].score);
  5957. }
  5958. result_type = t_invalid;
  5959. } else {
  5960. Ast *ident = operand->expr;
  5961. while (ident->kind == Ast_SelectorExpr) {
  5962. Ast *s = ident->SelectorExpr.selector;
  5963. ident = s;
  5964. }
  5965. Entity *e = procs[valids[0].index];
  5966. proc_type = e->type;
  5967. CallArgumentData data = {};
  5968. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  5969. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  5970. add_entity_use(c, ident, entity_to_use);
  5971. return data;
  5972. }
  5973. } else {
  5974. Ast *ident = operand->expr;
  5975. while (ident->kind == Ast_SelectorExpr) {
  5976. Ast *s = ident->SelectorExpr.selector;
  5977. ident = s;
  5978. }
  5979. Entity *e = entity_of_ident(ident);
  5980. CallArgumentData data = {};
  5981. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  5982. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  5983. add_entity_use(c, ident, entity_to_use);
  5984. return data;
  5985. }
  5986. CallArgumentData data = {};
  5987. data.result_type = t_invalid;
  5988. return data;
  5989. }
  5990. isize lookup_polymorphic_record_parameter(Type *t, String parameter_name) {
  5991. if (!is_type_polymorphic_record(t)) {
  5992. return -1;
  5993. }
  5994. TypeTuple *params = get_record_polymorphic_params(t);
  5995. if (params == nullptr) {
  5996. return -1;
  5997. }
  5998. for_array(i, params->variables) {
  5999. Entity *e = params->variables[i];
  6000. String name = e->token.string;
  6001. if (is_blank_ident(name)) {
  6002. continue;
  6003. }
  6004. if (name == parameter_name) {
  6005. return i;
  6006. }
  6007. }
  6008. return -1;
  6009. }
  6010. CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *operand, Ast *call) {
  6011. ast_node(ce, CallExpr, call);
  6012. Type *original_type = operand->type;
  6013. GB_ASSERT(is_type_polymorphic_record(original_type));
  6014. bool show_error = true;
  6015. Array<Operand> operands = {};
  6016. defer (array_free(&operands));
  6017. bool named_fields = false;
  6018. {
  6019. // NOTE(bill, 2019-10-26): Allow a cycle in the parameters but not in the fields themselves
  6020. auto prev_type_path = c->type_path;
  6021. c->type_path = new_checker_type_path();
  6022. defer ({
  6023. destroy_checker_type_path(c->type_path);
  6024. c->type_path = prev_type_path;
  6025. });
  6026. if (is_call_expr_field_value(ce)) {
  6027. named_fields = true;
  6028. operands = array_make<Operand>(heap_allocator(), ce->args.count);
  6029. for_array(i, ce->args) {
  6030. Ast *arg = ce->args[i];
  6031. ast_node(fv, FieldValue, arg);
  6032. if (fv->field->kind == Ast_Ident) {
  6033. String name = fv->field->Ident.token.string;
  6034. isize index = lookup_polymorphic_record_parameter(original_type, name);
  6035. if (index >= 0) {
  6036. TypeTuple *params = get_record_polymorphic_params(original_type);
  6037. Entity *e = params->variables[i];
  6038. if (e->kind == Entity_Constant) {
  6039. check_expr_with_type_hint(c, &operands[i], fv->value, e->type);
  6040. }
  6041. }
  6042. }
  6043. check_expr_or_type(c, &operands[i], fv->value);
  6044. }
  6045. bool vari_expand = (ce->ellipsis.pos.line != 0);
  6046. if (vari_expand) {
  6047. error(ce->ellipsis, "Invalid use of '..' in a polymorphic type call'");
  6048. }
  6049. } else {
  6050. operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
  6051. Entity **lhs = nullptr;
  6052. isize lhs_count = -1;
  6053. TypeTuple *params = get_record_polymorphic_params(original_type);
  6054. if (params != nullptr) {
  6055. lhs = params->variables.data;
  6056. lhs_count = params->variables.count;
  6057. }
  6058. check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, false, false);
  6059. }
  6060. }
  6061. CallArgumentError err = CallArgumentError_None;
  6062. TypeTuple *tuple = get_record_polymorphic_params(original_type);
  6063. isize param_count = tuple->variables.count;
  6064. Array<Operand> ordered_operands = operands;
  6065. if (named_fields) {
  6066. bool *visited = gb_alloc_array(c->allocator, bool, param_count);
  6067. // LEAK(bill)
  6068. ordered_operands = array_make<Operand>(c->allocator, param_count);
  6069. for_array(i, ce->args) {
  6070. Ast *arg = ce->args[i];
  6071. ast_node(fv, FieldValue, arg);
  6072. if (fv->field->kind != Ast_Ident) {
  6073. if (show_error) {
  6074. gbString expr_str = expr_to_string(fv->field);
  6075. error(arg, "Invalid parameter name '%s' in polymorphic type call", expr_str);
  6076. gb_string_free(expr_str);
  6077. }
  6078. err = CallArgumentError_InvalidFieldValue;
  6079. continue;
  6080. }
  6081. String name = fv->field->Ident.token.string;
  6082. isize index = lookup_polymorphic_record_parameter(original_type, name);
  6083. if (index < 0) {
  6084. if (show_error) {
  6085. error(arg, "No parameter named '%.*s' for this polymorphic type", LIT(name));
  6086. }
  6087. err = CallArgumentError_ParameterNotFound;
  6088. continue;
  6089. }
  6090. if (visited[index]) {
  6091. if (show_error) {
  6092. error(arg, "Duplicate parameter '%.*s' in polymorphic type", LIT(name));
  6093. }
  6094. err = CallArgumentError_DuplicateParameter;
  6095. continue;
  6096. }
  6097. visited[index] = true;
  6098. ordered_operands[index] = operands[i];
  6099. }
  6100. for (isize i = 0; i < param_count; i++) {
  6101. if (!visited[i]) {
  6102. Entity *e = tuple->variables[i];
  6103. if (is_blank_ident(e->token)) {
  6104. continue;
  6105. }
  6106. if (show_error) {
  6107. if (e->kind == Entity_TypeName) {
  6108. error(call, "Type parameter '%.*s' is missing in polymorphic type call",
  6109. LIT(e->token.string));
  6110. } else {
  6111. gbString str = type_to_string(e->type);
  6112. error(call, "Parameter '%.*s' of type '%s' is missing in polymorphic type call",
  6113. LIT(e->token.string), str);
  6114. gb_string_free(str);
  6115. }
  6116. }
  6117. err = CallArgumentError_ParameterMissing;
  6118. }
  6119. }
  6120. }
  6121. if (err != 0) {
  6122. operand->mode = Addressing_Invalid;
  6123. return err;
  6124. }
  6125. if (param_count < ordered_operands.count) {
  6126. error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  6127. err = CallArgumentError_TooManyArguments;
  6128. } else if (param_count > ordered_operands.count) {
  6129. error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  6130. err = CallArgumentError_TooFewArguments;
  6131. }
  6132. if (err != 0) {
  6133. return err;
  6134. }
  6135. i64 score = 0;
  6136. for (isize i = 0; i < param_count; i++) {
  6137. Operand *o = &ordered_operands[i];
  6138. if (o->mode == Addressing_Invalid) {
  6139. continue;
  6140. }
  6141. Entity *e = tuple->variables[i];
  6142. if (e->kind == Entity_TypeName) {
  6143. if (o->mode != Addressing_Type) {
  6144. if (show_error) {
  6145. error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  6146. }
  6147. err = CallArgumentError_WrongTypes;
  6148. }
  6149. if (are_types_identical(e->type, o->type)) {
  6150. score += assign_score_function(1);
  6151. } else {
  6152. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  6153. }
  6154. } else {
  6155. i64 s = 0;
  6156. if (o->type->kind == Type_Generic) {
  6157. // Polymorphic name!
  6158. score += assign_score_function(1);
  6159. continue;
  6160. } else if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
  6161. if (show_error) {
  6162. check_assignment(c, o, e->type, str_lit("polymorphic type argument"));
  6163. }
  6164. err = CallArgumentError_WrongTypes;
  6165. }
  6166. o->type = e->type;
  6167. if (o->mode != Addressing_Constant) {
  6168. if (show_error) {
  6169. error(o->expr, "Expected a constant value for this polymorphic type argument");
  6170. }
  6171. err = CallArgumentError_NoneConstantParameter;
  6172. }
  6173. score += s;
  6174. }
  6175. // NOTE(bill): Add type info the parameters
  6176. add_type_info_type(c, o->type);
  6177. }
  6178. {
  6179. gbAllocator a = c->allocator;
  6180. bool failure = false;
  6181. Entity *found_entity = find_polymorphic_record_entity(c, original_type, param_count, ordered_operands, &failure);
  6182. if (found_entity) {
  6183. operand->mode = Addressing_Type;
  6184. operand->type = found_entity->type;
  6185. return err;
  6186. }
  6187. String generated_name = make_string_c(expr_to_string(call));
  6188. CheckerContext ctx = *c;
  6189. // NOTE(bill): We need to make sure the lookup scope for the record is the same as where it was created
  6190. ctx.scope = polymorphic_record_parent_scope(original_type);
  6191. GB_ASSERT(ctx.scope != nullptr);
  6192. Type *named_type = alloc_type_named(generated_name, nullptr, nullptr);
  6193. Type *bt = base_type(original_type);
  6194. if (bt->kind == Type_Struct) {
  6195. Ast *node = clone_ast(bt->Struct.node);
  6196. Type *struct_type = alloc_type_struct();
  6197. struct_type->Struct.node = node;
  6198. struct_type->Struct.polymorphic_parent = original_type;
  6199. set_base_type(named_type, struct_type);
  6200. check_open_scope(&ctx, node);
  6201. check_struct_type(&ctx, struct_type, node, &ordered_operands, named_type, original_type);
  6202. check_close_scope(&ctx);
  6203. } else if (bt->kind == Type_Union) {
  6204. Ast *node = clone_ast(bt->Union.node);
  6205. Type *union_type = alloc_type_union();
  6206. union_type->Union.node = node;
  6207. union_type->Union.polymorphic_parent = original_type;
  6208. set_base_type(named_type, union_type);
  6209. check_open_scope(&ctx, node);
  6210. check_union_type(&ctx, union_type, node, &ordered_operands, named_type, original_type);
  6211. check_close_scope(&ctx);
  6212. } else {
  6213. GB_PANIC("Unsupported parametric polymorphic record type");
  6214. }
  6215. operand->mode = Addressing_Type;
  6216. operand->type = named_type;
  6217. }
  6218. return err;
  6219. }
  6220. ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) {
  6221. ast_node(ce, CallExpr, call);
  6222. if (ce->proc != nullptr &&
  6223. ce->proc->kind == Ast_BasicDirective) {
  6224. ast_node(bd, BasicDirective, ce->proc);
  6225. String name = bd->name;
  6226. if (name == "location" || name == "assert" || name == "panic" || name == "defined" || name == "load") {
  6227. operand->mode = Addressing_Builtin;
  6228. operand->builtin_id = BuiltinProc_DIRECTIVE;
  6229. operand->expr = ce->proc;
  6230. operand->type = t_invalid;
  6231. add_type_and_value(&c->checker->info, ce->proc, operand->mode, operand->type, operand->value);
  6232. } else {
  6233. GB_PANIC("Unhandled #%.*s", LIT(name));
  6234. }
  6235. if (ce->inlining != ProcInlining_none) {
  6236. error(call, "Inlining operators are not allowed on built-in procedures");
  6237. }
  6238. } else {
  6239. check_expr_or_type(c, operand, ce->proc);
  6240. }
  6241. if (ce->args.count > 0) {
  6242. bool fail = false;
  6243. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  6244. for_array(i, ce->args) {
  6245. Ast *arg = ce->args[i];
  6246. bool mix = false;
  6247. if (first_is_field_value) {
  6248. mix = arg->kind != Ast_FieldValue;
  6249. } else {
  6250. mix = arg->kind == Ast_FieldValue;
  6251. }
  6252. if (mix) {
  6253. error(arg, "Mixture of 'field = value' and value elements in a procedure all is not allowed");
  6254. fail = true;
  6255. }
  6256. }
  6257. if (fail) {
  6258. operand->mode = Addressing_Invalid;
  6259. operand->expr = call;
  6260. return Expr_Stmt;
  6261. }
  6262. }
  6263. if (operand->mode == Addressing_Invalid) {
  6264. for_array(i, ce->args) {
  6265. Ast *arg = ce->args[i];
  6266. if (arg->kind == Ast_FieldValue) {
  6267. arg = arg->FieldValue.value;
  6268. }
  6269. check_expr_base(c, operand, arg, nullptr);
  6270. }
  6271. operand->mode = Addressing_Invalid;
  6272. operand->expr = call;
  6273. return Expr_Stmt;
  6274. }
  6275. if (operand->mode == Addressing_Type) {
  6276. Type *t = operand->type;
  6277. if (is_type_polymorphic_record(t)) {
  6278. auto err = check_polymorphic_record_type(c, operand, call);
  6279. if (err == 0) {
  6280. Ast *ident = operand->expr;
  6281. while (ident->kind == Ast_SelectorExpr) {
  6282. Ast *s = ident->SelectorExpr.selector;
  6283. ident = s;
  6284. }
  6285. Type *ot = operand->type;
  6286. GB_ASSERT(ot->kind == Type_Named);
  6287. Entity *e = ot->Named.type_name;
  6288. add_entity_use(c, ident, e);
  6289. add_type_and_value(&c->checker->info, call, Addressing_Type, ot, empty_exact_value);
  6290. } else {
  6291. operand->mode = Addressing_Invalid;
  6292. operand->type = t_invalid;
  6293. }
  6294. } else {
  6295. gbString str = type_to_string(t);
  6296. defer (gb_string_free(str));
  6297. operand->mode = Addressing_Invalid;
  6298. isize arg_count = ce->args.count;
  6299. switch (arg_count) {
  6300. case 0: error(call, "Missing argument in conversion to '%s'", str); break;
  6301. default: error(call, "Too many arguments in conversion to '%s'", str); break;
  6302. case 1: {
  6303. Ast *arg = ce->args[0];
  6304. if (arg->kind == Ast_FieldValue) {
  6305. error(call, "'field = value' cannot be used in a type conversion");
  6306. arg = arg->FieldValue.value;
  6307. // NOTE(bill): Carry on the cast regardless
  6308. }
  6309. check_expr(c, operand, arg);
  6310. if (operand->mode != Addressing_Invalid) {
  6311. check_cast(c, operand, t);
  6312. }
  6313. break;
  6314. }
  6315. }
  6316. }
  6317. return Expr_Expr;
  6318. }
  6319. if (operand->mode == Addressing_Builtin) {
  6320. i32 id = operand->builtin_id;
  6321. if (!check_builtin_procedure(c, operand, call, id, type_hint)) {
  6322. operand->mode = Addressing_Invalid;
  6323. operand->type = t_invalid;
  6324. }
  6325. operand->expr = call;
  6326. return builtin_procs[id].kind;
  6327. }
  6328. Entity *e = entity_of_ident(operand->expr);
  6329. if (e != nullptr && e->kind == Entity_Procedure) {
  6330. if (e->Procedure.deferred_procedure.entity != nullptr) {
  6331. call->viral_state_flags |= ViralStateFlag_ContainsDeferredProcedure;
  6332. }
  6333. }
  6334. Type *proc_type = base_type(operand->type);
  6335. if (operand->mode != Addressing_ProcGroup) {
  6336. bool valid_type = (proc_type != nullptr) && is_type_proc(proc_type);
  6337. bool valid_mode = is_operand_value(*operand);
  6338. if (!valid_type || !valid_mode) {
  6339. Ast *e = operand->expr;
  6340. gbString str = expr_to_string(e);
  6341. gbString type_str = type_to_string(operand->type);
  6342. error(e, "Cannot call a non-procedure: '%s' of type '%s'", str, type_str);
  6343. gb_string_free(type_str);
  6344. gb_string_free(str);
  6345. operand->mode = Addressing_Invalid;
  6346. operand->expr = call;
  6347. return Expr_Stmt;
  6348. }
  6349. }
  6350. CallArgumentData data = check_call_arguments(c, operand, proc_type, call);
  6351. Type *result_type = data.result_type;
  6352. gb_zero_item(operand);
  6353. operand->expr = call;
  6354. if (result_type == t_invalid) {
  6355. operand->mode = Addressing_Invalid;
  6356. operand->type = t_invalid;
  6357. return Expr_Stmt;
  6358. }
  6359. Type *pt = base_type(proc_type);
  6360. if (result_type == nullptr) {
  6361. operand->mode = Addressing_NoValue;
  6362. } else {
  6363. GB_ASSERT(is_type_tuple(result_type));
  6364. isize count = result_type->Tuple.variables.count;
  6365. switch (count) {
  6366. case 0:
  6367. operand->mode = Addressing_NoValue;
  6368. break;
  6369. case 1:
  6370. operand->mode = Addressing_Value;
  6371. operand->type = result_type->Tuple.variables[0]->type;
  6372. break;
  6373. default:
  6374. operand->mode = Addressing_Value;
  6375. operand->type = result_type;
  6376. break;
  6377. }
  6378. }
  6379. switch (ce->inlining) {
  6380. case ProcInlining_inline: {
  6381. Entity *e = entity_from_expr(ce->proc);
  6382. if (e != nullptr && e->kind == Entity_Procedure) {
  6383. DeclInfo *decl = e->decl_info;
  6384. if (decl->proc_lit) {
  6385. ast_node(pl, ProcLit, decl->proc_lit);
  6386. if (pl->inlining == ProcInlining_no_inline) {
  6387. error(call, "'inline' cannot be applied to a procedure that has be marked as 'no_inline'");
  6388. }
  6389. }
  6390. }
  6391. break;
  6392. }
  6393. case ProcInlining_no_inline:
  6394. break;
  6395. }
  6396. operand->expr = call;
  6397. return Expr_Expr;
  6398. }
  6399. void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) {
  6400. check_expr_base(c, o, e, t);
  6401. check_not_tuple(c, o);
  6402. char const *err_str = nullptr;
  6403. switch (o->mode) {
  6404. case Addressing_NoValue:
  6405. err_str = "used as a value";
  6406. break;
  6407. case Addressing_Type:
  6408. err_str = "is not an expression";
  6409. break;
  6410. case Addressing_Builtin:
  6411. err_str = "must be called";
  6412. break;
  6413. }
  6414. if (err_str != nullptr) {
  6415. gbString str = expr_to_string(e);
  6416. error(e, "'%s' %s", str, err_str);
  6417. gb_string_free(str);
  6418. o->mode = Addressing_Invalid;
  6419. }
  6420. }
  6421. bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, Type *original_type) {
  6422. switch (t->kind) {
  6423. case Type_Basic:
  6424. if (t->Basic.kind == Basic_string) {
  6425. if (o->mode == Addressing_Constant) {
  6426. *max_count = o->value.value_string.len;
  6427. }
  6428. if (o->mode != Addressing_Constant) {
  6429. o->mode = Addressing_Value;
  6430. }
  6431. o->type = t_u8;
  6432. return true;
  6433. } else if (t->Basic.kind == Basic_UntypedString) {
  6434. if (o->mode == Addressing_Constant) {
  6435. *max_count = o->value.value_string.len;
  6436. o->type = t_u8;
  6437. return true;
  6438. }
  6439. return false;
  6440. }
  6441. break;
  6442. case Type_Array:
  6443. *max_count = t->Array.count;
  6444. if (indirection) {
  6445. o->mode = Addressing_Variable;
  6446. } else if (o->mode != Addressing_Variable &&
  6447. o->mode != Addressing_Constant) {
  6448. o->mode = Addressing_Value;
  6449. }
  6450. o->type = t->Array.elem;
  6451. return true;
  6452. case Type_EnumeratedArray:
  6453. *max_count = t->EnumeratedArray.count;
  6454. if (indirection) {
  6455. o->mode = Addressing_Variable;
  6456. } else if (o->mode != Addressing_Variable &&
  6457. o->mode != Addressing_Constant) {
  6458. o->mode = Addressing_Value;
  6459. }
  6460. o->type = t->EnumeratedArray.elem;
  6461. return true;
  6462. case Type_Slice:
  6463. o->type = t->Slice.elem;
  6464. if (o->mode != Addressing_Constant) {
  6465. o->mode = Addressing_Variable;
  6466. }
  6467. return true;
  6468. case Type_DynamicArray:
  6469. o->type = t->DynamicArray.elem;
  6470. if (o->mode != Addressing_Constant) {
  6471. o->mode = Addressing_Variable;
  6472. }
  6473. return true;
  6474. case Type_Struct:
  6475. if (t->Struct.soa_kind != StructSoa_None) {
  6476. if (t->Struct.soa_kind == StructSoa_Fixed) {
  6477. *max_count = t->Struct.soa_count;
  6478. }
  6479. o->type = t->Struct.soa_elem;
  6480. if (o->mode == Addressing_SoaVariable || o->mode == Addressing_Variable) {
  6481. o->mode = Addressing_SoaVariable;
  6482. } else {
  6483. o->mode = Addressing_Value;
  6484. }
  6485. return true;
  6486. }
  6487. return false;
  6488. }
  6489. if (is_type_pointer(original_type) && indirection) {
  6490. Type *ptr = base_type(original_type);
  6491. if (ptr->kind == Type_Pointer && o->mode == Addressing_SoaVariable) {
  6492. o->type = ptr->Pointer.elem;
  6493. o->mode = Addressing_Value;
  6494. return true;
  6495. }
  6496. }
  6497. return false;
  6498. }
  6499. bool ternary_compare_types(Type *x, Type *y) {
  6500. if (is_type_untyped_undef(x) && type_has_undef(y)) {
  6501. return true;
  6502. } else if (is_type_untyped_nil(x) && type_has_nil(y)) {
  6503. return true;
  6504. } else if (is_type_untyped_undef(y) && type_has_undef(x)) {
  6505. return true;
  6506. } else if (is_type_untyped_nil(y) && type_has_nil(x)) {
  6507. return true;
  6508. }
  6509. return are_types_identical(x, y);
  6510. }
  6511. bool check_range(CheckerContext *c, Ast *node, Operand *x, Operand *y, ExactValue *inline_for_depth_, Type *type_hint=nullptr) {
  6512. if (!is_ast_range(node)) {
  6513. return false;
  6514. }
  6515. ast_node(ie, BinaryExpr, node);
  6516. check_expr_with_type_hint(c, x, ie->left, type_hint);
  6517. if (x->mode == Addressing_Invalid) {
  6518. return false;
  6519. }
  6520. check_expr_with_type_hint(c, y, ie->right, type_hint);
  6521. if (y->mode == Addressing_Invalid) {
  6522. return false;
  6523. }
  6524. convert_to_typed(c, x, y->type);
  6525. if (x->mode == Addressing_Invalid) {
  6526. return false;
  6527. }
  6528. convert_to_typed(c, y, x->type);
  6529. if (y->mode == Addressing_Invalid) {
  6530. return false;
  6531. }
  6532. convert_to_typed(c, x, default_type(y->type));
  6533. if (x->mode == Addressing_Invalid) {
  6534. return false;
  6535. }
  6536. convert_to_typed(c, y, default_type(x->type));
  6537. if (y->mode == Addressing_Invalid) {
  6538. return false;
  6539. }
  6540. if (!are_types_identical(x->type, y->type)) {
  6541. if (x->type != t_invalid &&
  6542. y->type != t_invalid) {
  6543. gbString xt = type_to_string(x->type);
  6544. gbString yt = type_to_string(y->type);
  6545. gbString expr_str = expr_to_string(x->expr);
  6546. error(ie->op, "Mismatched types in interval expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
  6547. gb_string_free(expr_str);
  6548. gb_string_free(yt);
  6549. gb_string_free(xt);
  6550. }
  6551. return false;
  6552. }
  6553. Type *type = x->type;
  6554. if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type) && !is_type_enum(type)) {
  6555. error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
  6556. return false;
  6557. }
  6558. if (x->mode == Addressing_Constant &&
  6559. y->mode == Addressing_Constant) {
  6560. ExactValue a = x->value;
  6561. ExactValue b = y->value;
  6562. GB_ASSERT(are_types_identical(x->type, y->type));
  6563. TokenKind op = Token_Lt;
  6564. switch (ie->op.kind) {
  6565. case Token_Ellipsis: op = Token_LtEq; break;
  6566. case Token_RangeHalf: op = Token_Lt; break;
  6567. default: error(ie->op, "Invalid range operator"); break;
  6568. }
  6569. bool ok = compare_exact_values(op, a, b);
  6570. if (!ok) {
  6571. // TODO(bill): Better error message
  6572. error(ie->op, "Invalid interval range");
  6573. return false;
  6574. }
  6575. ExactValue inline_for_depth = exact_value_sub(b, a);
  6576. if (ie->op.kind == Token_Ellipsis) {
  6577. inline_for_depth = exact_value_increment_one(inline_for_depth);
  6578. }
  6579. if (inline_for_depth_) *inline_for_depth_ = inline_for_depth;
  6580. } else if (inline_for_depth_ != nullptr) {
  6581. error(ie->op, "Interval expressions must be constant");
  6582. return false;
  6583. }
  6584. add_type_and_value(&c->checker->info, ie->left, x->mode, x->type, x->value);
  6585. add_type_and_value(&c->checker->info, ie->right, y->mode, y->type, y->value);
  6586. return true;
  6587. }
  6588. ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
  6589. u32 prev_state_flags = c->state_flags;
  6590. defer (c->state_flags = prev_state_flags);
  6591. if (node->state_flags != 0) {
  6592. u32 in = node->state_flags;
  6593. u32 out = c->state_flags;
  6594. if (in & StateFlag_no_bounds_check) {
  6595. out |= StateFlag_no_bounds_check;
  6596. out &= ~StateFlag_bounds_check;
  6597. } else if (in & StateFlag_bounds_check) {
  6598. out |= StateFlag_bounds_check;
  6599. out &= ~StateFlag_no_bounds_check;
  6600. }
  6601. c->state_flags = out;
  6602. }
  6603. ExprKind kind = Expr_Stmt;
  6604. o->mode = Addressing_Invalid;
  6605. o->type = t_invalid;
  6606. switch (node->kind) {
  6607. default:
  6608. return kind;
  6609. case_ast_node(be, BadExpr, node)
  6610. return kind;
  6611. case_end;
  6612. case_ast_node(i, Implicit, node)
  6613. switch (i->kind) {
  6614. case Token_context:
  6615. if (c->proc_name.len == 0 && c->curr_proc_sig == nullptr) {
  6616. error(node, "'context' is only allowed within procedures %p", c->curr_proc_decl);
  6617. return kind;
  6618. }
  6619. init_core_context(c->checker);
  6620. o->mode = Addressing_Context;
  6621. o->type = t_context;
  6622. break;
  6623. case Token_size_of:
  6624. o->mode = Addressing_Builtin;
  6625. o->builtin_id = BuiltinProc_size_of;
  6626. break;
  6627. case Token_align_of:
  6628. o->mode = Addressing_Builtin;
  6629. o->builtin_id = BuiltinProc_align_of;
  6630. break;
  6631. case Token_offset_of:
  6632. o->mode = Addressing_Builtin;
  6633. o->builtin_id = BuiltinProc_offset_of;
  6634. break;
  6635. case Token_type_of:
  6636. o->mode = Addressing_Builtin;
  6637. o->builtin_id = BuiltinProc_type_of;
  6638. break;
  6639. default:
  6640. error(node, "Illegal implicit name '%.*s'", LIT(i->string));
  6641. return kind;
  6642. }
  6643. case_end;
  6644. case_ast_node(i, Ident, node);
  6645. check_ident(c, o, node, nullptr, type_hint, false);
  6646. case_end;
  6647. case_ast_node(u, Undef, node);
  6648. o->mode = Addressing_Value;
  6649. o->type = t_untyped_undef;
  6650. case_end;
  6651. case_ast_node(bl, BasicLit, node);
  6652. Type *t = t_invalid;
  6653. switch (bl->value.kind) {
  6654. case ExactValue_String: t = t_untyped_string; break;
  6655. case ExactValue_Float: t = t_untyped_float; break;
  6656. case ExactValue_Complex: t = t_untyped_complex; break;
  6657. case ExactValue_Quaternion: t = t_untyped_quaternion; break;
  6658. case ExactValue_Integer:
  6659. t = t_untyped_integer;
  6660. if (bl->token.kind == Token_Rune) {
  6661. t = t_untyped_rune;
  6662. }
  6663. break;
  6664. default:
  6665. GB_PANIC("Unhandled value type for basic literal");
  6666. break;
  6667. }
  6668. o->mode = Addressing_Constant;
  6669. o->type = t;
  6670. o->value = bl->value;
  6671. case_end;
  6672. case_ast_node(bd, BasicDirective, node);
  6673. o->mode = Addressing_Constant;
  6674. if (bd->name == "file") {
  6675. o->type = t_untyped_string;
  6676. o->value = exact_value_string(bd->token.pos.file);
  6677. } else if (bd->name == "line") {
  6678. o->type = t_untyped_integer;
  6679. o->value = exact_value_i64(bd->token.pos.line);
  6680. } else if (bd->name == "procedure") {
  6681. if (c->curr_proc_decl == nullptr) {
  6682. error(node, "#procedure may only be used within procedures");
  6683. o->type = t_untyped_string;
  6684. o->value = exact_value_string(str_lit(""));
  6685. } else {
  6686. o->type = t_untyped_string;
  6687. o->value = exact_value_string(c->proc_name);
  6688. }
  6689. } else if (bd->name == "caller_location") {
  6690. init_core_source_code_location(c->checker);
  6691. error(node, "#caller_location may only be used as a default argument parameter");
  6692. o->type = t_source_code_location;
  6693. o->mode = Addressing_Value;
  6694. } else {
  6695. GB_PANIC("Unknown basic directive");
  6696. }
  6697. case_end;
  6698. case_ast_node(pg, ProcGroup, node);
  6699. error(node, "Illegal use of a procedure group");
  6700. o->mode = Addressing_Invalid;
  6701. case_end;
  6702. case_ast_node(pl, ProcLit, node);
  6703. CheckerContext ctx = *c;
  6704. DeclInfo *decl = nullptr;
  6705. Type *type = alloc_type(Type_Proc);
  6706. check_open_scope(&ctx, pl->type);
  6707. {
  6708. decl = make_decl_info(ctx.allocator, ctx.scope, ctx.decl);
  6709. decl->proc_lit = node;
  6710. ctx.decl = decl;
  6711. defer (ctx.decl = ctx.decl->parent);
  6712. if (pl->tags != 0) {
  6713. error(node, "A procedure literal cannot have tags");
  6714. pl->tags = 0; // TODO(bill): Should I zero this?!
  6715. }
  6716. check_procedure_type(&ctx, type, pl->type);
  6717. if (!is_type_proc(type)) {
  6718. gbString str = expr_to_string(node);
  6719. error(node, "Invalid procedure literal '%s'", str);
  6720. gb_string_free(str);
  6721. check_close_scope(&ctx);
  6722. return kind;
  6723. }
  6724. if (pl->body == nullptr) {
  6725. error(node, "A procedure literal must have a body");
  6726. return kind;
  6727. }
  6728. pl->decl = decl;
  6729. check_procedure_later(ctx.checker, ctx.file, empty_token, decl, type, pl->body, pl->tags);
  6730. }
  6731. check_close_scope(&ctx);
  6732. o->mode = Addressing_Value;
  6733. o->type = type;
  6734. case_end;
  6735. case_ast_node(te, TernaryExpr, node);
  6736. Operand cond = {Addressing_Invalid};
  6737. check_expr(c, &cond, te->cond);
  6738. node->viral_state_flags |= te->cond->viral_state_flags;
  6739. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  6740. error(te->cond, "Non-boolean condition in if expression");
  6741. }
  6742. Operand x = {Addressing_Invalid};
  6743. Operand y = {Addressing_Invalid};
  6744. check_expr_or_type(c, &x, te->x, type_hint);
  6745. node->viral_state_flags |= te->x->viral_state_flags;
  6746. if (te->y != nullptr) {
  6747. check_expr_or_type(c, &y, te->y, type_hint);
  6748. node->viral_state_flags |= te->y->viral_state_flags;
  6749. } else {
  6750. error(node, "A ternary expression must have an else clause");
  6751. return kind;
  6752. }
  6753. if (x.type == nullptr || x.type == t_invalid ||
  6754. y.type == nullptr || y.type == t_invalid) {
  6755. return kind;
  6756. }
  6757. if (x.mode == Addressing_Type && y.mode == Addressing_Type &&
  6758. cond.mode == Addressing_Constant && is_type_boolean(cond.type)) {
  6759. o->mode = Addressing_Type;
  6760. if (cond.value.value_bool) {
  6761. o->type = x.type;
  6762. o->expr = x.expr;
  6763. } else {
  6764. o->type = y.type;
  6765. o->expr = y.expr;
  6766. }
  6767. return Expr_Expr;
  6768. }
  6769. convert_to_typed(c, &x, y.type);
  6770. if (x.mode == Addressing_Invalid) {
  6771. return kind;
  6772. }
  6773. convert_to_typed(c, &y, x.type);
  6774. if (y.mode == Addressing_Invalid) {
  6775. x.mode = Addressing_Invalid;
  6776. return kind;
  6777. }
  6778. if (!ternary_compare_types(x.type, y.type)) {
  6779. gbString its = type_to_string(x.type);
  6780. gbString ets = type_to_string(y.type);
  6781. error(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
  6782. gb_string_free(ets);
  6783. gb_string_free(its);
  6784. return kind;
  6785. }
  6786. Type *type = x.type;
  6787. if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
  6788. type = y.type;
  6789. }
  6790. o->type = type;
  6791. o->mode = Addressing_Value;
  6792. if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  6793. x.mode == Addressing_Constant &&
  6794. y.mode == Addressing_Constant) {
  6795. o->mode = Addressing_Constant;
  6796. if (cond.value.value_bool) {
  6797. o->value = x.value;
  6798. } else {
  6799. o->value = y.value;
  6800. }
  6801. }
  6802. case_end;
  6803. case_ast_node(te, TernaryIfExpr, node);
  6804. Operand cond = {Addressing_Invalid};
  6805. check_expr(c, &cond, te->cond);
  6806. node->viral_state_flags |= te->cond->viral_state_flags;
  6807. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  6808. error(te->cond, "Non-boolean condition in ternary if expression");
  6809. }
  6810. Operand x = {Addressing_Invalid};
  6811. Operand y = {Addressing_Invalid};
  6812. check_expr_or_type(c, &x, te->x, type_hint);
  6813. node->viral_state_flags |= te->x->viral_state_flags;
  6814. if (te->y != nullptr) {
  6815. check_expr_or_type(c, &y, te->y, type_hint);
  6816. node->viral_state_flags |= te->y->viral_state_flags;
  6817. } else {
  6818. error(node, "A ternary expression must have an else clause");
  6819. return kind;
  6820. }
  6821. if (x.type == nullptr || x.type == t_invalid ||
  6822. y.type == nullptr || y.type == t_invalid) {
  6823. return kind;
  6824. }
  6825. convert_to_typed(c, &x, y.type);
  6826. if (x.mode == Addressing_Invalid) {
  6827. return kind;
  6828. }
  6829. convert_to_typed(c, &y, x.type);
  6830. if (y.mode == Addressing_Invalid) {
  6831. x.mode = Addressing_Invalid;
  6832. return kind;
  6833. }
  6834. if (!ternary_compare_types(x.type, y.type)) {
  6835. gbString its = type_to_string(x.type);
  6836. gbString ets = type_to_string(y.type);
  6837. error(node, "Mismatched types in ternary if expression, %s vs %s", its, ets);
  6838. gb_string_free(ets);
  6839. gb_string_free(its);
  6840. return kind;
  6841. }
  6842. Type *type = x.type;
  6843. if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
  6844. type = y.type;
  6845. }
  6846. o->type = type;
  6847. o->mode = Addressing_Value;
  6848. // if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  6849. // x.mode == Addressing_Constant &&
  6850. // y.mode == Addressing_Constant) {
  6851. // o->mode = Addressing_Constant;
  6852. // if (cond.value.value_bool) {
  6853. // o->value = x.value;
  6854. // } else {
  6855. // o->value = y.value;
  6856. // }
  6857. // }
  6858. case_end;
  6859. case_ast_node(te, TernaryWhenExpr, node);
  6860. Operand cond = {};
  6861. check_expr(c, &cond, te->cond);
  6862. node->viral_state_flags |= te->cond->viral_state_flags;
  6863. if (cond.mode != Addressing_Constant || !is_type_boolean(cond.type)) {
  6864. error(te->cond, "Expected a constant boolean condition in ternary when expression");
  6865. return kind;
  6866. }
  6867. if (cond.value.value_bool) {
  6868. check_expr_or_type(c, o, te->x, type_hint);
  6869. node->viral_state_flags |= te->x->viral_state_flags;
  6870. } else {
  6871. if (te->y != nullptr) {
  6872. check_expr_or_type(c, o, te->y, type_hint);
  6873. node->viral_state_flags |= te->y->viral_state_flags;
  6874. } else {
  6875. error(node, "A ternary when expression must have an else clause");
  6876. return kind;
  6877. }
  6878. }
  6879. case_end;
  6880. case_ast_node(cl, CompoundLit, node);
  6881. Type *type = type_hint;
  6882. bool is_to_be_determined_array_count = false;
  6883. bool is_constant = true;
  6884. if (cl->type != nullptr) {
  6885. type = nullptr;
  6886. // [?]Type
  6887. if (cl->type->kind == Ast_ArrayType && cl->type->ArrayType.count != nullptr) {
  6888. Ast *count = cl->type->ArrayType.count;
  6889. if (count->kind == Ast_UnaryExpr &&
  6890. count->UnaryExpr.op.kind == Token_Question) {
  6891. type = alloc_type_array(check_type(c, cl->type->ArrayType.elem), -1);
  6892. is_to_be_determined_array_count = true;
  6893. }
  6894. if (cl->elems.count > 0) {
  6895. if (cl->type->ArrayType.tag != nullptr) {
  6896. Ast *tag = cl->type->ArrayType.tag;
  6897. GB_ASSERT(tag->kind == Ast_BasicDirective);
  6898. String name = tag->BasicDirective.name;
  6899. if (name == "soa") {
  6900. error(node, "#soa arrays are not supported for compound literals");
  6901. return kind;
  6902. }
  6903. }
  6904. }
  6905. }
  6906. if (cl->type->kind == Ast_DynamicArrayType && cl->type->DynamicArrayType.tag != nullptr) {
  6907. if (cl->elems.count > 0) {
  6908. Ast *tag = cl->type->DynamicArrayType.tag;
  6909. GB_ASSERT(tag->kind == Ast_BasicDirective);
  6910. String name = tag->BasicDirective.name;
  6911. if (name == "soa") {
  6912. error(node, "#soa arrays are not supported for compound literals");
  6913. return kind;
  6914. }
  6915. }
  6916. }
  6917. if (type == nullptr) {
  6918. type = check_type(c, cl->type);
  6919. }
  6920. }
  6921. if (type == nullptr) {
  6922. error(node, "Missing type in compound literal");
  6923. return kind;
  6924. }
  6925. Type *t = base_type(type);
  6926. if (is_type_polymorphic(t)) {
  6927. gbString str = type_to_string(type);
  6928. error(node, "Cannot use a polymorphic type for a compound literal, got '%s'", str);
  6929. o->expr = node;
  6930. o->type = type;
  6931. gb_string_free(str);
  6932. return kind;
  6933. }
  6934. switch (t->kind) {
  6935. case Type_Struct: {
  6936. if (cl->elems.count == 0) {
  6937. break; // NOTE(bill): No need to init
  6938. }
  6939. if (t->Struct.is_raw_union) {
  6940. if (cl->elems.count != 0) {
  6941. gbString type_str = type_to_string(type);
  6942. error(node, "Illegal compound literal type '%s'", type_str);
  6943. gb_string_free(type_str);
  6944. }
  6945. break;
  6946. }
  6947. isize field_count = t->Struct.fields.count;
  6948. isize min_field_count = t->Struct.fields.count;
  6949. for (isize i = min_field_count-1; i >= 0; i--) {
  6950. Entity *e = t->Struct.fields[i];
  6951. GB_ASSERT(e->kind == Entity_Variable);
  6952. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  6953. min_field_count--;
  6954. } else {
  6955. break;
  6956. }
  6957. }
  6958. if (cl->elems[0]->kind == Ast_FieldValue) {
  6959. bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
  6960. for_array(i, cl->elems) {
  6961. Ast *elem = cl->elems[i];
  6962. if (elem->kind != Ast_FieldValue) {
  6963. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  6964. continue;
  6965. }
  6966. ast_node(fv, FieldValue, elem);
  6967. if (fv->field->kind != Ast_Ident) {
  6968. gbString expr_str = expr_to_string(fv->field);
  6969. error(elem, "Invalid field name '%s' in structure literal", expr_str);
  6970. gb_string_free(expr_str);
  6971. continue;
  6972. }
  6973. String name = fv->field->Ident.token.string;
  6974. Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
  6975. bool is_unknown = sel.entity == nullptr;
  6976. if (is_unknown) {
  6977. error(elem, "Unknown field '%.*s' in structure literal", LIT(name));
  6978. continue;
  6979. }
  6980. if (sel.index.count > 1) {
  6981. error(elem, "Cannot assign to an anonymous field '%.*s' in a structure literal (at the moment)", LIT(name));
  6982. continue;
  6983. }
  6984. Entity *field = t->Struct.fields[sel.index[0]];
  6985. add_entity_use(c, fv->field, field);
  6986. if (fields_visited[sel.index[0]]) {
  6987. error(elem, "Duplicate field '%.*s' in structure literal", LIT(name));
  6988. continue;
  6989. }
  6990. fields_visited[sel.index[0]] = true;
  6991. check_expr_or_type(c, o, fv->value, field->type);
  6992. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
  6993. is_constant = false;
  6994. }
  6995. if (is_constant) {
  6996. is_constant = o->mode == Addressing_Constant;
  6997. }
  6998. check_assignment(c, o, field->type, str_lit("structure literal"));
  6999. }
  7000. } else {
  7001. bool seen_field_value = false;
  7002. for_array(index, cl->elems) {
  7003. Entity *field = nullptr;
  7004. Ast *elem = cl->elems[index];
  7005. if (elem->kind == Ast_FieldValue) {
  7006. seen_field_value = true;
  7007. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7008. continue;
  7009. } else if (seen_field_value) {
  7010. error(elem, "Value elements cannot be used after a 'field = value'");
  7011. continue;
  7012. }
  7013. if (index >= field_count) {
  7014. error(o->expr, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  7015. break;
  7016. }
  7017. if (field == nullptr) {
  7018. field = t->Struct.fields[index];
  7019. }
  7020. check_expr_or_type(c, o, elem, field->type);
  7021. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
  7022. is_constant = false;
  7023. }
  7024. if (is_constant) {
  7025. is_constant = o->mode == Addressing_Constant;
  7026. }
  7027. check_assignment(c, o, field->type, str_lit("structure literal"));
  7028. }
  7029. if (cl->elems.count < field_count) {
  7030. if (min_field_count < field_count) {
  7031. if (cl->elems.count < min_field_count) {
  7032. error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
  7033. }
  7034. } else {
  7035. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  7036. }
  7037. }
  7038. }
  7039. break;
  7040. }
  7041. case Type_Slice:
  7042. case Type_Array:
  7043. case Type_DynamicArray:
  7044. case Type_SimdVector:
  7045. {
  7046. Type *elem_type = nullptr;
  7047. String context_name = {};
  7048. i64 max_type_count = -1;
  7049. if (t->kind == Type_Slice) {
  7050. elem_type = t->Slice.elem;
  7051. context_name = str_lit("slice literal");
  7052. } else if (t->kind == Type_Array) {
  7053. elem_type = t->Array.elem;
  7054. context_name = str_lit("array literal");
  7055. if (!is_to_be_determined_array_count) {
  7056. max_type_count = t->Array.count;
  7057. }
  7058. } else if (t->kind == Type_DynamicArray) {
  7059. elem_type = t->DynamicArray.elem;
  7060. context_name = str_lit("dynamic array literal");
  7061. is_constant = false;
  7062. add_package_dependency(c, "runtime", "__dynamic_array_reserve");
  7063. add_package_dependency(c, "runtime", "__dynamic_array_append");
  7064. } else if (t->kind == Type_SimdVector) {
  7065. elem_type = t->SimdVector.elem;
  7066. context_name = str_lit("simd vector literal");
  7067. max_type_count = t->SimdVector.count;
  7068. } else {
  7069. GB_PANIC("unreachable");
  7070. }
  7071. i64 max = 0;
  7072. Type *bet = base_type(elem_type);
  7073. if (!elem_type_can_be_constant(bet)) {
  7074. is_constant = false;
  7075. }
  7076. if (bet == t_invalid) {
  7077. break;
  7078. }
  7079. if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
  7080. if (is_type_simd_vector(t)) {
  7081. error(cl->elems[0], "'field = value' is not allowed for SIMD vector literals");
  7082. } else {
  7083. RangeCache rc = range_cache_make(heap_allocator());
  7084. defer (range_cache_destroy(&rc));
  7085. for_array(i, cl->elems) {
  7086. Ast *elem = cl->elems[i];
  7087. if (elem->kind != Ast_FieldValue) {
  7088. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7089. continue;
  7090. }
  7091. ast_node(fv, FieldValue, elem);
  7092. if (is_ast_range(fv->field)) {
  7093. Token op = fv->field->BinaryExpr.op;
  7094. Operand x = {};
  7095. Operand y = {};
  7096. bool ok = check_range(c, fv->field, &x, &y, nullptr);
  7097. if (!ok) {
  7098. continue;
  7099. }
  7100. if (x.mode != Addressing_Constant || !is_type_integer(core_type(x.type))) {
  7101. error(x.expr, "Expected a constant integer as an array field");
  7102. continue;
  7103. }
  7104. if (y.mode != Addressing_Constant || !is_type_integer(core_type(y.type))) {
  7105. error(y.expr, "Expected a constant integer as an array field");
  7106. continue;
  7107. }
  7108. i64 lo = exact_value_to_i64(x.value);
  7109. i64 hi = exact_value_to_i64(y.value);
  7110. i64 max_index = hi;
  7111. if (op.kind == Token_RangeHalf) {
  7112. hi -= 1;
  7113. }
  7114. bool new_range = range_cache_add_range(&rc, lo, hi);
  7115. if (!new_range) {
  7116. error(elem, "Overlapping field range index %lld %.*s %lld for %.*s", lo, LIT(op.string), hi, LIT(context_name));
  7117. continue;
  7118. }
  7119. if (max_type_count >= 0 && (lo < 0 || lo >= max_type_count)) {
  7120. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", lo, max_type_count, LIT(context_name));
  7121. continue;
  7122. }
  7123. if (max_type_count >= 0 && (hi < 0 || hi >= max_type_count)) {
  7124. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", hi, max_type_count, LIT(context_name));
  7125. continue;
  7126. }
  7127. if (max < hi) {
  7128. max = max_index;
  7129. }
  7130. Operand operand = {};
  7131. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7132. check_assignment(c, &operand, elem_type, context_name);
  7133. is_constant = is_constant && operand.mode == Addressing_Constant;
  7134. } else {
  7135. Operand op_index = {};
  7136. check_expr(c, &op_index, fv->field);
  7137. if (op_index.mode != Addressing_Constant || !is_type_integer(core_type(op_index.type))) {
  7138. error(elem, "Expected a constant integer as an array field");
  7139. continue;
  7140. }
  7141. // add_type_and_value(c->info, op_index.expr, op_index.mode, op_index.type, op_index.value);
  7142. i64 index = exact_value_to_i64(op_index.value);
  7143. if (max_type_count >= 0 && (index < 0 || index >= max_type_count)) {
  7144. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", index, max_type_count, LIT(context_name));
  7145. continue;
  7146. }
  7147. bool new_index = range_cache_add_index(&rc, index);
  7148. if (!new_index) {
  7149. error(elem, "Duplicate field index %lld for %.*s", index, LIT(context_name));
  7150. continue;
  7151. }
  7152. if (max < index+1) {
  7153. max = index+1;
  7154. }
  7155. Operand operand = {};
  7156. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7157. check_assignment(c, &operand, elem_type, context_name);
  7158. is_constant = is_constant && operand.mode == Addressing_Constant;
  7159. }
  7160. }
  7161. cl->max_count = max;
  7162. }
  7163. } else {
  7164. isize index = 0;
  7165. for (; index < cl->elems.count; index++) {
  7166. Ast *e = cl->elems[index];
  7167. if (e == nullptr) {
  7168. error(node, "Invalid literal element");
  7169. continue;
  7170. }
  7171. if (e->kind == Ast_FieldValue) {
  7172. error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7173. continue;
  7174. }
  7175. if (0 <= max_type_count && max_type_count <= index) {
  7176. error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  7177. }
  7178. Operand operand = {};
  7179. check_expr_with_type_hint(c, &operand, e, elem_type);
  7180. check_assignment(c, &operand, elem_type, context_name);
  7181. is_constant = is_constant && operand.mode == Addressing_Constant;
  7182. }
  7183. if (max < index) {
  7184. max = index;
  7185. }
  7186. }
  7187. if (t->kind == Type_Array) {
  7188. if (is_to_be_determined_array_count) {
  7189. t->Array.count = max;
  7190. } else if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
  7191. if (0 < max && max < t->Array.count) {
  7192. error(node, "Expected %lld values for this array literal, got %lld", cast(long long)t->Array.count, cast(long long)max);
  7193. }
  7194. }
  7195. }
  7196. if (t->kind == Type_SimdVector) {
  7197. if (!is_constant) {
  7198. error(node, "Expected all constant elements for a simd vector");
  7199. }
  7200. if (t->SimdVector.is_x86_mmx) {
  7201. error(node, "Compound literals are not allowed with intrinsics.x86_mmx");
  7202. }
  7203. }
  7204. break;
  7205. }
  7206. case Type_EnumeratedArray:
  7207. {
  7208. Type *elem_type = t->EnumeratedArray.elem;
  7209. Type *index_type = t->EnumeratedArray.index;
  7210. String context_name = str_lit("enumerated array literal");
  7211. i64 max_type_count = t->EnumeratedArray.count;
  7212. gbString index_type_str = type_to_string(index_type);
  7213. defer (gb_string_free(index_type_str));
  7214. i64 total_lo = exact_value_to_i64(t->EnumeratedArray.min_value);
  7215. i64 total_hi = exact_value_to_i64(t->EnumeratedArray.max_value);
  7216. String total_lo_string = {};
  7217. String total_hi_string = {};
  7218. GB_ASSERT(is_type_enum(index_type));
  7219. {
  7220. Type *bt = base_type(index_type);
  7221. GB_ASSERT(bt->kind == Type_Enum);
  7222. for_array(i, bt->Enum.fields) {
  7223. Entity *f = bt->Enum.fields[i];
  7224. if (f->kind != Entity_Constant) {
  7225. continue;
  7226. }
  7227. if (total_lo_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, t->EnumeratedArray.min_value)) {
  7228. total_lo_string = f->token.string;
  7229. }
  7230. if (total_hi_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, t->EnumeratedArray.max_value)) {
  7231. total_hi_string = f->token.string;
  7232. }
  7233. if (total_lo_string.len != 0 && total_hi_string.len != 0) {
  7234. break;
  7235. }
  7236. }
  7237. }
  7238. i64 max = 0;
  7239. Type *bet = base_type(elem_type);
  7240. if (!elem_type_can_be_constant(bet)) {
  7241. is_constant = false;
  7242. }
  7243. if (bet == t_invalid) {
  7244. break;
  7245. }
  7246. if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
  7247. RangeCache rc = range_cache_make(heap_allocator());
  7248. defer (range_cache_destroy(&rc));
  7249. for_array(i, cl->elems) {
  7250. Ast *elem = cl->elems[i];
  7251. if (elem->kind != Ast_FieldValue) {
  7252. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7253. continue;
  7254. }
  7255. ast_node(fv, FieldValue, elem);
  7256. if (is_ast_range(fv->field)) {
  7257. Token op = fv->field->BinaryExpr.op;
  7258. Operand x = {};
  7259. Operand y = {};
  7260. bool ok = check_range(c, fv->field, &x, &y, nullptr, index_type);
  7261. if (!ok) {
  7262. continue;
  7263. }
  7264. if (x.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
  7265. error(x.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7266. continue;
  7267. }
  7268. if (y.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
  7269. error(y.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7270. continue;
  7271. }
  7272. i64 lo = exact_value_to_i64(x.value);
  7273. i64 hi = exact_value_to_i64(y.value);
  7274. i64 max_index = hi;
  7275. if (op.kind == Token_RangeHalf) {
  7276. hi -= 1;
  7277. }
  7278. bool new_range = range_cache_add_range(&rc, lo, hi);
  7279. if (!new_range) {
  7280. gbString lo_str = expr_to_string(x.expr);
  7281. gbString hi_str = expr_to_string(y.expr);
  7282. error(elem, "Overlapping field range index %s %.*s %s for %.*s", lo_str, LIT(op.string), hi_str, LIT(context_name));
  7283. gb_string_free(hi_str);
  7284. gb_string_free(lo_str);
  7285. continue;
  7286. }
  7287. // NOTE(bill): These are sanity checks for invalid enum values
  7288. if (max_type_count >= 0 && (lo < total_lo || lo > total_hi)) {
  7289. gbString lo_str = expr_to_string(x.expr);
  7290. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", lo_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7291. gb_string_free(lo_str);
  7292. continue;
  7293. }
  7294. if (max_type_count >= 0 && (hi < 0 || hi > total_hi)) {
  7295. gbString hi_str = expr_to_string(y.expr);
  7296. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", hi_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7297. gb_string_free(hi_str);
  7298. continue;
  7299. }
  7300. if (max < hi) {
  7301. max = max_index;
  7302. }
  7303. Operand operand = {};
  7304. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7305. check_assignment(c, &operand, elem_type, context_name);
  7306. is_constant = is_constant && operand.mode == Addressing_Constant;
  7307. } else {
  7308. Operand op_index = {};
  7309. check_expr_with_type_hint(c, &op_index, fv->field, index_type);
  7310. if (op_index.mode != Addressing_Constant || !are_types_identical(op_index.type, index_type)) {
  7311. error(op_index.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7312. continue;
  7313. }
  7314. i64 index = exact_value_to_i64(op_index.value);
  7315. if (max_type_count >= 0 && (index < total_lo || index > total_hi)) {
  7316. gbString idx_str = expr_to_string(op_index.expr);
  7317. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", idx_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7318. gb_string_free(idx_str);
  7319. continue;
  7320. }
  7321. bool new_index = range_cache_add_index(&rc, index);
  7322. if (!new_index) {
  7323. gbString idx_str = expr_to_string(op_index.expr);
  7324. error(elem, "Duplicate field index %s for %.*s", idx_str, LIT(context_name));
  7325. gb_string_free(idx_str);
  7326. continue;
  7327. }
  7328. if (max < index+1) {
  7329. max = index+1;
  7330. }
  7331. Operand operand = {};
  7332. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7333. check_assignment(c, &operand, elem_type, context_name);
  7334. is_constant = is_constant && operand.mode == Addressing_Constant;
  7335. }
  7336. }
  7337. cl->max_count = max;
  7338. } else {
  7339. isize index = 0;
  7340. for (; index < cl->elems.count; index++) {
  7341. Ast *e = cl->elems[index];
  7342. if (e == nullptr) {
  7343. error(node, "Invalid literal element");
  7344. continue;
  7345. }
  7346. if (e->kind == Ast_FieldValue) {
  7347. error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7348. continue;
  7349. }
  7350. if (0 <= max_type_count && max_type_count <= index) {
  7351. error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  7352. }
  7353. Operand operand = {};
  7354. check_expr_with_type_hint(c, &operand, e, elem_type);
  7355. check_assignment(c, &operand, elem_type, context_name);
  7356. is_constant = is_constant && operand.mode == Addressing_Constant;
  7357. }
  7358. if (max < index) {
  7359. max = index;
  7360. }
  7361. }
  7362. if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
  7363. if (0 < max && max < t->EnumeratedArray.count) {
  7364. error(node, "Expected %lld values for this enumerated array literal, got %lld", cast(long long)t->EnumeratedArray.count, cast(long long)max);
  7365. } else {
  7366. error(node, "Enumerated array literals must only have 'field = value' elements, bare elements are not allowed");
  7367. }
  7368. }
  7369. break;
  7370. }
  7371. case Type_Basic: {
  7372. if (!is_type_any(t)) {
  7373. if (cl->elems.count != 0) {
  7374. error(node, "Illegal compound literal");
  7375. }
  7376. break;
  7377. }
  7378. if (cl->elems.count == 0) {
  7379. break; // NOTE(bill): No need to init
  7380. }
  7381. { // Checker values
  7382. Type *field_types[2] = {t_rawptr, t_typeid};
  7383. isize field_count = 2;
  7384. if (cl->elems[0]->kind == Ast_FieldValue) {
  7385. bool fields_visited[2] = {};
  7386. for_array(i, cl->elems) {
  7387. Ast *elem = cl->elems[i];
  7388. if (elem->kind != Ast_FieldValue) {
  7389. error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
  7390. continue;
  7391. }
  7392. ast_node(fv, FieldValue, elem);
  7393. if (fv->field->kind != Ast_Ident) {
  7394. gbString expr_str = expr_to_string(fv->field);
  7395. error(elem, "Invalid field name '%s' in 'any' literal", expr_str);
  7396. gb_string_free(expr_str);
  7397. continue;
  7398. }
  7399. String name = fv->field->Ident.token.string;
  7400. Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
  7401. if (sel.entity == nullptr) {
  7402. error(elem, "Unknown field '%.*s' in 'any' literal", LIT(name));
  7403. continue;
  7404. }
  7405. isize index = sel.index[0];
  7406. if (fields_visited[index]) {
  7407. error(elem, "Duplicate field '%.*s' in 'any' literal", LIT(name));
  7408. continue;
  7409. }
  7410. fields_visited[index] = true;
  7411. check_expr(c, o, fv->value);
  7412. // NOTE(bill): 'any' literals can never be constant
  7413. is_constant = false;
  7414. check_assignment(c, o, field_types[index], str_lit("'any' literal"));
  7415. }
  7416. } else {
  7417. for_array(index, cl->elems) {
  7418. Ast *elem = cl->elems[index];
  7419. if (elem->kind == Ast_FieldValue) {
  7420. error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
  7421. continue;
  7422. }
  7423. check_expr(c, o, elem);
  7424. if (index >= field_count) {
  7425. error(o->expr, "Too many values in 'any' literal, expected %td", field_count);
  7426. break;
  7427. }
  7428. // NOTE(bill): 'any' literals can never be constant
  7429. is_constant = false;
  7430. check_assignment(c, o, field_types[index], str_lit("'any' literal"));
  7431. }
  7432. if (cl->elems.count < field_count) {
  7433. error(cl->close, "Too few values in 'any' literal, expected %td, got %td", field_count, cl->elems.count);
  7434. }
  7435. }
  7436. }
  7437. break;
  7438. }
  7439. case Type_Map: {
  7440. if (cl->elems.count == 0) {
  7441. break;
  7442. }
  7443. is_constant = false;
  7444. { // Checker values
  7445. for_array(i, cl->elems) {
  7446. Ast *elem = cl->elems[i];
  7447. if (elem->kind != Ast_FieldValue) {
  7448. error(elem, "Only 'field = value' elements are allowed in a map literal");
  7449. continue;
  7450. }
  7451. ast_node(fv, FieldValue, elem);
  7452. check_expr_with_type_hint(c, o, fv->field, t->Map.key);
  7453. check_assignment(c, o, t->Map.key, str_lit("map literal"));
  7454. if (o->mode == Addressing_Invalid) {
  7455. continue;
  7456. }
  7457. check_expr_with_type_hint(c, o, fv->value, t->Map.value);
  7458. check_assignment(c, o, t->Map.value, str_lit("map literal"));
  7459. }
  7460. }
  7461. add_package_dependency(c, "runtime", "__dynamic_map_reserve");
  7462. add_package_dependency(c, "runtime", "__dynamic_map_set");
  7463. break;
  7464. }
  7465. case Type_BitSet: {
  7466. if (cl->elems.count == 0) {
  7467. break; // NOTE(bill): No need to init
  7468. }
  7469. Type *et = base_type(t->BitSet.elem);
  7470. isize field_count = 0;
  7471. if (et->kind == Type_Enum) {
  7472. field_count = et->Enum.fields.count;
  7473. }
  7474. if (cl->elems[0]->kind == Ast_FieldValue) {
  7475. error(cl->elems[0], "'field = value' in a bit_set a literal is not allowed");
  7476. is_constant = false;
  7477. } else {
  7478. for_array(index, cl->elems) {
  7479. Entity *field = nullptr;
  7480. Ast *elem = cl->elems[index];
  7481. if (elem->kind == Ast_FieldValue) {
  7482. error(elem, "'field = value' in a bit_set a literal is not allowed");
  7483. continue;
  7484. }
  7485. check_expr_with_type_hint(c, o, elem, et);
  7486. if (is_constant) {
  7487. is_constant = o->mode == Addressing_Constant;
  7488. }
  7489. check_assignment(c, o, t->BitSet.elem, str_lit("bit_set literal"));
  7490. if (o->mode == Addressing_Constant) {
  7491. i64 lower = t->BitSet.lower;
  7492. i64 upper = t->BitSet.upper;
  7493. i64 v = exact_value_to_i64(o->value);
  7494. if (lower <= v && v <= upper) {
  7495. // okay
  7496. } else {
  7497. error(elem, "Bit field value out of bounds, %lld not in the range %lld .. %lld", v, lower, upper);
  7498. continue;
  7499. }
  7500. }
  7501. }
  7502. }
  7503. break;
  7504. }
  7505. default: {
  7506. if (cl->elems.count == 0) {
  7507. break; // NOTE(bill): No need to init
  7508. }
  7509. gbString str = type_to_string(type);
  7510. error(node, "Invalid compound literal type '%s'", str);
  7511. gb_string_free(str);
  7512. return kind;
  7513. }
  7514. }
  7515. if (is_constant) {
  7516. o->mode = Addressing_Constant;
  7517. if (is_type_bit_set(type)) {
  7518. // NOTE(bill): Encode as an integer
  7519. i64 lower = base_type(type)->BitSet.lower;
  7520. u64 bits = 0;
  7521. for_array(index, cl->elems) {
  7522. Entity *field = nullptr;
  7523. Ast *elem = cl->elems[index];
  7524. GB_ASSERT(elem->kind != Ast_FieldValue);
  7525. TypeAndValue tav = elem->tav;
  7526. ExactValue i = exact_value_to_integer(tav.value);
  7527. if (i.kind != ExactValue_Integer) {
  7528. continue;
  7529. }
  7530. i64 val = big_int_to_i64(&i.value_integer);
  7531. val -= lower;
  7532. u64 bit = u64(1ll<<val);
  7533. bits |= bit;
  7534. }
  7535. o->value = exact_value_u64(bits);
  7536. } else if (is_type_constant_type(type) && cl->elems.count == 0) {
  7537. ExactValue value = exact_value_compound(node);
  7538. Type *bt = core_type(type);
  7539. if (bt->kind == Type_Basic) {
  7540. if (bt->Basic.flags & BasicFlag_Boolean) {
  7541. value = exact_value_bool(false);
  7542. } else if (bt->Basic.flags & BasicFlag_Integer) {
  7543. value = exact_value_i64(0);
  7544. } else if (bt->Basic.flags & BasicFlag_Unsigned) {
  7545. value = exact_value_i64(0);
  7546. } else if (bt->Basic.flags & BasicFlag_Float) {
  7547. value = exact_value_float(0);
  7548. } else if (bt->Basic.flags & BasicFlag_Complex) {
  7549. value = exact_value_complex(0, 0);
  7550. } else if (bt->Basic.flags & BasicFlag_Quaternion) {
  7551. value = exact_value_quaternion(0, 0, 0, 0);
  7552. } else if (bt->Basic.flags & BasicFlag_Pointer) {
  7553. value = exact_value_pointer(0);
  7554. } else if (bt->Basic.flags & BasicFlag_String) {
  7555. String empty_string = {};
  7556. value = exact_value_string(empty_string);
  7557. } else if (bt->Basic.flags & BasicFlag_Rune) {
  7558. value = exact_value_i64(0);
  7559. }
  7560. }
  7561. o->value = value;
  7562. } else {
  7563. o->value = exact_value_compound(node);
  7564. }
  7565. } else {
  7566. o->mode = Addressing_Value;
  7567. }
  7568. o->type = type;
  7569. case_end;
  7570. case_ast_node(pe, ParenExpr, node);
  7571. kind = check_expr_base(c, o, pe->expr, type_hint);
  7572. node->viral_state_flags |= pe->expr->viral_state_flags;
  7573. o->expr = node;
  7574. case_end;
  7575. case_ast_node(te, TagExpr, node);
  7576. String name = te->name.string;
  7577. error(node, "Unknown tag expression, #%.*s", LIT(name));
  7578. if (te->expr) {
  7579. kind = check_expr_base(c, o, te->expr, type_hint);
  7580. node->viral_state_flags |= te->expr->viral_state_flags;
  7581. }
  7582. o->expr = node;
  7583. case_end;
  7584. case_ast_node(ta, TypeAssertion, node);
  7585. check_expr(c, o, ta->expr);
  7586. node->viral_state_flags |= ta->expr->viral_state_flags;
  7587. if (o->mode == Addressing_Invalid) {
  7588. o->expr = node;
  7589. return kind;
  7590. }
  7591. if (o->mode == Addressing_Constant) {
  7592. gbString expr_str = expr_to_string(o->expr);
  7593. error(o->expr, "A type assertion cannot be applied to a constant expression: '%s'", expr_str);
  7594. gb_string_free(expr_str);
  7595. o->mode = Addressing_Invalid;
  7596. o->expr = node;
  7597. return kind;
  7598. }
  7599. if (is_type_untyped(o->type)) {
  7600. gbString expr_str = expr_to_string(o->expr);
  7601. error(o->expr, "A type assertion cannot be applied to an untyped expression: '%s'", expr_str);
  7602. gb_string_free(expr_str);
  7603. o->mode = Addressing_Invalid;
  7604. o->expr = node;
  7605. return kind;
  7606. }
  7607. bool src_is_ptr = is_type_pointer(o->type);
  7608. Type *src = type_deref(o->type);
  7609. Type *bsrc = base_type(src);
  7610. if (ta->type != nullptr && ta->type->kind == Ast_UnaryExpr && ta->type->UnaryExpr.op.kind == Token_Question) {
  7611. if (!is_type_union(src)) {
  7612. gbString str = type_to_string(o->type);
  7613. error(o->expr, "Type assertions with .? can only operate on unions with 1 variant, got %s", str);
  7614. gb_string_free(str);
  7615. o->mode = Addressing_Invalid;
  7616. o->expr = node;
  7617. return kind;
  7618. }
  7619. if (bsrc->Union.variants.count != 1) {
  7620. error(o->expr, "Type assertions with .? can only operate on unions with 1 variant, got %lld", cast(long long)bsrc->Union.variants.count);
  7621. o->mode = Addressing_Invalid;
  7622. o->expr = node;
  7623. return kind;
  7624. }
  7625. add_type_info_type(c, o->type);
  7626. add_type_info_type(c, bsrc->Union.variants[0]);
  7627. o->type = bsrc->Union.variants[0];
  7628. o->mode = Addressing_OptionalOk;
  7629. } else {
  7630. Type *t = check_type(c, ta->type);
  7631. Type *dst = t;
  7632. Type *bdst = base_type(dst);
  7633. if (is_type_union(src)) {
  7634. bool ok = false;
  7635. for_array(i, bsrc->Union.variants) {
  7636. Type *vt = bsrc->Union.variants[i];
  7637. if (are_types_identical(vt, dst)) {
  7638. ok = true;
  7639. break;
  7640. }
  7641. }
  7642. if (!ok) {
  7643. gbString expr_str = expr_to_string(o->expr);
  7644. gbString dst_type_str = type_to_string(t);
  7645. defer (gb_string_free(expr_str));
  7646. defer (gb_string_free(dst_type_str));
  7647. if (bsrc->Union.variants.count == 0) {
  7648. error(o->expr, "Cannot type assert '%s' to '%s' as this is an empty union", expr_str, dst_type_str);
  7649. } else {
  7650. error(o->expr, "Cannot type assert '%s' to '%s' as it is not a variant of that union", expr_str, dst_type_str);
  7651. }
  7652. o->mode = Addressing_Invalid;
  7653. o->expr = node;
  7654. return kind;
  7655. }
  7656. add_type_info_type(c, o->type);
  7657. add_type_info_type(c, t);
  7658. o->type = t;
  7659. o->mode = Addressing_OptionalOk;
  7660. } else if (is_type_any(src)) {
  7661. o->type = t;
  7662. o->mode = Addressing_OptionalOk;
  7663. add_type_info_type(c, o->type);
  7664. add_type_info_type(c, t);
  7665. } else {
  7666. gbString str = type_to_string(o->type);
  7667. error(o->expr, "Type assertions can only operate on unions and 'any', got %s", str);
  7668. gb_string_free(str);
  7669. o->mode = Addressing_Invalid;
  7670. o->expr = node;
  7671. return kind;
  7672. }
  7673. }
  7674. add_package_dependency(c, "runtime", "type_assertion_check");
  7675. case_end;
  7676. case_ast_node(tc, TypeCast, node);
  7677. check_expr_or_type(c, o, tc->type);
  7678. if (o->mode != Addressing_Type) {
  7679. gbString str = expr_to_string(tc->type);
  7680. error(tc->type, "Expected a type, got %s", str);
  7681. gb_string_free(str);
  7682. o->mode = Addressing_Invalid;
  7683. }
  7684. if (o->mode == Addressing_Invalid) {
  7685. o->expr = node;
  7686. return kind;
  7687. }
  7688. Type *type = o->type;
  7689. check_expr_base(c, o, tc->expr, type);
  7690. node->viral_state_flags |= tc->expr->viral_state_flags;
  7691. if (o->mode != Addressing_Invalid) {
  7692. switch (tc->token.kind) {
  7693. case Token_transmute:
  7694. check_transmute(c, node, o, type);
  7695. break;
  7696. case Token_cast:
  7697. check_cast(c, o, type);
  7698. break;
  7699. default:
  7700. error(node, "Invalid AST: Invalid casting expression");
  7701. o->mode = Addressing_Invalid;
  7702. break;
  7703. }
  7704. }
  7705. return Expr_Expr;
  7706. case_end;
  7707. case_ast_node(ac, AutoCast, node);
  7708. check_expr_base(c, o, ac->expr, type_hint);
  7709. node->viral_state_flags |= ac->expr->viral_state_flags;
  7710. if (o->mode == Addressing_Invalid) {
  7711. o->expr = node;
  7712. return kind;
  7713. }
  7714. if (type_hint) {
  7715. check_cast(c, o, type_hint);
  7716. }
  7717. o->expr = node;
  7718. return Expr_Expr;
  7719. case_end;
  7720. case_ast_node(ue, UnaryExpr, node);
  7721. check_expr_base(c, o, ue->expr, type_hint);
  7722. node->viral_state_flags |= ue->expr->viral_state_flags;
  7723. if (o->mode == Addressing_Invalid) {
  7724. o->expr = node;
  7725. return kind;
  7726. }
  7727. check_unary_expr(c, o, ue->op, node);
  7728. if (o->mode == Addressing_Invalid) {
  7729. o->expr = node;
  7730. return kind;
  7731. }
  7732. case_end;
  7733. case_ast_node(be, BinaryExpr, node);
  7734. check_binary_expr(c, o, node, type_hint, true);
  7735. if (o->mode == Addressing_Invalid) {
  7736. o->expr = node;
  7737. return kind;
  7738. }
  7739. case_end;
  7740. case_ast_node(se, SelectorExpr, node);
  7741. check_selector(c, o, node, type_hint);
  7742. node->viral_state_flags |= se->expr->viral_state_flags;
  7743. case_end;
  7744. case_ast_node(ise, ImplicitSelectorExpr, node);
  7745. o->type = t_invalid;
  7746. o->expr = node;
  7747. o->mode = Addressing_Invalid;
  7748. Type *th = type_hint;
  7749. if (th == nullptr) {
  7750. gbString str = expr_to_string(node);
  7751. error(node, "Cannot determine type for implicit selector expression '%s'", str);
  7752. gb_string_free(str);
  7753. return Expr_Expr;
  7754. }
  7755. o->type = th;
  7756. Type *enum_type = th;
  7757. if (!is_type_enum(th)) {
  7758. bool show_error = true;
  7759. if (is_type_union(th)) {
  7760. Type *union_type = base_type(th);
  7761. isize enum_count = 0;
  7762. Type *et = nullptr;
  7763. for_array(i, union_type->Union.variants) {
  7764. Type *vt = union_type->Union.variants[i];
  7765. if (is_type_enum(vt)) {
  7766. enum_count += 1;
  7767. et = vt;
  7768. }
  7769. }
  7770. if (enum_count == 1) {
  7771. show_error = false;
  7772. enum_type = et;
  7773. }
  7774. }
  7775. if (show_error) {
  7776. gbString typ = type_to_string(th);
  7777. gbString str = expr_to_string(node);
  7778. error(node, "Invalid type '%s' for implicit selector expression '%s'", typ, str);
  7779. gb_string_free(str);
  7780. gb_string_free(typ);
  7781. return Expr_Expr;
  7782. }
  7783. }
  7784. GB_ASSERT(ise->selector->kind == Ast_Ident);
  7785. String name = ise->selector->Ident.token.string;
  7786. enum_type = base_type(enum_type);
  7787. GB_ASSERT(enum_type->kind == Type_Enum);
  7788. Entity *e = scope_lookup_current(enum_type->Enum.scope, name);
  7789. if (e == nullptr) {
  7790. gbString typ = type_to_string(th);
  7791. error(node, "Undeclared name %.*s for type '%s'", LIT(name), typ);
  7792. gb_string_free(typ);
  7793. return Expr_Expr;
  7794. }
  7795. GB_ASSERT(are_types_identical(base_type(e->type), enum_type));
  7796. GB_ASSERT(e->kind == Entity_Constant);
  7797. o->value = e->Constant.value;
  7798. o->mode = Addressing_Constant;
  7799. o->type = e->type;
  7800. return Expr_Expr;
  7801. case_end;
  7802. case_ast_node(ie, IndexExpr, node);
  7803. check_expr(c, o, ie->expr);
  7804. node->viral_state_flags |= ie->expr->viral_state_flags;
  7805. if (o->mode == Addressing_Invalid) {
  7806. o->expr = node;
  7807. return kind;
  7808. }
  7809. Type *t = base_type(type_deref(o->type));
  7810. bool is_ptr = is_type_pointer(o->type);
  7811. bool is_const = o->mode == Addressing_Constant;
  7812. if (is_type_map(t)) {
  7813. Operand key = {};
  7814. check_expr_with_type_hint(c, &key, ie->index, t->Map.key);
  7815. check_assignment(c, &key, t->Map.key, str_lit("map index"));
  7816. if (key.mode == Addressing_Invalid) {
  7817. o->mode = Addressing_Invalid;
  7818. o->expr = node;
  7819. return kind;
  7820. }
  7821. o->mode = Addressing_MapIndex;
  7822. o->type = t->Map.value;
  7823. o->expr = node;
  7824. add_package_dependency(c, "runtime", "__dynamic_map_get");
  7825. add_package_dependency(c, "runtime", "__dynamic_map_set");
  7826. return Expr_Expr;
  7827. }
  7828. i64 max_count = -1;
  7829. bool valid = check_set_index_data(o, t, is_ptr, &max_count, o->type);
  7830. if (is_const) {
  7831. if (is_type_array(t)) {
  7832. // Okay
  7833. } else if (is_type_enumerated_array(t)) {
  7834. // Okay
  7835. } else if (is_type_string(t)) {
  7836. // Okay
  7837. } else {
  7838. valid = false;
  7839. }
  7840. }
  7841. if (!valid) {
  7842. gbString str = expr_to_string(o->expr);
  7843. gbString type_str = type_to_string(o->type);
  7844. defer (gb_string_free(str));
  7845. defer (gb_string_free(type_str));
  7846. if (is_const) {
  7847. error(o->expr, "Cannot index constant '%s' of type '%s'", str, type_str);
  7848. } else {
  7849. error(o->expr, "Cannot index '%s' of type '%s'", str, type_str);
  7850. }
  7851. o->mode = Addressing_Invalid;
  7852. o->expr = node;
  7853. return kind;
  7854. }
  7855. if (ie->index == nullptr) {
  7856. gbString str = expr_to_string(o->expr);
  7857. error(o->expr, "Missing index for '%s'", str);
  7858. gb_string_free(str);
  7859. o->mode = Addressing_Invalid;
  7860. o->expr = node;
  7861. return kind;
  7862. }
  7863. Type *index_type_hint = nullptr;
  7864. if (is_type_enumerated_array(t)) {
  7865. Type *bt = base_type(t);
  7866. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  7867. index_type_hint = bt->EnumeratedArray.index;
  7868. }
  7869. i64 index = 0;
  7870. bool ok = check_index_value(c, false, ie->index, max_count, &index, index_type_hint);
  7871. if (is_const) {
  7872. if (index < 0) {
  7873. gbString str = expr_to_string(o->expr);
  7874. error(o->expr, "Cannot index a constant '%s'", str);
  7875. error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
  7876. gb_string_free(str);
  7877. o->mode = Addressing_Invalid;
  7878. o->expr = node;
  7879. return kind;
  7880. } else if (ok) {
  7881. ExactValue value = type_and_value_of_expr(ie->expr).value;
  7882. o->mode = Addressing_Constant;
  7883. o->value = get_constant_field_single(c, value, cast(i32)index, nullptr, nullptr);
  7884. }
  7885. }
  7886. node->viral_state_flags |= ie->index->viral_state_flags;
  7887. case_end;
  7888. case_ast_node(se, SliceExpr, node);
  7889. check_expr(c, o, se->expr);
  7890. node->viral_state_flags |= se->expr->viral_state_flags;
  7891. if (o->mode == Addressing_Invalid) {
  7892. o->mode = Addressing_Invalid;
  7893. o->expr = node;
  7894. return kind;
  7895. }
  7896. bool valid = false;
  7897. i64 max_count = -1;
  7898. Type *t = base_type(type_deref(o->type));
  7899. switch (t->kind) {
  7900. case Type_Basic:
  7901. if (t->Basic.kind == Basic_string || t->Basic.kind == Basic_UntypedString) {
  7902. valid = true;
  7903. if (o->mode == Addressing_Constant) {
  7904. max_count = o->value.value_string.len;
  7905. }
  7906. o->type = type_deref(o->type);
  7907. }
  7908. break;
  7909. case Type_Array:
  7910. valid = true;
  7911. max_count = t->Array.count;
  7912. if (o->mode != Addressing_Variable && !is_type_pointer(o->type)) {
  7913. gbString str = expr_to_string(node);
  7914. error(node, "Cannot slice array '%s', value is not addressable", str);
  7915. gb_string_free(str);
  7916. o->mode = Addressing_Invalid;
  7917. o->expr = node;
  7918. return kind;
  7919. }
  7920. o->type = alloc_type_slice(t->Array.elem);
  7921. break;
  7922. case Type_Slice:
  7923. valid = true;
  7924. o->type = type_deref(o->type);
  7925. break;
  7926. case Type_DynamicArray:
  7927. valid = true;
  7928. o->type = alloc_type_slice(t->DynamicArray.elem);
  7929. break;
  7930. case Type_Struct:
  7931. if (is_type_soa_struct(t)) {
  7932. valid = true;
  7933. o->type = make_soa_struct_slice(c, nullptr, nullptr, t->Struct.soa_elem);
  7934. }
  7935. break;
  7936. }
  7937. if (!valid) {
  7938. gbString str = expr_to_string(o->expr);
  7939. gbString type_str = type_to_string(o->type);
  7940. error(o->expr, "Cannot slice '%s' of type '%s'", str, type_str);
  7941. gb_string_free(type_str);
  7942. gb_string_free(str);
  7943. o->mode = Addressing_Invalid;
  7944. o->expr = node;
  7945. return kind;
  7946. }
  7947. o->mode = Addressing_Value;
  7948. if (se->low == nullptr && se->high != nullptr) {
  7949. // error(se->interval0, "1st index is required if a 2nd index is specified");
  7950. // It is okay to continue as it will assume the 1st index is zero
  7951. }
  7952. i64 indices[2] = {};
  7953. Ast *nodes[2] = {se->low, se->high};
  7954. for (isize i = 0; i < gb_count_of(nodes); i++) {
  7955. i64 index = max_count;
  7956. if (nodes[i] != nullptr) {
  7957. i64 capacity = -1;
  7958. if (max_count >= 0) {
  7959. capacity = max_count;
  7960. }
  7961. i64 j = 0;
  7962. if (check_index_value(c, true, nodes[i], capacity, &j)) {
  7963. index = j;
  7964. }
  7965. node->viral_state_flags |= nodes[i]->viral_state_flags;
  7966. } else if (i == 0) {
  7967. index = 0;
  7968. }
  7969. indices[i] = index;
  7970. }
  7971. for (isize i = 0; i < gb_count_of(indices); i++) {
  7972. i64 a = indices[i];
  7973. for (isize j = i+1; j < gb_count_of(indices); j++) {
  7974. i64 b = indices[j];
  7975. if (a > b && b >= 0) {
  7976. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  7977. }
  7978. }
  7979. }
  7980. if (is_type_string(t) && max_count >= 0) {
  7981. bool all_constant = true;
  7982. for (isize i = 0; i < gb_count_of(nodes); i++) {
  7983. if (nodes[i] != nullptr) {
  7984. TypeAndValue tav = type_and_value_of_expr(nodes[i]);
  7985. if (tav.mode != Addressing_Constant) {
  7986. all_constant = false;
  7987. break;
  7988. }
  7989. }
  7990. }
  7991. if (!all_constant) {
  7992. gbString str = expr_to_string(o->expr);
  7993. error(o->expr, "Cannot slice '%s' with non-constant indices", str);
  7994. error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
  7995. gb_string_free(str);
  7996. o->mode = Addressing_Value; // NOTE(bill): Keep subsequent values going without erring
  7997. o->expr = node;
  7998. return kind;
  7999. }
  8000. String s = {};
  8001. if (o->value.kind == ExactValue_String) {
  8002. s = o->value.value_string;
  8003. }
  8004. o->mode = Addressing_Constant;
  8005. o->type = t;
  8006. o->value = exact_value_string(substring(s, indices[0], indices[1]));
  8007. }
  8008. case_end;
  8009. case_ast_node(ce, CallExpr, node);
  8010. return check_call_expr(c, o, node, type_hint);
  8011. case_end;
  8012. case_ast_node(de, DerefExpr, node);
  8013. check_expr_or_type(c, o, de->expr);
  8014. node->viral_state_flags |= de->expr->viral_state_flags;
  8015. if (o->mode == Addressing_Invalid) {
  8016. o->mode = Addressing_Invalid;
  8017. o->expr = node;
  8018. return kind;
  8019. } else {
  8020. Type *t = base_type(o->type);
  8021. if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) {
  8022. o->mode = Addressing_Variable;
  8023. o->type = t->Pointer.elem;
  8024. } else {
  8025. gbString str = expr_to_string(o->expr);
  8026. gbString typ = type_to_string(o->type);
  8027. error(o->expr, "Cannot dereference '%s' of type '%s'", str, typ);
  8028. gb_string_free(typ);
  8029. gb_string_free(str);
  8030. o->mode = Addressing_Invalid;
  8031. o->expr = node;
  8032. return kind;
  8033. }
  8034. }
  8035. case_end;
  8036. case Ast_TypeidType:
  8037. case Ast_PolyType:
  8038. case Ast_ProcType:
  8039. case Ast_PointerType:
  8040. case Ast_ArrayType:
  8041. case Ast_DynamicArrayType:
  8042. case Ast_StructType:
  8043. case Ast_UnionType:
  8044. case Ast_EnumType:
  8045. case Ast_MapType:
  8046. case Ast_OpaqueType:
  8047. case Ast_BitSetType:
  8048. case Ast_BitFieldType:
  8049. o->mode = Addressing_Type;
  8050. o->type = check_type(c, node);
  8051. break;
  8052. }
  8053. kind = Expr_Expr;
  8054. o->expr = node;
  8055. return kind;
  8056. }
  8057. ExprKind check_expr_base(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
  8058. ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
  8059. Type *type = nullptr;
  8060. ExactValue value = {ExactValue_Invalid};
  8061. switch (o->mode) {
  8062. case Addressing_Invalid:
  8063. type = t_invalid;
  8064. break;
  8065. case Addressing_NoValue:
  8066. type = nullptr;
  8067. break;
  8068. case Addressing_Constant:
  8069. value = o->value;
  8070. type = o->type;
  8071. break;
  8072. default:
  8073. type = o->type;
  8074. break;
  8075. }
  8076. if (type != nullptr && is_type_untyped(type)) {
  8077. add_untyped(&c->checker->info, node, false, o->mode, type, value);
  8078. }
  8079. add_type_and_value(&c->checker->info, node, o->mode, type, value);
  8080. return kind;
  8081. }
  8082. void check_multi_expr_or_type(CheckerContext *c, Operand *o, Ast *e) {
  8083. check_expr_base(c, o, e, nullptr);
  8084. switch (o->mode) {
  8085. default:
  8086. return; // NOTE(bill): Valid
  8087. case Addressing_NoValue:
  8088. error_operand_no_value(o);
  8089. break;
  8090. }
  8091. o->mode = Addressing_Invalid;
  8092. }
  8093. void check_multi_expr(CheckerContext *c, Operand *o, Ast *e) {
  8094. check_expr_base(c, o, e, nullptr);
  8095. switch (o->mode) {
  8096. default:
  8097. return; // NOTE(bill): Valid
  8098. case Addressing_NoValue:
  8099. error_operand_no_value(o);
  8100. break;
  8101. case Addressing_Type:
  8102. error_operand_not_expression(o);
  8103. break;
  8104. }
  8105. o->mode = Addressing_Invalid;
  8106. }
  8107. void check_not_tuple(CheckerContext *c, Operand *o) {
  8108. if (o->mode == Addressing_Value) {
  8109. // NOTE(bill): Tuples are not first class thus never named
  8110. if (o->type->kind == Type_Tuple) {
  8111. isize count = o->type->Tuple.variables.count;
  8112. error(o->expr,
  8113. "%td-valued tuple found where single value expected", count);
  8114. o->mode = Addressing_Invalid;
  8115. GB_ASSERT(count != 1);
  8116. }
  8117. }
  8118. }
  8119. void check_expr(CheckerContext *c, Operand *o, Ast *e) {
  8120. check_multi_expr(c, o, e);
  8121. check_not_tuple(c, o);
  8122. }
  8123. void check_expr_or_type(CheckerContext *c, Operand *o, Ast *e, Type *type_hint) {
  8124. check_expr_base(c, o, e, type_hint);
  8125. check_not_tuple(c, o);
  8126. error_operand_no_value(o);
  8127. }
  8128. gbString write_expr_to_string(gbString str, Ast *node);
  8129. gbString write_struct_fields_to_string(gbString str, Array<Ast *> const &params) {
  8130. for_array(i, params) {
  8131. if (i > 0) {
  8132. str = gb_string_appendc(str, ", ");
  8133. }
  8134. str = write_expr_to_string(str, params[i]);
  8135. }
  8136. return str;
  8137. }
  8138. gbString string_append_string(gbString str, String string) {
  8139. if (string.len > 0) {
  8140. return gb_string_append_length(str, &string[0], string.len);
  8141. }
  8142. return str;
  8143. }
  8144. gbString string_append_token(gbString str, Token token) {
  8145. return string_append_string(str, token.string);
  8146. }
  8147. gbString write_expr_to_string(gbString str, Ast *node) {
  8148. if (node == nullptr)
  8149. return str;
  8150. if (is_ast_stmt(node)) {
  8151. GB_ASSERT("stmt passed to write_expr_to_string");
  8152. }
  8153. switch (node->kind) {
  8154. default:
  8155. str = gb_string_appendc(str, "(BadExpr)");
  8156. break;
  8157. case_ast_node(i, Ident, node);
  8158. str = string_append_token(str, i->token);
  8159. case_end;
  8160. case_ast_node(i, Implicit, node);
  8161. str = string_append_token(str, *i);
  8162. case_end;
  8163. case_ast_node(bl, BasicLit, node);
  8164. str = string_append_token(str, bl->token);
  8165. case_end;
  8166. case_ast_node(bd, BasicDirective, node);
  8167. str = gb_string_append_rune(str, '#');
  8168. str = string_append_string(str, bd->name);
  8169. case_end;
  8170. case_ast_node(ud, Undef, node);
  8171. str = gb_string_appendc(str, "---");
  8172. case_end;
  8173. case_ast_node(pg, ProcGroup, node);
  8174. str = gb_string_appendc(str, "proc{");
  8175. for_array(i, pg->args) {
  8176. if (i > 0) str = gb_string_appendc(str, ", ");
  8177. str = write_expr_to_string(str, pg->args[i]);
  8178. }
  8179. str = gb_string_append_rune(str, '}');
  8180. case_end;
  8181. case_ast_node(pl, ProcLit, node);
  8182. str = write_expr_to_string(str, pl->type);
  8183. case_end;
  8184. case_ast_node(cl, CompoundLit, node);
  8185. str = write_expr_to_string(str, cl->type);
  8186. str = gb_string_append_rune(str, '{');
  8187. for_array(i, cl->elems) {
  8188. if (i > 0) str = gb_string_appendc(str, ", ");
  8189. str = write_expr_to_string(str, cl->elems[i]);
  8190. }
  8191. str = gb_string_append_rune(str, '}');
  8192. case_end;
  8193. case_ast_node(te, TagExpr, node);
  8194. str = gb_string_append_rune(str, '#');
  8195. str = string_append_token(str, te->name);
  8196. str = write_expr_to_string(str, te->expr);
  8197. case_end;
  8198. case_ast_node(ue, UnaryExpr, node);
  8199. str = string_append_token(str, ue->op);
  8200. str = write_expr_to_string(str, ue->expr);
  8201. case_end;
  8202. case_ast_node(de, DerefExpr, node);
  8203. str = write_expr_to_string(str, de->expr);
  8204. str = gb_string_append_rune(str, '^');
  8205. case_end;
  8206. case_ast_node(be, BinaryExpr, node);
  8207. str = write_expr_to_string(str, be->left);
  8208. str = gb_string_append_rune(str, ' ');
  8209. str = string_append_token(str, be->op);
  8210. str = gb_string_append_rune(str, ' ');
  8211. str = write_expr_to_string(str, be->right);
  8212. case_end;
  8213. case_ast_node(te, TernaryExpr, node);
  8214. str = write_expr_to_string(str, te->cond);
  8215. str = gb_string_appendc(str, " ? ");
  8216. str = write_expr_to_string(str, te->x);
  8217. str = gb_string_appendc(str, " : ");
  8218. str = write_expr_to_string(str, te->y);
  8219. case_end;
  8220. case_ast_node(te, TernaryIfExpr, node);
  8221. str = write_expr_to_string(str, te->x);
  8222. str = gb_string_appendc(str, " if ");
  8223. str = write_expr_to_string(str, te->cond);
  8224. str = gb_string_appendc(str, " else ");
  8225. str = write_expr_to_string(str, te->y);
  8226. case_end;
  8227. case_ast_node(te, TernaryWhenExpr, node);
  8228. str = write_expr_to_string(str, te->x);
  8229. str = gb_string_appendc(str, " when ");
  8230. str = write_expr_to_string(str, te->cond);
  8231. str = gb_string_appendc(str, " else ");
  8232. str = write_expr_to_string(str, te->y);
  8233. case_end;
  8234. case_ast_node(pe, ParenExpr, node);
  8235. str = gb_string_append_rune(str, '(');
  8236. str = write_expr_to_string(str, pe->expr);
  8237. str = gb_string_append_rune(str, ')');
  8238. case_end;
  8239. case_ast_node(se, SelectorExpr, node);
  8240. str = write_expr_to_string(str, se->expr);
  8241. str = gb_string_append_rune(str, '.');
  8242. str = write_expr_to_string(str, se->selector);
  8243. case_end;
  8244. case_ast_node(se, ImplicitSelectorExpr, node);
  8245. str = gb_string_append_rune(str, '.');
  8246. str = write_expr_to_string(str, se->selector);
  8247. case_end;
  8248. case_ast_node(ta, TypeAssertion, node);
  8249. str = write_expr_to_string(str, ta->expr);
  8250. str = gb_string_appendc(str, ".(");
  8251. str = write_expr_to_string(str, ta->type);
  8252. str = gb_string_append_rune(str, ')');
  8253. case_end;
  8254. case_ast_node(tc, TypeCast, node);
  8255. str = string_append_token(str, tc->token);
  8256. str = gb_string_append_rune(str, '(');
  8257. str = write_expr_to_string(str, tc->type);
  8258. str = gb_string_append_rune(str, ')');
  8259. str = write_expr_to_string(str, tc->expr);
  8260. case_end;
  8261. case_ast_node(ac, AutoCast, node);
  8262. str = string_append_token(str, ac->token);
  8263. str = gb_string_append_rune(str, ' ');
  8264. str = write_expr_to_string(str, ac->expr);
  8265. case_end;
  8266. case_ast_node(ie, IndexExpr, node);
  8267. str = write_expr_to_string(str, ie->expr);
  8268. str = gb_string_append_rune(str, '[');
  8269. str = write_expr_to_string(str, ie->index);
  8270. str = gb_string_append_rune(str, ']');
  8271. case_end;
  8272. case_ast_node(se, SliceExpr, node);
  8273. str = write_expr_to_string(str, se->expr);
  8274. str = gb_string_append_rune(str, '[');
  8275. str = write_expr_to_string(str, se->low);
  8276. str = string_append_token(str, se->interval);
  8277. str = write_expr_to_string(str, se->high);
  8278. str = gb_string_append_rune(str, ']');
  8279. case_end;
  8280. case_ast_node(e, Ellipsis, node);
  8281. str = gb_string_appendc(str, "..");
  8282. str = write_expr_to_string(str, e->expr);
  8283. case_end;
  8284. case_ast_node(fv, FieldValue, node);
  8285. str = write_expr_to_string(str, fv->field);
  8286. str = gb_string_appendc(str, " = ");
  8287. str = write_expr_to_string(str, fv->value);
  8288. case_end;
  8289. case_ast_node(ht, HelperType, node);
  8290. str = gb_string_appendc(str, "#type ");
  8291. str = write_expr_to_string(str, ht->type);
  8292. case_end;
  8293. case_ast_node(ht, DistinctType, node);
  8294. str = gb_string_appendc(str, "distinct ");
  8295. str = write_expr_to_string(str, ht->type);
  8296. case_end;
  8297. case_ast_node(ht, OpaqueType, node);
  8298. str = gb_string_appendc(str, "opaque ");
  8299. str = write_expr_to_string(str, ht->type);
  8300. case_end;
  8301. case_ast_node(pt, PolyType, node);
  8302. str = gb_string_append_rune(str, '$');
  8303. str = write_expr_to_string(str, pt->type);
  8304. if (pt->specialization != nullptr) {
  8305. str = gb_string_append_rune(str, '/');
  8306. str = write_expr_to_string(str, pt->specialization);
  8307. }
  8308. case_end;
  8309. case_ast_node(pt, PointerType, node);
  8310. str = gb_string_append_rune(str, '^');
  8311. str = write_expr_to_string(str, pt->type);
  8312. case_end;
  8313. case_ast_node(at, ArrayType, node);
  8314. str = gb_string_append_rune(str, '[');
  8315. if (at->count != nullptr &&
  8316. at->count->kind == Ast_UnaryExpr &&
  8317. at->count->UnaryExpr.op.kind == Token_Question) {
  8318. str = gb_string_appendc(str, "?");
  8319. } else {
  8320. str = write_expr_to_string(str, at->count);
  8321. }
  8322. str = gb_string_append_rune(str, ']');
  8323. str = write_expr_to_string(str, at->elem);
  8324. case_end;
  8325. case_ast_node(at, DynamicArrayType, node);
  8326. str = gb_string_appendc(str, "[dynamic]");
  8327. str = write_expr_to_string(str, at->elem);
  8328. case_end;
  8329. case_ast_node(bf, BitFieldType, node);
  8330. str = gb_string_appendc(str, "bit_field ");
  8331. if (bf->align) {
  8332. str = gb_string_appendc(str, "#align ");
  8333. str = write_expr_to_string(str, bf->align);
  8334. }
  8335. str = gb_string_appendc(str, "{");
  8336. str = write_struct_fields_to_string(str, bf->fields);
  8337. str = gb_string_appendc(str, "}");
  8338. case_end;
  8339. case_ast_node(bs, BitSetType, node);
  8340. str = gb_string_appendc(str, "bit_set[");
  8341. str = write_expr_to_string(str, bs->elem);
  8342. str = gb_string_appendc(str, "]");
  8343. case_end;
  8344. case_ast_node(mt, MapType, node);
  8345. str = gb_string_appendc(str, "map[");
  8346. str = write_expr_to_string(str, mt->key);
  8347. str = gb_string_append_rune(str, ']');
  8348. str = write_expr_to_string(str, mt->value);
  8349. case_end;
  8350. case_ast_node(f, Field, node);
  8351. if (f->flags&FieldFlag_using) {
  8352. str = gb_string_appendc(str, "using ");
  8353. }
  8354. if (f->flags&FieldFlag_no_alias) {
  8355. str = gb_string_appendc(str, "#no_alias ");
  8356. }
  8357. if (f->flags&FieldFlag_c_vararg) {
  8358. str = gb_string_appendc(str, "#c_vararg ");
  8359. }
  8360. if (f->flags&FieldFlag_auto_cast) {
  8361. str = gb_string_appendc(str, "auto_cast ");
  8362. }
  8363. for_array(i, f->names) {
  8364. Ast *name = f->names[i];
  8365. if (i > 0) str = gb_string_appendc(str, ", ");
  8366. str = write_expr_to_string(str, name);
  8367. }
  8368. if (f->names.count > 0) {
  8369. if (f->type == nullptr && f->default_value != nullptr) {
  8370. str = gb_string_append_rune(str, ' ');
  8371. }
  8372. str = gb_string_appendc(str, ":");
  8373. }
  8374. if (f->type != nullptr) {
  8375. str = gb_string_append_rune(str, ' ');
  8376. str = write_expr_to_string(str, f->type);
  8377. }
  8378. if (f->default_value != nullptr) {
  8379. if (f->type != nullptr) {
  8380. str = gb_string_append_rune(str, ' ');
  8381. }
  8382. str = gb_string_appendc(str, "= ");
  8383. str = write_expr_to_string(str, f->default_value);
  8384. }
  8385. case_end;
  8386. case_ast_node(f, FieldList, node);
  8387. bool has_name = false;
  8388. for_array(i, f->list) {
  8389. ast_node(field, Field, f->list[i]);
  8390. if (field->names.count > 1) {
  8391. has_name = true;
  8392. break;
  8393. }
  8394. if (field->names.count == 0) {
  8395. continue;
  8396. }
  8397. if (!is_blank_ident(field->names[0])) {
  8398. has_name = true;
  8399. break;
  8400. }
  8401. }
  8402. for_array(i, f->list) {
  8403. if (i > 0) str = gb_string_appendc(str, ", ");
  8404. if (has_name) {
  8405. str = write_expr_to_string(str, f->list[i]);
  8406. } else {
  8407. ast_node(field, Field, f->list[i]);
  8408. if (field->flags&FieldFlag_using) {
  8409. str = gb_string_appendc(str, "using ");
  8410. }
  8411. if (field->flags&FieldFlag_no_alias) {
  8412. str = gb_string_appendc(str, "#no_alias ");
  8413. }
  8414. if (field->flags&FieldFlag_c_vararg) {
  8415. str = gb_string_appendc(str, "#c_vararg ");
  8416. }
  8417. str = write_expr_to_string(str, field->type);
  8418. }
  8419. }
  8420. case_end;
  8421. case_ast_node(ce, CallExpr, node);
  8422. switch (ce->inlining) {
  8423. case ProcInlining_inline:
  8424. str = gb_string_appendc(str, "inline ");
  8425. break;
  8426. case ProcInlining_no_inline:
  8427. str = gb_string_appendc(str, "no_inline ");
  8428. break;
  8429. }
  8430. str = write_expr_to_string(str, ce->proc);
  8431. str = gb_string_appendc(str, "(");
  8432. for_array(i, ce->args) {
  8433. Ast *arg = ce->args[i];
  8434. if (i > 0) {
  8435. str = gb_string_appendc(str, ", ");
  8436. }
  8437. str = write_expr_to_string(str, arg);
  8438. }
  8439. str = gb_string_appendc(str, ")");
  8440. case_end;
  8441. case_ast_node(tt, TypeidType, node);
  8442. str = gb_string_appendc(str, "typeid");
  8443. if (tt->specialization) {
  8444. str = gb_string_appendc(str, "/");
  8445. str = write_expr_to_string(str, tt->specialization);
  8446. }
  8447. case_end;
  8448. case_ast_node(pt, ProcType, node);
  8449. str = gb_string_appendc(str, "proc(");
  8450. str = write_expr_to_string(str, pt->params);
  8451. str = gb_string_appendc(str, ")");
  8452. if (pt->results != nullptr) {
  8453. str = gb_string_appendc(str, " -> ");
  8454. str = write_expr_to_string(str, pt->results);
  8455. }
  8456. case_end;
  8457. case_ast_node(st, StructType, node);
  8458. str = gb_string_appendc(str, "struct ");
  8459. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  8460. if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
  8461. str = gb_string_append_rune(str, '{');
  8462. str = write_struct_fields_to_string(str, st->fields);
  8463. str = gb_string_append_rune(str, '}');
  8464. case_end;
  8465. case_ast_node(st, UnionType, node);
  8466. str = gb_string_appendc(str, "union ");
  8467. str = gb_string_append_rune(str, '{');
  8468. str = write_struct_fields_to_string(str, st->variants);
  8469. str = gb_string_append_rune(str, '}');
  8470. case_end;
  8471. case_ast_node(et, EnumType, node);
  8472. str = gb_string_appendc(str, "enum ");
  8473. if (et->base_type != nullptr) {
  8474. str = write_expr_to_string(str, et->base_type);
  8475. str = gb_string_append_rune(str, ' ');
  8476. }
  8477. str = gb_string_append_rune(str, '{');
  8478. for_array(i, et->fields) {
  8479. if (i > 0) {
  8480. str = gb_string_appendc(str, ", ");
  8481. }
  8482. str = write_expr_to_string(str, et->fields[i]);
  8483. }
  8484. str = gb_string_append_rune(str, '}');
  8485. case_end;
  8486. }
  8487. return str;
  8488. }
  8489. gbString expr_to_string(Ast *expression) {
  8490. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
  8491. }