check_builtin.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222
  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_named,
  27. is_type_pointer,
  28. is_type_array,
  29. is_type_enumerated_array,
  30. is_type_slice,
  31. is_type_dynamic_array,
  32. is_type_map,
  33. is_type_struct,
  34. is_type_union,
  35. is_type_enum,
  36. is_type_proc,
  37. is_type_bit_set,
  38. is_type_simd_vector,
  39. is_type_polymorphic_record_specialized,
  40. is_type_polymorphic_record_unspecialized,
  41. type_has_nil,
  42. };
  43. void check_promote_optional_ok(CheckerContext *c, Operand *x, Type **val_type_, Type **ok_type_) {
  44. switch (x->mode) {
  45. case Addressing_MapIndex:
  46. case Addressing_OptionalOk:
  47. case Addressing_OptionalOkPtr:
  48. if (val_type_) *val_type_ = x->type;
  49. break;
  50. default:
  51. if (ok_type_) *ok_type_ = x->type;
  52. return;
  53. }
  54. Ast *expr = unparen_expr(x->expr);
  55. if (expr->kind == Ast_CallExpr) {
  56. Type *pt = base_type(type_of_expr(expr->CallExpr.proc));
  57. if (is_type_proc(pt)) {
  58. Type *tuple = pt->Proc.results;
  59. add_type_and_value(&c->checker->info, x->expr, x->mode, tuple, x->value);
  60. if (pt->Proc.result_count >= 2) {
  61. if (ok_type_) *ok_type_ = tuple->Tuple.variables[1]->type;
  62. }
  63. expr->CallExpr.optional_ok_one = false;
  64. x->type = tuple;
  65. return;
  66. }
  67. }
  68. Type *tuple = make_optional_ok_type(x->type);
  69. if (ok_type_) *ok_type_ = tuple->Tuple.variables[1]->type;
  70. add_type_and_value(&c->checker->info, x->expr, x->mode, tuple, x->value);
  71. x->type = tuple;
  72. GB_ASSERT(is_type_tuple(type_of_expr(x->expr)));
  73. }
  74. void check_try_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
  75. Type *left_type = nullptr;
  76. Type *right_type = nullptr;
  77. if (x->type->kind == Type_Tuple) {
  78. auto const &vars = x->type->Tuple.variables;
  79. auto lhs = array_slice(vars, 0, vars.count-1);
  80. auto rhs = vars[vars.count-1];
  81. if (lhs.count == 1) {
  82. left_type = lhs[0]->type;
  83. } else if (lhs.count != 0) {
  84. left_type = alloc_type_tuple();
  85. left_type->Tuple.variables = array_make_from_ptr(lhs.data, lhs.count, lhs.count);
  86. }
  87. right_type = rhs->type;
  88. } else {
  89. check_promote_optional_ok(c, x, &left_type, &right_type);
  90. }
  91. if (left_type_) *left_type_ = left_type;
  92. if (right_type_) *right_type_ = right_type;
  93. if (!type_has_nil(right_type) && !is_type_boolean(right_type)) {
  94. gbString str = type_to_string(right_type);
  95. error(x->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);
  96. gb_string_free(str);
  97. }
  98. }
  99. bool check_builtin_try(CheckerContext *c, Operand *operand, String const &name, Ast *call, Type *type_hint) {
  100. ast_node(ce, CallExpr, call);
  101. Operand x = {};
  102. check_multi_expr_with_type_hint(c, &x, ce->args[0], type_hint);
  103. if (x.mode == Addressing_Invalid) {
  104. return false;
  105. }
  106. if (c->in_defer) {
  107. error(call, "'%.*s' cannot be used within a defer statement", LIT(name));
  108. }
  109. Type *left_type = nullptr;
  110. Type *right_type = nullptr;
  111. check_try_split_types(c, &x, name, &left_type, &right_type);
  112. add_type_and_value(&c->checker->info, ce->args[0], x.mode, x.type, x.value);
  113. if (c->curr_proc_sig == nullptr) {
  114. error(call, "'%.*s' can only be used within a procedure", LIT(name));
  115. }
  116. Type *proc_type = base_type(c->curr_proc_sig);
  117. GB_ASSERT(proc_type->kind == Type_Proc);
  118. Type *result_type = proc_type->Proc.results;
  119. if (result_type == nullptr) {
  120. error(call, "'%.*s' requires the current procedure to have at least one return value", LIT(name));
  121. } else {
  122. GB_ASSERT(result_type->kind == Type_Tuple);
  123. auto const &vars = result_type->Tuple.variables;
  124. Type *end_type = vars[vars.count-1]->type;
  125. if (vars.count > 1) {
  126. if (!proc_type->Proc.has_named_results) {
  127. error(call, "'%.*s' within a procedure with more than 1 return value requires that the return values are named, allowing for early return", LIT(name));
  128. }
  129. }
  130. Operand rhs = {};
  131. rhs.type = right_type;
  132. rhs.mode = Addressing_Value;
  133. // TODO(bill): better error message
  134. if (!check_is_assignable_to(c, &rhs, end_type)) {
  135. gbString a = type_to_string(right_type);
  136. gbString b = type_to_string(end_type);
  137. gbString ret_type = type_to_string(result_type);
  138. error(call, "Cannot assign end value of type '%s' to '%s' in '%.*s'", a, b, LIT(name));
  139. if (vars.count == 1) {
  140. error_line("\tProcedure return value type: %s\n", ret_type);
  141. } else {
  142. error_line("\tProcedure return value types: (%s)\n", ret_type);
  143. }
  144. gb_string_free(ret_type);
  145. gb_string_free(b);
  146. gb_string_free(a);
  147. }
  148. }
  149. if (left_type != nullptr) {
  150. operand->mode = Addressing_Value;
  151. operand->type = left_type;
  152. } else {
  153. operand->mode = Addressing_NoValue;
  154. operand->type = nullptr;
  155. }
  156. return true;
  157. }
  158. bool check_builtin_or_else(CheckerContext *c, Operand *operand, String const &name, Ast *call, Type *type_hint) {
  159. ast_node(ce, CallExpr, call);
  160. Operand x = {};
  161. Operand y = {};
  162. check_multi_expr_with_type_hint(c, &x, ce->args[0], type_hint);
  163. if (x.mode == Addressing_Invalid) {
  164. return false;
  165. }
  166. check_multi_expr(c, &y, ce->args[1]);
  167. error_operand_no_value(&y);
  168. if (y.mode == Addressing_Invalid) {
  169. return false;
  170. }
  171. Type *left_type = nullptr;
  172. Type *right_type = nullptr;
  173. check_try_split_types(c, &x, name, &left_type, &right_type);
  174. add_type_and_value(&c->checker->info, ce->args[0], x.mode, x.type, x.value);
  175. if (left_type != nullptr) {
  176. check_assignment(c, &y, left_type, name);
  177. } else {
  178. // TODO(bill): better error message
  179. error(call, "'%.*s' does not return a value", LIT(name));
  180. }
  181. if (left_type == nullptr) {
  182. left_type = t_invalid;
  183. }
  184. operand->mode = Addressing_Value;
  185. operand->type = left_type;
  186. return true;
  187. }
  188. bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  189. ast_node(ce, CallExpr, call);
  190. if (ce->inlining != ProcInlining_none) {
  191. error(call, "Inlining operators are not allowed on built-in procedures");
  192. }
  193. BuiltinProc *bp = &builtin_procs[id];
  194. {
  195. char const *err = nullptr;
  196. if (ce->args.count < bp->arg_count) {
  197. err = "Too few";
  198. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  199. err = "Too many";
  200. }
  201. if (err != nullptr) {
  202. gbString expr = expr_to_string(ce->proc);
  203. error(ce->close, "%s arguments for '%s', expected %td, got %td",
  204. err, expr,
  205. bp->arg_count, ce->args.count);
  206. gb_string_free(expr);
  207. return false;
  208. }
  209. }
  210. switch (id) {
  211. case BuiltinProc_size_of:
  212. case BuiltinProc_align_of:
  213. case BuiltinProc_offset_of:
  214. case BuiltinProc_type_info_of:
  215. case BuiltinProc_typeid_of:
  216. case BuiltinProc_len:
  217. case BuiltinProc_min:
  218. case BuiltinProc_max:
  219. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  220. break;
  221. case BuiltinProc_try:
  222. case BuiltinProc_or_else:
  223. // NOTE(bill): The first arg may be a tuple
  224. break;
  225. case BuiltinProc_DIRECTIVE: {
  226. ast_node(bd, BasicDirective, ce->proc);
  227. String name = bd->name.string;
  228. if (name == "defined") {
  229. break;
  230. }
  231. if (name == "config") {
  232. break;
  233. }
  234. /*fallthrough*/
  235. }
  236. default:
  237. if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
  238. check_expr_or_type(c, operand, ce->args[0]);
  239. } else if (ce->args.count > 0) {
  240. check_multi_expr(c, operand, ce->args[0]);
  241. }
  242. break;
  243. }
  244. String builtin_name = builtin_procs[id].name;
  245. if (ce->args.count > 0) {
  246. if (ce->args[0]->kind == Ast_FieldValue) {
  247. if (id != BuiltinProc_soa_zip) {
  248. error(call, "'field = value' calling is not allowed on built-in procedures");
  249. return false;
  250. }
  251. }
  252. }
  253. switch (id) {
  254. default:
  255. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
  256. break;
  257. case BuiltinProc_DIRECTIVE: {
  258. ast_node(bd, BasicDirective, ce->proc);
  259. String name = bd->name.string;
  260. if (name == "location") {
  261. if (ce->args.count > 1) {
  262. error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
  263. }
  264. if (ce->args.count > 0) {
  265. Ast *arg = ce->args[0];
  266. Entity *e = nullptr;
  267. Operand o = {};
  268. if (arg->kind == Ast_Ident) {
  269. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  270. } else if (arg->kind == Ast_SelectorExpr) {
  271. e = check_selector(c, &o, arg, nullptr);
  272. }
  273. if (e == nullptr) {
  274. error(ce->args[0], "'#location' expected a valid entity name");
  275. }
  276. }
  277. operand->type = t_source_code_location;
  278. operand->mode = Addressing_Value;
  279. } else if (name == "load") {
  280. if (ce->args.count != 1) {
  281. error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
  282. return false;
  283. }
  284. Ast *arg = ce->args[0];
  285. Operand o = {};
  286. check_expr(c, &o, arg);
  287. if (o.mode != Addressing_Constant) {
  288. error(arg, "'#load' expected a constant string argument");
  289. return false;
  290. }
  291. if (!is_type_string(o.type)) {
  292. gbString str = type_to_string(o.type);
  293. error(arg, "'#load' expected a constant string, got %s", str);
  294. gb_string_free(str);
  295. return false;
  296. }
  297. gbAllocator a = heap_allocator();
  298. GB_ASSERT(o.value.kind == ExactValue_String);
  299. String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
  300. String original_string = o.value.value_string;
  301. gbMutex *ignore_mutex = nullptr;
  302. String path = {};
  303. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  304. char *c_str = alloc_cstring(a, path);
  305. defer (gb_free(a, c_str));
  306. gbFile f = {};
  307. gbFileError file_err = gb_file_open(&f, c_str);
  308. defer (gb_file_close(&f));
  309. switch (file_err) {
  310. default:
  311. case gbFileError_Invalid:
  312. error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
  313. return false;
  314. case gbFileError_NotExists:
  315. error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
  316. return false;
  317. case gbFileError_Permission:
  318. error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
  319. return false;
  320. case gbFileError_None:
  321. // Okay
  322. break;
  323. }
  324. String result = {};
  325. isize file_size = cast(isize)gb_file_size(&f);
  326. if (file_size > 0) {
  327. u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
  328. gb_file_read_at(&f, data, file_size, 0);
  329. data[file_size] = '\0';
  330. result.text = data;
  331. result.len = file_size;
  332. }
  333. operand->type = t_u8_slice;
  334. operand->mode = Addressing_Constant;
  335. operand->value = exact_value_string(result);
  336. } else if (name == "assert") {
  337. if (ce->args.count != 1) {
  338. error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
  339. return false;
  340. }
  341. if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
  342. gbString str = expr_to_string(ce->args[0]);
  343. error(call, "'%s' is not a constant boolean", str);
  344. gb_string_free(str);
  345. return false;
  346. }
  347. if (!operand->value.value_bool) {
  348. gbString arg = expr_to_string(ce->args[0]);
  349. error(call, "Compile time assertion: %s", arg);
  350. if (c->proc_name != "") {
  351. gbString str = type_to_string(c->curr_proc_sig);
  352. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  353. gb_string_free(str);
  354. }
  355. gb_string_free(arg);
  356. }
  357. operand->type = t_untyped_bool;
  358. operand->mode = Addressing_Constant;
  359. } else if (name == "panic") {
  360. if (ce->args.count != 1) {
  361. error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
  362. return false;
  363. }
  364. if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
  365. gbString str = expr_to_string(ce->args[0]);
  366. error(call, "'%s' is not a constant string", str);
  367. gb_string_free(str);
  368. return false;
  369. }
  370. error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
  371. if (c->proc_name != "") {
  372. gbString str = type_to_string(c->curr_proc_sig);
  373. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  374. gb_string_free(str);
  375. }
  376. operand->type = t_invalid;
  377. operand->mode = Addressing_NoValue;
  378. } else if (name == "defined") {
  379. if (ce->args.count != 1) {
  380. error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
  381. return false;
  382. }
  383. Ast *arg = unparen_expr(ce->args[0]);
  384. if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
  385. error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
  386. return false;
  387. }
  388. if (c->curr_proc_decl == nullptr) {
  389. error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
  390. return false;
  391. }
  392. bool is_defined = check_identifier_exists(c->scope, arg);
  393. operand->type = t_untyped_bool;
  394. operand->mode = Addressing_Constant;
  395. operand->value = exact_value_bool(false);
  396. } else if (name == "config") {
  397. if (ce->args.count != 2) {
  398. error(call, "'#config' expects 2 argument, got %td", ce->args.count);
  399. return false;
  400. }
  401. Ast *arg = unparen_expr(ce->args[0]);
  402. if (arg == nullptr || arg->kind != Ast_Ident) {
  403. error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
  404. return false;
  405. }
  406. Ast *def_arg = unparen_expr(ce->args[1]);
  407. Operand def = {};
  408. check_expr(c, &def, def_arg);
  409. if (def.mode != Addressing_Constant) {
  410. error(def_arg, "'#config' default value must be a constant");
  411. return false;
  412. }
  413. String name = arg->Ident.token.string;
  414. operand->type = def.type;
  415. operand->mode = def.mode;
  416. operand->value = def.value;
  417. Entity *found = scope_lookup_current(config_pkg->scope, name);
  418. if (found != nullptr) {
  419. if (found->kind != Entity_Constant) {
  420. error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
  421. } else {
  422. operand->type = found->type;
  423. operand->mode = Addressing_Constant;
  424. operand->value = found->Constant.value;
  425. }
  426. }
  427. } else {
  428. GB_PANIC("Unhandled #%.*s", LIT(name));
  429. }
  430. break;
  431. }
  432. case BuiltinProc_len:
  433. check_expr_or_type(c, operand, ce->args[0]);
  434. if (operand->mode == Addressing_Invalid) {
  435. return false;
  436. }
  437. /* fallthrough */
  438. case BuiltinProc_cap:
  439. {
  440. // len :: proc(Type) -> int
  441. // cap :: proc(Type) -> int
  442. Type *op_type = type_deref(operand->type);
  443. Type *type = t_int;
  444. AddressingMode mode = Addressing_Invalid;
  445. ExactValue value = {};
  446. if (is_type_string(op_type) && id == BuiltinProc_len) {
  447. if (operand->mode == Addressing_Constant) {
  448. mode = Addressing_Constant;
  449. String str = operand->value.value_string;
  450. value = exact_value_i64(str.len);
  451. type = t_untyped_integer;
  452. } else {
  453. mode = Addressing_Value;
  454. if (is_type_cstring(op_type)) {
  455. add_package_dependency(c, "runtime", "cstring_len");
  456. }
  457. }
  458. } else if (is_type_array(op_type)) {
  459. Type *at = core_type(op_type);
  460. mode = Addressing_Constant;
  461. value = exact_value_i64(at->Array.count);
  462. type = t_untyped_integer;
  463. } else if (is_type_enumerated_array(op_type) && id == BuiltinProc_len) {
  464. Type *at = core_type(op_type);
  465. mode = Addressing_Constant;
  466. value = exact_value_i64(at->EnumeratedArray.count);
  467. type = t_untyped_integer;
  468. } else if (is_type_slice(op_type) && id == BuiltinProc_len) {
  469. mode = Addressing_Value;
  470. } else if (is_type_dynamic_array(op_type)) {
  471. mode = Addressing_Value;
  472. } else if (is_type_map(op_type)) {
  473. mode = Addressing_Value;
  474. } else if (operand->mode == Addressing_Type && is_type_enum(op_type) && id == BuiltinProc_len) {
  475. Type *bt = base_type(op_type);
  476. mode = Addressing_Constant;
  477. value = exact_value_i64(bt->Enum.fields.count);
  478. type = t_untyped_integer;
  479. } else if (is_type_struct(op_type)) {
  480. Type *bt = base_type(op_type);
  481. if (bt->Struct.soa_kind == StructSoa_Fixed) {
  482. mode = Addressing_Constant;
  483. value = exact_value_i64(bt->Struct.soa_count);
  484. type = t_untyped_integer;
  485. } else if ((bt->Struct.soa_kind == StructSoa_Slice && id == BuiltinProc_len) ||
  486. bt->Struct.soa_kind == StructSoa_Dynamic) {
  487. mode = Addressing_Value;
  488. }
  489. }
  490. if (operand->mode == Addressing_Type && mode != Addressing_Constant) {
  491. mode = Addressing_Invalid;
  492. }
  493. if (mode == Addressing_Invalid) {
  494. gbString t = type_to_string(operand->type);
  495. error(call, "'%.*s' is not supported for '%s'", LIT(builtin_name), t);
  496. return false;
  497. }
  498. operand->mode = mode;
  499. operand->value = value;
  500. operand->type = type;
  501. break;
  502. }
  503. case BuiltinProc_size_of: {
  504. // size_of :: proc(Type or expr) -> untyped int
  505. Operand o = {};
  506. check_expr_or_type(c, &o, ce->args[0]);
  507. if (o.mode == Addressing_Invalid) {
  508. return false;
  509. }
  510. Type *t = o.type;
  511. if (t == nullptr || t == t_invalid) {
  512. error(ce->args[0], "Invalid argument for 'size_of'");
  513. return false;
  514. }
  515. t = default_type(t);
  516. operand->mode = Addressing_Constant;
  517. operand->value = exact_value_i64(type_size_of(t));
  518. operand->type = t_untyped_integer;
  519. break;
  520. }
  521. case BuiltinProc_align_of: {
  522. // align_of :: proc(Type or expr) -> untyped int
  523. Operand o = {};
  524. check_expr_or_type(c, &o, ce->args[0]);
  525. if (o.mode == Addressing_Invalid) {
  526. return false;
  527. }
  528. Type *t = o.type;
  529. if (t == nullptr || t == t_invalid) {
  530. error(ce->args[0], "Invalid argument for 'align_of'");
  531. return false;
  532. }
  533. t = default_type(t);
  534. operand->mode = Addressing_Constant;
  535. operand->value = exact_value_i64(type_align_of(t));
  536. operand->type = t_untyped_integer;
  537. break;
  538. }
  539. case BuiltinProc_offset_of: {
  540. // offset_of :: proc(value.field) -> uintptr
  541. // offset_of :: proc(Type, field) -> uintptr
  542. Type *type = nullptr;
  543. Ast *field_arg = nullptr;
  544. if (ce->args.count == 1) {
  545. Ast *arg0 = unparen_expr(ce->args[0]);
  546. if (arg0->kind != Ast_SelectorExpr) {
  547. gbString x = expr_to_string(arg0);
  548. error(ce->args[0], "Invalid expression for 'offset_of', '%s' is not a selector expression", x);
  549. gb_string_free(x);
  550. return false;
  551. }
  552. ast_node(se, SelectorExpr, arg0);
  553. Operand x = {};
  554. check_expr(c, &x, se->expr);
  555. if (x.mode == Addressing_Invalid) {
  556. return false;
  557. }
  558. type = type_deref(x.type);
  559. Type *bt = base_type(type);
  560. if (bt == nullptr || bt == t_invalid) {
  561. error(ce->args[0], "Expected a type for 'offset_of'");
  562. return false;
  563. }
  564. field_arg = unparen_expr(se->selector);
  565. } else if (ce->args.count == 2) {
  566. type = check_type(c, ce->args[0]);
  567. Type *bt = base_type(type);
  568. if (bt == nullptr || bt == t_invalid) {
  569. error(ce->args[0], "Expected a type for 'offset_of'");
  570. return false;
  571. }
  572. field_arg = unparen_expr(ce->args[1]);
  573. } else {
  574. error(ce->args[0], "Expected either 1 or 2 arguments to 'offset_of', in the format of 'offset_of(Type, field)', 'offset_of(value.field)'");
  575. return false;
  576. }
  577. GB_ASSERT(type != nullptr);
  578. if (field_arg == nullptr ||
  579. field_arg->kind != Ast_Ident) {
  580. error(field_arg, "Expected an identifier for field argument");
  581. return false;
  582. }
  583. if (is_type_array(type)) {
  584. gbString t = type_to_string(type);
  585. error(field_arg, "Invalid a struct type for 'offset_of', got '%s'", t);
  586. gb_string_free(t);
  587. return false;
  588. }
  589. ast_node(arg, Ident, field_arg);
  590. String field_name = arg->token.string;
  591. Selection sel = lookup_field(type, field_name, false);
  592. if (sel.entity == nullptr) {
  593. gbString type_str = type_to_string(type);
  594. error(ce->args[0],
  595. "'%s' has no field named '%.*s'", type_str, LIT(arg->token.string));
  596. gb_string_free(type_str);
  597. return false;
  598. }
  599. if (sel.indirect) {
  600. gbString type_str = type_to_string(type);
  601. error(ce->args[0],
  602. "Field '%.*s' is embedded via a pointer in '%s'", LIT(arg->token.string), type_str);
  603. gb_string_free(type_str);
  604. return false;
  605. }
  606. operand->mode = Addressing_Constant;
  607. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  608. operand->type = t_uintptr;
  609. break;
  610. }
  611. case BuiltinProc_type_of: {
  612. // type_of :: proc(val: Type) -> type(Type)
  613. Ast *expr = ce->args[0];
  614. Operand o = {};
  615. check_expr_or_type(c, &o, expr);
  616. // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
  617. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  618. return false;
  619. }
  620. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  621. error(o.expr, "Invalid argument to 'type_of'");
  622. return false;
  623. }
  624. // NOTE(bill): Prevent type cycles for procedure declarations
  625. if (c->curr_proc_sig == o.type) {
  626. gbString s = expr_to_string(o.expr);
  627. error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
  628. gb_string_free(s);
  629. return false;
  630. }
  631. if (is_type_polymorphic(o.type)) {
  632. error(o.expr, "'type_of' of polymorphic type cannot be determined");
  633. return false;
  634. }
  635. operand->mode = Addressing_Type;
  636. operand->type = o.type;
  637. break;
  638. }
  639. case BuiltinProc_type_info_of: {
  640. // type_info_of :: proc(Type) -> ^Type_Info
  641. if (c->scope->flags&ScopeFlag_Global) {
  642. compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  643. }
  644. // NOTE(bill): The type information may not be setup yet
  645. init_core_type_info(c->checker);
  646. Ast *expr = ce->args[0];
  647. Operand o = {};
  648. check_expr_or_type(c, &o, expr);
  649. if (o.mode == Addressing_Invalid) {
  650. return false;
  651. }
  652. Type *t = o.type;
  653. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(t)) {
  654. if (is_type_polymorphic(t)) {
  655. error(ce->args[0], "Invalid argument for 'type_info_of', unspecialized polymorphic type");
  656. } else {
  657. error(ce->args[0], "Invalid argument for 'type_info_of'");
  658. }
  659. return false;
  660. }
  661. t = default_type(t);
  662. add_type_info_type(c, t);
  663. if (is_operand_value(o) && is_type_typeid(t)) {
  664. add_package_dependency(c, "runtime", "__type_info_of");
  665. } else if (o.mode != Addressing_Type) {
  666. error(expr, "Expected a type or typeid for 'type_info_of'");
  667. return false;
  668. }
  669. operand->mode = Addressing_Value;
  670. operand->type = t_type_info_ptr;
  671. break;
  672. }
  673. case BuiltinProc_typeid_of: {
  674. // typeid_of :: proc(Type) -> typeid
  675. if (c->scope->flags&ScopeFlag_Global) {
  676. compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  677. }
  678. // NOTE(bill): The type information may not be setup yet
  679. init_core_type_info(c->checker);
  680. Ast *expr = ce->args[0];
  681. Operand o = {};
  682. check_expr_or_type(c, &o, expr);
  683. if (o.mode == Addressing_Invalid) {
  684. return false;
  685. }
  686. Type *t = o.type;
  687. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(operand->type)) {
  688. error(ce->args[0], "Invalid argument for 'typeid_of'");
  689. return false;
  690. }
  691. t = default_type(t);
  692. add_type_info_type(c, t);
  693. if (o.mode != Addressing_Type) {
  694. error(expr, "Expected a type for 'typeid_of'");
  695. return false;
  696. }
  697. operand->mode = Addressing_Value;
  698. operand->type = t_typeid;
  699. operand->value = exact_value_typeid(t);
  700. break;
  701. }
  702. case BuiltinProc_swizzle: {
  703. // swizzle :: proc(v: [N]T, ..int) -> [M]T
  704. Type *type = base_type(operand->type);
  705. i64 max_count = 0;
  706. Type *elem_type = nullptr;
  707. if (!is_type_array(type) && !is_type_simd_vector(type)) {
  708. gbString type_str = type_to_string(operand->type);
  709. error(call,
  710. "'swizzle' is only allowed on an array or #simd vector, got '%s'",
  711. type_str);
  712. gb_string_free(type_str);
  713. return false;
  714. }
  715. if (type->kind == Type_Array) {
  716. max_count = type->Array.count;
  717. elem_type = type->Array.elem;
  718. } else if (type->kind == Type_SimdVector) {
  719. max_count = type->SimdVector.count;
  720. elem_type = type->SimdVector.elem;
  721. }
  722. i64 arg_count = 0;
  723. for_array(i, ce->args) {
  724. if (i == 0) {
  725. continue;
  726. }
  727. Ast *arg = ce->args[i];
  728. Operand op = {};
  729. check_expr(c, &op, arg);
  730. if (op.mode == Addressing_Invalid) {
  731. return false;
  732. }
  733. Type *arg_type = base_type(op.type);
  734. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  735. error(op.expr, "Indices to 'swizzle' must be constant integers");
  736. return false;
  737. }
  738. if (op.value.value_integer.neg) {
  739. error(op.expr, "Negative 'swizzle' index");
  740. return false;
  741. }
  742. BigInt mc = {};
  743. big_int_from_i64(&mc, max_count);
  744. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  745. error(op.expr, "'swizzle' index exceeds length");
  746. return false;
  747. }
  748. arg_count++;
  749. }
  750. if (arg_count > max_count) {
  751. error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
  752. return false;
  753. }
  754. if (arg_count < max_count) {
  755. operand->type = alloc_type_array(elem_type, arg_count);
  756. }
  757. operand->mode = Addressing_Value;
  758. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  759. operand->type = type_hint;
  760. }
  761. break;
  762. }
  763. case BuiltinProc_complex: {
  764. // complex :: proc(real, imag: float_type) -> complex_type
  765. Operand x = *operand;
  766. Operand y = {};
  767. // NOTE(bill): Invalid will be the default till fixed
  768. operand->type = t_invalid;
  769. operand->mode = Addressing_Invalid;
  770. check_expr(c, &y, ce->args[1]);
  771. if (y.mode == Addressing_Invalid) {
  772. return false;
  773. }
  774. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  775. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  776. if (x.mode == Addressing_Constant &&
  777. y.mode == Addressing_Constant) {
  778. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  779. x.type = t_untyped_float;
  780. }
  781. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  782. y.type = t_untyped_float;
  783. }
  784. }
  785. if (!are_types_identical(x.type, y.type)) {
  786. gbString tx = type_to_string(x.type);
  787. gbString ty = type_to_string(y.type);
  788. error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
  789. gb_string_free(ty);
  790. gb_string_free(tx);
  791. return false;
  792. }
  793. if (!is_type_float(x.type)) {
  794. gbString s = type_to_string(x.type);
  795. error(call, "Arguments have type '%s', expected a floating point", s);
  796. gb_string_free(s);
  797. return false;
  798. }
  799. if (is_type_endian_specific(x.type)) {
  800. gbString s = type_to_string(x.type);
  801. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  802. gb_string_free(s);
  803. return false;
  804. }
  805. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  806. f64 r = exact_value_to_float(x.value).value_float;
  807. f64 i = exact_value_to_float(y.value).value_float;
  808. operand->value = exact_value_complex(r, i);
  809. operand->mode = Addressing_Constant;
  810. } else {
  811. operand->mode = Addressing_Value;
  812. }
  813. BasicKind kind = core_type(x.type)->Basic.kind;
  814. switch (kind) {
  815. case Basic_f16: operand->type = t_complex32; break;
  816. case Basic_f32: operand->type = t_complex64; break;
  817. case Basic_f64: operand->type = t_complex128; break;
  818. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  819. default: GB_PANIC("Invalid type"); break;
  820. }
  821. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  822. operand->type = type_hint;
  823. }
  824. break;
  825. }
  826. case BuiltinProc_quaternion: {
  827. // quaternion :: proc(real, imag, jmag, kmag: float_type) -> complex_type
  828. Operand x = *operand;
  829. Operand y = {};
  830. Operand z = {};
  831. Operand w = {};
  832. // NOTE(bill): Invalid will be the default till fixed
  833. operand->type = t_invalid;
  834. operand->mode = Addressing_Invalid;
  835. check_expr(c, &y, ce->args[1]);
  836. if (y.mode == Addressing_Invalid) {
  837. return false;
  838. }
  839. check_expr(c, &z, ce->args[2]);
  840. if (y.mode == Addressing_Invalid) {
  841. return false;
  842. }
  843. check_expr(c, &w, ce->args[3]);
  844. if (y.mode == Addressing_Invalid) {
  845. return false;
  846. }
  847. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  848. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  849. convert_to_typed(c, &z, x.type); if (z.mode == Addressing_Invalid) return false;
  850. convert_to_typed(c, &w, x.type); if (w.mode == Addressing_Invalid) return false;
  851. if (x.mode == Addressing_Constant &&
  852. y.mode == Addressing_Constant &&
  853. z.mode == Addressing_Constant &&
  854. w.mode == Addressing_Constant) {
  855. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  856. x.type = t_untyped_float;
  857. }
  858. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  859. y.type = t_untyped_float;
  860. }
  861. if (is_type_numeric(z.type) && exact_value_imag(z.value).value_float == 0) {
  862. z.type = t_untyped_float;
  863. }
  864. if (is_type_numeric(w.type) && exact_value_imag(w.value).value_float == 0) {
  865. w.type = t_untyped_float;
  866. }
  867. }
  868. if (!(are_types_identical(x.type, y.type) && are_types_identical(x.type, z.type) && are_types_identical(x.type, w.type))) {
  869. gbString tx = type_to_string(x.type);
  870. gbString ty = type_to_string(y.type);
  871. gbString tz = type_to_string(z.type);
  872. gbString tw = type_to_string(w.type);
  873. error(call, "Mismatched types to 'quaternion', '%s' vs '%s' vs '%s' vs '%s'", tx, ty, tz, tw);
  874. gb_string_free(tw);
  875. gb_string_free(tz);
  876. gb_string_free(ty);
  877. gb_string_free(tx);
  878. return false;
  879. }
  880. if (!is_type_float(x.type)) {
  881. gbString s = type_to_string(x.type);
  882. error(call, "Arguments have type '%s', expected a floating point", s);
  883. gb_string_free(s);
  884. return false;
  885. }
  886. if (is_type_endian_specific(x.type)) {
  887. gbString s = type_to_string(x.type);
  888. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  889. gb_string_free(s);
  890. return false;
  891. }
  892. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant && z.mode == Addressing_Constant && w.mode == Addressing_Constant) {
  893. f64 r = exact_value_to_float(x.value).value_float;
  894. f64 i = exact_value_to_float(y.value).value_float;
  895. f64 j = exact_value_to_float(z.value).value_float;
  896. f64 k = exact_value_to_float(w.value).value_float;
  897. operand->value = exact_value_quaternion(r, i, j, k);
  898. operand->mode = Addressing_Constant;
  899. } else {
  900. operand->mode = Addressing_Value;
  901. }
  902. BasicKind kind = core_type(x.type)->Basic.kind;
  903. switch (kind) {
  904. case Basic_f16: operand->type = t_quaternion64; break;
  905. case Basic_f32: operand->type = t_quaternion128; break;
  906. case Basic_f64: operand->type = t_quaternion256; break;
  907. case Basic_UntypedFloat: operand->type = t_untyped_quaternion; break;
  908. default: GB_PANIC("Invalid type"); break;
  909. }
  910. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  911. operand->type = type_hint;
  912. }
  913. break;
  914. }
  915. case BuiltinProc_real:
  916. case BuiltinProc_imag: {
  917. // real :: proc(x: type) -> float_type
  918. // imag :: proc(x: type) -> float_type
  919. Operand *x = operand;
  920. if (is_type_untyped(x->type)) {
  921. if (x->mode == Addressing_Constant) {
  922. if (is_type_numeric(x->type)) {
  923. x->type = t_untyped_complex;
  924. }
  925. } else if (is_type_quaternion(x->type)) {
  926. convert_to_typed(c, x, t_quaternion256);
  927. if (x->mode == Addressing_Invalid) {
  928. return false;
  929. }
  930. } else{
  931. convert_to_typed(c, x, t_complex128);
  932. if (x->mode == Addressing_Invalid) {
  933. return false;
  934. }
  935. }
  936. }
  937. if (!is_type_complex(x->type) && !is_type_quaternion(x->type)) {
  938. gbString s = type_to_string(x->type);
  939. error(call, "Argument has type '%s', expected a complex or quaternion type", s);
  940. gb_string_free(s);
  941. return false;
  942. }
  943. if (x->mode == Addressing_Constant) {
  944. switch (id) {
  945. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  946. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  947. }
  948. } else {
  949. x->mode = Addressing_Value;
  950. }
  951. BasicKind kind = core_type(x->type)->Basic.kind;
  952. switch (kind) {
  953. case Basic_complex32: x->type = t_f16; break;
  954. case Basic_complex64: x->type = t_f32; break;
  955. case Basic_complex128: x->type = t_f64; break;
  956. case Basic_quaternion64: x->type = t_f16; break;
  957. case Basic_quaternion128: x->type = t_f32; break;
  958. case Basic_quaternion256: x->type = t_f64; break;
  959. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  960. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  961. default: GB_PANIC("Invalid type"); break;
  962. }
  963. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  964. operand->type = type_hint;
  965. }
  966. break;
  967. }
  968. case BuiltinProc_jmag:
  969. case BuiltinProc_kmag: {
  970. // jmag :: proc(x: type) -> float_type
  971. // kmag :: proc(x: type) -> float_type
  972. Operand *x = operand;
  973. if (is_type_untyped(x->type)) {
  974. if (x->mode == Addressing_Constant) {
  975. if (is_type_numeric(x->type)) {
  976. x->type = t_untyped_complex;
  977. }
  978. } else{
  979. convert_to_typed(c, x, t_quaternion256);
  980. if (x->mode == Addressing_Invalid) {
  981. return false;
  982. }
  983. }
  984. }
  985. if (!is_type_quaternion(x->type)) {
  986. gbString s = type_to_string(x->type);
  987. error(call, "Argument has type '%s', expected a quaternion type", s);
  988. gb_string_free(s);
  989. return false;
  990. }
  991. if (x->mode == Addressing_Constant) {
  992. switch (id) {
  993. case BuiltinProc_jmag: x->value = exact_value_jmag(x->value); break;
  994. case BuiltinProc_kmag: x->value = exact_value_kmag(x->value); break;
  995. }
  996. } else {
  997. x->mode = Addressing_Value;
  998. }
  999. BasicKind kind = core_type(x->type)->Basic.kind;
  1000. switch (kind) {
  1001. case Basic_quaternion64: x->type = t_f16; break;
  1002. case Basic_quaternion128: x->type = t_f32; break;
  1003. case Basic_quaternion256: x->type = t_f64; break;
  1004. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  1005. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  1006. default: GB_PANIC("Invalid type"); break;
  1007. }
  1008. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  1009. operand->type = type_hint;
  1010. }
  1011. break;
  1012. }
  1013. case BuiltinProc_conj: {
  1014. // conj :: proc(x: type) -> type
  1015. Operand *x = operand;
  1016. if (is_type_complex(x->type)) {
  1017. if (x->mode == Addressing_Constant) {
  1018. ExactValue v = exact_value_to_complex(x->value);
  1019. f64 r = v.value_complex->real;
  1020. f64 i = -v.value_complex->imag;
  1021. x->value = exact_value_complex(r, i);
  1022. x->mode = Addressing_Constant;
  1023. } else {
  1024. x->mode = Addressing_Value;
  1025. }
  1026. } else if (is_type_quaternion(x->type)) {
  1027. if (x->mode == Addressing_Constant) {
  1028. ExactValue v = exact_value_to_quaternion(x->value);
  1029. f64 r = +v.value_quaternion->real;
  1030. f64 i = -v.value_quaternion->imag;
  1031. f64 j = -v.value_quaternion->jmag;
  1032. f64 k = -v.value_quaternion->kmag;
  1033. x->value = exact_value_quaternion(r, i, j, k);
  1034. x->mode = Addressing_Constant;
  1035. } else {
  1036. x->mode = Addressing_Value;
  1037. }
  1038. } else {
  1039. gbString s = type_to_string(x->type);
  1040. error(call, "Expected a complex or quaternion, got '%s'", s);
  1041. gb_string_free(s);
  1042. return false;
  1043. }
  1044. break;
  1045. }
  1046. case BuiltinProc_expand_to_tuple: {
  1047. Type *type = base_type(operand->type);
  1048. if (!is_type_struct(type) && !is_type_array(type)) {
  1049. gbString type_str = type_to_string(operand->type);
  1050. error(call, "Expected a struct or array type, got '%s'", type_str);
  1051. gb_string_free(type_str);
  1052. return false;
  1053. }
  1054. gbAllocator a = permanent_allocator();
  1055. Type *tuple = alloc_type_tuple();
  1056. if (is_type_struct(type)) {
  1057. isize variable_count = type->Struct.fields.count;
  1058. array_init(&tuple->Tuple.variables, a, variable_count);
  1059. // TODO(bill): Should I copy each of the entities or is this good enough?
  1060. gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
  1061. } else if (is_type_array(type)) {
  1062. isize variable_count = type->Array.count;
  1063. array_init(&tuple->Tuple.variables, a, variable_count);
  1064. for (isize i = 0; i < variable_count; i++) {
  1065. tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
  1066. }
  1067. }
  1068. operand->type = tuple;
  1069. operand->mode = Addressing_Value;
  1070. if (tuple->Tuple.variables.count == 1) {
  1071. operand->type = tuple->Tuple.variables[0]->type;
  1072. }
  1073. break;
  1074. }
  1075. case BuiltinProc_min: {
  1076. // min :: proc($T: typeid) -> ordered
  1077. // min :: proc(a: ..ordered) -> ordered
  1078. check_multi_expr_or_type(c, operand, ce->args[0]);
  1079. Type *original_type = operand->type;
  1080. Type *type = base_type(operand->type);
  1081. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  1082. // Okay
  1083. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1084. gbString type_str = type_to_string(original_type);
  1085. error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
  1086. gb_string_free(type_str);
  1087. return false;
  1088. }
  1089. if (operand->mode == Addressing_Type) {
  1090. if (ce->args.count != 1) {
  1091. error(call, "If 'min' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  1092. return false;
  1093. }
  1094. if (is_type_boolean(type)) {
  1095. operand->mode = Addressing_Constant;
  1096. operand->type = original_type;
  1097. operand->value = exact_value_bool(false);
  1098. return true;
  1099. } else if (is_type_integer(type)) {
  1100. operand->mode = Addressing_Constant;
  1101. operand->type = original_type;
  1102. if (is_type_unsigned(type)) {
  1103. operand->value = exact_value_u64(0);
  1104. return true;
  1105. } else {
  1106. i64 sz = 8*type_size_of(type);
  1107. ExactValue a = exact_value_i64(1);
  1108. ExactValue b = exact_value_i64(sz-1);
  1109. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1110. v = exact_unary_operator_value(Token_Sub, v, cast(i32)sz, false);
  1111. operand->value = v;
  1112. return true;
  1113. }
  1114. } else if (is_type_float(type)) {
  1115. operand->mode = Addressing_Constant;
  1116. operand->type = original_type;
  1117. switch (type_size_of(type)) {
  1118. case 2:
  1119. operand->value = exact_value_float(-65504.0f);
  1120. break;
  1121. case 4:
  1122. operand->value = exact_value_float(-3.402823466e+38f);
  1123. break;
  1124. case 8:
  1125. operand->value = exact_value_float(-1.7976931348623158e+308);
  1126. break;
  1127. default:
  1128. GB_PANIC("Unhandled float type");
  1129. break;
  1130. }
  1131. return true;
  1132. } else if (is_type_enum(type)) {
  1133. operand->mode = Addressing_Constant;
  1134. operand->type = original_type;
  1135. operand->value = type->Enum.min_value;
  1136. return true;
  1137. } else if (is_type_enumerated_array(type)) {
  1138. Type *bt = base_type(type);
  1139. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  1140. operand->mode = Addressing_Constant;
  1141. operand->type = bt->EnumeratedArray.index;
  1142. operand->value = bt->EnumeratedArray.min_value;
  1143. return true;
  1144. }
  1145. gbString type_str = type_to_string(original_type);
  1146. error(call, "Invalid type for 'min', got %s", type_str);
  1147. gb_string_free(type_str);
  1148. return false;
  1149. }
  1150. bool all_constant = operand->mode == Addressing_Constant;
  1151. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  1152. defer (array_free(&operands));
  1153. array_add(&operands, *operand);
  1154. for (isize i = 1; i < ce->args.count; i++) {
  1155. Ast *other_arg = ce->args[i];
  1156. Operand b = {};
  1157. check_expr(c, &b, other_arg);
  1158. if (b.mode == Addressing_Invalid) {
  1159. return false;
  1160. }
  1161. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  1162. gbString type_str = type_to_string(b.type);
  1163. error(call,
  1164. "Expected a ordered numeric type to 'min', got '%s'",
  1165. type_str);
  1166. gb_string_free(type_str);
  1167. return false;
  1168. }
  1169. array_add(&operands, b);
  1170. if (all_constant) {
  1171. all_constant = b.mode == Addressing_Constant;
  1172. }
  1173. }
  1174. if (all_constant) {
  1175. ExactValue value = operands[0].value;
  1176. Type *type = operands[0].type;
  1177. for (isize i = 1; i < operands.count; i++) {
  1178. Operand y = operands[i];
  1179. if (compare_exact_values(Token_Lt, value, y.value)) {
  1180. // okay
  1181. } else {
  1182. value = y.value;
  1183. type = y.type;
  1184. }
  1185. }
  1186. operand->value = value;
  1187. operand->type = type;
  1188. } else {
  1189. operand->mode = Addressing_Value;
  1190. operand->type = original_type;
  1191. for_array(i, operands) {
  1192. Operand *a = &operands[i];
  1193. for_array(j, operands) {
  1194. if (i == j) {
  1195. continue;
  1196. }
  1197. Operand *b = &operands[j];
  1198. convert_to_typed(c, a, b->type);
  1199. if (a->mode == Addressing_Invalid) {
  1200. return false;
  1201. }
  1202. convert_to_typed(c, b, a->type);
  1203. if (b->mode == Addressing_Invalid) {
  1204. return false;
  1205. }
  1206. }
  1207. }
  1208. for (isize i = 0; i < operands.count-1; i++) {
  1209. Operand *a = &operands[i];
  1210. Operand *b = &operands[i+1];
  1211. if (!are_types_identical(a->type, b->type)) {
  1212. gbString type_a = type_to_string(a->type);
  1213. gbString type_b = type_to_string(b->type);
  1214. error(a->expr,
  1215. "Mismatched types to 'min', '%s' vs '%s'",
  1216. type_a, type_b);
  1217. gb_string_free(type_b);
  1218. gb_string_free(type_a);
  1219. return false;
  1220. }
  1221. }
  1222. operand->type = operands[0].type;
  1223. }
  1224. break;
  1225. }
  1226. case BuiltinProc_max: {
  1227. // max :: proc($T: typeid) -> ordered
  1228. // max :: proc(a: ..ordered) -> ordered
  1229. check_multi_expr_or_type(c, operand, ce->args[0]);
  1230. Type *original_type = operand->type;
  1231. Type *type = base_type(operand->type);
  1232. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  1233. // Okay
  1234. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1235. gbString type_str = type_to_string(original_type);
  1236. error(call, "Expected a ordered numeric type to 'max', got '%s'", type_str);
  1237. gb_string_free(type_str);
  1238. return false;
  1239. }
  1240. if (operand->mode == Addressing_Type) {
  1241. if (ce->args.count != 1) {
  1242. error(call, "If 'max' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  1243. return false;
  1244. }
  1245. if (is_type_boolean(type)) {
  1246. operand->mode = Addressing_Constant;
  1247. operand->type = original_type;
  1248. operand->value = exact_value_bool(true);
  1249. return true;
  1250. } else if (is_type_integer(type)) {
  1251. operand->mode = Addressing_Constant;
  1252. operand->type = original_type;
  1253. if (is_type_unsigned(type)) {
  1254. i64 sz = 8*type_size_of(type);
  1255. ExactValue a = exact_value_i64(1);
  1256. ExactValue b = exact_value_i64(sz);
  1257. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1258. v = exact_binary_operator_value(Token_Sub, v, a);
  1259. operand->value = v;
  1260. return true;
  1261. } else {
  1262. i64 sz = 8*type_size_of(type);
  1263. ExactValue a = exact_value_i64(1);
  1264. ExactValue b = exact_value_i64(sz-1);
  1265. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  1266. v = exact_binary_operator_value(Token_Sub, v, a);
  1267. operand->value = v;
  1268. return true;
  1269. }
  1270. } else if (is_type_float(type)) {
  1271. operand->mode = Addressing_Constant;
  1272. operand->type = original_type;
  1273. switch (type_size_of(type)) {
  1274. case 2:
  1275. operand->value = exact_value_float(65504.0f);
  1276. break;
  1277. case 4:
  1278. operand->value = exact_value_float(3.402823466e+38f);
  1279. break;
  1280. case 8:
  1281. operand->value = exact_value_float(1.7976931348623158e+308);
  1282. break;
  1283. default:
  1284. GB_PANIC("Unhandled float type");
  1285. break;
  1286. }
  1287. return true;
  1288. } else if (is_type_enum(type)) {
  1289. operand->mode = Addressing_Constant;
  1290. operand->type = original_type;
  1291. operand->value = type->Enum.max_value;
  1292. return true;
  1293. } else if (is_type_enumerated_array(type)) {
  1294. Type *bt = base_type(type);
  1295. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  1296. operand->mode = Addressing_Constant;
  1297. operand->type = bt->EnumeratedArray.index;
  1298. operand->value = bt->EnumeratedArray.max_value;
  1299. return true;
  1300. }
  1301. gbString type_str = type_to_string(original_type);
  1302. error(call, "Invalid type for 'max', got %s", type_str);
  1303. gb_string_free(type_str);
  1304. return false;
  1305. }
  1306. bool all_constant = operand->mode == Addressing_Constant;
  1307. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  1308. defer (array_free(&operands));
  1309. array_add(&operands, *operand);
  1310. for (isize i = 1; i < ce->args.count; i++) {
  1311. Ast *arg = ce->args[i];
  1312. Operand b = {};
  1313. check_expr(c, &b, arg);
  1314. if (b.mode == Addressing_Invalid) {
  1315. return false;
  1316. }
  1317. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  1318. gbString type_str = type_to_string(b.type);
  1319. error(arg,
  1320. "Expected a ordered numeric type to 'max', got '%s'",
  1321. type_str);
  1322. gb_string_free(type_str);
  1323. return false;
  1324. }
  1325. array_add(&operands, b);
  1326. if (all_constant) {
  1327. all_constant = b.mode == Addressing_Constant;
  1328. }
  1329. }
  1330. if (all_constant) {
  1331. ExactValue value = operands[0].value;
  1332. Type *type = operands[0].type;
  1333. for (isize i = 1; i < operands.count; i++) {
  1334. Operand y = operands[i];
  1335. if (compare_exact_values(Token_Gt, value, y.value)) {
  1336. // okay
  1337. } else {
  1338. type = y.type;
  1339. value = y.value;
  1340. }
  1341. }
  1342. operand->value = value;
  1343. operand->type = type;
  1344. } else {
  1345. operand->mode = Addressing_Value;
  1346. operand->type = original_type;
  1347. for_array(i, operands) {
  1348. Operand *a = &operands[i];
  1349. for_array(j, operands) {
  1350. if (i == j) {
  1351. continue;
  1352. }
  1353. Operand *b = &operands[j];
  1354. convert_to_typed(c, a, b->type);
  1355. if (a->mode == Addressing_Invalid) {
  1356. return false;
  1357. }
  1358. convert_to_typed(c, b, a->type);
  1359. if (b->mode == Addressing_Invalid) {
  1360. return false;
  1361. }
  1362. }
  1363. }
  1364. for (isize i = 0; i < operands.count-1; i++) {
  1365. Operand *a = &operands[i];
  1366. Operand *b = &operands[i+1];
  1367. if (!are_types_identical(a->type, b->type)) {
  1368. gbString type_a = type_to_string(a->type);
  1369. gbString type_b = type_to_string(b->type);
  1370. error(a->expr,
  1371. "Mismatched types to 'max', '%s' vs '%s'",
  1372. type_a, type_b);
  1373. gb_string_free(type_b);
  1374. gb_string_free(type_a);
  1375. return false;
  1376. }
  1377. }
  1378. operand->type = operands[0].type;
  1379. }
  1380. break;
  1381. }
  1382. case BuiltinProc_abs: {
  1383. // abs :: proc(n: numeric) -> numeric
  1384. if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) {
  1385. gbString type_str = type_to_string(operand->type);
  1386. error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
  1387. gb_string_free(type_str);
  1388. return false;
  1389. }
  1390. if (operand->mode == Addressing_Constant) {
  1391. switch (operand->value.kind) {
  1392. case ExactValue_Integer:
  1393. operand->value.value_integer.neg = false;
  1394. break;
  1395. case ExactValue_Float:
  1396. operand->value.value_float = gb_abs(operand->value.value_float);
  1397. break;
  1398. case ExactValue_Complex: {
  1399. f64 r = operand->value.value_complex->real;
  1400. f64 i = operand->value.value_complex->imag;
  1401. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  1402. break;
  1403. }
  1404. default:
  1405. GB_PANIC("Invalid numeric constant");
  1406. break;
  1407. }
  1408. } else {
  1409. operand->mode = Addressing_Value;
  1410. {
  1411. Type *bt = base_type(operand->type);
  1412. if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64");
  1413. if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128");
  1414. if (are_types_identical(bt, t_quaternion128)) add_package_dependency(c, "runtime", "abs_quaternion128");
  1415. if (are_types_identical(bt, t_quaternion256)) add_package_dependency(c, "runtime", "abs_quaternion256");
  1416. }
  1417. }
  1418. if (is_type_complex(operand->type)) {
  1419. operand->type = base_complex_elem_type(operand->type);
  1420. }
  1421. GB_ASSERT(!is_type_complex(operand->type));
  1422. break;
  1423. }
  1424. case BuiltinProc_clamp: {
  1425. // clamp :: proc(a, min, max: ordered) -> ordered
  1426. Type *type = operand->type;
  1427. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  1428. gbString type_str = type_to_string(operand->type);
  1429. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1430. gb_string_free(type_str);
  1431. return false;
  1432. }
  1433. Ast *min_arg = ce->args[1];
  1434. Ast *max_arg = ce->args[2];
  1435. Operand x = *operand;
  1436. Operand y = {};
  1437. Operand z = {};
  1438. check_expr(c, &y, min_arg);
  1439. if (y.mode == Addressing_Invalid) {
  1440. return false;
  1441. }
  1442. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  1443. gbString type_str = type_to_string(y.type);
  1444. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1445. gb_string_free(type_str);
  1446. return false;
  1447. }
  1448. check_expr(c, &z, max_arg);
  1449. if (z.mode == Addressing_Invalid) {
  1450. return false;
  1451. }
  1452. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  1453. gbString type_str = type_to_string(z.type);
  1454. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  1455. gb_string_free(type_str);
  1456. return false;
  1457. }
  1458. if (x.mode == Addressing_Constant &&
  1459. y.mode == Addressing_Constant &&
  1460. z.mode == Addressing_Constant) {
  1461. ExactValue a = x.value;
  1462. ExactValue b = y.value;
  1463. ExactValue c = z.value;
  1464. operand->mode = Addressing_Constant;
  1465. if (compare_exact_values(Token_Lt, a, b)) {
  1466. operand->value = b;
  1467. operand->type = y.type;
  1468. } else if (compare_exact_values(Token_Gt, a, c)) {
  1469. operand->value = c;
  1470. operand->type = z.type;
  1471. } else {
  1472. operand->value = a;
  1473. operand->type = x.type;
  1474. }
  1475. } else {
  1476. operand->mode = Addressing_Value;
  1477. operand->type = type;
  1478. Operand *ops[3] = {&x, &y, &z};
  1479. for (isize i = 0; i < 3; i++) {
  1480. Operand *a = ops[i];
  1481. for (isize j = 0; j < 3; j++) {
  1482. if (i == j) continue;
  1483. Operand *b = ops[j];
  1484. convert_to_typed(c, a, b->type);
  1485. if (a->mode == Addressing_Invalid) { return false; }
  1486. }
  1487. }
  1488. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  1489. gbString type_x = type_to_string(x.type);
  1490. gbString type_y = type_to_string(y.type);
  1491. gbString type_z = type_to_string(z.type);
  1492. error(call,
  1493. "Mismatched types to 'clamp', '%s', '%s', '%s'",
  1494. type_x, type_y, type_z);
  1495. gb_string_free(type_z);
  1496. gb_string_free(type_y);
  1497. gb_string_free(type_x);
  1498. return false;
  1499. }
  1500. operand->type = ops[0]->type;
  1501. }
  1502. break;
  1503. }
  1504. case BuiltinProc_soa_zip: {
  1505. auto types = array_make<Type *>(temporary_allocator(), 0, ce->args.count);
  1506. auto names = array_make<String>(temporary_allocator(), 0, ce->args.count);
  1507. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  1508. bool fail = false;
  1509. for_array(i, ce->args) {
  1510. Ast *arg = ce->args[i];
  1511. bool mix = false;
  1512. if (first_is_field_value) {
  1513. mix = arg->kind != Ast_FieldValue;
  1514. } else {
  1515. mix = arg->kind == Ast_FieldValue;
  1516. }
  1517. if (mix) {
  1518. error(arg, "Mixture of 'field = value' and value elements in the procedure call 'soa_zip' is not allowed");
  1519. fail = true;
  1520. break;
  1521. }
  1522. }
  1523. StringSet name_set = {};
  1524. string_set_init(&name_set, temporary_allocator(), 2*ce->args.count);
  1525. for_array(i, ce->args) {
  1526. String name = {};
  1527. Ast *arg = ce->args[i];
  1528. if (arg->kind == Ast_FieldValue) {
  1529. Ast *ename = arg->FieldValue.field;
  1530. if (!fail && ename->kind != Ast_Ident) {
  1531. error(ename, "Expected an identifier for field argument");
  1532. } else if (ename->kind == Ast_Ident) {
  1533. name = ename->Ident.token.string;
  1534. }
  1535. arg = arg->FieldValue.value;
  1536. }
  1537. Operand op = {};
  1538. check_expr(c, &op, arg);
  1539. if (op.mode == Addressing_Invalid) {
  1540. return false;
  1541. }
  1542. Type *arg_type = base_type(op.type);
  1543. if (!is_type_slice(arg_type)) {
  1544. gbString s = type_to_string(op.type);
  1545. error(op.expr, "Indices to 'soa_zip' must be slices, got %s", s);
  1546. gb_string_free(s);
  1547. return false;
  1548. }
  1549. GB_ASSERT(arg_type->kind == Type_Slice);
  1550. if (name == "_") {
  1551. error(op.expr, "Field argument name '%.*s' is not allowed", LIT(name));
  1552. name = {};
  1553. }
  1554. if (name.len == 0) {
  1555. gbString field_name = gb_string_make(permanent_allocator(), "_");
  1556. field_name = gb_string_append_fmt(field_name, "%td", types.count);
  1557. name = make_string_c(field_name);
  1558. }
  1559. if (string_set_exists(&name_set, name)) {
  1560. error(op.expr, "Field argument name '%.*s' already exists", LIT(name));
  1561. } else {
  1562. array_add(&types, arg_type->Slice.elem);
  1563. array_add(&names, name);
  1564. string_set_add(&name_set, name);
  1565. }
  1566. }
  1567. Ast *dummy_node_struct = alloc_ast_node(nullptr, Ast_Invalid);
  1568. Ast *dummy_node_soa = alloc_ast_node(nullptr, Ast_Invalid);
  1569. Scope *s = create_scope(builtin_pkg->scope);
  1570. auto fields = array_make<Entity *>(permanent_allocator(), 0, types.count);
  1571. for_array(i, types) {
  1572. Type *type = types[i];
  1573. String name = names[i];
  1574. GB_ASSERT(name != "");
  1575. Entity *e = alloc_entity_field(s, make_token_ident(name), type, false, cast(i32)i, EntityState_Resolved);
  1576. array_add(&fields, e);
  1577. scope_insert(s, e);
  1578. }
  1579. Type *elem = nullptr;
  1580. if (type_hint != nullptr && is_type_struct(type_hint)) {
  1581. Type *soa_type = base_type(type_hint);
  1582. if (soa_type->Struct.soa_kind != StructSoa_Slice) {
  1583. goto soa_zip_end;
  1584. }
  1585. Type *soa_elem_type = soa_type->Struct.soa_elem;
  1586. Type *et = base_type(soa_elem_type);
  1587. if (et->kind != Type_Struct) {
  1588. goto soa_zip_end;
  1589. }
  1590. if (et->Struct.fields.count != fields.count) {
  1591. goto soa_zip_end;
  1592. }
  1593. if (!fail && first_is_field_value) {
  1594. for_array(i, names) {
  1595. Selection sel = lookup_field(et, names[i], false);
  1596. if (sel.entity == nullptr) {
  1597. goto soa_zip_end;
  1598. }
  1599. if (sel.index.count != 1) {
  1600. goto soa_zip_end;
  1601. }
  1602. if (!are_types_identical(sel.entity->type, types[i])) {
  1603. goto soa_zip_end;
  1604. }
  1605. }
  1606. } else {
  1607. for_array(i, et->Struct.fields) {
  1608. if (!are_types_identical(et->Struct.fields[i]->type, types[i])) {
  1609. goto soa_zip_end;
  1610. }
  1611. }
  1612. }
  1613. elem = soa_elem_type;
  1614. }
  1615. soa_zip_end:;
  1616. if (elem == nullptr) {
  1617. elem = alloc_type_struct();
  1618. elem->Struct.scope = s;
  1619. elem->Struct.fields = fields;
  1620. elem->Struct.tags = array_make<String>(permanent_allocator(), fields.count);
  1621. elem->Struct.node = dummy_node_struct;
  1622. type_set_offsets(elem);
  1623. }
  1624. Type *soa_type = make_soa_struct_slice(c, dummy_node_soa, nullptr, elem);
  1625. type_set_offsets(soa_type);
  1626. operand->type = soa_type;
  1627. operand->mode = Addressing_Value;
  1628. break;
  1629. }
  1630. case BuiltinProc_soa_unzip: {
  1631. Operand x = {};
  1632. check_expr(c, &x, ce->args[0]);
  1633. if (x.mode == Addressing_Invalid) {
  1634. return false;
  1635. }
  1636. if (!is_operand_value(x)) {
  1637. error(call, "'soa_unzip' expects an #soa slice");
  1638. return false;
  1639. }
  1640. Type *t = base_type(x.type);
  1641. if (!is_type_soa_struct(t) || t->Struct.soa_kind != StructSoa_Slice) {
  1642. gbString s = type_to_string(x.type);
  1643. error(call, "'soa_unzip' expects an #soa slice, got %s", s);
  1644. gb_string_free(s);
  1645. return false;
  1646. }
  1647. auto types = slice_make<Type *>(permanent_allocator(), t->Struct.fields.count-1);
  1648. for_array(i, types) {
  1649. Entity *f = t->Struct.fields[i];
  1650. GB_ASSERT(f->type->kind == Type_Pointer);
  1651. types[i] = alloc_type_slice(f->type->Pointer.elem);
  1652. }
  1653. operand->type = alloc_type_tuple_from_field_types(types.data, types.count, false, false);
  1654. operand->mode = Addressing_Value;
  1655. break;
  1656. }
  1657. case BuiltinProc_try:
  1658. return check_builtin_try(c, operand, builtin_name, call, type_hint);
  1659. case BuiltinProc_or_else:
  1660. return check_builtin_or_else(c, operand, builtin_name, call, type_hint);
  1661. case BuiltinProc_simd_vector: {
  1662. Operand x = {};
  1663. Operand y = {};
  1664. x = *operand;
  1665. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  1666. error(call, "Expected a constant integer for 'intrinsics.simd_vector'");
  1667. operand->mode = Addressing_Type;
  1668. operand->type = t_invalid;
  1669. return false;
  1670. }
  1671. if (x.value.value_integer.neg) {
  1672. error(call, "Negative vector element length");
  1673. operand->mode = Addressing_Type;
  1674. operand->type = t_invalid;
  1675. return false;
  1676. }
  1677. i64 count = big_int_to_i64(&x.value.value_integer);
  1678. check_expr_or_type(c, &y, ce->args[1]);
  1679. if (y.mode != Addressing_Type) {
  1680. error(call, "Expected a type 'intrinsics.simd_vector'");
  1681. operand->mode = Addressing_Type;
  1682. operand->type = t_invalid;
  1683. return false;
  1684. }
  1685. Type *elem = y.type;
  1686. if (!is_type_valid_vector_elem(elem)) {
  1687. gbString str = type_to_string(elem);
  1688. error(call, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  1689. gb_string_free(str);
  1690. operand->mode = Addressing_Type;
  1691. operand->type = t_invalid;
  1692. return false;
  1693. }
  1694. operand->mode = Addressing_Type;
  1695. operand->type = alloc_type_simd_vector(count, elem);
  1696. break;
  1697. }
  1698. case BuiltinProc_soa_struct: {
  1699. Operand x = {};
  1700. Operand y = {};
  1701. x = *operand;
  1702. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  1703. error(call, "Expected a constant integer for 'intrinsics.soa_struct'");
  1704. operand->mode = Addressing_Type;
  1705. operand->type = t_invalid;
  1706. return false;
  1707. }
  1708. if (x.value.value_integer.neg) {
  1709. error(call, "Negative array element length");
  1710. operand->mode = Addressing_Type;
  1711. operand->type = t_invalid;
  1712. return false;
  1713. }
  1714. i64 count = big_int_to_i64(&x.value.value_integer);
  1715. check_expr_or_type(c, &y, ce->args[1]);
  1716. if (y.mode != Addressing_Type) {
  1717. error(call, "Expected a type 'intrinsics.soa_struct'");
  1718. operand->mode = Addressing_Type;
  1719. operand->type = t_invalid;
  1720. return false;
  1721. }
  1722. Type *elem = y.type;
  1723. Type *bt_elem = base_type(elem);
  1724. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  1725. gbString str = type_to_string(elem);
  1726. error(call, "Invalid type for 'intrinsics.soa_struct', expected a struct or array of length 4 or below, got '%s'", str);
  1727. gb_string_free(str);
  1728. operand->mode = Addressing_Type;
  1729. operand->type = t_invalid;
  1730. return false;
  1731. }
  1732. operand->mode = Addressing_Type;
  1733. Type *soa_struct = nullptr;
  1734. Scope *scope = nullptr;
  1735. if (is_type_array(elem)) {
  1736. Type *old_array = base_type(elem);
  1737. soa_struct = alloc_type_struct();
  1738. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
  1739. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
  1740. soa_struct->Struct.node = operand->expr;
  1741. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  1742. soa_struct->Struct.soa_elem = elem;
  1743. soa_struct->Struct.soa_count = count;
  1744. scope = create_scope(c->scope);
  1745. soa_struct->Struct.scope = scope;
  1746. String params_xyzw[4] = {
  1747. str_lit("x"),
  1748. str_lit("y"),
  1749. str_lit("z"),
  1750. str_lit("w")
  1751. };
  1752. for (i64 i = 0; i < old_array->Array.count; i++) {
  1753. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  1754. Token token = {};
  1755. token.string = params_xyzw[i];
  1756. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  1757. soa_struct->Struct.fields[i] = new_field;
  1758. add_entity(c->checker, scope, nullptr, new_field);
  1759. add_entity_use(c, nullptr, new_field);
  1760. }
  1761. } else {
  1762. GB_ASSERT(is_type_struct(elem));
  1763. Type *old_struct = base_type(elem);
  1764. soa_struct = alloc_type_struct();
  1765. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  1766. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count);
  1767. soa_struct->Struct.node = operand->expr;
  1768. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  1769. soa_struct->Struct.soa_elem = elem;
  1770. soa_struct->Struct.soa_count = count;
  1771. scope = create_scope(old_struct->Struct.scope->parent);
  1772. soa_struct->Struct.scope = scope;
  1773. for_array(i, old_struct->Struct.fields) {
  1774. Entity *old_field = old_struct->Struct.fields[i];
  1775. if (old_field->kind == Entity_Variable) {
  1776. Type *array_type = alloc_type_array(old_field->type, count);
  1777. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  1778. soa_struct->Struct.fields[i] = new_field;
  1779. add_entity(c->checker, scope, nullptr, new_field);
  1780. } else {
  1781. soa_struct->Struct.fields[i] = old_field;
  1782. }
  1783. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  1784. }
  1785. }
  1786. Token token = {};
  1787. token.string = str_lit("Base_Type");
  1788. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  1789. add_entity(c->checker, scope, nullptr, base_type_entity);
  1790. add_type_info_type(c, soa_struct);
  1791. operand->type = soa_struct;
  1792. break;
  1793. }
  1794. case BuiltinProc_alloca:
  1795. {
  1796. Operand sz = {};
  1797. Operand al = {};
  1798. check_expr(c, &sz, ce->args[0]);
  1799. if (sz.mode == Addressing_Invalid) {
  1800. return false;
  1801. }
  1802. check_expr(c, &al, ce->args[1]);
  1803. if (al.mode == Addressing_Invalid) {
  1804. return false;
  1805. }
  1806. convert_to_typed(c, &sz, t_int); if (sz.mode == Addressing_Invalid) return false;
  1807. convert_to_typed(c, &al, t_int); if (al.mode == Addressing_Invalid) return false;
  1808. if (!is_type_integer(sz.type) || !is_type_integer(al.type)) {
  1809. error(operand->expr, "Both parameters to '%.*s' must integers", LIT(builtin_name));
  1810. return false;
  1811. }
  1812. if (sz.mode == Addressing_Constant) {
  1813. i64 i_sz = exact_value_to_i64(sz.value);
  1814. if (i_sz < 0) {
  1815. error(sz.expr, "Size parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_sz);
  1816. return false;
  1817. }
  1818. }
  1819. if (al.mode == Addressing_Constant) {
  1820. i64 i_al = exact_value_to_i64(al.value);
  1821. if (i_al < 0) {
  1822. error(al.expr, "Alignment parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_al);
  1823. return false;
  1824. }
  1825. if (i_al > 1<<29) {
  1826. error(al.expr, "Alignment parameter to '%.*s' must not exceed '1<<29', got %lld", LIT(builtin_name), cast(long long)i_al);
  1827. return false;
  1828. }
  1829. if (!gb_is_power_of_two(cast(isize)i_al) && i_al != 0) {
  1830. 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);
  1831. return false;
  1832. }
  1833. } else {
  1834. error(al.expr, "Alignment parameter to '%.*s' must be constant", LIT(builtin_name));
  1835. }
  1836. operand->type = t_u8_ptr;
  1837. operand->mode = Addressing_Value;
  1838. break;
  1839. }
  1840. case BuiltinProc_cpu_relax:
  1841. operand->mode = Addressing_NoValue;
  1842. break;
  1843. case BuiltinProc_trap:
  1844. case BuiltinProc_debug_trap:
  1845. operand->mode = Addressing_NoValue;
  1846. break;
  1847. case BuiltinProc_read_cycle_counter:
  1848. operand->mode = Addressing_Value;
  1849. operand->type = t_i64;
  1850. break;
  1851. case BuiltinProc_count_ones:
  1852. case BuiltinProc_count_zeros:
  1853. case BuiltinProc_count_trailing_zeros:
  1854. case BuiltinProc_count_leading_zeros:
  1855. case BuiltinProc_reverse_bits:
  1856. {
  1857. Operand x = {};
  1858. check_expr(c, &x, ce->args[0]);
  1859. if (x.mode == Addressing_Invalid) {
  1860. return false;
  1861. }
  1862. if (!is_type_integer_like(x.type)) {
  1863. gbString xts = type_to_string(x.type);
  1864. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts);
  1865. gb_string_free(xts);
  1866. } else if (x.type == t_llvm_bool) {
  1867. gbString xts = type_to_string(x.type);
  1868. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1869. gb_string_free(xts);
  1870. }
  1871. operand->mode = Addressing_Value;
  1872. operand->type = default_type(x.type);
  1873. }
  1874. break;
  1875. case BuiltinProc_byte_swap:
  1876. {
  1877. Operand x = {};
  1878. check_expr(c, &x, ce->args[0]);
  1879. if (x.mode == Addressing_Invalid) {
  1880. return false;
  1881. }
  1882. if (!is_type_integer_like(x.type) && !is_type_float(x.type)) {
  1883. gbString xts = type_to_string(x.type);
  1884. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set) or float, got %s", LIT(builtin_procs[id].name), xts);
  1885. gb_string_free(xts);
  1886. } else if (x.type == t_llvm_bool) {
  1887. gbString xts = type_to_string(x.type);
  1888. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1889. gb_string_free(xts);
  1890. }
  1891. i64 sz = type_size_of(x.type);
  1892. if (sz < 2) {
  1893. gbString xts = type_to_string(x.type);
  1894. error(x.expr, "Type passed to '%.*s' must be at least 2 bytes, got %s with size of %lld", LIT(builtin_procs[id].name), xts, sz);
  1895. gb_string_free(xts);
  1896. }
  1897. operand->mode = Addressing_Value;
  1898. operand->type = default_type(x.type);
  1899. }
  1900. break;
  1901. case BuiltinProc_overflow_add:
  1902. case BuiltinProc_overflow_sub:
  1903. case BuiltinProc_overflow_mul:
  1904. {
  1905. Operand x = {};
  1906. Operand y = {};
  1907. check_expr(c, &x, ce->args[0]);
  1908. check_expr(c, &y, ce->args[1]);
  1909. if (x.mode == Addressing_Invalid) {
  1910. return false;
  1911. }
  1912. if (y.mode == Addressing_Invalid) {
  1913. return false;
  1914. }
  1915. convert_to_typed(c, &y, x.type);
  1916. convert_to_typed(c, &x, y.type);
  1917. if (is_type_untyped(x.type)) {
  1918. gbString xts = type_to_string(x.type);
  1919. error(x.expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1920. gb_string_free(xts);
  1921. return false;
  1922. }
  1923. if (!is_type_integer(x.type)) {
  1924. gbString xts = type_to_string(x.type);
  1925. error(x.expr, "Expected an integer for '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1926. gb_string_free(xts);
  1927. return false;
  1928. }
  1929. Type *ct = core_type(x.type);
  1930. if (is_type_different_to_arch_endianness(ct)) {
  1931. GB_ASSERT(ct->kind == Type_Basic);
  1932. if (ct->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) {
  1933. gbString xts = type_to_string(x.type);
  1934. error(x.expr, "Expected an integer which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1935. gb_string_free(xts);
  1936. return false;
  1937. }
  1938. }
  1939. operand->mode = Addressing_OptionalOk;
  1940. operand->type = default_type(x.type);
  1941. }
  1942. break;
  1943. case BuiltinProc_sqrt:
  1944. {
  1945. Operand x = {};
  1946. check_expr(c, &x, ce->args[0]);
  1947. if (x.mode == Addressing_Invalid) {
  1948. return false;
  1949. }
  1950. if (!is_type_float(x.type)) {
  1951. gbString xts = type_to_string(x.type);
  1952. error(x.expr, "Expected a floating point value for '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  1953. gb_string_free(xts);
  1954. return false;
  1955. }
  1956. if (x.mode == Addressing_Constant) {
  1957. f64 v = exact_value_to_f64(x.value);
  1958. operand->mode = Addressing_Constant;
  1959. operand->type = x.type;
  1960. operand->value = exact_value_float(gb_sqrt(v));
  1961. break;
  1962. }
  1963. operand->mode = Addressing_Value;
  1964. operand->type = default_type(x.type);
  1965. }
  1966. break;
  1967. case BuiltinProc_mem_copy:
  1968. case BuiltinProc_mem_copy_non_overlapping:
  1969. {
  1970. operand->mode = Addressing_NoValue;
  1971. operand->type = t_invalid;
  1972. Operand dst = {};
  1973. Operand src = {};
  1974. Operand len = {};
  1975. check_expr(c, &dst, ce->args[0]);
  1976. check_expr(c, &src, ce->args[1]);
  1977. check_expr(c, &len, ce->args[2]);
  1978. if (dst.mode == Addressing_Invalid) {
  1979. return false;
  1980. }
  1981. if (src.mode == Addressing_Invalid) {
  1982. return false;
  1983. }
  1984. if (len.mode == Addressing_Invalid) {
  1985. return false;
  1986. }
  1987. if (!is_type_pointer(dst.type)) {
  1988. gbString str = type_to_string(dst.type);
  1989. error(dst.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  1990. gb_string_free(str);
  1991. return false;
  1992. }
  1993. if (!is_type_pointer(src.type)) {
  1994. gbString str = type_to_string(src.type);
  1995. error(src.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  1996. gb_string_free(str);
  1997. return false;
  1998. }
  1999. if (!is_type_integer(len.type)) {
  2000. gbString str = type_to_string(len.type);
  2001. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2002. gb_string_free(str);
  2003. return false;
  2004. }
  2005. if (len.mode == Addressing_Constant) {
  2006. i64 n = exact_value_to_i64(len.value);
  2007. if (n < 0) {
  2008. gbString str = expr_to_string(len.expr);
  2009. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2010. gb_string_free(str);
  2011. }
  2012. }
  2013. }
  2014. break;
  2015. case BuiltinProc_mem_zero:
  2016. {
  2017. operand->mode = Addressing_NoValue;
  2018. operand->type = t_invalid;
  2019. Operand ptr = {};
  2020. Operand len = {};
  2021. check_expr(c, &ptr, ce->args[0]);
  2022. check_expr(c, &len, ce->args[1]);
  2023. if (ptr.mode == Addressing_Invalid) {
  2024. return false;
  2025. }
  2026. if (len.mode == Addressing_Invalid) {
  2027. return false;
  2028. }
  2029. if (!is_type_pointer(ptr.type)) {
  2030. gbString str = type_to_string(ptr.type);
  2031. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2032. gb_string_free(str);
  2033. return false;
  2034. }
  2035. if (!is_type_integer(len.type)) {
  2036. gbString str = type_to_string(len.type);
  2037. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2038. gb_string_free(str);
  2039. return false;
  2040. }
  2041. if (len.mode == Addressing_Constant) {
  2042. i64 n = exact_value_to_i64(len.value);
  2043. if (n < 0) {
  2044. gbString str = expr_to_string(len.expr);
  2045. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2046. gb_string_free(str);
  2047. }
  2048. }
  2049. }
  2050. break;
  2051. case BuiltinProc_ptr_offset:
  2052. {
  2053. Operand ptr = {};
  2054. Operand offset = {};
  2055. check_expr(c, &ptr, ce->args[0]);
  2056. check_expr(c, &offset, ce->args[1]);
  2057. if (ptr.mode == Addressing_Invalid) {
  2058. operand->mode = Addressing_Invalid;
  2059. operand->type = t_invalid;
  2060. return false;
  2061. }
  2062. if (offset.mode == Addressing_Invalid) {
  2063. operand->mode = Addressing_Invalid;
  2064. operand->type = t_invalid;
  2065. return false;
  2066. }
  2067. operand->mode = Addressing_Value;
  2068. operand->type = ptr.type;
  2069. if (!is_type_pointer(ptr.type)) {
  2070. gbString str = type_to_string(ptr.type);
  2071. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2072. gb_string_free(str);
  2073. return false;
  2074. }
  2075. if (are_types_identical(core_type(ptr.type), t_rawptr)) {
  2076. gbString str = type_to_string(ptr.type);
  2077. error(ptr.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2078. gb_string_free(str);
  2079. return false;
  2080. }
  2081. if (!is_type_integer(offset.type)) {
  2082. gbString str = type_to_string(offset.type);
  2083. error(offset.expr, "Expected an integer value for the offset parameter for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2084. gb_string_free(str);
  2085. return false;
  2086. }
  2087. }
  2088. break;
  2089. case BuiltinProc_ptr_sub:
  2090. {
  2091. operand->mode = Addressing_NoValue;
  2092. operand->type = t_invalid;
  2093. Operand ptr0 = {};
  2094. Operand ptr1 = {};
  2095. check_expr(c, &ptr0, ce->args[0]);
  2096. check_expr(c, &ptr1, ce->args[1]);
  2097. if (ptr0.mode == Addressing_Invalid) {
  2098. operand->mode = Addressing_Invalid;
  2099. operand->type = t_invalid;
  2100. return false;
  2101. }
  2102. if (ptr1.mode == Addressing_Invalid) {
  2103. operand->mode = Addressing_Invalid;
  2104. operand->type = t_invalid;
  2105. return false;
  2106. }
  2107. operand->mode = Addressing_Value;
  2108. operand->type = t_int;
  2109. if (!is_type_pointer(ptr0.type)) {
  2110. gbString str = type_to_string(ptr0.type);
  2111. error(ptr0.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2112. gb_string_free(str);
  2113. return false;
  2114. }
  2115. if (are_types_identical(core_type(ptr0.type), t_rawptr)) {
  2116. gbString str = type_to_string(ptr0.type);
  2117. error(ptr0.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2118. gb_string_free(str);
  2119. return false;
  2120. }
  2121. if (!is_type_pointer(ptr1.type)) {
  2122. gbString str = type_to_string(ptr1.type);
  2123. error(ptr1.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2124. gb_string_free(str);
  2125. return false;
  2126. }
  2127. if (are_types_identical(core_type(ptr1.type), t_rawptr)) {
  2128. gbString str = type_to_string(ptr1.type);
  2129. error(ptr1.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
  2130. gb_string_free(str);
  2131. return false;
  2132. }
  2133. if (!are_types_identical(ptr0.type, ptr1.type)) {
  2134. gbString xts = type_to_string(ptr0.type);
  2135. gbString yts = type_to_string(ptr1.type);
  2136. error(ptr0.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_procs[id].name), xts, yts);
  2137. gb_string_free(yts);
  2138. gb_string_free(xts);
  2139. return false;
  2140. }
  2141. }
  2142. break;
  2143. case BuiltinProc_atomic_fence:
  2144. case BuiltinProc_atomic_fence_acq:
  2145. case BuiltinProc_atomic_fence_rel:
  2146. case BuiltinProc_atomic_fence_acqrel:
  2147. operand->mode = Addressing_NoValue;
  2148. break;
  2149. case BuiltinProc_volatile_store:
  2150. /*fallthrough*/
  2151. case BuiltinProc_atomic_store:
  2152. case BuiltinProc_atomic_store_rel:
  2153. case BuiltinProc_atomic_store_relaxed:
  2154. case BuiltinProc_atomic_store_unordered:
  2155. {
  2156. Type *elem = nullptr;
  2157. if (!is_type_normal_pointer(operand->type, &elem)) {
  2158. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2159. return false;
  2160. }
  2161. Operand x = {};
  2162. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  2163. check_assignment(c, &x, elem, builtin_name);
  2164. operand->type = nullptr;
  2165. operand->mode = Addressing_NoValue;
  2166. break;
  2167. }
  2168. case BuiltinProc_volatile_load:
  2169. /*fallthrough*/
  2170. case BuiltinProc_atomic_load:
  2171. case BuiltinProc_atomic_load_acq:
  2172. case BuiltinProc_atomic_load_relaxed:
  2173. case BuiltinProc_atomic_load_unordered:
  2174. {
  2175. Type *elem = nullptr;
  2176. if (!is_type_normal_pointer(operand->type, &elem)) {
  2177. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2178. return false;
  2179. }
  2180. operand->type = elem;
  2181. operand->mode = Addressing_Value;
  2182. break;
  2183. }
  2184. case BuiltinProc_atomic_add:
  2185. case BuiltinProc_atomic_add_acq:
  2186. case BuiltinProc_atomic_add_rel:
  2187. case BuiltinProc_atomic_add_acqrel:
  2188. case BuiltinProc_atomic_add_relaxed:
  2189. case BuiltinProc_atomic_sub:
  2190. case BuiltinProc_atomic_sub_acq:
  2191. case BuiltinProc_atomic_sub_rel:
  2192. case BuiltinProc_atomic_sub_acqrel:
  2193. case BuiltinProc_atomic_sub_relaxed:
  2194. case BuiltinProc_atomic_and:
  2195. case BuiltinProc_atomic_and_acq:
  2196. case BuiltinProc_atomic_and_rel:
  2197. case BuiltinProc_atomic_and_acqrel:
  2198. case BuiltinProc_atomic_and_relaxed:
  2199. case BuiltinProc_atomic_nand:
  2200. case BuiltinProc_atomic_nand_acq:
  2201. case BuiltinProc_atomic_nand_rel:
  2202. case BuiltinProc_atomic_nand_acqrel:
  2203. case BuiltinProc_atomic_nand_relaxed:
  2204. case BuiltinProc_atomic_or:
  2205. case BuiltinProc_atomic_or_acq:
  2206. case BuiltinProc_atomic_or_rel:
  2207. case BuiltinProc_atomic_or_acqrel:
  2208. case BuiltinProc_atomic_or_relaxed:
  2209. case BuiltinProc_atomic_xor:
  2210. case BuiltinProc_atomic_xor_acq:
  2211. case BuiltinProc_atomic_xor_rel:
  2212. case BuiltinProc_atomic_xor_acqrel:
  2213. case BuiltinProc_atomic_xor_relaxed:
  2214. case BuiltinProc_atomic_xchg:
  2215. case BuiltinProc_atomic_xchg_acq:
  2216. case BuiltinProc_atomic_xchg_rel:
  2217. case BuiltinProc_atomic_xchg_acqrel:
  2218. case BuiltinProc_atomic_xchg_relaxed:
  2219. {
  2220. Type *elem = nullptr;
  2221. if (!is_type_normal_pointer(operand->type, &elem)) {
  2222. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2223. return false;
  2224. }
  2225. Operand x = {};
  2226. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  2227. check_assignment(c, &x, elem, builtin_name);
  2228. operand->type = elem;
  2229. operand->mode = Addressing_Value;
  2230. break;
  2231. }
  2232. case BuiltinProc_atomic_cxchg:
  2233. case BuiltinProc_atomic_cxchg_acq:
  2234. case BuiltinProc_atomic_cxchg_rel:
  2235. case BuiltinProc_atomic_cxchg_acqrel:
  2236. case BuiltinProc_atomic_cxchg_relaxed:
  2237. case BuiltinProc_atomic_cxchg_failrelaxed:
  2238. case BuiltinProc_atomic_cxchg_failacq:
  2239. case BuiltinProc_atomic_cxchg_acq_failrelaxed:
  2240. case BuiltinProc_atomic_cxchg_acqrel_failrelaxed:
  2241. case BuiltinProc_atomic_cxchgweak:
  2242. case BuiltinProc_atomic_cxchgweak_acq:
  2243. case BuiltinProc_atomic_cxchgweak_rel:
  2244. case BuiltinProc_atomic_cxchgweak_acqrel:
  2245. case BuiltinProc_atomic_cxchgweak_relaxed:
  2246. case BuiltinProc_atomic_cxchgweak_failrelaxed:
  2247. case BuiltinProc_atomic_cxchgweak_failacq:
  2248. case BuiltinProc_atomic_cxchgweak_acq_failrelaxed:
  2249. case BuiltinProc_atomic_cxchgweak_acqrel_failrelaxed:
  2250. {
  2251. Type *elem = nullptr;
  2252. if (!is_type_normal_pointer(operand->type, &elem)) {
  2253. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  2254. return false;
  2255. }
  2256. Operand x = {};
  2257. Operand y = {};
  2258. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  2259. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  2260. check_assignment(c, &x, elem, builtin_name);
  2261. check_assignment(c, &y, elem, builtin_name);
  2262. operand->mode = Addressing_OptionalOk;
  2263. operand->type = elem;
  2264. break;
  2265. }
  2266. break;
  2267. case BuiltinProc_fixed_point_mul:
  2268. case BuiltinProc_fixed_point_div:
  2269. case BuiltinProc_fixed_point_mul_sat:
  2270. case BuiltinProc_fixed_point_div_sat:
  2271. {
  2272. Operand x = {};
  2273. Operand y = {};
  2274. Operand z = {};
  2275. check_expr(c, &x, ce->args[0]);
  2276. if (x.mode == Addressing_Invalid) {
  2277. return false;
  2278. }
  2279. check_expr(c, &y, ce->args[1]);
  2280. if (y.mode == Addressing_Invalid) {
  2281. return false;
  2282. }
  2283. convert_to_typed(c, &x, y.type);
  2284. if (x.mode == Addressing_Invalid) {
  2285. return false;
  2286. }
  2287. convert_to_typed(c, &y, x.type);
  2288. if (x.mode == Addressing_Invalid) {
  2289. return false;
  2290. }
  2291. if (!are_types_identical(x.type, y.type)) {
  2292. gbString xts = type_to_string(x.type);
  2293. gbString yts = type_to_string(y.type);
  2294. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_procs[id].name), xts, yts);
  2295. gb_string_free(yts);
  2296. gb_string_free(xts);
  2297. return false;
  2298. }
  2299. if (!is_type_integer(x.type) || is_type_untyped(x.type)) {
  2300. gbString xts = type_to_string(x.type);
  2301. error(x.expr, "Expected an integer type for '%.*s', got %s", LIT(builtin_procs[id].name), xts);
  2302. gb_string_free(xts);
  2303. return false;
  2304. }
  2305. check_expr(c, &z, ce->args[2]);
  2306. if (z.mode == Addressing_Invalid) {
  2307. return false;
  2308. }
  2309. if (z.mode != Addressing_Constant || !is_type_integer(z.type)) {
  2310. error(z.expr, "Expected a constant integer for the scale in '%.*s'", LIT(builtin_procs[id].name));
  2311. return false;
  2312. }
  2313. i64 n = exact_value_to_i64(z.value);
  2314. if (n <= 0) {
  2315. error(z.expr, "Scale parameter in '%.*s' must be positive, got %lld", LIT(builtin_procs[id].name), n);
  2316. return false;
  2317. }
  2318. i64 sz = 8*type_size_of(x.type);
  2319. if (n > sz) {
  2320. error(z.expr, "Scale parameter in '%.*s' is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_procs[id].name), n, sz);
  2321. return false;
  2322. }
  2323. operand->type = x.type;
  2324. operand->mode = Addressing_Value;
  2325. }
  2326. break;
  2327. case BuiltinProc_expect:
  2328. {
  2329. Operand x = {};
  2330. Operand y = {};
  2331. check_expr(c, &x, ce->args[0]);
  2332. check_expr(c, &y, ce->args[1]);
  2333. if (x.mode == Addressing_Invalid) {
  2334. return false;
  2335. }
  2336. if (y.mode == Addressing_Invalid) {
  2337. return false;
  2338. }
  2339. convert_to_typed(c, &y, x.type);
  2340. convert_to_typed(c, &x, y.type);
  2341. if (!are_types_identical(x.type, y.type)) {
  2342. gbString xts = type_to_string(x.type);
  2343. gbString yts = type_to_string(y.type);
  2344. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_procs[id].name), xts, yts);
  2345. gb_string_free(yts);
  2346. gb_string_free(xts);
  2347. *operand = x; // minimize error propagation
  2348. return true;
  2349. }
  2350. if (!is_type_integer_like(x.type)) {
  2351. gbString xts = type_to_string(x.type);
  2352. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts);
  2353. gb_string_free(xts);
  2354. *operand = x;
  2355. return true;
  2356. }
  2357. if (y.mode != Addressing_Constant) {
  2358. error(y.expr, "Second argument to '%.*s' must be constant as it is the expected value", LIT(builtin_procs[id].name));
  2359. }
  2360. if (x.mode == Addressing_Constant) {
  2361. // NOTE(bill): just completely ignore this intrinsic entirely
  2362. *operand = x;
  2363. return true;
  2364. }
  2365. operand->mode = Addressing_Value;
  2366. operand->type = x.type;
  2367. }
  2368. break;
  2369. case BuiltinProc_type_base_type:
  2370. if (operand->mode != Addressing_Type) {
  2371. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2372. } else {
  2373. operand->type = base_type(operand->type);
  2374. }
  2375. operand->mode = Addressing_Type;
  2376. break;
  2377. case BuiltinProc_type_core_type:
  2378. if (operand->mode != Addressing_Type) {
  2379. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2380. } else {
  2381. operand->type = core_type(operand->type);
  2382. }
  2383. operand->mode = Addressing_Type;
  2384. break;
  2385. case BuiltinProc_type_elem_type:
  2386. if (operand->mode != Addressing_Type) {
  2387. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2388. } else {
  2389. Type *bt = base_type(operand->type);
  2390. switch (bt->kind) {
  2391. case Type_Basic:
  2392. switch (bt->Basic.kind) {
  2393. case Basic_complex64: operand->type = t_f32; break;
  2394. case Basic_complex128: operand->type = t_f64; break;
  2395. case Basic_quaternion128: operand->type = t_f32; break;
  2396. case Basic_quaternion256: operand->type = t_f64; break;
  2397. }
  2398. break;
  2399. case Type_Pointer: operand->type = bt->Pointer.elem; break;
  2400. case Type_Array: operand->type = bt->Array.elem; break;
  2401. case Type_EnumeratedArray: operand->type = bt->EnumeratedArray.elem; break;
  2402. case Type_Slice: operand->type = bt->Slice.elem; break;
  2403. case Type_DynamicArray: operand->type = bt->DynamicArray.elem; break;
  2404. }
  2405. }
  2406. operand->mode = Addressing_Type;
  2407. break;
  2408. case BuiltinProc_type_is_boolean:
  2409. case BuiltinProc_type_is_integer:
  2410. case BuiltinProc_type_is_rune:
  2411. case BuiltinProc_type_is_float:
  2412. case BuiltinProc_type_is_complex:
  2413. case BuiltinProc_type_is_quaternion:
  2414. case BuiltinProc_type_is_string:
  2415. case BuiltinProc_type_is_typeid:
  2416. case BuiltinProc_type_is_any:
  2417. case BuiltinProc_type_is_endian_platform:
  2418. case BuiltinProc_type_is_endian_little:
  2419. case BuiltinProc_type_is_endian_big:
  2420. case BuiltinProc_type_is_unsigned:
  2421. case BuiltinProc_type_is_numeric:
  2422. case BuiltinProc_type_is_ordered:
  2423. case BuiltinProc_type_is_ordered_numeric:
  2424. case BuiltinProc_type_is_indexable:
  2425. case BuiltinProc_type_is_sliceable:
  2426. case BuiltinProc_type_is_comparable:
  2427. case BuiltinProc_type_is_simple_compare:
  2428. case BuiltinProc_type_is_dereferenceable:
  2429. case BuiltinProc_type_is_valid_map_key:
  2430. case BuiltinProc_type_is_named:
  2431. case BuiltinProc_type_is_pointer:
  2432. case BuiltinProc_type_is_array:
  2433. case BuiltinProc_type_is_slice:
  2434. case BuiltinProc_type_is_dynamic_array:
  2435. case BuiltinProc_type_is_map:
  2436. case BuiltinProc_type_is_struct:
  2437. case BuiltinProc_type_is_union:
  2438. case BuiltinProc_type_is_enum:
  2439. case BuiltinProc_type_is_proc:
  2440. case BuiltinProc_type_is_bit_field:
  2441. case BuiltinProc_type_is_bit_field_value:
  2442. case BuiltinProc_type_is_bit_set:
  2443. case BuiltinProc_type_is_simd_vector:
  2444. case BuiltinProc_type_is_specialized_polymorphic_record:
  2445. case BuiltinProc_type_is_unspecialized_polymorphic_record:
  2446. case BuiltinProc_type_has_nil:
  2447. GB_ASSERT(BuiltinProc__type_simple_boolean_begin < id && id < BuiltinProc__type_simple_boolean_end);
  2448. operand->value = exact_value_bool(false);
  2449. if (operand->mode != Addressing_Type) {
  2450. gbString str = expr_to_string(ce->args[0]);
  2451. error(operand->expr, "Expected a type for '%.*s', got '%s'", LIT(builtin_name), str);
  2452. gb_string_free(str);
  2453. } else {
  2454. i32 i = id - cast(i32)BuiltinProc__type_simple_boolean_begin;
  2455. auto procedure = builtin_type_is_procs[i];
  2456. GB_ASSERT_MSG(procedure != nullptr, "%.*s", LIT(builtin_name));
  2457. bool ok = procedure(operand->type);
  2458. operand->value = exact_value_bool(ok);
  2459. }
  2460. operand->mode = Addressing_Constant;
  2461. operand->type = t_untyped_bool;
  2462. break;
  2463. case BuiltinProc_type_has_field:
  2464. {
  2465. Operand op = {};
  2466. Type *bt = check_type(c, ce->args[0]);
  2467. Type *type = base_type(bt);
  2468. if (type == nullptr || type == t_invalid) {
  2469. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  2470. return false;
  2471. }
  2472. Operand x = {};
  2473. check_expr(c, &x, ce->args[1]);
  2474. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  2475. error(ce->args[1], "Expected a const string for field argument");
  2476. return false;
  2477. }
  2478. String field_name = x.value.value_string;
  2479. Selection sel = lookup_field(type, field_name, false);
  2480. operand->mode = Addressing_Constant;
  2481. operand->value = exact_value_bool(sel.index.count != 0);
  2482. operand->type = t_untyped_bool;
  2483. break;
  2484. }
  2485. break;
  2486. case BuiltinProc_type_is_specialization_of:
  2487. {
  2488. if (operand->mode != Addressing_Type) {
  2489. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2490. operand->mode = Addressing_Invalid;
  2491. operand->type = t_invalid;
  2492. return false;
  2493. }
  2494. Type *t = operand->type;
  2495. Type *s = nullptr;
  2496. bool prev_ips = c->in_polymorphic_specialization;
  2497. c->in_polymorphic_specialization = true;
  2498. s = check_type(c, ce->args[1]);
  2499. c->in_polymorphic_specialization = prev_ips;
  2500. if (s == t_invalid) {
  2501. error(ce->args[1], "Invalid specialization type for '%.*s'", LIT(builtin_name));
  2502. operand->mode = Addressing_Invalid;
  2503. operand->type = t_invalid;
  2504. return false;
  2505. }
  2506. operand->mode = Addressing_Constant;
  2507. operand->type = t_untyped_bool;
  2508. operand->value = exact_value_bool(check_type_specialization_to(c, s, t, false, false));
  2509. }
  2510. break;
  2511. case BuiltinProc_type_is_variant_of:
  2512. {
  2513. if (operand->mode != Addressing_Type) {
  2514. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2515. operand->mode = Addressing_Invalid;
  2516. operand->type = t_invalid;
  2517. return false;
  2518. }
  2519. Type *u = operand->type;
  2520. if (!is_type_union(u)) {
  2521. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  2522. operand->mode = Addressing_Invalid;
  2523. operand->type = t_invalid;
  2524. return false;
  2525. }
  2526. Type *v = check_type(c, ce->args[1]);
  2527. u = base_type(u);
  2528. GB_ASSERT(u->kind == Type_Union);
  2529. bool is_variant = false;
  2530. for_array(i, u->Union.variants) {
  2531. Type *vt = u->Union.variants[i];
  2532. if (are_types_identical(v, vt)) {
  2533. is_variant = true;
  2534. break;
  2535. }
  2536. }
  2537. operand->mode = Addressing_Constant;
  2538. operand->type = t_untyped_bool;
  2539. operand->value = exact_value_bool(is_variant);
  2540. }
  2541. break;
  2542. case BuiltinProc_type_struct_field_count:
  2543. operand->value = exact_value_i64(0);
  2544. if (operand->mode != Addressing_Type) {
  2545. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  2546. } else if (!is_type_struct(operand->type)) {
  2547. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  2548. } else {
  2549. Type *bt = base_type(operand->type);
  2550. operand->value = exact_value_i64(bt->Struct.fields.count);
  2551. }
  2552. operand->mode = Addressing_Constant;
  2553. operand->type = t_untyped_integer;
  2554. break;
  2555. case BuiltinProc_type_proc_parameter_count:
  2556. operand->value = exact_value_i64(0);
  2557. if (operand->mode != Addressing_Type) {
  2558. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2559. } else if (!is_type_proc(operand->type)) {
  2560. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2561. } else {
  2562. Type *bt = base_type(operand->type);
  2563. operand->value = exact_value_i64(bt->Proc.param_count);
  2564. }
  2565. operand->mode = Addressing_Constant;
  2566. operand->type = t_untyped_integer;
  2567. break;
  2568. case BuiltinProc_type_proc_return_count:
  2569. operand->value = exact_value_i64(0);
  2570. if (operand->mode != Addressing_Type) {
  2571. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2572. } else if (!is_type_proc(operand->type)) {
  2573. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2574. } else {
  2575. Type *bt = base_type(operand->type);
  2576. operand->value = exact_value_i64(bt->Proc.result_count);
  2577. }
  2578. operand->mode = Addressing_Constant;
  2579. operand->type = t_untyped_integer;
  2580. break;
  2581. case BuiltinProc_type_proc_parameter_type:
  2582. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  2583. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2584. return false;
  2585. } else {
  2586. if (is_type_polymorphic(operand->type)) {
  2587. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  2588. return false;
  2589. }
  2590. Operand op = {};
  2591. check_expr(c, &op, ce->args[1]);
  2592. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  2593. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  2594. return false;
  2595. }
  2596. i64 index = exact_value_to_i64(op.value);
  2597. if (index < 0) {
  2598. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  2599. return false;
  2600. }
  2601. Entity *param = nullptr;
  2602. i64 count = 0;
  2603. Type *bt = base_type(operand->type);
  2604. if (bt->kind == Type_Proc) {
  2605. count = bt->Proc.param_count;
  2606. if (index < count) {
  2607. param = bt->Proc.params->Tuple.variables[index];
  2608. }
  2609. }
  2610. if (index >= count) {
  2611. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  2612. return false;
  2613. }
  2614. GB_ASSERT(param != nullptr);
  2615. switch (param->kind) {
  2616. case Entity_Constant:
  2617. operand->mode = Addressing_Constant;
  2618. operand->type = param->type;
  2619. operand->value = param->Constant.value;
  2620. break;
  2621. case Entity_TypeName:
  2622. case Entity_Variable:
  2623. operand->mode = Addressing_Type;
  2624. operand->type = param->type;
  2625. break;
  2626. default:
  2627. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  2628. break;
  2629. }
  2630. }
  2631. break;
  2632. case BuiltinProc_type_proc_return_type:
  2633. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  2634. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  2635. return false;
  2636. } else {
  2637. if (is_type_polymorphic(operand->type)) {
  2638. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  2639. return false;
  2640. }
  2641. Operand op = {};
  2642. check_expr(c, &op, ce->args[1]);
  2643. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  2644. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  2645. return false;
  2646. }
  2647. i64 index = exact_value_to_i64(op.value);
  2648. if (index < 0) {
  2649. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  2650. return false;
  2651. }
  2652. Entity *param = nullptr;
  2653. i64 count = 0;
  2654. Type *bt = base_type(operand->type);
  2655. if (bt->kind == Type_Proc) {
  2656. count = bt->Proc.result_count;
  2657. if (index < count) {
  2658. param = bt->Proc.results->Tuple.variables[index];
  2659. }
  2660. }
  2661. if (index >= count) {
  2662. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  2663. return false;
  2664. }
  2665. GB_ASSERT(param != nullptr);
  2666. switch (param->kind) {
  2667. case Entity_Constant:
  2668. operand->mode = Addressing_Constant;
  2669. operand->type = param->type;
  2670. operand->value = param->Constant.value;
  2671. break;
  2672. case Entity_TypeName:
  2673. case Entity_Variable:
  2674. operand->mode = Addressing_Type;
  2675. operand->type = param->type;
  2676. break;
  2677. default:
  2678. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  2679. break;
  2680. }
  2681. }
  2682. break;
  2683. case BuiltinProc_type_polymorphic_record_parameter_count:
  2684. operand->value = exact_value_i64(0);
  2685. if (operand->mode != Addressing_Type) {
  2686. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  2687. } else {
  2688. Type *bt = base_type(operand->type);
  2689. if (bt->kind == Type_Struct) {
  2690. if (bt->Struct.polymorphic_params != nullptr) {
  2691. operand->value = exact_value_i64(bt->Struct.polymorphic_params->Tuple.variables.count);
  2692. }
  2693. } else if (bt->kind == Type_Union) {
  2694. if (bt->Union.polymorphic_params != nullptr) {
  2695. operand->value = exact_value_i64(bt->Union.polymorphic_params->Tuple.variables.count);
  2696. }
  2697. } else {
  2698. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  2699. }
  2700. }
  2701. operand->mode = Addressing_Constant;
  2702. operand->type = t_untyped_integer;
  2703. break;
  2704. case BuiltinProc_type_polymorphic_record_parameter_value:
  2705. if (operand->mode != Addressing_Type) {
  2706. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  2707. return false;
  2708. } else if (!is_type_polymorphic_record_specialized(operand->type)) {
  2709. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  2710. return false;
  2711. } else {
  2712. Operand op = {};
  2713. check_expr(c, &op, ce->args[1]);
  2714. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  2715. error(op.expr, "Expected a constant integer for the index of record parameter value");
  2716. return false;
  2717. }
  2718. i64 index = exact_value_to_i64(op.value);
  2719. if (index < 0) {
  2720. error(op.expr, "Expected a non-negative integer for the index of record parameter value, got %lld", cast(long long)index);
  2721. return false;
  2722. }
  2723. Entity *param = nullptr;
  2724. i64 count = 0;
  2725. Type *bt = base_type(operand->type);
  2726. if (bt->kind == Type_Struct) {
  2727. if (bt->Struct.polymorphic_params != nullptr) {
  2728. count = bt->Struct.polymorphic_params->Tuple.variables.count;
  2729. if (index < count) {
  2730. param = bt->Struct.polymorphic_params->Tuple.variables[cast(isize)index];
  2731. }
  2732. }
  2733. } else if (bt->kind == Type_Union) {
  2734. if (bt->Union.polymorphic_params != nullptr) {
  2735. count = bt->Union.polymorphic_params->Tuple.variables.count;
  2736. if (index < count) {
  2737. param = bt->Union.polymorphic_params->Tuple.variables[cast(isize)index];
  2738. }
  2739. }
  2740. } else {
  2741. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  2742. return false;
  2743. }
  2744. if (index >= count) {
  2745. error(op.expr, "Index of record parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  2746. return false;
  2747. }
  2748. GB_ASSERT(param != nullptr);
  2749. switch (param->kind) {
  2750. case Entity_Constant:
  2751. operand->mode = Addressing_Constant;
  2752. operand->type = param->type;
  2753. operand->value = param->Constant.value;
  2754. break;
  2755. case Entity_TypeName:
  2756. operand->mode = Addressing_Type;
  2757. operand->type = param->type;
  2758. break;
  2759. default:
  2760. GB_PANIC("Unhandled polymorphic record type");
  2761. break;
  2762. }
  2763. }
  2764. break;
  2765. case BuiltinProc_type_field_index_of:
  2766. {
  2767. Operand op = {};
  2768. Type *bt = check_type(c, ce->args[0]);
  2769. Type *type = base_type(bt);
  2770. if (type == nullptr || type == t_invalid) {
  2771. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  2772. return false;
  2773. }
  2774. Operand x = {};
  2775. check_expr(c, &x, ce->args[1]);
  2776. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  2777. error(ce->args[1], "Expected a const string for field argument");
  2778. return false;
  2779. }
  2780. String field_name = x.value.value_string;
  2781. Selection sel = lookup_field(type, field_name, false);
  2782. if (sel.entity == nullptr) {
  2783. gbString type_str = type_to_string(bt);
  2784. error(ce->args[0],
  2785. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  2786. gb_string_free(type_str);
  2787. return false;
  2788. }
  2789. if (sel.indirect) {
  2790. gbString type_str = type_to_string(bt);
  2791. error(ce->args[0],
  2792. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  2793. gb_string_free(type_str);
  2794. return false;
  2795. }
  2796. operand->mode = Addressing_Constant;
  2797. operand->value = exact_value_u64(sel.index[0]);
  2798. operand->type = t_uintptr;
  2799. break;
  2800. }
  2801. break;
  2802. case BuiltinProc_type_equal_proc:
  2803. {
  2804. Operand op = {};
  2805. Type *bt = check_type(c, ce->args[0]);
  2806. Type *type = base_type(bt);
  2807. if (type == nullptr || type == t_invalid) {
  2808. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  2809. return false;
  2810. }
  2811. if (!is_type_comparable(type)) {
  2812. gbString t = type_to_string(type);
  2813. error(ce->args[0], "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), t);
  2814. gb_string_free(t);
  2815. return false;
  2816. }
  2817. operand->mode = Addressing_Value;
  2818. operand->type = t_equal_proc;
  2819. break;
  2820. }
  2821. case BuiltinProc_type_hasher_proc:
  2822. {
  2823. Operand op = {};
  2824. Type *bt = check_type(c, ce->args[0]);
  2825. Type *type = base_type(bt);
  2826. if (type == nullptr || type == t_invalid) {
  2827. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  2828. return false;
  2829. }
  2830. if (!is_type_valid_for_keys(type)) {
  2831. gbString t = type_to_string(type);
  2832. error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
  2833. gb_string_free(t);
  2834. return false;
  2835. }
  2836. add_map_key_type_dependencies(c, type);
  2837. operand->mode = Addressing_Value;
  2838. operand->type = t_hasher_proc;
  2839. break;
  2840. }
  2841. }
  2842. return true;
  2843. }