expr.cpp 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404
  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 = NULL);
  5. Type * check_type (Checker *c, AstNode *expression, Type *named_type = NULL, CycleChecker *cycle_checker = NULL);
  6. void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
  7. Entity * check_selector (Checker *c, Operand *operand, AstNode *node);
  8. void check_not_tuple (Checker *c, Operand *operand);
  9. b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
  10. void convert_to_typed (Checker *c, Operand *operand, Type *target_type, i32 level = 0);
  11. gbString expr_to_string (AstNode *expression);
  12. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type, CycleChecker *cycle_checker = NULL);
  13. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  14. void update_expr_type (Checker *c, AstNode *e, Type *type, b32 final);
  15. b32 check_is_assignable_to_using_subtype(Type *dst, Type *src) {
  16. Type *prev_src = src;
  17. // Type *prev_dst = dst;
  18. src = base_type(type_deref(src));
  19. // dst = base_type(type_deref(dst));
  20. b32 src_is_ptr = src != prev_src;
  21. // b32 dst_is_ptr = dst != prev_dst;
  22. if (is_type_struct(src)) {
  23. for (isize i = 0; i < src->Record.field_count; i++) {
  24. Entity *f = src->Record.fields[i];
  25. if (f->kind == Entity_Variable && f->Variable.anonymous) {
  26. if (are_types_identical(dst, f->type)) {
  27. return true;
  28. }
  29. if (src_is_ptr && is_type_pointer(dst)) {
  30. if (are_types_identical(type_deref(dst), f->type)) {
  31. return true;
  32. }
  33. }
  34. b32 ok = check_is_assignable_to_using_subtype(dst, f->type);
  35. if (ok) {
  36. return true;
  37. }
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argument = false) {
  44. PROF_PROC();
  45. if (operand->mode == Addressing_Invalid ||
  46. type == t_invalid) {
  47. return true;
  48. }
  49. if (operand->mode == Addressing_Builtin) {
  50. return false;
  51. }
  52. Type *s = operand->type;
  53. if (are_types_identical(s, type)) {
  54. return true;
  55. }
  56. Type *src = base_type(s);
  57. Type *dst = base_type(type);
  58. if (is_type_untyped(src)) {
  59. switch (dst->kind) {
  60. case Type_Basic:
  61. if (operand->mode == Addressing_Constant) {
  62. return check_value_is_expressible(c, operand->value, dst, NULL);
  63. }
  64. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  65. return is_type_boolean(dst);
  66. }
  67. break;
  68. }
  69. if (type_has_nil(dst)) {
  70. return is_operand_nil(operand);
  71. }
  72. }
  73. if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
  74. if (is_type_enum(dst) && is_type_enum(src)) {
  75. return are_types_identical(s, type);
  76. }
  77. return true;
  78. }
  79. if (is_type_maybe(dst)) {
  80. Type *elem = base_type(dst)->Maybe.elem;
  81. return are_types_identical(elem, src);
  82. }
  83. if (is_type_untyped_nil(src)) {
  84. return type_has_nil(dst);
  85. }
  86. // ^T <- rawptr
  87. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  88. // if (is_type_pointer(dst) && is_type_rawptr(src)) {
  89. // return true;
  90. // }
  91. // rawptr <- ^T
  92. if (is_type_rawptr(dst) && is_type_pointer(src)) {
  93. return true;
  94. }
  95. if (dst->kind == Type_Array && src->kind == Type_Array) {
  96. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  97. return dst->Array.count == src->Array.count;
  98. }
  99. }
  100. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  101. if (are_types_identical(dst->Slice.elem, src->Slice.elem)) {
  102. return true;
  103. }
  104. }
  105. if (is_type_union(dst)) {
  106. for (isize i = 0; i < dst->Record.field_count; i++) {
  107. Entity *f = dst->Record.fields[i];
  108. if (are_types_identical(f->type, s)) {
  109. return true;
  110. }
  111. }
  112. }
  113. if (dst == t_any) {
  114. // NOTE(bill): Anything can cast to `Any`
  115. add_type_info_type(c, s);
  116. return true;
  117. }
  118. if (true || is_argument) {
  119. // NOTE(bill): Polymorphism for subtyping
  120. if (check_is_assignable_to_using_subtype(type, src)) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. // NOTE(bill): `content_name` is for debugging and error messages
  127. void check_assignment(Checker *c, Operand *operand, Type *type, String context_name, b32 is_argument = false) {
  128. PROF_PROC();
  129. check_not_tuple(c, operand);
  130. if (operand->mode == Addressing_Invalid)
  131. return;
  132. if (is_type_untyped(operand->type)) {
  133. Type *target_type = type;
  134. if (type == NULL || is_type_any(type) || is_type_untyped_nil(type)) {
  135. if (type == NULL && base_type(operand->type) == t_untyped_nil) {
  136. error(ast_node_token(operand->expr), "Use of untyped nil in %.*s", LIT(context_name));
  137. operand->mode = Addressing_Invalid;
  138. return;
  139. }
  140. add_type_info_type(c, type);
  141. target_type = default_type(operand->type);
  142. }
  143. convert_to_typed(c, operand, target_type);
  144. if (operand->mode == Addressing_Invalid) {
  145. return;
  146. }
  147. }
  148. if (type != NULL) {
  149. if (!check_is_assignable_to(c, operand, type, is_argument)) {
  150. gbString type_string = type_to_string(type);
  151. gbString op_type_string = type_to_string(operand->type);
  152. gbString expr_str = expr_to_string(operand->expr);
  153. defer (gb_string_free(type_string));
  154. defer (gb_string_free(op_type_string));
  155. defer (gb_string_free(expr_str));
  156. if (operand->mode == Addressing_Builtin) {
  157. // TODO(bill): is this a good enough error message?
  158. error(ast_node_token(operand->expr),
  159. "Cannot assign builtin procedure `%s` in %.*s",
  160. expr_str,
  161. LIT(context_name));
  162. } else {
  163. // TODO(bill): is this a good enough error message?
  164. error(ast_node_token(operand->expr),
  165. "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
  166. expr_str,
  167. op_type_string,
  168. type_string,
  169. LIT(context_name));
  170. }
  171. operand->mode = Addressing_Invalid;
  172. return;
  173. }
  174. }
  175. }
  176. void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *> *entity_map) {
  177. PROF_PROC();
  178. t = base_type(type_deref(t));
  179. gbString str = expr_to_string(node);
  180. defer (gb_string_free(str));
  181. if (t->kind == Type_Record) {
  182. for (isize i = 0; i < t->Record.field_count; i++) {
  183. Entity *f = t->Record.fields[i];
  184. GB_ASSERT(f->kind == Entity_Variable);
  185. String name = f->token.string;
  186. HashKey key = hash_string(name);
  187. Entity **found = map_get(entity_map, key);
  188. if (found != NULL) {
  189. Entity *e = *found;
  190. // TODO(bill): Better type error
  191. error(e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
  192. } else {
  193. map_set(entity_map, key, f);
  194. add_entity(c, c->context.scope, NULL, f);
  195. if (f->Variable.anonymous) {
  196. populate_using_entity_map(c, node, f->type, entity_map);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
  203. void check_fields(Checker *c, AstNode *node, AstNodeArray decls,
  204. Entity **fields, isize field_count,
  205. Entity **other_fields, isize other_field_count,
  206. CycleChecker *cycle_checker, String context) {
  207. PROF_PROC();
  208. Map<Entity *> entity_map = {};
  209. map_init(&entity_map, gb_heap_allocator());
  210. defer (map_destroy(&entity_map));
  211. isize other_field_index = 0;
  212. Entity *using_index_expr = NULL;
  213. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  214. defer (gb_temp_arena_memory_end(tmp));
  215. struct Delay {
  216. Entity *e;
  217. AstNode *t;
  218. };
  219. Array<Delay> delayed_const; array_init(&delayed_const, c->tmp_allocator, other_field_count);
  220. Array<Delay> delayed_type; array_init(&delayed_type, c->tmp_allocator, other_field_count);
  221. for_array(decl_index, decls) {
  222. AstNode *decl = decls[decl_index];
  223. if (decl->kind == AstNode_ConstDecl) {
  224. ast_node(cd, ConstDecl, decl);
  225. isize entity_count = cd->names.count;
  226. isize entity_index = 0;
  227. Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
  228. for_array(i, cd->values) {
  229. AstNode *name = cd->names[i];
  230. AstNode *value = cd->values[i];
  231. GB_ASSERT(name->kind == AstNode_Ident);
  232. ExactValue v = {ExactValue_Invalid};
  233. Token name_token = name->Ident;
  234. Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, NULL, v);
  235. entities[entity_index++] = e;
  236. Delay delay = {e, cd->type};
  237. array_add(&delayed_const, delay);
  238. }
  239. isize lhs_count = cd->names.count;
  240. isize rhs_count = cd->values.count;
  241. // TODO(bill): Better error messages or is this good enough?
  242. if (rhs_count == 0 && cd->type == NULL) {
  243. error(ast_node_token(node), "Missing type or initial expression");
  244. } else if (lhs_count < rhs_count) {
  245. error(ast_node_token(node), "Extra initial expression");
  246. }
  247. for_array(i, cd->names) {
  248. AstNode *name = cd->names[i];
  249. Entity *e = entities[i];
  250. Token name_token = name->Ident;
  251. if (name_token.string == "_") {
  252. other_fields[other_field_index++] = e;
  253. } else {
  254. HashKey key = hash_string(name_token.string);
  255. if (map_get(&entity_map, key) != NULL) {
  256. // TODO(bill): Scope checking already checks the declaration
  257. error(name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
  258. } else {
  259. map_set(&entity_map, key, e);
  260. other_fields[other_field_index++] = e;
  261. }
  262. add_entity(c, c->context.scope, name, e);
  263. }
  264. }
  265. } else if (decl->kind == AstNode_TypeDecl) {
  266. ast_node(td, TypeDecl, decl);
  267. Token name_token = td->name->Ident;
  268. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, NULL);
  269. Delay delay = {e, td->type};
  270. array_add(&delayed_type, delay);
  271. if (name_token.string == "_") {
  272. other_fields[other_field_index++] = e;
  273. } else {
  274. HashKey key = hash_string(name_token.string);
  275. if (map_get(&entity_map, key) != NULL) {
  276. // TODO(bill): Scope checking already checks the declaration
  277. error(name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
  278. } else {
  279. map_set(&entity_map, key, e);
  280. other_fields[other_field_index++] = e;
  281. }
  282. add_entity(c, c->context.scope, td->name, e);
  283. add_entity_use(c, td->name, e);
  284. }
  285. }
  286. }
  287. for_array(i, delayed_type) {
  288. check_const_decl(c, delayed_type[i].e, delayed_type[i].t, NULL);
  289. }
  290. for_array(i, delayed_const) {
  291. check_type_decl(c, delayed_const[i].e, delayed_const[i].t, NULL, NULL);
  292. }
  293. if (node->kind == AstNode_UnionType) {
  294. isize field_index = 0;
  295. fields[field_index++] = make_entity_type_name(c->allocator, c->context.scope, empty_token, NULL);
  296. for_array(decl_index, decls) {
  297. AstNode *decl = decls[decl_index];
  298. if (decl->kind != AstNode_VarDecl) {
  299. continue;
  300. }
  301. ast_node(vd, VarDecl, decl);
  302. Type *base_type = check_type(c, vd->type, NULL, cycle_checker);
  303. for_array(name_index, vd->names) {
  304. AstNode *name = vd->names[name_index];
  305. Token name_token = name->Ident;
  306. Type *type = make_type_named(c->allocator, name_token.string, base_type, NULL);
  307. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, type);
  308. type->Named.type_name = e;
  309. add_entity(c, c->context.scope, name, e);
  310. if (name_token.string == "_") {
  311. error(name_token, "`_` cannot be used a union subtype");
  312. continue;
  313. }
  314. HashKey key = hash_string(name_token.string);
  315. if (map_get(&entity_map, key) != NULL) {
  316. // TODO(bill): Scope checking already checks the declaration
  317. error(name_token, "`%.*s` is already declared in this union", LIT(name_token.string));
  318. } else {
  319. map_set(&entity_map, key, e);
  320. fields[field_index++] = e;
  321. }
  322. add_entity_use(c, name, e);
  323. }
  324. }
  325. } else {
  326. isize field_index = 0;
  327. for_array(decl_index, decls) {
  328. AstNode *decl = decls[decl_index];
  329. if (decl->kind != AstNode_VarDecl) {
  330. continue;
  331. }
  332. ast_node(vd, VarDecl, decl);
  333. Type *type = check_type(c, vd->type, NULL, cycle_checker);
  334. if (vd->is_using) {
  335. if (vd->names.count > 1) {
  336. error(ast_node_token(vd->names[0]),
  337. "Cannot apply `using` to more than one of the same type");
  338. }
  339. }
  340. for_array(name_index, vd->names) {
  341. AstNode *name = vd->names[name_index];
  342. Token name_token = name->Ident;
  343. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, vd->is_using, cast(i32)field_index);
  344. e->identifier = name;
  345. if (name_token.string == "_") {
  346. fields[field_index++] = e;
  347. } else {
  348. HashKey key = hash_string(name_token.string);
  349. if (map_get(&entity_map, key) != NULL) {
  350. // TODO(bill): Scope checking already checks the declaration
  351. error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
  352. } else {
  353. map_set(&entity_map, key, e);
  354. fields[field_index++] = e;
  355. add_entity(c, c->context.scope, name, e);
  356. }
  357. add_entity_use(c, name, e);
  358. }
  359. }
  360. if (vd->is_using) {
  361. Type *t = base_type(type_deref(type));
  362. if (!is_type_struct(t) && !is_type_raw_union(t)) {
  363. Token name_token = vd->names[0]->Ident;
  364. if (is_type_indexable(t)) {
  365. b32 ok = true;
  366. for_array(emi, entity_map.entries) {
  367. Entity *e = entity_map.entries[emi].value;
  368. if (e->kind == Entity_Variable && e->Variable.anonymous) {
  369. if (is_type_indexable(e->type)) {
  370. if (e->identifier != vd->names[0]) {
  371. ok = false;
  372. using_index_expr = e;
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. if (ok) {
  379. using_index_expr = fields[field_index-1];
  380. } else {
  381. fields[field_index-1]->Variable.anonymous = false;
  382. error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
  383. }
  384. } else {
  385. error(name_token, "`using` on a field `%.*s` must be a `struct` or `raw_union`", LIT(name_token.string));
  386. continue;
  387. }
  388. }
  389. populate_using_entity_map(c, node, type, &entity_map);
  390. }
  391. }
  392. }
  393. }
  394. // TODO(bill): Cleanup struct field reordering
  395. // TODO(bill): Inline sorting procedure?
  396. gb_global BaseTypeSizes __checker_sizes = {};
  397. gb_global gbAllocator __checker_allocator = {};
  398. GB_COMPARE_PROC(cmp_struct_entity_size) {
  399. // Rule:
  400. // Biggest to smallest alignment
  401. // if same alignment: biggest to smallest size
  402. // if same size: order by source order
  403. Entity *x = *(Entity **)a;
  404. Entity *y = *(Entity **)b;
  405. GB_ASSERT(x != NULL);
  406. GB_ASSERT(y != NULL);
  407. GB_ASSERT(x->kind == Entity_Variable);
  408. GB_ASSERT(y->kind == Entity_Variable);
  409. i64 xa = type_align_of(__checker_sizes, __checker_allocator, x->type);
  410. i64 ya = type_align_of(__checker_sizes, __checker_allocator, y->type);
  411. i64 xs = type_size_of(__checker_sizes, __checker_allocator, x->type);
  412. i64 ys = type_size_of(__checker_sizes, __checker_allocator, y->type);
  413. if (xa == ya) {
  414. if (xs == ys) {
  415. i32 diff = x->Variable.field_index - y->Variable.field_index;
  416. return diff < 0 ? -1 : diff > 0;
  417. }
  418. return xs > ys ? -1 : xs < ys;
  419. }
  420. return xa > ya ? -1 : xa < ya;
  421. }
  422. void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) {
  423. PROF_PROC();
  424. GB_ASSERT(is_type_struct(struct_type));
  425. ast_node(st, StructType, node);
  426. isize field_count = 0;
  427. isize other_field_count = 0;
  428. for_array(decl_index, st->decls) {
  429. AstNode *decl = st->decls[decl_index];
  430. switch (decl->kind) {
  431. case_ast_node(vd, VarDecl, decl);
  432. field_count += vd->names.count;
  433. case_end;
  434. case_ast_node(cd, ConstDecl, decl);
  435. other_field_count += cd->names.count;
  436. case_end;
  437. case_ast_node(td, TypeDecl, decl);
  438. other_field_count += 1;
  439. case_end;
  440. }
  441. }
  442. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  443. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  444. check_fields(c, node, st->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("struct"));
  445. struct_type->Record.struct_is_packed = st->is_packed;
  446. struct_type->Record.struct_is_ordered = st->is_ordered;
  447. struct_type->Record.fields = fields;
  448. struct_type->Record.fields_in_src_order = fields;
  449. struct_type->Record.field_count = field_count;
  450. struct_type->Record.other_fields = other_fields;
  451. struct_type->Record.other_field_count = other_field_count;
  452. if (!st->is_packed && !st->is_ordered) {
  453. // NOTE(bill): Reorder fields for reduced size/performance
  454. Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, field_count);
  455. for (isize i = 0; i < field_count; i++) {
  456. reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
  457. }
  458. // NOTE(bill): Hacky thing
  459. // TODO(bill): Probably make an inline sorting procedure rather than use global variables
  460. __checker_sizes = c->sizes;
  461. __checker_allocator = c->allocator;
  462. // NOTE(bill): compound literal order must match source not layout
  463. gb_sort_array(reordered_fields, field_count, cmp_struct_entity_size);
  464. for (isize i = 0; i < field_count; i++) {
  465. reordered_fields[i]->Variable.field_index = i;
  466. }
  467. struct_type->Record.fields = reordered_fields;
  468. }
  469. type_set_offsets(c->sizes, c->allocator, struct_type);
  470. }
  471. void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
  472. PROF_PROC();
  473. GB_ASSERT(is_type_union(union_type));
  474. ast_node(ut, UnionType, node);
  475. isize field_count = 1;
  476. isize other_field_count = 0;
  477. for_array(decl_index, ut->decls) {
  478. AstNode *decl = ut->decls[decl_index];
  479. switch (decl->kind) {
  480. case_ast_node(vd, VarDecl, decl);
  481. field_count += vd->names.count;
  482. case_end;
  483. case_ast_node(cd, ConstDecl, decl);
  484. other_field_count += cd->names.count;
  485. case_end;
  486. case_ast_node(td, TypeDecl, decl);
  487. other_field_count += 1;
  488. case_end;
  489. }
  490. }
  491. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  492. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  493. check_fields(c, node, ut->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("union"));
  494. union_type->Record.fields = fields;
  495. union_type->Record.field_count = field_count;
  496. union_type->Record.other_fields = other_fields;
  497. union_type->Record.other_field_count = other_field_count;
  498. }
  499. void check_raw_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
  500. PROF_PROC();
  501. GB_ASSERT(node->kind == AstNode_RawUnionType);
  502. GB_ASSERT(is_type_raw_union(union_type));
  503. ast_node(ut, RawUnionType, node);
  504. isize field_count = 0;
  505. isize other_field_count = 0;
  506. for_array(decl_index, ut->decls) {
  507. AstNode *decl = ut->decls[decl_index];
  508. switch (decl->kind) {
  509. case_ast_node(vd, VarDecl, decl);
  510. field_count += vd->names.count;
  511. case_end;
  512. case_ast_node(cd, ConstDecl, decl);
  513. other_field_count += cd->names.count;
  514. case_end;
  515. case_ast_node(td, TypeDecl, decl);
  516. other_field_count += 1;
  517. case_end;
  518. }
  519. }
  520. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  521. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  522. check_fields(c, node, ut->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("raw union"));
  523. union_type->Record.fields = fields;
  524. union_type->Record.field_count = field_count;
  525. union_type->Record.other_fields = other_fields;
  526. union_type->Record.other_field_count = other_field_count;
  527. }
  528. GB_COMPARE_PROC(cmp_enum_order) {
  529. // Rule:
  530. // Biggest to smallest alignment
  531. // if same alignment: biggest to smallest size
  532. // if same size: order by source order
  533. Entity *x = *(Entity **)a;
  534. Entity *y = *(Entity **)b;
  535. GB_ASSERT(x != NULL);
  536. GB_ASSERT(y != NULL);
  537. GB_ASSERT(x->kind == Entity_Constant);
  538. GB_ASSERT(y->kind == Entity_Constant);
  539. GB_ASSERT(x->Constant.value.kind == ExactValue_Integer);
  540. GB_ASSERT(y->Constant.value.kind == ExactValue_Integer);
  541. i64 i = x->Constant.value.value_integer;
  542. i64 j = y->Constant.value.value_integer;
  543. return i < j ? -1 : i > j;
  544. }
  545. void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *node) {
  546. PROF_PROC();
  547. GB_ASSERT(node->kind == AstNode_EnumType);
  548. GB_ASSERT(is_type_enum(enum_type));
  549. ast_node(et, EnumType, node);
  550. Map<Entity *> entity_map = {};
  551. map_init(&entity_map, gb_heap_allocator());
  552. defer (map_destroy(&entity_map));
  553. Type *base_type = t_int;
  554. if (et->base_type != NULL) {
  555. base_type = check_type(c, et->base_type);
  556. }
  557. if (base_type == NULL || !is_type_integer(base_type)) {
  558. error(et->token, "Base type for enumeration must be an integer");
  559. return;
  560. } else
  561. if (base_type == NULL) {
  562. base_type = t_int;
  563. }
  564. enum_type->Record.enum_base = base_type;
  565. Entity **fields = gb_alloc_array(c->allocator, Entity *, et->fields.count);
  566. isize field_index = 0;
  567. ExactValue iota = make_exact_value_integer(-1);
  568. i64 min_value = 0;
  569. i64 max_value = 0;
  570. Type *constant_type = enum_type;
  571. if (named_type != NULL) {
  572. constant_type = named_type;
  573. }
  574. Entity *blank_entity = make_entity_constant(c->allocator, c->context.scope, blank_token, constant_type, make_exact_value_integer(0));;
  575. for_array(i, et->fields) {
  576. AstNode *field = et->fields[i];
  577. ast_node(f, FieldValue, field);
  578. Token name_token = f->field->Ident;
  579. if (name_token.string == "count") {
  580. error(name_token, "`count` is a reserved identifier for enumerations");
  581. fields[field_index++] = blank_entity;
  582. continue;
  583. } else if (name_token.string == "min_value") {
  584. error(name_token, "`min_value` is a reserved identifier for enumerations");
  585. fields[field_index++] = blank_entity;
  586. continue;
  587. } else if (name_token.string == "max_value") {
  588. error(name_token, "`max_value` is a reserved identifier for enumerations");
  589. fields[field_index++] = blank_entity;
  590. continue;
  591. }
  592. Operand o = {};
  593. if (f->value != NULL) {
  594. check_expr(c, &o, f->value);
  595. if (o.mode != Addressing_Constant) {
  596. error(ast_node_token(f->value), "Enumeration value must be a constant integer");
  597. o.mode = Addressing_Invalid;
  598. }
  599. if (o.mode != Addressing_Invalid) {
  600. check_assignment(c, &o, constant_type, make_string("enumeration"));
  601. }
  602. if (o.mode != Addressing_Invalid) {
  603. iota = o.value;
  604. } else {
  605. Token add_token = {Token_Add};
  606. iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
  607. }
  608. } else {
  609. Token add_token = {Token_Add};
  610. iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
  611. }
  612. Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, constant_type, iota);
  613. if (min_value > iota.value_integer) {
  614. min_value = iota.value_integer;
  615. }
  616. if (max_value < iota.value_integer) {
  617. max_value = iota.value_integer;
  618. }
  619. HashKey key = hash_string(name_token.string);
  620. if (map_get(&entity_map, key)) {
  621. // TODO(bill): Scope checking already checks the declaration
  622. error(name_token, "`%.*s` is already declared in this enumeration", LIT(name_token.string));
  623. } else {
  624. map_set(&entity_map, key, e);
  625. add_entity(c, c->context.scope, NULL, e);
  626. fields[field_index++] = e;
  627. }
  628. add_entity_use(c, f->field, e);
  629. }
  630. gb_sort_array(fields, et->fields.count, cmp_enum_order);
  631. enum_type->Record.other_fields = fields;
  632. enum_type->Record.other_field_count = et->fields.count;
  633. enum_type->Record.enum_count = make_entity_constant(c->allocator, NULL,
  634. make_token_ident(make_string("count")), t_int, make_exact_value_integer(enum_type->Record.other_field_count));
  635. enum_type->Record.min_value = make_entity_constant(c->allocator, NULL,
  636. make_token_ident(make_string("min_value")), constant_type, make_exact_value_integer(min_value));
  637. enum_type->Record.max_value = make_entity_constant(c->allocator, NULL,
  638. make_token_ident(make_string("max_value")), constant_type, make_exact_value_integer(max_value));
  639. }
  640. Type *check_get_params(Checker *c, Scope *scope, AstNodeArray params, b32 *is_variadic_) {
  641. PROF_PROC();
  642. if (params.count == 0) {
  643. return NULL;
  644. }
  645. b32 is_variadic = false;
  646. Type *tuple = make_type_tuple(c->allocator);
  647. isize variable_count = 0;
  648. for_array(i, params) {
  649. AstNode *field = params[i];
  650. ast_node(p, Parameter, field);
  651. variable_count += p->names.count;
  652. }
  653. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  654. isize variable_index = 0;
  655. for_array(i, params) {
  656. ast_node(p, Parameter, params[i]);
  657. AstNode *type_expr = p->type;
  658. if (type_expr) {
  659. if (type_expr->kind == AstNode_Ellipsis) {
  660. type_expr = type_expr->Ellipsis.expr;
  661. if (i+1 == params.count) {
  662. is_variadic = true;
  663. } else {
  664. error(ast_node_token(params[i]), "Invalid AST: Invalid variadic parameter");
  665. }
  666. }
  667. Type *type = check_type(c, type_expr);
  668. for_array(j, p->names) {
  669. AstNode *name = p->names[j];
  670. if (name->kind == AstNode_Ident) {
  671. Entity *param = make_entity_param(c->allocator, scope, name->Ident, type, p->is_using);
  672. add_entity(c, scope, name, param);
  673. variables[variable_index++] = param;
  674. } else {
  675. error(ast_node_token(name), "Invalid AST: Invalid parameter");
  676. }
  677. }
  678. }
  679. }
  680. if (is_variadic && params.count > 0) {
  681. // NOTE(bill): Change last variadic parameter to be a slice
  682. // Custom Calling convention for variadic parameters
  683. Entity *end = variables[params.count-1];
  684. end->type = make_type_slice(c->allocator, end->type);
  685. }
  686. tuple->Tuple.variables = variables;
  687. tuple->Tuple.variable_count = variable_count;
  688. if (is_variadic_) *is_variadic_ = is_variadic;
  689. return tuple;
  690. }
  691. Type *check_get_results(Checker *c, Scope *scope, AstNodeArray results) {
  692. PROF_PROC();
  693. if (results.count == 0) {
  694. return NULL;
  695. }
  696. Type *tuple = make_type_tuple(c->allocator);
  697. Entity **variables = gb_alloc_array(c->allocator, Entity *, results.count);
  698. isize variable_index = 0;
  699. for_array(i, results) {
  700. AstNode *item = results[i];
  701. Type *type = check_type(c, item);
  702. Token token = ast_node_token(item);
  703. token.string = make_string(""); // NOTE(bill): results are not named
  704. // TODO(bill): Should I have named results?
  705. Entity *param = make_entity_param(c->allocator, scope, token, type, false);
  706. // NOTE(bill): No need to record
  707. variables[variable_index++] = param;
  708. }
  709. tuple->Tuple.variables = variables;
  710. tuple->Tuple.variable_count = results.count;
  711. return tuple;
  712. }
  713. void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
  714. PROF_PROC();
  715. ast_node(pt, ProcType, proc_type_node);
  716. b32 variadic = false;
  717. Type *params = check_get_params(c, c->context.scope, pt->params, &variadic);
  718. Type *results = check_get_results(c, c->context.scope, pt->results);
  719. isize param_count = 0;
  720. isize result_count = 0;
  721. if (params) param_count = params ->Tuple.variable_count;
  722. if (results) result_count = results->Tuple.variable_count;
  723. type->Proc.scope = c->context.scope;
  724. type->Proc.params = params;
  725. type->Proc.param_count = param_count;
  726. type->Proc.results = results;
  727. type->Proc.result_count = result_count;
  728. type->Proc.variadic = variadic;
  729. // type->Proc.implicit_context = implicit_context;
  730. }
  731. void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type, CycleChecker *cycle_checker) {
  732. PROF_PROC();
  733. GB_ASSERT(n->kind == AstNode_Ident);
  734. o->mode = Addressing_Invalid;
  735. o->expr = n;
  736. Entity *e = scope_lookup_entity(c->context.scope, n->Ident.string);
  737. if (e == NULL) {
  738. if (n->Ident.string == "_") {
  739. error(n->Ident, "`_` cannot be used as a value type");
  740. } else {
  741. error(n->Ident, "Undeclared name: %.*s", LIT(n->Ident.string));
  742. }
  743. o->type = t_invalid;
  744. o->mode = Addressing_Invalid;
  745. if (named_type != NULL) {
  746. set_base_type(named_type, t_invalid);
  747. }
  748. return;
  749. }
  750. add_entity_use(c, n, e);
  751. // CycleChecker local_cycle_checker = {};
  752. // if (cycle_checker == NULL) {
  753. // cycle_checker = &local_cycle_checker;
  754. // }
  755. // defer (cycle_checker_destroy(&local_cycle_checker));
  756. check_entity_decl(c, e, NULL, named_type, cycle_checker);
  757. if (e->type == NULL) {
  758. compiler_error("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(n->Ident.string));
  759. return;
  760. }
  761. Type *type = e->type;
  762. switch (e->kind) {
  763. case Entity_Constant:
  764. if (type == t_invalid) {
  765. o->type = t_invalid;
  766. return;
  767. }
  768. o->value = e->Constant.value;
  769. GB_ASSERT(o->value.kind != ExactValue_Invalid);
  770. o->mode = Addressing_Constant;
  771. break;
  772. case Entity_Variable:
  773. e->Variable.used = true;
  774. if (type == t_invalid) {
  775. o->type = t_invalid;
  776. return;
  777. }
  778. #if 0
  779. if (e->Variable.param) {
  780. o->mode = Addressing_Value;
  781. } else {
  782. o->mode = Addressing_Variable;
  783. }
  784. #else
  785. o->mode = Addressing_Variable;
  786. #endif
  787. break;
  788. case Entity_TypeName: {
  789. o->mode = Addressing_Type;
  790. #if 0
  791. // TODO(bill): Fix cyclical dependancy checker
  792. if (cycle_checker != NULL) {
  793. for_array(i, cycle_checker->path) {
  794. Entity *prev = cycle_checker->path[i];
  795. if (prev == e) {
  796. error(e->token, "Illegal declaration cycle for %.*s", LIT(e->token.string));
  797. for (isize j = i; j < gb_array_count(cycle_checker->path); j++) {
  798. Entity *ref = cycle_checker->path[j];
  799. error(ref->token, "\t%.*s refers to", LIT(ref->token.string));
  800. }
  801. error(e->token, "\t%.*s", LIT(e->token.string));
  802. type = t_invalid;
  803. break;
  804. }
  805. }
  806. }
  807. #endif
  808. } break;
  809. case Entity_Procedure:
  810. o->mode = Addressing_Value;
  811. break;
  812. case Entity_Builtin:
  813. o->builtin_id = e->Builtin.id;
  814. o->mode = Addressing_Builtin;
  815. break;
  816. case Entity_ImportName:
  817. error(ast_node_token(n), "Use of import `%.*s` not in selector", LIT(e->ImportName.name));
  818. return;
  819. case Entity_Nil:
  820. o->mode = Addressing_Value;
  821. break;
  822. case Entity_ImplicitValue:
  823. o->mode = Addressing_Value;
  824. break;
  825. default:
  826. compiler_error("Compiler error: Unknown EntityKind");
  827. break;
  828. }
  829. o->type = type;
  830. }
  831. i64 check_array_count(Checker *c, AstNode *e) {
  832. PROF_PROC();
  833. if (e == NULL) {
  834. return 0;
  835. }
  836. Operand o = {};
  837. check_expr(c, &o, e);
  838. if (o.mode != Addressing_Constant) {
  839. if (o.mode != Addressing_Invalid) {
  840. error(ast_node_token(e), "Array count must be a constant");
  841. }
  842. return 0;
  843. }
  844. if (is_type_untyped(o.type) || is_type_integer(o.type)) {
  845. if (o.value.kind == ExactValue_Integer) {
  846. i64 count = o.value.value_integer;
  847. if (count >= 0) {
  848. return count;
  849. }
  850. error(ast_node_token(e), "Invalid array count");
  851. return 0;
  852. }
  853. }
  854. error(ast_node_token(e), "Array count must be an integer");
  855. return 0;
  856. }
  857. Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_checker) {
  858. PROF_PROC();
  859. ExactValue null_value = {ExactValue_Invalid};
  860. Type *type = NULL;
  861. gbString err_str = NULL;
  862. defer (gb_string_free(err_str));
  863. switch (e->kind) {
  864. case_ast_node(i, Ident, e);
  865. Operand o = {};
  866. check_identifier(c, &o, e, named_type, cycle_checker);
  867. switch (o.mode) {
  868. case Addressing_Invalid:
  869. break;
  870. case Addressing_Type: {
  871. type = o.type;
  872. goto end;
  873. } break;
  874. case Addressing_NoValue:
  875. err_str = expr_to_string(e);
  876. error(ast_node_token(e), "`%s` used as a type", err_str);
  877. break;
  878. default:
  879. err_str = expr_to_string(e);
  880. error(ast_node_token(e), "`%s` used as a type when not a type", err_str);
  881. break;
  882. }
  883. case_end;
  884. case_ast_node(se, SelectorExpr, e);
  885. Operand o = {};
  886. check_selector(c, &o, e);
  887. switch (o.mode) {
  888. case Addressing_Invalid:
  889. break;
  890. case Addressing_Type:
  891. GB_ASSERT(o.type != NULL);
  892. type = o.type;
  893. goto end;
  894. case Addressing_NoValue:
  895. err_str = expr_to_string(e);
  896. error(ast_node_token(e), "`%s` used as a type", err_str);
  897. break;
  898. default:
  899. err_str = expr_to_string(e);
  900. error(ast_node_token(e), "`%s` is not a type", err_str);
  901. break;
  902. }
  903. case_end;
  904. case_ast_node(pe, ParenExpr, e);
  905. type = check_type(c, pe->expr, named_type, cycle_checker);
  906. goto end;
  907. case_end;
  908. case_ast_node(ue, UnaryExpr, e);
  909. if (ue->op.kind == Token_Pointer) {
  910. type = make_type_pointer(c->allocator, check_type(c, ue->expr));
  911. goto end;
  912. } else if (ue->op.kind == Token_Maybe) {
  913. type = make_type_maybe(c->allocator, check_type(c, ue->expr));
  914. goto end;
  915. }
  916. case_end;
  917. case_ast_node(pt, PointerType, e);
  918. Type *elem = check_type(c, pt->type);
  919. type = make_type_pointer(c->allocator, elem);
  920. goto end;
  921. case_end;
  922. case_ast_node(mt, MaybeType, e);
  923. Type *elem = check_type(c, mt->type);
  924. type = make_type_maybe(c->allocator, elem);
  925. goto end;
  926. case_end;
  927. case_ast_node(at, ArrayType, e);
  928. if (at->count != NULL) {
  929. Type *elem = check_type(c, at->elem, NULL, cycle_checker);
  930. type = make_type_array(c->allocator, elem, check_array_count(c, at->count));
  931. } else {
  932. Type *elem = check_type(c, at->elem);
  933. type = make_type_slice(c->allocator, elem);
  934. }
  935. goto end;
  936. case_end;
  937. case_ast_node(vt, VectorType, e);
  938. Type *elem = check_type(c, vt->elem);
  939. Type *be = base_type(elem);
  940. i64 count = check_array_count(c, vt->count);
  941. if (!is_type_boolean(be) && !is_type_numeric(be)) {
  942. err_str = type_to_string(elem);
  943. error(ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
  944. }
  945. type = make_type_vector(c->allocator, elem, count);
  946. goto end;
  947. case_end;
  948. case_ast_node(st, StructType, e);
  949. type = make_type_struct(c->allocator);
  950. set_base_type(named_type, type);
  951. check_open_scope(c, e);
  952. check_struct_type(c, type, e, cycle_checker);
  953. check_close_scope(c);
  954. type->Record.node = e;
  955. goto end;
  956. case_end;
  957. case_ast_node(ut, UnionType, e);
  958. type = make_type_union(c->allocator);
  959. set_base_type(named_type, type);
  960. check_open_scope(c, e);
  961. check_union_type(c, type, e, cycle_checker);
  962. check_close_scope(c);
  963. type->Record.node = e;
  964. goto end;
  965. case_end;
  966. case_ast_node(rut, RawUnionType, e);
  967. type = make_type_raw_union(c->allocator);
  968. set_base_type(named_type, type);
  969. check_open_scope(c, e);
  970. check_raw_union_type(c, type, e, cycle_checker);
  971. check_close_scope(c);
  972. type->Record.node = e;
  973. goto end;
  974. case_end;
  975. case_ast_node(et, EnumType, e);
  976. type = make_type_enum(c->allocator);
  977. set_base_type(named_type, type);
  978. check_open_scope(c, e);
  979. check_enum_type(c, type, named_type, e);
  980. check_close_scope(c);
  981. type->Record.node = e;
  982. goto end;
  983. case_end;
  984. case_ast_node(pt, ProcType, e);
  985. type = alloc_type(c->allocator, Type_Proc);
  986. set_base_type(named_type, type);
  987. check_open_scope(c, e);
  988. check_procedure_type(c, type, e);
  989. check_close_scope(c);
  990. goto end;
  991. case_end;
  992. case_ast_node(ce, CallExpr, e);
  993. Operand o = {};
  994. check_expr_or_type(c, &o, e);
  995. if (o.mode == Addressing_Type) {
  996. type = o.type;
  997. goto end;
  998. }
  999. case_end;
  1000. }
  1001. err_str = expr_to_string(e);
  1002. error(ast_node_token(e), "`%s` is not a type", err_str);
  1003. type = t_invalid;
  1004. end:
  1005. if (type == NULL) {
  1006. type = t_invalid;
  1007. }
  1008. set_base_type(named_type, type);
  1009. GB_ASSERT(is_type_typed(type));
  1010. add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
  1011. return type;
  1012. }
  1013. b32 check_unary_op(Checker *c, Operand *o, Token op) {
  1014. // TODO(bill): Handle errors correctly
  1015. Type *type = base_type(base_vector_type(o->type));
  1016. gbString str = NULL;
  1017. defer (gb_string_free(str));
  1018. switch (op.kind) {
  1019. case Token_Add:
  1020. case Token_Sub:
  1021. if (!is_type_numeric(type)) {
  1022. str = expr_to_string(o->expr);
  1023. error(op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  1024. }
  1025. break;
  1026. case Token_Xor:
  1027. if (!is_type_integer(type)) {
  1028. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  1029. }
  1030. break;
  1031. case Token_Not:
  1032. if (!is_type_boolean(type)) {
  1033. str = expr_to_string(o->expr);
  1034. error(op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  1035. }
  1036. break;
  1037. default:
  1038. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1039. return false;
  1040. }
  1041. return true;
  1042. }
  1043. b32 check_binary_op(Checker *c, Operand *o, Token op) {
  1044. // TODO(bill): Handle errors correctly
  1045. Type *type = base_type(base_vector_type(o->type));
  1046. switch (op.kind) {
  1047. case Token_Sub:
  1048. case Token_SubEq:
  1049. if (!is_type_numeric(type) && !is_type_pointer(type)) {
  1050. error(op, "Operator `%.*s` is only allowed with numeric or pointer expressions", LIT(op.string));
  1051. return false;
  1052. }
  1053. if (is_type_pointer(type)) {
  1054. o->type = t_int;
  1055. }
  1056. if (base_type(type) == t_rawptr) {
  1057. gbString str = type_to_string(type);
  1058. defer (gb_string_free(str));
  1059. error(ast_node_token(o->expr), "Invalid pointer type for pointer arithmetic: `%s`", str);
  1060. return false;
  1061. }
  1062. break;
  1063. case Token_Add:
  1064. case Token_Mul:
  1065. case Token_Quo:
  1066. case Token_AddEq:
  1067. case Token_MulEq:
  1068. case Token_QuoEq:
  1069. if (!is_type_numeric(type)) {
  1070. error(op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  1071. return false;
  1072. }
  1073. break;
  1074. case Token_And:
  1075. case Token_Or:
  1076. case Token_AndEq:
  1077. case Token_OrEq:
  1078. if (!is_type_integer(type) && !is_type_boolean(type)) {
  1079. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  1080. return false;
  1081. }
  1082. break;
  1083. case Token_Mod:
  1084. case Token_Xor:
  1085. case Token_AndNot:
  1086. case Token_ModEq:
  1087. case Token_XorEq:
  1088. case Token_AndNotEq:
  1089. if (!is_type_integer(type)) {
  1090. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  1091. return false;
  1092. }
  1093. break;
  1094. case Token_CmpAnd:
  1095. case Token_CmpOr:
  1096. case Token_CmpAndEq:
  1097. case Token_CmpOrEq:
  1098. if (!is_type_boolean(type)) {
  1099. error(op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  1100. return false;
  1101. }
  1102. break;
  1103. default:
  1104. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1105. return false;
  1106. }
  1107. return true;
  1108. }
  1109. b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  1110. PROF_PROC();
  1111. if (in_value.kind == ExactValue_Invalid) {
  1112. // NOTE(bill): There's already been an error
  1113. return true;
  1114. }
  1115. if (is_type_boolean(type)) {
  1116. return in_value.kind == ExactValue_Bool;
  1117. } else if (is_type_string(type)) {
  1118. return in_value.kind == ExactValue_String;
  1119. } else if (is_type_integer(type)) {
  1120. ExactValue v = exact_value_to_integer(in_value);
  1121. if (v.kind != ExactValue_Integer) {
  1122. return false;
  1123. }
  1124. if (out_value) *out_value = v;
  1125. i64 i = v.value_integer;
  1126. u64 u = *cast(u64 *)&i;
  1127. i64 s = 8*type_size_of(c->sizes, c->allocator, type);
  1128. u64 umax = ~0ull;
  1129. if (s < 64) {
  1130. umax = (1ull << s) - 1ull;
  1131. }
  1132. i64 imax = (1ll << (s-1ll));
  1133. switch (type->Basic.kind) {
  1134. case Basic_i8:
  1135. case Basic_i16:
  1136. case Basic_i32:
  1137. case Basic_i64:
  1138. case Basic_int:
  1139. return gb_is_between(i, -imax, imax-1);
  1140. case Basic_u8:
  1141. case Basic_u16:
  1142. case Basic_u32:
  1143. case Basic_u64:
  1144. case Basic_uint:
  1145. return !(u < 0 || u > umax);
  1146. case Basic_UntypedInteger:
  1147. return true;
  1148. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  1149. }
  1150. } else if (is_type_float(type)) {
  1151. ExactValue v = exact_value_to_float(in_value);
  1152. if (v.kind != ExactValue_Float) {
  1153. return false;
  1154. }
  1155. switch (type->Basic.kind) {
  1156. case Basic_f32:
  1157. if (out_value) *out_value = v;
  1158. return true;
  1159. case Basic_f64:
  1160. if (out_value) *out_value = v;
  1161. return true;
  1162. case Basic_UntypedFloat:
  1163. return true;
  1164. }
  1165. } else if (is_type_pointer(type)) {
  1166. if (in_value.kind == ExactValue_Pointer)
  1167. return true;
  1168. if (in_value.kind == ExactValue_Integer)
  1169. return true;
  1170. if (out_value) *out_value = in_value;
  1171. }
  1172. return false;
  1173. }
  1174. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  1175. PROF_PROC();
  1176. GB_ASSERT(type->kind == Type_Basic);
  1177. GB_ASSERT(o->mode == Addressing_Constant);
  1178. if (!check_value_is_expressible(c, o->value, type, &o->value)) {
  1179. gbString a = expr_to_string(o->expr);
  1180. gbString b = type_to_string(type);
  1181. defer (gb_string_free(a));
  1182. defer (gb_string_free(b));
  1183. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1184. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1185. error(ast_node_token(o->expr), "`%s` truncated to `%s`", a, b);
  1186. } else {
  1187. error(ast_node_token(o->expr), "`%s = %lld` overflows `%s`", a, o->value.value_integer, b);
  1188. }
  1189. } else {
  1190. error(ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
  1191. }
  1192. o->mode = Addressing_Invalid;
  1193. }
  1194. }
  1195. b32 check_is_expr_vector_index(Checker *c, AstNode *expr) {
  1196. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1197. expr = unparen_expr(expr);
  1198. if (expr->kind == AstNode_IndexExpr) {
  1199. ast_node(ie, IndexExpr, expr);
  1200. Type *t = type_of_expr(&c->info, ie->expr);
  1201. if (t != NULL) {
  1202. return is_type_vector(base_type(t));
  1203. }
  1204. }
  1205. return false;
  1206. }
  1207. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  1208. PROF_PROC();
  1209. switch (op.kind) {
  1210. case Token_Pointer: { // Pointer address
  1211. if (o->mode != Addressing_Variable ||
  1212. check_is_expr_vector_index(c, o->expr)) {
  1213. ast_node(ue, UnaryExpr, node);
  1214. gbString str = expr_to_string(ue->expr);
  1215. defer (gb_string_free(str));
  1216. error(op, "Cannot take the pointer address of `%s`", str);
  1217. o->mode = Addressing_Invalid;
  1218. return;
  1219. }
  1220. o->mode = Addressing_Value;
  1221. o->type = make_type_pointer(c->allocator, o->type);
  1222. return;
  1223. }
  1224. case Token_Maybe: { // Make maybe
  1225. Type *t = default_type(o->type);
  1226. b32 is_value =
  1227. o->mode == Addressing_Variable ||
  1228. o->mode == Addressing_Value ||
  1229. o->mode == Addressing_Constant;
  1230. if (!is_value || is_type_untyped(t)) {
  1231. ast_node(ue, UnaryExpr, node);
  1232. gbString str = expr_to_string(ue->expr);
  1233. defer (gb_string_free(str));
  1234. error(op, "Cannot convert `%s` to a maybe", str);
  1235. o->mode = Addressing_Invalid;
  1236. return;
  1237. }
  1238. o->mode = Addressing_Value;
  1239. o->type = make_type_maybe(c->allocator, t);
  1240. return;
  1241. }
  1242. }
  1243. if (!check_unary_op(c, o, op)) {
  1244. o->mode = Addressing_Invalid;
  1245. return;
  1246. }
  1247. if (o->mode == Addressing_Constant) {
  1248. Type *type = base_type(o->type);
  1249. if (type->kind != Type_Basic) {
  1250. gbString xt = type_to_string(o->type);
  1251. gbString err_str = expr_to_string(node);
  1252. defer (gb_string_free(xt));
  1253. defer (gb_string_free(err_str));
  1254. error(op, "Invalid type, `%s`, for constant unary expression `%s`", xt, err_str);
  1255. o->mode = Addressing_Invalid;
  1256. return;
  1257. }
  1258. i32 precision = 0;
  1259. if (is_type_unsigned(type)) {
  1260. precision = cast(i32)(8 * type_size_of(c->sizes, c->allocator, type));
  1261. }
  1262. o->value = exact_unary_operator_value(op, o->value, precision);
  1263. if (is_type_typed(type)) {
  1264. if (node != NULL)
  1265. o->expr = node;
  1266. check_is_expressible(c, o, type);
  1267. }
  1268. return;
  1269. }
  1270. o->mode = Addressing_Value;
  1271. }
  1272. void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
  1273. PROF_PROC();
  1274. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1275. defer (gb_temp_arena_memory_end(tmp));
  1276. gbString err_str = NULL;
  1277. defer ({
  1278. if (err_str != NULL)
  1279. gb_string_free(err_str);
  1280. });
  1281. if (check_is_assignable_to(c, x, y->type) ||
  1282. check_is_assignable_to(c, y, x->type)) {
  1283. b32 defined = false;
  1284. switch (op.kind) {
  1285. case Token_CmpEq:
  1286. case Token_NotEq:
  1287. defined = is_type_comparable(base_type(x->type));
  1288. break;
  1289. case Token_Lt:
  1290. case Token_Gt:
  1291. case Token_LtEq:
  1292. case Token_GtEq: {
  1293. defined = is_type_ordered(base_type(x->type));
  1294. } break;
  1295. }
  1296. if (!defined) {
  1297. gbString type_string = type_to_string(x->type);
  1298. defer (gb_string_free(type_string));
  1299. err_str = gb_string_make(c->tmp_allocator,
  1300. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(op.string), type_string));
  1301. }
  1302. } else {
  1303. gbString xt = type_to_string(x->type);
  1304. gbString yt = type_to_string(y->type);
  1305. defer(gb_string_free(xt));
  1306. defer(gb_string_free(yt));
  1307. err_str = gb_string_make(c->tmp_allocator,
  1308. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  1309. }
  1310. if (err_str != NULL) {
  1311. error(op, "Cannot compare expression, %s", err_str);
  1312. x->type = t_untyped_bool;
  1313. return;
  1314. }
  1315. if (x->mode == Addressing_Constant &&
  1316. y->mode == Addressing_Constant) {
  1317. x->value = make_exact_value_bool(compare_exact_values(op, x->value, y->value));
  1318. } else {
  1319. x->mode = Addressing_Value;
  1320. update_expr_type(c, x->expr, default_type(x->type), true);
  1321. update_expr_type(c, y->expr, default_type(y->type), true);
  1322. }
  1323. if (is_type_vector(base_type(y->type))) {
  1324. x->type = make_type_vector(c->allocator, t_bool, base_type(y->type)->Vector.count);
  1325. } else {
  1326. x->type = t_untyped_bool;
  1327. }
  1328. }
  1329. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  1330. PROF_PROC();
  1331. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1332. ast_node(be, BinaryExpr, node);
  1333. ExactValue x_val = {};
  1334. if (x->mode == Addressing_Constant) {
  1335. x_val = exact_value_to_integer(x->value);
  1336. }
  1337. b32 x_is_untyped = is_type_untyped(x->type);
  1338. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  1339. gbString err_str = expr_to_string(x->expr);
  1340. defer (gb_string_free(err_str));
  1341. error(ast_node_token(node),
  1342. "Shifted operand `%s` must be an integer", err_str);
  1343. x->mode = Addressing_Invalid;
  1344. return;
  1345. }
  1346. if (is_type_unsigned(y->type)) {
  1347. } else if (is_type_untyped(y->type)) {
  1348. convert_to_typed(c, y, t_untyped_integer);
  1349. if (y->mode == Addressing_Invalid) {
  1350. x->mode = Addressing_Invalid;
  1351. return;
  1352. }
  1353. } else {
  1354. gbString err_str = expr_to_string(y->expr);
  1355. defer (gb_string_free(err_str));
  1356. error(ast_node_token(node),
  1357. "Shift amount `%s` must be an unsigned integer", err_str);
  1358. x->mode = Addressing_Invalid;
  1359. return;
  1360. }
  1361. if (x->mode == Addressing_Constant) {
  1362. if (y->mode == Addressing_Constant) {
  1363. ExactValue y_val = exact_value_to_integer(y->value);
  1364. if (y_val.kind != ExactValue_Integer) {
  1365. gbString err_str = expr_to_string(y->expr);
  1366. defer (gb_string_free(err_str));
  1367. error(ast_node_token(node),
  1368. "Shift amount `%s` must be an unsigned integer", err_str);
  1369. x->mode = Addressing_Invalid;
  1370. return;
  1371. }
  1372. u64 amount = cast(u64)y_val.value_integer;
  1373. if (amount > 1074) {
  1374. gbString err_str = expr_to_string(y->expr);
  1375. defer (gb_string_free(err_str));
  1376. error(ast_node_token(node),
  1377. "Shift amount too large: `%s`", err_str);
  1378. x->mode = Addressing_Invalid;
  1379. return;
  1380. }
  1381. if (!is_type_integer(x->type)) {
  1382. // NOTE(bill): It could be an untyped float but still representable
  1383. // as an integer
  1384. x->type = t_untyped_integer;
  1385. }
  1386. x->value = exact_value_shift(be->op, x_val, make_exact_value_integer(amount));
  1387. if (is_type_typed(x->type)) {
  1388. check_is_expressible(c, x, base_type(x->type));
  1389. }
  1390. return;
  1391. }
  1392. if (x_is_untyped) {
  1393. ExpressionInfo *info = map_get(&c->info.untyped, hash_pointer(x->expr));
  1394. if (info != NULL) {
  1395. info->is_lhs = true;
  1396. }
  1397. x->mode = Addressing_Value;
  1398. return;
  1399. }
  1400. }
  1401. if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
  1402. gbString err_str = expr_to_string(y->expr);
  1403. defer (gb_string_free(err_str));
  1404. error(ast_node_token(node),
  1405. "Shift amount cannot be negative: `%s`", err_str);
  1406. }
  1407. x->mode = Addressing_Value;
  1408. }
  1409. b32 check_is_castable_to(Checker *c, Operand *operand, Type *y) {
  1410. PROF_PROC();
  1411. if (check_is_assignable_to(c, operand, y)) {
  1412. return true;
  1413. }
  1414. Type *x = operand->type;
  1415. Type *xb = base_type(x);
  1416. Type *yb = base_type(y);
  1417. if (are_types_identical(xb, yb)) {
  1418. return true;
  1419. }
  1420. xb = get_enum_base_type(x);
  1421. yb = get_enum_base_type(y);
  1422. // Cast between booleans and integers
  1423. if (is_type_boolean(xb) || is_type_integer(xb)) {
  1424. if (is_type_boolean(yb) || is_type_integer(yb))
  1425. return true;
  1426. }
  1427. // Cast between numbers
  1428. if (is_type_integer(xb) || is_type_float(xb)) {
  1429. if (is_type_integer(yb) || is_type_float(yb))
  1430. return true;
  1431. }
  1432. // Cast between pointers
  1433. if (is_type_pointer(xb) && is_type_pointer(yb)) {
  1434. return true;
  1435. }
  1436. // (u)int <-> pointer
  1437. if (is_type_int_or_uint(xb) && is_type_rawptr(yb)) {
  1438. return true;
  1439. }
  1440. if (is_type_rawptr(xb) && is_type_int_or_uint(yb)) {
  1441. return true;
  1442. }
  1443. // []byte/[]u8 <-> string
  1444. if (is_type_u8_slice(xb) && is_type_string(yb)) {
  1445. return true;
  1446. }
  1447. if (is_type_string(xb) && is_type_u8_slice(yb)) {
  1448. if (is_type_typed(xb)) {
  1449. return true;
  1450. }
  1451. }
  1452. // proc <-> proc
  1453. if (is_type_proc(xb) && is_type_proc(yb)) {
  1454. return true;
  1455. }
  1456. // proc -> rawptr
  1457. if (is_type_proc(xb) && is_type_rawptr(yb)) {
  1458. return true;
  1459. }
  1460. return false;
  1461. }
  1462. String check_down_cast_name(Type *dst_, Type *src_) {
  1463. String result = {};
  1464. Type *dst = type_deref(dst_);
  1465. Type *src = type_deref(src_);
  1466. Type *dst_s = base_type(dst);
  1467. GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
  1468. for (isize i = 0; i < dst_s->Record.field_count; i++) {
  1469. Entity *f = dst_s->Record.fields[i];
  1470. GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
  1471. if (f->Variable.anonymous) {
  1472. if (are_types_identical(f->type, src_)) {
  1473. return f->token.string;
  1474. }
  1475. if (are_types_identical(type_deref(f->type), src_)) {
  1476. return f->token.string;
  1477. }
  1478. if (!is_type_pointer(f->type)) {
  1479. result = check_down_cast_name(f->type, src_);
  1480. if (result.len > 0)
  1481. return result;
  1482. }
  1483. }
  1484. }
  1485. return result;
  1486. }
  1487. Operand check_ptr_addition(Checker *c, TokenKind op, Operand *ptr, Operand *offset, AstNode *node) {
  1488. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1489. ast_node(be, BinaryExpr, node);
  1490. GB_ASSERT(is_type_pointer(ptr->type));
  1491. GB_ASSERT(is_type_integer(offset->type));
  1492. GB_ASSERT(op == Token_Add || op == Token_Sub);
  1493. Operand operand = {};
  1494. operand.mode = Addressing_Value;
  1495. operand.type = ptr->type;
  1496. operand.expr = node;
  1497. if (base_type(ptr->type) == t_rawptr) {
  1498. gbString str = type_to_string(ptr->type);
  1499. defer (gb_string_free(str));
  1500. error(ast_node_token(node), "Invalid pointer type for pointer arithmetic: `%s`", str);
  1501. operand.mode = Addressing_Invalid;
  1502. return operand;
  1503. }
  1504. if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
  1505. i64 elem_size = type_size_of(c->sizes, c->allocator, ptr->type);
  1506. i64 ptr_val = ptr->value.value_pointer;
  1507. i64 offset_val = exact_value_to_integer(offset->value).value_integer;
  1508. i64 new_ptr_val = ptr_val;
  1509. if (op == Token_Add) {
  1510. new_ptr_val += elem_size*offset_val;
  1511. } else {
  1512. new_ptr_val -= elem_size*offset_val;
  1513. }
  1514. operand.mode = Addressing_Constant;
  1515. operand.value = make_exact_value_pointer(new_ptr_val);
  1516. }
  1517. return operand;
  1518. }
  1519. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  1520. PROF_PROC();
  1521. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1522. Operand y_ = {}, *y = &y_;
  1523. gbString err_str = NULL;
  1524. defer (gb_string_free(err_str));
  1525. ast_node(be, BinaryExpr, node);
  1526. if (be->op.kind == Token_as) {
  1527. check_expr(c, x, be->left);
  1528. Type *type = check_type(c, be->right);
  1529. if (x->mode == Addressing_Invalid)
  1530. return;
  1531. b32 is_const_expr = x->mode == Addressing_Constant;
  1532. b32 can_convert = false;
  1533. Type *bt = base_type(type);
  1534. if (is_const_expr && is_type_constant_type(bt)) {
  1535. if (bt->kind == Type_Basic) {
  1536. if (check_value_is_expressible(c, x->value, bt, &x->value)) {
  1537. can_convert = true;
  1538. }
  1539. }
  1540. } else if (check_is_castable_to(c, x, type)) {
  1541. if (x->mode != Addressing_Constant) {
  1542. x->mode = Addressing_Value;
  1543. }
  1544. can_convert = true;
  1545. }
  1546. if (!can_convert) {
  1547. gbString expr_str = expr_to_string(x->expr);
  1548. gbString to_type = type_to_string(type);
  1549. gbString from_type = type_to_string(x->type);
  1550. defer (gb_string_free(expr_str));
  1551. defer (gb_string_free(to_type));
  1552. defer (gb_string_free(from_type));
  1553. error(ast_node_token(x->expr), "Cannot cast `%s` as `%s` from `%s`", expr_str, to_type, from_type);
  1554. x->mode = Addressing_Invalid;
  1555. return;
  1556. }
  1557. if (is_type_untyped(x->type)) {
  1558. Type *final_type = type;
  1559. if (is_const_expr && !is_type_constant_type(type)) {
  1560. final_type = default_type(x->type);
  1561. }
  1562. update_expr_type(c, x->expr, final_type, true);
  1563. }
  1564. x->type = type;
  1565. return;
  1566. } else if (be->op.kind == Token_transmute) {
  1567. check_expr(c, x, be->left);
  1568. Type *type = check_type(c, be->right);
  1569. if (x->mode == Addressing_Invalid)
  1570. return;
  1571. if (x->mode == Addressing_Constant) {
  1572. gbString expr_str = expr_to_string(x->expr);
  1573. defer (gb_string_free(expr_str));
  1574. error(ast_node_token(x->expr), "Cannot transmute constant expression: `%s`", expr_str);
  1575. x->mode = Addressing_Invalid;
  1576. return;
  1577. }
  1578. if (is_type_untyped(x->type)) {
  1579. gbString expr_str = expr_to_string(x->expr);
  1580. defer (gb_string_free(expr_str));
  1581. error(ast_node_token(x->expr), "Cannot transmute untyped expression: `%s`", expr_str);
  1582. x->mode = Addressing_Invalid;
  1583. return;
  1584. }
  1585. i64 srcz = type_size_of(c->sizes, c->allocator, x->type);
  1586. i64 dstz = type_size_of(c->sizes, c->allocator, type);
  1587. if (srcz != dstz) {
  1588. gbString expr_str = expr_to_string(x->expr);
  1589. gbString type_str = type_to_string(type);
  1590. defer (gb_string_free(expr_str));
  1591. defer (gb_string_free(type_str));
  1592. error(ast_node_token(x->expr), "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  1593. x->mode = Addressing_Invalid;
  1594. return;
  1595. }
  1596. x->type = type;
  1597. return;
  1598. } else if (be->op.kind == Token_down_cast) {
  1599. check_expr(c, x, be->left);
  1600. Type *type = check_type(c, be->right);
  1601. if (x->mode == Addressing_Invalid) {
  1602. return;
  1603. }
  1604. if (x->mode == Addressing_Constant) {
  1605. gbString expr_str = expr_to_string(node);
  1606. defer (gb_string_free(expr_str));
  1607. error(ast_node_token(node), "Cannot `down_cast` a constant expression: `%s`", expr_str);
  1608. x->mode = Addressing_Invalid;
  1609. return;
  1610. }
  1611. if (is_type_untyped(x->type)) {
  1612. gbString expr_str = expr_to_string(node);
  1613. defer (gb_string_free(expr_str));
  1614. error(ast_node_token(node), "Cannot `down_cast` an untyped expression: `%s`", expr_str);
  1615. x->mode = Addressing_Invalid;
  1616. return;
  1617. }
  1618. if (!(is_type_pointer(x->type) && is_type_pointer(type))) {
  1619. gbString expr_str = expr_to_string(node);
  1620. defer (gb_string_free(expr_str));
  1621. error(ast_node_token(node), "Can only `down_cast` pointers: `%s`", expr_str);
  1622. x->mode = Addressing_Invalid;
  1623. return;
  1624. }
  1625. Type *src = type_deref(x->type);
  1626. Type *dst = type_deref(type);
  1627. Type *bsrc = base_type(src);
  1628. Type *bdst = base_type(dst);
  1629. if (!(is_type_struct(bsrc) || is_type_raw_union(bsrc))) {
  1630. gbString expr_str = expr_to_string(node);
  1631. defer (gb_string_free(expr_str));
  1632. error(ast_node_token(node), "Can only `down_cast` pointer from structs or unions: `%s`", expr_str);
  1633. x->mode = Addressing_Invalid;
  1634. return;
  1635. }
  1636. if (!(is_type_struct(bdst) || is_type_raw_union(bdst))) {
  1637. gbString expr_str = expr_to_string(node);
  1638. defer (gb_string_free(expr_str));
  1639. error(ast_node_token(node), "Can only `down_cast` pointer to structs or unions: `%s`", expr_str);
  1640. x->mode = Addressing_Invalid;
  1641. return;
  1642. }
  1643. String param_name = check_down_cast_name(dst, src);
  1644. if (param_name.len == 0) {
  1645. gbString expr_str = expr_to_string(node);
  1646. defer (gb_string_free(expr_str));
  1647. error(ast_node_token(node), "Illegal `down_cast`: `%s`", expr_str);
  1648. x->mode = Addressing_Invalid;
  1649. return;
  1650. }
  1651. x->mode = Addressing_Value;
  1652. x->type = type;
  1653. return;
  1654. } else if (be->op.kind == Token_union_cast) {
  1655. check_expr(c, x, be->left);
  1656. Type *type = check_type(c, be->right);
  1657. if (x->mode == Addressing_Invalid) {
  1658. return;
  1659. }
  1660. if (x->mode == Addressing_Constant) {
  1661. gbString expr_str = expr_to_string(node);
  1662. defer (gb_string_free(expr_str));
  1663. error(ast_node_token(node), "Cannot `union_cast` a constant expression: `%s`", expr_str);
  1664. x->mode = Addressing_Invalid;
  1665. return;
  1666. }
  1667. if (is_type_untyped(x->type)) {
  1668. gbString expr_str = expr_to_string(node);
  1669. defer (gb_string_free(expr_str));
  1670. error(ast_node_token(node), "Cannot `union_cast` an untyped expression: `%s`", expr_str);
  1671. x->mode = Addressing_Invalid;
  1672. return;
  1673. }
  1674. b32 src_is_ptr = is_type_pointer(x->type);
  1675. b32 dst_is_ptr = is_type_pointer(type);
  1676. Type *src = type_deref(x->type);
  1677. Type *dst = type_deref(type);
  1678. Type *bsrc = base_type(src);
  1679. Type *bdst = base_type(dst);
  1680. if (src_is_ptr != dst_is_ptr) {
  1681. gbString src_type_str = type_to_string(x->type);
  1682. gbString dst_type_str = type_to_string(type);
  1683. defer (gb_string_free(src_type_str));
  1684. defer (gb_string_free(dst_type_str));
  1685. error(ast_node_token(node), "Invalid `union_cast` types: `%s` and `%s`", src_type_str, dst_type_str);
  1686. x->mode = Addressing_Invalid;
  1687. return;
  1688. }
  1689. if (!is_type_union(src)) {
  1690. error(ast_node_token(node), "`union_cast` can only operate on unions");
  1691. x->mode = Addressing_Invalid;
  1692. return;
  1693. }
  1694. b32 ok = false;
  1695. for (isize i = 1; i < bsrc->Record.field_count; i++) {
  1696. Entity *f = bsrc->Record.fields[i];
  1697. if (are_types_identical(f->type, dst)) {
  1698. ok = true;
  1699. break;
  1700. }
  1701. }
  1702. if (!ok) {
  1703. gbString expr_str = expr_to_string(node);
  1704. gbString dst_type_str = type_to_string(type);
  1705. defer (gb_string_free(expr_str));
  1706. defer (gb_string_free(dst_type_str));
  1707. error(ast_node_token(node), "Cannot `union_cast` `%s` to `%s`", expr_str, dst_type_str);
  1708. x->mode = Addressing_Invalid;
  1709. return;
  1710. }
  1711. Entity **variables = gb_alloc_array(c->allocator, Entity *, 2);
  1712. Token tok = make_token_ident(make_string(""));
  1713. variables[0] = make_entity_param(c->allocator, NULL, tok, type, false);
  1714. variables[1] = make_entity_param(c->allocator, NULL, tok, t_bool, false);
  1715. Type *tuple = make_type_tuple(c->allocator);
  1716. tuple->Tuple.variables = variables;
  1717. tuple->Tuple.variable_count = 2;
  1718. x->type = tuple;
  1719. x->mode = Addressing_Value;
  1720. return;
  1721. }
  1722. check_expr(c, x, be->left);
  1723. check_expr(c, y, be->right);
  1724. if (x->mode == Addressing_Invalid) {
  1725. return;
  1726. }
  1727. if (y->mode == Addressing_Invalid) {
  1728. x->mode = Addressing_Invalid;
  1729. x->expr = y->expr;
  1730. return;
  1731. }
  1732. Token op = be->op;
  1733. if (token_is_shift(op)) {
  1734. check_shift(c, x, y, node);
  1735. return;
  1736. }
  1737. if (op.kind == Token_Add || op.kind == Token_Sub) {
  1738. if (is_type_pointer(x->type) && is_type_integer(y->type)) {
  1739. *x = check_ptr_addition(c, op.kind, x, y, node);
  1740. return;
  1741. } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
  1742. if (op.kind == Token_Sub) {
  1743. gbString lhs = expr_to_string(x->expr);
  1744. gbString rhs = expr_to_string(y->expr);
  1745. defer (gb_string_free(lhs));
  1746. defer (gb_string_free(rhs));
  1747. error(ast_node_token(node), "Invalid pointer arithmetic, did you mean `%s %.*s %s`?", rhs, LIT(op.string), lhs);
  1748. x->mode = Addressing_Invalid;
  1749. return;
  1750. }
  1751. *x = check_ptr_addition(c, op.kind, y, x, node);
  1752. return;
  1753. }
  1754. }
  1755. convert_to_typed(c, x, y->type);
  1756. if (x->mode == Addressing_Invalid) {
  1757. return;
  1758. }
  1759. convert_to_typed(c, y, x->type);
  1760. if (y->mode == Addressing_Invalid) {
  1761. x->mode = Addressing_Invalid;
  1762. return;
  1763. }
  1764. if (token_is_comparison(op)) {
  1765. check_comparison(c, x, y, op);
  1766. return;
  1767. }
  1768. if (!are_types_identical(x->type, y->type)) {
  1769. if (x->type != t_invalid &&
  1770. y->type != t_invalid) {
  1771. gbString xt = type_to_string(x->type);
  1772. gbString yt = type_to_string(y->type);
  1773. defer (gb_string_free(xt));
  1774. defer (gb_string_free(yt));
  1775. err_str = expr_to_string(x->expr);
  1776. error(op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", err_str, xt, yt);
  1777. }
  1778. x->mode = Addressing_Invalid;
  1779. return;
  1780. }
  1781. if (!check_binary_op(c, x, op)) {
  1782. x->mode = Addressing_Invalid;
  1783. return;
  1784. }
  1785. switch (op.kind) {
  1786. case Token_Quo:
  1787. case Token_Mod:
  1788. case Token_QuoEq:
  1789. case Token_ModEq:
  1790. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  1791. y->mode == Addressing_Constant) {
  1792. b32 fail = false;
  1793. switch (y->value.kind) {
  1794. case ExactValue_Integer:
  1795. if (y->value.value_integer == 0) {
  1796. fail = true;
  1797. }
  1798. break;
  1799. case ExactValue_Float:
  1800. if (y->value.value_float == 0.0) {
  1801. fail = true;
  1802. }
  1803. break;
  1804. }
  1805. if (fail) {
  1806. error(ast_node_token(y->expr), "Division by zero not allowed");
  1807. x->mode = Addressing_Invalid;
  1808. return;
  1809. }
  1810. }
  1811. }
  1812. if (x->mode == Addressing_Constant &&
  1813. y->mode == Addressing_Constant) {
  1814. ExactValue a = x->value;
  1815. ExactValue b = y->value;
  1816. Type *type = base_type(x->type);
  1817. if (is_type_pointer(type)) {
  1818. GB_ASSERT(op.kind == Token_Sub);
  1819. i64 bytes = a.value_pointer - b.value_pointer;
  1820. i64 diff = bytes/type_size_of(c->sizes, c->allocator, type);
  1821. x->value = make_exact_value_pointer(diff);
  1822. return;
  1823. }
  1824. if (type->kind != Type_Basic) {
  1825. gbString xt = type_to_string(x->type);
  1826. defer (gb_string_free(xt));
  1827. err_str = expr_to_string(node);
  1828. error(op, "Invalid type, `%s`, for constant binary expression `%s`", xt, err_str);
  1829. x->mode = Addressing_Invalid;
  1830. return;
  1831. }
  1832. if (op.kind == Token_Quo && is_type_integer(type)) {
  1833. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  1834. }
  1835. x->value = exact_binary_operator_value(op, a, b);
  1836. if (is_type_typed(type)) {
  1837. if (node != NULL)
  1838. x->expr = node;
  1839. check_is_expressible(c, x, type);
  1840. }
  1841. return;
  1842. }
  1843. x->mode = Addressing_Value;
  1844. }
  1845. void update_expr_type(Checker *c, AstNode *e, Type *type, b32 final) {
  1846. PROF_PROC();
  1847. HashKey key = hash_pointer(e);
  1848. ExpressionInfo *found = map_get(&c->info.untyped, key);
  1849. if (found == NULL)
  1850. return;
  1851. switch (e->kind) {
  1852. case_ast_node(ue, UnaryExpr, e);
  1853. if (found->value.kind != ExactValue_Invalid)
  1854. break;
  1855. update_expr_type(c, ue->expr, type, final);
  1856. case_end;
  1857. case_ast_node(be, BinaryExpr, e);
  1858. if (found->value.kind != ExactValue_Invalid)
  1859. break;
  1860. if (!token_is_comparison(be->op)) {
  1861. if (token_is_shift(be->op)) {
  1862. update_expr_type(c, be->left, type, final);
  1863. } else {
  1864. update_expr_type(c, be->left, type, final);
  1865. update_expr_type(c, be->right, type, final);
  1866. }
  1867. }
  1868. case_end;
  1869. }
  1870. if (!final && is_type_untyped(type)) {
  1871. found->type = base_type(type);
  1872. map_set(&c->info.untyped, key, *found);
  1873. } else {
  1874. ExpressionInfo old = *found;
  1875. map_remove(&c->info.untyped, key);
  1876. if (old.is_lhs && !is_type_integer(type)) {
  1877. gbString expr_str = expr_to_string(e);
  1878. gbString type_str = type_to_string(type);
  1879. defer (gb_string_free(expr_str));
  1880. defer (gb_string_free(type_str));
  1881. error(ast_node_token(e), "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  1882. return;
  1883. }
  1884. add_type_and_value(&c->info, e, found->mode, type, found->value);
  1885. }
  1886. }
  1887. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  1888. ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
  1889. if (found)
  1890. found->value = value;
  1891. }
  1892. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  1893. gbString expr_str = expr_to_string(operand->expr);
  1894. gbString type_str = type_to_string(target_type);
  1895. char *extra_text = "";
  1896. defer (gb_string_free(expr_str));
  1897. defer (gb_string_free(type_str));
  1898. if (operand->mode == Addressing_Constant) {
  1899. if (operand->value.value_integer == 0) {
  1900. if (make_string(expr_str) != "nil") { // HACK NOTE(bill): Just in case
  1901. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  1902. extra_text = " - Did you want `nil`?";
  1903. }
  1904. }
  1905. }
  1906. error(ast_node_token(operand->expr), "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  1907. operand->mode = Addressing_Invalid;
  1908. }
  1909. void convert_to_typed(Checker *c, Operand *operand, Type *target_type, i32 level) {
  1910. PROF_PROC();
  1911. GB_ASSERT_NOT_NULL(target_type);
  1912. if (operand->mode == Addressing_Invalid ||
  1913. is_type_typed(operand->type) ||
  1914. target_type == t_invalid) {
  1915. return;
  1916. }
  1917. if (is_type_untyped(target_type)) {
  1918. Type *x = operand->type;
  1919. Type *y = target_type;
  1920. if (is_type_numeric(x) && is_type_numeric(y)) {
  1921. if (x < y) {
  1922. operand->type = target_type;
  1923. update_expr_type(c, operand->expr, target_type, false);
  1924. }
  1925. } else if (x != y) {
  1926. convert_untyped_error(c, operand, target_type);
  1927. }
  1928. return;
  1929. }
  1930. Type *t = get_enum_base_type(base_type(target_type));
  1931. switch (t->kind) {
  1932. case Type_Basic:
  1933. if (operand->mode == Addressing_Constant) {
  1934. check_is_expressible(c, operand, t);
  1935. if (operand->mode == Addressing_Invalid) {
  1936. return;
  1937. }
  1938. update_expr_value(c, operand->expr, operand->value);
  1939. } else {
  1940. switch (operand->type->Basic.kind) {
  1941. case Basic_UntypedBool:
  1942. if (!is_type_boolean(target_type)) {
  1943. convert_untyped_error(c, operand, target_type);
  1944. return;
  1945. }
  1946. break;
  1947. case Basic_UntypedInteger:
  1948. case Basic_UntypedFloat:
  1949. case Basic_UntypedRune:
  1950. if (!is_type_numeric(target_type)) {
  1951. convert_untyped_error(c, operand, target_type);
  1952. return;
  1953. }
  1954. break;
  1955. case Basic_UntypedNil:
  1956. if (!type_has_nil(target_type)) {
  1957. convert_untyped_error(c, operand, target_type);
  1958. return;
  1959. }
  1960. break;
  1961. }
  1962. }
  1963. break;
  1964. case Type_Maybe:
  1965. if (is_type_untyped_nil(operand->type)) {
  1966. // Okay
  1967. } else if (level == 0) {
  1968. convert_to_typed(c, operand, t->Maybe.elem, level+1);
  1969. return;
  1970. }
  1971. default:
  1972. if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  1973. convert_untyped_error(c, operand, target_type);
  1974. return;
  1975. }
  1976. break;
  1977. }
  1978. operand->type = target_type;
  1979. }
  1980. b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *value) {
  1981. PROF_PROC();
  1982. Operand operand = {Addressing_Invalid};
  1983. check_expr(c, &operand, index_value);
  1984. if (operand.mode == Addressing_Invalid) {
  1985. if (value) *value = 0;
  1986. return false;
  1987. }
  1988. convert_to_typed(c, &operand, t_int);
  1989. if (operand.mode == Addressing_Invalid) {
  1990. if (value) *value = 0;
  1991. return false;
  1992. }
  1993. if (!is_type_integer(get_enum_base_type(operand.type))) {
  1994. gbString expr_str = expr_to_string(operand.expr);
  1995. error(ast_node_token(operand.expr),
  1996. "Index `%s` must be an integer", expr_str);
  1997. gb_string_free(expr_str);
  1998. if (value) *value = 0;
  1999. return false;
  2000. }
  2001. if (operand.mode == Addressing_Constant &&
  2002. (c->context.stmt_state_flags & StmtStateFlag_bounds_check) != 0) {
  2003. i64 i = exact_value_to_integer(operand.value).value_integer;
  2004. if (i < 0) {
  2005. gbString expr_str = expr_to_string(operand.expr);
  2006. error(ast_node_token(operand.expr),
  2007. "Index `%s` cannot be a negative value", expr_str);
  2008. gb_string_free(expr_str);
  2009. if (value) *value = 0;
  2010. return false;
  2011. }
  2012. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  2013. if (value) *value = i;
  2014. if (i >= max_count) {
  2015. gbString expr_str = expr_to_string(operand.expr);
  2016. error(ast_node_token(operand.expr),
  2017. "Index `%s` is out of bounds range 0..<%lld", expr_str, max_count);
  2018. gb_string_free(expr_str);
  2019. return false;
  2020. }
  2021. return true;
  2022. }
  2023. }
  2024. // NOTE(bill): It's alright :D
  2025. if (value) *value = -1;
  2026. return true;
  2027. }
  2028. Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
  2029. PROF_PROC();
  2030. ast_node(se, SelectorExpr, node);
  2031. b32 check_op_expr = true;
  2032. Entity *entity = NULL;
  2033. Selection sel = {}; // NOTE(bill): Not used if it's an import name
  2034. AstNode *op_expr = se->expr;
  2035. AstNode *selector = unparen_expr(se->selector);
  2036. if (selector == NULL) {
  2037. goto error;
  2038. }
  2039. GB_ASSERT(selector->kind == AstNode_Ident);
  2040. if (op_expr->kind == AstNode_Ident) {
  2041. String name = op_expr->Ident.string;
  2042. Entity *e = scope_lookup_entity(c->context.scope, name);
  2043. add_entity_use(c, op_expr, e);
  2044. if (e != NULL && e->kind == Entity_ImportName) {
  2045. String sel_name = selector->Ident.string;
  2046. check_op_expr = false;
  2047. entity = scope_lookup_entity(e->ImportName.scope, sel_name);
  2048. if (entity == NULL) {
  2049. error(ast_node_token(op_expr), "`%.*s` is not declared by `%.*s`", LIT(sel_name), LIT(name));
  2050. goto error;
  2051. }
  2052. if (entity->type == NULL) { // Not setup yet
  2053. check_entity_decl(c, entity, NULL, NULL);
  2054. }
  2055. GB_ASSERT(entity->type != NULL);
  2056. b32 is_not_exported = !is_entity_exported(entity);
  2057. if (is_not_exported) {
  2058. auto found = map_get(&e->ImportName.scope->implicit, hash_string(sel_name));
  2059. if (!found && e->ImportName.scope != entity->scope) {
  2060. is_not_exported = false;
  2061. }
  2062. }
  2063. if (is_not_exported) {
  2064. gbString sel_str = expr_to_string(selector);
  2065. defer (gb_string_free(sel_str));
  2066. error(ast_node_token(op_expr), "`%s` is not exported by `%.*s`", sel_str, LIT(name));
  2067. // NOTE(bill): Not really an error so don't goto error
  2068. }
  2069. add_entity_use(c, selector, entity);
  2070. }
  2071. }
  2072. if (check_op_expr) {
  2073. check_expr_base(c, operand, op_expr);
  2074. if (operand->mode == Addressing_Invalid) {
  2075. goto error;
  2076. }
  2077. }
  2078. if (entity == NULL) {
  2079. sel = lookup_field(c->allocator, operand->type, selector->Ident.string, operand->mode == Addressing_Type);
  2080. entity = sel.entity;
  2081. }
  2082. if (entity == NULL) {
  2083. gbString op_str = expr_to_string(op_expr);
  2084. gbString type_str = type_to_string(operand->type);
  2085. gbString sel_str = expr_to_string(selector);
  2086. defer (gb_string_free(op_str));
  2087. defer (gb_string_free(type_str));
  2088. defer (gb_string_free(sel_str));
  2089. error(ast_node_token(op_expr), "`%s` (`%s`) has no field `%s`", op_str, type_str, sel_str);
  2090. goto error;
  2091. }
  2092. add_entity_use(c, selector, entity);
  2093. switch (entity->kind) {
  2094. case Entity_Constant:
  2095. operand->mode = Addressing_Constant;
  2096. operand->value = entity->Constant.value;
  2097. break;
  2098. case Entity_Variable:
  2099. // TODO(bill): This is the rule I need?
  2100. if (sel.indirect || operand->mode != Addressing_Value) {
  2101. operand->mode = Addressing_Variable;
  2102. }
  2103. break;
  2104. case Entity_TypeName:
  2105. operand->mode = Addressing_Type;
  2106. break;
  2107. case Entity_Procedure:
  2108. operand->mode = Addressing_Value;
  2109. break;
  2110. case Entity_Builtin:
  2111. operand->mode = Addressing_Builtin;
  2112. operand->builtin_id = entity->Builtin.id;
  2113. break;
  2114. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  2115. case Entity_Nil:
  2116. operand->mode = Addressing_Value;
  2117. break;
  2118. case Entity_ImplicitValue:
  2119. operand->mode = Addressing_Value;
  2120. break;
  2121. }
  2122. operand->type = entity->type;
  2123. operand->expr = node;
  2124. return entity;
  2125. error:
  2126. operand->mode = Addressing_Invalid;
  2127. operand->expr = node;
  2128. return NULL;
  2129. }
  2130. b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  2131. PROF_PROC();
  2132. GB_ASSERT(call->kind == AstNode_CallExpr);
  2133. ast_node(ce, CallExpr, call);
  2134. BuiltinProc *bp = &builtin_procs[id];
  2135. {
  2136. char *err = NULL;
  2137. if (ce->args.count < bp->arg_count) {
  2138. err = "Too few";
  2139. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  2140. err = "Too many";
  2141. }
  2142. if (err) {
  2143. ast_node(proc, Ident, ce->proc);
  2144. error(ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
  2145. err, LIT(proc->string),
  2146. bp->arg_count, ce->args.count);
  2147. return false;
  2148. }
  2149. }
  2150. switch (id) {
  2151. case BuiltinProc_new:
  2152. case BuiltinProc_new_slice:
  2153. case BuiltinProc_size_of:
  2154. case BuiltinProc_align_of:
  2155. case BuiltinProc_offset_of:
  2156. case BuiltinProc_type_info:
  2157. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  2158. break;
  2159. default:
  2160. check_multi_expr(c, operand, ce->args[0]);
  2161. }
  2162. switch (id) {
  2163. case BuiltinProc_new: {
  2164. // new :: proc(Type) -> ^Type
  2165. Operand op = {};
  2166. check_expr_or_type(c, &op, ce->args[0]);
  2167. Type *type = op.type;
  2168. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  2169. error(ast_node_token(ce->args[0]), "Expected a type for `new`");
  2170. return false;
  2171. }
  2172. operand->mode = Addressing_Value;
  2173. operand->type = make_type_pointer(c->allocator, type);
  2174. } break;
  2175. case BuiltinProc_new_slice: {
  2176. // new_slice :: proc(Type, len: int[, cap: int]) -> []Type
  2177. Operand op = {};
  2178. check_expr_or_type(c, &op, ce->args[0]);
  2179. Type *type = op.type;
  2180. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  2181. error(ast_node_token(ce->args[0]), "Expected a type for `new_slice`");
  2182. return false;
  2183. }
  2184. AstNode *len = ce->args[1];
  2185. AstNode *cap = NULL;
  2186. if (ce->args.count > 2) {
  2187. cap = ce->args[2];
  2188. }
  2189. check_expr(c, &op, len);
  2190. if (op.mode == Addressing_Invalid)
  2191. return false;
  2192. if (!is_type_integer(op.type)) {
  2193. gbString type_str = type_to_string(operand->type);
  2194. defer (gb_string_free(type_str));
  2195. error(ast_node_token(call),
  2196. "Length for `new_slice` must be an integer, got `%s`",
  2197. type_str);
  2198. return false;
  2199. }
  2200. if (cap != NULL) {
  2201. check_expr(c, &op, cap);
  2202. if (op.mode == Addressing_Invalid)
  2203. return false;
  2204. if (!is_type_integer(op.type)) {
  2205. gbString type_str = type_to_string(operand->type);
  2206. defer (gb_string_free(type_str));
  2207. error(ast_node_token(call),
  2208. "Capacity for `new_slice` must be an integer, got `%s`",
  2209. type_str);
  2210. return false;
  2211. }
  2212. if (ce->args.count > 3) {
  2213. error(ast_node_token(call),
  2214. "Too many arguments to `new_slice`, expected either 2 or 3");
  2215. return false;
  2216. }
  2217. }
  2218. operand->mode = Addressing_Value;
  2219. operand->type = make_type_slice(c->allocator, type);
  2220. } break;
  2221. case BuiltinProc_size_of: {
  2222. // size_of :: proc(Type) -> int
  2223. Type *type = check_type(c, ce->args[0]);
  2224. if (type == NULL || type == t_invalid) {
  2225. error(ast_node_token(ce->args[0]), "Expected a type for `size_of`");
  2226. return false;
  2227. }
  2228. operand->mode = Addressing_Constant;
  2229. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, type));
  2230. operand->type = t_int;
  2231. } break;
  2232. case BuiltinProc_size_of_val:
  2233. // size_of_val :: proc(val: Type) -> int
  2234. check_assignment(c, operand, NULL, make_string("argument of `size_of_val`"));
  2235. if (operand->mode == Addressing_Invalid) {
  2236. return false;
  2237. }
  2238. operand->mode = Addressing_Constant;
  2239. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, operand->type));
  2240. operand->type = t_int;
  2241. break;
  2242. case BuiltinProc_align_of: {
  2243. // align_of :: proc(Type) -> int
  2244. Type *type = check_type(c, ce->args[0]);
  2245. if (type == NULL || type == t_invalid) {
  2246. error(ast_node_token(ce->args[0]), "Expected a type for `align_of`");
  2247. return false;
  2248. }
  2249. operand->mode = Addressing_Constant;
  2250. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, type));
  2251. operand->type = t_int;
  2252. } break;
  2253. case BuiltinProc_align_of_val:
  2254. // align_of_val :: proc(val: Type) -> int
  2255. check_assignment(c, operand, NULL, make_string("argument of `align_of_val`"));
  2256. if (operand->mode == Addressing_Invalid) {
  2257. return false;
  2258. }
  2259. operand->mode = Addressing_Constant;
  2260. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, operand->type));
  2261. operand->type = t_int;
  2262. break;
  2263. case BuiltinProc_offset_of: {
  2264. // offset_of :: proc(Type, field) -> int
  2265. Operand op = {};
  2266. Type *bt = check_type(c, ce->args[0]);
  2267. Type *type = base_type(bt);
  2268. if (type == NULL || type == t_invalid) {
  2269. error(ast_node_token(ce->args[0]), "Expected a type for `offset_of`");
  2270. return false;
  2271. }
  2272. AstNode *field_arg = unparen_expr(ce->args[1]);
  2273. if (field_arg == NULL ||
  2274. field_arg->kind != AstNode_Ident) {
  2275. error(ast_node_token(field_arg), "Expected an identifier for field argument");
  2276. return false;
  2277. }
  2278. if (is_type_array(type) || is_type_vector(type)) {
  2279. error(ast_node_token(field_arg), "Invalid type for `offset_of`");
  2280. return false;
  2281. }
  2282. ast_node(arg, Ident, field_arg);
  2283. Selection sel = lookup_field(c->allocator, type, arg->string, operand->mode == Addressing_Type);
  2284. if (sel.entity == NULL) {
  2285. gbString type_str = type_to_string(bt);
  2286. defer (gb_string_free(type_str));
  2287. error(ast_node_token(ce->args[0]),
  2288. "`%s` has no field named `%.*s`", type_str, LIT(arg->string));
  2289. return false;
  2290. }
  2291. if (sel.indirect) {
  2292. gbString type_str = type_to_string(bt);
  2293. defer (gb_string_free(type_str));
  2294. error(ast_node_token(ce->args[0]),
  2295. "Field `%.*s` is embedded via a pointer in `%s`", LIT(arg->string), type_str);
  2296. return false;
  2297. }
  2298. operand->mode = Addressing_Constant;
  2299. operand->value = make_exact_value_integer(type_offset_of_from_selection(c->sizes, c->allocator, type, sel));
  2300. operand->type = t_int;
  2301. } break;
  2302. case BuiltinProc_offset_of_val: {
  2303. // offset_of_val :: proc(val: expression) -> int
  2304. AstNode *arg = unparen_expr(ce->args[0]);
  2305. if (arg->kind != AstNode_SelectorExpr) {
  2306. gbString str = expr_to_string(arg);
  2307. error(ast_node_token(arg), "`%s` is not a selector expression", str);
  2308. return false;
  2309. }
  2310. ast_node(s, SelectorExpr, arg);
  2311. check_expr(c, operand, s->expr);
  2312. if (operand->mode == Addressing_Invalid)
  2313. return false;
  2314. Type *type = operand->type;
  2315. if (base_type(type)->kind == Type_Pointer) {
  2316. Type *p = base_type(type);
  2317. if (is_type_struct(p)) {
  2318. type = p->Pointer.elem;
  2319. }
  2320. }
  2321. if (is_type_array(type) || is_type_vector(type)) {
  2322. error(ast_node_token(arg), "Invalid type for `offset_of_val`");
  2323. return false;
  2324. }
  2325. ast_node(i, Ident, s->selector);
  2326. Selection sel = lookup_field(c->allocator, type, i->string, operand->mode == Addressing_Type);
  2327. if (sel.entity == NULL) {
  2328. gbString type_str = type_to_string(type);
  2329. error(ast_node_token(arg),
  2330. "`%s` has no field named `%.*s`", type_str, LIT(i->string));
  2331. return false;
  2332. }
  2333. if (sel.indirect) {
  2334. gbString type_str = type_to_string(type);
  2335. defer (gb_string_free(type_str));
  2336. error(ast_node_token(ce->args[0]),
  2337. "Field `%.*s` is embedded via a pointer in `%s`", LIT(i->string), type_str);
  2338. return false;
  2339. }
  2340. operand->mode = Addressing_Constant;
  2341. // IMPORTANT TODO(bill): Fix for anonymous fields
  2342. operand->value = make_exact_value_integer(type_offset_of_from_selection(c->sizes, c->allocator, type, sel));
  2343. operand->type = t_int;
  2344. } break;
  2345. case BuiltinProc_type_of_val:
  2346. // type_of_val :: proc(val: Type) -> type(Type)
  2347. check_assignment(c, operand, NULL, make_string("argument of `type_of_val`"));
  2348. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin)
  2349. return false;
  2350. operand->mode = Addressing_Type;
  2351. break;
  2352. case BuiltinProc_type_info: {
  2353. // type_info :: proc(Type) -> ^Type_Info
  2354. AstNode *expr = ce->args[0];
  2355. Type *type = check_type(c, expr);
  2356. if (type == NULL || type == t_invalid) {
  2357. error(ast_node_token(expr), "Invalid argument to `type_info`");
  2358. return false;
  2359. }
  2360. add_type_info_type(c, type);
  2361. operand->mode = Addressing_Value;
  2362. operand->type = t_type_info_ptr;
  2363. } break;
  2364. case BuiltinProc_type_info_of_val: {
  2365. // type_info_of_val :: proc(val: Type) -> ^Type_Info
  2366. AstNode *expr = ce->args[0];
  2367. check_assignment(c, operand, NULL, make_string("argument of `type_info_of_val`"));
  2368. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin)
  2369. return false;
  2370. add_type_info_type(c, operand->type);
  2371. operand->mode = Addressing_Value;
  2372. operand->type = t_type_info_ptr;
  2373. } break;
  2374. case BuiltinProc_compile_assert:
  2375. // compile_assert :: proc(cond: bool)
  2376. if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
  2377. gbString str = expr_to_string(ce->args[0]);
  2378. defer (gb_string_free(str));
  2379. error(ast_node_token(call),
  2380. "`%s` is not a constant boolean", str);
  2381. return false;
  2382. }
  2383. if (!operand->value.value_bool) {
  2384. gbString str = expr_to_string(ce->args[0]);
  2385. defer (gb_string_free(str));
  2386. error(ast_node_token(call),
  2387. "Compile time assertion: `%s`", str);
  2388. }
  2389. break;
  2390. case BuiltinProc_assert:
  2391. // assert :: proc(cond: bool)
  2392. if (!is_type_boolean(operand->type)) {
  2393. gbString str = expr_to_string(ce->args[0]);
  2394. defer (gb_string_free(str));
  2395. error(ast_node_token(call),
  2396. "`%s` is not a boolean", str);
  2397. return false;
  2398. }
  2399. operand->mode = Addressing_NoValue;
  2400. break;
  2401. case BuiltinProc_panic:
  2402. // panic :: proc(msg: string)
  2403. if (!is_type_string(operand->type)) {
  2404. gbString str = expr_to_string(ce->args[0]);
  2405. defer (gb_string_free(str));
  2406. error(ast_node_token(call),
  2407. "`%s` is not a string", str);
  2408. return false;
  2409. }
  2410. operand->mode = Addressing_NoValue;
  2411. break;
  2412. case BuiltinProc_copy: {
  2413. // copy :: proc(x, y: []Type) -> int
  2414. Type *dest_type = NULL, *src_type = NULL;
  2415. Type *d = base_type(operand->type);
  2416. if (d->kind == Type_Slice)
  2417. dest_type = d->Slice.elem;
  2418. Operand op = {};
  2419. check_expr(c, &op, ce->args[1]);
  2420. if (op.mode == Addressing_Invalid)
  2421. return false;
  2422. Type *s = base_type(op.type);
  2423. if (s->kind == Type_Slice)
  2424. src_type = s->Slice.elem;
  2425. if (dest_type == NULL || src_type == NULL) {
  2426. error(ast_node_token(call), "`copy` only expects slices as arguments");
  2427. return false;
  2428. }
  2429. if (!are_types_identical(dest_type, src_type)) {
  2430. gbString d_arg = expr_to_string(ce->args[0]);
  2431. gbString s_arg = expr_to_string(ce->args[1]);
  2432. gbString d_str = type_to_string(dest_type);
  2433. gbString s_str = type_to_string(src_type);
  2434. defer (gb_string_free(d_arg));
  2435. defer (gb_string_free(s_arg));
  2436. defer (gb_string_free(d_str));
  2437. defer (gb_string_free(s_str));
  2438. error(ast_node_token(call),
  2439. "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
  2440. d_arg, s_arg, d_str, s_str);
  2441. return false;
  2442. }
  2443. operand->type = t_int; // Returns number of elems copied
  2444. operand->mode = Addressing_Value;
  2445. } break;
  2446. case BuiltinProc_append: {
  2447. // append :: proc(x : ^[]Type, y : Type) -> bool
  2448. Type *x_type = NULL, *y_type = NULL;
  2449. x_type = base_type(operand->type);
  2450. Operand op = {};
  2451. check_expr(c, &op, ce->args[1]);
  2452. if (op.mode == Addressing_Invalid)
  2453. return false;
  2454. y_type = base_type(op.type);
  2455. if (!(is_type_pointer(x_type) && is_type_slice(x_type->Pointer.elem))) {
  2456. error(ast_node_token(call), "First argument to `append` must be a pointer to a slice");
  2457. return false;
  2458. }
  2459. Type *elem_type = x_type->Pointer.elem->Slice.elem;
  2460. if (!check_is_assignable_to(c, &op, elem_type)) {
  2461. gbString d_arg = expr_to_string(ce->args[0]);
  2462. gbString s_arg = expr_to_string(ce->args[1]);
  2463. gbString d_str = type_to_string(elem_type);
  2464. gbString s_str = type_to_string(y_type);
  2465. defer (gb_string_free(d_arg));
  2466. defer (gb_string_free(s_arg));
  2467. defer (gb_string_free(d_str));
  2468. defer (gb_string_free(s_str));
  2469. error(ast_node_token(call),
  2470. "Arguments to `append`, %s, %s, have different element types: %s vs %s",
  2471. d_arg, s_arg, d_str, s_str);
  2472. return false;
  2473. }
  2474. operand->type = t_bool; // Returns if it was successful
  2475. operand->mode = Addressing_Value;
  2476. } break;
  2477. case BuiltinProc_swizzle: {
  2478. // swizzle :: proc(v: {N}T, T...) -> {M}T
  2479. Type *vector_type = base_type(operand->type);
  2480. if (!is_type_vector(vector_type)) {
  2481. gbString type_str = type_to_string(operand->type);
  2482. defer (gb_string_free(type_str));
  2483. error(ast_node_token(call),
  2484. "You can only `swizzle` a vector, got `%s`",
  2485. type_str);
  2486. return false;
  2487. }
  2488. isize max_count = vector_type->Vector.count;
  2489. isize arg_count = 0;
  2490. for_array(i, ce->args) {
  2491. if (i == 0) continue;
  2492. AstNode *arg = ce->args[i];
  2493. Operand op = {};
  2494. check_expr(c, &op, arg);
  2495. if (op.mode == Addressing_Invalid)
  2496. return false;
  2497. Type *arg_type = base_type(op.type);
  2498. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  2499. error(ast_node_token(op.expr), "Indices to `swizzle` must be constant integers");
  2500. return false;
  2501. }
  2502. if (op.value.value_integer < 0) {
  2503. error(ast_node_token(op.expr), "Negative `swizzle` index");
  2504. return false;
  2505. }
  2506. if (max_count <= op.value.value_integer) {
  2507. error(ast_node_token(op.expr), "`swizzle` index exceeds vector length");
  2508. return false;
  2509. }
  2510. arg_count++;
  2511. }
  2512. if (arg_count > max_count) {
  2513. error(ast_node_token(call), "Too many `swizzle` indices, %td > %td", arg_count, max_count);
  2514. return false;
  2515. }
  2516. Type *elem_type = vector_type->Vector.elem;
  2517. operand->type = make_type_vector(c->allocator, elem_type, arg_count);
  2518. operand->mode = Addressing_Value;
  2519. } break;
  2520. #if 0
  2521. case BuiltinProc_ptr_offset: {
  2522. // ptr_offset :: proc(ptr: ^T, offset: int) -> ^T
  2523. // ^T cannot be rawptr
  2524. Type *ptr_type = base_type(operand->type);
  2525. if (!is_type_pointer(ptr_type)) {
  2526. gbString type_str = type_to_string(operand->type);
  2527. defer (gb_string_free(type_str));
  2528. error(ast_node_token(call),
  2529. "Expected a pointer to `ptr_offset`, got `%s`",
  2530. type_str);
  2531. return false;
  2532. }
  2533. if (ptr_type == t_rawptr) {
  2534. error(ast_node_token(call),
  2535. "`rawptr` cannot have pointer arithmetic");
  2536. return false;
  2537. }
  2538. AstNode *offset = ce->args[1];
  2539. Operand op = {};
  2540. check_expr(c, &op, offset);
  2541. if (op.mode == Addressing_Invalid)
  2542. return false;
  2543. Type *offset_type = base_type(op.type);
  2544. if (!is_type_integer(offset_type)) {
  2545. error(ast_node_token(op.expr), "Pointer offsets for `ptr_offset` must be an integer");
  2546. return false;
  2547. }
  2548. if (operand->mode == Addressing_Constant &&
  2549. op.mode == Addressing_Constant) {
  2550. i64 ptr = operand->value.value_pointer;
  2551. i64 elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
  2552. ptr += elem_size * op.value.value_integer;
  2553. operand->value.value_pointer = ptr;
  2554. } else {
  2555. operand->mode = Addressing_Value;
  2556. }
  2557. } break;
  2558. case BuiltinProc_ptr_sub: {
  2559. // ptr_sub :: proc(a, b: ^T) -> int
  2560. // ^T cannot be rawptr
  2561. Type *ptr_type = base_type(operand->type);
  2562. if (!is_type_pointer(ptr_type)) {
  2563. gbString type_str = type_to_string(operand->type);
  2564. defer (gb_string_free(type_str));
  2565. error(ast_node_token(call),
  2566. "Expected a pointer to `ptr_add`, got `%s`",
  2567. type_str);
  2568. return false;
  2569. }
  2570. if (ptr_type == t_rawptr) {
  2571. error(ast_node_token(call),
  2572. "`rawptr` cannot have pointer arithmetic");
  2573. return false;
  2574. }
  2575. AstNode *offset = ce->args[1];
  2576. Operand op = {};
  2577. check_expr(c, &op, offset);
  2578. if (op.mode == Addressing_Invalid)
  2579. return false;
  2580. if (!is_type_pointer(op.type)) {
  2581. gbString type_str = type_to_string(operand->type);
  2582. defer (gb_string_free(type_str));
  2583. error(ast_node_token(call),
  2584. "Expected a pointer to `ptr_add`, got `%s`",
  2585. type_str);
  2586. return false;
  2587. }
  2588. if (base_type(op.type) == t_rawptr) {
  2589. error(ast_node_token(call),
  2590. "`rawptr` cannot have pointer arithmetic");
  2591. return false;
  2592. }
  2593. if (!are_types_identical(operand->type, op.type)) {
  2594. gbString a = type_to_string(operand->type);
  2595. gbString b = type_to_string(op.type);
  2596. defer (gb_string_free(a));
  2597. defer (gb_string_free(b));
  2598. error(ast_node_token(op.expr),
  2599. "`ptr_sub` requires to pointer of the same type. Got `%s` and `%s`.", a, b);
  2600. return false;
  2601. }
  2602. operand->type = t_int;
  2603. if (operand->mode == Addressing_Constant &&
  2604. op.mode == Addressing_Constant) {
  2605. u8 *ptr_a = cast(u8 *)operand->value.value_pointer;
  2606. u8 *ptr_b = cast(u8 *)op.value.value_pointer;
  2607. isize elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
  2608. operand->value = make_exact_value_integer((ptr_a - ptr_b) / elem_size);
  2609. } else {
  2610. operand->mode = Addressing_Value;
  2611. }
  2612. } break;
  2613. #endif
  2614. case BuiltinProc_slice_ptr: {
  2615. // slice_ptr :: proc(a: ^T, len: int[, cap: int]) -> []T
  2616. // ^T cannot be rawptr
  2617. Type *ptr_type = base_type(operand->type);
  2618. if (!is_type_pointer(ptr_type)) {
  2619. gbString type_str = type_to_string(operand->type);
  2620. defer (gb_string_free(type_str));
  2621. error(ast_node_token(call),
  2622. "Expected a pointer to `slice_ptr`, got `%s`",
  2623. type_str);
  2624. return false;
  2625. }
  2626. if (ptr_type == t_rawptr) {
  2627. error(ast_node_token(call),
  2628. "`rawptr` cannot have pointer arithmetic");
  2629. return false;
  2630. }
  2631. AstNode *len = ce->args[1];
  2632. AstNode *cap = NULL;
  2633. if (ce->args.count > 2) {
  2634. cap = ce->args[2];
  2635. }
  2636. Operand op = {};
  2637. check_expr(c, &op, len);
  2638. if (op.mode == Addressing_Invalid)
  2639. return false;
  2640. if (!is_type_integer(op.type)) {
  2641. gbString type_str = type_to_string(operand->type);
  2642. defer (gb_string_free(type_str));
  2643. error(ast_node_token(call),
  2644. "Length for `slice_ptr` must be an integer, got `%s`",
  2645. type_str);
  2646. return false;
  2647. }
  2648. if (cap != NULL) {
  2649. check_expr(c, &op, cap);
  2650. if (op.mode == Addressing_Invalid)
  2651. return false;
  2652. if (!is_type_integer(op.type)) {
  2653. gbString type_str = type_to_string(operand->type);
  2654. defer (gb_string_free(type_str));
  2655. error(ast_node_token(call),
  2656. "Capacity for `slice_ptr` must be an integer, got `%s`",
  2657. type_str);
  2658. return false;
  2659. }
  2660. if (ce->args.count > 3) {
  2661. error(ast_node_token(call),
  2662. "Too many arguments to `slice_ptr`, expected either 2 or 3");
  2663. return false;
  2664. }
  2665. }
  2666. operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
  2667. operand->mode = Addressing_Value;
  2668. } break;
  2669. case BuiltinProc_min: {
  2670. // min :: proc(a, b: comparable) -> comparable
  2671. Type *type = base_type(operand->type);
  2672. if (!is_type_comparable(type) || !is_type_numeric(type)) {
  2673. gbString type_str = type_to_string(operand->type);
  2674. defer (gb_string_free(type_str));
  2675. error(ast_node_token(call),
  2676. "Expected a comparable numeric type to `min`, got `%s`",
  2677. type_str);
  2678. return false;
  2679. }
  2680. AstNode *other_arg = ce->args[1];
  2681. Operand a = *operand;
  2682. Operand b = {};
  2683. check_expr(c, &b, other_arg);
  2684. if (b.mode == Addressing_Invalid)
  2685. return false;
  2686. if (!is_type_comparable(b.type) || !is_type_numeric(type)) {
  2687. gbString type_str = type_to_string(b.type);
  2688. defer (gb_string_free(type_str));
  2689. error(ast_node_token(call),
  2690. "Expected a comparable numeric type to `min`, got `%s`",
  2691. type_str);
  2692. return false;
  2693. }
  2694. if (a.mode == Addressing_Constant &&
  2695. b.mode == Addressing_Constant) {
  2696. ExactValue x = a.value;
  2697. ExactValue y = b.value;
  2698. Token lt = {Token_Lt};
  2699. operand->mode = Addressing_Constant;
  2700. if (compare_exact_values(lt, x, y)) {
  2701. operand->value = x;
  2702. operand->type = a.type;
  2703. } else {
  2704. operand->value = y;
  2705. operand->type = b.type;
  2706. }
  2707. } else {
  2708. operand->mode = Addressing_Value;
  2709. operand->type = type;
  2710. if (!are_types_identical(operand->type, b.type)) {
  2711. gbString type_a = type_to_string(a.type);
  2712. gbString type_b = type_to_string(b.type);
  2713. defer (gb_string_free(type_a));
  2714. defer (gb_string_free(type_b));
  2715. error(ast_node_token(call),
  2716. "Mismatched types to `min`, `%s` vs `%s`",
  2717. type_a, type_b);
  2718. return false;
  2719. }
  2720. }
  2721. } break;
  2722. case BuiltinProc_max: {
  2723. // min :: proc(a, b: comparable) -> comparable
  2724. Type *type = base_type(operand->type);
  2725. if (!is_type_comparable(type) || !is_type_numeric(type)) {
  2726. gbString type_str = type_to_string(operand->type);
  2727. defer (gb_string_free(type_str));
  2728. error(ast_node_token(call),
  2729. "Expected a comparable numeric type to `max`, got `%s`",
  2730. type_str);
  2731. return false;
  2732. }
  2733. AstNode *other_arg = ce->args[1];
  2734. Operand a = *operand;
  2735. Operand b = {};
  2736. check_expr(c, &b, other_arg);
  2737. if (b.mode == Addressing_Invalid)
  2738. return false;
  2739. if (!is_type_comparable(b.type) || !is_type_numeric(type)) {
  2740. gbString type_str = type_to_string(b.type);
  2741. defer (gb_string_free(type_str));
  2742. error(ast_node_token(call),
  2743. "Expected a comparable numeric type to `max`, got `%s`",
  2744. type_str);
  2745. return false;
  2746. }
  2747. if (a.mode == Addressing_Constant &&
  2748. b.mode == Addressing_Constant) {
  2749. ExactValue x = a.value;
  2750. ExactValue y = b.value;
  2751. Token gt = {Token_Gt};
  2752. operand->mode = Addressing_Constant;
  2753. if (compare_exact_values(gt, x, y)) {
  2754. operand->value = x;
  2755. operand->type = a.type;
  2756. } else {
  2757. operand->value = y;
  2758. operand->type = b.type;
  2759. }
  2760. } else {
  2761. operand->mode = Addressing_Value;
  2762. operand->type = type;
  2763. if (!are_types_identical(operand->type, b.type)) {
  2764. gbString type_a = type_to_string(a.type);
  2765. gbString type_b = type_to_string(b.type);
  2766. defer (gb_string_free(type_a));
  2767. defer (gb_string_free(type_b));
  2768. error(ast_node_token(call),
  2769. "Mismatched types to `max`, `%s` vs `%s`",
  2770. type_a, type_b);
  2771. return false;
  2772. }
  2773. }
  2774. } break;
  2775. case BuiltinProc_abs: {
  2776. // abs :: proc(n: numeric) -> numeric
  2777. Type *type = base_type(operand->type);
  2778. if (!is_type_numeric(type)) {
  2779. gbString type_str = type_to_string(operand->type);
  2780. defer (gb_string_free(type_str));
  2781. error(ast_node_token(call),
  2782. "Expected a numeric type to `abs`, got `%s`",
  2783. type_str);
  2784. return false;
  2785. }
  2786. if (operand->mode == Addressing_Constant) {
  2787. switch (operand->value.kind) {
  2788. case ExactValue_Integer:
  2789. operand->value.value_integer = gb_abs(operand->value.value_integer);
  2790. break;
  2791. case ExactValue_Float:
  2792. operand->value.value_float = gb_abs(operand->value.value_float);
  2793. break;
  2794. default:
  2795. GB_PANIC("Invalid numeric constant");
  2796. break;
  2797. }
  2798. } else {
  2799. operand->mode = Addressing_Value;
  2800. }
  2801. operand->type = type;
  2802. } break;
  2803. case BuiltinProc_enum_to_string: {
  2804. Type *type = base_type(operand->type);
  2805. if (!is_type_enum(type)) {
  2806. gbString type_str = type_to_string(operand->type);
  2807. defer (gb_string_free(type_str));
  2808. error(ast_node_token(call),
  2809. "Expected an enum to `enum_to_string`, got `%s`",
  2810. type_str);
  2811. return false;
  2812. }
  2813. if (operand->mode == Addressing_Constant) {
  2814. ExactValue value = make_exact_value_string(make_string(""));
  2815. if (operand->value.kind == ExactValue_Integer) {
  2816. i64 index = operand->value.value_integer;
  2817. for (isize i = 0; i < type->Record.other_field_count; i++) {
  2818. Entity *f = type->Record.other_fields[i];
  2819. if (f->kind == Entity_Constant && f->Constant.value.kind == ExactValue_Integer) {
  2820. i64 fv = f->Constant.value.value_integer;
  2821. if (index == fv) {
  2822. value = make_exact_value_string(f->token.string);
  2823. break;
  2824. }
  2825. }
  2826. }
  2827. }
  2828. operand->value = value;
  2829. operand->type = t_string;
  2830. return true;
  2831. }
  2832. add_type_info_type(c, operand->type);
  2833. operand->mode = Addressing_Value;
  2834. operand->type = t_string;
  2835. } break;
  2836. }
  2837. return true;
  2838. }
  2839. void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  2840. PROF_PROC();
  2841. GB_ASSERT(call->kind == AstNode_CallExpr);
  2842. GB_ASSERT(proc_type->kind == Type_Proc);
  2843. ast_node(ce, CallExpr, call);
  2844. isize param_count = 0;
  2845. b32 variadic = proc_type->Proc.variadic;
  2846. b32 vari_expand = (ce->ellipsis.pos.line != 0);
  2847. if (proc_type->Proc.params != NULL) {
  2848. param_count = proc_type->Proc.params->Tuple.variable_count;
  2849. if (variadic) {
  2850. param_count--;
  2851. }
  2852. }
  2853. if (vari_expand && !variadic) {
  2854. error(ce->ellipsis,
  2855. "Cannot use `..` in call to a non-variadic procedure: `%.*s`",
  2856. LIT(ce->proc->Ident.string));
  2857. return;
  2858. }
  2859. if (ce->args.count == 0 && param_count == 0) {
  2860. return;
  2861. }
  2862. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  2863. defer (gb_temp_arena_memory_end(tmp));
  2864. Array<Operand> operands;
  2865. array_init(&operands, c->tmp_allocator, 2*param_count);
  2866. for_array(i, ce->args) {
  2867. Operand o = {};
  2868. check_multi_expr(c, &o, ce->args[i]);
  2869. if (o.type->kind != Type_Tuple) {
  2870. array_add(&operands, o);
  2871. } else {
  2872. auto *tuple = &o.type->Tuple;
  2873. if (variadic && i >= param_count) {
  2874. error(ast_node_token(ce->args[i]),
  2875. "`..` in a variadic procedure cannot be applied to a %td-valued expression", tuple->variable_count);
  2876. operand->mode = Addressing_Invalid;
  2877. return;
  2878. }
  2879. for (isize j = 0; j < tuple->variable_count; j++) {
  2880. o.type = tuple->variables[j]->type;
  2881. array_add(&operands, o);
  2882. }
  2883. }
  2884. }
  2885. i32 error_code = 0;
  2886. if (operands.count < param_count) {
  2887. error_code = -1;
  2888. } else if (!variadic && operands.count > param_count) {
  2889. error_code = +1;
  2890. }
  2891. if (error_code != 0) {
  2892. char *err_fmt = "Too many arguments for `%s`, expected %td arguments";
  2893. if (error_code < 0) {
  2894. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  2895. }
  2896. gbString proc_str = expr_to_string(ce->proc);
  2897. defer (gb_string_free(proc_str));
  2898. error(ast_node_token(call), err_fmt, proc_str, param_count);
  2899. operand->mode = Addressing_Invalid;
  2900. }
  2901. GB_ASSERT(proc_type->Proc.params != NULL);
  2902. Entity **sig_params = proc_type->Proc.params->Tuple.variables;
  2903. isize operand_index = 0;
  2904. for (; operand_index < param_count; operand_index++) {
  2905. Type *arg_type = sig_params[operand_index]->type;
  2906. Operand o = operands[operand_index];
  2907. if (variadic) {
  2908. o = operands[operand_index];
  2909. }
  2910. check_assignment(c, &o, arg_type, make_string("argument"), true);
  2911. }
  2912. if (variadic) {
  2913. b32 variadic_expand = false;
  2914. Type *slice = sig_params[param_count]->type;
  2915. Type *elem = base_type(slice)->Slice.elem;
  2916. Type *t = elem;
  2917. for (; operand_index < operands.count; operand_index++) {
  2918. Operand o = operands[operand_index];
  2919. if (vari_expand) {
  2920. variadic_expand = true;
  2921. t = slice;
  2922. if (operand_index != param_count) {
  2923. error(ast_node_token(o.expr),
  2924. "`..` in a variadic procedure can only have one variadic argument at the end");
  2925. break;
  2926. }
  2927. }
  2928. check_assignment(c, &o, t, make_string("argument"), true);
  2929. }
  2930. }
  2931. }
  2932. Entity *find_using_index_expr(Type *t) {
  2933. t = base_type(t);
  2934. if (t->kind != Type_Record) {
  2935. return NULL;
  2936. }
  2937. for (isize i = 0; i < t->Record.field_count; i++) {
  2938. Entity *f = t->Record.fields[i];
  2939. if (f->kind == Entity_Variable &&
  2940. f->Variable.field && f->Variable.anonymous) {
  2941. if (is_type_indexable(f->type)) {
  2942. return f;
  2943. }
  2944. Entity *res = find_using_index_expr(f->type);
  2945. if (res != NULL) {
  2946. return res;
  2947. }
  2948. }
  2949. }
  2950. return NULL;
  2951. }
  2952. ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  2953. PROF_PROC();
  2954. GB_ASSERT(call->kind == AstNode_CallExpr);
  2955. ast_node(ce, CallExpr, call);
  2956. check_expr_or_type(c, operand, ce->proc);
  2957. if (operand->mode == Addressing_Invalid) {
  2958. for_array(i, ce->args) {
  2959. check_expr_base(c, operand, ce->args[i]);
  2960. }
  2961. operand->mode = Addressing_Invalid;
  2962. operand->expr = call;
  2963. return Expr_Stmt;
  2964. }
  2965. if (operand->mode == Addressing_Builtin) {
  2966. i32 id = operand->builtin_id;
  2967. if (!check_builtin_procedure(c, operand, call, id)) {
  2968. operand->mode = Addressing_Invalid;
  2969. }
  2970. operand->expr = call;
  2971. return builtin_procs[id].kind;
  2972. }
  2973. Type *proc_type = base_type(operand->type);
  2974. if (proc_type == NULL || proc_type->kind != Type_Proc) {
  2975. AstNode *e = operand->expr;
  2976. gbString str = expr_to_string(e);
  2977. defer (gb_string_free(str));
  2978. error(ast_node_token(e), "Cannot call a non-procedure: `%s`", str);
  2979. operand->mode = Addressing_Invalid;
  2980. operand->expr = call;
  2981. return Expr_Stmt;
  2982. }
  2983. check_call_arguments(c, operand, proc_type, call);
  2984. switch (proc_type->Proc.result_count) {
  2985. case 0:
  2986. operand->mode = Addressing_NoValue;
  2987. break;
  2988. case 1:
  2989. operand->mode = Addressing_Value;
  2990. operand->type = proc_type->Proc.results->Tuple.variables[0]->type;
  2991. break;
  2992. default:
  2993. operand->mode = Addressing_Value;
  2994. operand->type = proc_type->Proc.results;
  2995. break;
  2996. }
  2997. operand->expr = call;
  2998. return Expr_Stmt;
  2999. }
  3000. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  3001. check_expr_base(c, o, e, t);
  3002. check_not_tuple(c, o);
  3003. char *err_str = NULL;
  3004. switch (o->mode) {
  3005. case Addressing_NoValue:
  3006. err_str = "used as a value";
  3007. break;
  3008. case Addressing_Type:
  3009. err_str = "is not an expression";
  3010. break;
  3011. case Addressing_Builtin:
  3012. err_str = "must be called";
  3013. break;
  3014. }
  3015. if (err_str != NULL) {
  3016. gbString str = expr_to_string(e);
  3017. defer (gb_string_free(str));
  3018. error(ast_node_token(e), "`%s` %s", str, err_str);
  3019. o->mode = Addressing_Invalid;
  3020. }
  3021. }
  3022. ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  3023. ExprKind kind = Expr_Stmt;
  3024. o->mode = Addressing_Invalid;
  3025. o->type = t_invalid;
  3026. switch (node->kind) {
  3027. default:
  3028. goto error;
  3029. break;
  3030. case_ast_node(be, BadExpr, node)
  3031. goto error;
  3032. case_end;
  3033. case_ast_node(i, Ident, node);
  3034. check_identifier(c, o, node, type_hint, NULL);
  3035. case_end;
  3036. case_ast_node(bl, BasicLit, node);
  3037. Type *t = t_invalid;
  3038. switch (bl->kind) {
  3039. case Token_Integer: t = t_untyped_integer; break;
  3040. case Token_Float: t = t_untyped_float; break;
  3041. case Token_String: t = t_untyped_string; break;
  3042. case Token_Rune: t = t_untyped_rune; break;
  3043. default: GB_PANIC("Unknown literal"); break;
  3044. }
  3045. o->mode = Addressing_Constant;
  3046. o->type = t;
  3047. o->value = make_exact_value_from_basic_literal(*bl);
  3048. case_end;
  3049. case_ast_node(pl, ProcLit, node);
  3050. check_open_scope(c, pl->type);
  3051. defer (check_close_scope(c));
  3052. c->context.decl = make_declaration_info(c->allocator, c->context.scope);
  3053. Type *proc_type = check_type(c, pl->type);
  3054. if (proc_type != NULL) {
  3055. check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body);
  3056. o->mode = Addressing_Value;
  3057. o->type = proc_type;
  3058. } else {
  3059. gbString str = expr_to_string(node);
  3060. error(ast_node_token(node), "Invalid procedure literal `%s`", str);
  3061. gb_string_free(str);
  3062. goto error;
  3063. }
  3064. case_end;
  3065. case_ast_node(cl, CompoundLit, node);
  3066. PROF_SCOPED("check__expr_base - CompoundLit");
  3067. Type *type = type_hint;
  3068. b32 ellipsis_array = false;
  3069. b32 is_constant = true;
  3070. if (cl->type != NULL) {
  3071. type = NULL;
  3072. // [..]Type
  3073. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
  3074. if (cl->type->ArrayType.count->kind == AstNode_Ellipsis) {
  3075. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  3076. ellipsis_array = true;
  3077. }
  3078. }
  3079. if (type == NULL) {
  3080. type = check_type(c, cl->type);
  3081. }
  3082. }
  3083. if (type == NULL) {
  3084. error(ast_node_token(node), "Missing type in compound literal");
  3085. goto error;
  3086. }
  3087. Type *t = base_type(type);
  3088. switch (t->kind) {
  3089. case Type_Record: {
  3090. if (!is_type_struct(t)) {
  3091. break;
  3092. }
  3093. if (cl->elems.count == 0) {
  3094. break; // NOTE(bill): No need to init
  3095. }
  3096. { // Checker values
  3097. isize field_count = t->Record.field_count;
  3098. if (cl->elems[0]->kind == AstNode_FieldValue) {
  3099. b32 *fields_visited = gb_alloc_array(c->allocator, b32, field_count);
  3100. for_array(i, cl->elems) {
  3101. AstNode *elem = cl->elems[i];
  3102. if (elem->kind != AstNode_FieldValue) {
  3103. error(ast_node_token(elem),
  3104. "Mixture of `field = value` and value elements in a structure literal is not allowed");
  3105. continue;
  3106. }
  3107. ast_node(fv, FieldValue, elem);
  3108. if (fv->field->kind != AstNode_Ident) {
  3109. gbString expr_str = expr_to_string(fv->field);
  3110. defer (gb_string_free(expr_str));
  3111. error(ast_node_token(elem),
  3112. "Invalid field name `%s` in structure literal", expr_str);
  3113. continue;
  3114. }
  3115. String name = fv->field->Ident.string;
  3116. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  3117. if (sel.entity == NULL) {
  3118. error(ast_node_token(elem),
  3119. "Unknown field `%.*s` in structure literal", LIT(name));
  3120. continue;
  3121. }
  3122. if (sel.index.count > 1) {
  3123. error(ast_node_token(elem),
  3124. "Cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
  3125. continue;
  3126. }
  3127. Entity *field = t->Record.fields[sel.index[0]];
  3128. add_entity_use(c, fv->field, field);
  3129. if (fields_visited[sel.index[0]]) {
  3130. error(ast_node_token(elem),
  3131. "Duplicate field `%.*s` in structure literal", LIT(name));
  3132. continue;
  3133. }
  3134. fields_visited[sel.index[0]] = true;
  3135. check_expr(c, o, fv->value);
  3136. if (base_type(field->type) == t_any) {
  3137. is_constant = false;
  3138. }
  3139. if (is_constant) {
  3140. is_constant = o->mode == Addressing_Constant;
  3141. }
  3142. check_assignment(c, o, field->type, make_string("structure literal"));
  3143. }
  3144. } else {
  3145. for_array(index, cl->elems) {
  3146. AstNode *elem = cl->elems[index];
  3147. if (elem->kind == AstNode_FieldValue) {
  3148. error(ast_node_token(elem),
  3149. "Mixture of `field = value` and value elements in a structure literal is not allowed");
  3150. continue;
  3151. }
  3152. Entity *field = t->Record.fields_in_src_order[index];
  3153. check_expr(c, o, elem);
  3154. if (index >= field_count) {
  3155. error(ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count);
  3156. break;
  3157. }
  3158. if (base_type(field->type) == t_any) {
  3159. is_constant = false;
  3160. }
  3161. if (is_constant) {
  3162. is_constant = o->mode == Addressing_Constant;
  3163. }
  3164. check_assignment(c, o, field->type, make_string("structure literal"));
  3165. }
  3166. if (cl->elems.count < field_count) {
  3167. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  3168. }
  3169. }
  3170. }
  3171. } break;
  3172. case Type_Slice:
  3173. case Type_Array:
  3174. case Type_Vector:
  3175. {
  3176. Type *elem_type = NULL;
  3177. String context_name = {};
  3178. if (t->kind == Type_Slice) {
  3179. elem_type = t->Slice.elem;
  3180. context_name = make_string("slice literal");
  3181. } else if (t->kind == Type_Vector) {
  3182. elem_type = t->Vector.elem;
  3183. context_name = make_string("vector literal");
  3184. } else {
  3185. elem_type = t->Array.elem;
  3186. context_name = make_string("array literal");
  3187. }
  3188. i64 max = 0;
  3189. isize index = 0;
  3190. isize elem_count = cl->elems.count;
  3191. if (base_type(elem_type) == t_any) {
  3192. is_constant = false;
  3193. }
  3194. for (; index < elem_count; index++) {
  3195. AstNode *e = cl->elems[index];
  3196. if (e->kind == AstNode_FieldValue) {
  3197. error(ast_node_token(e),
  3198. "`field = value` is only allowed in struct literals");
  3199. continue;
  3200. }
  3201. if (t->kind == Type_Array &&
  3202. t->Array.count >= 0 &&
  3203. index >= t->Array.count) {
  3204. error(ast_node_token(e), "Index %lld is out of bounds (>= %lld) for array literal", index, t->Array.count);
  3205. }
  3206. if (t->kind == Type_Vector &&
  3207. t->Vector.count >= 0 &&
  3208. index >= t->Vector.count) {
  3209. error(ast_node_token(e), "Index %lld is out of bounds (>= %lld) for vector literal", index, t->Vector.count);
  3210. }
  3211. Operand operand = {};
  3212. check_expr_with_type_hint(c, &operand, e, elem_type);
  3213. check_assignment(c, &operand, elem_type, context_name);
  3214. if (is_constant) {
  3215. is_constant = operand.mode == Addressing_Constant;
  3216. }
  3217. }
  3218. if (max < index) {
  3219. max = index;
  3220. }
  3221. if (t->kind == Type_Vector) {
  3222. if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
  3223. error(ast_node_token(cl->elems[0]),
  3224. "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
  3225. }
  3226. }
  3227. if (t->kind == Type_Array && ellipsis_array) {
  3228. t->Array.count = max;
  3229. }
  3230. } break;
  3231. default: {
  3232. gbString str = type_to_string(type);
  3233. error(ast_node_token(node), "Invalid compound literal type `%s`", str);
  3234. gb_string_free(str);
  3235. goto error;
  3236. } break;
  3237. }
  3238. if (is_constant) {
  3239. o->mode = Addressing_Constant;
  3240. o->value = make_exact_value_compound(node);
  3241. } else {
  3242. o->mode = Addressing_Value;
  3243. }
  3244. o->type = type;
  3245. case_end;
  3246. case_ast_node(pe, ParenExpr, node);
  3247. kind = check_expr_base(c, o, pe->expr, type_hint);
  3248. o->expr = node;
  3249. case_end;
  3250. case_ast_node(te, TagExpr, node);
  3251. // TODO(bill): Tag expressions
  3252. error(ast_node_token(node), "Tag expressions are not supported yet");
  3253. kind = check_expr_base(c, o, te->expr, type_hint);
  3254. o->expr = node;
  3255. case_end;
  3256. case_ast_node(re, RunExpr, node);
  3257. // TODO(bill): Tag expressions
  3258. kind = check_expr_base(c, o, re->expr, type_hint);
  3259. o->expr = node;
  3260. case_end;
  3261. case_ast_node(ue, UnaryExpr, node);
  3262. check_expr(c, o, ue->expr);
  3263. if (o->mode == Addressing_Invalid) {
  3264. goto error;
  3265. }
  3266. check_unary_expr(c, o, ue->op, node);
  3267. if (o->mode == Addressing_Invalid) {
  3268. goto error;
  3269. }
  3270. case_end;
  3271. case_ast_node(be, BinaryExpr, node);
  3272. check_binary_expr(c, o, node);
  3273. if (o->mode == Addressing_Invalid) {
  3274. goto error;
  3275. }
  3276. case_end;
  3277. case_ast_node(se, SelectorExpr, node);
  3278. check_selector(c, o, node);
  3279. case_end;
  3280. case_ast_node(ie, IndexExpr, node);
  3281. PROF_SCOPED("check__expr_base - IndexExpr");
  3282. check_expr(c, o, ie->expr);
  3283. if (o->mode == Addressing_Invalid) {
  3284. goto error;
  3285. }
  3286. Type *t = base_type(type_deref(o->type));
  3287. auto set_index_data = [](Operand *o, Type *t, i64 *max_count) -> b32 {
  3288. t = base_type(type_deref(t));
  3289. switch (t->kind) {
  3290. case Type_Basic:
  3291. if (is_type_string(t)) {
  3292. if (o->mode == Addressing_Constant) {
  3293. *max_count = o->value.value_string.len;
  3294. }
  3295. if (o->mode != Addressing_Variable) {
  3296. o->mode = Addressing_Value;
  3297. }
  3298. o->type = t_u8;
  3299. return true;
  3300. }
  3301. break;
  3302. case Type_Array:
  3303. *max_count = t->Array.count;
  3304. if (o->mode != Addressing_Variable) {
  3305. o->mode = Addressing_Value;
  3306. }
  3307. o->type = t->Array.elem;
  3308. return true;
  3309. case Type_Vector:
  3310. *max_count = t->Vector.count;
  3311. if (o->mode != Addressing_Variable) {
  3312. o->mode = Addressing_Value;
  3313. }
  3314. o->type = t->Vector.elem;
  3315. return true;
  3316. case Type_Slice:
  3317. o->type = t->Slice.elem;
  3318. o->mode = Addressing_Variable;
  3319. return true;
  3320. }
  3321. return false;
  3322. };
  3323. i64 max_count = -1;
  3324. b32 valid = set_index_data(o, t, &max_count);
  3325. if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
  3326. Entity *found = find_using_index_expr(t);
  3327. if (found != NULL) {
  3328. valid = set_index_data(o, found->type, &max_count);
  3329. }
  3330. }
  3331. if (!valid) {
  3332. gbString str = expr_to_string(o->expr);
  3333. error(ast_node_token(o->expr), "Cannot index `%s`", str);
  3334. gb_string_free(str);
  3335. goto error;
  3336. }
  3337. if (ie->index == NULL) {
  3338. gbString str = expr_to_string(o->expr);
  3339. error(ast_node_token(o->expr), "Missing index for `%s`", str);
  3340. gb_string_free(str);
  3341. goto error;
  3342. }
  3343. i64 index = 0;
  3344. b32 ok = check_index_value(c, ie->index, max_count, &index);
  3345. case_end;
  3346. case_ast_node(se, SliceExpr, node);
  3347. PROF_SCOPED("check__expr_base - SliceExpr");
  3348. check_expr(c, o, se->expr);
  3349. if (o->mode == Addressing_Invalid) {
  3350. goto error;
  3351. }
  3352. b32 valid = false;
  3353. i64 max_count = -1;
  3354. Type *t = base_type(type_deref(o->type));
  3355. switch (t->kind) {
  3356. case Type_Basic:
  3357. if (is_type_string(t)) {
  3358. valid = true;
  3359. if (o->mode == Addressing_Constant) {
  3360. max_count = o->value.value_string.len;
  3361. }
  3362. if (se->max != NULL) {
  3363. error(ast_node_token(se->max), "Max (3rd) index not needed in substring expression");
  3364. }
  3365. o->type = t_string;
  3366. }
  3367. break;
  3368. case Type_Array:
  3369. valid = true;
  3370. max_count = t->Array.count;
  3371. if (o->mode != Addressing_Variable) {
  3372. gbString str = expr_to_string(node);
  3373. error(ast_node_token(node), "Cannot slice array `%s`, value is not addressable", str);
  3374. gb_string_free(str);
  3375. goto error;
  3376. }
  3377. o->type = make_type_slice(c->allocator, t->Array.elem);
  3378. break;
  3379. case Type_Slice:
  3380. valid = true;
  3381. break;
  3382. }
  3383. if (!valid) {
  3384. gbString str = expr_to_string(o->expr);
  3385. error(ast_node_token(o->expr), "Cannot slice `%s`", str);
  3386. gb_string_free(str);
  3387. goto error;
  3388. }
  3389. o->mode = Addressing_Value;
  3390. i64 indices[3] = {};
  3391. AstNode *nodes[3] = {se->low, se->high, se->max};
  3392. for (isize i = 0; i < gb_count_of(nodes); i++) {
  3393. i64 index = max_count;
  3394. if (nodes[i] != NULL) {
  3395. i64 capacity = -1;
  3396. if (max_count >= 0)
  3397. capacity = max_count;
  3398. i64 j = 0;
  3399. if (check_index_value(c, nodes[i], capacity, &j)) {
  3400. index = j;
  3401. }
  3402. } else if (i == 0) {
  3403. index = 0;
  3404. }
  3405. indices[i] = index;
  3406. }
  3407. for (isize i = 0; i < gb_count_of(indices); i++) {
  3408. i64 a = indices[i];
  3409. for (isize j = i+1; j < gb_count_of(indices); j++) {
  3410. i64 b = indices[j];
  3411. if (a > b && b >= 0) {
  3412. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  3413. }
  3414. }
  3415. }
  3416. case_end;
  3417. case_ast_node(ce, CallExpr, node);
  3418. return check_call_expr(c, o, node);
  3419. case_end;
  3420. case_ast_node(de, DerefExpr, node);
  3421. check_expr_or_type(c, o, de->expr);
  3422. if (o->mode == Addressing_Invalid) {
  3423. goto error;
  3424. } else {
  3425. Type *t = base_type(o->type);
  3426. if (t->kind == Type_Pointer) {
  3427. o->mode = Addressing_Variable;
  3428. o->type = t->Pointer.elem;
  3429. } else {
  3430. gbString str = expr_to_string(o->expr);
  3431. error(ast_node_token(o->expr), "Cannot dereference `%s`", str);
  3432. gb_string_free(str);
  3433. goto error;
  3434. }
  3435. }
  3436. case_end;
  3437. case_ast_node(de, DemaybeExpr, node);
  3438. check_expr_or_type(c, o, de->expr);
  3439. if (o->mode == Addressing_Invalid) {
  3440. goto error;
  3441. } else {
  3442. Type *t = base_type(o->type);
  3443. if (t->kind == Type_Maybe) {
  3444. Entity **variables = gb_alloc_array(c->allocator, Entity *, 2);
  3445. Type *elem = t->Maybe.elem;
  3446. Token tok = make_token_ident(make_string(""));
  3447. variables[0] = make_entity_param(c->allocator, NULL, tok, elem, false);
  3448. variables[1] = make_entity_param(c->allocator, NULL, tok, t_bool, false);
  3449. Type *tuple = make_type_tuple(c->allocator);
  3450. tuple->Tuple.variables = variables;
  3451. tuple->Tuple.variable_count = 2;
  3452. o->type = tuple;
  3453. o->mode = Addressing_Variable;
  3454. } else {
  3455. gbString str = expr_to_string(o->expr);
  3456. error(ast_node_token(o->expr), "Cannot demaybe `%s`", str);
  3457. gb_string_free(str);
  3458. goto error;
  3459. }
  3460. }
  3461. case_end;
  3462. case AstNode_ProcType:
  3463. case AstNode_PointerType:
  3464. case AstNode_MaybeType:
  3465. case AstNode_ArrayType:
  3466. case AstNode_VectorType:
  3467. case AstNode_StructType:
  3468. case AstNode_RawUnionType:
  3469. o->mode = Addressing_Type;
  3470. o->type = check_type(c, node);
  3471. break;
  3472. }
  3473. kind = Expr_Expr;
  3474. o->expr = node;
  3475. return kind;
  3476. error:
  3477. o->mode = Addressing_Invalid;
  3478. o->expr = node;
  3479. return kind;
  3480. }
  3481. ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  3482. ExprKind kind = check__expr_base(c, o, node, type_hint);
  3483. Type *type = NULL;
  3484. ExactValue value = {ExactValue_Invalid};
  3485. switch (o->mode) {
  3486. case Addressing_Invalid:
  3487. type = t_invalid;
  3488. break;
  3489. case Addressing_NoValue:
  3490. type = NULL;
  3491. break;
  3492. case Addressing_Constant:
  3493. type = o->type;
  3494. value = o->value;
  3495. break;
  3496. default:
  3497. type = o->type;
  3498. break;
  3499. }
  3500. if (type != NULL && is_type_untyped(type)) {
  3501. add_untyped(&c->info, node, false, o->mode, type, value);
  3502. } else {
  3503. add_type_and_value(&c->info, node, o->mode, type, value);
  3504. }
  3505. return kind;
  3506. }
  3507. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  3508. gbString err_str = NULL;
  3509. defer (gb_string_free(err_str));
  3510. check_expr_base(c, o, e);
  3511. switch (o->mode) {
  3512. default:
  3513. return; // NOTE(bill): Valid
  3514. case Addressing_NoValue:
  3515. err_str = expr_to_string(e);
  3516. error(ast_node_token(e), "`%s` used as value", err_str);
  3517. break;
  3518. case Addressing_Type:
  3519. err_str = expr_to_string(e);
  3520. error(ast_node_token(e), "`%s` is not an expression", err_str);
  3521. break;
  3522. }
  3523. o->mode = Addressing_Invalid;
  3524. }
  3525. void check_not_tuple(Checker *c, Operand *o) {
  3526. if (o->mode == Addressing_Value) {
  3527. // NOTE(bill): Tuples are not first class thus never named
  3528. if (o->type->kind == Type_Tuple) {
  3529. isize count = o->type->Tuple.variable_count;
  3530. GB_ASSERT(count != 1);
  3531. error(ast_node_token(o->expr),
  3532. "%td-valued tuple found where single value expected", count);
  3533. o->mode = Addressing_Invalid;
  3534. }
  3535. }
  3536. }
  3537. void check_expr(Checker *c, Operand *o, AstNode *e) {
  3538. check_multi_expr(c, o, e);
  3539. check_not_tuple(c, o);
  3540. }
  3541. void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
  3542. check_expr_base(c, o, e);
  3543. check_not_tuple(c, o);
  3544. if (o->mode == Addressing_NoValue) {
  3545. gbString str = expr_to_string(o->expr);
  3546. defer (gb_string_free(str));
  3547. error(ast_node_token(o->expr),
  3548. "`%s` used as value or type", str);
  3549. o->mode = Addressing_Invalid;
  3550. }
  3551. }
  3552. gbString write_expr_to_string(gbString str, AstNode *node);
  3553. gbString write_params_to_string(gbString str, AstNodeArray params, char *sep) {
  3554. for_array(i, params) {
  3555. ast_node(p, Parameter, params[i]);
  3556. if (i > 0) {
  3557. str = gb_string_appendc(str, sep);
  3558. }
  3559. str = write_expr_to_string(str, params[i]);
  3560. }
  3561. return str;
  3562. }
  3563. gbString string_append_token(gbString str, Token token) {
  3564. if (token.string.len > 0) {
  3565. return gb_string_append_length(str, token.string.text, token.string.len);
  3566. }
  3567. return str;
  3568. }
  3569. gbString write_expr_to_string(gbString str, AstNode *node) {
  3570. if (node == NULL)
  3571. return str;
  3572. if (is_ast_node_stmt(node)) {
  3573. GB_ASSERT("stmt passed to write_expr_to_string");
  3574. }
  3575. switch (node->kind) {
  3576. default:
  3577. str = gb_string_appendc(str, "(BadExpr)");
  3578. break;
  3579. case_ast_node(i, Ident, node);
  3580. str = string_append_token(str, *i);
  3581. case_end;
  3582. case_ast_node(bl, BasicLit, node);
  3583. str = string_append_token(str, *bl);
  3584. case_end;
  3585. case_ast_node(pl, ProcLit, node);
  3586. str = write_expr_to_string(str, pl->type);
  3587. case_end;
  3588. case_ast_node(cl, CompoundLit, node);
  3589. str = write_expr_to_string(str, cl->type);
  3590. str = gb_string_appendc(str, "{");
  3591. for_array(i, cl->elems) {
  3592. if (i > 0) {
  3593. str = gb_string_appendc(str, ", ");
  3594. }
  3595. str = write_expr_to_string(str, cl->elems[i]);
  3596. }
  3597. str = gb_string_appendc(str, "}");
  3598. case_end;
  3599. case_ast_node(te, TagExpr, node);
  3600. str = gb_string_appendc(str, "#");
  3601. str = string_append_token(str, te->name);
  3602. str = write_expr_to_string(str, te->expr);
  3603. case_end;
  3604. case_ast_node(ue, UnaryExpr, node);
  3605. str = string_append_token(str, ue->op);
  3606. str = write_expr_to_string(str, ue->expr);
  3607. case_end;
  3608. case_ast_node(de, DerefExpr, node);
  3609. str = write_expr_to_string(str, de->expr);
  3610. str = gb_string_appendc(str, "^");
  3611. case_end;
  3612. case_ast_node(de, DemaybeExpr, node);
  3613. str = write_expr_to_string(str, de->expr);
  3614. str = gb_string_appendc(str, "?");
  3615. case_end;
  3616. case_ast_node(be, BinaryExpr, node);
  3617. str = write_expr_to_string(str, be->left);
  3618. str = gb_string_appendc(str, " ");
  3619. str = string_append_token(str, be->op);
  3620. str = gb_string_appendc(str, " ");
  3621. str = write_expr_to_string(str, be->right);
  3622. case_end;
  3623. case_ast_node(pe, ParenExpr, node);
  3624. str = gb_string_appendc(str, "(");
  3625. str = write_expr_to_string(str, pe->expr);
  3626. str = gb_string_appendc(str, ")");
  3627. case_end;
  3628. case_ast_node(se, SelectorExpr, node);
  3629. str = write_expr_to_string(str, se->expr);
  3630. str = gb_string_appendc(str, ".");
  3631. str = write_expr_to_string(str, se->selector);
  3632. case_end;
  3633. case_ast_node(ie, IndexExpr, node);
  3634. str = write_expr_to_string(str, ie->expr);
  3635. str = gb_string_appendc(str, "[");
  3636. str = write_expr_to_string(str, ie->index);
  3637. str = gb_string_appendc(str, "]");
  3638. case_end;
  3639. case_ast_node(se, SliceExpr, node);
  3640. str = write_expr_to_string(str, se->expr);
  3641. str = gb_string_appendc(str, "[");
  3642. str = write_expr_to_string(str, se->low);
  3643. str = gb_string_appendc(str, ":");
  3644. str = write_expr_to_string(str, se->high);
  3645. if (se->triple_indexed) {
  3646. str = gb_string_appendc(str, ":");
  3647. str = write_expr_to_string(str, se->max);
  3648. }
  3649. str = gb_string_appendc(str, "]");
  3650. case_end;
  3651. case_ast_node(e, Ellipsis, node);
  3652. str = gb_string_appendc(str, "..");
  3653. case_end;
  3654. case_ast_node(fv, FieldValue, node);
  3655. str = write_expr_to_string(str, fv->field);
  3656. str = gb_string_appendc(str, " = ");
  3657. str = write_expr_to_string(str, fv->value);
  3658. case_end;
  3659. case_ast_node(pt, PointerType, node);
  3660. str = gb_string_appendc(str, "^");
  3661. str = write_expr_to_string(str, pt->type);
  3662. case_end;
  3663. case_ast_node(mt, MaybeType, node);
  3664. str = gb_string_appendc(str, "?");
  3665. str = write_expr_to_string(str, mt->type);
  3666. case_end;
  3667. case_ast_node(at, ArrayType, node);
  3668. str = gb_string_appendc(str, "[");
  3669. str = write_expr_to_string(str, at->count);
  3670. str = gb_string_appendc(str, "]");
  3671. str = write_expr_to_string(str, at->elem);
  3672. case_end;
  3673. case_ast_node(vt, VectorType, node);
  3674. str = gb_string_appendc(str, "{");
  3675. str = write_expr_to_string(str, vt->count);
  3676. str = gb_string_appendc(str, "}");
  3677. str = write_expr_to_string(str, vt->elem);
  3678. case_end;
  3679. case_ast_node(p, Parameter, node);
  3680. if (p->is_using) {
  3681. str = gb_string_appendc(str, "using ");
  3682. }
  3683. for_array(i, p->names) {
  3684. AstNode *name = p->names[i];
  3685. if (i > 0)
  3686. str = gb_string_appendc(str, ", ");
  3687. str = write_expr_to_string(str, name);
  3688. }
  3689. str = gb_string_appendc(str, ": ");
  3690. str = write_expr_to_string(str, p->type);
  3691. case_end;
  3692. case_ast_node(ce, CallExpr, node);
  3693. str = write_expr_to_string(str, ce->proc);
  3694. str = gb_string_appendc(str, "(");
  3695. for_array(i, ce->args) {
  3696. AstNode *arg = ce->args[i];
  3697. if (i > 0) {
  3698. str = gb_string_appendc(str, ", ");
  3699. }
  3700. str = write_expr_to_string(str, arg);
  3701. }
  3702. str = gb_string_appendc(str, ")");
  3703. case_end;
  3704. case_ast_node(pt, ProcType, node);
  3705. str = gb_string_appendc(str, "proc(");
  3706. str = write_params_to_string(str, pt->params, ", ");
  3707. str = gb_string_appendc(str, ")");
  3708. case_end;
  3709. case_ast_node(st, StructType, node);
  3710. str = gb_string_appendc(str, "struct ");
  3711. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  3712. if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
  3713. for_array(i, st->decls) {
  3714. if (i > 0) {
  3715. str = gb_string_appendc(str, "; ");
  3716. }
  3717. str = write_expr_to_string(str, st->decls[i]);
  3718. }
  3719. // str = write_params_to_string(str, st->decl_list, ", ");
  3720. str = gb_string_appendc(str, "}");
  3721. case_end;
  3722. case_ast_node(st, RawUnionType, node);
  3723. str = gb_string_appendc(str, "raw_union {");
  3724. for_array(i, st->decls) {
  3725. if (i > 0) {
  3726. str = gb_string_appendc(str, "; ");
  3727. }
  3728. str = write_expr_to_string(str, st->decls[i]);
  3729. }
  3730. // str = write_params_to_string(str, st->decl_list, ", ");
  3731. str = gb_string_appendc(str, "}");
  3732. case_end;
  3733. case_ast_node(st, UnionType, node);
  3734. str = gb_string_appendc(str, "union {");
  3735. for_array(i, st->decls) {
  3736. if (i > 0) {
  3737. str = gb_string_appendc(str, "; ");
  3738. }
  3739. str = write_expr_to_string(str, st->decls[i]);
  3740. }
  3741. // str = write_params_to_string(str, st->decl_list, ", ");
  3742. str = gb_string_appendc(str, "}");
  3743. case_end;
  3744. case_ast_node(et, EnumType, node);
  3745. str = gb_string_appendc(str, "enum ");
  3746. if (et->base_type != NULL) {
  3747. str = write_expr_to_string(str, et->base_type);
  3748. str = gb_string_appendc(str, " ");
  3749. }
  3750. str = gb_string_appendc(str, "{");
  3751. str = gb_string_appendc(str, "}");
  3752. case_end;
  3753. }
  3754. return str;
  3755. }
  3756. gbString expr_to_string(AstNode *expression) {
  3757. return write_expr_to_string(gb_string_make(gb_heap_allocator(), ""), expression);
  3758. }