check_expr.cpp 306 KB

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