check_builtin.cpp 151 KB

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