check_expr.c 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667
  1. void check_expr (Checker *c, Operand *operand, AstNode *expression);
  2. void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
  3. void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression);
  4. ExprKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint);
  5. Type * check_type_extra (Checker *c, AstNode *expression, Type *named_type);
  6. Type * check_type (Checker *c, AstNode *expression);
  7. void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def);
  8. Entity * check_selector (Checker *c, Operand *operand, AstNode *node, Type *type_hint);
  9. void check_not_tuple (Checker *c, Operand *operand);
  10. void convert_to_typed (Checker *c, Operand *operand, Type *target_type, i32 level);
  11. gbString expr_to_string (AstNode *expression);
  12. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type);
  13. void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr, Type *named_type);
  14. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  15. void update_expr_type (Checker *c, AstNode *e, Type *type, bool final);
  16. bool check_is_terminating (AstNode *node);
  17. bool check_has_break (AstNode *stmt, bool implicit);
  18. void check_stmt (Checker *c, AstNode *node, u32 flags);
  19. void check_stmt_list (Checker *c, AstNodeArray stmts, u32 flags);
  20. void check_init_constant (Checker *c, Entity *e, Operand *operand);
  21. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
  22. Type * check_call_arguments (Checker *c, Operand *operand, Type *proc_type, AstNode *call);
  23. gb_inline Type *check_type(Checker *c, AstNode *expression) {
  24. return check_type_extra(c, expression, NULL);
  25. }
  26. void error_operand_not_expression(Operand *o) {
  27. if (o->mode == Addressing_Type) {
  28. gbString err = expr_to_string(o->expr);
  29. error_node(o->expr, "`%s` is not an expression", err);
  30. gb_string_free(err);
  31. o->mode = Addressing_Invalid;
  32. }
  33. }
  34. void error_operand_no_value(Operand *o) {
  35. if (o->mode == Addressing_NoValue) {
  36. gbString err = expr_to_string(o->expr);
  37. error_node(o->expr, "`%s` used as value", err);
  38. gb_string_free(err);
  39. o->mode = Addressing_Invalid;
  40. }
  41. }
  42. void check_scope_decls(Checker *c, AstNodeArray nodes, isize reserve_size) {
  43. Scope *s = c->context.scope;
  44. GB_ASSERT(!s->is_file);
  45. check_collect_entities(c, nodes, false);
  46. for_array(i, s->elements.entries) {
  47. Entity *e = s->elements.entries.e[i].value;
  48. switch (e->kind) {
  49. case Entity_Constant:
  50. case Entity_TypeName:
  51. case Entity_Procedure:
  52. break;
  53. default:
  54. continue;
  55. }
  56. DeclInfo **found = map_decl_info_get(&c->info.entities, hash_pointer(e));
  57. if (found != NULL) {
  58. DeclInfo *d = *found;
  59. check_entity_decl(c, e, d, NULL);
  60. }
  61. }
  62. for_array(i, s->elements.entries) {
  63. Entity *e = s->elements.entries.e[i].value;
  64. if (e->kind != Entity_Procedure) {
  65. continue;
  66. }
  67. check_procedure_overloading(c, e);
  68. }
  69. }
  70. bool check_is_assignable_to_using_subtype(Type *dst, Type *src) {
  71. bool src_is_ptr;
  72. Type *prev_src = src;
  73. src = type_deref(src);
  74. src_is_ptr = src != prev_src;
  75. src = base_type(src);
  76. if (is_type_struct(src)) {
  77. for (isize i = 0; i < src->Record.field_count; i++) {
  78. Entity *f = src->Record.fields[i];
  79. if (f->kind == Entity_Variable && (f->flags & EntityFlag_Anonymous)) {
  80. if (are_types_identical(dst, f->type)) {
  81. return true;
  82. }
  83. if (src_is_ptr && is_type_pointer(dst)) {
  84. if (are_types_identical(type_deref(dst), f->type)) {
  85. return true;
  86. }
  87. }
  88. bool ok = check_is_assignable_to_using_subtype(dst, f->type);
  89. if (ok) {
  90. return true;
  91. }
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. // IMPORTANT TODO(bill): figure out the exact distance rules
  98. // -1 is not convertable
  99. // 0 is exact
  100. // >0 is convertable
  101. i64 check_distance_between_types(Checker *c, Operand *operand, Type *type) {
  102. if (operand->mode == Addressing_Invalid ||
  103. type == t_invalid) {
  104. return 0;
  105. }
  106. if (operand->mode == Addressing_Builtin) {
  107. return -1;
  108. }
  109. Type *s = operand->type;
  110. if (are_types_identical(s, type)) {
  111. return 0;
  112. }
  113. Type *src = base_type(s);
  114. Type *dst = base_type(type);
  115. if (is_type_untyped_nil(src)) {
  116. if (type_has_nil(dst)) {
  117. return 1;
  118. }
  119. return -1;
  120. }
  121. if (is_type_untyped(src)) {
  122. if (is_type_any(dst)) {
  123. // NOTE(bill): Anything can cast to `Any`
  124. add_type_info_type(c, s);
  125. return 10;
  126. }
  127. if (dst->kind == Type_Basic) {
  128. if (operand->mode == Addressing_Constant) {
  129. if (check_representable_as_constant(c, operand->value, dst, NULL)) {
  130. return 1;
  131. }
  132. return -1;
  133. }
  134. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  135. if (is_type_boolean(dst)) {
  136. if (is_type_typed(type)) {
  137. return 2;
  138. }
  139. return 1;
  140. }
  141. return -1;
  142. }
  143. }
  144. }
  145. if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
  146. return 1;
  147. }
  148. if (check_is_assignable_to_using_subtype(operand->type, type)) {
  149. return 4;
  150. }
  151. // ^T <- rawptr
  152. #if 0
  153. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  154. if (is_type_pointer(dst) && is_type_rawptr(src)) {
  155. return true;
  156. }
  157. #endif
  158. #if 1
  159. // TODO(bill): Should I allow this implicit conversion at all?!
  160. // rawptr <- ^T
  161. if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
  162. return 5;
  163. }
  164. #endif
  165. if (is_type_union(dst)) {
  166. for (isize i = 0; i < dst->Record.variant_count; i++) {
  167. Entity *f = dst->Record.variants[i];
  168. if (are_types_identical(f->type, s)) {
  169. return 1;
  170. }
  171. }
  172. }
  173. if (is_type_proc(dst)) {
  174. if (are_types_identical(src, dst)) {
  175. return 3;
  176. }
  177. }
  178. if (is_type_any(dst)) {
  179. // NOTE(bill): Anything can cast to `Any`
  180. add_type_info_type(c, s);
  181. return 10;
  182. }
  183. return -1;
  184. }
  185. bool check_is_assignable_to_with_score(Checker *c, Operand *operand, Type *type, i64 *score_) {
  186. i64 score = 0;
  187. i64 distance = check_distance_between_types(c, operand, type);
  188. bool ok = distance >= 0;
  189. if (ok) {
  190. // TODO(bill): A decent score function
  191. score = gb_max(1000000 - distance*distance, 0);
  192. }
  193. if (score_) *score_ = score;
  194. return ok;
  195. }
  196. bool check_is_assignable_to(Checker *c, Operand *operand, Type *type) {
  197. i64 score = 0;
  198. return check_is_assignable_to_with_score(c, operand, type, &score);
  199. }
  200. // NOTE(bill): `content_name` is for debugging and error messages
  201. void check_assignment(Checker *c, Operand *operand, Type *type, String context_name) {
  202. check_not_tuple(c, operand);
  203. if (operand->mode == Addressing_Invalid) {
  204. return;
  205. }
  206. if (is_type_untyped(operand->type)) {
  207. Type *target_type = type;
  208. if (type == NULL || is_type_any(type)) {
  209. if (type == NULL && is_type_untyped_nil(operand->type)) {
  210. error_node(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
  211. operand->mode = Addressing_Invalid;
  212. return;
  213. }
  214. target_type = default_type(operand->type);
  215. GB_ASSERT(is_type_typed(target_type));
  216. add_type_info_type(c, type);
  217. add_type_info_type(c, target_type);
  218. }
  219. convert_to_typed(c, operand, target_type, 0);
  220. if (operand->mode == Addressing_Invalid) {
  221. return;
  222. }
  223. }
  224. if (type == NULL) {
  225. return;
  226. }
  227. if (!check_is_assignable_to(c, operand, type)) {
  228. gbString type_str = type_to_string(type);
  229. gbString op_type_str = type_to_string(operand->type);
  230. gbString expr_str = expr_to_string(operand->expr);
  231. if (operand->mode == Addressing_Builtin) {
  232. // TODO(bill): is this a good enough error message?
  233. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  234. error_node(operand->expr,
  235. "Cannot assign builtin procedure `%s` in %.*s",
  236. expr_str,
  237. LIT(context_name));
  238. } else {
  239. // TODO(bill): is this a good enough error message?
  240. error_node(operand->expr,
  241. "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
  242. expr_str,
  243. op_type_str,
  244. type_str,
  245. LIT(context_name));
  246. }
  247. operand->mode = Addressing_Invalid;
  248. gb_string_free(expr_str);
  249. gb_string_free(op_type_str);
  250. gb_string_free(type_str);
  251. return;
  252. }
  253. }
  254. void populate_using_entity_map(Checker *c, AstNode *node, Type *t, MapEntity *entity_map) {
  255. t = base_type(type_deref(t));
  256. gbString str = NULL;
  257. if (node != NULL) {
  258. expr_to_string(node);
  259. }
  260. if (t->kind == Type_Record) {
  261. for (isize i = 0; i < t->Record.field_count; i++) {
  262. Entity *f = t->Record.fields[i];
  263. GB_ASSERT(f->kind == Entity_Variable);
  264. String name = f->token.string;
  265. HashKey key = hash_string(name);
  266. Entity **found = map_entity_get(entity_map, key);
  267. if (found != NULL) {
  268. Entity *e = *found;
  269. // TODO(bill): Better type error
  270. if (str != NULL) {
  271. error(e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
  272. } else {
  273. error(e->token, "`%.*s` is already declared`", LIT(name));
  274. }
  275. } else {
  276. map_entity_set(entity_map, key, f);
  277. add_entity(c, c->context.scope, NULL, f);
  278. if (f->flags & EntityFlag_Anonymous) {
  279. populate_using_entity_map(c, node, f->type, entity_map);
  280. }
  281. }
  282. }
  283. }
  284. gb_string_free(str);
  285. }
  286. // Returns filled field_count
  287. isize check_fields(Checker *c, AstNode *node, AstNodeArray decls,
  288. Entity **fields, isize field_count,
  289. String context) {
  290. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  291. MapEntity entity_map = {0};
  292. map_entity_init_with_reserve(&entity_map, c->tmp_allocator, 2*field_count);
  293. Entity *using_index_expr = NULL;
  294. if (node != NULL) {
  295. GB_ASSERT(node->kind != AstNode_UnionType);
  296. }
  297. isize field_index = 0;
  298. for_array(decl_index, decls) {
  299. AstNode *decl = decls.e[decl_index];
  300. if (decl->kind != AstNode_Field) {
  301. continue;
  302. }
  303. ast_node(f, Field, decl);
  304. Type *type = check_type(c, f->type);
  305. if (f->flags&FieldFlag_using) {
  306. if (f->names.count > 1) {
  307. error_node(f->names.e[0], "Cannot apply `using` to more than one of the same type");
  308. }
  309. }
  310. for_array(name_index, f->names) {
  311. AstNode *name = f->names.e[name_index];
  312. if (!ast_node_expect(name, AstNode_Ident)) {
  313. continue;
  314. }
  315. Token name_token = name->Ident;
  316. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, f->flags&FieldFlag_using, cast(i32)field_index);
  317. e->identifier = name;
  318. if (str_eq(name_token.string, str_lit("_"))) {
  319. fields[field_index++] = e;
  320. } else {
  321. HashKey key = hash_string(name_token.string);
  322. Entity **found = map_entity_get(&entity_map, key);
  323. if (found != NULL) {
  324. Entity *e = *found;
  325. // NOTE(bill): Scope checking already checks the declaration but in many cases, this can happen so why not?
  326. // This may be a little janky but it's not really that much of a problem
  327. error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
  328. error(e->token, "\tpreviously declared");
  329. } else {
  330. map_entity_set(&entity_map, key, e);
  331. fields[field_index++] = e;
  332. add_entity(c, c->context.scope, name, e);
  333. }
  334. add_entity_use(c, name, e);
  335. }
  336. }
  337. if (f->flags&FieldFlag_using) {
  338. Type *t = base_type(type_deref(type));
  339. if (!is_type_struct(t) && !is_type_raw_union(t) &&
  340. f->names.count >= 1 &&
  341. f->names.e[0]->kind == AstNode_Ident) {
  342. Token name_token = f->names.e[0]->Ident;
  343. if (is_type_indexable(t)) {
  344. bool ok = true;
  345. for_array(emi, entity_map.entries) {
  346. Entity *e = entity_map.entries.e[emi].value;
  347. if (e->kind == Entity_Variable && e->flags & EntityFlag_Anonymous) {
  348. if (is_type_indexable(e->type)) {
  349. if (e->identifier != f->names.e[0]) {
  350. ok = false;
  351. using_index_expr = e;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. if (ok) {
  358. using_index_expr = fields[field_index-1];
  359. } else {
  360. fields[field_index-1]->flags &= ~EntityFlag_Anonymous;
  361. error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
  362. }
  363. } else {
  364. error(name_token, "`using` on a field `%.*s` must be a `struct` or `raw_union`", LIT(name_token.string));
  365. continue;
  366. }
  367. }
  368. populate_using_entity_map(c, node, type, &entity_map);
  369. }
  370. }
  371. gb_temp_arena_memory_end(tmp);
  372. return field_index;
  373. }
  374. // TODO(bill): Cleanup struct field reordering
  375. // TODO(bill): Inline sorting procedure?
  376. gb_global gbAllocator __checker_allocator = {0};
  377. GB_COMPARE_PROC(cmp_struct_entity_size) {
  378. // Rule:
  379. // Biggest to smallest alignment
  380. // if same alignment: biggest to smallest size
  381. // if same size: order by source order
  382. Entity *x = *(Entity **)a;
  383. Entity *y = *(Entity **)b;
  384. GB_ASSERT(x != NULL);
  385. GB_ASSERT(y != NULL);
  386. GB_ASSERT(x->kind == Entity_Variable);
  387. GB_ASSERT(y->kind == Entity_Variable);
  388. i64 xa = type_align_of(__checker_allocator, x->type);
  389. i64 ya = type_align_of(__checker_allocator, y->type);
  390. i64 xs = type_size_of(__checker_allocator, x->type);
  391. i64 ys = type_size_of(__checker_allocator, y->type);
  392. if (xa == ya) {
  393. if (xs == ys) {
  394. i32 diff = x->Variable.field_index - y->Variable.field_index;
  395. return diff < 0 ? -1 : diff > 0;
  396. }
  397. return xs > ys ? -1 : xs < ys;
  398. }
  399. return xa > ya ? -1 : xa < ya;
  400. }
  401. Entity *make_names_field_for_record(Checker *c, Scope *scope) {
  402. Entity *e = make_entity_field(c->allocator, scope,
  403. make_token_ident(str_lit("names")), t_string_slice, false, 0);
  404. e->Variable.is_immutable = true;
  405. e->flags |= EntityFlag_TypeField;
  406. return e;
  407. }
  408. void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
  409. GB_ASSERT(is_type_struct(struct_type));
  410. ast_node(st, StructType, node);
  411. isize field_count = 0;
  412. for_array(field_index, st->fields) {
  413. AstNode *field = st->fields.e[field_index];
  414. switch (field->kind) {
  415. case_ast_node(f, Field, field);
  416. field_count += f->names.count;
  417. case_end;
  418. }
  419. }
  420. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  421. field_count = check_fields(c, node, st->fields, fields, field_count, str_lit("struct"));
  422. struct_type->Record.is_packed = st->is_packed;
  423. struct_type->Record.is_ordered = st->is_ordered;
  424. struct_type->Record.fields = fields;
  425. struct_type->Record.fields_in_src_order = fields;
  426. struct_type->Record.field_count = field_count;
  427. struct_type->Record.names = make_names_field_for_record(c, c->context.scope);
  428. if (!st->is_packed && !st->is_ordered) {
  429. // NOTE(bill): Reorder fields for reduced size/performance
  430. Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, field_count);
  431. for (isize i = 0; i < field_count; i++) {
  432. reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
  433. }
  434. // NOTE(bill): Hacky thing
  435. // TODO(bill): Probably make an inline sorting procedure rather than use global variables
  436. __checker_allocator = c->allocator;
  437. // NOTE(bill): compound literal order must match source not layout
  438. gb_sort_array(reordered_fields, field_count, cmp_struct_entity_size);
  439. for (isize i = 0; i < field_count; i++) {
  440. reordered_fields[i]->Variable.field_index = i;
  441. }
  442. struct_type->Record.fields = reordered_fields;
  443. }
  444. type_set_offsets(c->allocator, struct_type);
  445. if (st->align != NULL) {
  446. if (st->is_packed) {
  447. syntax_error_node(st->align, "`#align` cannot be applied with `#packed`");
  448. return;
  449. }
  450. Operand o = {0};
  451. check_expr(c, &o, st->align);
  452. if (o.mode != Addressing_Constant) {
  453. if (o.mode != Addressing_Invalid) {
  454. error_node(st->align, "#align must be a constant");
  455. }
  456. return;
  457. }
  458. Type *type = base_type(o.type);
  459. if (is_type_untyped(type) || is_type_integer(type)) {
  460. if (o.value.kind == ExactValue_Integer) {
  461. i64 align = o.value.value_integer;
  462. if (align < 1 || !gb_is_power_of_two(align)) {
  463. error_node(st->align, "#align must be a power of 2, got %lld", align);
  464. return;
  465. }
  466. // NOTE(bill): Success!!!
  467. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  468. if (custom_align < align) {
  469. warning_node(st->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  470. }
  471. struct_type->Record.custom_align = custom_align;
  472. return;
  473. }
  474. }
  475. error_node(st->align, "#align must be an integer");
  476. return;
  477. }
  478. }
  479. void check_union_type(Checker *c, Type *union_type, AstNode *node) {
  480. GB_ASSERT(is_type_union(union_type));
  481. ast_node(ut, UnionType, node);
  482. isize variant_count = ut->variants.count+1;
  483. isize field_count = 0;
  484. for_array(i, ut->fields) {
  485. AstNode *field = ut->fields.e[i];
  486. if (field->kind == AstNode_Field) {
  487. ast_node(f, Field, field);
  488. field_count += f->names.count;
  489. }
  490. }
  491. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  492. MapEntity entity_map = {0};
  493. map_entity_init_with_reserve(&entity_map, c->tmp_allocator, 2*variant_count);
  494. Entity *using_index_expr = NULL;
  495. Entity **variants = gb_alloc_array(c->allocator, Entity *, variant_count);
  496. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  497. isize variant_index = 0;
  498. variants[variant_index++] = make_entity_type_name(c->allocator, c->context.scope, empty_token, NULL);
  499. field_count = check_fields(c, NULL, ut->fields, fields, field_count, str_lit("union"));
  500. union_type->Record.fields = fields;
  501. union_type->Record.field_count = field_count;
  502. for_array(i, ut->variants) {
  503. AstNode *variant = ut->variants.e[i];
  504. if (variant->kind != AstNode_UnionField) {
  505. continue;
  506. }
  507. ast_node(f, UnionField, variant);
  508. Token name_token = f->name->Ident;
  509. Type *base_type = make_type_struct(c->allocator);
  510. {
  511. ast_node(fl, FieldList, f->list);
  512. // NOTE(bill): Copy the contents for the common fields for now
  513. AstNodeArray list = {0};
  514. array_init_count(&list, c->allocator, ut->fields.count+fl->list.count);
  515. gb_memmove_array(list.e, ut->fields.e, ut->fields.count);
  516. gb_memmove_array(list.e+ut->fields.count, fl->list.e, fl->list.count);
  517. isize list_count = 0;
  518. for_array(j, list) {
  519. ast_node(f, Field, list.e[j]);
  520. list_count += f->names.count;
  521. }
  522. Token token = name_token;
  523. token.kind = Token_struct;
  524. AstNode *dummy_struct = ast_struct_type(c->curr_ast_file, token, list, list_count, false, true, NULL);
  525. check_open_scope(c, dummy_struct);
  526. Entity **fields = gb_alloc_array(c->allocator, Entity *, list_count);
  527. isize field_count = check_fields(c, dummy_struct, list, fields, list_count, str_lit("variant"));
  528. base_type->Record.is_packed = false;
  529. base_type->Record.is_ordered = true;
  530. base_type->Record.fields = fields;
  531. base_type->Record.fields_in_src_order = fields;
  532. base_type->Record.field_count = field_count;
  533. base_type->Record.names = make_names_field_for_record(c, c->context.scope);
  534. base_type->Record.node = dummy_struct;
  535. type_set_offsets(c->allocator, base_type);
  536. check_close_scope(c);
  537. }
  538. Type *type = make_type_named(c->allocator, name_token.string, base_type, NULL);
  539. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, type);
  540. type->Named.type_name = e;
  541. add_entity(c, c->context.scope, f->name, e);
  542. if (str_eq(name_token.string, str_lit("_"))) {
  543. error(name_token, "`_` cannot be used a union subtype");
  544. continue;
  545. }
  546. HashKey key = hash_string(name_token.string);
  547. if (map_entity_get(&entity_map, key) != NULL) {
  548. // NOTE(bill): Scope checking already checks the declaration
  549. error(name_token, "`%.*s` is already declared in this union", LIT(name_token.string));
  550. } else {
  551. map_entity_set(&entity_map, key, e);
  552. variants[variant_index++] = e;
  553. }
  554. add_entity_use(c, f->name, e);
  555. }
  556. gb_temp_arena_memory_end(tmp);
  557. union_type->Record.variants = variants;
  558. union_type->Record.variant_count = variant_index;
  559. }
  560. void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) {
  561. GB_ASSERT(node->kind == AstNode_RawUnionType);
  562. GB_ASSERT(is_type_raw_union(union_type));
  563. ast_node(ut, RawUnionType, node);
  564. isize field_count = 0;
  565. for_array(field_index, ut->fields) {
  566. AstNode *field = ut->fields.e[field_index];
  567. switch (field->kind) {
  568. case_ast_node(f, Field, field);
  569. field_count += f->names.count;
  570. case_end;
  571. }
  572. }
  573. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  574. field_count = check_fields(c, node, ut->fields, fields, field_count, str_lit("raw_union"));
  575. union_type->Record.fields = fields;
  576. union_type->Record.field_count = field_count;
  577. union_type->Record.names = make_names_field_for_record(c, c->context.scope);
  578. }
  579. // GB_COMPARE_PROC(cmp_enum_order) {
  580. // // Rule:
  581. // // Biggest to smallest alignment
  582. // // if same alignment: biggest to smallest size
  583. // // if same size: order by source order
  584. // Entity *x = *(Entity **)a;
  585. // Entity *y = *(Entity **)b;
  586. // GB_ASSERT(x != NULL);
  587. // GB_ASSERT(y != NULL);
  588. // GB_ASSERT(x->kind == Entity_Constant);
  589. // GB_ASSERT(y->kind == Entity_Constant);
  590. // GB_ASSERT(x->Constant.value.kind == ExactValue_Integer);
  591. // GB_ASSERT(y->Constant.value.kind == ExactValue_Integer);
  592. // i64 i = x->Constant.value.value_integer;
  593. // i64 j = y->Constant.value.value_integer;
  594. // return i < j ? -1 : i > j;
  595. // }
  596. void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *node) {
  597. ast_node(et, EnumType, node);
  598. GB_ASSERT(is_type_enum(enum_type));
  599. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  600. Type *base_type = t_int;
  601. if (et->base_type != NULL) {
  602. base_type = check_type(c, et->base_type);
  603. }
  604. if (base_type == NULL || !(is_type_integer(base_type) || is_type_float(base_type))) {
  605. error_node(node, "Base type for enumeration must be numeric");
  606. return;
  607. }
  608. if (is_type_enum(base_type)) {
  609. error_node(node, "Base type for enumeration cannot be another enumeration");
  610. return;
  611. }
  612. // NOTE(bill): Must be up here for the `check_init_constant` system
  613. enum_type->Record.enum_base_type = base_type;
  614. MapEntity entity_map = {0}; // Key: String
  615. map_entity_init_with_reserve(&entity_map, c->tmp_allocator, 2*(et->fields.count));
  616. Entity **fields = gb_alloc_array(c->allocator, Entity *, et->fields.count);
  617. isize field_count = 0;
  618. Type *constant_type = enum_type;
  619. if (named_type != NULL) {
  620. constant_type = named_type;
  621. }
  622. ExactValue iota = exact_value_integer(-1);
  623. ExactValue min_value = exact_value_integer(0);
  624. ExactValue max_value = exact_value_integer(0);
  625. for_array(i, et->fields) {
  626. AstNode *field = et->fields.e[i];
  627. AstNode *ident = NULL;
  628. AstNode *init = NULL;
  629. if (field->kind == AstNode_FieldValue) {
  630. ast_node(fv, FieldValue, field);
  631. if (fv->field == NULL || fv->field->kind != AstNode_Ident) {
  632. error_node(field, "An enum field's name must be an identifier");
  633. continue;
  634. }
  635. ident = fv->field;
  636. init = fv->value;
  637. } else if (field->kind == AstNode_Ident) {
  638. ident = field;
  639. } else {
  640. error_node(field, "An enum field's name must be an identifier");
  641. continue;
  642. }
  643. String name = ident->Ident.string;
  644. if (init != NULL) {
  645. Operand o = {0};
  646. check_expr(c, &o, init);
  647. if (o.mode != Addressing_Constant) {
  648. error_node(init, "Enumeration value must be a constant");
  649. o.mode = Addressing_Invalid;
  650. }
  651. if (o.mode != Addressing_Invalid) {
  652. check_assignment(c, &o, constant_type, str_lit("enumeration"));
  653. }
  654. if (o.mode != Addressing_Invalid) {
  655. iota = o.value;
  656. } else {
  657. iota = exact_binary_operator_value(Token_Add, iota, exact_value_integer(1));
  658. }
  659. } else {
  660. iota = exact_binary_operator_value(Token_Add, iota, exact_value_integer(1));
  661. }
  662. // NOTE(bill): Skip blank identifiers
  663. if (str_eq(name, str_lit("_"))) {
  664. continue;
  665. } else if (str_eq(name, str_lit("count"))) {
  666. error_node(field, "`count` is a reserved identifier for enumerations");
  667. continue;
  668. } else if (str_eq(name, str_lit("min_value"))) {
  669. error_node(field, "`min_value` is a reserved identifier for enumerations");
  670. continue;
  671. } else if (str_eq(name, str_lit("max_value"))) {
  672. error_node(field, "`max_value` is a reserved identifier for enumerations");
  673. continue;
  674. } else if (str_eq(name, str_lit("names"))) {
  675. error_node(field, "`names` is a reserved identifier for enumerations");
  676. continue;
  677. }/* else if (str_eq(name, str_lit("base_type"))) {
  678. error_node(field, "`base_type` is a reserved identifier for enumerations");
  679. continue;
  680. } */
  681. if (compare_exact_values(Token_Gt, min_value, iota)) {
  682. min_value = iota;
  683. }
  684. if (compare_exact_values(Token_Lt, max_value, iota)) {
  685. max_value = iota;
  686. }
  687. Entity *e = make_entity_constant(c->allocator, c->context.scope, ident->Ident, constant_type, iota);
  688. e->identifier = ident;
  689. e->flags |= EntityFlag_Visited;
  690. HashKey key = hash_string(name);
  691. if (map_entity_get(&entity_map, key) != NULL) {
  692. error_node(ident, "`%.*s` is already declared in this enumeration", LIT(name));
  693. } else {
  694. map_entity_set(&entity_map, key, e);
  695. add_entity(c, c->context.scope, NULL, e);
  696. fields[field_count++] = e;
  697. add_entity_use(c, field, e);
  698. }
  699. }
  700. GB_ASSERT(field_count <= et->fields.count);
  701. gb_temp_arena_memory_end(tmp);
  702. enum_type->Record.fields = fields;
  703. enum_type->Record.field_count = field_count;
  704. enum_type->Record.enum_count = make_entity_constant(c->allocator, c->context.scope,
  705. make_token_ident(str_lit("count")), t_int, exact_value_integer(field_count));
  706. enum_type->Record.enum_min_value = make_entity_constant(c->allocator, c->context.scope,
  707. make_token_ident(str_lit("min_value")), constant_type, min_value);
  708. enum_type->Record.enum_max_value = make_entity_constant(c->allocator, c->context.scope,
  709. make_token_ident(str_lit("max_value")), constant_type, max_value);
  710. enum_type->Record.names = make_names_field_for_record(c, c->context.scope);
  711. }
  712. Type *check_get_params(Checker *c, Scope *scope, AstNode *_params, bool *is_variadic_) {
  713. if (_params == NULL) {
  714. return NULL;
  715. }
  716. ast_node(field_list, FieldList, _params);
  717. AstNodeArray params = field_list->list;
  718. if (params.count == 0) {
  719. return NULL;
  720. }
  721. isize variable_count = 0;
  722. for_array(i, params) {
  723. AstNode *field = params.e[i];
  724. if (ast_node_expect(field, AstNode_Field)) {
  725. ast_node(f, Field, field);
  726. variable_count += gb_max(f->names.count, 1);
  727. }
  728. }
  729. bool is_variadic = false;
  730. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  731. isize variable_index = 0;
  732. for_array(i, params) {
  733. if (params.e[i]->kind != AstNode_Field) {
  734. continue;
  735. }
  736. ast_node(p, Field, params.e[i]);
  737. AstNode *type_expr = p->type;
  738. if (type_expr) {
  739. if (type_expr->kind == AstNode_Ellipsis) {
  740. type_expr = type_expr->Ellipsis.expr;
  741. if (i+1 == params.count) {
  742. is_variadic = true;
  743. } else {
  744. error_node(params.e[i], "Invalid AST: Invalid variadic parameter");
  745. }
  746. }
  747. Type *type = check_type(c, type_expr);
  748. if (p->flags&FieldFlag_no_alias) {
  749. if (!is_type_pointer(type)) {
  750. error_node(params.e[i], "`no_alias` can only be applied to fields of pointer type");
  751. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  752. }
  753. }
  754. for_array(j, p->names) {
  755. AstNode *name = p->names.e[j];
  756. if (ast_node_expect(name, AstNode_Ident)) {
  757. Entity *param = make_entity_param(c->allocator, scope, name->Ident, type,
  758. p->flags&FieldFlag_using, p->flags&FieldFlag_immutable);
  759. if (p->flags&FieldFlag_no_alias) {
  760. param->flags |= EntityFlag_NoAlias;
  761. }
  762. if (p->flags&FieldFlag_immutable) {
  763. param->Variable.is_immutable = true;
  764. }
  765. add_entity(c, scope, name, param);
  766. variables[variable_index++] = param;
  767. }
  768. }
  769. }
  770. }
  771. variable_count = variable_index;
  772. if (is_variadic) {
  773. GB_ASSERT(params.count > 0);
  774. // NOTE(bill): Change last variadic parameter to be a slice
  775. // Custom Calling convention for variadic parameters
  776. Entity *end = variables[variable_count-1];
  777. end->type = make_type_slice(c->allocator, end->type);
  778. end->flags |= EntityFlag_Ellipsis;
  779. }
  780. Type *tuple = make_type_tuple(c->allocator);
  781. tuple->Tuple.variables = variables;
  782. tuple->Tuple.variable_count = variable_count;
  783. if (is_variadic_) *is_variadic_ = is_variadic;
  784. return tuple;
  785. }
  786. Type *check_get_results(Checker *c, Scope *scope, AstNode *_results) {
  787. if (_results == NULL) {
  788. return NULL;
  789. }
  790. ast_node(field_list, FieldList, _results);
  791. AstNodeArray results = field_list->list;
  792. if (results.count == 0) {
  793. return NULL;
  794. }
  795. Type *tuple = make_type_tuple(c->allocator);
  796. isize variable_count = 0;
  797. for_array(i, results) {
  798. AstNode *field = results.e[i];
  799. if (ast_node_expect(field, AstNode_Field)) {
  800. ast_node(f, Field, field);
  801. variable_count += gb_max(f->names.count, 1);
  802. }
  803. }
  804. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  805. isize variable_index = 0;
  806. for_array(i, results) {
  807. ast_node(field, Field, results.e[i]);
  808. Type *type = check_type(c, field->type);
  809. if (field->names.count == 0) {
  810. Token token = ast_node_token(field->type);
  811. token.string = str_lit("");
  812. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  813. variables[variable_index++] = param;
  814. } else {
  815. for_array(j, field->names) {
  816. Token token = ast_node_token(field->type);
  817. token.string = str_lit("");
  818. AstNode *name = field->names.e[j];
  819. if (name->kind != AstNode_Ident) {
  820. error_node(name, "Expected an identifer for as the field name");
  821. } else {
  822. token = name->Ident;
  823. }
  824. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  825. variables[variable_index++] = param;
  826. }
  827. }
  828. }
  829. for (isize i = 0; i < variable_index; i++) {
  830. String x = variables[i]->token.string;
  831. if (x.len == 0 || str_eq(x, str_lit("_"))) {
  832. continue;
  833. }
  834. for (isize j = i+1; j < variable_index; j++) {
  835. String y = variables[j]->token.string;
  836. if (y.len == 0 || str_eq(y, str_lit("_"))) {
  837. continue;
  838. }
  839. if (str_eq(x, y)) {
  840. error(variables[j]->token, "Duplicate return value name `%.*s`", LIT(y));
  841. }
  842. }
  843. }
  844. tuple->Tuple.variables = variables;
  845. tuple->Tuple.variable_count = variable_index;
  846. return tuple;
  847. }
  848. void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
  849. ast_node(pt, ProcType, proc_type_node);
  850. bool variadic = false;
  851. Type *params = check_get_params(c, c->context.scope, pt->params, &variadic);
  852. Type *results = check_get_results(c, c->context.scope, pt->results);
  853. isize param_count = 0;
  854. isize result_count = 0;
  855. if (params) param_count = params ->Tuple.variable_count;
  856. if (results) result_count = results->Tuple.variable_count;
  857. type->Proc.scope = c->context.scope;
  858. type->Proc.params = params;
  859. type->Proc.param_count = param_count;
  860. type->Proc.results = results;
  861. type->Proc.result_count = result_count;
  862. type->Proc.variadic = variadic;
  863. type->Proc.calling_convention = pt->calling_convention;
  864. }
  865. Entity *check_ident(Checker *c, Operand *o, AstNode *n, Type *named_type, Type *type_hint, bool allow_import_name) {
  866. GB_ASSERT(n->kind == AstNode_Ident);
  867. o->mode = Addressing_Invalid;
  868. o->expr = n;
  869. String name = n->Ident.string;
  870. Entity *e = scope_lookup_entity(c->context.scope, name);
  871. if (e == NULL) {
  872. if (str_eq(name, str_lit("_"))) {
  873. error(n->Ident, "`_` cannot be used as a value type");
  874. } else {
  875. error(n->Ident, "Undeclared name: %.*s", LIT(name));
  876. }
  877. o->type = t_invalid;
  878. o->mode = Addressing_Invalid;
  879. if (named_type != NULL) {
  880. set_base_type(named_type, t_invalid);
  881. }
  882. return NULL;
  883. }
  884. bool is_overloaded = false;
  885. isize overload_count = 0;
  886. HashKey key = hash_string(name);
  887. if (e->kind == Entity_Procedure) {
  888. // NOTE(bill): Overloads are only allowed with the same scope
  889. Scope *s = e->scope;
  890. overload_count = map_entity_multi_count(&s->elements, key);
  891. if (overload_count > 1) {
  892. is_overloaded = true;
  893. }
  894. }
  895. if (is_overloaded) {
  896. Scope *s = e->scope;
  897. bool skip = false;
  898. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  899. map_entity_multi_get_all(&s->elements, key, procs);
  900. if (type_hint != NULL) {
  901. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  902. // NOTE(bill): These should be done
  903. for (isize i = 0; i < overload_count; i++) {
  904. Type *t = base_type(procs[i]->type);
  905. if (t == t_invalid) {
  906. continue;
  907. }
  908. Operand x = {0};
  909. x.mode = Addressing_Value;
  910. x.type = t;
  911. if (check_is_assignable_to(c, &x, type_hint)) {
  912. e = procs[i];
  913. add_entity_use(c, n, e);
  914. skip = true;
  915. break;
  916. }
  917. }
  918. gb_temp_arena_memory_end(tmp);
  919. }
  920. if (!skip) {
  921. o->mode = Addressing_Overload;
  922. o->type = t_invalid;
  923. o->overload_count = overload_count;
  924. o->overload_entities = procs;
  925. return NULL;
  926. }
  927. gb_free(heap_allocator(), procs);
  928. }
  929. add_entity_use(c, n, e);
  930. check_entity_decl(c, e, NULL, named_type);
  931. if (e->type == NULL) {
  932. compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
  933. // return NULL;
  934. }
  935. e->flags |= EntityFlag_Used;
  936. Entity *original_e = e;
  937. while (e != NULL && e->kind == Entity_Alias && e->Alias.original != NULL) {
  938. e = e->Alias.original;
  939. }
  940. Type *type = e->type;
  941. switch (e->kind) {
  942. case Entity_Constant:
  943. if (type == t_invalid) {
  944. o->type = t_invalid;
  945. return e;
  946. }
  947. o->value = e->Constant.value;
  948. if (o->value.kind == ExactValue_Invalid) {
  949. return e;
  950. }
  951. o->mode = Addressing_Constant;
  952. break;
  953. case Entity_Variable:
  954. e->flags |= EntityFlag_Used;
  955. if (type == t_invalid) {
  956. o->type = t_invalid;
  957. return e;
  958. }
  959. o->mode = Addressing_Variable;
  960. if (e->Variable.is_immutable) {
  961. o->mode = Addressing_Immutable;
  962. }
  963. break;
  964. case Entity_TypeName:
  965. // NOTE(bill): Cyclical dependency checking is handled in the "type system" not here
  966. o->mode = Addressing_Type;
  967. break;
  968. case Entity_Procedure:
  969. o->mode = Addressing_Value;
  970. break;
  971. case Entity_Builtin:
  972. o->builtin_id = e->Builtin.id;
  973. o->mode = Addressing_Builtin;
  974. break;
  975. case Entity_ImportName:
  976. if (!allow_import_name) {
  977. error_node(n, "Use of import `%.*s` not in selector", LIT(name));
  978. }
  979. return e;
  980. case Entity_LibraryName:
  981. error_node(n, "Use of library `%.*s` not in #foreign tag", LIT(name));
  982. return e;
  983. case Entity_Label:
  984. o->mode = Addressing_NoValue;
  985. break;
  986. case Entity_Nil:
  987. o->mode = Addressing_Value;
  988. break;
  989. default:
  990. compiler_error("Unknown EntityKind");
  991. break;
  992. }
  993. o->type = type;
  994. return e;
  995. }
  996. i64 check_array_or_map_count(Checker *c, AstNode *e, bool is_map) {
  997. if (e == NULL) {
  998. return 0;
  999. }
  1000. Operand o = {0};
  1001. if (e->kind == AstNode_UnaryExpr &&
  1002. e->UnaryExpr.op.kind == Token_Ellipsis) {
  1003. return -1;
  1004. }
  1005. check_expr(c, &o, e);
  1006. if (o.mode != Addressing_Constant) {
  1007. if (o.mode != Addressing_Invalid) {
  1008. if (is_map) {
  1009. error_node(e, "Fixed map count must be a constant");
  1010. } else {
  1011. error_node(e, "Array count must be a constant");
  1012. }
  1013. }
  1014. return 0;
  1015. }
  1016. Type *type = base_type(o.type);
  1017. if (is_type_untyped(type) || is_type_integer(type)) {
  1018. if (o.value.kind == ExactValue_Integer) {
  1019. i64 count = o.value.value_integer;
  1020. if (is_map) {
  1021. if (count > 0) {
  1022. return count;
  1023. }
  1024. error_node(e, "Invalid fixed map count");
  1025. } else {
  1026. if (count >= 0) {
  1027. return count;
  1028. }
  1029. error_node(e, "Invalid array count");
  1030. }
  1031. return 0;
  1032. }
  1033. }
  1034. if (is_map) {
  1035. error_node(e, "Fixed map count must be an integer");
  1036. } else {
  1037. error_node(e, "Array count must be an integer");
  1038. }
  1039. return 0;
  1040. }
  1041. Type *make_optional_ok_type(gbAllocator a, Type *value) {
  1042. bool typed = true;
  1043. Type *t = make_type_tuple(a);
  1044. t->Tuple.variables = gb_alloc_array(a, Entity *, 2);
  1045. t->Tuple.variable_count = 2;
  1046. t->Tuple.variables[0] = make_entity_field(a, NULL, blank_token, value, false, 0);
  1047. t->Tuple.variables[1] = make_entity_field(a, NULL, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  1048. return t;
  1049. }
  1050. void check_map_type(Checker *c, Type *type, AstNode *node) {
  1051. GB_ASSERT(type->kind == Type_Map);
  1052. ast_node(mt, MapType, node);
  1053. i64 count = check_array_or_map_count(c, mt->count, true);
  1054. Type *key = check_type_extra(c, mt->key, NULL);
  1055. Type *value = check_type_extra(c, mt->value, NULL);
  1056. if (!is_type_valid_for_keys(key)) {
  1057. if (is_type_boolean(key)) {
  1058. error_node(node, "A boolean cannot be used as a key for a map");
  1059. } else {
  1060. gbString str = type_to_string(key);
  1061. error_node(node, "Invalid type of a key for a map, got `%s`", str);
  1062. gb_string_free(str);
  1063. }
  1064. }
  1065. if (count > 0) {
  1066. count = 0;
  1067. error_node(node, "Fixed map types are not yet implemented");
  1068. }
  1069. type->Map.count = count;
  1070. type->Map.key = key;
  1071. type->Map.value = value;
  1072. gbAllocator a = c->allocator;
  1073. {
  1074. // NOTE(bill): The preload types may have not been set yet
  1075. if (t_map_key == NULL) {
  1076. init_preload(c);
  1077. }
  1078. GB_ASSERT(t_map_key != NULL);
  1079. Type *entry_type = make_type_struct(a);
  1080. /*
  1081. struct {
  1082. hash: Map_Key,
  1083. next: int,
  1084. key: Key_Type,
  1085. value: Value_Type,
  1086. }
  1087. */
  1088. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1089. dummy_node->kind = AstNode_Invalid;
  1090. check_open_scope(c, dummy_node);
  1091. isize field_count = 3;
  1092. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  1093. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("key")), t_map_key, false, 0);
  1094. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("next")), t_int, false, 1);
  1095. fields[2] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("value")), value, false, 2);
  1096. check_close_scope(c);
  1097. entry_type->Record.fields = fields;
  1098. entry_type->Record.fields_in_src_order = fields;
  1099. entry_type->Record.field_count = field_count;
  1100. type_set_offsets(a, entry_type);
  1101. type->Map.entry_type = entry_type;
  1102. }
  1103. {
  1104. Type *generated_struct_type = make_type_struct(a);
  1105. /*
  1106. struct {
  1107. hashes: [dynamic]int,
  1108. entries; [dynamic]Entry_Type,
  1109. }
  1110. */
  1111. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1112. dummy_node->kind = AstNode_Invalid;
  1113. check_open_scope(c, dummy_node);
  1114. Type *hashes_type = make_type_dynamic_array(a, t_int);
  1115. Type *entries_type = make_type_dynamic_array(a, type->Map.entry_type);
  1116. isize field_count = 2;
  1117. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  1118. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("hashes")), hashes_type, false, 0);
  1119. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("entries")), entries_type, false, 1);
  1120. check_close_scope(c);
  1121. generated_struct_type->Record.fields = fields;
  1122. generated_struct_type->Record.fields_in_src_order = fields;
  1123. generated_struct_type->Record.field_count = field_count;
  1124. type_set_offsets(a, generated_struct_type);
  1125. type->Map.generated_struct_type = generated_struct_type;
  1126. }
  1127. type->Map.lookup_result_type = make_optional_ok_type(a, value);
  1128. // error_node(node, "`map` types are not yet implemented");
  1129. }
  1130. bool check_type_extra_interal(Checker *c, AstNode *e, Type **type, Type *named_type) {
  1131. GB_ASSERT_NOT_NULL(type);
  1132. if (e == NULL) {
  1133. *type = t_invalid;
  1134. return true;
  1135. }
  1136. switch (e->kind) {
  1137. case_ast_node(i, Ident, e);
  1138. Operand o = {0};
  1139. check_ident(c, &o, e, named_type, NULL, false);
  1140. switch (o.mode) {
  1141. case Addressing_Invalid:
  1142. break;
  1143. case Addressing_Type: {
  1144. *type = o.type;
  1145. return true;
  1146. } break;
  1147. case Addressing_NoValue: {
  1148. gbString err_str = expr_to_string(e);
  1149. error_node(e, "`%s` used as a type", err_str);
  1150. gb_string_free(err_str);
  1151. } break;
  1152. default: {
  1153. gbString err_str = expr_to_string(e);
  1154. error_node(e, "`%s` used as a type when not a type", err_str);
  1155. gb_string_free(err_str);
  1156. } break;
  1157. }
  1158. case_end;
  1159. case_ast_node(se, SelectorExpr, e);
  1160. Operand o = {0};
  1161. check_selector(c, &o, e, NULL);
  1162. switch (o.mode) {
  1163. case Addressing_Invalid:
  1164. break;
  1165. case Addressing_Type:
  1166. GB_ASSERT(o.type != NULL);
  1167. *type = o.type;
  1168. return true;
  1169. case Addressing_NoValue: {
  1170. gbString err_str = expr_to_string(e);
  1171. error_node(e, "`%s` used as a type", err_str);
  1172. gb_string_free(err_str);
  1173. } break;
  1174. default: {
  1175. gbString err_str = expr_to_string(e);
  1176. error_node(e, "`%s` is not a type", err_str);
  1177. gb_string_free(err_str);
  1178. } break;
  1179. }
  1180. case_end;
  1181. case_ast_node(pe, ParenExpr, e);
  1182. *type = check_type_extra(c, pe->expr, named_type);
  1183. return true;
  1184. case_end;
  1185. case_ast_node(ue, UnaryExpr, e);
  1186. if (ue->op.kind == Token_Pointer) {
  1187. *type = make_type_pointer(c->allocator, check_type(c, ue->expr));
  1188. return true;
  1189. } /* else if (ue->op.kind == Token_Maybe) {
  1190. *type = make_type_maybe(c->allocator, check_type(c, ue->expr));
  1191. return true;
  1192. } */
  1193. case_end;
  1194. case_ast_node(ht, HelperType, e);
  1195. *type = check_type(c, ht->type);
  1196. return true;
  1197. case_end;
  1198. case_ast_node(pt, PointerType, e);
  1199. Type *elem = check_type(c, pt->type);
  1200. i64 esz = type_size_of(c->allocator, elem);
  1201. *type = make_type_pointer(c->allocator, elem);
  1202. return true;
  1203. case_end;
  1204. case_ast_node(at, ArrayType, e);
  1205. if (at->count != NULL) {
  1206. Type *elem = check_type_extra(c, at->elem, NULL);
  1207. i64 count = check_array_or_map_count(c, at->count, false);
  1208. if (count < 0) {
  1209. error_node(at->count, ".. can only be used in conjuction with compound literals");
  1210. count = 0;
  1211. }
  1212. i64 esz = type_size_of(c->allocator, elem);
  1213. if (esz <= 0) {
  1214. gbString str = type_to_string(elem);
  1215. error_node(at->elem, "Zero sized element type `%s` is not allowed", str);
  1216. gb_string_free(str);
  1217. }
  1218. *type = make_type_array(c->allocator, elem, count);
  1219. } else {
  1220. Type *elem = check_type(c, at->elem);
  1221. i64 esz = type_size_of(c->allocator, elem);
  1222. if (esz <= 0) {
  1223. gbString str = type_to_string(elem);
  1224. error_node(at->elem, "Zero sized element type `%s` is not allowed", str);
  1225. gb_string_free(str);
  1226. }
  1227. *type = make_type_slice(c->allocator, elem);
  1228. }
  1229. return true;
  1230. case_end;
  1231. case_ast_node(dat, DynamicArrayType, e);
  1232. Type *elem = check_type_extra(c, dat->elem, NULL);
  1233. i64 esz = type_size_of(c->allocator, elem);
  1234. if (esz <= 0) {
  1235. gbString str = type_to_string(elem);
  1236. error_node(dat->elem, "Zero sized element type `%s` is not allowed", str);
  1237. gb_string_free(str);
  1238. }
  1239. *type = make_type_dynamic_array(c->allocator, elem);
  1240. return true;
  1241. case_end;
  1242. case_ast_node(vt, VectorType, e);
  1243. Type *elem = check_type(c, vt->elem);
  1244. Type *be = base_type(elem);
  1245. i64 count = check_array_or_map_count(c, vt->count, false);
  1246. if (is_type_vector(be) || (!is_type_boolean(be) && !is_type_numeric(be))) {
  1247. gbString err_str = type_to_string(elem);
  1248. error_node(vt->elem, "Vector element type must be numerical or a boolean, got `%s`", err_str);
  1249. gb_string_free(err_str);
  1250. }
  1251. *type = make_type_vector(c->allocator, elem, count);
  1252. return true;
  1253. case_end;
  1254. case_ast_node(st, StructType, e);
  1255. *type = make_type_struct(c->allocator);
  1256. set_base_type(named_type, *type);
  1257. check_open_scope(c, e);
  1258. check_struct_type(c, *type, e);
  1259. check_close_scope(c);
  1260. (*type)->Record.node = e;
  1261. return true;
  1262. case_end;
  1263. case_ast_node(ut, UnionType, e);
  1264. *type = make_type_union(c->allocator);
  1265. set_base_type(named_type, *type);
  1266. check_open_scope(c, e);
  1267. check_union_type(c, *type, e);
  1268. check_close_scope(c);
  1269. (*type)->Record.node = e;
  1270. return true;
  1271. case_end;
  1272. case_ast_node(rut, RawUnionType, e);
  1273. *type = make_type_raw_union(c->allocator);
  1274. set_base_type(named_type, *type);
  1275. check_open_scope(c, e);
  1276. check_raw_union_type(c, *type, e);
  1277. check_close_scope(c);
  1278. (*type)->Record.node = e;
  1279. return true;
  1280. case_end;
  1281. case_ast_node(et, EnumType, e);
  1282. *type = make_type_enum(c->allocator);
  1283. set_base_type(named_type, *type);
  1284. check_open_scope(c, e);
  1285. check_enum_type(c, *type, named_type, e);
  1286. check_close_scope(c);
  1287. (*type)->Record.node = e;
  1288. return true;
  1289. case_end;
  1290. case_ast_node(pt, ProcType, e);
  1291. *type = alloc_type(c->allocator, Type_Proc);
  1292. set_base_type(named_type, *type);
  1293. check_open_scope(c, e);
  1294. check_procedure_type(c, *type, e);
  1295. check_close_scope(c);
  1296. return true;
  1297. case_end;
  1298. case_ast_node(mt, MapType, e);
  1299. *type = alloc_type(c->allocator, Type_Map);
  1300. set_base_type(named_type, *type);
  1301. check_map_type(c, *type, e);
  1302. return true;
  1303. case_end;
  1304. case_ast_node(ce, CallExpr, e);
  1305. Operand o = {0};
  1306. check_expr_or_type(c, &o, e);
  1307. if (o.mode == Addressing_Type) {
  1308. *type = o.type;
  1309. return true;
  1310. }
  1311. case_end;
  1312. }
  1313. *type = t_invalid;
  1314. return false;
  1315. }
  1316. Type *check_type_extra(Checker *c, AstNode *e, Type *named_type) {
  1317. Type *type = NULL;
  1318. bool ok = check_type_extra_interal(c, e, &type, named_type);
  1319. if (!ok) {
  1320. gbString err_str = expr_to_string(e);
  1321. error_node(e, "`%s` is not a type", err_str);
  1322. gb_string_free(err_str);
  1323. type = t_invalid;
  1324. }
  1325. if (type == NULL) {
  1326. type = t_invalid;
  1327. }
  1328. if (is_type_named(type)) {
  1329. if (type->Named.base == NULL) {
  1330. gbString name = type_to_string(type);
  1331. error_node(e, "Invalid type definition of %s", name);
  1332. gb_string_free(name);
  1333. type->Named.base = t_invalid;
  1334. }
  1335. }
  1336. if (is_type_typed(type)) {
  1337. add_type_and_value(&c->info, e, Addressing_Type, type, (ExactValue){0});
  1338. } else {
  1339. gbString name = type_to_string(type);
  1340. error_node(e, "Invalid type definition of %s", name);
  1341. gb_string_free(name);
  1342. type = t_invalid;
  1343. }
  1344. set_base_type(named_type, type);
  1345. return type;
  1346. }
  1347. bool check_unary_op(Checker *c, Operand *o, Token op) {
  1348. // TODO(bill): Handle errors correctly
  1349. Type *type = base_type(base_vector_type(o->type));
  1350. gbString str = NULL;
  1351. switch (op.kind) {
  1352. case Token_Add:
  1353. case Token_Sub:
  1354. if (!is_type_numeric(type)) {
  1355. str = expr_to_string(o->expr);
  1356. error(op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  1357. gb_string_free(str);
  1358. }
  1359. break;
  1360. case Token_Xor:
  1361. if (!is_type_integer(type)) {
  1362. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  1363. }
  1364. break;
  1365. case Token_Not:
  1366. if (!is_type_boolean(type)) {
  1367. str = expr_to_string(o->expr);
  1368. error(op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  1369. gb_string_free(str);
  1370. }
  1371. break;
  1372. default:
  1373. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1374. return false;
  1375. }
  1376. return true;
  1377. }
  1378. bool check_binary_op(Checker *c, Operand *o, Token op) {
  1379. // TODO(bill): Handle errors correctly
  1380. Type *type = base_type(base_vector_type(o->type));
  1381. switch (op.kind) {
  1382. case Token_Sub:
  1383. case Token_SubEq:
  1384. if (!is_type_numeric(type) && !is_type_pointer(type)) {
  1385. error(op, "Operator `%.*s` is only allowed with numeric or pointer expressions", LIT(op.string));
  1386. return false;
  1387. }
  1388. if (is_type_pointer(type)) {
  1389. o->type = t_int;
  1390. }
  1391. if (base_type(type) == t_rawptr) {
  1392. gbString str = type_to_string(type);
  1393. error_node(o->expr, "Invalid pointer type for pointer arithmetic: `%s`", str);
  1394. gb_string_free(str);
  1395. return false;
  1396. }
  1397. break;
  1398. case Token_Add:
  1399. case Token_Mul:
  1400. case Token_Quo:
  1401. case Token_AddEq:
  1402. case Token_MulEq:
  1403. case Token_QuoEq:
  1404. if (!is_type_numeric(type)) {
  1405. error(op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  1406. return false;
  1407. }
  1408. break;
  1409. case Token_And:
  1410. case Token_Or:
  1411. case Token_AndEq:
  1412. case Token_OrEq:
  1413. if (!is_type_integer(type) && !is_type_boolean(type)) {
  1414. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  1415. return false;
  1416. }
  1417. break;
  1418. case Token_Mod:
  1419. case Token_Xor:
  1420. case Token_AndNot:
  1421. case Token_ModEq:
  1422. case Token_XorEq:
  1423. case Token_AndNotEq:
  1424. if (!is_type_integer(type)) {
  1425. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  1426. return false;
  1427. }
  1428. break;
  1429. case Token_CmpAnd:
  1430. case Token_CmpOr:
  1431. case Token_CmpAndEq:
  1432. case Token_CmpOrEq:
  1433. if (!is_type_boolean(type)) {
  1434. error(op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  1435. return false;
  1436. }
  1437. break;
  1438. default:
  1439. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1440. return false;
  1441. }
  1442. return true;
  1443. }
  1444. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  1445. if (in_value.kind == ExactValue_Invalid) {
  1446. // NOTE(bill): There's already been an error
  1447. return true;
  1448. }
  1449. type = core_type(type);
  1450. if (is_type_boolean(type)) {
  1451. return in_value.kind == ExactValue_Bool;
  1452. } else if (is_type_string(type)) {
  1453. return in_value.kind == ExactValue_String;
  1454. } else if (is_type_integer(type)) {
  1455. ExactValue v = exact_value_to_integer(in_value);
  1456. if (v.kind != ExactValue_Integer) {
  1457. return false;
  1458. }
  1459. if (out_value) *out_value = v;
  1460. i64 i = v.value_integer;
  1461. u64 u = *cast(u64 *)&i;
  1462. i64 s = 8*type_size_of(c->allocator, type);
  1463. u64 umax = ~0ull;
  1464. if (s < 64) {
  1465. umax = (1ull << s) - 1ull;
  1466. } else {
  1467. // IMPORTANT TODO(bill): I NEED A PROPER BIG NUMBER LIBRARY THAT CAN SUPPORT 128 bit integers and floats
  1468. s = 64;
  1469. }
  1470. i64 imax = (1ll << (s-1ll));
  1471. switch (type->Basic.kind) {
  1472. case Basic_i8:
  1473. case Basic_i16:
  1474. case Basic_i32:
  1475. case Basic_i64:
  1476. // case Basic_i128:
  1477. case Basic_int:
  1478. return gb_is_between(i, -imax, imax-1);
  1479. case Basic_u8:
  1480. case Basic_u16:
  1481. case Basic_u32:
  1482. case Basic_u64:
  1483. // case Basic_u128:
  1484. case Basic_uint:
  1485. return !(u < 0 || u > umax);
  1486. case Basic_UntypedInteger:
  1487. return true;
  1488. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  1489. }
  1490. } else if (is_type_float(type)) {
  1491. ExactValue v = exact_value_to_float(in_value);
  1492. if (v.kind != ExactValue_Float) {
  1493. return false;
  1494. }
  1495. switch (type->Basic.kind) {
  1496. // case Basic_f16:
  1497. case Basic_f32:
  1498. case Basic_f64:
  1499. // case Basic_f128:
  1500. if (out_value) *out_value = v;
  1501. return true;
  1502. case Basic_UntypedFloat:
  1503. return true;
  1504. }
  1505. } else if (is_type_pointer(type)) {
  1506. if (in_value.kind == ExactValue_Pointer) {
  1507. return true;
  1508. }
  1509. if (in_value.kind == ExactValue_Integer) {
  1510. return false;
  1511. // return true;
  1512. }
  1513. if (out_value) *out_value = in_value;
  1514. }
  1515. return false;
  1516. }
  1517. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  1518. GB_ASSERT(is_type_constant_type(type));
  1519. GB_ASSERT(o->mode == Addressing_Constant);
  1520. if (!check_representable_as_constant(c, o->value, type, &o->value)) {
  1521. gbString a = expr_to_string(o->expr);
  1522. gbString b = type_to_string(type);
  1523. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1524. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1525. error_node(o->expr, "`%s` truncated to `%s`", a, b);
  1526. } else {
  1527. error_node(o->expr, "`%s = %lld` overflows `%s`", a, o->value.value_integer, b);
  1528. }
  1529. } else {
  1530. error_node(o->expr, "Cannot convert `%s` to `%s`", a, b);
  1531. }
  1532. gb_string_free(b);
  1533. gb_string_free(a);
  1534. o->mode = Addressing_Invalid;
  1535. }
  1536. }
  1537. bool check_is_expr_vector_index(Checker *c, AstNode *expr) {
  1538. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1539. expr = unparen_expr(expr);
  1540. if (expr->kind == AstNode_IndexExpr) {
  1541. ast_node(ie, IndexExpr, expr);
  1542. Type *t = type_deref(type_of_expr(&c->info, ie->expr));
  1543. if (t != NULL) {
  1544. return is_type_vector(t);
  1545. }
  1546. }
  1547. return false;
  1548. }
  1549. bool check_is_vector_elem(Checker *c, AstNode *expr) {
  1550. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1551. expr = unparen_expr(expr);
  1552. if (expr->kind == AstNode_SelectorExpr) {
  1553. ast_node(se, SelectorExpr, expr);
  1554. Type *t = type_deref(type_of_expr(&c->info, se->expr));
  1555. if (t != NULL && is_type_vector(t)) {
  1556. return true;
  1557. }
  1558. }
  1559. return false;
  1560. }
  1561. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  1562. switch (op.kind) {
  1563. case Token_Pointer: { // Pointer address
  1564. if (o->mode == Addressing_Type) {
  1565. o->type = make_type_pointer(c->allocator, o->type);
  1566. return;
  1567. }
  1568. if (o->mode != Addressing_Variable ||
  1569. check_is_expr_vector_index(c, o->expr) ||
  1570. check_is_vector_elem(c, o->expr)) {
  1571. if (ast_node_expect(node, AstNode_UnaryExpr)) {
  1572. ast_node(ue, UnaryExpr, node);
  1573. gbString str = expr_to_string(ue->expr);
  1574. error(op, "Cannot take the pointer address of `%s`", str);
  1575. gb_string_free(str);
  1576. }
  1577. o->mode = Addressing_Invalid;
  1578. return;
  1579. }
  1580. o->mode = Addressing_Value;
  1581. o->type = make_type_pointer(c->allocator, o->type);
  1582. return;
  1583. }
  1584. }
  1585. if (!check_unary_op(c, o, op)) {
  1586. o->mode = Addressing_Invalid;
  1587. return;
  1588. }
  1589. if (o->mode == Addressing_Constant) {
  1590. Type *type = base_type(o->type);
  1591. if (!is_type_constant_type(o->type)) {
  1592. gbString xt = type_to_string(o->type);
  1593. gbString err_str = expr_to_string(node);
  1594. error(op, "Invalid type, `%s`, for constant unary expression `%s`", xt, err_str);
  1595. gb_string_free(err_str);
  1596. gb_string_free(xt);
  1597. o->mode = Addressing_Invalid;
  1598. return;
  1599. }
  1600. i32 precision = 0;
  1601. if (is_type_unsigned(type)) {
  1602. precision = cast(i32)(8 * type_size_of(c->allocator, type));
  1603. }
  1604. o->value = exact_unary_operator_value(op.kind, o->value, precision);
  1605. if (is_type_typed(type)) {
  1606. if (node != NULL) {
  1607. o->expr = node;
  1608. }
  1609. check_is_expressible(c, o, type);
  1610. }
  1611. return;
  1612. }
  1613. o->mode = Addressing_Value;
  1614. }
  1615. void check_comparison(Checker *c, Operand *x, Operand *y, TokenKind op) {
  1616. if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
  1617. bool comp = are_types_identical(x->type, y->type);
  1618. switch (op) {
  1619. case Token_CmpEq: comp = comp; break;
  1620. case Token_NotEq: comp = !comp; break;
  1621. }
  1622. x->mode = Addressing_Constant;
  1623. x->type = t_untyped_bool;
  1624. x->value = exact_value_bool(comp);
  1625. return;
  1626. }
  1627. gbString err_str = NULL;
  1628. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1629. if (check_is_assignable_to(c, x, y->type) ||
  1630. check_is_assignable_to(c, y, x->type)) {
  1631. Type *err_type = x->type;
  1632. bool defined = false;
  1633. switch (op) {
  1634. case Token_CmpEq:
  1635. case Token_NotEq:
  1636. defined = is_type_comparable(x->type);
  1637. break;
  1638. case Token_Lt:
  1639. case Token_Gt:
  1640. case Token_LtEq:
  1641. case Token_GtEq: {
  1642. defined = is_type_ordered(x->type);
  1643. } break;
  1644. }
  1645. // CLEANUP(bill) NOTE(bill): there is an auto assignment to `any` which needs to be checked
  1646. if (is_type_any(x->type) && !is_type_any(y->type)) {
  1647. err_type = x->type;
  1648. defined = false;
  1649. } else if (is_type_any(y->type) && !is_type_any(x->type)) {
  1650. err_type = y->type;
  1651. defined = false;
  1652. }
  1653. if (!defined) {
  1654. gbString type_string = type_to_string(err_type);
  1655. err_str = gb_string_make(c->tmp_allocator,
  1656. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(token_strings[op]), type_string));
  1657. gb_string_free(type_string);
  1658. }
  1659. } else {
  1660. gbString xt = type_to_string(x->type);
  1661. gbString yt = type_to_string(y->type);
  1662. err_str = gb_string_make(c->tmp_allocator,
  1663. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  1664. gb_string_free(yt);
  1665. gb_string_free(xt);
  1666. }
  1667. if (err_str != NULL) {
  1668. error_node(x->expr, "Cannot compare expression, %s", err_str);
  1669. x->type = t_untyped_bool;
  1670. } else {
  1671. if (x->mode == Addressing_Constant &&
  1672. y->mode == Addressing_Constant) {
  1673. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  1674. } else {
  1675. x->mode = Addressing_Value;
  1676. update_expr_type(c, x->expr, default_type(x->type), true);
  1677. update_expr_type(c, y->expr, default_type(y->type), true);
  1678. }
  1679. if (is_type_vector(base_type(y->type))) {
  1680. x->type = make_type_vector(c->allocator, t_bool, base_type(y->type)->Vector.count);
  1681. } else {
  1682. x->type = t_untyped_bool;
  1683. }
  1684. }
  1685. if (err_str != NULL) {
  1686. gb_string_free(err_str);
  1687. }
  1688. gb_temp_arena_memory_end(tmp);
  1689. }
  1690. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  1691. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1692. ast_node(be, BinaryExpr, node);
  1693. ExactValue x_val = {0};
  1694. if (x->mode == Addressing_Constant) {
  1695. x_val = exact_value_to_integer(x->value);
  1696. }
  1697. bool x_is_untyped = is_type_untyped(x->type);
  1698. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  1699. gbString err_str = expr_to_string(x->expr);
  1700. error_node(node, "Shifted operand `%s` must be an integer", err_str);
  1701. gb_string_free(err_str);
  1702. x->mode = Addressing_Invalid;
  1703. return;
  1704. }
  1705. if (is_type_unsigned(y->type)) {
  1706. } else if (is_type_untyped(y->type)) {
  1707. convert_to_typed(c, y, t_untyped_integer, 0);
  1708. if (y->mode == Addressing_Invalid) {
  1709. x->mode = Addressing_Invalid;
  1710. return;
  1711. }
  1712. } else {
  1713. gbString err_str = expr_to_string(y->expr);
  1714. error_node(node, "Shift amount `%s` must be an unsigned integer", err_str);
  1715. gb_string_free(err_str);
  1716. x->mode = Addressing_Invalid;
  1717. return;
  1718. }
  1719. if (x->mode == Addressing_Constant) {
  1720. if (y->mode == Addressing_Constant) {
  1721. ExactValue y_val = exact_value_to_integer(y->value);
  1722. if (y_val.kind != ExactValue_Integer) {
  1723. gbString err_str = expr_to_string(y->expr);
  1724. error_node(node, "Shift amount `%s` must be an unsigned integer", err_str);
  1725. gb_string_free(err_str);
  1726. x->mode = Addressing_Invalid;
  1727. return;
  1728. }
  1729. u64 amount = cast(u64)y_val.value_integer;
  1730. if (amount > 64) {
  1731. gbString err_str = expr_to_string(y->expr);
  1732. error_node(node, "Shift amount too large: `%s`", err_str);
  1733. gb_string_free(err_str);
  1734. x->mode = Addressing_Invalid;
  1735. return;
  1736. }
  1737. if (!is_type_integer(x->type)) {
  1738. // NOTE(bill): It could be an untyped float but still representable
  1739. // as an integer
  1740. x->type = t_untyped_integer;
  1741. }
  1742. x->value = exact_value_shift(be->op.kind, x_val, exact_value_integer(amount));
  1743. if (is_type_typed(x->type)) {
  1744. check_is_expressible(c, x, base_type(x->type));
  1745. }
  1746. return;
  1747. }
  1748. TokenPos pos = ast_node_token(x->expr).pos;
  1749. if (x_is_untyped) {
  1750. ExprInfo *info = map_expr_info_get(&c->info.untyped, hash_pointer(x->expr));
  1751. if (info != NULL) {
  1752. info->is_lhs = true;
  1753. }
  1754. x->mode = Addressing_Value;
  1755. // x->value = x_val;
  1756. return;
  1757. }
  1758. }
  1759. if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
  1760. gbString err_str = expr_to_string(y->expr);
  1761. error_node(node, "Shift amount cannot be negative: `%s`", err_str);
  1762. gb_string_free(err_str);
  1763. }
  1764. if (!is_type_integer(x->type)) {
  1765. gbString err_str = expr_to_string(y->expr);
  1766. error_node(node, "Shift operand `%s` must be an integer", err_str);
  1767. gb_string_free(err_str);
  1768. x->mode = Addressing_Invalid;
  1769. return;
  1770. }
  1771. x->mode = Addressing_Value;
  1772. }
  1773. String check_down_cast_name(Type *dst_, Type *src_) {
  1774. String result = {0};
  1775. Type *dst = type_deref(dst_);
  1776. Type *src = type_deref(src_);
  1777. Type *dst_s = base_type(dst);
  1778. GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
  1779. for (isize i = 0; i < dst_s->Record.field_count; i++) {
  1780. Entity *f = dst_s->Record.fields[i];
  1781. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  1782. if (f->flags & EntityFlag_Anonymous) {
  1783. if (are_types_identical(f->type, src_)) {
  1784. return f->token.string;
  1785. }
  1786. if (are_types_identical(type_deref(f->type), src_)) {
  1787. return f->token.string;
  1788. }
  1789. if (!is_type_pointer(f->type)) {
  1790. result = check_down_cast_name(f->type, src_);
  1791. if (result.len > 0) {
  1792. return result;
  1793. }
  1794. }
  1795. }
  1796. }
  1797. return result;
  1798. }
  1799. Operand check_ptr_addition(Checker *c, TokenKind op, Operand *ptr, Operand *offset, AstNode *node) {
  1800. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1801. ast_node(be, BinaryExpr, node);
  1802. GB_ASSERT(is_type_pointer(ptr->type));
  1803. GB_ASSERT(is_type_integer(offset->type));
  1804. GB_ASSERT(op == Token_Add || op == Token_Sub);
  1805. Operand operand = {0};
  1806. operand.mode = Addressing_Value;
  1807. operand.type = ptr->type;
  1808. operand.expr = node;
  1809. if (base_type(ptr->type) == t_rawptr) {
  1810. gbString str = type_to_string(ptr->type);
  1811. error_node(node, "Invalid pointer type for pointer arithmetic: `%s`", str);
  1812. gb_string_free(str);
  1813. operand.mode = Addressing_Invalid;
  1814. return operand;
  1815. }
  1816. Type *base_ptr = base_type(ptr->type); GB_ASSERT(base_ptr->kind == Type_Pointer);
  1817. Type *elem = base_ptr->Pointer.elem;
  1818. i64 elem_size = type_size_of(c->allocator, elem);
  1819. if (elem_size <= 0) {
  1820. gbString str = type_to_string(elem);
  1821. error_node(node, "Size of pointer's element type `%s` is zero and cannot be used for pointer arithmetic", str);
  1822. gb_string_free(str);
  1823. operand.mode = Addressing_Invalid;
  1824. return operand;
  1825. }
  1826. if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
  1827. i64 ptr_val = ptr->value.value_pointer;
  1828. i64 offset_val = exact_value_to_integer(offset->value).value_integer;
  1829. i64 new_ptr_val = ptr_val;
  1830. if (op == Token_Add) {
  1831. new_ptr_val += elem_size*offset_val;
  1832. } else {
  1833. new_ptr_val -= elem_size*offset_val;
  1834. }
  1835. operand.mode = Addressing_Constant;
  1836. operand.value = exact_value_pointer(new_ptr_val);
  1837. }
  1838. return operand;
  1839. }
  1840. bool check_is_castable_to(Checker *c, Operand *operand, Type *y) {
  1841. if (check_is_assignable_to(c, operand, y)) {
  1842. return true;
  1843. }
  1844. Type *x = operand->type;
  1845. Type *src = core_type(x);
  1846. Type *dst = core_type(y);
  1847. if (are_types_identical(src, dst)) {
  1848. return true;
  1849. }
  1850. if (dst->kind == Type_Array && src->kind == Type_Array) {
  1851. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  1852. return dst->Array.count == src->Array.count;
  1853. }
  1854. }
  1855. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  1856. return are_types_identical(dst->Slice.elem, src->Slice.elem);
  1857. }
  1858. // Cast between booleans and integers
  1859. if (is_type_boolean(src) || is_type_integer(src)) {
  1860. if (is_type_boolean(dst) || is_type_integer(dst)) {
  1861. return true;
  1862. }
  1863. }
  1864. // Cast between numbers
  1865. if (is_type_integer(src) || is_type_float(src)) {
  1866. if (is_type_integer(dst) || is_type_float(dst)) {
  1867. return true;
  1868. }
  1869. }
  1870. // Cast between pointers
  1871. if (is_type_pointer(src) && is_type_pointer(dst)) {
  1872. Type *s = base_type(type_deref(src));
  1873. if (is_type_union(s)) {
  1874. // NOTE(bill): Should the error be here?!
  1875. // NOTE(bill): This error should suppress the next casting error as it's at the same position
  1876. gbString xs = type_to_string(x);
  1877. gbString ys = type_to_string(y);
  1878. error_node(operand->expr, "Cannot cast from a union pointer `%s` to `%s`, try using `union_cast` or cast to a `rawptr`", xs, ys);
  1879. gb_string_free(ys);
  1880. gb_string_free(xs);
  1881. return false;
  1882. }
  1883. return true;
  1884. }
  1885. // (u)int <-> rawptr
  1886. if (is_type_int_or_uint(src) && is_type_rawptr(dst)) {
  1887. return true;
  1888. }
  1889. if (is_type_rawptr(src) && is_type_int_or_uint(dst)) {
  1890. return true;
  1891. }
  1892. // []byte/[]u8 <-> string
  1893. if (is_type_u8_slice(src) && is_type_string(dst)) {
  1894. return true;
  1895. }
  1896. if (is_type_string(src) && is_type_u8_slice(dst)) {
  1897. if (is_type_typed(src)) {
  1898. return true;
  1899. }
  1900. }
  1901. // proc <-> proc
  1902. if (is_type_proc(src) && is_type_proc(dst)) {
  1903. return true;
  1904. }
  1905. // proc -> rawptr
  1906. if (is_type_proc(src) && is_type_rawptr(dst)) {
  1907. return true;
  1908. }
  1909. // rawptr -> proc
  1910. if (is_type_rawptr(src) && is_type_proc(dst)) {
  1911. return true;
  1912. }
  1913. return false;
  1914. }
  1915. void check_cast(Checker *c, Operand *x, Type *type) {
  1916. bool is_const_expr = x->mode == Addressing_Constant;
  1917. bool can_convert = false;
  1918. Type *bt = base_type(type);
  1919. if (is_const_expr && is_type_constant_type(bt)) {
  1920. if (bt->kind == Type_Basic) {
  1921. if (check_representable_as_constant(c, x->value, bt, &x->value)) {
  1922. can_convert = true;
  1923. } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
  1924. can_convert = true;
  1925. }
  1926. }
  1927. } else if (check_is_castable_to(c, x, type)) {
  1928. if (x->mode != Addressing_Constant) {
  1929. x->mode = Addressing_Value;
  1930. }
  1931. can_convert = true;
  1932. }
  1933. if (!can_convert) {
  1934. gbString expr_str = expr_to_string(x->expr);
  1935. gbString to_type = type_to_string(type);
  1936. gbString from_type = type_to_string(x->type);
  1937. error_node(x->expr, "Cannot cast `%s` as `%s` from `%s`", expr_str, to_type, from_type);
  1938. gb_string_free(from_type);
  1939. gb_string_free(to_type);
  1940. gb_string_free(expr_str);
  1941. x->mode = Addressing_Invalid;
  1942. return;
  1943. }
  1944. if (is_type_untyped(x->type)) {
  1945. Type *final_type = type;
  1946. if (is_const_expr && !is_type_constant_type(type)) {
  1947. final_type = default_type(x->type);
  1948. }
  1949. update_expr_type(c, x->expr, final_type, true);
  1950. }
  1951. x->type = type;
  1952. }
  1953. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  1954. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1955. Operand y_ = {0}, *y = &y_;
  1956. ast_node(be, BinaryExpr, node);
  1957. Token op = be->op;
  1958. switch (op.kind) {
  1959. case Token_CmpEq:
  1960. case Token_NotEq: {
  1961. // NOTE(bill): Allow comparisons between types
  1962. check_expr_or_type(c, x, be->left);
  1963. check_expr_or_type(c, y, be->right);
  1964. bool xt = x->mode == Addressing_Type;
  1965. bool yt = y->mode == Addressing_Type;
  1966. // If only one is a type, this is an error
  1967. if (xt ^ yt) {
  1968. GB_ASSERT(xt != yt);
  1969. if (xt) error_operand_not_expression(x);
  1970. if (yt) error_operand_not_expression(y);
  1971. }
  1972. } break;
  1973. default:
  1974. check_expr(c, x, be->left);
  1975. check_expr(c, y, be->right);
  1976. break;
  1977. }
  1978. if (x->mode == Addressing_Invalid) {
  1979. return;
  1980. }
  1981. if (y->mode == Addressing_Invalid) {
  1982. x->mode = Addressing_Invalid;
  1983. x->expr = y->expr;
  1984. return;
  1985. }
  1986. if (token_is_shift(op.kind)) {
  1987. check_shift(c, x, y, node);
  1988. return;
  1989. }
  1990. if (op.kind == Token_Add || op.kind == Token_Sub) {
  1991. if (is_type_pointer(x->type) && is_type_integer(y->type)) {
  1992. *x = check_ptr_addition(c, op.kind, x, y, node);
  1993. return;
  1994. } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
  1995. if (op.kind == Token_Sub) {
  1996. gbString lhs = expr_to_string(x->expr);
  1997. gbString rhs = expr_to_string(y->expr);
  1998. error_node(node, "Invalid pointer arithmetic, did you mean `%s %.*s %s`?", rhs, LIT(op.string), lhs);
  1999. gb_string_free(rhs);
  2000. gb_string_free(lhs);
  2001. x->mode = Addressing_Invalid;
  2002. return;
  2003. }
  2004. *x = check_ptr_addition(c, op.kind, y, x, node);
  2005. return;
  2006. }
  2007. }
  2008. convert_to_typed(c, x, y->type, 0);
  2009. if (x->mode == Addressing_Invalid) {
  2010. return;
  2011. }
  2012. convert_to_typed(c, y, x->type, 0);
  2013. if (y->mode == Addressing_Invalid) {
  2014. x->mode = Addressing_Invalid;
  2015. return;
  2016. }
  2017. if (token_is_comparison(op.kind)) {
  2018. check_comparison(c, x, y, op.kind);
  2019. return;
  2020. }
  2021. if (!are_types_identical(x->type, y->type)) {
  2022. if (x->type != t_invalid &&
  2023. y->type != t_invalid) {
  2024. gbString xt = type_to_string(x->type);
  2025. gbString yt = type_to_string(y->type);
  2026. gbString expr_str = expr_to_string(x->expr);
  2027. error(op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", expr_str, xt, yt);
  2028. gb_string_free(expr_str);
  2029. gb_string_free(yt);
  2030. gb_string_free(xt);
  2031. }
  2032. x->mode = Addressing_Invalid;
  2033. return;
  2034. }
  2035. if (!check_binary_op(c, x, op)) {
  2036. x->mode = Addressing_Invalid;
  2037. return;
  2038. }
  2039. switch (op.kind) {
  2040. case Token_Quo:
  2041. case Token_Mod:
  2042. case Token_QuoEq:
  2043. case Token_ModEq:
  2044. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  2045. y->mode == Addressing_Constant) {
  2046. bool fail = false;
  2047. switch (y->value.kind) {
  2048. case ExactValue_Integer:
  2049. if (y->value.value_integer == 0) {
  2050. fail = true;
  2051. }
  2052. break;
  2053. case ExactValue_Float:
  2054. if (y->value.value_float == 0.0) {
  2055. fail = true;
  2056. }
  2057. break;
  2058. }
  2059. if (fail) {
  2060. error_node(y->expr, "Division by zero not allowed");
  2061. x->mode = Addressing_Invalid;
  2062. return;
  2063. }
  2064. }
  2065. }
  2066. if (x->mode == Addressing_Constant &&
  2067. y->mode == Addressing_Constant) {
  2068. ExactValue a = x->value;
  2069. ExactValue b = y->value;
  2070. Type *type = base_type(x->type);
  2071. if (is_type_pointer(type)) {
  2072. GB_ASSERT(op.kind == Token_Sub);
  2073. i64 bytes = a.value_pointer - b.value_pointer;
  2074. i64 diff = bytes/type_size_of(c->allocator, type);
  2075. x->value = exact_value_pointer(diff);
  2076. return;
  2077. }
  2078. if (!is_type_constant_type(type)) {
  2079. gbString xt = type_to_string(x->type);
  2080. gbString err_str = expr_to_string(node);
  2081. error(op, "Invalid type, `%s`, for constant binary expression `%s`", xt, err_str);
  2082. gb_string_free(err_str);
  2083. gb_string_free(xt);
  2084. x->mode = Addressing_Invalid;
  2085. return;
  2086. }
  2087. if (op.kind == Token_Quo && is_type_integer(type)) {
  2088. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  2089. }
  2090. x->value = exact_binary_operator_value(op.kind, a, b);
  2091. if (is_type_typed(type)) {
  2092. if (node != NULL) {
  2093. x->expr = node;
  2094. }
  2095. check_is_expressible(c, x, type);
  2096. }
  2097. return;
  2098. }
  2099. x->mode = Addressing_Value;
  2100. }
  2101. void update_expr_type(Checker *c, AstNode *e, Type *type, bool final) {
  2102. HashKey key = hash_pointer(e);
  2103. ExprInfo *found = map_expr_info_get(&c->info.untyped, key);
  2104. if (found == NULL) {
  2105. return;
  2106. }
  2107. ExprInfo old = *found;
  2108. switch (e->kind) {
  2109. case_ast_node(ue, UnaryExpr, e);
  2110. if (old.value.kind != ExactValue_Invalid) {
  2111. // NOTE(bill): if `e` is constant, the operands will be constant too.
  2112. // They don't need to be updated as they will be updated later and
  2113. // checked at the end of general checking stage.
  2114. break;
  2115. }
  2116. update_expr_type(c, ue->expr, type, final);
  2117. case_end;
  2118. case_ast_node(be, BinaryExpr, e);
  2119. if (old.value.kind != ExactValue_Invalid) {
  2120. // See above note in UnaryExpr case
  2121. break;
  2122. }
  2123. if (token_is_comparison(be->op.kind)) {
  2124. // NOTE(bill): Do nothing as the types are fine
  2125. } else if (token_is_shift(be->op.kind)) {
  2126. update_expr_type(c, be->left, type, final);
  2127. } else {
  2128. update_expr_type(c, be->left, type, final);
  2129. update_expr_type(c, be->right, type, final);
  2130. }
  2131. case_end;
  2132. case_ast_node(pe, ParenExpr, e);
  2133. update_expr_type(c, pe->expr, type, final);
  2134. case_end;
  2135. }
  2136. if (!final && is_type_untyped(type)) {
  2137. old.type = base_type(type);
  2138. map_expr_info_set(&c->info.untyped, key, old);
  2139. return;
  2140. }
  2141. // We need to remove it and then give it a new one
  2142. map_expr_info_remove(&c->info.untyped, key);
  2143. if (old.is_lhs && !is_type_integer(type)) {
  2144. gbString expr_str = expr_to_string(e);
  2145. gbString type_str = type_to_string(type);
  2146. error_node(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  2147. gb_string_free(type_str);
  2148. gb_string_free(expr_str);
  2149. return;
  2150. }
  2151. add_type_and_value(&c->info, e, old.mode, type, old.value);
  2152. }
  2153. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  2154. ExprInfo *found = map_expr_info_get(&c->info.untyped, hash_pointer(e));
  2155. if (found) {
  2156. found->value = value;
  2157. }
  2158. }
  2159. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  2160. gbString expr_str = expr_to_string(operand->expr);
  2161. gbString type_str = type_to_string(target_type);
  2162. char *extra_text = "";
  2163. if (operand->mode == Addressing_Constant) {
  2164. if (operand->value.value_integer == 0) {
  2165. if (str_ne(make_string_c(expr_str), str_lit("nil"))) { // HACK NOTE(bill): Just in case
  2166. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  2167. extra_text = " - Did you want `nil`?";
  2168. }
  2169. }
  2170. }
  2171. error_node(operand->expr, "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  2172. gb_string_free(type_str);
  2173. gb_string_free(expr_str);
  2174. operand->mode = Addressing_Invalid;
  2175. }
  2176. // NOTE(bill): Set initial level to 0
  2177. void convert_to_typed(Checker *c, Operand *operand, Type *target_type, i32 level) {
  2178. GB_ASSERT_NOT_NULL(target_type);
  2179. if (operand->mode == Addressing_Invalid ||
  2180. operand->mode == Addressing_Type ||
  2181. is_type_typed(operand->type) ||
  2182. target_type == t_invalid) {
  2183. return;
  2184. }
  2185. if (is_type_untyped(target_type)) {
  2186. GB_ASSERT(operand->type->kind == Type_Basic);
  2187. GB_ASSERT(target_type->kind == Type_Basic);
  2188. BasicKind x_kind = operand->type->Basic.kind;
  2189. BasicKind y_kind = target_type->Basic.kind;
  2190. if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
  2191. if (x_kind < y_kind) {
  2192. operand->type = target_type;
  2193. update_expr_type(c, operand->expr, target_type, false);
  2194. }
  2195. } else if (x_kind != y_kind) {
  2196. operand->mode = Addressing_Invalid;
  2197. convert_untyped_error(c, operand, target_type);
  2198. return;
  2199. }
  2200. return;
  2201. }
  2202. Type *t = core_type(target_type);
  2203. switch (t->kind) {
  2204. case Type_Basic:
  2205. if (operand->mode == Addressing_Constant) {
  2206. check_is_expressible(c, operand, t);
  2207. if (operand->mode == Addressing_Invalid) {
  2208. return;
  2209. }
  2210. update_expr_value(c, operand->expr, operand->value);
  2211. } else {
  2212. switch (operand->type->Basic.kind) {
  2213. case Basic_UntypedBool:
  2214. if (!is_type_boolean(target_type)) {
  2215. operand->mode = Addressing_Invalid;
  2216. convert_untyped_error(c, operand, target_type);
  2217. return;
  2218. }
  2219. break;
  2220. case Basic_UntypedInteger:
  2221. case Basic_UntypedFloat:
  2222. case Basic_UntypedRune:
  2223. if (!is_type_numeric(target_type)) {
  2224. operand->mode = Addressing_Invalid;
  2225. convert_untyped_error(c, operand, target_type);
  2226. return;
  2227. }
  2228. break;
  2229. case Basic_UntypedNil:
  2230. if (!type_has_nil(target_type)) {
  2231. operand->mode = Addressing_Invalid;
  2232. convert_untyped_error(c, operand, target_type);
  2233. return;
  2234. }
  2235. break;
  2236. }
  2237. }
  2238. break;
  2239. default:
  2240. if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  2241. operand->mode = Addressing_Invalid;
  2242. convert_untyped_error(c, operand, target_type);
  2243. return;
  2244. }
  2245. target_type = t_untyped_nil;
  2246. break;
  2247. }
  2248. operand->type = target_type;
  2249. update_expr_type(c, operand->expr, target_type, true);
  2250. }
  2251. bool check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *value) {
  2252. Operand operand = {Addressing_Invalid};
  2253. check_expr(c, &operand, index_value);
  2254. if (operand.mode == Addressing_Invalid) {
  2255. if (value) *value = 0;
  2256. return false;
  2257. }
  2258. convert_to_typed(c, &operand, t_int, 0);
  2259. if (operand.mode == Addressing_Invalid) {
  2260. if (value) *value = 0;
  2261. return false;
  2262. }
  2263. if (!is_type_integer(operand.type)) {
  2264. gbString expr_str = expr_to_string(operand.expr);
  2265. error_node(operand.expr, "Index `%s` must be an integer", expr_str);
  2266. gb_string_free(expr_str);
  2267. if (value) *value = 0;
  2268. return false;
  2269. }
  2270. if (operand.mode == Addressing_Constant &&
  2271. (c->context.stmt_state_flags & StmtStateFlag_no_bounds_check) == 0) {
  2272. i64 i = exact_value_to_integer(operand.value).value_integer;
  2273. if (i < 0) {
  2274. gbString expr_str = expr_to_string(operand.expr);
  2275. error_node(operand.expr, "Index `%s` cannot be a negative value", expr_str);
  2276. gb_string_free(expr_str);
  2277. if (value) *value = 0;
  2278. return false;
  2279. }
  2280. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  2281. if (value) *value = i;
  2282. if (i >= max_count) {
  2283. gbString expr_str = expr_to_string(operand.expr);
  2284. error_node(operand.expr, "Index `%s` is out of bounds range 0..<%lld", expr_str, max_count);
  2285. gb_string_free(expr_str);
  2286. return false;
  2287. }
  2288. return true;
  2289. }
  2290. }
  2291. // NOTE(bill): It's alright :D
  2292. if (value) *value = -1;
  2293. return true;
  2294. }
  2295. isize entity_overload_count(Scope *s, String name) {
  2296. Entity *e = scope_lookup_entity(s, name);
  2297. if (e == NULL) {
  2298. return 0;
  2299. }
  2300. if (e->kind == Entity_Procedure) {
  2301. // NOTE(bill): Overloads are only allowed with the same scope
  2302. return map_entity_multi_count(&s->elements, hash_string(e->token.string));
  2303. }
  2304. return 1;
  2305. }
  2306. bool check_is_field_exported(Checker *c, Entity *field) {
  2307. if (field == NULL) {
  2308. // NOTE(bill): Just incase
  2309. return true;
  2310. }
  2311. if (field->kind != Entity_Variable) {
  2312. return true;
  2313. }
  2314. Scope *file_scope = field->scope;
  2315. if (file_scope == NULL) {
  2316. return true;
  2317. }
  2318. while (!file_scope->is_file) {
  2319. file_scope = file_scope->parent;
  2320. }
  2321. if (!is_entity_exported(field) && file_scope != c->context.file_scope) {
  2322. return false;
  2323. }
  2324. return true;
  2325. }
  2326. Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_hint) {
  2327. ast_node(se, SelectorExpr, node);
  2328. bool check_op_expr = true;
  2329. Entity *expr_entity = NULL;
  2330. Entity *entity = NULL;
  2331. Selection sel = {0}; // NOTE(bill): Not used if it's an import name
  2332. operand->expr = node;
  2333. AstNode *op_expr = se->expr;
  2334. AstNode *selector = unparen_expr(se->selector);
  2335. if (selector == NULL) {
  2336. operand->mode = Addressing_Invalid;
  2337. operand->expr = node;
  2338. return NULL;
  2339. }
  2340. // if (selector->kind != AstNode_Ident && selector->kind != AstNode_BasicLit) {
  2341. if (selector->kind != AstNode_Ident) {
  2342. error_node(selector, "Illegal selector kind: `%.*s`", LIT(ast_node_strings[selector->kind]));
  2343. operand->mode = Addressing_Invalid;
  2344. operand->expr = node;
  2345. return NULL;
  2346. }
  2347. if (op_expr->kind == AstNode_Ident) {
  2348. String op_name = op_expr->Ident.string;
  2349. Entity *e = scope_lookup_entity(c->context.scope, op_name);
  2350. add_entity_use(c, op_expr, e);
  2351. expr_entity = e;
  2352. Entity *original_e = e;
  2353. while (e != NULL && e->kind == Entity_Alias && e->Alias.original != NULL) {
  2354. e = e->Alias.original;
  2355. }
  2356. if (e != NULL && e->kind == Entity_ImportName && selector->kind == AstNode_Ident) {
  2357. // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
  2358. // It pretty much needs to be in this order and this way
  2359. // If you can clean this up, please do but be really careful
  2360. String import_name = op_name;
  2361. Scope *import_scope = e->ImportName.scope;
  2362. String entity_name = selector->Ident.string;
  2363. check_op_expr = false;
  2364. entity = scope_lookup_entity(import_scope, entity_name);
  2365. bool is_declared = entity != NULL;
  2366. if (is_declared) {
  2367. if (entity->kind == Entity_Builtin) {
  2368. // NOTE(bill): Builtin's are in the universe scope which is part of every scopes hierarchy
  2369. // This means that we should just ignore the found result through it
  2370. is_declared = false;
  2371. } else if (entity->scope->is_global && !import_scope->is_global) {
  2372. is_declared = false;
  2373. }
  2374. }
  2375. if (!is_declared) {
  2376. error_node(op_expr, "`%.*s` is not declared by `%.*s`", LIT(entity_name), LIT(import_name));
  2377. operand->mode = Addressing_Invalid;
  2378. operand->expr = node;
  2379. return NULL;
  2380. }
  2381. check_entity_decl(c, entity, NULL, NULL);
  2382. GB_ASSERT(entity->type != NULL);
  2383. isize overload_count = entity_overload_count(import_scope, entity_name);
  2384. bool is_overloaded = overload_count > 1;
  2385. bool implicit_is_found = map_bool_get(&e->ImportName.scope->implicit, hash_pointer(entity)) != NULL;
  2386. bool is_not_exported = !is_entity_exported(entity);
  2387. if (!implicit_is_found) {
  2388. is_not_exported = false;
  2389. } else if (entity->kind == Entity_ImportName) {
  2390. is_not_exported = true;
  2391. }
  2392. if (is_not_exported) {
  2393. gbString sel_str = expr_to_string(selector);
  2394. error_node(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
  2395. gb_string_free(sel_str);
  2396. operand->mode = Addressing_Invalid;
  2397. operand->expr = node;
  2398. return NULL;
  2399. }
  2400. if (is_overloaded) {
  2401. HashKey key = hash_string(entity_name);
  2402. bool skip = false;
  2403. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  2404. map_entity_multi_get_all(&import_scope->elements, key, procs);
  2405. for (isize i = 0; i < overload_count; i++) {
  2406. Type *t = base_type(procs[i]->type);
  2407. if (t == t_invalid) {
  2408. continue;
  2409. }
  2410. // NOTE(bill): Check to see if it's imported
  2411. if (map_bool_get(&import_scope->implicit, hash_pointer(procs[i]))) {
  2412. gb_swap(Entity *, procs[i], procs[overload_count-1]);
  2413. overload_count--;
  2414. i--; // NOTE(bill): Counteract the post event
  2415. continue;
  2416. }
  2417. Operand x = {0};
  2418. x.mode = Addressing_Value;
  2419. x.type = t;
  2420. if (type_hint != NULL) {
  2421. if (check_is_assignable_to(c, &x, type_hint)) {
  2422. entity = procs[i];
  2423. skip = true;
  2424. break;
  2425. }
  2426. }
  2427. }
  2428. if (overload_count > 0 && !skip) {
  2429. operand->mode = Addressing_Overload;
  2430. operand->type = t_invalid;
  2431. operand->expr = node;
  2432. operand->overload_count = overload_count;
  2433. operand->overload_entities = procs;
  2434. return procs[0];
  2435. }
  2436. }
  2437. }
  2438. }
  2439. if (check_op_expr) {
  2440. check_expr_base(c, operand, op_expr, NULL);
  2441. if (operand->mode == Addressing_Invalid) {
  2442. operand->mode = Addressing_Invalid;
  2443. operand->expr = node;
  2444. return NULL;
  2445. }
  2446. }
  2447. if (entity == NULL && selector->kind == AstNode_Ident) {
  2448. String field_name = selector->Ident.string;
  2449. sel = lookup_field(c->allocator, operand->type, field_name, operand->mode == Addressing_Type);
  2450. if (operand->mode != Addressing_Type && !check_is_field_exported(c, sel.entity)) {
  2451. error_node(op_expr, "`%.*s` is an unexported field", LIT(field_name));
  2452. operand->mode = Addressing_Invalid;
  2453. operand->expr = node;
  2454. return NULL;
  2455. }
  2456. entity = sel.entity;
  2457. // NOTE(bill): Add type info needed for fields like `names`
  2458. if (entity != NULL && (entity->flags&EntityFlag_TypeField)) {
  2459. add_type_info_type(c, operand->type);
  2460. }
  2461. }
  2462. if (entity == NULL && selector->kind == AstNode_BasicLit) {
  2463. if (is_type_struct(operand->type) || is_type_tuple(operand->type)) {
  2464. Type *type = base_type(operand->type);
  2465. Operand o = {0};
  2466. check_expr(c, &o, selector);
  2467. if (o.mode != Addressing_Constant ||
  2468. !is_type_integer(o.type)) {
  2469. error_node(op_expr, "Indexed based selectors must be a constant integer %s");
  2470. operand->mode = Addressing_Invalid;
  2471. operand->expr = node;
  2472. return NULL;
  2473. }
  2474. i64 index = o.value.value_integer;
  2475. if (index < 0) {
  2476. error_node(o.expr, "Index %lld cannot be a negative value", index);
  2477. operand->mode = Addressing_Invalid;
  2478. operand->expr = node;
  2479. return NULL;
  2480. }
  2481. i64 max_count = 0;
  2482. switch (type->kind) {
  2483. case Type_Record: max_count = type->Record.field_count; break;
  2484. case Type_Tuple: max_count = type->Tuple.variable_count; break;
  2485. }
  2486. if (index >= max_count) {
  2487. error_node(o.expr, "Index %lld is out of bounds range 0..<%lld", index, max_count);
  2488. operand->mode = Addressing_Invalid;
  2489. operand->expr = node;
  2490. return NULL;
  2491. }
  2492. sel = lookup_field_from_index(heap_allocator(), type, index);
  2493. entity = sel.entity;
  2494. GB_ASSERT(entity != NULL);
  2495. } else {
  2496. error_node(op_expr, "Indexed based selectors may only be used on structs or tuples");
  2497. operand->mode = Addressing_Invalid;
  2498. operand->expr = node;
  2499. return NULL;
  2500. }
  2501. }
  2502. if (entity == NULL &&
  2503. operand->type != NULL && is_type_untyped(operand->type) && is_type_string(operand->type)) {
  2504. String s = operand->value.value_string;
  2505. operand->mode = Addressing_Constant;
  2506. operand->value = exact_value_integer(s.len);
  2507. operand->type = t_untyped_integer;
  2508. return NULL;
  2509. }
  2510. if (entity == NULL) {
  2511. gbString op_str = expr_to_string(op_expr);
  2512. gbString type_str = type_to_string(operand->type);
  2513. gbString sel_str = expr_to_string(selector);
  2514. error_node(op_expr, "`%s` of type `%s` has no field `%s`", op_str, type_str, sel_str);
  2515. gb_string_free(sel_str);
  2516. gb_string_free(type_str);
  2517. gb_string_free(op_str);
  2518. operand->mode = Addressing_Invalid;
  2519. operand->expr = node;
  2520. return NULL;
  2521. }
  2522. if (expr_entity != NULL && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
  2523. gbString op_str = expr_to_string(op_expr);
  2524. gbString type_str = type_to_string(operand->type);
  2525. gbString sel_str = expr_to_string(selector);
  2526. error_node(op_expr, "Cannot access non-constant field `%s` from `%s`", sel_str, op_str);
  2527. gb_string_free(sel_str);
  2528. gb_string_free(type_str);
  2529. gb_string_free(op_str);
  2530. operand->mode = Addressing_Invalid;
  2531. operand->expr = node;
  2532. return NULL;
  2533. }
  2534. add_entity_use(c, selector, entity);
  2535. switch (entity->kind) {
  2536. case Entity_Constant:
  2537. operand->mode = Addressing_Constant;
  2538. operand->value = entity->Constant.value;
  2539. break;
  2540. case Entity_Variable:
  2541. // TODO(bill): Is this the rule I need?
  2542. if (operand->mode == Addressing_Immutable) {
  2543. // Okay
  2544. } else if (sel.indirect || operand->mode != Addressing_Value) {
  2545. operand->mode = Addressing_Variable;
  2546. } else {
  2547. operand->mode = Addressing_Value;
  2548. }
  2549. break;
  2550. case Entity_TypeName:
  2551. operand->mode = Addressing_Type;
  2552. break;
  2553. case Entity_Procedure:
  2554. operand->mode = Addressing_Value;
  2555. break;
  2556. case Entity_Builtin:
  2557. operand->mode = Addressing_Builtin;
  2558. operand->builtin_id = entity->Builtin.id;
  2559. break;
  2560. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  2561. case Entity_Nil:
  2562. operand->mode = Addressing_Value;
  2563. break;
  2564. }
  2565. operand->type = entity->type;
  2566. operand->expr = node;
  2567. return entity;
  2568. }
  2569. bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  2570. GB_ASSERT(call->kind == AstNode_CallExpr);
  2571. ast_node(ce, CallExpr, call);
  2572. BuiltinProc *bp = &builtin_procs[id];
  2573. {
  2574. char *err = NULL;
  2575. if (ce->args.count < bp->arg_count) {
  2576. err = "Too few";
  2577. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  2578. err = "Too many";
  2579. }
  2580. if (err != NULL) {
  2581. gbString expr = expr_to_string(ce->proc);
  2582. error(ce->close, "%s arguments for `%s`, expected %td, got %td",
  2583. err, expr,
  2584. bp->arg_count, ce->args.count);
  2585. gb_string_free(expr);
  2586. return false;
  2587. }
  2588. }
  2589. Operand prev_operand = *operand;
  2590. switch (id) {
  2591. case BuiltinProc_new:
  2592. case BuiltinProc_new_slice:
  2593. case BuiltinProc_size_of:
  2594. case BuiltinProc_align_of:
  2595. case BuiltinProc_offset_of:
  2596. case BuiltinProc_type_info:
  2597. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  2598. break;
  2599. default:
  2600. check_multi_expr(c, operand, ce->args.e[0]);
  2601. }
  2602. switch (id) {
  2603. default:
  2604. GB_PANIC("Implement builtin procedure: %.*s", LIT(builtin_procs[id].name));
  2605. break;
  2606. case BuiltinProc_new: {
  2607. // new :: proc(Type) -> ^Type
  2608. Operand op = {0};
  2609. check_expr_or_type(c, &op, ce->args.e[0]);
  2610. Type *type = op.type;
  2611. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  2612. error_node(ce->args.e[0], "Expected a type for `new`");
  2613. return false;
  2614. }
  2615. operand->mode = Addressing_Value;
  2616. operand->type = make_type_pointer(c->allocator, type);
  2617. } break;
  2618. case BuiltinProc_new_slice: {
  2619. // new_slice :: proc(Type, len: int) -> []Type
  2620. // new_slice :: proc(Type, len, cap: int) -> []Type
  2621. Operand op = {0};
  2622. check_expr_or_type(c, &op, ce->args.e[0]);
  2623. Type *type = op.type;
  2624. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  2625. error_node(ce->args.e[0], "Expected a type for `new_slice`");
  2626. return false;
  2627. }
  2628. isize arg_count = ce->args.count;
  2629. if (arg_count < 2 || 3 < arg_count) {
  2630. error_node(ce->args.e[0], "`new_slice` expects 2 or 3 arguments, found %td", arg_count);
  2631. // NOTE(bill): Return the correct type to reduce errors
  2632. } else {
  2633. // If any are constant
  2634. i64 sizes[2] = {0};
  2635. isize size_count = 0;
  2636. for (isize i = 1; i < arg_count; i++) {
  2637. i64 val = 0;
  2638. bool ok = check_index_value(c, ce->args.e[i], -1, &val);
  2639. if (ok && val >= 0) {
  2640. GB_ASSERT(size_count < gb_count_of(sizes));
  2641. sizes[size_count++] = val;
  2642. }
  2643. }
  2644. if (size_count == 2 && sizes[0] > sizes[1]) {
  2645. error_node(ce->args.e[1], "`new_slice` count and capacity are swapped");
  2646. // No need quit
  2647. }
  2648. }
  2649. operand->mode = Addressing_Value;
  2650. operand->type = make_type_slice(c->allocator, type);
  2651. } break;
  2652. case BuiltinProc_free: {
  2653. // free :: proc(^Type)
  2654. // free :: proc([]Type)
  2655. // free :: proc(string)
  2656. // free :: proc(map[K]T)
  2657. Type *type = operand->type;
  2658. bool ok = false;
  2659. if (is_type_pointer(type)) {
  2660. ok = true;
  2661. } else if (is_type_slice(type)) {
  2662. ok = true;
  2663. } else if (is_type_string(type)) {
  2664. ok = true;
  2665. } else if (is_type_dynamic_array(type)) {
  2666. ok = true;
  2667. } else if (is_type_dynamic_map(type)) {
  2668. ok = true;
  2669. }
  2670. if (!ok) {
  2671. gbString type_str = type_to_string(type);
  2672. error_node(operand->expr, "Invalid type for `free`, got `%s`", type_str);
  2673. gb_string_free(type_str);
  2674. return false;
  2675. }
  2676. operand->mode = Addressing_NoValue;
  2677. } break;
  2678. case BuiltinProc_reserve: {
  2679. // reserve :: proc([dynamic]Type, count: int) {
  2680. // reserve :: proc(map[Key]Type, count: int) {
  2681. Type *type = operand->type;
  2682. if (!is_type_dynamic_array(type) && !is_type_dynamic_map(type)) {
  2683. gbString str = type_to_string(type);
  2684. error_node(operand->expr, "Expected a dynamic array or dynamic map, got `%s`", str);
  2685. gb_string_free(str);
  2686. return false;
  2687. }
  2688. AstNode *capacity = ce->args.e[1];
  2689. Operand op = {0};
  2690. check_expr(c, &op, capacity);
  2691. if (op.mode == Addressing_Invalid) {
  2692. return false;
  2693. }
  2694. Type *arg_type = base_type(op.type);
  2695. if (!is_type_integer(arg_type)) {
  2696. error_node(operand->expr, "`reserve` capacities must be an integer");
  2697. return false;
  2698. }
  2699. operand->type = NULL;
  2700. operand->mode = Addressing_NoValue;
  2701. } break;
  2702. case BuiltinProc_clear: {
  2703. Type *type = operand->type;
  2704. bool is_pointer = is_type_pointer(type);
  2705. type = base_type(type_deref(type));
  2706. if (!is_type_dynamic_array(type) && !is_type_map(type) && !is_type_slice(type)) {
  2707. gbString str = type_to_string(type);
  2708. error_node(operand->expr, "Invalid type for `clear`, got `%s`", str);
  2709. gb_string_free(str);
  2710. return false;
  2711. }
  2712. operand->type = NULL;
  2713. operand->mode = Addressing_NoValue;
  2714. } break;
  2715. case BuiltinProc_append: {
  2716. // append :: proc([dynamic]Type, item: ..Type)
  2717. // append :: proc([]Type, item: ..Type)
  2718. Type *type = operand->type;
  2719. bool is_pointer = is_type_pointer(type);
  2720. type = base_type(type_deref(type));
  2721. if (!is_type_dynamic_array(type) && !is_type_slice(type)) {
  2722. gbString str = type_to_string(type);
  2723. error_node(operand->expr, "Expected a slice or dynamic array, got `%s`", str);
  2724. gb_string_free(str);
  2725. return false;
  2726. }
  2727. bool is_addressable = operand->mode == Addressing_Variable;
  2728. if (is_pointer) {
  2729. is_addressable = true;
  2730. }
  2731. if (!is_addressable) {
  2732. error_node(operand->expr, "`append` can only operate on addressable values");
  2733. return false;
  2734. }
  2735. Type *elem = NULL;
  2736. if (is_type_dynamic_array(type)) {
  2737. elem = type->DynamicArray.elem;
  2738. } else {
  2739. elem = type->Slice.elem;
  2740. }
  2741. Type *slice_elem = make_type_slice(c->allocator, elem);
  2742. Type *proc_type_params = make_type_tuple(c->allocator);
  2743. proc_type_params->Tuple.variables = gb_alloc_array(c->allocator, Entity *, 2);
  2744. proc_type_params->Tuple.variable_count = 2;
  2745. proc_type_params->Tuple.variables[0] = make_entity_param(c->allocator, NULL, blank_token, operand->type, false, false);
  2746. proc_type_params->Tuple.variables[1] = make_entity_param(c->allocator, NULL, blank_token, slice_elem, false, false);
  2747. Type *proc_type = make_type_proc(c->allocator, NULL, proc_type_params, 2, NULL, false, true, ProcCC_Odin);
  2748. check_call_arguments(c, &prev_operand, proc_type, call);
  2749. if (prev_operand.mode == Addressing_Invalid) {
  2750. return false;
  2751. }
  2752. operand->mode = Addressing_Value;
  2753. operand->type = t_int;
  2754. } break;
  2755. case BuiltinProc_delete: {
  2756. // delete :: proc(map[Key]Value, key: Key)
  2757. Type *type = operand->type;
  2758. if (!is_type_map(type)) {
  2759. gbString str = type_to_string(type);
  2760. error_node(operand->expr, "Expected a map, got `%s`", str);
  2761. gb_string_free(str);
  2762. return false;
  2763. }
  2764. Type *key = base_type(type)->Map.key;
  2765. Operand x = {Addressing_Invalid};
  2766. AstNode *key_node = ce->args.e[1];
  2767. Operand op = {0};
  2768. check_expr(c, &op, key_node);
  2769. if (op.mode == Addressing_Invalid) {
  2770. return false;
  2771. }
  2772. if (!check_is_assignable_to(c, &op, key)) {
  2773. gbString kt = type_to_string(key);
  2774. gbString ot = type_to_string(op.type);
  2775. error_node(operand->expr, "Expected a key of type `%s`, got `%s`", key, ot);
  2776. gb_string_free(ot);
  2777. gb_string_free(kt);
  2778. return false;
  2779. }
  2780. operand->mode = Addressing_NoValue;
  2781. } break;
  2782. case BuiltinProc_size_of: {
  2783. // size_of :: proc(Type) -> untyped int
  2784. Type *type = check_type(c, ce->args.e[0]);
  2785. if (type == NULL || type == t_invalid) {
  2786. error_node(ce->args.e[0], "Expected a type for `size_of`");
  2787. return false;
  2788. }
  2789. operand->mode = Addressing_Constant;
  2790. operand->value = exact_value_integer(type_size_of(c->allocator, type));
  2791. operand->type = t_untyped_integer;
  2792. } break;
  2793. case BuiltinProc_size_of_val:
  2794. // size_of_val :: proc(val: Type) -> untyped int
  2795. check_assignment(c, operand, NULL, str_lit("argument of `size_of_val`"));
  2796. if (operand->mode == Addressing_Invalid) {
  2797. return false;
  2798. }
  2799. operand->mode = Addressing_Constant;
  2800. operand->value = exact_value_integer(type_size_of(c->allocator, operand->type));
  2801. operand->type = t_untyped_integer;
  2802. break;
  2803. case BuiltinProc_align_of: {
  2804. // align_of :: proc(Type) -> untyped int
  2805. Type *type = check_type(c, ce->args.e[0]);
  2806. if (type == NULL || type == t_invalid) {
  2807. error_node(ce->args.e[0], "Expected a type for `align_of`");
  2808. return false;
  2809. }
  2810. operand->mode = Addressing_Constant;
  2811. operand->value = exact_value_integer(type_align_of(c->allocator, type));
  2812. operand->type = t_untyped_integer;
  2813. } break;
  2814. case BuiltinProc_align_of_val:
  2815. // align_of_val :: proc(val: Type) -> untyped int
  2816. check_assignment(c, operand, NULL, str_lit("argument of `align_of_val`"));
  2817. if (operand->mode == Addressing_Invalid) {
  2818. return false;
  2819. }
  2820. operand->mode = Addressing_Constant;
  2821. operand->value = exact_value_integer(type_align_of(c->allocator, operand->type));
  2822. operand->type = t_untyped_integer;
  2823. break;
  2824. case BuiltinProc_offset_of: {
  2825. // offset_of :: proc(Type, field) -> untyped int
  2826. Operand op = {0};
  2827. Type *bt = check_type(c, ce->args.e[0]);
  2828. Type *type = base_type(bt);
  2829. if (type == NULL || type == t_invalid) {
  2830. error_node(ce->args.e[0], "Expected a type for `offset_of`");
  2831. return false;
  2832. }
  2833. AstNode *field_arg = unparen_expr(ce->args.e[1]);
  2834. if (field_arg == NULL ||
  2835. field_arg->kind != AstNode_Ident) {
  2836. error_node(field_arg, "Expected an identifier for field argument");
  2837. return false;
  2838. }
  2839. if (is_type_array(type) || is_type_vector(type)) {
  2840. error_node(field_arg, "Invalid type for `offset_of`");
  2841. return false;
  2842. }
  2843. ast_node(arg, Ident, field_arg);
  2844. Selection sel = lookup_field(c->allocator, type, arg->string, operand->mode == Addressing_Type);
  2845. if (sel.entity == NULL) {
  2846. gbString type_str = type_to_string(bt);
  2847. error_node(ce->args.e[0],
  2848. "`%s` has no field named `%.*s`", type_str, LIT(arg->string));
  2849. gb_string_free(type_str);
  2850. return false;
  2851. }
  2852. if (sel.indirect) {
  2853. gbString type_str = type_to_string(bt);
  2854. error_node(ce->args.e[0],
  2855. "Field `%.*s` is embedded via a pointer in `%s`", LIT(arg->string), type_str);
  2856. gb_string_free(type_str);
  2857. return false;
  2858. }
  2859. operand->mode = Addressing_Constant;
  2860. operand->value = exact_value_integer(type_offset_of_from_selection(c->allocator, type, sel));
  2861. operand->type = t_untyped_integer;
  2862. } break;
  2863. case BuiltinProc_offset_of_val: {
  2864. // offset_of_val :: proc(val: expression) -> untyped int
  2865. AstNode *arg = unparen_expr(ce->args.e[0]);
  2866. if (arg->kind != AstNode_SelectorExpr) {
  2867. gbString str = expr_to_string(arg);
  2868. error_node(arg, "`%s` is not a selector expression", str);
  2869. return false;
  2870. }
  2871. ast_node(s, SelectorExpr, arg);
  2872. check_expr(c, operand, s->expr);
  2873. if (operand->mode == Addressing_Invalid) {
  2874. return false;
  2875. }
  2876. Type *type = operand->type;
  2877. if (base_type(type)->kind == Type_Pointer) {
  2878. Type *p = base_type(type);
  2879. if (is_type_struct(p)) {
  2880. type = p->Pointer.elem;
  2881. }
  2882. }
  2883. if (is_type_array(type) || is_type_vector(type)) {
  2884. error_node(arg, "Invalid type for `offset_of_val`");
  2885. return false;
  2886. }
  2887. ast_node(i, Ident, s->selector);
  2888. Selection sel = lookup_field(c->allocator, type, i->string, operand->mode == Addressing_Type);
  2889. if (sel.entity == NULL) {
  2890. gbString type_str = type_to_string(type);
  2891. error_node(arg,
  2892. "`%s` has no field named `%.*s`", type_str, LIT(i->string));
  2893. return false;
  2894. }
  2895. if (sel.indirect) {
  2896. gbString type_str = type_to_string(type);
  2897. error_node(ce->args.e[0],
  2898. "Field `%.*s` is embedded via a pointer in `%s`", LIT(i->string), type_str);
  2899. gb_string_free(type_str);
  2900. return false;
  2901. }
  2902. operand->mode = Addressing_Constant;
  2903. // IMPORTANT TODO(bill): Fix for anonymous fields
  2904. operand->value = exact_value_integer(type_offset_of_from_selection(c->allocator, type, sel));
  2905. operand->type = t_untyped_integer;
  2906. } break;
  2907. case BuiltinProc_type_of_val:
  2908. // type_of_val :: proc(val: Type) -> type(Type)
  2909. check_assignment(c, operand, NULL, str_lit("argument of `type_of_val`"));
  2910. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin) {
  2911. return false;
  2912. }
  2913. if (operand->type == NULL || operand->type == t_invalid) {
  2914. error_node(operand->expr, "Invalid argument to `type_of_val`");
  2915. return false;
  2916. }
  2917. operand->mode = Addressing_Type;
  2918. break;
  2919. case BuiltinProc_type_info: {
  2920. // type_info :: proc(Type) -> ^Type_Info
  2921. if (c->context.scope->is_global) {
  2922. compiler_error("`type_info` Cannot be declared within a #shared_global_scope due to how the internals of the compiler works");
  2923. }
  2924. // NOTE(bill): The type information may not be setup yet
  2925. init_preload(c);
  2926. AstNode *expr = ce->args.e[0];
  2927. Type *type = check_type(c, expr);
  2928. if (type == NULL || type == t_invalid) {
  2929. error_node(expr, "Invalid argument to `type_info`");
  2930. return false;
  2931. }
  2932. add_type_info_type(c, type);
  2933. operand->mode = Addressing_Value;
  2934. operand->type = t_type_info_ptr;
  2935. } break;
  2936. case BuiltinProc_type_info_of_val: {
  2937. // type_info_of_val :: proc(val: Type) -> ^Type_Info
  2938. if (c->context.scope->is_global) {
  2939. compiler_error("`type_info` Cannot be declared within a #shared_global_scope due to how the internals of the compiler works");
  2940. }
  2941. // NOTE(bill): The type information may not be setup yet
  2942. init_preload(c);
  2943. AstNode *expr = ce->args.e[0];
  2944. check_assignment(c, operand, NULL, str_lit("argument of `type_info_of_val`"));
  2945. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin)
  2946. return false;
  2947. add_type_info_type(c, operand->type);
  2948. operand->mode = Addressing_Value;
  2949. operand->type = t_type_info_ptr;
  2950. } break;
  2951. case BuiltinProc_compile_assert:
  2952. // compile_assert :: proc(cond: bool) -> bool
  2953. if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
  2954. gbString str = expr_to_string(ce->args.e[0]);
  2955. error_node(call, "`%s` is not a constant boolean", str);
  2956. gb_string_free(str);
  2957. return false;
  2958. }
  2959. if (!operand->value.value_bool) {
  2960. gbString str = expr_to_string(ce->args.e[0]);
  2961. error_node(call, "Compile time assertion: `%s`", str);
  2962. gb_string_free(str);
  2963. }
  2964. operand->mode = Addressing_Constant;
  2965. operand->type = t_untyped_bool;
  2966. break;
  2967. case BuiltinProc_assert:
  2968. // assert :: proc(cond: bool) -> bool
  2969. if (!is_type_boolean(operand->type)) {
  2970. gbString str = expr_to_string(ce->args.e[0]);
  2971. error_node(call, "`%s` is not a boolean", str);
  2972. gb_string_free(str);
  2973. return false;
  2974. }
  2975. operand->mode = Addressing_Value;
  2976. operand->type = t_untyped_bool;
  2977. break;
  2978. case BuiltinProc_panic:
  2979. // panic :: proc(msg: string)
  2980. if (!is_type_string(operand->type)) {
  2981. gbString str = expr_to_string(ce->args.e[0]);
  2982. error_node(call, "`%s` is not a string", str);
  2983. gb_string_free(str);
  2984. return false;
  2985. }
  2986. operand->mode = Addressing_NoValue;
  2987. break;
  2988. case BuiltinProc_copy: {
  2989. // copy :: proc(x, y: []Type) -> int
  2990. Type *dest_type = NULL, *src_type = NULL;
  2991. Type *d = base_type(operand->type);
  2992. if (d->kind == Type_Slice) {
  2993. dest_type = d->Slice.elem;
  2994. }
  2995. Operand op = {0};
  2996. check_expr(c, &op, ce->args.e[1]);
  2997. if (op.mode == Addressing_Invalid) {
  2998. return false;
  2999. }
  3000. Type *s = base_type(op.type);
  3001. if (s->kind == Type_Slice) {
  3002. src_type = s->Slice.elem;
  3003. }
  3004. if (dest_type == NULL || src_type == NULL) {
  3005. error_node(call, "`copy` only expects slices as arguments");
  3006. return false;
  3007. }
  3008. if (!are_types_identical(dest_type, src_type)) {
  3009. gbString d_arg = expr_to_string(ce->args.e[0]);
  3010. gbString s_arg = expr_to_string(ce->args.e[1]);
  3011. gbString d_str = type_to_string(dest_type);
  3012. gbString s_str = type_to_string(src_type);
  3013. error_node(call,
  3014. "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
  3015. d_arg, s_arg, d_str, s_str);
  3016. gb_string_free(s_str);
  3017. gb_string_free(d_str);
  3018. gb_string_free(s_arg);
  3019. gb_string_free(d_arg);
  3020. return false;
  3021. }
  3022. operand->type = t_int; // Returns number of elems copied
  3023. operand->mode = Addressing_Value;
  3024. } break;
  3025. case BuiltinProc_swizzle: {
  3026. // swizzle :: proc(v: {N}T, T..) -> {M}T
  3027. Type *vector_type = base_type(operand->type);
  3028. if (!is_type_vector(vector_type)) {
  3029. gbString type_str = type_to_string(operand->type);
  3030. error_node(call,
  3031. "You can only `swizzle` a vector, got `%s`",
  3032. type_str);
  3033. gb_string_free(type_str);
  3034. return false;
  3035. }
  3036. isize max_count = vector_type->Vector.count;
  3037. isize arg_count = 0;
  3038. for_array(i, ce->args) {
  3039. if (i == 0) {
  3040. continue;
  3041. }
  3042. AstNode *arg = ce->args.e[i];
  3043. Operand op = {0};
  3044. check_expr(c, &op, arg);
  3045. if (op.mode == Addressing_Invalid) {
  3046. return false;
  3047. }
  3048. Type *arg_type = base_type(op.type);
  3049. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  3050. error_node(op.expr, "Indices to `swizzle` must be constant integers");
  3051. return false;
  3052. }
  3053. if (op.value.value_integer < 0) {
  3054. error_node(op.expr, "Negative `swizzle` index");
  3055. return false;
  3056. }
  3057. if (max_count <= op.value.value_integer) {
  3058. error_node(op.expr, "`swizzle` index exceeds vector length");
  3059. return false;
  3060. }
  3061. arg_count++;
  3062. }
  3063. if (arg_count > max_count) {
  3064. error_node(call, "Too many `swizzle` indices, %td > %td", arg_count, max_count);
  3065. return false;
  3066. }
  3067. Type *elem_type = vector_type->Vector.elem;
  3068. operand->type = make_type_vector(c->allocator, elem_type, arg_count);
  3069. operand->mode = Addressing_Value;
  3070. } break;
  3071. case BuiltinProc_slice_ptr: {
  3072. // slice_ptr :: proc(a: ^T, len: int) -> []T
  3073. // slice_ptr :: proc(a: ^T, len, cap: int) -> []T
  3074. // ^T cannot be rawptr
  3075. Type *ptr_type = base_type(operand->type);
  3076. if (!is_type_pointer(ptr_type)) {
  3077. gbString type_str = type_to_string(operand->type);
  3078. error_node(call,
  3079. "Expected a pointer to `slice_ptr`, got `%s`",
  3080. type_str);
  3081. gb_string_free(type_str);
  3082. return false;
  3083. }
  3084. if (ptr_type == t_rawptr) {
  3085. error_node(call,
  3086. "`rawptr` cannot have pointer arithmetic");
  3087. return false;
  3088. }
  3089. isize arg_count = ce->args.count;
  3090. if (arg_count < 2 || 3 < arg_count) {
  3091. error_node(ce->args.e[0], "`slice_ptr` expects 2 or 3 arguments, found %td", arg_count);
  3092. // NOTE(bill): Return the correct type to reduce errors
  3093. } else {
  3094. // If any are constant
  3095. i64 sizes[2] = {0};
  3096. isize size_count = 0;
  3097. for (isize i = 1; i < arg_count; i++) {
  3098. i64 val = 0;
  3099. bool ok = check_index_value(c, ce->args.e[i], -1, &val);
  3100. if (ok && val >= 0) {
  3101. GB_ASSERT(size_count < gb_count_of(sizes));
  3102. sizes[size_count++] = val;
  3103. }
  3104. }
  3105. if (size_count == 2 && sizes[0] > sizes[1]) {
  3106. error_node(ce->args.e[1], "`slice_ptr` count and capacity are swapped");
  3107. // No need quit
  3108. }
  3109. }
  3110. operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
  3111. operand->mode = Addressing_Value;
  3112. } break;
  3113. case BuiltinProc_slice_to_bytes: {
  3114. // slice_to_bytes :: proc(a: []T) -> []byte
  3115. Type *slice_type = base_type(operand->type);
  3116. if (!is_type_slice(slice_type)) {
  3117. gbString type_str = type_to_string(operand->type);
  3118. error_node(call, "Expected a slice type, got `%s`", type_str);
  3119. gb_string_free(type_str);
  3120. return false;
  3121. }
  3122. operand->type = t_byte_slice;
  3123. operand->mode = Addressing_Value;
  3124. } break;
  3125. case BuiltinProc_min: {
  3126. // min :: proc(a, b: comparable) -> comparable
  3127. Type *type = base_type(operand->type);
  3128. if (!is_type_comparable(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3129. gbString type_str = type_to_string(operand->type);
  3130. error_node(call, "Expected a comparable numeric type to `min`, got `%s`", type_str);
  3131. gb_string_free(type_str);
  3132. return false;
  3133. }
  3134. AstNode *other_arg = ce->args.e[1];
  3135. Operand a = *operand;
  3136. Operand b = {0};
  3137. check_expr(c, &b, other_arg);
  3138. if (b.mode == Addressing_Invalid) {
  3139. return false;
  3140. }
  3141. if (!is_type_comparable(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  3142. gbString type_str = type_to_string(b.type);
  3143. error_node(call,
  3144. "Expected a comparable numeric type to `min`, got `%s`",
  3145. type_str);
  3146. gb_string_free(type_str);
  3147. return false;
  3148. }
  3149. if (a.mode == Addressing_Constant &&
  3150. b.mode == Addressing_Constant) {
  3151. ExactValue x = a.value;
  3152. ExactValue y = b.value;
  3153. operand->mode = Addressing_Constant;
  3154. if (compare_exact_values(Token_Lt, x, y)) {
  3155. operand->value = x;
  3156. operand->type = a.type;
  3157. } else {
  3158. operand->value = y;
  3159. operand->type = b.type;
  3160. }
  3161. } else {
  3162. operand->mode = Addressing_Value;
  3163. operand->type = type;
  3164. convert_to_typed(c, &a, b.type, 0);
  3165. if (a.mode == Addressing_Invalid) {
  3166. return false;
  3167. }
  3168. convert_to_typed(c, &b, a.type, 0);
  3169. if (b.mode == Addressing_Invalid) {
  3170. return false;
  3171. }
  3172. if (!are_types_identical(a.type, b.type)) {
  3173. gbString type_a = type_to_string(a.type);
  3174. gbString type_b = type_to_string(b.type);
  3175. error_node(call,
  3176. "Mismatched types to `min`, `%s` vs `%s`",
  3177. type_a, type_b);
  3178. gb_string_free(type_b);
  3179. gb_string_free(type_a);
  3180. return false;
  3181. }
  3182. }
  3183. } break;
  3184. case BuiltinProc_max: {
  3185. // min :: proc(a, b: comparable) -> comparable
  3186. Type *type = base_type(operand->type);
  3187. if (!is_type_comparable(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3188. gbString type_str = type_to_string(operand->type);
  3189. error_node(call,
  3190. "Expected a comparable numeric or string type to `max`, got `%s`",
  3191. type_str);
  3192. gb_string_free(type_str);
  3193. return false;
  3194. }
  3195. AstNode *other_arg = ce->args.e[1];
  3196. Operand a = *operand;
  3197. Operand b = {0};
  3198. check_expr(c, &b, other_arg);
  3199. if (b.mode == Addressing_Invalid) {
  3200. return false;
  3201. }
  3202. if (!is_type_comparable(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  3203. gbString type_str = type_to_string(b.type);
  3204. error_node(call,
  3205. "Expected a comparable numeric or string type to `max`, got `%s`",
  3206. type_str);
  3207. gb_string_free(type_str);
  3208. return false;
  3209. }
  3210. if (a.mode == Addressing_Constant &&
  3211. b.mode == Addressing_Constant) {
  3212. ExactValue x = a.value;
  3213. ExactValue y = b.value;
  3214. operand->mode = Addressing_Constant;
  3215. if (compare_exact_values(Token_Gt, x, y)) {
  3216. operand->value = x;
  3217. operand->type = a.type;
  3218. } else {
  3219. operand->value = y;
  3220. operand->type = b.type;
  3221. }
  3222. } else {
  3223. operand->mode = Addressing_Value;
  3224. operand->type = type;
  3225. convert_to_typed(c, &a, b.type, 0);
  3226. if (a.mode == Addressing_Invalid) {
  3227. return false;
  3228. }
  3229. convert_to_typed(c, &b, a.type, 0);
  3230. if (b.mode == Addressing_Invalid) {
  3231. return false;
  3232. }
  3233. if (!are_types_identical(a.type, b.type)) {
  3234. gbString type_a = type_to_string(a.type);
  3235. gbString type_b = type_to_string(b.type);
  3236. error_node(call,
  3237. "Mismatched types to `max`, `%s` vs `%s`",
  3238. type_a, type_b);
  3239. gb_string_free(type_b);
  3240. gb_string_free(type_a);
  3241. return false;
  3242. }
  3243. }
  3244. } break;
  3245. case BuiltinProc_abs: {
  3246. // abs :: proc(n: numeric) -> numeric
  3247. Type *type = base_type(operand->type);
  3248. if (!is_type_numeric(type)) {
  3249. gbString type_str = type_to_string(operand->type);
  3250. error_node(call,
  3251. "Expected a numeric type to `abs`, got `%s`",
  3252. type_str);
  3253. gb_string_free(type_str);
  3254. return false;
  3255. }
  3256. if (operand->mode == Addressing_Constant) {
  3257. switch (operand->value.kind) {
  3258. case ExactValue_Integer:
  3259. operand->value.value_integer = gb_abs(operand->value.value_integer);
  3260. break;
  3261. case ExactValue_Float:
  3262. operand->value.value_float = gb_abs(operand->value.value_float);
  3263. break;
  3264. default:
  3265. GB_PANIC("Invalid numeric constant");
  3266. break;
  3267. }
  3268. } else {
  3269. operand->mode = Addressing_Value;
  3270. }
  3271. operand->type = type;
  3272. } break;
  3273. case BuiltinProc_clamp: {
  3274. // clamp :: proc(a, min, max: comparable) -> comparable
  3275. Type *type = base_type(operand->type);
  3276. if (!is_type_comparable(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3277. gbString type_str = type_to_string(operand->type);
  3278. error_node(call,
  3279. "Expected a comparable numeric or string type to `clamp`, got `%s`",
  3280. type_str);
  3281. gb_string_free(type_str);
  3282. return false;
  3283. }
  3284. AstNode *min_arg = ce->args.e[1];
  3285. AstNode *max_arg = ce->args.e[2];
  3286. Operand x = *operand;
  3287. Operand y = {0};
  3288. Operand z = {0};
  3289. check_expr(c, &y, min_arg);
  3290. if (y.mode == Addressing_Invalid) {
  3291. return false;
  3292. }
  3293. if (!is_type_comparable(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  3294. gbString type_str = type_to_string(y.type);
  3295. error_node(call,
  3296. "Expected a comparable numeric or string type to `clamp`, got `%s`",
  3297. type_str);
  3298. gb_string_free(type_str);
  3299. return false;
  3300. }
  3301. check_expr(c, &z, max_arg);
  3302. if (z.mode == Addressing_Invalid) {
  3303. return false;
  3304. }
  3305. if (!is_type_comparable(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  3306. gbString type_str = type_to_string(z.type);
  3307. error_node(call,
  3308. "Expected a comparable numeric or string type to `clamp`, got `%s`",
  3309. type_str);
  3310. gb_string_free(type_str);
  3311. return false;
  3312. }
  3313. if (x.mode == Addressing_Constant &&
  3314. y.mode == Addressing_Constant &&
  3315. z.mode == Addressing_Constant) {
  3316. ExactValue a = x.value;
  3317. ExactValue b = y.value;
  3318. ExactValue c = z.value;
  3319. operand->mode = Addressing_Constant;
  3320. if (compare_exact_values(Token_Lt, a, b)) {
  3321. operand->value = b;
  3322. operand->type = y.type;
  3323. } else if (compare_exact_values(Token_Gt, a, c)) {
  3324. operand->value = c;
  3325. operand->type = z.type;
  3326. } else {
  3327. operand->value = a;
  3328. operand->type = x.type;
  3329. }
  3330. } else {
  3331. operand->mode = Addressing_Value;
  3332. operand->type = type;
  3333. convert_to_typed(c, &x, y.type, 0);
  3334. if (x.mode == Addressing_Invalid) { return false; }
  3335. convert_to_typed(c, &y, x.type, 0);
  3336. if (y.mode == Addressing_Invalid) { return false; }
  3337. convert_to_typed(c, &x, z.type, 0);
  3338. if (x.mode == Addressing_Invalid) { return false; }
  3339. convert_to_typed(c, &z, x.type, 0);
  3340. if (z.mode == Addressing_Invalid) { return false; }
  3341. convert_to_typed(c, &y, z.type, 0);
  3342. if (y.mode == Addressing_Invalid) { return false; }
  3343. convert_to_typed(c, &z, y.type, 0);
  3344. if (z.mode == Addressing_Invalid) { return false; }
  3345. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  3346. gbString type_x = type_to_string(x.type);
  3347. gbString type_y = type_to_string(y.type);
  3348. gbString type_z = type_to_string(z.type);
  3349. error_node(call,
  3350. "Mismatched types to `clamp`, `%s`, `%s`, `%s`",
  3351. type_x, type_y, type_z);
  3352. gb_string_free(type_z);
  3353. gb_string_free(type_y);
  3354. gb_string_free(type_x);
  3355. return false;
  3356. }
  3357. }
  3358. } break;
  3359. }
  3360. return true;
  3361. }
  3362. typedef enum CallArgumentError {
  3363. CallArgumentError_None,
  3364. CallArgumentError_WrongTypes,
  3365. CallArgumentError_NonVariadicExpand,
  3366. CallArgumentError_VariadicTuple,
  3367. CallArgumentError_MultipleVariadicExpand,
  3368. CallArgumentError_ArgumentCount,
  3369. CallArgumentError_TooFewArguments,
  3370. CallArgumentError_TooManyArguments,
  3371. } CallArgumentError;
  3372. CallArgumentError check_call_arguments_internal(Checker *c, AstNode *call, Type *proc_type, Operand *operands, isize operand_count,
  3373. bool show_error, i64 *score_) {
  3374. ast_node(ce, CallExpr, call);
  3375. isize param_count = 0;
  3376. bool variadic = proc_type->Proc.variadic;
  3377. bool vari_expand = (ce->ellipsis.pos.line != 0);
  3378. i64 score = 0;
  3379. if (proc_type->Proc.params != NULL) {
  3380. param_count = proc_type->Proc.params->Tuple.variable_count;
  3381. if (variadic) {
  3382. param_count--;
  3383. }
  3384. }
  3385. if (vari_expand && !variadic) {
  3386. if (show_error) {
  3387. error(ce->ellipsis,
  3388. "Cannot use `..` in call to a non-variadic procedure: `%.*s`",
  3389. LIT(ce->proc->Ident.string));
  3390. }
  3391. if (score_) *score_ = score;
  3392. return CallArgumentError_NonVariadicExpand;
  3393. }
  3394. if (operand_count == 0 && param_count == 0) {
  3395. if (score_) *score_ = score;
  3396. return CallArgumentError_None;
  3397. }
  3398. i32 error_code = 0;
  3399. if (operand_count < param_count) {
  3400. error_code = -1;
  3401. } else if (!variadic && operand_count > param_count) {
  3402. error_code = +1;
  3403. }
  3404. if (error_code != 0) {
  3405. CallArgumentError err = CallArgumentError_TooManyArguments;
  3406. char *err_fmt = "Too many arguments for `%s`, expected %td arguments";
  3407. if (error_code < 0) {
  3408. err = CallArgumentError_TooFewArguments;
  3409. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  3410. }
  3411. if (show_error) {
  3412. gbString proc_str = expr_to_string(ce->proc);
  3413. error_node(call, err_fmt, proc_str, param_count);
  3414. gb_string_free(proc_str);
  3415. }
  3416. if (score_) *score_ = score;
  3417. return err;
  3418. }
  3419. bool err = CallArgumentError_None;
  3420. GB_ASSERT(proc_type->Proc.params != NULL);
  3421. Entity **sig_params = proc_type->Proc.params->Tuple.variables;
  3422. isize operand_index = 0;
  3423. for (; operand_index < param_count; operand_index++) {
  3424. Type *t = sig_params[operand_index]->type;
  3425. Operand o = operands[operand_index];
  3426. if (variadic) {
  3427. o = operands[operand_index];
  3428. }
  3429. i64 s = 0;
  3430. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  3431. if (show_error) {
  3432. check_assignment(c, &o, t, str_lit("argument"));
  3433. }
  3434. err = CallArgumentError_WrongTypes;
  3435. }
  3436. score += s;
  3437. }
  3438. if (variadic) {
  3439. bool variadic_expand = false;
  3440. Type *slice = sig_params[param_count]->type;
  3441. GB_ASSERT(is_type_slice(slice));
  3442. Type *elem = base_type(slice)->Slice.elem;
  3443. Type *t = elem;
  3444. for (; operand_index < operand_count; operand_index++) {
  3445. Operand o = operands[operand_index];
  3446. if (vari_expand) {
  3447. variadic_expand = true;
  3448. t = slice;
  3449. if (operand_index != param_count) {
  3450. if (show_error) {
  3451. error_node(o.expr, "`..` in a variadic procedure can only have one variadic argument at the end");
  3452. }
  3453. if (score_) *score_ = score;
  3454. return CallArgumentError_MultipleVariadicExpand;
  3455. }
  3456. }
  3457. i64 s = 0;
  3458. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  3459. if (show_error) {
  3460. check_assignment(c, &o, t, str_lit("argument"));
  3461. }
  3462. err = CallArgumentError_WrongTypes;
  3463. }
  3464. score += s;
  3465. }
  3466. }
  3467. if (score_) *score_ = score;
  3468. return err;
  3469. }
  3470. typedef struct ValidProcAndScore {
  3471. isize index;
  3472. i64 score;
  3473. } ValidProcAndScore;
  3474. int valid_proc_and_score_cmp(void const *a, void const *b) {
  3475. i64 si = (cast(ValidProcAndScore const *)a)->score;
  3476. i64 sj = (cast(ValidProcAndScore const *)b)->score;
  3477. return sj < si ? -1 : sj > si;
  3478. }
  3479. typedef Array(Operand) ArrayOperand;
  3480. bool check_unpack_arguments(Checker *c, isize lhs_count, ArrayOperand *operands, AstNodeArray rhs, bool allow_ok) {
  3481. bool optional_ok = false;
  3482. for_array(i, rhs) {
  3483. Operand o = {0};
  3484. check_multi_expr(c, &o, rhs.e[i]);
  3485. if (o.type == NULL || o.type->kind != Type_Tuple) {
  3486. if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
  3487. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  3488. Type *tuple = make_optional_ok_type(c->allocator, o.type);
  3489. add_type_and_value(&c->info, o.expr, o.mode, tuple, o.value);
  3490. Operand val = o;
  3491. Operand ok = o;
  3492. val.mode = Addressing_Value;
  3493. ok.mode = Addressing_Value;
  3494. ok.type = t_bool;
  3495. array_add(operands, val);
  3496. array_add(operands, ok);
  3497. optional_ok = true;
  3498. } else {
  3499. array_add(operands, o);
  3500. }
  3501. } else {
  3502. TypeTuple *tuple = &o.type->Tuple;
  3503. for (isize j = 0; j < tuple->variable_count; j++) {
  3504. o.type = tuple->variables[j]->type;
  3505. array_add(operands, o);
  3506. }
  3507. }
  3508. }
  3509. return optional_ok;
  3510. }
  3511. Type *check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  3512. GB_ASSERT(call->kind == AstNode_CallExpr);
  3513. ast_node(ce, CallExpr, call);
  3514. ArrayOperand operands;
  3515. array_init_reserve(&operands, heap_allocator(), 2*ce->args.count);
  3516. check_unpack_arguments(c, -1, &operands, ce->args, false);
  3517. if (operand->mode == Addressing_Overload) {
  3518. GB_ASSERT(operand->overload_entities != NULL &&
  3519. operand->overload_count > 0);
  3520. isize overload_count = operand->overload_count;
  3521. Entity ** procs = operand->overload_entities;
  3522. ValidProcAndScore *valids = gb_alloc_array(heap_allocator(), ValidProcAndScore, overload_count);
  3523. isize valid_count = 0;
  3524. String name = procs[0]->token.string;
  3525. for (isize i = 0; i < overload_count; i++) {
  3526. Entity *e = procs[i];
  3527. DeclInfo **found = map_decl_info_get(&c->info.entities, hash_pointer(e));
  3528. GB_ASSERT(found != NULL);
  3529. DeclInfo *d = *found;
  3530. check_entity_decl(c, e, d, NULL);
  3531. }
  3532. for (isize i = 0; i < overload_count; i++) {
  3533. Entity *p = procs[i];
  3534. Type *proc_type = base_type(p->type);
  3535. if (proc_type != NULL && is_type_proc(proc_type)) {
  3536. i64 score = 0;
  3537. CallArgumentError err = check_call_arguments_internal(c, call, proc_type, operands.e, operands.count, false, &score);
  3538. if (err == CallArgumentError_None) {
  3539. valids[valid_count].index = i;
  3540. valids[valid_count].score = score;
  3541. valid_count++;
  3542. }
  3543. }
  3544. }
  3545. if (valid_count > 1) {
  3546. gb_sort_array(valids, valid_count, valid_proc_and_score_cmp);
  3547. i64 best_score = valids[0].score;
  3548. for (isize i = 0; i < valid_count; i++) {
  3549. if (best_score > valids[i].score) {
  3550. valid_count = i;
  3551. break;
  3552. }
  3553. best_score = valids[i].score;
  3554. }
  3555. }
  3556. if (valid_count == 0) {
  3557. error_node(operand->expr, "No overloads for `%.*s` that match with the given arguments", LIT(name));
  3558. proc_type = t_invalid;
  3559. } else if (valid_count > 1) {
  3560. error_node(operand->expr, "Ambiguous procedure call `%.*s`, could be:", LIT(name));
  3561. for (isize i = 0; i < valid_count; i++) {
  3562. Entity *proc = procs[valids[i].index];
  3563. TokenPos pos = proc->token.pos;
  3564. gbString pt = type_to_string(proc->type);
  3565. gb_printf_err("\t%.*s :: %s at %.*s(%td:%td)\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column);
  3566. gb_string_free(pt);
  3567. }
  3568. proc_type = t_invalid;
  3569. } else {
  3570. AstNode *expr = operand->expr;
  3571. while (expr->kind == AstNode_SelectorExpr) {
  3572. expr = expr->SelectorExpr.selector;
  3573. }
  3574. GB_ASSERT(expr->kind == AstNode_Ident);
  3575. Entity *e = procs[valids[0].index];
  3576. add_entity_use(c, expr, e);
  3577. proc_type = e->type;
  3578. i64 score = 0;
  3579. CallArgumentError err = check_call_arguments_internal(c, call, proc_type, operands.e, operands.count, true, &score);
  3580. }
  3581. gb_free(heap_allocator(), valids);
  3582. gb_free(heap_allocator(), procs);
  3583. } else {
  3584. i64 score = 0;
  3585. CallArgumentError err = check_call_arguments_internal(c, call, proc_type, operands.e, operands.count, true, &score);
  3586. array_free(&operands);
  3587. }
  3588. return proc_type;
  3589. }
  3590. Entity *find_using_index_expr(Type *t) {
  3591. t = base_type(t);
  3592. if (t->kind != Type_Record) {
  3593. return NULL;
  3594. }
  3595. for (isize i = 0; i < t->Record.field_count; i++) {
  3596. Entity *f = t->Record.fields[i];
  3597. if (f->kind == Entity_Variable &&
  3598. f->flags & (EntityFlag_Anonymous|EntityFlag_Field)) {
  3599. if (is_type_indexable(f->type)) {
  3600. return f;
  3601. }
  3602. Entity *res = find_using_index_expr(f->type);
  3603. if (res != NULL) {
  3604. return res;
  3605. }
  3606. }
  3607. }
  3608. return NULL;
  3609. }
  3610. ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  3611. GB_ASSERT(call->kind == AstNode_CallExpr);
  3612. ast_node(ce, CallExpr, call);
  3613. check_expr_or_type(c, operand, ce->proc);
  3614. if (operand->mode == Addressing_Invalid) {
  3615. for_array(i, ce->args) {
  3616. check_expr_base(c, operand, ce->args.e[i], NULL);
  3617. }
  3618. operand->mode = Addressing_Invalid;
  3619. operand->expr = call;
  3620. return Expr_Stmt;
  3621. }
  3622. if (operand->mode == Addressing_Type) {
  3623. gbString str = type_to_string(operand->type);
  3624. error_node(call, "Expected a procedure, got a type `%s`", str);
  3625. gb_string_free(str);
  3626. operand->mode = Addressing_Invalid;
  3627. operand->expr = call;
  3628. return Expr_Stmt;
  3629. #if 0
  3630. Type *t = operand->type;
  3631. gbString str = type_to_string(t);
  3632. operand->mode = Addressing_Invalid;
  3633. isize arg_count = ce->args.count;
  3634. switch (arg_count) {
  3635. case 0: error_node(call, "Missing argument in convertion to `%s`", str); break;
  3636. default: error_node(call, "Too many arguments in convertion to `%s`", str); break;
  3637. case 1:
  3638. check_expr(c, operand, ce->args.e[0]);
  3639. if (operand->mode != Addressing_Invalid) {
  3640. check_cast(c, operand, t);
  3641. }
  3642. break;
  3643. }
  3644. gb_string_free(str);
  3645. return Expr_Expr;
  3646. #endif
  3647. }
  3648. if (operand->mode == Addressing_Builtin) {
  3649. i32 id = operand->builtin_id;
  3650. if (!check_builtin_procedure(c, operand, call, id)) {
  3651. operand->mode = Addressing_Invalid;
  3652. }
  3653. operand->expr = call;
  3654. return builtin_procs[id].kind;
  3655. }
  3656. Type *proc_type = base_type(operand->type);
  3657. if (operand->mode != Addressing_Overload) {
  3658. bool valid_type = (proc_type != NULL) && is_type_proc(proc_type);
  3659. bool valid_mode = (operand->mode == Addressing_Value) || (operand->mode == Addressing_Variable);
  3660. if (!valid_type || !valid_mode) {
  3661. AstNode *e = operand->expr;
  3662. gbString str = expr_to_string(e);
  3663. error_node(e, "Cannot call a non-procedure: `%s` %d", str, operand->mode);
  3664. gb_string_free(str);
  3665. operand->mode = Addressing_Invalid;
  3666. operand->expr = call;
  3667. return Expr_Stmt;
  3668. }
  3669. }
  3670. proc_type = check_call_arguments(c, operand, proc_type, call);
  3671. gb_zero_item(operand);
  3672. Type *pt = base_type(proc_type);
  3673. if (pt == NULL || !is_type_proc(pt)) {
  3674. operand->mode = Addressing_Invalid;
  3675. operand->type = t_invalid;
  3676. operand->expr = call;
  3677. return Expr_Stmt;
  3678. }
  3679. switch (pt->Proc.result_count) {
  3680. case 0:
  3681. operand->mode = Addressing_NoValue;
  3682. break;
  3683. case 1:
  3684. operand->mode = Addressing_Value;
  3685. operand->type = pt->Proc.results->Tuple.variables[0]->type;
  3686. break;
  3687. default:
  3688. operand->mode = Addressing_Value;
  3689. operand->type = pt->Proc.results;
  3690. break;
  3691. }
  3692. operand->expr = call;
  3693. return Expr_Stmt;
  3694. }
  3695. ExprKind check_macro_call_expr(Checker *c, Operand *operand, AstNode *call) {
  3696. GB_ASSERT(call->kind == AstNode_MacroCallExpr);
  3697. ast_node(mce, MacroCallExpr, call);
  3698. error_node(call, "Macro call expressions are not yet supported");
  3699. operand->mode = Addressing_Invalid;
  3700. operand->expr = call;
  3701. return Expr_Stmt;
  3702. }
  3703. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  3704. check_expr_base(c, o, e, t);
  3705. check_not_tuple(c, o);
  3706. char *err_str = NULL;
  3707. switch (o->mode) {
  3708. case Addressing_NoValue:
  3709. err_str = "used as a value";
  3710. break;
  3711. case Addressing_Type:
  3712. err_str = "is not an expression";
  3713. break;
  3714. case Addressing_Builtin:
  3715. err_str = "must be called";
  3716. break;
  3717. }
  3718. if (err_str != NULL) {
  3719. gbString str = expr_to_string(e);
  3720. error_node(e, "`%s` %s", str, err_str);
  3721. gb_string_free(str);
  3722. o->mode = Addressing_Invalid;
  3723. }
  3724. }
  3725. void check_set_mode_with_indirection(Operand *o, bool indirection) {
  3726. if (o->mode != Addressing_Immutable) {
  3727. if (indirection) {
  3728. o->mode = Addressing_Variable;
  3729. } else if (o->mode != Addressing_Variable &&
  3730. o->mode != Addressing_Constant) {
  3731. o->mode = Addressing_Value;
  3732. }
  3733. }
  3734. }
  3735. bool check_set_index_data(Operand *o, Type *type, bool indirection, i64 *max_count) {
  3736. Type *t = base_type(type_deref(type));
  3737. switch (t->kind) {
  3738. case Type_Basic:
  3739. if (is_type_string(t)) {
  3740. if (o->mode == Addressing_Constant) {
  3741. *max_count = o->value.value_string.len;
  3742. }
  3743. check_set_mode_with_indirection(o, indirection);
  3744. o->type = t_u8;
  3745. return true;
  3746. }
  3747. break;
  3748. case Type_Array:
  3749. *max_count = t->Array.count;
  3750. check_set_mode_with_indirection(o, indirection);
  3751. o->type = t->Array.elem;
  3752. return true;
  3753. case Type_Vector:
  3754. *max_count = t->Vector.count;
  3755. check_set_mode_with_indirection(o, indirection);
  3756. o->type = t->Vector.elem;
  3757. return true;
  3758. case Type_Slice:
  3759. o->type = t->Slice.elem;
  3760. if (o->mode != Addressing_Immutable) {
  3761. o->mode = Addressing_Variable;
  3762. }
  3763. return true;
  3764. case Type_DynamicArray:
  3765. o->type = t->DynamicArray.elem;
  3766. check_set_mode_with_indirection(o, indirection);
  3767. return true;
  3768. }
  3769. return false;
  3770. }
  3771. ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  3772. ExprKind kind = Expr_Stmt;
  3773. o->mode = Addressing_Invalid;
  3774. o->type = t_invalid;
  3775. switch (node->kind) {
  3776. default:
  3777. return kind;
  3778. case_ast_node(be, BadExpr, node)
  3779. return kind;
  3780. case_end;
  3781. case_ast_node(i, IntervalExpr, node);
  3782. error_node(node, "Invalid use of an interval expression");
  3783. return kind;
  3784. case_end;
  3785. case_ast_node(i, Implicit, node)
  3786. switch (i->kind) {
  3787. case Token_context:
  3788. if (c->context.proc_name.len == 0) {
  3789. error_node(node, "`context` is only allowed within procedures");
  3790. return kind;
  3791. }
  3792. o->mode = Addressing_Value;
  3793. o->type = t_context;
  3794. break;
  3795. default:
  3796. error_node(node, "Illegal implicit name `%.*s`", LIT(i->string));
  3797. return kind;
  3798. }
  3799. case_end;
  3800. case_ast_node(i, Ident, node);
  3801. check_ident(c, o, node, NULL, type_hint, false);
  3802. case_end;
  3803. case_ast_node(bl, BasicLit, node);
  3804. Type *t = t_invalid;
  3805. switch (bl->kind) {
  3806. case Token_Integer: t = t_untyped_integer; break;
  3807. case Token_Float: t = t_untyped_float; break;
  3808. case Token_String: t = t_untyped_string; break;
  3809. case Token_Rune: t = t_untyped_rune; break;
  3810. default: GB_PANIC("Unknown literal"); break;
  3811. }
  3812. o->mode = Addressing_Constant;
  3813. o->type = t;
  3814. o->value = exact_value_from_basic_literal(*bl);
  3815. case_end;
  3816. case_ast_node(bd, BasicDirective, node);
  3817. if (str_eq(bd->name, str_lit("file"))) {
  3818. o->type = t_untyped_string;
  3819. o->value = exact_value_string(bd->token.pos.file);
  3820. } else if (str_eq(bd->name, str_lit("line"))) {
  3821. o->type = t_untyped_integer;
  3822. o->value = exact_value_integer(bd->token.pos.line);
  3823. } else if (str_eq(bd->name, str_lit("procedure"))) {
  3824. if (c->proc_stack.count == 0) {
  3825. error_node(node, "#procedure may only be used within procedures");
  3826. o->type = t_untyped_string;
  3827. o->value = exact_value_string(str_lit(""));
  3828. } else {
  3829. o->type = t_untyped_string;
  3830. o->value = exact_value_string(c->context.proc_name);
  3831. }
  3832. } else {
  3833. GB_PANIC("Unknown basic basic directive");
  3834. }
  3835. o->mode = Addressing_Constant;
  3836. case_end;
  3837. case_ast_node(pl, ProcLit, node);
  3838. Type *type = check_type(c, pl->type);
  3839. if (type == NULL || !is_type_proc(type)) {
  3840. gbString str = expr_to_string(node);
  3841. error_node(node, "Invalid procedure literal `%s`", str);
  3842. gb_string_free(str);
  3843. check_close_scope(c);
  3844. return kind;
  3845. }
  3846. if (pl->tags != 0) {
  3847. error_node(node, "A procedure literal cannot have tags");
  3848. pl->tags = 0; // TODO(bill): Should I zero this?!
  3849. }
  3850. check_open_scope(c, pl->type);
  3851. check_procedure_later(c, c->curr_ast_file, empty_token, c->context.decl, type, pl->body, pl->tags);
  3852. // check_proc_body(c, empty_token, c->context.decl, type, pl->body);
  3853. check_close_scope(c);
  3854. o->mode = Addressing_Value;
  3855. o->type = type;
  3856. case_end;
  3857. case_ast_node(te, TernaryExpr, node);
  3858. Operand cond = {Addressing_Invalid};
  3859. check_expr(c, &cond, te->cond);
  3860. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  3861. error_node(te->cond, "Non-boolean condition in if expression");
  3862. }
  3863. Operand x = {Addressing_Invalid};
  3864. Operand y = {Addressing_Invalid};
  3865. check_expr_with_type_hint(c, &x, te->x, type_hint);
  3866. if (te->y != NULL) {
  3867. check_expr_with_type_hint(c, &y, te->y, type_hint);
  3868. } else {
  3869. error_node(node, "A ternary expression must have an else clause");
  3870. return kind;
  3871. }
  3872. if (x.type == NULL || x.type == t_invalid ||
  3873. y.type == NULL || y.type == t_invalid) {
  3874. return kind;
  3875. }
  3876. convert_to_typed(c, &x, y.type, 0);
  3877. if (x.mode == Addressing_Invalid) {
  3878. return kind;
  3879. }
  3880. convert_to_typed(c, &y, x.type, 0);
  3881. if (y.mode == Addressing_Invalid) {
  3882. x.mode = Addressing_Invalid;
  3883. return kind;
  3884. }
  3885. if (!are_types_identical(x.type, y.type)) {
  3886. gbString its = type_to_string(x.type);
  3887. gbString ets = type_to_string(y.type);
  3888. error_node(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
  3889. gb_string_free(ets);
  3890. gb_string_free(its);
  3891. return kind;
  3892. }
  3893. o->type = x.type;
  3894. o->mode = Addressing_Value;
  3895. if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  3896. x.mode == Addressing_Constant &&
  3897. y.mode == Addressing_Constant) {
  3898. o->mode = Addressing_Constant;
  3899. if (cond.value.value_bool) {
  3900. o->value = x.value;
  3901. } else {
  3902. o->value = y.value;
  3903. }
  3904. }
  3905. case_end;
  3906. case_ast_node(cl, CompoundLit, node);
  3907. Type *type = type_hint;
  3908. bool is_to_be_determined_array_count = false;
  3909. bool is_constant = true;
  3910. if (cl->type != NULL) {
  3911. type = NULL;
  3912. // [..]Type
  3913. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
  3914. AstNode *count = cl->type->ArrayType.count;
  3915. if (count->kind == AstNode_UnaryExpr &&
  3916. count->UnaryExpr.op.kind == Token_Ellipsis) {
  3917. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  3918. is_to_be_determined_array_count = true;
  3919. }
  3920. }
  3921. if (type == NULL) {
  3922. type = check_type(c, cl->type);
  3923. }
  3924. }
  3925. if (type == NULL) {
  3926. error_node(node, "Missing type in compound literal");
  3927. return kind;
  3928. }
  3929. Type *t = base_type(type);
  3930. switch (t->kind) {
  3931. case Type_Record: {
  3932. if (!is_type_struct(t) && !is_type_union(t)) {
  3933. if (cl->elems.count != 0) {
  3934. error_node(node, "Illegal compound literal");
  3935. }
  3936. break;
  3937. }
  3938. if (is_type_union(t)) {
  3939. is_constant = false;
  3940. }
  3941. if (cl->elems.count == 0) {
  3942. break; // NOTE(bill): No need to init
  3943. }
  3944. { // Checker values
  3945. isize field_count = t->Record.field_count;
  3946. if (cl->elems.e[0]->kind == AstNode_FieldValue) {
  3947. bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
  3948. for_array(i, cl->elems) {
  3949. AstNode *elem = cl->elems.e[i];
  3950. if (elem->kind != AstNode_FieldValue) {
  3951. error_node(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  3952. continue;
  3953. }
  3954. ast_node(fv, FieldValue, elem);
  3955. if (fv->field->kind != AstNode_Ident) {
  3956. gbString expr_str = expr_to_string(fv->field);
  3957. error_node(elem, "Invalid field name `%s` in structure literal", expr_str);
  3958. gb_string_free(expr_str);
  3959. continue;
  3960. }
  3961. String name = fv->field->Ident.string;
  3962. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  3963. bool is_unknown = sel.entity == NULL;
  3964. if (is_unknown) {
  3965. error_node(elem, "Unknown field `%.*s` in structure literal", LIT(name));
  3966. continue;
  3967. }
  3968. if (!is_unknown && !check_is_field_exported(c, sel.entity)) {
  3969. error_node(elem, "Cannot assign to an unexported field `%.*s` in structure literal", LIT(name));
  3970. continue;
  3971. }
  3972. if (sel.index.count > 1) {
  3973. error_node(elem, "Cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
  3974. continue;
  3975. }
  3976. Entity *field = t->Record.fields[sel.index.e[0]];
  3977. add_entity_use(c, fv->field, field);
  3978. if (fields_visited[sel.index.e[0]]) {
  3979. error_node(elem, "Duplicate field `%.*s` in structure literal", LIT(name));
  3980. continue;
  3981. }
  3982. fields_visited[sel.index.e[0]] = true;
  3983. check_expr(c, o, fv->value);
  3984. if (base_type(field->type) == t_any) {
  3985. is_constant = false;
  3986. }
  3987. if (is_constant) {
  3988. is_constant = o->mode == Addressing_Constant;
  3989. }
  3990. check_assignment(c, o, field->type, str_lit("structure literal"));
  3991. }
  3992. } else {
  3993. for_array(index, cl->elems) {
  3994. AstNode *elem = cl->elems.e[index];
  3995. if (elem->kind == AstNode_FieldValue) {
  3996. error_node(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  3997. continue;
  3998. }
  3999. Entity *field = t->Record.fields_in_src_order[index];
  4000. check_expr(c, o, elem);
  4001. if (index >= field_count) {
  4002. error_node(o->expr, "Too many values in structure literal, expected %td", field_count);
  4003. break;
  4004. }
  4005. if (!check_is_field_exported(c, field)) {
  4006. gbString t = type_to_string(type);
  4007. error_node(o->expr, "Implicit assignment to an unexported field `%.*s` in `%s` literal",
  4008. LIT(field->token.string), t);
  4009. gb_string_free(t);
  4010. continue;
  4011. }
  4012. if (base_type(field->type) == t_any) {
  4013. is_constant = false;
  4014. }
  4015. if (is_constant) {
  4016. is_constant = o->mode == Addressing_Constant;
  4017. }
  4018. check_assignment(c, o, field->type, str_lit("structure literal"));
  4019. }
  4020. if (cl->elems.count < field_count) {
  4021. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  4022. }
  4023. }
  4024. }
  4025. } break;
  4026. case Type_Slice:
  4027. case Type_Array:
  4028. case Type_Vector:
  4029. case Type_DynamicArray:
  4030. {
  4031. Type *elem_type = NULL;
  4032. String context_name = {0};
  4033. i64 max_type_count = -1;
  4034. if (t->kind == Type_Slice) {
  4035. elem_type = t->Slice.elem;
  4036. context_name = str_lit("slice literal");
  4037. } else if (t->kind == Type_Vector) {
  4038. elem_type = t->Vector.elem;
  4039. context_name = str_lit("vector literal");
  4040. max_type_count = t->Vector.count;
  4041. } else if (t->kind == Type_Array) {
  4042. elem_type = t->Array.elem;
  4043. context_name = str_lit("array literal");
  4044. max_type_count = t->Array.count;
  4045. } else if (t->kind == Type_DynamicArray) {
  4046. elem_type = t->DynamicArray.elem;
  4047. context_name = str_lit("dynamic array literal");
  4048. is_constant = false;
  4049. } else {
  4050. GB_PANIC("unreachable");
  4051. }
  4052. i64 max = 0;
  4053. isize index = 0;
  4054. isize elem_count = cl->elems.count;
  4055. if (base_type(elem_type) == t_any) {
  4056. is_constant = false;
  4057. }
  4058. for (; index < elem_count; index++) {
  4059. AstNode *e = cl->elems.e[index];
  4060. if (e->kind == AstNode_FieldValue) {
  4061. error_node(e,
  4062. "`field = value` is only allowed in struct literals");
  4063. continue;
  4064. }
  4065. if (0 <= max_type_count && max_type_count <= index) {
  4066. error_node(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  4067. }
  4068. Operand operand = {0};
  4069. check_expr_with_type_hint(c, &operand, e, elem_type);
  4070. check_assignment(c, &operand, elem_type, context_name);
  4071. if (is_constant) {
  4072. is_constant = operand.mode == Addressing_Constant;
  4073. }
  4074. }
  4075. if (max < index) {
  4076. max = index;
  4077. }
  4078. if (t->kind == Type_Vector) {
  4079. if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
  4080. error_node(cl->elems.e[0], "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
  4081. }
  4082. }
  4083. if (t->kind == Type_Array && is_to_be_determined_array_count) {
  4084. t->Array.count = max;
  4085. }
  4086. } break;
  4087. case Type_Basic: {
  4088. if (!is_type_any(t)) {
  4089. if (cl->elems.count != 0) {
  4090. error_node(node, "Illegal compound literal");
  4091. }
  4092. break;
  4093. }
  4094. if (cl->elems.count == 0) {
  4095. break; // NOTE(bill): No need to init
  4096. }
  4097. { // Checker values
  4098. Type *field_types[2] = {t_type_info_ptr, t_rawptr};
  4099. isize field_count = 2;
  4100. if (cl->elems.e[0]->kind == AstNode_FieldValue) {
  4101. bool fields_visited[2] = {0};
  4102. for_array(i, cl->elems) {
  4103. AstNode *elem = cl->elems.e[i];
  4104. if (elem->kind != AstNode_FieldValue) {
  4105. error_node(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  4106. continue;
  4107. }
  4108. ast_node(fv, FieldValue, elem);
  4109. if (fv->field->kind != AstNode_Ident) {
  4110. gbString expr_str = expr_to_string(fv->field);
  4111. error_node(elem, "Invalid field name `%s` in `any` literal", expr_str);
  4112. gb_string_free(expr_str);
  4113. continue;
  4114. }
  4115. String name = fv->field->Ident.string;
  4116. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  4117. if (sel.entity == NULL) {
  4118. error_node(elem, "Unknown field `%.*s` in `any` literal", LIT(name));
  4119. continue;
  4120. }
  4121. isize index = sel.index.e[0];
  4122. if (fields_visited[index]) {
  4123. error_node(elem, "Duplicate field `%.*s` in `any` literal", LIT(name));
  4124. continue;
  4125. }
  4126. fields_visited[index] = true;
  4127. check_expr(c, o, fv->value);
  4128. // NOTE(bill): `any` literals can never be constant
  4129. is_constant = false;
  4130. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  4131. }
  4132. } else {
  4133. for_array(index, cl->elems) {
  4134. AstNode *elem = cl->elems.e[index];
  4135. if (elem->kind == AstNode_FieldValue) {
  4136. error_node(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  4137. continue;
  4138. }
  4139. check_expr(c, o, elem);
  4140. if (index >= field_count) {
  4141. error_node(o->expr, "Too many values in `any` literal, expected %td", field_count);
  4142. break;
  4143. }
  4144. // NOTE(bill): `any` literals can never be constant
  4145. is_constant = false;
  4146. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  4147. }
  4148. if (cl->elems.count < field_count) {
  4149. error(cl->close, "Too few values in `any` literal, expected %td, got %td", field_count, cl->elems.count);
  4150. }
  4151. }
  4152. }
  4153. } break;
  4154. case Type_Map: {
  4155. if (cl->elems.count == 0) {
  4156. break;
  4157. }
  4158. is_constant = false;
  4159. { // Checker values
  4160. for_array(i, cl->elems) {
  4161. AstNode *elem = cl->elems.e[i];
  4162. if (elem->kind != AstNode_FieldValue) {
  4163. error_node(elem, "Only `field = value` elements are allowed in a map literal");
  4164. continue;
  4165. }
  4166. ast_node(fv, FieldValue, elem);
  4167. check_expr_with_type_hint(c, o, fv->field, t->Map.key);
  4168. check_assignment(c, o, t->Map.key, str_lit("map literal"));
  4169. if (o->mode == Addressing_Invalid) {
  4170. continue;
  4171. }
  4172. check_expr_with_type_hint(c, o, fv->value, t->Map.value);
  4173. check_assignment(c, o, t->Map.value, str_lit("map literal"));
  4174. }
  4175. }
  4176. } break;
  4177. default: {
  4178. gbString str = type_to_string(type);
  4179. error_node(node, "Invalid compound literal type `%s`", str);
  4180. gb_string_free(str);
  4181. return kind;
  4182. } break;
  4183. }
  4184. if (is_constant) {
  4185. o->mode = Addressing_Constant;
  4186. o->value = exact_value_compound(node);
  4187. } else {
  4188. o->mode = Addressing_Value;
  4189. }
  4190. o->type = type;
  4191. case_end;
  4192. case_ast_node(pe, ParenExpr, node);
  4193. kind = check_expr_base(c, o, pe->expr, type_hint);
  4194. o->expr = node;
  4195. case_end;
  4196. case_ast_node(te, TagExpr, node);
  4197. String name = te->name.string;
  4198. error_node(node, "Unknown tag expression, #%.*s", LIT(name));
  4199. if (te->expr) {
  4200. kind = check_expr_base(c, o, te->expr, type_hint);
  4201. }
  4202. o->expr = node;
  4203. case_end;
  4204. case_ast_node(re, RunExpr, node);
  4205. // TODO(bill): Tag expressions
  4206. kind = check_expr_base(c, o, re->expr, type_hint);
  4207. o->expr = node;
  4208. case_end;
  4209. case_ast_node(ce, CastExpr, node);
  4210. Type *t = check_type(c, ce->type);
  4211. check_expr(c, o, ce->expr);
  4212. if (o->mode == Addressing_Invalid) {
  4213. o->expr = node;
  4214. return kind;
  4215. }
  4216. switch (ce->token.kind) {
  4217. case Token_cast:
  4218. check_cast(c, o, t);
  4219. break;
  4220. case Token_transmute: {
  4221. if (o->mode == Addressing_Constant) {
  4222. gbString expr_str = expr_to_string(o->expr);
  4223. error_node(o->expr, "Cannot transmute constant expression: `%s`", expr_str);
  4224. gb_string_free(expr_str);
  4225. o->mode = Addressing_Invalid;
  4226. o->expr = node;
  4227. return kind;
  4228. }
  4229. if (is_type_untyped(o->type)) {
  4230. gbString expr_str = expr_to_string(o->expr);
  4231. error_node(o->expr, "Cannot transmute untyped expression: `%s`", expr_str);
  4232. gb_string_free(expr_str);
  4233. o->mode = Addressing_Invalid;
  4234. o->expr = node;
  4235. return kind;
  4236. }
  4237. i64 srcz = type_size_of(c->allocator, o->type);
  4238. i64 dstz = type_size_of(c->allocator, t);
  4239. if (srcz != dstz) {
  4240. gbString expr_str = expr_to_string(o->expr);
  4241. gbString type_str = type_to_string(t);
  4242. error_node(o->expr, "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  4243. gb_string_free(type_str);
  4244. gb_string_free(expr_str);
  4245. o->mode = Addressing_Invalid;
  4246. o->expr = node;
  4247. return kind;
  4248. }
  4249. o->type = t;
  4250. } break;
  4251. case Token_union_cast: {
  4252. if (o->mode == Addressing_Constant) {
  4253. gbString expr_str = expr_to_string(o->expr);
  4254. error_node(o->expr, "Cannot `union_cast` a constant expression: `%s`", expr_str);
  4255. gb_string_free(expr_str);
  4256. o->mode = Addressing_Invalid;
  4257. o->expr = node;
  4258. return kind;
  4259. }
  4260. if (is_type_untyped(o->type)) {
  4261. gbString expr_str = expr_to_string(o->expr);
  4262. error_node(o->expr, "Cannot `union_cast` an untyped expression: `%s`", expr_str);
  4263. gb_string_free(expr_str);
  4264. o->mode = Addressing_Invalid;
  4265. o->expr = node;
  4266. return kind;
  4267. }
  4268. bool src_is_ptr = is_type_pointer(o->type);
  4269. bool dst_is_ptr = is_type_pointer(t);
  4270. Type *src = type_deref(o->type);
  4271. Type *dst = type_deref(t);
  4272. Type *bsrc = base_type(src);
  4273. Type *bdst = base_type(dst);
  4274. if (src_is_ptr != dst_is_ptr) {
  4275. gbString src_type_str = type_to_string(o->type);
  4276. gbString dst_type_str = type_to_string(t);
  4277. error_node(o->expr, "Invalid `union_cast` types: `%s` and `%s`", src_type_str, dst_type_str);
  4278. gb_string_free(dst_type_str);
  4279. gb_string_free(src_type_str);
  4280. o->mode = Addressing_Invalid;
  4281. o->expr = node;
  4282. return kind;
  4283. }
  4284. if (!is_type_union(src)) {
  4285. error_node(o->expr, "`union_cast` can only operate on unions");
  4286. o->mode = Addressing_Invalid;
  4287. o->expr = node;
  4288. return kind;
  4289. }
  4290. bool ok = false;
  4291. for (isize i = 1; i < bsrc->Record.variant_count; i++) {
  4292. Entity *f = bsrc->Record.variants[i];
  4293. if (are_types_identical(f->type, dst)) {
  4294. ok = true;
  4295. break;
  4296. }
  4297. }
  4298. if (!ok) {
  4299. gbString expr_str = expr_to_string(o->expr);
  4300. gbString dst_type_str = type_to_string(t);
  4301. error_node(o->expr, "Cannot `union_cast` `%s` to `%s`", expr_str, dst_type_str);
  4302. gb_string_free(dst_type_str);
  4303. gb_string_free(expr_str);
  4304. o->mode = Addressing_Invalid;
  4305. o->expr = node;
  4306. return kind;
  4307. }
  4308. add_type_info_type(c, o->type);
  4309. add_type_info_type(c, t);
  4310. o->type = t;
  4311. o->mode = Addressing_OptionalOk;
  4312. } break;
  4313. case Token_down_cast: {
  4314. if (o->mode == Addressing_Constant) {
  4315. gbString expr_str = expr_to_string(o->expr);
  4316. error_node(o->expr, "Cannot `down_cast` a constant expression: `%s`", expr_str);
  4317. gb_string_free(expr_str);
  4318. o->mode = Addressing_Invalid;
  4319. o->expr = node;
  4320. return kind;
  4321. }
  4322. if (is_type_untyped(o->type)) {
  4323. gbString expr_str = expr_to_string(o->expr);
  4324. error_node(o->expr, "Cannot `down_cast` an untyped expression: `%s`", expr_str);
  4325. gb_string_free(expr_str);
  4326. o->mode = Addressing_Invalid;
  4327. o->expr = node;
  4328. return kind;
  4329. }
  4330. if (!(is_type_pointer(o->type) && is_type_pointer(t))) {
  4331. gbString expr_str = expr_to_string(o->expr);
  4332. error_node(o->expr, "Can only `down_cast` pointers: `%s`", expr_str);
  4333. gb_string_free(expr_str);
  4334. o->mode = Addressing_Invalid;
  4335. o->expr = node;
  4336. return kind;
  4337. }
  4338. Type *src = type_deref(o->type);
  4339. Type *dst = type_deref(t);
  4340. Type *bsrc = base_type(src);
  4341. Type *bdst = base_type(dst);
  4342. if (!(is_type_struct(bsrc) || is_type_raw_union(bsrc))) {
  4343. gbString expr_str = expr_to_string(o->expr);
  4344. error_node(o->expr, "Can only `down_cast` pointer from structs or unions: `%s`", expr_str);
  4345. gb_string_free(expr_str);
  4346. o->mode = Addressing_Invalid;
  4347. o->expr = node;
  4348. return kind;
  4349. }
  4350. if (!(is_type_struct(bdst) || is_type_raw_union(bdst))) {
  4351. gbString expr_str = expr_to_string(o->expr);
  4352. error_node(o->expr, "Can only `down_cast` pointer to structs or unions: `%s`", expr_str);
  4353. gb_string_free(expr_str);
  4354. o->mode = Addressing_Invalid;
  4355. o->expr = node;
  4356. return kind;
  4357. }
  4358. String param_name = check_down_cast_name(dst, src);
  4359. if (param_name.len == 0) {
  4360. gbString expr_str = expr_to_string(o->expr);
  4361. error_node(o->expr, "Illegal `down_cast`: `%s`", expr_str);
  4362. gb_string_free(expr_str);
  4363. o->mode = Addressing_Invalid;
  4364. o->expr = node;
  4365. return kind;
  4366. }
  4367. o->mode = Addressing_Value;
  4368. o->type = t;
  4369. } break;
  4370. default:
  4371. GB_PANIC("Unknown cast expression");
  4372. }
  4373. case_end;
  4374. case_ast_node(ue, UnaryExpr, node);
  4375. check_expr_base(c, o, ue->expr, type_hint);
  4376. if (o->mode == Addressing_Invalid) {
  4377. o->expr = node;
  4378. return kind;
  4379. }
  4380. check_unary_expr(c, o, ue->op, node);
  4381. if (o->mode == Addressing_Invalid) {
  4382. o->expr = node;
  4383. return kind;
  4384. }
  4385. case_end;
  4386. case_ast_node(be, BinaryExpr, node);
  4387. check_binary_expr(c, o, node);
  4388. if (o->mode == Addressing_Invalid) {
  4389. o->expr = node;
  4390. return kind;
  4391. }
  4392. case_end;
  4393. case_ast_node(se, SelectorExpr, node);
  4394. check_selector(c, o, node, type_hint);
  4395. case_end;
  4396. case_ast_node(ie, IndexExpr, node);
  4397. check_expr(c, o, ie->expr);
  4398. if (o->mode == Addressing_Invalid) {
  4399. o->expr = node;
  4400. return kind;
  4401. }
  4402. Type *t = base_type(type_deref(o->type));
  4403. bool is_ptr = is_type_pointer(o->type);
  4404. bool is_const = o->mode == Addressing_Constant;
  4405. if (is_type_map(t)) {
  4406. Operand key = {0};
  4407. check_expr(c, &key, ie->index);
  4408. check_assignment(c, &key, t->Map.key, str_lit("map index"));
  4409. if (key.mode == Addressing_Invalid) {
  4410. o->mode = Addressing_Invalid;
  4411. o->expr = node;
  4412. return kind;
  4413. }
  4414. o->mode = Addressing_MapIndex;
  4415. o->type = t->Map.value;
  4416. o->expr = node;
  4417. return Expr_Expr;
  4418. }
  4419. i64 max_count = -1;
  4420. bool valid = check_set_index_data(o, t, is_ptr, &max_count);
  4421. if (is_const) {
  4422. valid = false;
  4423. }
  4424. if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
  4425. Entity *found = find_using_index_expr(t);
  4426. if (found != NULL) {
  4427. valid = check_set_index_data(o, found->type, is_type_pointer(found->type), &max_count);
  4428. }
  4429. }
  4430. if (!valid) {
  4431. gbString str = expr_to_string(o->expr);
  4432. if (is_const) {
  4433. error_node(o->expr, "Cannot index a constant `%s`", str);
  4434. } else {
  4435. error_node(o->expr, "Cannot index `%s`", str);
  4436. }
  4437. gb_string_free(str);
  4438. o->mode = Addressing_Invalid;
  4439. o->expr = node;
  4440. return kind;
  4441. }
  4442. if (ie->index == NULL) {
  4443. gbString str = expr_to_string(o->expr);
  4444. error_node(o->expr, "Missing index for `%s`", str);
  4445. gb_string_free(str);
  4446. o->mode = Addressing_Invalid;
  4447. o->expr = node;
  4448. return kind;
  4449. }
  4450. i64 index = 0;
  4451. bool ok = check_index_value(c, ie->index, max_count, &index);
  4452. case_end;
  4453. case_ast_node(se, SliceExpr, node);
  4454. check_expr(c, o, se->expr);
  4455. if (o->mode == Addressing_Invalid) {
  4456. o->mode = Addressing_Invalid;
  4457. o->expr = node;
  4458. return kind;
  4459. }
  4460. bool valid = false;
  4461. i64 max_count = -1;
  4462. Type *t = base_type(type_deref(o->type));
  4463. switch (t->kind) {
  4464. case Type_Basic:
  4465. if (is_type_string(t)) {
  4466. if (se->index3) {
  4467. error_node(node, "3-index slice on a string in not needed");
  4468. o->mode = Addressing_Invalid;
  4469. o->expr = node;
  4470. return kind;
  4471. }
  4472. valid = true;
  4473. if (o->mode == Addressing_Constant) {
  4474. max_count = o->value.value_string.len;
  4475. }
  4476. o->type = t_string;
  4477. }
  4478. break;
  4479. case Type_Array:
  4480. valid = true;
  4481. max_count = t->Array.count;
  4482. if (o->mode != Addressing_Variable) {
  4483. gbString str = expr_to_string(node);
  4484. error_node(node, "Cannot slice array `%s`, value is not addressable", str);
  4485. gb_string_free(str);
  4486. o->mode = Addressing_Invalid;
  4487. o->expr = node;
  4488. return kind;
  4489. }
  4490. o->type = make_type_slice(c->allocator, t->Array.elem);
  4491. break;
  4492. case Type_Slice:
  4493. valid = true;
  4494. break;
  4495. case Type_DynamicArray:
  4496. valid = true;
  4497. break;
  4498. }
  4499. if (!valid) {
  4500. gbString str = expr_to_string(o->expr);
  4501. error_node(o->expr, "Cannot slice `%s`", str);
  4502. gb_string_free(str);
  4503. o->mode = Addressing_Invalid;
  4504. o->expr = node;
  4505. return kind;
  4506. }
  4507. if (o->mode != Addressing_Immutable) {
  4508. o->mode = Addressing_Value;
  4509. }
  4510. if (se->index3 && (se->high == NULL || se->max == NULL)) {
  4511. error(se->close, "2nd and 3rd indices are required in a 3-index slice");
  4512. o->mode = Addressing_Invalid;
  4513. o->expr = node;
  4514. return kind;
  4515. }
  4516. i64 indices[2] = {0};
  4517. AstNode *nodes[3] = {se->low, se->high, se->max};
  4518. for (isize i = 0; i < gb_count_of(nodes); i++) {
  4519. i64 index = max_count;
  4520. if (nodes[i] != NULL) {
  4521. i64 capacity = -1;
  4522. if (max_count >= 0) {
  4523. capacity = max_count;
  4524. }
  4525. i64 j = 0;
  4526. if (check_index_value(c, nodes[i], capacity, &j)) {
  4527. index = j;
  4528. }
  4529. } else if (i == 0) {
  4530. index = 0;
  4531. }
  4532. indices[i] = index;
  4533. }
  4534. for (isize i = 0; i < gb_count_of(indices); i++) {
  4535. i64 a = indices[i];
  4536. for (isize j = i+1; j < gb_count_of(indices); j++) {
  4537. i64 b = indices[j];
  4538. if (a > b && b >= 0) {
  4539. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  4540. }
  4541. }
  4542. }
  4543. case_end;
  4544. case_ast_node(ce, CallExpr, node);
  4545. return check_call_expr(c, o, node);
  4546. case_end;
  4547. case_ast_node(ce, MacroCallExpr, node);
  4548. return check_macro_call_expr(c, o, node);
  4549. case_end;
  4550. case_ast_node(de, DerefExpr, node);
  4551. check_expr_or_type(c, o, de->expr);
  4552. if (o->mode == Addressing_Invalid) {
  4553. o->mode = Addressing_Invalid;
  4554. o->expr = node;
  4555. return kind;
  4556. } else {
  4557. Type *t = base_type(o->type);
  4558. if (t->kind == Type_Pointer) {
  4559. if (o->mode != Addressing_Immutable) {
  4560. o->mode = Addressing_Variable;
  4561. }
  4562. o->type = t->Pointer.elem;
  4563. } else {
  4564. gbString str = expr_to_string(o->expr);
  4565. error_node(o->expr, "Cannot dereference `%s`", str);
  4566. gb_string_free(str);
  4567. o->mode = Addressing_Invalid;
  4568. o->expr = node;
  4569. return kind;
  4570. }
  4571. }
  4572. case_end;
  4573. case AstNode_HelperType:
  4574. case AstNode_ProcType:
  4575. case AstNode_PointerType:
  4576. case AstNode_ArrayType:
  4577. case AstNode_DynamicArrayType:
  4578. case AstNode_VectorType:
  4579. case AstNode_StructType:
  4580. case AstNode_UnionType:
  4581. case AstNode_RawUnionType:
  4582. case AstNode_EnumType:
  4583. case AstNode_MapType:
  4584. o->mode = Addressing_Type;
  4585. o->type = check_type(c, node);
  4586. break;
  4587. }
  4588. kind = Expr_Expr;
  4589. o->expr = node;
  4590. return kind;
  4591. }
  4592. ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  4593. ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
  4594. Type *type = NULL;
  4595. ExactValue value = {ExactValue_Invalid};
  4596. switch (o->mode) {
  4597. case Addressing_Invalid:
  4598. type = t_invalid;
  4599. break;
  4600. case Addressing_NoValue:
  4601. type = NULL;
  4602. break;
  4603. case Addressing_Constant:
  4604. type = o->type;
  4605. value = o->value;
  4606. break;
  4607. default:
  4608. type = o->type;
  4609. break;
  4610. }
  4611. if (type != NULL && is_type_untyped(type)) {
  4612. add_untyped(&c->info, node, false, o->mode, type, value);
  4613. } else {
  4614. add_type_and_value(&c->info, node, o->mode, type, value);
  4615. }
  4616. return kind;
  4617. }
  4618. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  4619. check_expr_base(c, o, e, NULL);
  4620. switch (o->mode) {
  4621. default:
  4622. return; // NOTE(bill): Valid
  4623. case Addressing_NoValue:
  4624. error_operand_no_value(o);
  4625. break;
  4626. case Addressing_Type:
  4627. error_operand_not_expression(o);
  4628. break;
  4629. }
  4630. o->mode = Addressing_Invalid;
  4631. }
  4632. void check_not_tuple(Checker *c, Operand *o) {
  4633. if (o->mode == Addressing_Value) {
  4634. // NOTE(bill): Tuples are not first class thus never named
  4635. if (o->type->kind == Type_Tuple) {
  4636. isize count = o->type->Tuple.variable_count;
  4637. GB_ASSERT(count != 1);
  4638. error_node(o->expr,
  4639. "%td-valued tuple found where single value expected", count);
  4640. o->mode = Addressing_Invalid;
  4641. }
  4642. }
  4643. }
  4644. void check_expr(Checker *c, Operand *o, AstNode *e) {
  4645. check_multi_expr(c, o, e);
  4646. check_not_tuple(c, o);
  4647. }
  4648. void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
  4649. check_expr_base(c, o, e, NULL);
  4650. check_not_tuple(c, o);
  4651. error_operand_no_value(o);
  4652. }
  4653. gbString write_expr_to_string(gbString str, AstNode *node);
  4654. gbString write_record_fields_to_string(gbString str, AstNodeArray params) {
  4655. for_array(i, params) {
  4656. if (i > 0) {
  4657. str = gb_string_appendc(str, ", ");
  4658. }
  4659. str = write_expr_to_string(str, params.e[i]);
  4660. }
  4661. return str;
  4662. }
  4663. gbString string_append_token(gbString str, Token token) {
  4664. if (token.string.len > 0) {
  4665. return gb_string_append_length(str, token.string.text, token.string.len);
  4666. }
  4667. return str;
  4668. }
  4669. gbString write_expr_to_string(gbString str, AstNode *node) {
  4670. if (node == NULL)
  4671. return str;
  4672. if (is_ast_node_stmt(node)) {
  4673. GB_ASSERT("stmt passed to write_expr_to_string");
  4674. }
  4675. switch (node->kind) {
  4676. default:
  4677. str = gb_string_appendc(str, "(BadExpr)");
  4678. break;
  4679. case_ast_node(i, Ident, node);
  4680. str = string_append_token(str, *i);
  4681. case_end;
  4682. case_ast_node(i, Implicit, node);
  4683. str = string_append_token(str, *i);
  4684. case_end;
  4685. case_ast_node(bl, BasicLit, node);
  4686. str = string_append_token(str, *bl);
  4687. case_end;
  4688. case_ast_node(pl, ProcLit, node);
  4689. str = write_expr_to_string(str, pl->type);
  4690. case_end;
  4691. case_ast_node(cl, CompoundLit, node);
  4692. str = write_expr_to_string(str, cl->type);
  4693. str = gb_string_appendc(str, "{");
  4694. for_array(i, cl->elems) {
  4695. if (i > 0) {
  4696. str = gb_string_appendc(str, ", ");
  4697. }
  4698. str = write_expr_to_string(str, cl->elems.e[i]);
  4699. }
  4700. str = gb_string_appendc(str, "}");
  4701. case_end;
  4702. case_ast_node(te, TagExpr, node);
  4703. str = gb_string_appendc(str, "#");
  4704. str = string_append_token(str, te->name);
  4705. str = write_expr_to_string(str, te->expr);
  4706. case_end;
  4707. case_ast_node(ue, UnaryExpr, node);
  4708. str = string_append_token(str, ue->op);
  4709. str = write_expr_to_string(str, ue->expr);
  4710. case_end;
  4711. case_ast_node(ce, CastExpr, node);
  4712. str = string_append_token(str, ce->token);
  4713. str = gb_string_appendc(str, "(");
  4714. str = write_expr_to_string(str, ce->type);
  4715. str = gb_string_appendc(str, ")");
  4716. str = write_expr_to_string(str, ce->expr);
  4717. case_end;
  4718. case_ast_node(de, DerefExpr, node);
  4719. str = write_expr_to_string(str, de->expr);
  4720. str = gb_string_appendc(str, "^");
  4721. case_end;
  4722. case_ast_node(be, BinaryExpr, node);
  4723. str = write_expr_to_string(str, be->left);
  4724. str = gb_string_appendc(str, " ");
  4725. str = string_append_token(str, be->op);
  4726. str = gb_string_appendc(str, " ");
  4727. str = write_expr_to_string(str, be->right);
  4728. case_end;
  4729. case_ast_node(pe, ParenExpr, node);
  4730. str = gb_string_appendc(str, "(");
  4731. str = write_expr_to_string(str, pe->expr);
  4732. str = gb_string_appendc(str, ")");
  4733. case_end;
  4734. case_ast_node(se, SelectorExpr, node);
  4735. str = write_expr_to_string(str, se->expr);
  4736. str = gb_string_appendc(str, ".");
  4737. str = write_expr_to_string(str, se->selector);
  4738. case_end;
  4739. case_ast_node(ie, IndexExpr, node);
  4740. str = write_expr_to_string(str, ie->expr);
  4741. str = gb_string_appendc(str, "[");
  4742. str = write_expr_to_string(str, ie->index);
  4743. str = gb_string_appendc(str, "]");
  4744. case_end;
  4745. case_ast_node(se, SliceExpr, node);
  4746. str = write_expr_to_string(str, se->expr);
  4747. str = gb_string_appendc(str, "[");
  4748. str = write_expr_to_string(str, se->low);
  4749. str = gb_string_appendc(str, "..");
  4750. str = write_expr_to_string(str, se->high);
  4751. if (se->index3) {
  4752. str = gb_string_appendc(str, "..");
  4753. str = write_expr_to_string(str, se->max);
  4754. }
  4755. str = gb_string_appendc(str, "]");
  4756. case_end;
  4757. case_ast_node(e, Ellipsis, node);
  4758. str = gb_string_appendc(str, "..");
  4759. case_end;
  4760. case_ast_node(fv, FieldValue, node);
  4761. str = write_expr_to_string(str, fv->field);
  4762. str = gb_string_appendc(str, " = ");
  4763. str = write_expr_to_string(str, fv->value);
  4764. case_end;
  4765. case_ast_node(pt, PointerType, node);
  4766. str = gb_string_appendc(str, "^");
  4767. str = write_expr_to_string(str, pt->type);
  4768. case_end;
  4769. case_ast_node(at, ArrayType, node);
  4770. str = gb_string_appendc(str, "[");
  4771. if (at->count != NULL &&
  4772. at->count->kind == AstNode_UnaryExpr &&
  4773. at->count->UnaryExpr.op.kind == Token_Ellipsis) {
  4774. str = gb_string_appendc(str, "..");
  4775. } else {
  4776. str = write_expr_to_string(str, at->count);
  4777. }
  4778. str = gb_string_appendc(str, "]");
  4779. str = write_expr_to_string(str, at->elem);
  4780. case_end;
  4781. case_ast_node(at, DynamicArrayType, node);
  4782. str = gb_string_appendc(str, "[..]");
  4783. str = write_expr_to_string(str, at->elem);
  4784. case_end;
  4785. case_ast_node(vt, VectorType, node);
  4786. str = gb_string_appendc(str, "[vector ");
  4787. str = write_expr_to_string(str, vt->count);
  4788. str = gb_string_appendc(str, "]");
  4789. str = write_expr_to_string(str, vt->elem);
  4790. case_end;
  4791. case_ast_node(f, Field, node);
  4792. if (f->flags&FieldFlag_using) {
  4793. str = gb_string_appendc(str, "using ");
  4794. }
  4795. if (f->flags&FieldFlag_immutable) {
  4796. str = gb_string_appendc(str, "immutable ");
  4797. }
  4798. if (f->flags&FieldFlag_no_alias) {
  4799. str = gb_string_appendc(str, "no_alias ");
  4800. }
  4801. for_array(i, f->names) {
  4802. AstNode *name = f->names.e[i];
  4803. if (i > 0) {
  4804. str = gb_string_appendc(str, ", ");
  4805. }
  4806. str = write_expr_to_string(str, name);
  4807. }
  4808. if (f->names.count > 0) {
  4809. str = gb_string_appendc(str, ": ");
  4810. }
  4811. if (f->flags&FieldFlag_ellipsis) {
  4812. str = gb_string_appendc(str, "..");
  4813. }
  4814. str = write_expr_to_string(str, f->type);
  4815. case_end;
  4816. case_ast_node(f, FieldList, node);
  4817. for_array(i, f->list) {
  4818. if (i > 0) {
  4819. str = gb_string_appendc(str, ", ");
  4820. }
  4821. str = write_expr_to_string(str, f->list.e[i]);
  4822. }
  4823. case_end;
  4824. case_ast_node(f, UnionField, node);
  4825. str = write_expr_to_string(str, f->name);
  4826. str = gb_string_appendc(str, "{");
  4827. str = write_expr_to_string(str, f->list);
  4828. str = gb_string_appendc(str, "}");
  4829. case_end;
  4830. case_ast_node(ce, CallExpr, node);
  4831. str = write_expr_to_string(str, ce->proc);
  4832. str = gb_string_appendc(str, "(");
  4833. for_array(i, ce->args) {
  4834. AstNode *arg = ce->args.e[i];
  4835. if (i > 0) {
  4836. str = gb_string_appendc(str, ", ");
  4837. }
  4838. str = write_expr_to_string(str, arg);
  4839. }
  4840. str = gb_string_appendc(str, ")");
  4841. case_end;
  4842. case_ast_node(pt, ProcType, node);
  4843. str = gb_string_appendc(str, "proc(");
  4844. str = write_expr_to_string(str, pt->params);
  4845. str = gb_string_appendc(str, ")");
  4846. case_end;
  4847. case_ast_node(st, StructType, node);
  4848. str = gb_string_appendc(str, "struct ");
  4849. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  4850. if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
  4851. str = gb_string_appendc(str, "{");
  4852. str = write_record_fields_to_string(str, st->fields);
  4853. str = gb_string_appendc(str, "}");
  4854. case_end;
  4855. case_ast_node(st, RawUnionType, node);
  4856. str = gb_string_appendc(str, "raw_union ");
  4857. str = gb_string_appendc(str, "{");
  4858. str = write_record_fields_to_string(str, st->fields);
  4859. str = gb_string_appendc(str, "}");
  4860. case_end;
  4861. case_ast_node(st, UnionType, node);
  4862. str = gb_string_appendc(str, "union ");
  4863. str = gb_string_appendc(str, "{");
  4864. str = write_record_fields_to_string(str, st->fields);
  4865. str = gb_string_appendc(str, "}");
  4866. case_end;
  4867. case_ast_node(et, EnumType, node);
  4868. str = gb_string_appendc(str, "enum ");
  4869. if (et->base_type != NULL) {
  4870. str = write_expr_to_string(str, et->base_type);
  4871. str = gb_string_appendc(str, " ");
  4872. }
  4873. str = gb_string_appendc(str, "{");
  4874. for_array(i, et->fields) {
  4875. if (i > 0) {
  4876. str = gb_string_appendc(str, ", ");
  4877. }
  4878. str = write_expr_to_string(str, et->fields.e[i]);
  4879. }
  4880. str = gb_string_appendc(str, "}");
  4881. case_end;
  4882. case_ast_node(ht, HelperType, node);
  4883. str = gb_string_appendc(str, "#type ");
  4884. str = write_expr_to_string(str, ht->type);
  4885. case_end;
  4886. }
  4887. return str;
  4888. }
  4889. gbString expr_to_string(AstNode *expression) {
  4890. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
  4891. }