check_expr.cpp 232 KB

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