check_expr.cpp 239 KB

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