check_builtin.cpp 113 KB

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