check_builtin.cpp 86 KB

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