check_expr.cpp 307 KB

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