check_builtin.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572
  1. typedef bool (BuiltinTypeIsProc)(Type *t);
  2. BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
  3. nullptr, // BuiltinProc__type_simple_boolean_begin
  4. is_type_boolean,
  5. is_type_integer,
  6. is_type_rune,
  7. is_type_float,
  8. is_type_complex,
  9. is_type_quaternion,
  10. is_type_string,
  11. is_type_typeid,
  12. is_type_any,
  13. is_type_endian_platform,
  14. is_type_endian_little,
  15. is_type_endian_big,
  16. is_type_unsigned,
  17. is_type_numeric,
  18. is_type_ordered,
  19. is_type_ordered_numeric,
  20. is_type_indexable,
  21. is_type_sliceable,
  22. is_type_comparable,
  23. is_type_simple_compare,
  24. is_type_dereferenceable,
  25. is_type_valid_for_keys,
  26. is_type_valid_for_matrix_elems,
  27. is_type_named,
  28. is_type_pointer,
  29. is_type_multi_pointer,
  30. is_type_array,
  31. is_type_enumerated_array,
  32. is_type_slice,
  33. is_type_dynamic_array,
  34. is_type_map,
  35. is_type_struct,
  36. is_type_union,
  37. is_type_enum,
  38. is_type_proc,
  39. is_type_bit_set,
  40. is_type_simd_vector,
  41. is_type_matrix,
  42. is_type_polymorphic_record_specialized,
  43. is_type_polymorphic_record_unspecialized,
  44. type_has_nil,
  45. };
  46. void check_or_else_right_type(CheckerContext *c, Ast *expr, String const &name, Type *right_type) {
  47. if (right_type == nullptr) {
  48. return;
  49. }
  50. if (!is_type_boolean(right_type) && !type_has_nil(right_type)) {
  51. gbString str = type_to_string(right_type);
  52. error(expr, "'%.*s' expects an \"optional ok\" like value, or an n-valued expression where the last value is either a boolean or can be compared against 'nil', got %s", LIT(name), str);
  53. gb_string_free(str);
  54. }
  55. }
  56. void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
  57. Type *left_type = nullptr;
  58. Type *right_type = nullptr;
  59. if (x->type->kind == Type_Tuple) {
  60. auto const &vars = x->type->Tuple.variables;
  61. auto lhs = slice(vars, 0, vars.count-1);
  62. auto rhs = vars[vars.count-1];
  63. if (lhs.count == 1) {
  64. left_type = lhs[0]->type;
  65. } else if (lhs.count != 0) {
  66. left_type = alloc_type_tuple();
  67. left_type->Tuple.variables = lhs;
  68. }
  69. right_type = rhs->type;
  70. } else {
  71. check_promote_optional_ok(c, x, &left_type, &right_type);
  72. }
  73. if (left_type_) *left_type_ = left_type;
  74. if (right_type_) *right_type_ = right_type;
  75. check_or_else_right_type(c, x->expr, name, right_type);
  76. }
  77. void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) {
  78. // TODO(bill): better error message
  79. gbString t = type_to_string(x.type);
  80. error(x.expr, "'%.*s' does not return a value, value is of type %s", LIT(name), t);
  81. if (is_type_union(type_deref(x.type))) {
  82. Type *bsrc = base_type(type_deref(x.type));
  83. gbString th = nullptr;
  84. if (type_hint != nullptr) {
  85. GB_ASSERT(bsrc->kind == Type_Union);
  86. for_array(i, bsrc->Union.variants) {
  87. Type *vt = bsrc->Union.variants[i];
  88. if (are_types_identical(vt, type_hint)) {
  89. th = type_to_string(type_hint);
  90. break;
  91. }
  92. }
  93. }
  94. gbString expr_str = expr_to_string(x.expr);
  95. if (th != nullptr) {
  96. error_line("\tSuggestion: was a type assertion such as %s.(%s) or %s.? wanted?\n", expr_str, th, expr_str);
  97. } else {
  98. error_line("\tSuggestion: was a type assertion such as %s.(T) or %s.? wanted?\n", expr_str, expr_str);
  99. }
  100. gb_string_free(th);
  101. gb_string_free(expr_str);
  102. }
  103. gb_string_free(t);
  104. }
  105. void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
  106. Type *left_type = nullptr;
  107. Type *right_type = nullptr;
  108. if (x->type->kind == Type_Tuple) {
  109. auto const &vars = x->type->Tuple.variables;
  110. auto lhs = slice(vars, 0, vars.count-1);
  111. auto rhs = vars[vars.count-1];
  112. if (lhs.count == 1) {
  113. left_type = lhs[0]->type;
  114. } else if (lhs.count != 0) {
  115. left_type = alloc_type_tuple();
  116. left_type->Tuple.variables = lhs;
  117. }
  118. right_type = rhs->type;
  119. } else {
  120. check_promote_optional_ok(c, x, &left_type, &right_type);
  121. }
  122. if (left_type_) *left_type_ = left_type;
  123. if (right_type_) *right_type_ = right_type;
  124. check_or_else_right_type(c, x->expr, name, right_type);
  125. }
  126. bool does_require_msgSend_stret(Type *return_type) {
  127. if (return_type == nullptr) {
  128. return false;
  129. }
  130. if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
  131. i64 struct_limit = type_size_of(t_uintptr) << 1;
  132. return type_size_of(return_type) > struct_limit;
  133. }
  134. if (build_context.metrics.arch == TargetArch_arm64) {
  135. return false;
  136. }
  137. // if (build_context.metrics.arch == TargetArch_arm32) {
  138. // i64 struct_limit = type_size_of(t_uintptr);
  139. // // NOTE(bill): This is technically wrong
  140. // return is_type_struct(return_type) && !is_type_raw_union(return_type) && type_size_of(return_type) > struct_limit;
  141. // }
  142. GB_PANIC("unsupported architecture");
  143. return false;
  144. }
  145. ObjcMsgKind get_objc_proc_kind(Type *return_type) {
  146. if (return_type == nullptr) {
  147. return ObjcMsg_normal;
  148. }
  149. if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
  150. if (is_type_float(return_type)) {
  151. return ObjcMsg_fpret;
  152. }
  153. if (build_context.metrics.arch == TargetArch_amd64) {
  154. if (is_type_complex(return_type)) {
  155. // URL: https://github.com/opensource-apple/objc4/blob/cd5e62a5597ea7a31dccef089317abb3a661c154/runtime/message.h#L143-L159
  156. return ObjcMsg_fpret;
  157. }
  158. }
  159. }
  160. if (build_context.metrics.arch != TargetArch_arm64) {
  161. if (does_require_msgSend_stret(return_type)) {
  162. return ObjcMsg_stret;
  163. }
  164. }
  165. return ObjcMsg_normal;
  166. }
  167. void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_type, Slice<Type *> param_types) {
  168. ObjcMsgKind kind = get_objc_proc_kind(return_type);
  169. Scope *scope = create_scope(c->info, nullptr);
  170. // NOTE(bill, 2022-02-08): the backend's ABI handling should handle this correctly, I hope
  171. Type *params = alloc_type_tuple();
  172. {
  173. auto variables = array_make<Entity *>(permanent_allocator(), 0, param_types.count);
  174. for_array(i, param_types) {
  175. Type *type = param_types[i];
  176. Entity *param = alloc_entity_param(scope, blank_token, type, false, true);
  177. array_add(&variables, param);
  178. }
  179. params->Tuple.variables = slice_from_array(variables);
  180. }
  181. Type *results = alloc_type_tuple();
  182. if (return_type) {
  183. auto variables = array_make<Entity *>(permanent_allocator(), 1);
  184. results->Tuple.variables = slice_from_array(variables);
  185. Entity *param = alloc_entity_param(scope, blank_token, return_type, false, true);
  186. results->Tuple.variables[0] = param;
  187. }
  188. ObjcMsgData data = {};
  189. data.kind = kind;
  190. data.proc_type = alloc_type_proc(scope, params, param_types.count, results, results->Tuple.variables.count, false, ProcCC_CDecl);
  191. mutex_lock(&c->info->objc_types_mutex);
  192. map_set(&c->info->objc_msgSend_types, call, data);
  193. mutex_unlock(&c->info->objc_types_mutex);
  194. try_to_add_package_dependency(c, "runtime", "objc_msgSend");
  195. try_to_add_package_dependency(c, "runtime", "objc_msgSend_fpret");
  196. try_to_add_package_dependency(c, "runtime", "objc_msgSend_fp2ret");
  197. try_to_add_package_dependency(c, "runtime", "objc_msgSend_stret");
  198. }
  199. bool is_constant_string(CheckerContext *c, String const &builtin_name, Ast *expr, String *name_) {
  200. Operand op = {};
  201. check_expr(c, &op, expr);
  202. if (op.mode == Addressing_Constant && op.value.kind == ExactValue_String) {
  203. if (name_) *name_ = op.value.value_string;
  204. return true;
  205. }
  206. gbString e = expr_to_string(op.expr);
  207. gbString t = type_to_string(op.type);
  208. error(op.expr, "'%.*s' expected a constant string value, got %s of type %s", LIT(builtin_name), e, t);
  209. gb_string_free(t);
  210. gb_string_free(e);
  211. return false;
  212. }
  213. bool check_builtin_objc_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  214. String builtin_name = builtin_procs[id].name;
  215. if (build_context.metrics.os != TargetOs_darwin) {
  216. // allow on doc generation (e.g. Metal stuff)
  217. if (build_context.command_kind != Command_doc && build_context.command_kind != Command_check) {
  218. error(call, "'%.*s' only works on darwin", LIT(builtin_name));
  219. }
  220. }
  221. ast_node(ce, CallExpr, call);
  222. switch (id) {
  223. default:
  224. GB_PANIC("Implement objective built-in procedure: %.*s", LIT(builtin_name));
  225. return false;
  226. case BuiltinProc_objc_send: {
  227. Type *return_type = nullptr;
  228. Operand rt = {};
  229. check_expr_or_type(c, &rt, ce->args[0]);
  230. if (rt.mode == Addressing_Type) {
  231. return_type = rt.type;
  232. } else if (is_operand_nil(rt)) {
  233. return_type = nullptr;
  234. } else {
  235. gbString e = expr_to_string(rt.expr);
  236. error(rt.expr, "'%.*s' expected a type or nil to define the return type of the Objective-C call, got %s", LIT(builtin_name), e);
  237. gb_string_free(e);
  238. return false;
  239. }
  240. operand->type = return_type;
  241. operand->mode = return_type ? Addressing_Value : Addressing_NoValue;
  242. String class_name = {};
  243. String sel_name = {};
  244. Type *sel_type = t_objc_SEL;
  245. Operand self = {};
  246. check_expr_or_type(c, &self, ce->args[1]);
  247. if (self.mode == Addressing_Type) {
  248. if (!is_type_objc_object(self.type)) {
  249. gbString t = type_to_string(self.type);
  250. error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got type %s", LIT(builtin_name), t);
  251. gb_string_free(t);
  252. return false;
  253. }
  254. if (!has_type_got_objc_class_attribute(self.type)) {
  255. gbString t = type_to_string(self.type);
  256. error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
  257. gb_string_free(t);
  258. return false;
  259. }
  260. sel_type = t_objc_Class;
  261. } else if (!is_operand_value(self) || !check_is_assignable_to(c, &self, t_objc_id)) {
  262. gbString e = expr_to_string(self.expr);
  263. gbString t = type_to_string(self.type);
  264. error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
  265. gb_string_free(t);
  266. gb_string_free(e);
  267. return false;
  268. } else if (!is_type_pointer(self.type)) {
  269. gbString e = expr_to_string(self.expr);
  270. gbString t = type_to_string(self.type);
  271. error(self.expr, "'%.*s' expected a pointer of a value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
  272. gb_string_free(t);
  273. gb_string_free(e);
  274. return false;
  275. } else {
  276. Type *type = type_deref(self.type);
  277. if (!(type->kind == Type_Named &&
  278. type->Named.type_name != nullptr &&
  279. type->Named.type_name->TypeName.objc_class_name != "")) {
  280. gbString t = type_to_string(type);
  281. error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
  282. gb_string_free(t);
  283. return false;
  284. }
  285. }
  286. if (!is_constant_string(c, builtin_name, ce->args[2], &sel_name)) {
  287. return false;
  288. }
  289. isize const arg_offset = 1;
  290. auto param_types = slice_make<Type *>(permanent_allocator(), ce->args.count-arg_offset);
  291. param_types[0] = t_objc_id;
  292. param_types[1] = sel_type;
  293. for (isize i = 2+arg_offset; i < ce->args.count; i++) {
  294. Operand x = {};
  295. check_expr(c, &x, ce->args[i]);
  296. param_types[i-arg_offset] = x.type;
  297. }
  298. add_objc_proc_type(c, call, return_type, param_types);
  299. return true;
  300. } break;
  301. case BuiltinProc_objc_find_selector:
  302. case BuiltinProc_objc_find_class:
  303. case BuiltinProc_objc_register_selector:
  304. case BuiltinProc_objc_register_class:
  305. {
  306. String sel_name = {};
  307. if (!is_constant_string(c, builtin_name, ce->args[0], &sel_name)) {
  308. return false;
  309. }
  310. switch (id) {
  311. case BuiltinProc_objc_find_selector:
  312. case BuiltinProc_objc_register_selector:
  313. operand->type = t_objc_SEL;
  314. break;
  315. case BuiltinProc_objc_find_class:
  316. case BuiltinProc_objc_register_class:
  317. operand->type = t_objc_Class;
  318. break;
  319. }
  320. operand->mode = Addressing_Value;
  321. try_to_add_package_dependency(c, "runtime", "objc_lookUpClass");
  322. try_to_add_package_dependency(c, "runtime", "sel_registerName");
  323. try_to_add_package_dependency(c, "runtime", "objc_allocateClassPair");
  324. return true;
  325. } break;
  326. }
  327. }
  328. bool check_atomic_memory_order_argument(CheckerContext *c, Ast *expr, String const &builtin_name, OdinAtomicMemoryOrder *memory_order_, char const *extra_message = nullptr) {
  329. Operand x = {};
  330. check_expr_with_type_hint(c, &x, expr, t_atomic_memory_order);
  331. if (x.mode == Addressing_Invalid) {
  332. return false;
  333. }
  334. if (!are_types_identical(x.type, t_atomic_memory_order) || x.mode != Addressing_Constant) {
  335. gbString str = type_to_string(x.type);
  336. if (extra_message) {
  337. error(x.expr, "Expected a constant Atomic_Memory_Order value for the %s of '%.*s', got %s", extra_message, LIT(builtin_name), str);
  338. } else {
  339. error(x.expr, "Expected a constant Atomic_Memory_Order value for '%.*s', got %s", LIT(builtin_name), str);
  340. }
  341. gb_string_free(str);
  342. return false;
  343. }
  344. i64 value = exact_value_to_i64(x.value);
  345. if (value < 0 || value >= OdinAtomicMemoryOrder_COUNT) {
  346. error(x.expr, "Illegal Atomic_Memory_Order value, got %lld", cast(long long)value);
  347. return false;
  348. }
  349. if (memory_order_) {
  350. *memory_order_ = cast(OdinAtomicMemoryOrder)value;
  351. }
  352. return true;
  353. }
  354. bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  355. ast_node(ce, CallExpr, call);
  356. if (ce->inlining != ProcInlining_none) {
  357. error(call, "Inlining operators are not allowed on built-in procedures");
  358. }
  359. BuiltinProc *bp = &builtin_procs[id];
  360. {
  361. char const *err = nullptr;
  362. if (ce->args.count < bp->arg_count) {
  363. err = "Too few";
  364. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  365. err = "Too many";
  366. }
  367. if (err != nullptr) {
  368. gbString expr = expr_to_string(ce->proc);
  369. error(ce->close, "%s arguments for '%s', expected %td, got %td",
  370. err, expr,
  371. bp->arg_count, ce->args.count);
  372. gb_string_free(expr);
  373. return false;
  374. }
  375. }
  376. switch (id) {
  377. case BuiltinProc_size_of:
  378. case BuiltinProc_align_of:
  379. case BuiltinProc_offset_of:
  380. case BuiltinProc_offset_of_by_string:
  381. case BuiltinProc_type_info_of:
  382. case BuiltinProc_typeid_of:
  383. case BuiltinProc_len:
  384. case BuiltinProc_min:
  385. case BuiltinProc_max:
  386. case BuiltinProc_type_is_subtype_of:
  387. case BuiltinProc_objc_send:
  388. case BuiltinProc_objc_find_selector:
  389. case BuiltinProc_objc_find_class:
  390. case BuiltinProc_objc_register_selector:
  391. case BuiltinProc_objc_register_class:
  392. case BuiltinProc_atomic_type_is_lock_free:
  393. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  394. break;
  395. case BuiltinProc_atomic_thread_fence:
  396. case BuiltinProc_atomic_signal_fence:
  397. // NOTE(bill): first type will require a type hint
  398. break;
  399. case BuiltinProc_DIRECTIVE: {
  400. ast_node(bd, BasicDirective, ce->proc);
  401. String name = bd->name.string;
  402. if (name == "defined") {
  403. break;
  404. }
  405. if (name == "config") {
  406. break;
  407. }
  408. /*fallthrough*/
  409. }
  410. default:
  411. if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
  412. check_expr_or_type(c, operand, ce->args[0]);
  413. } else if (ce->args.count > 0) {
  414. check_multi_expr(c, operand, ce->args[0]);
  415. }
  416. break;
  417. }
  418. String builtin_name = builtin_procs[id].name;
  419. if (ce->args.count > 0) {
  420. if (ce->args[0]->kind == Ast_FieldValue) {
  421. if (id != BuiltinProc_soa_zip) {
  422. error(call, "'field = value' calling is not allowed on built-in procedures");
  423. return false;
  424. }
  425. }
  426. }
  427. switch (id) {
  428. default:
  429. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
  430. break;
  431. case BuiltinProc_objc_send:
  432. case BuiltinProc_objc_find_selector:
  433. case BuiltinProc_objc_find_class:
  434. case BuiltinProc_objc_register_selector:
  435. case BuiltinProc_objc_register_class:
  436. return check_builtin_objc_procedure(c, operand, call, id, type_hint);
  437. case BuiltinProc___entry_point:
  438. operand->mode = Addressing_NoValue;
  439. operand->type = nullptr;
  440. mpmc_enqueue(&c->info->intrinsics_entry_point_usage, call);
  441. break;
  442. case BuiltinProc_DIRECTIVE: {
  443. ast_node(bd, BasicDirective, ce->proc);
  444. String name = bd->name.string;
  445. if (name == "location") {
  446. if (ce->args.count > 1) {
  447. error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
  448. }
  449. if (ce->args.count > 0) {
  450. Ast *arg = ce->args[0];
  451. Entity *e = nullptr;
  452. Operand o = {};
  453. if (arg->kind == Ast_Ident) {
  454. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  455. } else if (arg->kind == Ast_SelectorExpr) {
  456. e = check_selector(c, &o, arg, nullptr);
  457. }
  458. if (e == nullptr) {
  459. error(ce->args[0], "'#location' expected a valid entity name");
  460. }
  461. }
  462. operand->type = t_source_code_location;
  463. operand->mode = Addressing_Value;
  464. } else if (name == "load") {
  465. if (ce->args.count != 1) {
  466. if (ce->args.count == 0) {
  467. error(ce->close, "'#load' expects 1 argument, got 0");
  468. } else {
  469. error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
  470. }
  471. return false;
  472. }
  473. Ast *arg = ce->args[0];
  474. Operand o = {};
  475. check_expr(c, &o, arg);
  476. if (o.mode != Addressing_Constant) {
  477. error(arg, "'#load' expected a constant string argument");
  478. return false;
  479. }
  480. if (!is_type_string(o.type)) {
  481. gbString str = type_to_string(o.type);
  482. error(arg, "'#load' expected a constant string, got %s", str);
  483. gb_string_free(str);
  484. return false;
  485. }
  486. gbAllocator a = heap_allocator();
  487. GB_ASSERT(o.value.kind == ExactValue_String);
  488. String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
  489. String original_string = o.value.value_string;
  490. BlockingMutex *ignore_mutex = nullptr;
  491. String path = {};
  492. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  493. gb_unused(ok);
  494. char *c_str = alloc_cstring(a, path);
  495. defer (gb_free(a, c_str));
  496. gbFile f = {};
  497. gbFileError file_err = gb_file_open(&f, c_str);
  498. defer (gb_file_close(&f));
  499. switch (file_err) {
  500. default:
  501. case gbFileError_Invalid:
  502. error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
  503. return false;
  504. case gbFileError_NotExists:
  505. error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
  506. return false;
  507. case gbFileError_Permission:
  508. error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
  509. return false;
  510. case gbFileError_None:
  511. // Okay
  512. break;
  513. }
  514. String result = {};
  515. isize file_size = cast(isize)gb_file_size(&f);
  516. if (file_size > 0) {
  517. u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
  518. gb_file_read_at(&f, data, file_size, 0);
  519. data[file_size] = '\0';
  520. result.text = data;
  521. result.len = file_size;
  522. }
  523. operand->type = t_u8_slice;
  524. operand->mode = Addressing_Constant;
  525. operand->value = exact_value_string(result);
  526. } else if (name == "load_hash") {
  527. if (ce->args.count != 2) {
  528. if (ce->args.count == 0) {
  529. error(ce->close, "'#load_hash' expects 2 argument, got 0");
  530. } else {
  531. error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count);
  532. }
  533. return false;
  534. }
  535. Ast *arg0 = ce->args[0];
  536. Ast *arg1 = ce->args[1];
  537. Operand o = {};
  538. check_expr(c, &o, arg0);
  539. if (o.mode != Addressing_Constant) {
  540. error(arg0, "'#load_hash' expected a constant string argument");
  541. return false;
  542. }
  543. if (!is_type_string(o.type)) {
  544. gbString str = type_to_string(o.type);
  545. error(arg0, "'#load_hash' expected a constant string, got %s", str);
  546. gb_string_free(str);
  547. return false;
  548. }
  549. Operand o_hash = {};
  550. check_expr(c, &o_hash, arg1);
  551. if (o_hash.mode != Addressing_Constant) {
  552. error(arg1, "'#load_hash' expected a constant string argument");
  553. return false;
  554. }
  555. if (!is_type_string(o_hash.type)) {
  556. gbString str = type_to_string(o.type);
  557. error(arg1, "'#load_hash' expected a constant string, got %s", str);
  558. gb_string_free(str);
  559. return false;
  560. }
  561. gbAllocator a = heap_allocator();
  562. GB_ASSERT(o.value.kind == ExactValue_String);
  563. GB_ASSERT(o_hash.value.kind == ExactValue_String);
  564. String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
  565. String original_string = o.value.value_string;
  566. String hash_kind = o_hash.value.value_string;
  567. String supported_hashes[] = {
  568. str_lit("adler32"),
  569. str_lit("crc32"),
  570. str_lit("crc64"),
  571. str_lit("fnv32"),
  572. str_lit("fnv64"),
  573. str_lit("fnv32a"),
  574. str_lit("fnv64a"),
  575. str_lit("murmur32"),
  576. str_lit("murmur64"),
  577. };
  578. bool hash_found = false;
  579. for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
  580. if (supported_hashes[i] == hash_kind) {
  581. hash_found = true;
  582. break;
  583. }
  584. }
  585. if (!hash_found) {
  586. ERROR_BLOCK();
  587. error(ce->proc, "Invalid hash kind passed to `#load_hash`, got: %.*s", LIT(hash_kind));
  588. error_line("\tAvailable hash kinds:\n");
  589. for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
  590. error_line("\t%.*s\n", LIT(supported_hashes[i]));
  591. }
  592. return false;
  593. }
  594. BlockingMutex *ignore_mutex = nullptr;
  595. String path = {};
  596. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  597. gb_unused(ok);
  598. char *c_str = alloc_cstring(a, path);
  599. defer (gb_free(a, c_str));
  600. gbFile f = {};
  601. gbFileError file_err = gb_file_open(&f, c_str);
  602. defer (gb_file_close(&f));
  603. switch (file_err) {
  604. default:
  605. case gbFileError_Invalid:
  606. error(ce->proc, "Failed to `#load_hash` file: %s; invalid file or cannot be found", c_str);
  607. return false;
  608. case gbFileError_NotExists:
  609. error(ce->proc, "Failed to `#load_hash` file: %s; file cannot be found", c_str);
  610. return false;
  611. case gbFileError_Permission:
  612. error(ce->proc, "Failed to `#load_hash` file: %s; file permissions problem", c_str);
  613. return false;
  614. case gbFileError_None:
  615. // Okay
  616. break;
  617. }
  618. // TODO(bill): make these procedures fast :P
  619. u64 hash_value = 0;
  620. String result = {};
  621. isize file_size = cast(isize)gb_file_size(&f);
  622. if (file_size > 0) {
  623. u8 *data = cast(u8 *)gb_alloc(a, file_size);
  624. gb_file_read_at(&f, data, file_size, 0);
  625. if (hash_kind == "adler32") {
  626. hash_value = gb_adler32(data, file_size);
  627. } else if (hash_kind == "crc32") {
  628. hash_value = gb_crc32(data, file_size);
  629. } else if (hash_kind == "crc64") {
  630. hash_value = gb_crc64(data, file_size);
  631. } else if (hash_kind == "fnv32") {
  632. hash_value = gb_fnv32(data, file_size);
  633. } else if (hash_kind == "fnv64") {
  634. hash_value = gb_fnv64(data, file_size);
  635. } else if (hash_kind == "fnv32a") {
  636. hash_value = fnv32a(data, file_size);
  637. } else if (hash_kind == "fnv64a") {
  638. hash_value = fnv64a(data, file_size);
  639. } else if (hash_kind == "murmur32") {
  640. hash_value = gb_murmur32(data, file_size);
  641. } else if (hash_kind == "murmur64") {
  642. hash_value = gb_murmur64(data, file_size);
  643. } else {
  644. compiler_error("unhandled hash kind: %.*s", LIT(hash_kind));
  645. }
  646. gb_free(a, data);
  647. }
  648. operand->type = t_untyped_integer;
  649. operand->mode = Addressing_Constant;
  650. operand->value = exact_value_u64(hash_value);
  651. } else if (name == "load_or") {
  652. if (ce->args.count != 2) {
  653. if (ce->args.count == 0) {
  654. error(ce->close, "'#load_or' expects 2 arguments, got 0");
  655. } else {
  656. error(ce->args[0], "'#load_or' expects 2 arguments, got %td", ce->args.count);
  657. }
  658. return false;
  659. }
  660. Ast *arg = ce->args[0];
  661. Operand o = {};
  662. check_expr(c, &o, arg);
  663. if (o.mode != Addressing_Constant) {
  664. error(arg, "'#load_or' expected a constant string argument");
  665. return false;
  666. }
  667. if (!is_type_string(o.type)) {
  668. gbString str = type_to_string(o.type);
  669. error(arg, "'#load_or' expected a constant string, got %s", str);
  670. gb_string_free(str);
  671. return false;
  672. }
  673. Ast *default_arg = ce->args[1];
  674. Operand default_op = {};
  675. check_expr_with_type_hint(c, &default_op, default_arg, t_u8_slice);
  676. if (default_op.mode != Addressing_Constant) {
  677. error(arg, "'#load_or' expected a constant '[]byte' argument");
  678. return false;
  679. }
  680. if (!are_types_identical(base_type(default_op.type), t_u8_slice)) {
  681. gbString str = type_to_string(default_op.type);
  682. error(arg, "'#load_or' expected a constant '[]byte', got %s", str);
  683. gb_string_free(str);
  684. return false;
  685. }
  686. gbAllocator a = heap_allocator();
  687. GB_ASSERT(o.value.kind == ExactValue_String);
  688. String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
  689. String original_string = o.value.value_string;
  690. BlockingMutex *ignore_mutex = nullptr;
  691. String path = {};
  692. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  693. gb_unused(ok);
  694. char *c_str = alloc_cstring(a, path);
  695. defer (gb_free(a, c_str));
  696. gbFile f = {};
  697. gbFileError file_err = gb_file_open(&f, c_str);
  698. defer (gb_file_close(&f));
  699. operand->type = t_u8_slice;
  700. operand->mode = Addressing_Constant;
  701. if (file_err == gbFileError_None) {
  702. String result = {};
  703. isize file_size = cast(isize)gb_file_size(&f);
  704. if (file_size > 0) {
  705. u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
  706. gb_file_read_at(&f, data, file_size, 0);
  707. data[file_size] = '\0';
  708. result.text = data;
  709. result.len = file_size;
  710. }
  711. operand->value = exact_value_string(result);
  712. } else {
  713. operand->value = default_op.value;
  714. }
  715. } else if (name == "assert") {
  716. if (ce->args.count != 1 && ce->args.count != 2) {
  717. error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count);
  718. return false;
  719. }
  720. if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
  721. gbString str = expr_to_string(ce->args[0]);
  722. error(call, "'%s' is not a constant boolean", str);
  723. gb_string_free(str);
  724. return false;
  725. }
  726. if (ce->args.count == 2) {
  727. Ast *arg = unparen_expr(ce->args[1]);
  728. if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) {
  729. gbString str = expr_to_string(arg);
  730. error(call, "'%s' is not a constant string", str);
  731. gb_string_free(str);
  732. return false;
  733. }
  734. }
  735. if (!operand->value.value_bool) {
  736. gbString arg1 = expr_to_string(ce->args[0]);
  737. gbString arg2 = {};
  738. if (ce->args.count == 1) {
  739. error(call, "Compile time assertion: %s", arg1);
  740. } else {
  741. arg2 = expr_to_string(ce->args[1]);
  742. error(call, "Compile time assertion: %s (%s)", arg1, arg2);
  743. }
  744. if (c->proc_name != "") {
  745. gbString str = type_to_string(c->curr_proc_sig);
  746. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  747. gb_string_free(str);
  748. }
  749. gb_string_free(arg1);
  750. if (ce->args.count == 2) {
  751. gb_string_free(arg2);
  752. }
  753. }
  754. operand->type = t_untyped_bool;
  755. operand->mode = Addressing_Constant;
  756. } else if (name == "panic") {
  757. if (ce->args.count != 1) {
  758. error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
  759. return false;
  760. }
  761. if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
  762. gbString str = expr_to_string(ce->args[0]);
  763. error(call, "'%s' is not a constant string", str);
  764. gb_string_free(str);
  765. return false;
  766. }
  767. error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
  768. if (c->proc_name != "") {
  769. gbString str = type_to_string(c->curr_proc_sig);
  770. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  771. gb_string_free(str);
  772. }
  773. operand->type = t_invalid;
  774. operand->mode = Addressing_NoValue;
  775. } else if (name == "defined") {
  776. if (ce->args.count != 1) {
  777. error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
  778. return false;
  779. }
  780. Ast *arg = unparen_expr(ce->args[0]);
  781. if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
  782. error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
  783. return false;
  784. }
  785. if (c->curr_proc_decl == nullptr) {
  786. error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
  787. return false;
  788. }
  789. bool is_defined = check_identifier_exists(c->scope, arg);
  790. gb_unused(is_defined);
  791. operand->type = t_untyped_bool;
  792. operand->mode = Addressing_Constant;
  793. operand->value = exact_value_bool(false);
  794. } else if (name == "config") {
  795. if (ce->args.count != 2) {
  796. error(call, "'#config' expects 2 argument, got %td", ce->args.count);
  797. return false;
  798. }
  799. Ast *arg = unparen_expr(ce->args[0]);
  800. if (arg == nullptr || arg->kind != Ast_Ident) {
  801. error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
  802. return false;
  803. }
  804. Ast *def_arg = unparen_expr(ce->args[1]);
  805. Operand def = {};
  806. check_expr(c, &def, def_arg);
  807. if (def.mode != Addressing_Constant) {
  808. error(def_arg, "'#config' default value must be a constant");
  809. return false;
  810. }
  811. String name = arg->Ident.token.string;
  812. operand->type = def.type;
  813. operand->mode = def.mode;
  814. operand->value = def.value;
  815. Entity *found = scope_lookup_current(config_pkg->scope, name);
  816. if (found != nullptr) {
  817. if (found->kind != Entity_Constant) {
  818. error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
  819. } else {
  820. operand->type = found->type;
  821. operand->mode = Addressing_Constant;
  822. operand->value = found->Constant.value;
  823. }
  824. }
  825. } else {
  826. error(call, "Unknown directive call: #%.*s", LIT(name));
  827. }
  828. break;
  829. }
  830. case BuiltinProc_len:
  831. check_expr_or_type(c, operand, ce->args[0]);
  832. if (operand->mode == Addressing_Invalid) {
  833. return false;
  834. }
  835. /* fallthrough */
  836. case BuiltinProc_cap:
  837. {
  838. // len :: proc(Type) -> int
  839. // cap :: proc(Type) -> int
  840. Type *op_type = type_deref(operand->type);
  841. Type *type = t_int;
  842. if (type_hint != nullptr) {
  843. Type *bt = type_hint;
  844. // bt = base_type(bt);
  845. if (bt == t_int) {
  846. type = type_hint;
  847. } else if (bt == t_uint) {
  848. type = type_hint;
  849. }
  850. }
  851. AddressingMode mode = Addressing_Invalid;
  852. ExactValue value = {};
  853. if (is_type_string(op_type) && id == BuiltinProc_len) {
  854. if (operand->mode == Addressing_Constant) {
  855. mode = Addressing_Constant;
  856. String str = operand->value.value_string;
  857. value = exact_value_i64(str.len);
  858. type = t_untyped_integer;
  859. } else {
  860. mode = Addressing_Value;
  861. if (is_type_cstring(op_type)) {
  862. add_package_dependency(c, "runtime", "cstring_len");
  863. }
  864. }
  865. } else if (is_type_array(op_type)) {
  866. Type *at = core_type(op_type);
  867. mode = Addressing_Constant;
  868. value = exact_value_i64(at->Array.count);
  869. type = t_untyped_integer;
  870. } else if (is_type_enumerated_array(op_type) && id == BuiltinProc_len) {
  871. Type *at = core_type(op_type);
  872. mode = Addressing_Constant;
  873. value = exact_value_i64(at->EnumeratedArray.count);
  874. type = t_untyped_integer;
  875. } else if ((is_type_slice(op_type) || is_type_relative_slice(op_type)) && id == BuiltinProc_len) {
  876. mode = Addressing_Value;
  877. } else if (is_type_dynamic_array(op_type)) {
  878. mode = Addressing_Value;
  879. } else if (is_type_map(op_type)) {
  880. mode = Addressing_Value;
  881. } else if (operand->mode == Addressing_Type && is_type_enum(op_type) && id == BuiltinProc_len) {
  882. Type *bt = base_type(op_type);
  883. mode = Addressing_Constant;
  884. value = exact_value_i64(bt->Enum.fields.count);
  885. type = t_untyped_integer;
  886. } else if (is_type_struct(op_type)) {
  887. Type *bt = base_type(op_type);
  888. if (bt->Struct.soa_kind == StructSoa_Fixed) {
  889. mode = Addressing_Constant;
  890. value = exact_value_i64(bt->Struct.soa_count);
  891. type = t_untyped_integer;
  892. } else if ((bt->Struct.soa_kind == StructSoa_Slice && id == BuiltinProc_len) ||
  893. bt->Struct.soa_kind == StructSoa_Dynamic) {
  894. mode = Addressing_Value;
  895. }
  896. }
  897. if (operand->mode == Addressing_Type && mode != Addressing_Constant) {
  898. mode = Addressing_Invalid;
  899. }
  900. if (mode == Addressing_Invalid) {
  901. gbString t = type_to_string(operand->type);
  902. error(call, "'%.*s' is not supported for '%s'", LIT(builtin_name), t);
  903. return false;
  904. }
  905. operand->mode = mode;
  906. operand->value = value;
  907. operand->type = type;
  908. break;
  909. }
  910. case BuiltinProc_size_of: {
  911. // size_of :: proc(Type or expr) -> untyped int
  912. Operand o = {};
  913. check_expr_or_type(c, &o, ce->args[0]);
  914. if (o.mode == Addressing_Invalid) {
  915. return false;
  916. }
  917. Type *t = o.type;
  918. if (t == nullptr || t == t_invalid) {
  919. error(ce->args[0], "Invalid argument for 'size_of'");
  920. return false;
  921. }
  922. t = default_type(t);
  923. operand->mode = Addressing_Constant;
  924. operand->value = exact_value_i64(type_size_of(t));
  925. operand->type = t_untyped_integer;
  926. break;
  927. }
  928. case BuiltinProc_align_of: {
  929. // align_of :: proc(Type or expr) -> untyped int
  930. Operand o = {};
  931. check_expr_or_type(c, &o, ce->args[0]);
  932. if (o.mode == Addressing_Invalid) {
  933. return false;
  934. }
  935. Type *t = o.type;
  936. if (t == nullptr || t == t_invalid) {
  937. error(ce->args[0], "Invalid argument for 'align_of'");
  938. return false;
  939. }
  940. t = default_type(t);
  941. operand->mode = Addressing_Constant;
  942. operand->value = exact_value_i64(type_align_of(t));
  943. operand->type = t_untyped_integer;
  944. break;
  945. }
  946. case BuiltinProc_offset_of: {
  947. // offset_of :: proc(value.field) -> uintptr
  948. // offset_of :: proc(Type, field) -> uintptr
  949. Type *type = nullptr;
  950. Ast *field_arg = nullptr;
  951. if (ce->args.count == 1) {
  952. Ast *arg0 = unparen_expr(ce->args[0]);
  953. if (arg0->kind != Ast_SelectorExpr) {
  954. gbString x = expr_to_string(arg0);
  955. error(ce->args[0], "Invalid expression for '%.*s', '%s' is not a selector expression", LIT(builtin_name), x);
  956. gb_string_free(x);
  957. return false;
  958. }
  959. ast_node(se, SelectorExpr, arg0);
  960. Operand x = {};
  961. check_expr(c, &x, se->expr);
  962. if (x.mode == Addressing_Invalid) {
  963. return false;
  964. }
  965. type = type_deref(x.type);
  966. Type *bt = base_type(type);
  967. if (bt == nullptr || bt == t_invalid) {
  968. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  969. return false;
  970. }
  971. field_arg = unparen_expr(se->selector);
  972. } else if (ce->args.count == 2) {
  973. type = check_type(c, ce->args[0]);
  974. Type *bt = base_type(type);
  975. if (bt == nullptr || bt == t_invalid) {
  976. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  977. return false;
  978. }
  979. field_arg = unparen_expr(ce->args[1]);
  980. } else {
  981. error(ce->args[0], "Expected either 1 or 2 arguments to '%.*s', in the format of '%.*s(Type, field)', '%.*s(value.field)'", LIT(builtin_name), LIT(builtin_name), LIT(builtin_name));
  982. return false;
  983. }
  984. GB_ASSERT(type != nullptr);
  985. String field_name = {};
  986. if (field_arg == nullptr) {
  987. error(call, "Expected an identifier for field argument");
  988. return false;
  989. }
  990. if (field_arg->kind == Ast_Ident) {
  991. field_name = field_arg->Ident.token.string;
  992. }
  993. if (field_name.len == 0) {
  994. error(field_arg, "Expected an identifier for field argument");
  995. return false;
  996. }
  997. if (is_type_array(type)) {
  998. gbString t = type_to_string(type);
  999. error(field_arg, "Invalid a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
  1000. gb_string_free(t);
  1001. return false;
  1002. }
  1003. Selection sel = lookup_field(type, field_name, false);
  1004. if (sel.entity == nullptr) {
  1005. gbString type_str = type_to_string_shorthand(type);
  1006. error(ce->args[0],
  1007. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  1008. gb_string_free(type_str);
  1009. Type *bt = base_type(type);
  1010. if (bt->kind == Type_Struct) {
  1011. check_did_you_mean_type(field_name, bt->Struct.fields);
  1012. }
  1013. return false;
  1014. }
  1015. if (sel.indirect) {
  1016. gbString type_str = type_to_string_shorthand(type);
  1017. error(ce->args[0],
  1018. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  1019. gb_string_free(type_str);
  1020. return false;
  1021. }
  1022. operand->mode = Addressing_Constant;
  1023. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  1024. operand->type = t_uintptr;
  1025. break;
  1026. }
  1027. case BuiltinProc_offset_of_by_string: {
  1028. // offset_of_by_string :: proc(Type, string) -> uintptr
  1029. Type *type = nullptr;
  1030. Ast *field_arg = nullptr;
  1031. if (ce->args.count == 2) {
  1032. type = check_type(c, ce->args[0]);
  1033. Type *bt = base_type(type);
  1034. if (bt == nullptr || bt == t_invalid) {
  1035. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  1036. return false;
  1037. }
  1038. field_arg = unparen_expr(ce->args[1]);
  1039. } else {
  1040. error(ce->args[0], "Expected either 2 arguments to '%.*s', in the format of '%.*s(Type, field)'", LIT(builtin_name), LIT(builtin_name));
  1041. return false;
  1042. }
  1043. GB_ASSERT(type != nullptr);
  1044. String field_name = {};
  1045. if (field_arg == nullptr) {
  1046. error(call, "Expected a constant (not-empty) string for field argument");
  1047. return false;
  1048. }
  1049. Operand x = {};
  1050. check_expr(c, &x, field_arg);
  1051. if (x.mode == Addressing_Constant && x.value.kind == ExactValue_String) {
  1052. field_name = x.value.value_string;
  1053. }
  1054. if (field_name.len == 0) {
  1055. error(field_arg, "Expected a constant (non-empty) string for field argument");
  1056. return false;
  1057. }
  1058. if (is_type_array(type)) {
  1059. gbString t = type_to_string(type);
  1060. error(field_arg, "Invalid a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
  1061. gb_string_free(t);
  1062. return false;
  1063. }
  1064. Selection sel = lookup_field(type, field_name, false);
  1065. if (sel.entity == nullptr) {
  1066. gbString type_str = type_to_string_shorthand(type);
  1067. error(ce->args[0],
  1068. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  1069. gb_string_free(type_str);
  1070. Type *bt = base_type(type);
  1071. if (bt->kind == Type_Struct) {
  1072. check_did_you_mean_type(field_name, bt->Struct.fields);
  1073. }
  1074. return false;
  1075. }
  1076. if (sel.indirect) {
  1077. gbString type_str = type_to_string_shorthand(type);
  1078. error(ce->args[0],
  1079. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  1080. gb_string_free(type_str);
  1081. return false;
  1082. }
  1083. operand->mode = Addressing_Constant;
  1084. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  1085. operand->type = t_uintptr;
  1086. break;
  1087. }
  1088. case BuiltinProc_type_of: {
  1089. // type_of :: proc(val: Type) -> type(Type)
  1090. Ast *expr = ce->args[0];
  1091. Operand o = {};
  1092. check_expr_or_type(c, &o, expr);
  1093. // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
  1094. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  1095. return false;
  1096. }
  1097. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  1098. error(o.expr, "Invalid argument to 'type_of'");
  1099. return false;
  1100. }
  1101. // NOTE(bill): Prevent type cycles for procedure declarations
  1102. if (c->curr_proc_sig == o.type) {
  1103. gbString s = expr_to_string(o.expr);
  1104. error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
  1105. gb_string_free(s);
  1106. return false;
  1107. }
  1108. if (is_type_polymorphic(o.type)) {
  1109. error(o.expr, "'type_of' of polymorphic type cannot be determined");
  1110. return false;
  1111. }
  1112. operand->mode = Addressing_Type;
  1113. operand->type = o.type;
  1114. break;
  1115. }
  1116. case BuiltinProc_type_info_of: {
  1117. // type_info_of :: proc(Type) -> ^Type_Info
  1118. if (c->scope->flags&ScopeFlag_Global) {
  1119. compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  1120. }
  1121. if (build_context.disallow_rtti) {
  1122. error(call, "'%.*s' has been disallowed", LIT(builtin_name));
  1123. return false;
  1124. }
  1125. // NOTE(bill): The type information may not be setup yet
  1126. init_core_type_info(c->checker);
  1127. Ast *expr = ce->args[0];
  1128. Operand o = {};
  1129. check_expr_or_type(c, &o, expr);
  1130. if (o.mode == Addressing_Invalid) {
  1131. return false;
  1132. }
  1133. Type *t = o.type;
  1134. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(t)) {
  1135. if (is_type_polymorphic(t)) {
  1136. error(ce->args[0], "Invalid argument for '%.*s', unspecialized polymorphic type", LIT(builtin_name));
  1137. } else {
  1138. error(ce->args[0], "Invalid argument for '%.*s'", LIT(builtin_name));
  1139. }
  1140. return false;
  1141. }
  1142. t = default_type(t);
  1143. add_type_info_type(c, t);
  1144. if (is_operand_value(o) && is_type_typeid(t)) {
  1145. add_package_dependency(c, "runtime", "__type_info_of");
  1146. } else if (o.mode != Addressing_Type) {
  1147. error(expr, "Expected a type or typeid for '%.*s'", LIT(builtin_name));
  1148. return false;
  1149. }
  1150. operand->mode = Addressing_Value;
  1151. operand->type = t_type_info_ptr;
  1152. break;
  1153. }
  1154. case BuiltinProc_typeid_of: {
  1155. // typeid_of :: proc(Type) -> typeid
  1156. if (c->scope->flags&ScopeFlag_Global) {
  1157. compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  1158. }
  1159. if (build_context.disallow_rtti) {
  1160. error(call, "'%.*s' has been disallowed", LIT(builtin_name));
  1161. return false;
  1162. }
  1163. // NOTE(bill): The type information may not be setup yet
  1164. init_core_type_info(c->checker);
  1165. Ast *expr = ce->args[0];
  1166. Operand o = {};
  1167. check_expr_or_type(c, &o, expr);
  1168. if (o.mode == Addressing_Invalid) {
  1169. return false;
  1170. }
  1171. Type *t = o.type;
  1172. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(operand->type)) {
  1173. error(ce->args[0], "Invalid argument for '%.*s'", LIT(builtin_name));
  1174. return false;
  1175. }
  1176. t = default_type(t);
  1177. add_type_info_type(c, t);
  1178. if (o.mode != Addressing_Type) {
  1179. error(expr, "Expected a type for '%.*s'", LIT(builtin_name));
  1180. return false;
  1181. }
  1182. operand->mode = Addressing_Value;
  1183. operand->type = t_typeid;
  1184. operand->value = exact_value_typeid(t);
  1185. break;
  1186. }
  1187. case BuiltinProc_swizzle: {
  1188. // swizzle :: proc(v: [N]T, ..int) -> [M]T
  1189. Type *original_type = operand->type;
  1190. Type *type = base_type(original_type);
  1191. i64 max_count = 0;
  1192. Type *elem_type = nullptr;
  1193. if (!is_type_array(type) && !is_type_simd_vector(type)) {
  1194. gbString type_str = type_to_string(operand->type);
  1195. error(call,
  1196. "'swizzle' is only allowed on an array or #simd vector, got '%s'",
  1197. type_str);
  1198. gb_string_free(type_str);
  1199. return false;
  1200. }
  1201. if (type->kind == Type_Array) {
  1202. max_count = type->Array.count;
  1203. elem_type = type->Array.elem;
  1204. } else if (type->kind == Type_SimdVector) {
  1205. max_count = type->SimdVector.count;
  1206. elem_type = type->SimdVector.elem;
  1207. }
  1208. i64 arg_count = 0;
  1209. for_array(i, ce->args) {
  1210. if (i == 0) {
  1211. continue;
  1212. }
  1213. Ast *arg = ce->args[i];
  1214. Operand op = {};
  1215. check_expr(c, &op, arg);
  1216. if (op.mode == Addressing_Invalid) {
  1217. return false;
  1218. }
  1219. Type *arg_type = base_type(op.type);
  1220. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  1221. error(op.expr, "Indices to 'swizzle' must be constant integers");
  1222. return false;
  1223. }
  1224. if (big_int_is_neg(&op.value.value_integer)) {
  1225. error(op.expr, "Negative 'swizzle' index");
  1226. return false;
  1227. }
  1228. BigInt mc = {};
  1229. big_int_from_i64(&mc, max_count);
  1230. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  1231. error(op.expr, "'swizzle' index exceeds length");
  1232. return false;
  1233. }
  1234. arg_count++;
  1235. }
  1236. if (arg_count > max_count) {
  1237. error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
  1238. return false;
  1239. }
  1240. if (type->kind == Type_Array) {
  1241. if (operand->mode == Addressing_Variable) {
  1242. operand->mode = Addressing_SwizzleVariable;
  1243. } else {
  1244. operand->mode = Addressing_SwizzleValue;
  1245. }
  1246. } else {
  1247. operand->mode = Addressing_Value;
  1248. }
  1249. operand->type = determine_swizzle_array_type(original_type, type_hint, arg_count);
  1250. break;
  1251. }
  1252. case BuiltinProc_complex: {
  1253. // complex :: proc(real, imag: float_type) -> complex_type
  1254. Operand x = *operand;
  1255. Operand y = {};
  1256. // NOTE(bill): Invalid will be the default till fixed
  1257. operand->type = t_invalid;
  1258. operand->mode = Addressing_Invalid;
  1259. check_expr(c, &y, ce->args[1]);
  1260. if (y.mode == Addressing_Invalid) {
  1261. return false;
  1262. }
  1263. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  1264. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  1265. if (x.mode == Addressing_Constant &&
  1266. y.mode == Addressing_Constant) {
  1267. x.value = exact_value_to_float(x.value);
  1268. y.value = exact_value_to_float(y.value);
  1269. if (is_type_numeric(x.type) && x.value.kind == ExactValue_Float) {
  1270. x.type = t_untyped_float;
  1271. }
  1272. if (is_type_numeric(y.type) && y.value.kind == ExactValue_Float) {
  1273. y.type = t_untyped_float;
  1274. }
  1275. }
  1276. if (!are_types_identical(x.type, y.type)) {
  1277. gbString tx = type_to_string(x.type);
  1278. gbString ty = type_to_string(y.type);
  1279. error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
  1280. gb_string_free(ty);
  1281. gb_string_free(tx);
  1282. return false;
  1283. }
  1284. if (!is_type_float(x.type)) {
  1285. gbString s = type_to_string(x.type);
  1286. error(call, "Arguments have type '%s', expected a floating point", s);
  1287. gb_string_free(s);
  1288. return false;
  1289. }
  1290. if (is_type_endian_specific(x.type)) {
  1291. gbString s = type_to_string(x.type);
  1292. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  1293. gb_string_free(s);
  1294. return false;
  1295. }
  1296. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  1297. f64 r = exact_value_to_float(x.value).value_float;
  1298. f64 i = exact_value_to_float(y.value).value_float;
  1299. operand->value = exact_value_complex(r, i);
  1300. operand->mode = Addressing_Constant;
  1301. } else {
  1302. operand->mode = Addressing_Value;
  1303. }
  1304. BasicKind kind = core_type(x.type)->Basic.kind;
  1305. switch (kind) {
  1306. case Basic_f16: operand->type = t_complex32; break;
  1307. case Basic_f32: operand->type = t_complex64; break;
  1308. case Basic_f64: operand->type = t_complex128; break;
  1309. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  1310. default: GB_PANIC("Invalid type"); break;
  1311. }
  1312. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  1313. operand->type = type_hint;
  1314. }
  1315. break;
  1316. }
  1317. case BuiltinProc_quaternion: {
  1318. // quaternion :: proc(real, imag, jmag, kmag: float_type) -> complex_type
  1319. Operand x = *operand;
  1320. Operand y = {};
  1321. Operand z = {};
  1322. Operand w = {};
  1323. // NOTE(bill): Invalid will be the default till fixed
  1324. operand->type = t_invalid;
  1325. operand->mode = Addressing_Invalid;
  1326. check_expr(c, &y, ce->args[1]);
  1327. if (y.mode == Addressing_Invalid) {
  1328. return false;
  1329. }
  1330. check_expr(c, &z, ce->args[2]);
  1331. if (y.mode == Addressing_Invalid) {
  1332. return false;
  1333. }
  1334. check_expr(c, &w, ce->args[3]);
  1335. if (y.mode == Addressing_Invalid) {
  1336. return false;
  1337. }
  1338. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  1339. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  1340. convert_to_typed(c, &z, x.type); if (z.mode == Addressing_Invalid) return false;
  1341. convert_to_typed(c, &w, x.type); if (w.mode == Addressing_Invalid) return false;
  1342. if (x.mode == Addressing_Constant &&
  1343. y.mode == Addressing_Constant &&
  1344. z.mode == Addressing_Constant &&
  1345. w.mode == Addressing_Constant) {
  1346. x.value = exact_value_to_float(x.value);
  1347. y.value = exact_value_to_float(y.value);
  1348. z.value = exact_value_to_float(z.value);
  1349. w.value = exact_value_to_float(w.value);
  1350. if (is_type_numeric(x.type) && x.value.kind == ExactValue_Float) {
  1351. x.type = t_untyped_float;
  1352. }
  1353. if (is_type_numeric(y.type) && y.value.kind == ExactValue_Float) {
  1354. y.type = t_untyped_float;
  1355. }
  1356. if (is_type_numeric(z.type) && z.value.kind == ExactValue_Float) {
  1357. z.type = t_untyped_float;
  1358. }
  1359. if (is_type_numeric(w.type) && w.value.kind == ExactValue_Float) {
  1360. w.type = t_untyped_float;
  1361. }
  1362. }
  1363. if (!(are_types_identical(x.type, y.type) && are_types_identical(x.type, z.type) && are_types_identical(x.type, w.type))) {
  1364. gbString tx = type_to_string(x.type);
  1365. gbString ty = type_to_string(y.type);
  1366. gbString tz = type_to_string(z.type);
  1367. gbString tw = type_to_string(w.type);
  1368. error(call, "Mismatched types to 'quaternion', '%s' vs '%s' vs '%s' vs '%s'", tx, ty, tz, tw);
  1369. gb_string_free(tw);
  1370. gb_string_free(tz);
  1371. gb_string_free(ty);
  1372. gb_string_free(tx);
  1373. return false;
  1374. }
  1375. if (!is_type_float(x.type)) {
  1376. gbString s = type_to_string(x.type);
  1377. error(call, "Arguments have type '%s', expected a floating point", s);
  1378. gb_string_free(s);
  1379. return false;
  1380. }
  1381. if (is_type_endian_specific(x.type)) {
  1382. gbString s = type_to_string(x.type);
  1383. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  1384. gb_string_free(s);
  1385. return false;
  1386. }
  1387. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant && z.mode == Addressing_Constant && w.mode == Addressing_Constant) {
  1388. f64 r = exact_value_to_float(x.value).value_float;
  1389. f64 i = exact_value_to_float(y.value).value_float;
  1390. f64 j = exact_value_to_float(z.value).value_float;
  1391. f64 k = exact_value_to_float(w.value).value_float;
  1392. operand->value = exact_value_quaternion(r, i, j, k);
  1393. operand->mode = Addressing_Constant;
  1394. } else {
  1395. operand->mode = Addressing_Value;
  1396. }
  1397. BasicKind kind = core_type(x.type)->Basic.kind;
  1398. switch (kind) {
  1399. case Basic_f16: operand->type = t_quaternion64; break;
  1400. case Basic_f32: operand->type = t_quaternion128; break;
  1401. case Basic_f64: operand->type = t_quaternion256; break;
  1402. case Basic_UntypedFloat: operand->type = t_untyped_quaternion; break;
  1403. default: GB_PANIC("Invalid type"); break;
  1404. }
  1405. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  1406. operand->type = type_hint;
  1407. }
  1408. break;
  1409. }
  1410. case BuiltinProc_real:
  1411. case BuiltinProc_imag: {
  1412. // real :: proc(x: type) -> float_type
  1413. // imag :: proc(x: type) -> float_type
  1414. Operand *x = operand;
  1415. if (is_type_untyped(x->type)) {
  1416. if (x->mode == Addressing_Constant) {
  1417. if (is_type_numeric(x->type)) {
  1418. x->type = t_untyped_complex;
  1419. }
  1420. } else if (is_type_quaternion(x->type)) {
  1421. convert_to_typed(c, x, t_quaternion256);
  1422. if (x->mode == Addressing_Invalid) {
  1423. return false;
  1424. }
  1425. } else{
  1426. convert_to_typed(c, x, t_complex128);
  1427. if (x->mode == Addressing_Invalid) {
  1428. return false;
  1429. }
  1430. }
  1431. }
  1432. if (!is_type_complex(x->type) && !is_type_quaternion(x->type)) {
  1433. gbString s = type_to_string(x->type);
  1434. error(call, "Argument has type '%s', expected a complex or quaternion type", s);
  1435. gb_string_free(s);
  1436. return false;
  1437. }
  1438. if (x->mode == Addressing_Constant) {
  1439. switch (id) {
  1440. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  1441. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  1442. }
  1443. } else {
  1444. x->mode = Addressing_Value;
  1445. }
  1446. BasicKind kind = core_type(x->type)->Basic.kind;
  1447. switch (kind) {
  1448. case Basic_complex32: x->type = t_f16; break;
  1449. case Basic_complex64: x->type = t_f32; break;
  1450. case Basic_complex128: x->type = t_f64; break;
  1451. case Basic_quaternion64: x->type = t_f16; break;
  1452. case Basic_quaternion128: x->type = t_f32; break;
  1453. case Basic_quaternion256: x->type = t_f64; break;
  1454. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  1455. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  1456. default: GB_PANIC("Invalid type"); break;
  1457. }
  1458. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  1459. operand->type = type_hint;
  1460. }
  1461. break;
  1462. }
  1463. case BuiltinProc_jmag:
  1464. case BuiltinProc_kmag: {
  1465. // jmag :: proc(x: type) -> float_type
  1466. // kmag :: proc(x: type) -> float_type
  1467. Operand *x = operand;
  1468. if (is_type_untyped(x->type)) {
  1469. if (x->mode == Addressing_Constant) {
  1470. if (is_type_numeric(x->type)) {
  1471. x->type = t_untyped_complex;
  1472. }
  1473. } else{
  1474. convert_to_typed(c, x, t_quaternion256);
  1475. if (x->mode == Addressing_Invalid) {
  1476. return false;
  1477. }
  1478. }
  1479. }
  1480. if (!is_type_quaternion(x->type)) {
  1481. gbString s = type_to_string(x->type);
  1482. error(call, "Argument has type '%s', expected a quaternion type", s);
  1483. gb_string_free(s);
  1484. return false;
  1485. }
  1486. if (x->mode == Addressing_Constant) {
  1487. switch (id) {
  1488. case BuiltinProc_jmag: x->value = exact_value_jmag(x->value); break;
  1489. case BuiltinProc_kmag: x->value = exact_value_kmag(x->value); break;
  1490. }
  1491. } else {
  1492. x->mode = Addressing_Value;
  1493. }
  1494. BasicKind kind = core_type(x->type)->Basic.kind;
  1495. switch (kind) {
  1496. case Basic_quaternion64: x->type = t_f16; break;
  1497. case Basic_quaternion128: x->type = t_f32; break;
  1498. case Basic_quaternion256: x->type = t_f64; break;
  1499. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  1500. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  1501. default: GB_PANIC("Invalid type"); break;
  1502. }
  1503. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  1504. operand->type = type_hint;
  1505. }
  1506. break;
  1507. }
  1508. case BuiltinProc_conj: {
  1509. // conj :: proc(x: type) -> type
  1510. Operand *x = operand;
  1511. Type *t = x->type;
  1512. Type *elem = core_array_type(t);
  1513. if (is_type_complex(t)) {
  1514. if (x->mode == Addressing_Constant) {
  1515. ExactValue v = exact_value_to_complex(x->value);
  1516. f64 r = v.value_complex->real;
  1517. f64 i = -v.value_complex->imag;
  1518. x->value = exact_value_complex(r, i);
  1519. x->mode = Addressing_Constant;
  1520. } else {
  1521. x->mode = Addressing_Value;
  1522. }
  1523. } else if (is_type_quaternion(t)) {
  1524. if (x->mode == Addressing_Constant) {
  1525. ExactValue v = exact_value_to_quaternion(x->value);
  1526. f64 r = +v.value_quaternion->real;
  1527. f64 i = -v.value_quaternion->imag;
  1528. f64 j = -v.value_quaternion->jmag;
  1529. f64 k = -v.value_quaternion->kmag;
  1530. x->value = exact_value_quaternion(r, i, j, k);
  1531. x->mode = Addressing_Constant;
  1532. } else {
  1533. x->mode = Addressing_Value;
  1534. }
  1535. } else if (is_type_array_like(t) && (is_type_complex(elem) || is_type_quaternion(elem))) {
  1536. x->mode = Addressing_Value;
  1537. } else if (is_type_matrix(t) && (is_type_complex(elem) || is_type_quaternion(elem))) {
  1538. x->mode = Addressing_Value;
  1539. }else {
  1540. gbString s = type_to_string(x->type);
  1541. error(call, "Expected a complex or quaternion, got '%s'", s);
  1542. gb_string_free(s);
  1543. return false;
  1544. }
  1545. break;
  1546. }
  1547. case BuiltinProc_expand_to_tuple: {
  1548. Type *type = base_type(operand->type);
  1549. if (!is_type_struct(type) && !is_type_array(type)) {
  1550. gbString type_str = type_to_string(operand->type);
  1551. error(call, "Expected a struct or array type, got '%s'", type_str);
  1552. gb_string_free(type_str);
  1553. return false;
  1554. }
  1555. gbAllocator a = permanent_allocator();
  1556. Type *tuple = alloc_type_tuple();
  1557. if (is_type_struct(type)) {
  1558. isize variable_count = type->Struct.fields.count;
  1559. slice_init(&tuple->Tuple.variables, a, variable_count);
  1560. // TODO(bill): Should I copy each of the entities or is this good enough?
  1561. gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
  1562. } else if (is_type_array(type)) {
  1563. isize variable_count = cast(isize)type->Array.count;
  1564. slice_init(&tuple->Tuple.variables, a, variable_count);
  1565. for (isize i = 0; i < variable_count; i++) {
  1566. tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
  1567. }
  1568. }
  1569. operand->type = tuple;
  1570. operand->mode = Addressing_Value;
  1571. if (tuple->Tuple.variables.count == 1) {
  1572. operand->type = tuple->Tuple.variables[0]->type;
  1573. }
  1574. break;
  1575. }
  1576. case BuiltinProc_min: {
  1577. // min :: proc($T: typeid) -> ordered
  1578. // min :: proc(a: ..ordered) -> ordered
  1579. check_multi_expr_or_type(c, operand, ce->args[0]);
  1580. Type *original_type = operand->type;
  1581. Type *type = base_type(operand->type);
  1582. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  1583. // Okay
  1584. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1585. gbString type_str = type_to_string(original_type);
  1586. error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
  1587. gb_string_free(type_str);
  1588. return false;
  1589. }
  1590. if (operand->mode == Addressing_Type) {
  1591. if (ce->args.count != 1) {
  1592. error(call, "If 'min' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  1593. return false;
  1594. }
  1595. if (is_type_boolean(type)) {
  1596. operand->mode = Addressing_Constant;
  1597. operand->type = original_type;
  1598. operand->value = exact_value_bool(false);
  1599. return true;
  1600. } else if (is_type_integer(type)) {
  1601. operand->mode = Addressing_Constant;
  1602. operand->type = original_type;
  1603. if (is_type_unsigned(type)) {
  1604. operand->value = exact_value_u64(0);
  1605. return true;
  1606. } else {
  1607. i64 sz = 8*type_size_of(type);
  1608. ExactValue a = exact_value_i64(1);
  1609. ExactValue b = exact_value_i64(sz-1);
  1610. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1611. v = exact_unary_operator_value(Token_Sub, v, cast(i32)sz, false);
  1612. operand->value = v;
  1613. return true;
  1614. }
  1615. } else if (is_type_float(type)) {
  1616. operand->mode = Addressing_Constant;
  1617. operand->type = original_type;
  1618. switch (type_size_of(type)) {
  1619. case 2:
  1620. operand->value = exact_value_float(-65504.0f);
  1621. break;
  1622. case 4:
  1623. operand->value = exact_value_float(-3.402823466e+38f);
  1624. break;
  1625. case 8:
  1626. operand->value = exact_value_float(-1.7976931348623158e+308);
  1627. break;
  1628. default:
  1629. GB_PANIC("Unhandled float type");
  1630. break;
  1631. }
  1632. return true;
  1633. } else if (is_type_enum(type)) {
  1634. operand->mode = Addressing_Constant;
  1635. operand->type = original_type;
  1636. operand->value = *type->Enum.min_value;
  1637. return true;
  1638. } else if (is_type_enumerated_array(type)) {
  1639. Type *bt = base_type(type);
  1640. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  1641. operand->mode = Addressing_Constant;
  1642. operand->type = bt->EnumeratedArray.index;
  1643. operand->value = *bt->EnumeratedArray.min_value;
  1644. return true;
  1645. }
  1646. gbString type_str = type_to_string(original_type);
  1647. error(call, "Invalid type for 'min', got %s", type_str);
  1648. gb_string_free(type_str);
  1649. return false;
  1650. }
  1651. bool all_constant = operand->mode == Addressing_Constant;
  1652. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  1653. defer (array_free(&operands));
  1654. array_add(&operands, *operand);
  1655. for (isize i = 1; i < ce->args.count; i++) {
  1656. Ast *other_arg = ce->args[i];
  1657. Operand b = {};
  1658. check_expr(c, &b, other_arg);
  1659. if (b.mode == Addressing_Invalid) {
  1660. return false;
  1661. }
  1662. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  1663. gbString type_str = type_to_string(b.type);
  1664. error(call,
  1665. "Expected a ordered numeric type to 'min', got '%s'",
  1666. type_str);
  1667. gb_string_free(type_str);
  1668. return false;
  1669. }
  1670. array_add(&operands, b);
  1671. if (all_constant) {
  1672. all_constant = b.mode == Addressing_Constant;
  1673. }
  1674. }
  1675. if (all_constant) {
  1676. ExactValue value = operands[0].value;
  1677. Type *type = operands[0].type;
  1678. for (isize i = 1; i < operands.count; i++) {
  1679. Operand y = operands[i];
  1680. if (compare_exact_values(Token_Lt, value, y.value)) {
  1681. // okay
  1682. } else {
  1683. value = y.value;
  1684. type = y.type;
  1685. }
  1686. }
  1687. operand->value = value;
  1688. operand->type = type;
  1689. } else {
  1690. operand->mode = Addressing_Value;
  1691. operand->type = original_type;
  1692. for_array(i, operands) {
  1693. Operand *a = &operands[i];
  1694. for_array(j, operands) {
  1695. if (i == j) {
  1696. continue;
  1697. }
  1698. Operand *b = &operands[j];
  1699. convert_to_typed(c, a, b->type);
  1700. if (a->mode == Addressing_Invalid) {
  1701. return false;
  1702. }
  1703. convert_to_typed(c, b, a->type);
  1704. if (b->mode == Addressing_Invalid) {
  1705. return false;
  1706. }
  1707. }
  1708. }
  1709. for (isize i = 0; i < operands.count-1; i++) {
  1710. Operand *a = &operands[i];
  1711. Operand *b = &operands[i+1];
  1712. if (!are_types_identical(a->type, b->type)) {
  1713. gbString type_a = type_to_string(a->type);
  1714. gbString type_b = type_to_string(b->type);
  1715. error(a->expr,
  1716. "Mismatched types to 'min', '%s' vs '%s'",
  1717. type_a, type_b);
  1718. gb_string_free(type_b);
  1719. gb_string_free(type_a);
  1720. return false;
  1721. }
  1722. }
  1723. operand->type = operands[0].type;
  1724. }
  1725. break;
  1726. }
  1727. case BuiltinProc_max: {
  1728. // max :: proc($T: typeid) -> ordered
  1729. // max :: proc(a: ..ordered) -> ordered
  1730. check_multi_expr_or_type(c, operand, ce->args[0]);
  1731. Type *original_type = operand->type;
  1732. Type *type = base_type(operand->type);
  1733. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  1734. // Okay
  1735. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1736. gbString type_str = type_to_string(original_type);
  1737. error(call, "Expected a ordered numeric type to 'max', got '%s'", type_str);
  1738. gb_string_free(type_str);
  1739. return false;
  1740. }
  1741. if (operand->mode == Addressing_Type) {
  1742. if (ce->args.count != 1) {
  1743. error(call, "If 'max' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  1744. return false;
  1745. }
  1746. if (is_type_boolean(type)) {
  1747. operand->mode = Addressing_Constant;
  1748. operand->type = original_type;
  1749. operand->value = exact_value_bool(true);
  1750. return true;
  1751. } else if (is_type_integer(type)) {
  1752. operand->mode = Addressing_Constant;
  1753. operand->type = original_type;
  1754. if (is_type_unsigned(type)) {
  1755. i64 sz = 8*type_size_of(type);
  1756. ExactValue a = exact_value_i64(1);
  1757. ExactValue b = exact_value_i64(sz);
  1758. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1759. v = exact_binary_operator_value(Token_Sub, v, a);
  1760. operand->value = v;
  1761. return true;
  1762. } else {
  1763. i64 sz = 8*type_size_of(type);
  1764. ExactValue a = exact_value_i64(1);
  1765. ExactValue b = exact_value_i64(sz-1);
  1766. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1767. v = exact_binary_operator_value(Token_Sub, v, a);
  1768. operand->value = v;
  1769. return true;
  1770. }
  1771. } else if (is_type_float(type)) {
  1772. operand->mode = Addressing_Constant;
  1773. operand->type = original_type;
  1774. switch (type_size_of(type)) {
  1775. case 2:
  1776. operand->value = exact_value_float(65504.0f);
  1777. break;
  1778. case 4:
  1779. operand->value = exact_value_float(3.402823466e+38f);
  1780. break;
  1781. case 8:
  1782. operand->value = exact_value_float(1.7976931348623158e+308);
  1783. break;
  1784. default:
  1785. GB_PANIC("Unhandled float type");
  1786. break;
  1787. }
  1788. return true;
  1789. } else if (is_type_enum(type)) {
  1790. operand->mode = Addressing_Constant;
  1791. operand->type = original_type;
  1792. operand->value = *type->Enum.max_value;
  1793. return true;
  1794. } else if (is_type_enumerated_array(type)) {
  1795. Type *bt = base_type(type);
  1796. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  1797. operand->mode = Addressing_Constant;
  1798. operand->type = bt->EnumeratedArray.index;
  1799. operand->value = *bt->EnumeratedArray.max_value;
  1800. return true;
  1801. }
  1802. gbString type_str = type_to_string(original_type);
  1803. error(call, "Invalid type for 'max', got %s", type_str);
  1804. gb_string_free(type_str);
  1805. return false;
  1806. }
  1807. bool all_constant = operand->mode == Addressing_Constant;
  1808. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  1809. defer (array_free(&operands));
  1810. array_add(&operands, *operand);
  1811. for (isize i = 1; i < ce->args.count; i++) {
  1812. Ast *arg = ce->args[i];
  1813. Operand b = {};
  1814. check_expr(c, &b, arg);
  1815. if (b.mode == Addressing_Invalid) {
  1816. return false;
  1817. }
  1818. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  1819. gbString type_str = type_to_string(b.type);
  1820. error(arg,
  1821. "Expected a ordered numeric type to 'max', got '%s'",
  1822. type_str);
  1823. gb_string_free(type_str);
  1824. return false;
  1825. }
  1826. array_add(&operands, b);
  1827. if (all_constant) {
  1828. all_constant = b.mode == Addressing_Constant;
  1829. }
  1830. }
  1831. if (all_constant) {
  1832. ExactValue value = operands[0].value;
  1833. Type *type = operands[0].type;
  1834. for (isize i = 1; i < operands.count; i++) {
  1835. Operand y = operands[i];
  1836. if (compare_exact_values(Token_Gt, value, y.value)) {
  1837. // okay
  1838. } else {
  1839. type = y.type;
  1840. value = y.value;
  1841. }
  1842. }
  1843. operand->value = value;
  1844. operand->type = type;
  1845. } else {
  1846. operand->mode = Addressing_Value;
  1847. operand->type = original_type;
  1848. for_array(i, operands) {
  1849. Operand *a = &operands[i];
  1850. for_array(j, operands) {
  1851. if (i == j) {
  1852. continue;
  1853. }
  1854. Operand *b = &operands[j];
  1855. convert_to_typed(c, a, b->type);
  1856. if (a->mode == Addressing_Invalid) {
  1857. return false;
  1858. }
  1859. convert_to_typed(c, b, a->type);
  1860. if (b->mode == Addressing_Invalid) {
  1861. return false;
  1862. }
  1863. }
  1864. }
  1865. for (isize i = 0; i < operands.count-1; i++) {
  1866. Operand *a = &operands[i];
  1867. Operand *b = &operands[i+1];
  1868. if (!are_types_identical(a->type, b->type)) {
  1869. gbString type_a = type_to_string(a->type);
  1870. gbString type_b = type_to_string(b->type);
  1871. error(a->expr,
  1872. "Mismatched types to 'max', '%s' vs '%s'",
  1873. type_a, type_b);
  1874. gb_string_free(type_b);
  1875. gb_string_free(type_a);
  1876. return false;
  1877. }
  1878. }
  1879. operand->type = operands[0].type;
  1880. }
  1881. break;
  1882. }
  1883. case BuiltinProc_abs: {
  1884. // abs :: proc(n: numeric) -> numeric
  1885. if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) {
  1886. gbString type_str = type_to_string(operand->type);
  1887. error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
  1888. gb_string_free(type_str);
  1889. return false;
  1890. }
  1891. if (operand->mode == Addressing_Constant) {
  1892. switch (operand->value.kind) {
  1893. case ExactValue_Integer:
  1894. mp_abs(&operand->value.value_integer, &operand->value.value_integer);
  1895. break;
  1896. case ExactValue_Float:
  1897. operand->value.value_float = gb_abs(operand->value.value_float);
  1898. break;
  1899. case ExactValue_Complex: {
  1900. f64 r = operand->value.value_complex->real;
  1901. f64 i = operand->value.value_complex->imag;
  1902. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  1903. break;
  1904. }
  1905. case ExactValue_Quaternion: {
  1906. f64 r = operand->value.value_quaternion->real;
  1907. f64 i = operand->value.value_quaternion->imag;
  1908. f64 j = operand->value.value_quaternion->jmag;
  1909. f64 k = operand->value.value_quaternion->kmag;
  1910. operand->value = exact_value_float(gb_sqrt(r*r + i*i + j*j + k*k));
  1911. break;
  1912. }
  1913. default:
  1914. GB_PANIC("Invalid numeric constant");
  1915. break;
  1916. }
  1917. } else {
  1918. operand->mode = Addressing_Value;
  1919. {
  1920. Type *bt = base_type(operand->type);
  1921. if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64");
  1922. if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128");
  1923. if (are_types_identical(bt, t_quaternion128)) add_package_dependency(c, "runtime", "abs_quaternion128");
  1924. if (are_types_identical(bt, t_quaternion256)) add_package_dependency(c, "runtime", "abs_quaternion256");
  1925. }
  1926. }
  1927. if (is_type_complex_or_quaternion(operand->type)) {
  1928. operand->type = base_complex_elem_type(operand->type);
  1929. }
  1930. GB_ASSERT(!is_type_complex_or_quaternion(operand->type));
  1931. break;
  1932. }
  1933. case BuiltinProc_clamp: {
  1934. // clamp :: proc(a, min, max: ordered) -> ordered
  1935. Type *type = operand->type;
  1936. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1937. gbString type_str = type_to_string(operand->type);
  1938. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1939. gb_string_free(type_str);
  1940. return false;
  1941. }
  1942. Ast *min_arg = ce->args[1];
  1943. Ast *max_arg = ce->args[2];
  1944. Operand x = *operand;
  1945. Operand y = {};
  1946. Operand z = {};
  1947. check_expr(c, &y, min_arg);
  1948. if (y.mode == Addressing_Invalid) {
  1949. return false;
  1950. }
  1951. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  1952. gbString type_str = type_to_string(y.type);
  1953. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1954. gb_string_free(type_str);
  1955. return false;
  1956. }
  1957. check_expr(c, &z, max_arg);
  1958. if (z.mode == Addressing_Invalid) {
  1959. return false;
  1960. }
  1961. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  1962. gbString type_str = type_to_string(z.type);
  1963. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1964. gb_string_free(type_str);
  1965. return false;
  1966. }
  1967. if (x.mode == Addressing_Constant &&
  1968. y.mode == Addressing_Constant &&
  1969. z.mode == Addressing_Constant) {
  1970. ExactValue a = x.value;
  1971. ExactValue b = y.value;
  1972. ExactValue c = z.value;
  1973. operand->mode = Addressing_Constant;
  1974. if (compare_exact_values(Token_Lt, a, b)) {
  1975. operand->value = b;
  1976. operand->type = y.type;
  1977. } else if (compare_exact_values(Token_Gt, a, c)) {
  1978. operand->value = c;
  1979. operand->type = z.type;
  1980. } else {
  1981. operand->value = a;
  1982. operand->type = x.type;
  1983. }
  1984. } else {
  1985. operand->mode = Addressing_Value;
  1986. operand->type = type;
  1987. Operand *ops[3] = {&x, &y, &z};
  1988. for (isize i = 0; i < 3; i++) {
  1989. Operand *a = ops[i];
  1990. for (isize j = 0; j < 3; j++) {
  1991. if (i == j) continue;
  1992. Operand *b = ops[j];
  1993. convert_to_typed(c, a, b->type);
  1994. if (a->mode == Addressing_Invalid) { return false; }
  1995. }
  1996. }
  1997. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  1998. gbString type_x = type_to_string(x.type);
  1999. gbString type_y = type_to_string(y.type);
  2000. gbString type_z = type_to_string(z.type);
  2001. error(call,
  2002. "Mismatched types to 'clamp', '%s', '%s', '%s'",
  2003. type_x, type_y, type_z);
  2004. gb_string_free(type_z);
  2005. gb_string_free(type_y);
  2006. gb_string_free(type_x);
  2007. return false;
  2008. }
  2009. operand->type = ops[0]->type;
  2010. }
  2011. break;
  2012. }
  2013. case BuiltinProc_soa_zip: {
  2014. auto types = array_make<Type *>(temporary_allocator(), 0, ce->args.count);
  2015. auto names = array_make<String>(temporary_allocator(), 0, ce->args.count);
  2016. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  2017. bool fail = false;
  2018. for_array(i, ce->args) {
  2019. Ast *arg = ce->args[i];
  2020. bool mix = false;
  2021. if (first_is_field_value) {
  2022. mix = arg->kind != Ast_FieldValue;
  2023. } else {
  2024. mix = arg->kind == Ast_FieldValue;
  2025. }
  2026. if (mix) {
  2027. error(arg, "Mixture of 'field = value' and value elements in the procedure call 'soa_zip' is not allowed");
  2028. fail = true;
  2029. break;
  2030. }
  2031. }
  2032. StringSet name_set = {};
  2033. string_set_init(&name_set, heap_allocator(), 2*ce->args.count);
  2034. for_array(i, ce->args) {
  2035. String name = {};
  2036. Ast *arg = ce->args[i];
  2037. if (arg->kind == Ast_FieldValue) {
  2038. Ast *ename = arg->FieldValue.field;
  2039. if (!fail && ename->kind != Ast_Ident) {
  2040. error(ename, "Expected an identifier for field argument");
  2041. } else if (ename->kind == Ast_Ident) {
  2042. name = ename->Ident.token.string;
  2043. }
  2044. arg = arg->FieldValue.value;
  2045. }
  2046. Operand op = {};
  2047. check_expr(c, &op, arg);
  2048. if (op.mode == Addressing_Invalid) {
  2049. return false;
  2050. }
  2051. Type *arg_type = base_type(op.type);
  2052. if (!is_type_slice(arg_type)) {
  2053. gbString s = type_to_string(op.type);
  2054. error(op.expr, "Indices to 'soa_zip' must be slices, got %s", s);
  2055. gb_string_free(s);
  2056. return false;
  2057. }
  2058. GB_ASSERT(arg_type->kind == Type_Slice);
  2059. if (name == "_") {
  2060. error(op.expr, "Field argument name '%.*s' is not allowed", LIT(name));
  2061. name = {};
  2062. }
  2063. if (name.len == 0) {
  2064. gbString field_name = gb_string_make(permanent_allocator(), "_");
  2065. field_name = gb_string_append_fmt(field_name, "%td", types.count);
  2066. name = make_string_c(field_name);
  2067. }
  2068. if (string_set_exists(&name_set, name)) {
  2069. error(op.expr, "Field argument name '%.*s' already exists", LIT(name));
  2070. } else {
  2071. array_add(&types, arg_type->Slice.elem);
  2072. array_add(&names, name);
  2073. string_set_add(&name_set, name);
  2074. }
  2075. }
  2076. Ast *dummy_node_struct = alloc_ast_node(nullptr, Ast_Invalid);
  2077. Ast *dummy_node_soa = alloc_ast_node(nullptr, Ast_Invalid);
  2078. Scope *s = create_scope(c->info, builtin_pkg->scope);
  2079. auto fields = array_make<Entity *>(permanent_allocator(), 0, types.count);
  2080. for_array(i, types) {
  2081. Type *type = types[i];
  2082. String name = names[i];
  2083. GB_ASSERT(name != "");
  2084. Entity *e = alloc_entity_field(s, make_token_ident(name), type, false, cast(i32)i, EntityState_Resolved);
  2085. array_add(&fields, e);
  2086. scope_insert(s, e);
  2087. }
  2088. Type *elem = nullptr;
  2089. if (type_hint != nullptr && is_type_struct(type_hint)) {
  2090. Type *soa_type = base_type(type_hint);
  2091. if (soa_type->Struct.soa_kind != StructSoa_Slice) {
  2092. goto soa_zip_end;
  2093. }
  2094. Type *soa_elem_type = soa_type->Struct.soa_elem;
  2095. Type *et = base_type(soa_elem_type);
  2096. if (et->kind != Type_Struct) {
  2097. goto soa_zip_end;
  2098. }
  2099. if (et->Struct.fields.count != fields.count) {
  2100. goto soa_zip_end;
  2101. }
  2102. if (!fail && first_is_field_value) {
  2103. for_array(i, names) {
  2104. Selection sel = lookup_field(et, names[i], false);
  2105. if (sel.entity == nullptr) {
  2106. goto soa_zip_end;
  2107. }
  2108. if (sel.index.count != 1) {
  2109. goto soa_zip_end;
  2110. }
  2111. if (!are_types_identical(sel.entity->type, types[i])) {
  2112. goto soa_zip_end;
  2113. }
  2114. }
  2115. } else {
  2116. for_array(i, et->Struct.fields) {
  2117. if (!are_types_identical(et->Struct.fields[i]->type, types[i])) {
  2118. goto soa_zip_end;
  2119. }
  2120. }
  2121. }
  2122. elem = soa_elem_type;
  2123. }
  2124. soa_zip_end:;
  2125. if (elem == nullptr) {
  2126. elem = alloc_type_struct();
  2127. elem->Struct.scope = s;
  2128. elem->Struct.fields = slice_from_array(fields);
  2129. elem->Struct.tags = gb_alloc_array(permanent_allocator(), String, fields.count);
  2130. elem->Struct.node = dummy_node_struct;
  2131. type_set_offsets(elem);
  2132. }
  2133. Type *soa_type = make_soa_struct_slice(c, dummy_node_soa, nullptr, elem);
  2134. type_set_offsets(soa_type);
  2135. operand->type = soa_type;
  2136. operand->mode = Addressing_Value;
  2137. break;
  2138. }
  2139. case BuiltinProc_soa_unzip: {
  2140. Operand x = {};
  2141. check_expr(c, &x, ce->args[0]);
  2142. if (x.mode == Addressing_Invalid) {
  2143. return false;
  2144. }
  2145. if (!is_operand_value(x)) {
  2146. error(call, "'%.*s' expects an #soa slice", LIT(builtin_name));
  2147. return false;
  2148. }
  2149. Type *t = base_type(x.type);
  2150. if (!is_type_soa_struct(t) || t->Struct.soa_kind != StructSoa_Slice) {
  2151. gbString s = type_to_string(x.type);
  2152. error(call, "'%.*s' expects an #soa slice, got %s", LIT(builtin_name), s);
  2153. gb_string_free(s);
  2154. return false;
  2155. }
  2156. auto types = slice_make<Type *>(permanent_allocator(), t->Struct.fields.count-1);
  2157. for_array(i, types) {
  2158. Entity *f = t->Struct.fields[i];
  2159. GB_ASSERT(f->type->kind == Type_Pointer);
  2160. types[i] = alloc_type_slice(f->type->Pointer.elem);
  2161. }
  2162. operand->type = alloc_type_tuple_from_field_types(types.data, types.count, false, false);
  2163. operand->mode = Addressing_Value;
  2164. break;
  2165. }
  2166. case BuiltinProc_transpose: {
  2167. Operand x = {};
  2168. check_expr(c, &x, ce->args[0]);
  2169. if (x.mode == Addressing_Invalid) {
  2170. return false;
  2171. }
  2172. if (!is_operand_value(x)) {
  2173. error(call, "'%.*s' expects a matrix or array", LIT(builtin_name));
  2174. return false;
  2175. }
  2176. Type *t = base_type(x.type);
  2177. if (!is_type_matrix(t) && !is_type_array(t)) {
  2178. gbString s = type_to_string(x.type);
  2179. error(call, "'%.*s' expects a matrix or array, got %s", LIT(builtin_name), s);
  2180. gb_string_free(s);
  2181. return false;
  2182. }
  2183. operand->mode = Addressing_Value;
  2184. if (t->kind == Type_Array) {
  2185. i32 rank = type_math_rank(t);
  2186. // Do nothing
  2187. operand->type = x.type;
  2188. if (rank > 2) {
  2189. gbString s = type_to_string(x.type);
  2190. error(call, "'%.*s' expects a matrix or array with a rank of 2, got %s of rank %d", LIT(builtin_name), s, rank);
  2191. gb_string_free(s);
  2192. return false;
  2193. } else if (rank == 2) {
  2194. Type *inner = base_type(t->Array.elem);
  2195. GB_ASSERT(inner->kind == Type_Array);
  2196. Type *elem = inner->Array.elem;
  2197. Type *array_inner = alloc_type_array(elem, t->Array.count);
  2198. Type *array_outer = alloc_type_array(array_inner, inner->Array.count);
  2199. operand->type = array_outer;
  2200. i64 elements = t->Array.count*inner->Array.count;
  2201. i64 size = type_size_of(operand->type);
  2202. if (!is_type_valid_for_matrix_elems(elem)) {
  2203. gbString s = type_to_string(x.type);
  2204. error(call, "'%.*s' expects a matrix or array with a base element type of an integer, float, or complex number, got %s", LIT(builtin_name), s);
  2205. gb_string_free(s);
  2206. } else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
  2207. gbString s = type_to_string(x.type);
  2208. error(call, "'%.*s' expects a matrix or array with a maximum of %d elements, got %s with %lld elements", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s, elements);
  2209. gb_string_free(s);
  2210. } else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
  2211. gbString s = type_to_string(x.type);
  2212. error(call, "'%.*s' expects a matrix or array with non-zero elements, got %s", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s);
  2213. gb_string_free(s);
  2214. } else if (size > MATRIX_ELEMENT_MAX_SIZE) {
  2215. gbString s = type_to_string(x.type);
  2216. error(call, "Too large of a type for '%.*s', got %s of size %lld, maximum size %d", LIT(builtin_name), s, cast(long long)size, MATRIX_ELEMENT_MAX_SIZE);
  2217. gb_string_free(s);
  2218. }
  2219. }
  2220. } else {
  2221. GB_ASSERT(t->kind == Type_Matrix);
  2222. operand->type = alloc_type_matrix(t->Matrix.elem, t->Matrix.column_count, t->Matrix.row_count);
  2223. }
  2224. operand->type = check_matrix_type_hint(operand->type, type_hint);
  2225. break;
  2226. }
  2227. case BuiltinProc_outer_product: {
  2228. Operand x = {};
  2229. Operand y = {};
  2230. check_expr(c, &x, ce->args[0]);
  2231. if (x.mode == Addressing_Invalid) {
  2232. return false;
  2233. }
  2234. check_expr(c, &y, ce->args[1]);
  2235. if (y.mode == Addressing_Invalid) {
  2236. return false;
  2237. }
  2238. if (!is_operand_value(x) || !is_operand_value(y)) {
  2239. error(call, "'%.*s' expects only arrays", LIT(builtin_name));
  2240. return false;
  2241. }
  2242. if (!is_type_array(x.type) && !is_type_array(y.type)) {
  2243. gbString s1 = type_to_string(x.type);
  2244. gbString s2 = type_to_string(y.type);
  2245. error(call, "'%.*s' expects only arrays, got %s and %s", LIT(builtin_name), s1, s2);
  2246. gb_string_free(s2);
  2247. gb_string_free(s1);
  2248. return false;
  2249. }
  2250. Type *xt = base_type(x.type);
  2251. Type *yt = base_type(y.type);
  2252. GB_ASSERT(xt->kind == Type_Array);
  2253. GB_ASSERT(yt->kind == Type_Array);
  2254. if (!are_types_identical(xt->Array.elem, yt->Array.elem)) {
  2255. gbString s1 = type_to_string(xt->Array.elem);
  2256. gbString s2 = type_to_string(yt->Array.elem);
  2257. error(call, "'%.*s' mismatched element types, got %s vs %s", LIT(builtin_name), s1, s2);
  2258. gb_string_free(s2);
  2259. gb_string_free(s1);
  2260. return false;
  2261. }
  2262. Type *elem = xt->Array.elem;
  2263. if (!is_type_valid_for_matrix_elems(elem)) {
  2264. gbString s = type_to_string(elem);
  2265. error(call, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
  2266. gb_string_free(s);
  2267. }
  2268. if (xt->Array.count == 0 || yt->Array.count == 0) {
  2269. gbString s1 = type_to_string(x.type);
  2270. gbString s2 = type_to_string(y.type);
  2271. error(call, "'%.*s' expects only arrays of non-zero length, got %s and %s", LIT(builtin_name), s1, s2);
  2272. gb_string_free(s2);
  2273. gb_string_free(s1);
  2274. return false;
  2275. }
  2276. i64 max_count = xt->Array.count*yt->Array.count;
  2277. if (max_count > MATRIX_ELEMENT_COUNT_MAX) {
  2278. error(call, "Product of the array lengths exceed the maximum matrix element count, got %d, expected a maximum of %d", cast(int)max_count, MATRIX_ELEMENT_COUNT_MAX);
  2279. return false;
  2280. }
  2281. operand->mode = Addressing_Value;
  2282. operand->type = alloc_type_matrix(elem, xt->Array.count, yt->Array.count);
  2283. operand->type = check_matrix_type_hint(operand->type, type_hint);
  2284. break;
  2285. }
  2286. case BuiltinProc_hadamard_product: {
  2287. Operand x = {};
  2288. Operand y = {};
  2289. check_expr(c, &x, ce->args[0]);
  2290. if (x.mode == Addressing_Invalid) {
  2291. return false;
  2292. }
  2293. check_expr(c, &y, ce->args[1]);
  2294. if (y.mode == Addressing_Invalid) {
  2295. return false;
  2296. }
  2297. if (!is_operand_value(x) || !is_operand_value(y)) {
  2298. error(call, "'%.*s' expects a matrix or array types", LIT(builtin_name));
  2299. return false;
  2300. }
  2301. if (!is_type_matrix(x.type) && !is_type_array(y.type)) {
  2302. gbString s1 = type_to_string(x.type);
  2303. gbString s2 = type_to_string(y.type);
  2304. error(call, "'%.*s' expects matrix or array values, got %s and %s", LIT(builtin_name), s1, s2);
  2305. gb_string_free(s2);
  2306. gb_string_free(s1);
  2307. return false;
  2308. }
  2309. if (!are_types_identical(x.type, y.type)) {
  2310. gbString s1 = type_to_string(x.type);
  2311. gbString s2 = type_to_string(y.type);
  2312. error(call, "'%.*s' values of the same type, got %s and %s", LIT(builtin_name), s1, s2);
  2313. gb_string_free(s2);
  2314. gb_string_free(s1);
  2315. return false;
  2316. }
  2317. Type *elem = core_array_type(x.type);
  2318. if (!is_type_valid_for_matrix_elems(elem)) {
  2319. gbString s = type_to_string(elem);
  2320. error(call, "'%.*s' expects elements to be types are limited to integers, floats, and complex, got %s", LIT(builtin_name), s);
  2321. gb_string_free(s);
  2322. }
  2323. operand->mode = Addressing_Value;
  2324. operand->type = x.type;
  2325. operand->type = check_matrix_type_hint(operand->type, type_hint);
  2326. break;
  2327. }
  2328. case BuiltinProc_matrix_flatten: {
  2329. Operand x = {};
  2330. check_expr(c, &x, ce->args[0]);
  2331. if (x.mode == Addressing_Invalid) {
  2332. return false;
  2333. }
  2334. if (!is_operand_value(x)) {
  2335. error(call, "'%.*s' expects a matrix or array", LIT(builtin_name));
  2336. return false;
  2337. }
  2338. Type *t = base_type(x.type);
  2339. if (!is_type_matrix(t) && !is_type_array(t)) {
  2340. gbString s = type_to_string(x.type);
  2341. error(call, "'%.*s' expects a matrix or array, got %s", LIT(builtin_name), s);
  2342. gb_string_free(s);
  2343. return false;
  2344. }
  2345. operand->mode = Addressing_Value;
  2346. if (is_type_array(t)) {
  2347. // Do nothing
  2348. operand->type = x.type;
  2349. } else {
  2350. GB_ASSERT(t->kind == Type_Matrix);
  2351. operand->type = alloc_type_array(t->Matrix.elem, t->Matrix.row_count*t->Matrix.column_count);
  2352. }
  2353. operand->type = check_matrix_type_hint(operand->type, type_hint);
  2354. break;
  2355. }
  2356. case BuiltinProc_simd_vector: {
  2357. Operand x = {};
  2358. Operand y = {};
  2359. x = *operand;
  2360. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  2361. error(call, "Expected a constant integer for 'intrinsics.simd_vector'");
  2362. operand->mode = Addressing_Type;
  2363. operand->type = t_invalid;
  2364. return false;
  2365. }
  2366. if (big_int_is_neg(&x.value.value_integer)) {
  2367. error(call, "Negative vector element length");
  2368. operand->mode = Addressing_Type;
  2369. operand->type = t_invalid;
  2370. return false;
  2371. }
  2372. i64 count = big_int_to_i64(&x.value.value_integer);
  2373. check_expr_or_type(c, &y, ce->args[1]);
  2374. if (y.mode != Addressing_Type) {
  2375. error(call, "Expected a type 'intrinsics.simd_vector'");
  2376. operand->mode = Addressing_Type;
  2377. operand->type = t_invalid;
  2378. return false;
  2379. }
  2380. Type *elem = y.type;
  2381. if (!is_type_valid_vector_elem(elem)) {
  2382. gbString str = type_to_string(elem);
  2383. error(call, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  2384. gb_string_free(str);
  2385. operand->mode = Addressing_Type;
  2386. operand->type = t_invalid;
  2387. return false;
  2388. }
  2389. operand->mode = Addressing_Type;
  2390. operand->type = alloc_type_simd_vector(count, elem);
  2391. break;
  2392. }
  2393. case BuiltinProc_is_package_imported: {
  2394. bool value = false;
  2395. if (!is_type_string(operand->type) && (operand->mode != Addressing_Constant)) {
  2396. error(ce->args[0], "Expected a constant string for '%.*s'", LIT(builtin_name));
  2397. } else if (operand->value.kind == ExactValue_String) {
  2398. String pkg_name = operand->value.value_string;
  2399. // TODO(bill): probably should have this be a `StringMap` eventually
  2400. for_array(i, c->info->packages.entries) {
  2401. AstPackage *pkg = c->info->packages.entries[i].value;
  2402. if (pkg->name == pkg_name) {
  2403. value = true;
  2404. break;
  2405. }
  2406. }
  2407. }
  2408. operand->mode = Addressing_Constant;
  2409. operand->type = t_untyped_bool;
  2410. operand->value = exact_value_bool(value);
  2411. break;
  2412. }
  2413. case BuiltinProc_soa_struct: {
  2414. Operand x = {};
  2415. Operand y = {};
  2416. x = *operand;
  2417. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  2418. error(call, "Expected a constant integer for 'intrinsics.soa_struct'");
  2419. operand->mode = Addressing_Type;
  2420. operand->type = t_invalid;
  2421. return false;
  2422. }
  2423. if (big_int_is_neg(&x.value.value_integer)) {
  2424. error(call, "Negative array element length");
  2425. operand->mode = Addressing_Type;
  2426. operand->type = t_invalid;
  2427. return false;
  2428. }
  2429. i64 count = big_int_to_i64(&x.value.value_integer);
  2430. check_expr_or_type(c, &y, ce->args[1]);
  2431. if (y.mode != Addressing_Type) {
  2432. error(call, "Expected a type 'intrinsics.soa_struct'");
  2433. operand->mode = Addressing_Type;
  2434. operand->type = t_invalid;
  2435. return false;
  2436. }
  2437. Type *elem = y.type;
  2438. Type *bt_elem = base_type(elem);
  2439. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2440. gbString str = type_to_string(elem);
  2441. error(call, "Invalid type for 'intrinsics.soa_struct', expected a struct or array of length 4 or below, got '%s'", str);
  2442. gb_string_free(str);
  2443. operand->mode = Addressing_Type;
  2444. operand->type = t_invalid;
  2445. return false;
  2446. }
  2447. operand->mode = Addressing_Type;
  2448. Type *soa_struct = nullptr;
  2449. Scope *scope = nullptr;
  2450. if (is_type_array(elem)) {
  2451. Type *old_array = base_type(elem);
  2452. soa_struct = alloc_type_struct();
  2453. soa_struct->Struct.fields = slice_make<Entity *>(heap_allocator(), cast(isize)old_array->Array.count);
  2454. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, cast(isize)old_array->Array.count);
  2455. soa_struct->Struct.node = operand->expr;
  2456. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2457. soa_struct->Struct.soa_elem = elem;
  2458. soa_struct->Struct.soa_count = cast(i32)count;
  2459. scope = create_scope(c->info, c->scope);
  2460. soa_struct->Struct.scope = scope;
  2461. String params_xyzw[4] = {
  2462. str_lit("x"),
  2463. str_lit("y"),
  2464. str_lit("z"),
  2465. str_lit("w")
  2466. };
  2467. for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
  2468. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  2469. Token token = {};
  2470. token.string = params_xyzw[i];
  2471. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2472. soa_struct->Struct.fields[i] = new_field;
  2473. add_entity(c, scope, nullptr, new_field);
  2474. add_entity_use(c, nullptr, new_field);
  2475. }
  2476. } else {
  2477. GB_ASSERT(is_type_struct(elem));
  2478. Type *old_struct = base_type(elem);
  2479. soa_struct = alloc_type_struct();
  2480. soa_struct->Struct.fields = slice_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  2481. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, old_struct->Struct.fields.count);
  2482. soa_struct->Struct.node = operand->expr;
  2483. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2484. soa_struct->Struct.soa_elem = elem;
  2485. if (count > I32_MAX) {
  2486. count = I32_MAX;
  2487. error(call, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  2488. }
  2489. soa_struct->Struct.soa_count = cast(i32)count;
  2490. scope = create_scope(c->info, old_struct->Struct.scope->parent);
  2491. soa_struct->Struct.scope = scope;
  2492. for_array(i, old_struct->Struct.fields) {
  2493. Entity *old_field = old_struct->Struct.fields[i];
  2494. if (old_field->kind == Entity_Variable) {
  2495. Type *array_type = alloc_type_array(old_field->type, count);
  2496. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_index);
  2497. soa_struct->Struct.fields[i] = new_field;
  2498. add_entity(c, scope, nullptr, new_field);
  2499. } else {
  2500. soa_struct->Struct.fields[i] = old_field;
  2501. }
  2502. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2503. }
  2504. }
  2505. Token token = {};
  2506. token.string = str_lit("Base_Type");
  2507. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2508. add_entity(c, scope, nullptr, base_type_entity);
  2509. add_type_info_type(c, soa_struct);
  2510. operand->type = soa_struct;
  2511. break;
  2512. }
  2513. case BuiltinProc_alloca:
  2514. {
  2515. Operand sz = {};
  2516. Operand al = {};
  2517. check_expr(c, &sz, ce->args[0]);
  2518. if (sz.mode == Addressing_Invalid) {
  2519. return false;
  2520. }
  2521. check_expr(c, &al, ce->args[1]);
  2522. if (al.mode == Addressing_Invalid) {
  2523. return false;
  2524. }
  2525. convert_to_typed(c, &sz, t_int); if (sz.mode == Addressing_Invalid) return false;
  2526. convert_to_typed(c, &al, t_int); if (al.mode == Addressing_Invalid) return false;
  2527. if (!is_type_integer(sz.type) || !is_type_integer(al.type)) {
  2528. error(operand->expr, "Both parameters to '%.*s' must integers", LIT(builtin_name));
  2529. return false;
  2530. }
  2531. if (sz.mode == Addressing_Constant) {
  2532. i64 i_sz = exact_value_to_i64(sz.value);
  2533. if (i_sz < 0) {
  2534. error(sz.expr, "Size parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_sz);
  2535. return false;
  2536. }
  2537. }
  2538. if (al.mode == Addressing_Constant) {
  2539. i64 i_al = exact_value_to_i64(al.value);
  2540. if (i_al < 0) {
  2541. error(al.expr, "Alignment parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_al);
  2542. return false;
  2543. }
  2544. if (i_al > 1<<29) {
  2545. error(al.expr, "Alignment parameter to '%.*s' must not exceed '1<<29', got %lld", LIT(builtin_name), cast(long long)i_al);
  2546. return false;
  2547. }
  2548. if (!gb_is_power_of_two(cast(isize)i_al) && i_al != 0) {
  2549. error(al.expr, "Alignment parameter to '%.*s' must be a power of 2 or 0, got %lld", LIT(builtin_name), cast(long long)i_al);
  2550. return false;
  2551. }
  2552. } else {
  2553. error(al.expr, "Alignment parameter to '%.*s' must be constant", LIT(builtin_name));
  2554. }
  2555. operand->type = alloc_type_multi_pointer(t_u8);
  2556. operand->mode = Addressing_Value;
  2557. break;
  2558. }
  2559. case BuiltinProc_cpu_relax:
  2560. operand->mode = Addressing_NoValue;
  2561. break;
  2562. case BuiltinProc_trap:
  2563. case BuiltinProc_debug_trap:
  2564. operand->mode = Addressing_NoValue;
  2565. break;
  2566. case BuiltinProc_read_cycle_counter:
  2567. operand->mode = Addressing_Value;
  2568. operand->type = t_i64;
  2569. break;
  2570. case BuiltinProc_count_ones:
  2571. case BuiltinProc_count_zeros:
  2572. case BuiltinProc_count_trailing_zeros:
  2573. case BuiltinProc_count_leading_zeros:
  2574. case BuiltinProc_reverse_bits:
  2575. {
  2576. Operand x = {};
  2577. check_expr(c, &x, ce->args[0]);
  2578. if (x.mode == Addressing_Invalid) {
  2579. return false;
  2580. }
  2581. if (!is_type_integer_like(x.type)) {
  2582. gbString xts = type_to_string(x.type);
  2583. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_name), xts);
  2584. gb_string_free(xts);
  2585. } else if (x.type == t_llvm_bool) {
  2586. gbString xts = type_to_string(x.type);
  2587. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_name), xts);
  2588. gb_string_free(xts);
  2589. }
  2590. operand->mode = Addressing_Value;
  2591. operand->type = default_type(x.type);
  2592. }
  2593. break;
  2594. case BuiltinProc_byte_swap:
  2595. {
  2596. Operand x = {};
  2597. check_expr(c, &x, ce->args[0]);
  2598. if (x.mode == Addressing_Invalid) {
  2599. return false;
  2600. }
  2601. if (!is_type_integer_like(x.type) && !is_type_float(x.type)) {
  2602. gbString xts = type_to_string(x.type);
  2603. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set) or float, got %s", LIT(builtin_name), xts);
  2604. gb_string_free(xts);
  2605. } else if (x.type == t_llvm_bool) {
  2606. gbString xts = type_to_string(x.type);
  2607. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_name), xts);
  2608. gb_string_free(xts);
  2609. }
  2610. i64 sz = type_size_of(x.type);
  2611. if (sz < 2) {
  2612. gbString xts = type_to_string(x.type);
  2613. error(x.expr, "Type passed to '%.*s' must be at least 2 bytes, got %s with size of %lld", LIT(builtin_name), xts, sz);
  2614. gb_string_free(xts);
  2615. }
  2616. operand->mode = Addressing_Value;
  2617. operand->type = default_type(x.type);
  2618. }
  2619. break;
  2620. case BuiltinProc_overflow_add:
  2621. case BuiltinProc_overflow_sub:
  2622. case BuiltinProc_overflow_mul:
  2623. {
  2624. Operand x = {};
  2625. Operand y = {};
  2626. check_expr(c, &x, ce->args[0]);
  2627. check_expr(c, &y, ce->args[1]);
  2628. if (x.mode == Addressing_Invalid) {
  2629. return false;
  2630. }
  2631. if (y.mode == Addressing_Invalid) {
  2632. return false;
  2633. }
  2634. convert_to_typed(c, &y, x.type);
  2635. convert_to_typed(c, &x, y.type);
  2636. if (is_type_untyped(x.type)) {
  2637. gbString xts = type_to_string(x.type);
  2638. error(x.expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_name), xts);
  2639. gb_string_free(xts);
  2640. return false;
  2641. }
  2642. if (!is_type_integer(x.type)) {
  2643. gbString xts = type_to_string(x.type);
  2644. error(x.expr, "Expected an integer for '%.*s', got %s", LIT(builtin_name), xts);
  2645. gb_string_free(xts);
  2646. return false;
  2647. }
  2648. Type *ct = core_type(x.type);
  2649. if (is_type_different_to_arch_endianness(ct)) {
  2650. GB_ASSERT(ct->kind == Type_Basic);
  2651. if (ct->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) {
  2652. gbString xts = type_to_string(x.type);
  2653. error(x.expr, "Expected an integer which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_name), xts);
  2654. gb_string_free(xts);
  2655. return false;
  2656. }
  2657. }
  2658. operand->mode = Addressing_OptionalOk;
  2659. operand->type = default_type(x.type);
  2660. }
  2661. break;
  2662. case BuiltinProc_sqrt:
  2663. {
  2664. Operand x = {};
  2665. check_expr(c, &x, ce->args[0]);
  2666. if (x.mode == Addressing_Invalid) {
  2667. return false;
  2668. }
  2669. if (!is_type_float(x.type)) {
  2670. gbString xts = type_to_string(x.type);
  2671. error(x.expr, "Expected a floating point value for '%.*s', got %s", LIT(builtin_name), xts);
  2672. gb_string_free(xts);
  2673. return false;
  2674. }
  2675. if (x.mode == Addressing_Constant) {
  2676. f64 v = exact_value_to_f64(x.value);
  2677. operand->mode = Addressing_Constant;
  2678. operand->type = x.type;
  2679. operand->value = exact_value_float(gb_sqrt(v));
  2680. break;
  2681. }
  2682. operand->mode = Addressing_Value;
  2683. operand->type = default_type(x.type);
  2684. }
  2685. break;
  2686. case BuiltinProc_mem_copy:
  2687. case BuiltinProc_mem_copy_non_overlapping:
  2688. {
  2689. operand->mode = Addressing_NoValue;
  2690. operand->type = t_invalid;
  2691. Operand dst = {};
  2692. Operand src = {};
  2693. Operand len = {};
  2694. check_expr(c, &dst, ce->args[0]);
  2695. check_expr(c, &src, ce->args[1]);
  2696. check_expr(c, &len, ce->args[2]);
  2697. if (dst.mode == Addressing_Invalid) {
  2698. return false;
  2699. }
  2700. if (src.mode == Addressing_Invalid) {
  2701. return false;
  2702. }
  2703. if (len.mode == Addressing_Invalid) {
  2704. return false;
  2705. }
  2706. if (!is_type_pointer(dst.type) && !is_type_multi_pointer(dst.type)) {
  2707. gbString str = type_to_string(dst.type);
  2708. error(dst.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2709. gb_string_free(str);
  2710. return false;
  2711. }
  2712. if (!is_type_pointer(src.type) && !is_type_multi_pointer(src.type)) {
  2713. gbString str = type_to_string(src.type);
  2714. error(src.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2715. gb_string_free(str);
  2716. return false;
  2717. }
  2718. if (!is_type_integer(len.type)) {
  2719. gbString str = type_to_string(len.type);
  2720. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  2721. gb_string_free(str);
  2722. return false;
  2723. }
  2724. if (len.mode == Addressing_Constant) {
  2725. i64 n = exact_value_to_i64(len.value);
  2726. if (n < 0) {
  2727. gbString str = expr_to_string(len.expr);
  2728. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  2729. gb_string_free(str);
  2730. }
  2731. }
  2732. }
  2733. break;
  2734. case BuiltinProc_mem_zero:
  2735. case BuiltinProc_mem_zero_volatile:
  2736. {
  2737. operand->mode = Addressing_NoValue;
  2738. operand->type = t_invalid;
  2739. Operand ptr = {};
  2740. Operand len = {};
  2741. check_expr(c, &ptr, ce->args[0]);
  2742. check_expr(c, &len, ce->args[1]);
  2743. if (ptr.mode == Addressing_Invalid) {
  2744. return false;
  2745. }
  2746. if (len.mode == Addressing_Invalid) {
  2747. return false;
  2748. }
  2749. if (!is_type_pointer(ptr.type) && !is_type_multi_pointer(ptr.type)) {
  2750. gbString str = type_to_string(ptr.type);
  2751. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2752. gb_string_free(str);
  2753. return false;
  2754. }
  2755. if (!is_type_integer(len.type)) {
  2756. gbString str = type_to_string(len.type);
  2757. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  2758. gb_string_free(str);
  2759. return false;
  2760. }
  2761. if (len.mode == Addressing_Constant) {
  2762. i64 n = exact_value_to_i64(len.value);
  2763. if (n < 0) {
  2764. gbString str = expr_to_string(len.expr);
  2765. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  2766. gb_string_free(str);
  2767. }
  2768. }
  2769. }
  2770. break;
  2771. case BuiltinProc_ptr_offset:
  2772. {
  2773. Operand ptr = {};
  2774. Operand offset = {};
  2775. check_expr(c, &ptr, ce->args[0]);
  2776. check_expr(c, &offset, ce->args[1]);
  2777. if (ptr.mode == Addressing_Invalid) {
  2778. operand->mode = Addressing_Invalid;
  2779. operand->type = t_invalid;
  2780. return false;
  2781. }
  2782. if (offset.mode == Addressing_Invalid) {
  2783. operand->mode = Addressing_Invalid;
  2784. operand->type = t_invalid;
  2785. return false;
  2786. }
  2787. operand->mode = Addressing_Value;
  2788. operand->type = ptr.type;
  2789. if (!is_type_pointer(ptr.type) && !is_type_multi_pointer(ptr.type)) {
  2790. gbString str = type_to_string(ptr.type);
  2791. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2792. gb_string_free(str);
  2793. return false;
  2794. }
  2795. if (are_types_identical(core_type(ptr.type), t_rawptr)) {
  2796. gbString str = type_to_string(ptr.type);
  2797. error(ptr.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2798. gb_string_free(str);
  2799. return false;
  2800. }
  2801. if (!is_type_integer(offset.type)) {
  2802. gbString str = type_to_string(offset.type);
  2803. error(offset.expr, "Expected an integer value for the offset parameter for '%.*s', got %s", LIT(builtin_name), str);
  2804. gb_string_free(str);
  2805. return false;
  2806. }
  2807. }
  2808. break;
  2809. case BuiltinProc_ptr_sub:
  2810. {
  2811. operand->mode = Addressing_NoValue;
  2812. operand->type = t_invalid;
  2813. Operand ptr0 = {};
  2814. Operand ptr1 = {};
  2815. check_expr(c, &ptr0, ce->args[0]);
  2816. check_expr(c, &ptr1, ce->args[1]);
  2817. if (ptr0.mode == Addressing_Invalid) {
  2818. operand->mode = Addressing_Invalid;
  2819. operand->type = t_invalid;
  2820. return false;
  2821. }
  2822. if (ptr1.mode == Addressing_Invalid) {
  2823. operand->mode = Addressing_Invalid;
  2824. operand->type = t_invalid;
  2825. return false;
  2826. }
  2827. operand->mode = Addressing_Value;
  2828. operand->type = t_int;
  2829. if (!is_type_pointer(ptr0.type) && !is_type_multi_pointer(ptr0.type)) {
  2830. gbString str = type_to_string(ptr0.type);
  2831. error(ptr0.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2832. gb_string_free(str);
  2833. return false;
  2834. }
  2835. if (are_types_identical(core_type(ptr0.type), t_rawptr)) {
  2836. gbString str = type_to_string(ptr0.type);
  2837. error(ptr0.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2838. gb_string_free(str);
  2839. return false;
  2840. }
  2841. if (!is_type_pointer(ptr1.type) && !is_type_multi_pointer(ptr1.type)) {
  2842. gbString str = type_to_string(ptr1.type);
  2843. error(ptr1.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2844. gb_string_free(str);
  2845. return false;
  2846. }
  2847. if (are_types_identical(core_type(ptr1.type), t_rawptr)) {
  2848. gbString str = type_to_string(ptr1.type);
  2849. error(ptr1.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  2850. gb_string_free(str);
  2851. return false;
  2852. }
  2853. if (!are_types_identical(ptr0.type, ptr1.type)) {
  2854. gbString xts = type_to_string(ptr0.type);
  2855. gbString yts = type_to_string(ptr1.type);
  2856. error(ptr0.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  2857. gb_string_free(yts);
  2858. gb_string_free(xts);
  2859. return false;
  2860. }
  2861. }
  2862. break;
  2863. case BuiltinProc_atomic_type_is_lock_free:
  2864. {
  2865. Ast *expr = ce->args[0];
  2866. Operand o = {};
  2867. check_expr_or_type(c, &o, expr);
  2868. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  2869. return false;
  2870. }
  2871. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  2872. error(o.expr, "Invalid argument to '%.*s'", LIT(builtin_name));
  2873. return false;
  2874. }
  2875. if (is_type_polymorphic(o.type)) {
  2876. error(o.expr, "'%.*s' of polymorphic type cannot be determined", LIT(builtin_name));
  2877. return false;
  2878. }
  2879. if (is_type_untyped(o.type)) {
  2880. error(o.expr, "'%.*s' of untyped type is not allowed", LIT(builtin_name));
  2881. return false;
  2882. }
  2883. Type *t = o.type;
  2884. bool is_lock_free = is_type_lock_free(t);
  2885. operand->mode = Addressing_Constant;
  2886. operand->type = t_untyped_bool;
  2887. operand->value = exact_value_bool(is_lock_free);
  2888. break;
  2889. }
  2890. case BuiltinProc_atomic_thread_fence:
  2891. case BuiltinProc_atomic_signal_fence:
  2892. {
  2893. OdinAtomicMemoryOrder memory_order = {};
  2894. if (!check_atomic_memory_order_argument(c, ce->args[0], builtin_name, &memory_order)) {
  2895. return false;
  2896. }
  2897. switch (memory_order) {
  2898. case OdinAtomicMemoryOrder_acquire:
  2899. case OdinAtomicMemoryOrder_release:
  2900. case OdinAtomicMemoryOrder_acq_rel:
  2901. case OdinAtomicMemoryOrder_seq_cst:
  2902. break;
  2903. default:
  2904. error(ce->args[0], "Illegal memory ordering for '%.*s', got .%s", LIT(builtin_name), OdinAtomicMemoryOrder_strings[memory_order]);
  2905. break;
  2906. }
  2907. operand->mode = Addressing_NoValue;
  2908. }
  2909. break;
  2910. case BuiltinProc_volatile_store:
  2911. /*fallthrough*/
  2912. case BuiltinProc_unaligned_store:
  2913. /*fallthrough*/
  2914. case BuiltinProc_atomic_store:
  2915. {
  2916. Type *elem = nullptr;
  2917. if (!is_type_normal_pointer(operand->type, &elem)) {
  2918. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2919. return false;
  2920. }
  2921. Operand x = {};
  2922. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  2923. check_assignment(c, &x, elem, builtin_name);
  2924. operand->type = nullptr;
  2925. operand->mode = Addressing_NoValue;
  2926. break;
  2927. }
  2928. case BuiltinProc_atomic_store_explicit:
  2929. {
  2930. Type *elem = nullptr;
  2931. if (!is_type_normal_pointer(operand->type, &elem)) {
  2932. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2933. return false;
  2934. }
  2935. Operand x = {};
  2936. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  2937. check_assignment(c, &x, elem, builtin_name);
  2938. OdinAtomicMemoryOrder memory_order = {};
  2939. if (!check_atomic_memory_order_argument(c, ce->args[2], builtin_name, &memory_order)) {
  2940. return false;
  2941. }
  2942. switch (memory_order) {
  2943. case OdinAtomicMemoryOrder_consume:
  2944. case OdinAtomicMemoryOrder_acquire:
  2945. case OdinAtomicMemoryOrder_acq_rel:
  2946. error(ce->args[2], "Illegal memory order .%s for '%.*s'", OdinAtomicMemoryOrder_strings[memory_order], LIT(builtin_name));
  2947. break;
  2948. }
  2949. operand->type = nullptr;
  2950. operand->mode = Addressing_NoValue;
  2951. break;
  2952. }
  2953. case BuiltinProc_volatile_load:
  2954. /*fallthrough*/
  2955. case BuiltinProc_unaligned_load:
  2956. /*fallthrough*/
  2957. case BuiltinProc_atomic_load:
  2958. {
  2959. Type *elem = nullptr;
  2960. if (!is_type_normal_pointer(operand->type, &elem)) {
  2961. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2962. return false;
  2963. }
  2964. operand->type = elem;
  2965. operand->mode = Addressing_Value;
  2966. break;
  2967. }
  2968. case BuiltinProc_atomic_load_explicit:
  2969. {
  2970. Type *elem = nullptr;
  2971. if (!is_type_normal_pointer(operand->type, &elem)) {
  2972. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2973. return false;
  2974. }
  2975. OdinAtomicMemoryOrder memory_order = {};
  2976. if (!check_atomic_memory_order_argument(c, ce->args[1], builtin_name, &memory_order)) {
  2977. return false;
  2978. }
  2979. switch (memory_order) {
  2980. case OdinAtomicMemoryOrder_release:
  2981. case OdinAtomicMemoryOrder_acq_rel:
  2982. error(ce->args[1], "Illegal memory order .%s for '%.*s'", OdinAtomicMemoryOrder_strings[memory_order], LIT(builtin_name));
  2983. break;
  2984. }
  2985. operand->type = elem;
  2986. operand->mode = Addressing_Value;
  2987. break;
  2988. }
  2989. case BuiltinProc_atomic_add:
  2990. case BuiltinProc_atomic_sub:
  2991. case BuiltinProc_atomic_and:
  2992. case BuiltinProc_atomic_nand:
  2993. case BuiltinProc_atomic_or:
  2994. case BuiltinProc_atomic_xor:
  2995. case BuiltinProc_atomic_exchange:
  2996. {
  2997. Type *elem = nullptr;
  2998. if (!is_type_normal_pointer(operand->type, &elem)) {
  2999. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  3000. return false;
  3001. }
  3002. Operand x = {};
  3003. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  3004. check_assignment(c, &x, elem, builtin_name);
  3005. Type *t = type_deref(operand->type);
  3006. switch (id) {
  3007. case BuiltinProc_atomic_add:
  3008. case BuiltinProc_atomic_sub:
  3009. if (!is_type_numeric(t)) {
  3010. gbString str = type_to_string(t);
  3011. error(operand->expr, "Expected a numeric type for '%.*s', got %s", LIT(builtin_name), str);
  3012. gb_string_free(str);
  3013. } else if (is_type_different_to_arch_endianness(t)) {
  3014. gbString str = type_to_string(t);
  3015. error(operand->expr, "Expected a numeric type of the same platform endianness for '%.*s', got %s", LIT(builtin_name), str);
  3016. gb_string_free(str);
  3017. }
  3018. }
  3019. operand->type = elem;
  3020. operand->mode = Addressing_Value;
  3021. break;
  3022. }
  3023. case BuiltinProc_atomic_add_explicit:
  3024. case BuiltinProc_atomic_sub_explicit:
  3025. case BuiltinProc_atomic_and_explicit:
  3026. case BuiltinProc_atomic_nand_explicit:
  3027. case BuiltinProc_atomic_or_explicit:
  3028. case BuiltinProc_atomic_xor_explicit:
  3029. case BuiltinProc_atomic_exchange_explicit:
  3030. {
  3031. Type *elem = nullptr;
  3032. if (!is_type_normal_pointer(operand->type, &elem)) {
  3033. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  3034. return false;
  3035. }
  3036. Operand x = {};
  3037. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  3038. check_assignment(c, &x, elem, builtin_name);
  3039. if (!check_atomic_memory_order_argument(c, ce->args[2], builtin_name, nullptr)) {
  3040. return false;
  3041. }
  3042. Type *t = type_deref(operand->type);
  3043. switch (id) {
  3044. case BuiltinProc_atomic_add_explicit:
  3045. case BuiltinProc_atomic_sub_explicit:
  3046. if (!is_type_numeric(t)) {
  3047. gbString str = type_to_string(t);
  3048. error(operand->expr, "Expected a numeric type for '%.*s', got %s", LIT(builtin_name), str);
  3049. gb_string_free(str);
  3050. } else if (is_type_different_to_arch_endianness(t)) {
  3051. gbString str = type_to_string(t);
  3052. error(operand->expr, "Expected a numeric type of the same platform endianness for '%.*s', got %s", LIT(builtin_name), str);
  3053. gb_string_free(str);
  3054. }
  3055. break;
  3056. }
  3057. operand->type = elem;
  3058. operand->mode = Addressing_Value;
  3059. break;
  3060. }
  3061. case BuiltinProc_atomic_compare_exchange_strong:
  3062. case BuiltinProc_atomic_compare_exchange_weak:
  3063. {
  3064. Type *elem = nullptr;
  3065. if (!is_type_normal_pointer(operand->type, &elem)) {
  3066. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  3067. return false;
  3068. }
  3069. Operand x = {};
  3070. Operand y = {};
  3071. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  3072. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  3073. check_assignment(c, &x, elem, builtin_name);
  3074. check_assignment(c, &y, elem, builtin_name);
  3075. Type *t = type_deref(operand->type);
  3076. if (!is_type_comparable(t)) {
  3077. gbString str = type_to_string(t);
  3078. error(operand->expr, "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), str);
  3079. gb_string_free(str);
  3080. }
  3081. operand->mode = Addressing_OptionalOk;
  3082. operand->type = elem;
  3083. break;
  3084. }
  3085. case BuiltinProc_atomic_compare_exchange_strong_explicit:
  3086. case BuiltinProc_atomic_compare_exchange_weak_explicit:
  3087. {
  3088. Type *elem = nullptr;
  3089. if (!is_type_normal_pointer(operand->type, &elem)) {
  3090. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  3091. return false;
  3092. }
  3093. Operand x = {};
  3094. Operand y = {};
  3095. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  3096. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  3097. check_assignment(c, &x, elem, builtin_name);
  3098. check_assignment(c, &y, elem, builtin_name);
  3099. OdinAtomicMemoryOrder success_memory_order = {};
  3100. OdinAtomicMemoryOrder failure_memory_order = {};
  3101. if (!check_atomic_memory_order_argument(c, ce->args[3], builtin_name, &success_memory_order, "success ordering")) {
  3102. return false;
  3103. }
  3104. if (!check_atomic_memory_order_argument(c, ce->args[4], builtin_name, &failure_memory_order, "failure ordering")) {
  3105. return false;
  3106. }
  3107. Type *t = type_deref(operand->type);
  3108. if (!is_type_comparable(t)) {
  3109. gbString str = type_to_string(t);
  3110. error(operand->expr, "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), str);
  3111. gb_string_free(str);
  3112. }
  3113. bool invalid_combination = false;
  3114. switch (success_memory_order) {
  3115. case OdinAtomicMemoryOrder_relaxed:
  3116. case OdinAtomicMemoryOrder_release:
  3117. if (failure_memory_order != OdinAtomicMemoryOrder_relaxed) {
  3118. invalid_combination = true;
  3119. }
  3120. break;
  3121. case OdinAtomicMemoryOrder_consume:
  3122. switch (failure_memory_order) {
  3123. case OdinAtomicMemoryOrder_relaxed:
  3124. case OdinAtomicMemoryOrder_consume:
  3125. break;
  3126. default:
  3127. invalid_combination = true;
  3128. break;
  3129. }
  3130. break;
  3131. case OdinAtomicMemoryOrder_acquire:
  3132. case OdinAtomicMemoryOrder_acq_rel:
  3133. switch (failure_memory_order) {
  3134. case OdinAtomicMemoryOrder_relaxed:
  3135. case OdinAtomicMemoryOrder_consume:
  3136. case OdinAtomicMemoryOrder_acquire:
  3137. break;
  3138. default:
  3139. invalid_combination = true;
  3140. break;
  3141. }
  3142. break;
  3143. case OdinAtomicMemoryOrder_seq_cst:
  3144. switch (failure_memory_order) {
  3145. case OdinAtomicMemoryOrder_relaxed:
  3146. case OdinAtomicMemoryOrder_consume:
  3147. case OdinAtomicMemoryOrder_acquire:
  3148. case OdinAtomicMemoryOrder_seq_cst:
  3149. break;
  3150. default:
  3151. invalid_combination = true;
  3152. break;
  3153. }
  3154. break;
  3155. default:
  3156. invalid_combination = true;
  3157. break;
  3158. }
  3159. if (invalid_combination) {
  3160. error(ce->args[3], "Illegal memory order pairing for '%.*s', success = .%s, failure = .%s",
  3161. LIT(builtin_name),
  3162. OdinAtomicMemoryOrder_strings[success_memory_order],
  3163. OdinAtomicMemoryOrder_strings[failure_memory_order]
  3164. );
  3165. }
  3166. operand->mode = Addressing_OptionalOk;
  3167. operand->type = elem;
  3168. break;
  3169. }
  3170. case BuiltinProc_fixed_point_mul:
  3171. case BuiltinProc_fixed_point_div:
  3172. case BuiltinProc_fixed_point_mul_sat:
  3173. case BuiltinProc_fixed_point_div_sat:
  3174. {
  3175. Operand x = {};
  3176. Operand y = {};
  3177. Operand z = {};
  3178. check_expr(c, &x, ce->args[0]);
  3179. if (x.mode == Addressing_Invalid) {
  3180. return false;
  3181. }
  3182. check_expr(c, &y, ce->args[1]);
  3183. if (y.mode == Addressing_Invalid) {
  3184. return false;
  3185. }
  3186. convert_to_typed(c, &x, y.type);
  3187. if (x.mode == Addressing_Invalid) {
  3188. return false;
  3189. }
  3190. convert_to_typed(c, &y, x.type);
  3191. if (x.mode == Addressing_Invalid) {
  3192. return false;
  3193. }
  3194. if (!are_types_identical(x.type, y.type)) {
  3195. gbString xts = type_to_string(x.type);
  3196. gbString yts = type_to_string(y.type);
  3197. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  3198. gb_string_free(yts);
  3199. gb_string_free(xts);
  3200. return false;
  3201. }
  3202. if (!is_type_integer(x.type) || is_type_untyped(x.type)) {
  3203. gbString xts = type_to_string(x.type);
  3204. error(x.expr, "Expected an integer type for '%.*s', got %s", LIT(builtin_name), xts);
  3205. gb_string_free(xts);
  3206. return false;
  3207. }
  3208. check_expr(c, &z, ce->args[2]);
  3209. if (z.mode == Addressing_Invalid) {
  3210. return false;
  3211. }
  3212. if (z.mode != Addressing_Constant || !is_type_integer(z.type)) {
  3213. error(z.expr, "Expected a constant integer for the scale in '%.*s'", LIT(builtin_name));
  3214. return false;
  3215. }
  3216. i64 n = exact_value_to_i64(z.value);
  3217. if (n <= 0) {
  3218. error(z.expr, "Scale parameter in '%.*s' must be positive, got %lld", LIT(builtin_name), n);
  3219. return false;
  3220. }
  3221. i64 sz = 8*type_size_of(x.type);
  3222. if (n > sz) {
  3223. error(z.expr, "Scale parameter in '%.*s' is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_name), n, sz);
  3224. return false;
  3225. }
  3226. operand->type = x.type;
  3227. operand->mode = Addressing_Value;
  3228. }
  3229. break;
  3230. case BuiltinProc_expect:
  3231. {
  3232. Operand x = {};
  3233. Operand y = {};
  3234. check_expr(c, &x, ce->args[0]);
  3235. check_expr(c, &y, ce->args[1]);
  3236. if (x.mode == Addressing_Invalid) {
  3237. return false;
  3238. }
  3239. if (y.mode == Addressing_Invalid) {
  3240. return false;
  3241. }
  3242. convert_to_typed(c, &y, x.type);
  3243. convert_to_typed(c, &x, y.type);
  3244. if (!are_types_identical(x.type, y.type)) {
  3245. gbString xts = type_to_string(x.type);
  3246. gbString yts = type_to_string(y.type);
  3247. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  3248. gb_string_free(yts);
  3249. gb_string_free(xts);
  3250. *operand = x; // minimize error propagation
  3251. return true;
  3252. }
  3253. if (!is_type_integer_like(x.type)) {
  3254. gbString xts = type_to_string(x.type);
  3255. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_name), xts);
  3256. gb_string_free(xts);
  3257. *operand = x;
  3258. return true;
  3259. }
  3260. if (y.mode != Addressing_Constant) {
  3261. error(y.expr, "Second argument to '%.*s' must be constant as it is the expected value", LIT(builtin_name));
  3262. }
  3263. if (x.mode == Addressing_Constant) {
  3264. // NOTE(bill): just completely ignore this intrinsic entirely
  3265. *operand = x;
  3266. return true;
  3267. }
  3268. operand->mode = Addressing_Value;
  3269. operand->type = x.type;
  3270. }
  3271. break;
  3272. case BuiltinProc_prefetch_read_instruction:
  3273. case BuiltinProc_prefetch_read_data:
  3274. case BuiltinProc_prefetch_write_instruction:
  3275. case BuiltinProc_prefetch_write_data:
  3276. {
  3277. operand->mode = Addressing_NoValue;
  3278. operand->type = nullptr;
  3279. Operand x = {};
  3280. Operand y = {};
  3281. check_expr(c, &x, ce->args[0]);
  3282. check_expr(c, &y, ce->args[1]);
  3283. if (x.mode == Addressing_Invalid) {
  3284. return false;
  3285. }
  3286. if (y.mode == Addressing_Invalid) {
  3287. return false;
  3288. }
  3289. check_assignment(c, &x, t_rawptr, builtin_name);
  3290. if (x.mode == Addressing_Invalid) {
  3291. return false;
  3292. }
  3293. if (y.mode != Addressing_Constant && is_type_integer(y.type)) {
  3294. error(y.expr, "Second argument to '%.*s' representing the locality must be an integer in the range 0..=3", LIT(builtin_name));
  3295. return false;
  3296. }
  3297. i64 locality = exact_value_to_i64(y.value);
  3298. if (!(0 <= locality && locality <= 3)) {
  3299. error(y.expr, "Second argument to '%.*s' representing the locality must be an integer in the range 0..=3", LIT(builtin_name));
  3300. return false;
  3301. }
  3302. }
  3303. break;
  3304. case BuiltinProc_syscall:
  3305. {
  3306. convert_to_typed(c, operand, t_uintptr);
  3307. if (!is_type_uintptr(operand->type)) {
  3308. gbString t = type_to_string(operand->type);
  3309. error(operand->expr, "Argument 0 must be of type 'uintptr', got %s", t);
  3310. gb_string_free(t);
  3311. }
  3312. for (isize i = 1; i < ce->args.count; i++) {
  3313. Operand x = {};
  3314. check_expr(c, &x, ce->args[i]);
  3315. if (x.mode != Addressing_Invalid) {
  3316. convert_to_typed(c, &x, t_uintptr);
  3317. }
  3318. if (!is_type_uintptr(operand->type)) {
  3319. gbString t = type_to_string(x.type);
  3320. error(x.expr, "Argument %td must be of type 'uintptr', got %s", i, t);
  3321. gb_string_free(t);
  3322. }
  3323. }
  3324. isize max_arg_count = 32;
  3325. switch (build_context.metrics.os) {
  3326. case TargetOs_windows:
  3327. case TargetOs_freestanding:
  3328. error(call, "'%.*s' is not supported on this platform (%.*s)", LIT(builtin_name), LIT(target_os_names[build_context.metrics.os]));
  3329. break;
  3330. case TargetOs_darwin:
  3331. case TargetOs_linux:
  3332. case TargetOs_essence:
  3333. case TargetOs_freebsd:
  3334. case TargetOs_openbsd:
  3335. switch (build_context.metrics.arch) {
  3336. case TargetArch_i386:
  3337. case TargetArch_amd64:
  3338. case TargetArch_arm64:
  3339. max_arg_count = 7;
  3340. break;
  3341. }
  3342. break;
  3343. }
  3344. if (ce->args.count > max_arg_count) {
  3345. error(ast_end_token(call), "'%.*s' has a maximum of %td arguments on this platform (%.*s), got %td", LIT(builtin_name), max_arg_count, LIT(target_os_names[build_context.metrics.os]), ce->args.count);
  3346. }
  3347. operand->mode = Addressing_Value;
  3348. operand->type = t_uintptr;
  3349. return true;
  3350. }
  3351. break;
  3352. case BuiltinProc_type_base_type:
  3353. if (operand->mode != Addressing_Type) {
  3354. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  3355. } else {
  3356. operand->type = base_type(operand->type);
  3357. }
  3358. operand->mode = Addressing_Type;
  3359. break;
  3360. case BuiltinProc_type_core_type:
  3361. if (operand->mode != Addressing_Type) {
  3362. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  3363. } else {
  3364. operand->type = core_type(operand->type);
  3365. }
  3366. operand->mode = Addressing_Type;
  3367. break;
  3368. case BuiltinProc_type_elem_type:
  3369. if (operand->mode != Addressing_Type) {
  3370. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  3371. } else {
  3372. Type *bt = base_type(operand->type);
  3373. switch (bt->kind) {
  3374. case Type_Basic:
  3375. switch (bt->Basic.kind) {
  3376. case Basic_complex64: operand->type = t_f32; break;
  3377. case Basic_complex128: operand->type = t_f64; break;
  3378. case Basic_quaternion128: operand->type = t_f32; break;
  3379. case Basic_quaternion256: operand->type = t_f64; break;
  3380. }
  3381. break;
  3382. case Type_Pointer: operand->type = bt->Pointer.elem; break;
  3383. case Type_Array: operand->type = bt->Array.elem; break;
  3384. case Type_EnumeratedArray: operand->type = bt->EnumeratedArray.elem; break;
  3385. case Type_Slice: operand->type = bt->Slice.elem; break;
  3386. case Type_DynamicArray: operand->type = bt->DynamicArray.elem; break;
  3387. }
  3388. }
  3389. operand->mode = Addressing_Type;
  3390. break;
  3391. case BuiltinProc_type_is_boolean:
  3392. case BuiltinProc_type_is_integer:
  3393. case BuiltinProc_type_is_rune:
  3394. case BuiltinProc_type_is_float:
  3395. case BuiltinProc_type_is_complex:
  3396. case BuiltinProc_type_is_quaternion:
  3397. case BuiltinProc_type_is_string:
  3398. case BuiltinProc_type_is_typeid:
  3399. case BuiltinProc_type_is_any:
  3400. case BuiltinProc_type_is_endian_platform:
  3401. case BuiltinProc_type_is_endian_little:
  3402. case BuiltinProc_type_is_endian_big:
  3403. case BuiltinProc_type_is_unsigned:
  3404. case BuiltinProc_type_is_numeric:
  3405. case BuiltinProc_type_is_ordered:
  3406. case BuiltinProc_type_is_ordered_numeric:
  3407. case BuiltinProc_type_is_indexable:
  3408. case BuiltinProc_type_is_sliceable:
  3409. case BuiltinProc_type_is_comparable:
  3410. case BuiltinProc_type_is_simple_compare:
  3411. case BuiltinProc_type_is_dereferenceable:
  3412. case BuiltinProc_type_is_valid_map_key:
  3413. case BuiltinProc_type_is_valid_matrix_elements:
  3414. case BuiltinProc_type_is_named:
  3415. case BuiltinProc_type_is_pointer:
  3416. case BuiltinProc_type_is_multi_pointer:
  3417. case BuiltinProc_type_is_array:
  3418. case BuiltinProc_type_is_enumerated_array:
  3419. case BuiltinProc_type_is_slice:
  3420. case BuiltinProc_type_is_dynamic_array:
  3421. case BuiltinProc_type_is_map:
  3422. case BuiltinProc_type_is_struct:
  3423. case BuiltinProc_type_is_union:
  3424. case BuiltinProc_type_is_enum:
  3425. case BuiltinProc_type_is_proc:
  3426. case BuiltinProc_type_is_bit_set:
  3427. case BuiltinProc_type_is_simd_vector:
  3428. case BuiltinProc_type_is_matrix:
  3429. case BuiltinProc_type_is_specialized_polymorphic_record:
  3430. case BuiltinProc_type_is_unspecialized_polymorphic_record:
  3431. case BuiltinProc_type_has_nil:
  3432. GB_ASSERT(BuiltinProc__type_simple_boolean_begin < id && id < BuiltinProc__type_simple_boolean_end);
  3433. operand->value = exact_value_bool(false);
  3434. if (operand->mode != Addressing_Type) {
  3435. gbString str = expr_to_string(ce->args[0]);
  3436. error(operand->expr, "Expected a type for '%.*s', got '%s'", LIT(builtin_name), str);
  3437. gb_string_free(str);
  3438. } else {
  3439. i32 i = id - cast(i32)BuiltinProc__type_simple_boolean_begin;
  3440. auto procedure = builtin_type_is_procs[i];
  3441. GB_ASSERT_MSG(procedure != nullptr, "%.*s", LIT(builtin_name));
  3442. bool ok = procedure(operand->type);
  3443. operand->value = exact_value_bool(ok);
  3444. }
  3445. operand->mode = Addressing_Constant;
  3446. operand->type = t_untyped_bool;
  3447. break;
  3448. case BuiltinProc_type_has_field:
  3449. {
  3450. Operand op = {};
  3451. Type *bt = check_type(c, ce->args[0]);
  3452. Type *type = base_type(bt);
  3453. if (type == nullptr || type == t_invalid) {
  3454. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  3455. return false;
  3456. }
  3457. Operand x = {};
  3458. check_expr(c, &x, ce->args[1]);
  3459. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  3460. error(ce->args[1], "Expected a const string for field argument");
  3461. return false;
  3462. }
  3463. String field_name = x.value.value_string;
  3464. Selection sel = lookup_field(type, field_name, false);
  3465. operand->mode = Addressing_Constant;
  3466. operand->value = exact_value_bool(sel.index.count != 0);
  3467. operand->type = t_untyped_bool;
  3468. break;
  3469. }
  3470. break;
  3471. case BuiltinProc_type_field_type:
  3472. {
  3473. Operand op = {};
  3474. Type *bt = check_type(c, ce->args[0]);
  3475. Type *type = base_type(bt);
  3476. if (type == nullptr || type == t_invalid) {
  3477. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  3478. return false;
  3479. }
  3480. Operand x = {};
  3481. check_expr(c, &x, ce->args[1]);
  3482. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  3483. error(ce->args[1], "Expected a const string for field argument");
  3484. return false;
  3485. }
  3486. String field_name = x.value.value_string;
  3487. Selection sel = lookup_field(type, field_name, false);
  3488. if (sel.index.count == 0) {
  3489. gbString t = type_to_string(type);
  3490. error(ce->args[1], "'%.*s' is not a field of type %s", LIT(field_name), t);
  3491. gb_string_free(t);
  3492. return false;
  3493. }
  3494. operand->mode = Addressing_Type;
  3495. operand->type = sel.entity->type;
  3496. break;
  3497. }
  3498. break;
  3499. case BuiltinProc_type_is_specialization_of:
  3500. {
  3501. if (operand->mode != Addressing_Type) {
  3502. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  3503. operand->mode = Addressing_Invalid;
  3504. operand->type = t_invalid;
  3505. return false;
  3506. }
  3507. Type *t = operand->type;
  3508. Type *s = nullptr;
  3509. bool prev_ips = c->in_polymorphic_specialization;
  3510. c->in_polymorphic_specialization = true;
  3511. s = check_type(c, ce->args[1]);
  3512. c->in_polymorphic_specialization = prev_ips;
  3513. if (s == t_invalid) {
  3514. error(ce->args[1], "Invalid specialization type for '%.*s'", LIT(builtin_name));
  3515. operand->mode = Addressing_Invalid;
  3516. operand->type = t_invalid;
  3517. return false;
  3518. }
  3519. operand->mode = Addressing_Constant;
  3520. operand->type = t_untyped_bool;
  3521. operand->value = exact_value_bool(check_type_specialization_to(c, s, t, false, false));
  3522. }
  3523. break;
  3524. case BuiltinProc_type_is_variant_of:
  3525. {
  3526. if (operand->mode != Addressing_Type) {
  3527. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  3528. operand->mode = Addressing_Invalid;
  3529. operand->type = t_invalid;
  3530. return false;
  3531. }
  3532. Type *u = operand->type;
  3533. if (!is_type_union(u)) {
  3534. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  3535. operand->mode = Addressing_Invalid;
  3536. operand->type = t_invalid;
  3537. return false;
  3538. }
  3539. Type *v = check_type(c, ce->args[1]);
  3540. u = base_type(u);
  3541. GB_ASSERT(u->kind == Type_Union);
  3542. bool is_variant = false;
  3543. for_array(i, u->Union.variants) {
  3544. Type *vt = u->Union.variants[i];
  3545. if (are_types_identical(v, vt)) {
  3546. is_variant = true;
  3547. break;
  3548. }
  3549. }
  3550. operand->mode = Addressing_Constant;
  3551. operand->type = t_untyped_bool;
  3552. operand->value = exact_value_bool(is_variant);
  3553. }
  3554. break;
  3555. case BuiltinProc_type_struct_field_count:
  3556. operand->value = exact_value_i64(0);
  3557. if (operand->mode != Addressing_Type) {
  3558. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  3559. } else if (!is_type_struct(operand->type)) {
  3560. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  3561. } else {
  3562. Type *bt = base_type(operand->type);
  3563. operand->value = exact_value_i64(bt->Struct.fields.count);
  3564. }
  3565. operand->mode = Addressing_Constant;
  3566. operand->type = t_untyped_integer;
  3567. break;
  3568. case BuiltinProc_type_proc_parameter_count:
  3569. operand->value = exact_value_i64(0);
  3570. if (operand->mode != Addressing_Type) {
  3571. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3572. } else if (!is_type_proc(operand->type)) {
  3573. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3574. } else {
  3575. Type *bt = base_type(operand->type);
  3576. operand->value = exact_value_i64(bt->Proc.param_count);
  3577. }
  3578. operand->mode = Addressing_Constant;
  3579. operand->type = t_untyped_integer;
  3580. break;
  3581. case BuiltinProc_type_proc_return_count:
  3582. operand->value = exact_value_i64(0);
  3583. if (operand->mode != Addressing_Type) {
  3584. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3585. } else if (!is_type_proc(operand->type)) {
  3586. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3587. } else {
  3588. Type *bt = base_type(operand->type);
  3589. operand->value = exact_value_i64(bt->Proc.result_count);
  3590. }
  3591. operand->mode = Addressing_Constant;
  3592. operand->type = t_untyped_integer;
  3593. break;
  3594. case BuiltinProc_type_proc_parameter_type:
  3595. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  3596. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3597. return false;
  3598. } else {
  3599. if (is_type_polymorphic(operand->type)) {
  3600. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  3601. return false;
  3602. }
  3603. Operand op = {};
  3604. check_expr(c, &op, ce->args[1]);
  3605. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  3606. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  3607. return false;
  3608. }
  3609. i64 index = exact_value_to_i64(op.value);
  3610. if (index < 0) {
  3611. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  3612. return false;
  3613. }
  3614. Entity *param = nullptr;
  3615. i64 count = 0;
  3616. Type *bt = base_type(operand->type);
  3617. if (bt->kind == Type_Proc) {
  3618. count = bt->Proc.param_count;
  3619. if (index < count) {
  3620. param = bt->Proc.params->Tuple.variables[cast(isize)index];
  3621. }
  3622. }
  3623. if (index >= count) {
  3624. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  3625. return false;
  3626. }
  3627. GB_ASSERT(param != nullptr);
  3628. switch (param->kind) {
  3629. case Entity_Constant:
  3630. operand->mode = Addressing_Constant;
  3631. operand->type = param->type;
  3632. operand->value = param->Constant.value;
  3633. break;
  3634. case Entity_TypeName:
  3635. case Entity_Variable:
  3636. operand->mode = Addressing_Type;
  3637. operand->type = param->type;
  3638. break;
  3639. default:
  3640. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  3641. break;
  3642. }
  3643. }
  3644. break;
  3645. case BuiltinProc_type_proc_return_type:
  3646. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  3647. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  3648. return false;
  3649. } else {
  3650. if (is_type_polymorphic(operand->type)) {
  3651. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  3652. return false;
  3653. }
  3654. Operand op = {};
  3655. check_expr(c, &op, ce->args[1]);
  3656. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  3657. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  3658. return false;
  3659. }
  3660. i64 index = exact_value_to_i64(op.value);
  3661. if (index < 0) {
  3662. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  3663. return false;
  3664. }
  3665. Entity *param = nullptr;
  3666. i64 count = 0;
  3667. Type *bt = base_type(operand->type);
  3668. if (bt->kind == Type_Proc) {
  3669. count = bt->Proc.result_count;
  3670. if (index < count) {
  3671. param = bt->Proc.results->Tuple.variables[cast(isize)index];
  3672. }
  3673. }
  3674. if (index >= count) {
  3675. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  3676. return false;
  3677. }
  3678. GB_ASSERT(param != nullptr);
  3679. switch (param->kind) {
  3680. case Entity_Constant:
  3681. operand->mode = Addressing_Constant;
  3682. operand->type = param->type;
  3683. operand->value = param->Constant.value;
  3684. break;
  3685. case Entity_TypeName:
  3686. case Entity_Variable:
  3687. operand->mode = Addressing_Type;
  3688. operand->type = param->type;
  3689. break;
  3690. default:
  3691. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  3692. break;
  3693. }
  3694. }
  3695. break;
  3696. case BuiltinProc_type_polymorphic_record_parameter_count:
  3697. operand->value = exact_value_i64(0);
  3698. if (operand->mode != Addressing_Type) {
  3699. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  3700. } else {
  3701. Type *bt = base_type(operand->type);
  3702. if (bt->kind == Type_Struct) {
  3703. if (bt->Struct.polymorphic_params != nullptr) {
  3704. operand->value = exact_value_i64(bt->Struct.polymorphic_params->Tuple.variables.count);
  3705. }
  3706. } else if (bt->kind == Type_Union) {
  3707. if (bt->Union.polymorphic_params != nullptr) {
  3708. operand->value = exact_value_i64(bt->Union.polymorphic_params->Tuple.variables.count);
  3709. }
  3710. } else {
  3711. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  3712. }
  3713. }
  3714. operand->mode = Addressing_Constant;
  3715. operand->type = t_untyped_integer;
  3716. break;
  3717. case BuiltinProc_type_polymorphic_record_parameter_value:
  3718. if (operand->mode != Addressing_Type) {
  3719. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  3720. return false;
  3721. } else if (!is_type_polymorphic_record_specialized(operand->type)) {
  3722. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  3723. return false;
  3724. } else {
  3725. Operand op = {};
  3726. check_expr(c, &op, ce->args[1]);
  3727. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  3728. error(op.expr, "Expected a constant integer for the index of record parameter value");
  3729. return false;
  3730. }
  3731. i64 index = exact_value_to_i64(op.value);
  3732. if (index < 0) {
  3733. error(op.expr, "Expected a non-negative integer for the index of record parameter value, got %lld", cast(long long)index);
  3734. return false;
  3735. }
  3736. Entity *param = nullptr;
  3737. i64 count = 0;
  3738. Type *bt = base_type(operand->type);
  3739. if (bt->kind == Type_Struct) {
  3740. if (bt->Struct.polymorphic_params != nullptr) {
  3741. count = bt->Struct.polymorphic_params->Tuple.variables.count;
  3742. if (index < count) {
  3743. param = bt->Struct.polymorphic_params->Tuple.variables[cast(isize)index];
  3744. }
  3745. }
  3746. } else if (bt->kind == Type_Union) {
  3747. if (bt->Union.polymorphic_params != nullptr) {
  3748. count = bt->Union.polymorphic_params->Tuple.variables.count;
  3749. if (index < count) {
  3750. param = bt->Union.polymorphic_params->Tuple.variables[cast(isize)index];
  3751. }
  3752. }
  3753. } else {
  3754. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  3755. return false;
  3756. }
  3757. if (index >= count) {
  3758. error(op.expr, "Index of record parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  3759. return false;
  3760. }
  3761. GB_ASSERT(param != nullptr);
  3762. switch (param->kind) {
  3763. case Entity_Constant:
  3764. operand->mode = Addressing_Constant;
  3765. operand->type = param->type;
  3766. operand->value = param->Constant.value;
  3767. break;
  3768. case Entity_TypeName:
  3769. operand->mode = Addressing_Type;
  3770. operand->type = param->type;
  3771. break;
  3772. default:
  3773. GB_PANIC("Unhandled polymorphic record type");
  3774. break;
  3775. }
  3776. }
  3777. break;
  3778. case BuiltinProc_type_is_subtype_of:
  3779. {
  3780. Operand op_src = {};
  3781. Operand op_dst = {};
  3782. check_expr_or_type(c, &op_src, ce->args[0]);
  3783. if (op_src.mode != Addressing_Type) {
  3784. gbString e = expr_to_string(op_src.expr);
  3785. error(op_src.expr, "'%.*s' expects a type, got %s", LIT(builtin_name), e);
  3786. gb_string_free(e);
  3787. return false;
  3788. }
  3789. check_expr_or_type(c, &op_dst, ce->args[1]);
  3790. if (op_dst.mode != Addressing_Type) {
  3791. gbString e = expr_to_string(op_dst.expr);
  3792. error(op_dst.expr, "'%.*s' expects a type, got %s", LIT(builtin_name), e);
  3793. gb_string_free(e);
  3794. return false;
  3795. }
  3796. operand->value = exact_value_bool(is_type_subtype_of(op_src.type, op_dst.type));
  3797. operand->mode = Addressing_Constant;
  3798. operand->type = t_untyped_bool;
  3799. } break;
  3800. case BuiltinProc_type_field_index_of:
  3801. {
  3802. Operand op = {};
  3803. Type *bt = check_type(c, ce->args[0]);
  3804. Type *type = base_type(bt);
  3805. if (type == nullptr || type == t_invalid) {
  3806. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  3807. return false;
  3808. }
  3809. Operand x = {};
  3810. check_expr(c, &x, ce->args[1]);
  3811. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  3812. error(ce->args[1], "Expected a const string for field argument");
  3813. return false;
  3814. }
  3815. String field_name = x.value.value_string;
  3816. Selection sel = lookup_field(type, field_name, false);
  3817. if (sel.entity == nullptr) {
  3818. gbString type_str = type_to_string(bt);
  3819. error(ce->args[0],
  3820. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  3821. gb_string_free(type_str);
  3822. if (bt->kind == Type_Struct) {
  3823. check_did_you_mean_type(field_name, bt->Struct.fields);
  3824. }
  3825. return false;
  3826. }
  3827. if (sel.indirect) {
  3828. gbString type_str = type_to_string(bt);
  3829. error(ce->args[0],
  3830. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  3831. gb_string_free(type_str);
  3832. return false;
  3833. }
  3834. operand->mode = Addressing_Constant;
  3835. operand->value = exact_value_u64(sel.index[0]);
  3836. operand->type = t_uintptr;
  3837. break;
  3838. }
  3839. break;
  3840. case BuiltinProc_type_equal_proc:
  3841. {
  3842. Operand op = {};
  3843. Type *bt = check_type(c, ce->args[0]);
  3844. Type *type = base_type(bt);
  3845. if (type == nullptr || type == t_invalid) {
  3846. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  3847. return false;
  3848. }
  3849. if (!is_type_comparable(type)) {
  3850. gbString t = type_to_string(type);
  3851. error(ce->args[0], "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), t);
  3852. gb_string_free(t);
  3853. return false;
  3854. }
  3855. operand->mode = Addressing_Value;
  3856. operand->type = t_equal_proc;
  3857. break;
  3858. }
  3859. case BuiltinProc_type_hasher_proc:
  3860. {
  3861. Operand op = {};
  3862. Type *bt = check_type(c, ce->args[0]);
  3863. Type *type = base_type(bt);
  3864. if (type == nullptr || type == t_invalid) {
  3865. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  3866. return false;
  3867. }
  3868. if (!is_type_valid_for_keys(type)) {
  3869. gbString t = type_to_string(type);
  3870. error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
  3871. gb_string_free(t);
  3872. return false;
  3873. }
  3874. add_map_key_type_dependencies(c, type);
  3875. operand->mode = Addressing_Value;
  3876. operand->type = t_hasher_proc;
  3877. break;
  3878. }
  3879. case BuiltinProc_constant_utf16_cstring:
  3880. {
  3881. String value = {};
  3882. if (!is_constant_string(c, builtin_name, ce->args[0], &value)) {
  3883. return false;
  3884. }
  3885. operand->mode = Addressing_Value;
  3886. operand->type = alloc_type_multi_pointer(t_u16);
  3887. operand->value = {};
  3888. break;
  3889. }
  3890. case BuiltinProc_wasm_memory_grow:
  3891. {
  3892. if (!is_arch_wasm()) {
  3893. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  3894. return false;
  3895. }
  3896. Operand index = {};
  3897. Operand delta = {};
  3898. check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
  3899. check_expr(c, &delta, ce->args[1]); if (delta.mode == Addressing_Invalid) return false;
  3900. convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
  3901. convert_to_typed(c, &delta, t_uintptr); if (delta.mode == Addressing_Invalid) return false;
  3902. if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
  3903. gbString e = expr_to_string(index.expr);
  3904. gbString t = type_to_string(index.type);
  3905. error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
  3906. gb_string_free(t);
  3907. gb_string_free(e);
  3908. return false;
  3909. }
  3910. if (!is_operand_value(delta) || !check_is_assignable_to(c, &delta, t_uintptr)) {
  3911. gbString e = expr_to_string(delta.expr);
  3912. gbString t = type_to_string(delta.type);
  3913. error(delta.expr, "'%.*s' expected a uintptr for the memory delta, got '%s' of type %s", LIT(builtin_name), e, t);
  3914. gb_string_free(t);
  3915. gb_string_free(e);
  3916. return false;
  3917. }
  3918. operand->mode = Addressing_Value;
  3919. operand->type = t_int;
  3920. operand->value = {};
  3921. break;
  3922. }
  3923. break;
  3924. case BuiltinProc_wasm_memory_size:
  3925. {
  3926. if (!is_arch_wasm()) {
  3927. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  3928. return false;
  3929. }
  3930. Operand index = {};
  3931. check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
  3932. convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
  3933. if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
  3934. gbString e = expr_to_string(index.expr);
  3935. gbString t = type_to_string(index.type);
  3936. error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
  3937. gb_string_free(t);
  3938. gb_string_free(e);
  3939. return false;
  3940. }
  3941. operand->mode = Addressing_Value;
  3942. operand->type = t_int;
  3943. operand->value = {};
  3944. break;
  3945. }
  3946. break;
  3947. case BuiltinProc_wasm_memory_atomic_wait32:
  3948. {
  3949. if (!is_arch_wasm()) {
  3950. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  3951. return false;
  3952. }
  3953. Operand ptr = {};
  3954. Operand expected = {};
  3955. Operand timeout = {};
  3956. check_expr(c, &ptr, ce->args[0]); if (ptr.mode == Addressing_Invalid) return false;
  3957. check_expr(c, &expected, ce->args[1]); if (expected.mode == Addressing_Invalid) return false;
  3958. check_expr(c, &timeout, ce->args[2]); if (timeout.mode == Addressing_Invalid) return false;
  3959. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  3960. convert_to_typed(c, &ptr, t_u32_ptr); if (ptr.mode == Addressing_Invalid) return false;
  3961. convert_to_typed(c, &expected, t_u32); if (expected.mode == Addressing_Invalid) return false;
  3962. convert_to_typed(c, &timeout, t_i64); if (timeout.mode == Addressing_Invalid) return false;
  3963. if (!is_operand_value(ptr) || !check_is_assignable_to(c, &ptr, t_u32_ptr)) {
  3964. gbString e = expr_to_string(ptr.expr);
  3965. gbString t = type_to_string(ptr.type);
  3966. error(ptr.expr, "'%.*s' expected ^u32 for the memory pointer, got '%s' of type %s", LIT(builtin_name), e, t);
  3967. gb_string_free(t);
  3968. gb_string_free(e);
  3969. return false;
  3970. }
  3971. if (!is_operand_value(expected) || !check_is_assignable_to(c, &expected, t_u32)) {
  3972. gbString e = expr_to_string(expected.expr);
  3973. gbString t = type_to_string(expected.type);
  3974. error(expected.expr, "'%.*s' expected u32 for the 'expected' value, got '%s' of type %s", LIT(builtin_name), e, t);
  3975. gb_string_free(t);
  3976. gb_string_free(e);
  3977. return false;
  3978. }
  3979. if (!is_operand_value(timeout) || !check_is_assignable_to(c, &timeout, t_i64)) {
  3980. gbString e = expr_to_string(timeout.expr);
  3981. gbString t = type_to_string(timeout.type);
  3982. error(timeout.expr, "'%.*s' expected i64 for the timeout, got '%s' of type %s", LIT(builtin_name), e, t);
  3983. gb_string_free(t);
  3984. gb_string_free(e);
  3985. return false;
  3986. }
  3987. operand->mode = Addressing_Value;
  3988. operand->type = t_u32;
  3989. operand->value = {};
  3990. break;
  3991. }
  3992. break;
  3993. case BuiltinProc_wasm_memory_atomic_notify32:
  3994. {
  3995. if (!is_arch_wasm()) {
  3996. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  3997. return false;
  3998. }
  3999. Operand ptr = {};
  4000. Operand waiters = {};
  4001. check_expr(c, &ptr, ce->args[0]); if (ptr.mode == Addressing_Invalid) return false;
  4002. check_expr(c, &waiters, ce->args[1]); if (waiters.mode == Addressing_Invalid) return false;
  4003. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  4004. convert_to_typed(c, &ptr, t_u32_ptr); if (ptr.mode == Addressing_Invalid) return false;
  4005. convert_to_typed(c, &waiters, t_u32); if (waiters.mode == Addressing_Invalid) return false;
  4006. if (!is_operand_value(ptr) || !check_is_assignable_to(c, &ptr, t_u32_ptr)) {
  4007. gbString e = expr_to_string(ptr.expr);
  4008. gbString t = type_to_string(ptr.type);
  4009. error(ptr.expr, "'%.*s' expected ^u32 for the memory pointer, got '%s' of type %s", LIT(builtin_name), e, t);
  4010. gb_string_free(t);
  4011. gb_string_free(e);
  4012. return false;
  4013. }
  4014. if (!is_operand_value(waiters) || !check_is_assignable_to(c, &waiters, t_u32)) {
  4015. gbString e = expr_to_string(waiters.expr);
  4016. gbString t = type_to_string(waiters.type);
  4017. error(waiters.expr, "'%.*s' expected u32 for the 'waiters' value, got '%s' of type %s", LIT(builtin_name), e, t);
  4018. gb_string_free(t);
  4019. gb_string_free(e);
  4020. return false;
  4021. }
  4022. operand->mode = Addressing_Value;
  4023. operand->type = t_u32;
  4024. operand->value = {};
  4025. break;
  4026. }
  4027. break;
  4028. }
  4029. return true;
  4030. }