check_builtin.cpp 123 KB

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