check_type.cpp 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670
  1. gb_internal ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location);
  2. gb_internal Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand const &operand);
  3. gb_internal Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> const *operands);
  4. gb_internal void populate_using_array_index(CheckerContext *ctx, Ast *node, AstField *field, Type *t, String name, i32 idx) {
  5. t = base_type(t);
  6. GB_ASSERT(t->kind == Type_Array);
  7. Entity *e = scope_lookup_current(ctx->scope, name);
  8. if (e != nullptr) {
  9. gbString str = nullptr;
  10. defer (gb_string_free(str));
  11. if (node != nullptr) {
  12. str = expr_to_string(node);
  13. }
  14. if (str != nullptr) {
  15. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  16. } else {
  17. error(e->token, "'%.*s' is already declared", LIT(name));
  18. }
  19. } else {
  20. Token tok = make_token_ident(name);
  21. if (field) {
  22. if (field->names.count > 0) {
  23. tok.pos = ast_token(field->names[0]).pos;
  24. } else {
  25. tok.pos = ast_token(field->type).pos;
  26. }
  27. }
  28. Entity *f = alloc_entity_array_elem(nullptr, tok, t->Array.elem, idx);
  29. add_entity(ctx, ctx->scope, nullptr, f);
  30. }
  31. }
  32. gb_internal void populate_using_entity_scope(CheckerContext *ctx, Ast *node, AstField *field, Type *t, isize level) {
  33. if (t == nullptr) {
  34. return;
  35. }
  36. Type *original_type = t;
  37. t = base_type(type_deref(t));
  38. gbString str = nullptr;
  39. defer (gb_string_free(str));
  40. if (node != nullptr) {
  41. str = expr_to_string(node);
  42. }
  43. if (t->kind == Type_Struct) {
  44. for (Entity *f : t->Struct.fields) {
  45. GB_ASSERT(f->kind == Entity_Variable);
  46. String name = f->token.string;
  47. Entity *e = scope_lookup_current(ctx->scope, name);
  48. if (e != nullptr && name != "_") {
  49. gbString ot = type_to_string(original_type);
  50. // TODO(bill): Better type error
  51. if (str != nullptr) {
  52. error(e->token, "'%.*s' is already declared in '%s', through 'using' from '%s'", LIT(name), str, ot);
  53. } else {
  54. error(e->token, "'%.*s' is already declared, through 'using' from '%s'", LIT(name), ot);
  55. }
  56. gb_string_free(ot);
  57. } else {
  58. add_entity(ctx, ctx->scope, nullptr, f);
  59. if (f->flags & EntityFlag_Using) {
  60. populate_using_entity_scope(ctx, node, field, f->type, level+1);
  61. }
  62. }
  63. }
  64. } else if (t->kind == Type_Array && t->Array.count <= 4) {
  65. switch (t->Array.count) {
  66. case 4:
  67. populate_using_array_index(ctx, node, field, t, str_lit("w"), 3);
  68. populate_using_array_index(ctx, node, field, t, str_lit("a"), 3);
  69. /*fallthrough*/
  70. case 3:
  71. populate_using_array_index(ctx, node, field, t, str_lit("z"), 2);
  72. populate_using_array_index(ctx, node, field, t, str_lit("b"), 2);
  73. /*fallthrough*/
  74. case 2:
  75. populate_using_array_index(ctx, node, field, t, str_lit("y"), 1);
  76. populate_using_array_index(ctx, node, field, t, str_lit("g"), 1);
  77. /*fallthrough*/
  78. case 1:
  79. populate_using_array_index(ctx, node, field, t, str_lit("x"), 0);
  80. populate_using_array_index(ctx, node, field, t, str_lit("r"), 0);
  81. /*fallthrough*/
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. gb_internal bool does_field_type_allow_using(Type *t) {
  88. t = base_type(t);
  89. if (is_type_struct(t)) {
  90. return true;
  91. } else if (is_type_array(t)) {
  92. return t->Array.count <= 4;
  93. } else if (is_type_bit_field(t)) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. gb_internal void check_struct_fields(CheckerContext *ctx, Ast *node, Slice<Entity *> *fields, String **tags, Slice<Ast *> const &params,
  99. isize init_field_capacity, Type *struct_type, String context) {
  100. auto fields_array = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
  101. auto tags_array = array_make<String>(heap_allocator(), 0, init_field_capacity);
  102. GB_ASSERT(node->kind == Ast_StructType);
  103. GB_ASSERT(struct_type->kind == Type_Struct);
  104. isize variable_count = 0;
  105. for_array(i, params) {
  106. Ast *field = params[i];
  107. if (ast_node_expect(field, Ast_Field)) {
  108. ast_node(f, Field, field);
  109. variable_count += gb_max(f->names.count, 1);
  110. }
  111. }
  112. i32 field_src_index = 0;
  113. i32 field_group_index = -1;
  114. for_array(i, params) {
  115. Ast *param = params[i];
  116. if (param->kind != Ast_Field) {
  117. continue;
  118. }
  119. field_group_index += 1;
  120. ast_node(p, Field, param);
  121. Ast *type_expr = p->type;
  122. Type *type = nullptr;
  123. CommentGroup *docs = p->docs;
  124. CommentGroup *comment = p->comment;
  125. if (type_expr != nullptr) {
  126. type = check_type_expr(ctx, type_expr, nullptr);
  127. if (is_type_polymorphic(type)) {
  128. struct_type->Struct.is_polymorphic = true;
  129. type = nullptr;
  130. }
  131. }
  132. if (type == nullptr) {
  133. error(params[i], "Invalid parameter type");
  134. type = t_invalid;
  135. }
  136. if (is_type_untyped(type)) {
  137. if (is_type_untyped_uninit(type)) {
  138. error(params[i], "Cannot determine parameter type from ---");
  139. } else {
  140. error(params[i], "Cannot determine parameter type from a nil");
  141. }
  142. type = t_invalid;
  143. }
  144. bool is_using = (p->flags&FieldFlag_using) != 0;
  145. bool is_subtype = (p->flags&FieldFlag_subtype) != 0;
  146. for_array(j, p->names) {
  147. Ast *name = p->names[j];
  148. if (!ast_node_expect2(name, Ast_Ident, Ast_PolyType)) {
  149. continue;
  150. }
  151. if (name->kind == Ast_PolyType) {
  152. name = name->PolyType.type;
  153. }
  154. Token name_token = name->Ident.token;
  155. Entity *field = alloc_entity_field(ctx->scope, name_token, type, is_using, field_src_index);
  156. add_entity(ctx, ctx->scope, name, field);
  157. field->Variable.field_group_index = field_group_index;
  158. if (is_subtype) {
  159. field->flags |= EntityFlag_Subtype;
  160. }
  161. if (j == 0) {
  162. field->Variable.docs = docs;
  163. }
  164. if (j+1 == p->names.count) {
  165. field->Variable.comment = comment;
  166. }
  167. array_add(&fields_array, field);
  168. String tag = p->tag.string;
  169. if (tag.len != 0 && !unquote_string(permanent_allocator(), &tag, 0, tag.text[0] == '`')) {
  170. error(p->tag, "Invalid string literal");
  171. tag = {};
  172. }
  173. array_add(&tags_array, tag);
  174. field_src_index += 1;
  175. }
  176. if (is_using && p->names.count > 0) {
  177. Type *first_type = fields_array[fields_array.count-1]->type;
  178. bool soa_ptr = is_type_soa_pointer(first_type);
  179. Type *t = base_type(type_deref(first_type));
  180. if ((soa_ptr || !does_field_type_allow_using(t)) &&
  181. p->names.count >= 1 &&
  182. p->names[0]->kind == Ast_Ident) {
  183. Token name_token = p->names[0]->Ident.token;
  184. gbString type_str = type_to_string(first_type);
  185. error(name_token, "'using' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  186. gb_string_free(type_str);
  187. continue;
  188. }
  189. populate_using_entity_scope(ctx, node, p, type, 1);
  190. }
  191. if (is_subtype && p->names.count > 0) {
  192. Type *first_type = fields_array[fields_array.count-1]->type;
  193. Type *t = base_type(type_deref(first_type));
  194. if (!does_field_type_allow_using(t) &&
  195. p->names.count >= 1 &&
  196. p->names[0]->kind == Ast_Ident) {
  197. Token name_token = p->names[0]->Ident.token;
  198. gbString type_str = type_to_string(first_type);
  199. error(name_token, "'subtype' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  200. gb_string_free(type_str);
  201. }
  202. }
  203. }
  204. *fields = slice_from_array(fields_array);
  205. *tags = tags_array.data;
  206. }
  207. gb_internal bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_, char const *msg) {
  208. GB_ASSERT(align_ != nullptr);
  209. Operand o = {};
  210. check_expr(ctx, &o, node);
  211. if (o.mode != Addressing_Constant) {
  212. if (o.mode != Addressing_Invalid) {
  213. error(node, "#%s must be a constant", msg);
  214. }
  215. return false;
  216. }
  217. Type *type = base_type(o.type);
  218. if (is_type_untyped(type) || is_type_integer(type)) {
  219. if (o.value.kind == ExactValue_Integer) {
  220. BigInt v = o.value.value_integer;
  221. if (v.used > 1) {
  222. gbAllocator a = heap_allocator();
  223. String str = big_int_to_string(a, &v);
  224. error(node, "#%s too large, %.*s", msg, LIT(str));
  225. gb_free(a, str.text);
  226. return false;
  227. }
  228. i64 align = big_int_to_i64(&v);
  229. if (align < 1 || !gb_is_power_of_two(cast(isize)align)) {
  230. error(node, "#%s must be a power of 2, got %lld", msg, align);
  231. return false;
  232. }
  233. *align_ = align;
  234. return true;
  235. }
  236. }
  237. error(node, "#%s must be an integer", msg);
  238. return false;
  239. }
  240. gb_internal GenTypesData *ensure_polymorphic_record_entity_has_gen_types(CheckerContext *ctx, Type *original_type) {
  241. mutex_lock(&ctx->info->gen_types_mutex); // @@global
  242. GenTypesData *found_gen_types = nullptr;
  243. auto *found_gen_types_ptr = map_get(&ctx->info->gen_types, original_type);
  244. if (found_gen_types_ptr == nullptr) {
  245. GenTypesData *gen_types = gb_alloc_item(permanent_allocator(), GenTypesData);
  246. gen_types->types = array_make<Entity *>(heap_allocator());
  247. map_set(&ctx->info->gen_types, original_type, gen_types);
  248. found_gen_types_ptr = map_get(&ctx->info->gen_types, original_type);
  249. }
  250. found_gen_types = *found_gen_types_ptr;
  251. GB_ASSERT(found_gen_types != nullptr);
  252. mutex_unlock(&ctx->info->gen_types_mutex); // @@global
  253. return found_gen_types;
  254. }
  255. gb_internal void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, Type *named_type, Type *original_type) {
  256. GB_ASSERT(is_type_named(named_type));
  257. gbAllocator a = heap_allocator();
  258. Scope *s = ctx->scope->parent;
  259. Entity *e = nullptr;
  260. {
  261. Token token = ast_token(node);
  262. token.kind = Token_String;
  263. token.string = named_type->Named.name;
  264. Ast *node = ast_ident(nullptr, token);
  265. e = alloc_entity_type_name(s, token, named_type);
  266. e->state = EntityState_Resolved;
  267. e->file = ctx->file;
  268. e->pkg = ctx->pkg;
  269. add_entity_use(ctx, node, e);
  270. }
  271. named_type->Named.type_name = e;
  272. GB_ASSERT(original_type->kind == Type_Named);
  273. e->TypeName.objc_class_name = original_type->Named.type_name->TypeName.objc_class_name;
  274. // TODO(bill): Is this even correct? Or should the metadata be copied?
  275. e->TypeName.objc_metadata = original_type->Named.type_name->TypeName.objc_metadata;
  276. auto *found_gen_types = ensure_polymorphic_record_entity_has_gen_types(ctx, original_type);
  277. mutex_lock(&found_gen_types->mutex);
  278. defer (mutex_unlock(&found_gen_types->mutex));
  279. for (Entity *prev : found_gen_types->types) {
  280. if (prev == e) {
  281. return;
  282. }
  283. }
  284. array_add(&found_gen_types->types, e);
  285. }
  286. bool check_constant_parameter_value(Type *type, Ast *expr) {
  287. if (!is_type_constant_type(type)) {
  288. gbString str = type_to_string(type);
  289. defer (gb_string_free(str));
  290. error(expr, "A parameter must be a valid constant type, got %s", str);
  291. return true;
  292. }
  293. return false;
  294. }
  295. gb_internal Type *check_record_polymorphic_params(CheckerContext *ctx, Ast *polymorphic_params,
  296. bool *is_polymorphic_,
  297. Ast *node, Array<Operand> *poly_operands) {
  298. Type *polymorphic_params_type = nullptr;
  299. GB_ASSERT(is_polymorphic_ != nullptr);
  300. if (polymorphic_params == nullptr) {
  301. if (!*is_polymorphic_) {
  302. *is_polymorphic_ = polymorphic_params != nullptr && poly_operands == nullptr;
  303. }
  304. return polymorphic_params_type;
  305. }
  306. // bool is_variadic = false;
  307. // isize variadic_index = 0;
  308. // bool success = false;
  309. // isize specialization_count = 0;
  310. // polymorphic_params_type = check_get_params(ctx, ctx->scope, polymorphic_params, &is_variadic, &variadic_index, &success, &specialization_count, poly_operands);
  311. // if (success) {
  312. // return nullptr;
  313. // }
  314. bool can_check_fields = true;
  315. ast_node(field_list, FieldList, polymorphic_params);
  316. Slice<Ast *> params = field_list->list;
  317. if (params.count != 0) {
  318. isize variable_count = 0;
  319. for_array(i, params) {
  320. Ast *field = params[i];
  321. if (ast_node_expect(field, Ast_Field)) {
  322. ast_node(f, Field, field);
  323. variable_count += gb_max(f->names.count, 1);
  324. }
  325. }
  326. auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  327. i32 field_group_index = -1;
  328. for_array(i, params) {
  329. Ast *param = params[i];
  330. if (param->kind != Ast_Field) {
  331. continue;
  332. }
  333. field_group_index += 1;
  334. ast_node(p, Field, param);
  335. Ast *type_expr = p->type;
  336. Ast *default_value = unparen_expr(p->default_value);
  337. Type *type = nullptr;
  338. bool is_type_param = false;
  339. bool is_type_polymorphic_type = false;
  340. Type *specialization = nullptr;
  341. if (type_expr == nullptr && default_value == nullptr) {
  342. error(param, "Expected a type for this parameter");
  343. continue;
  344. }
  345. if (type_expr != nullptr) {
  346. if (type_expr->kind == Ast_Ellipsis) {
  347. type_expr = type_expr->Ellipsis.expr;
  348. error(param, "A polymorphic parameter cannot be variadic");
  349. }
  350. if (type_expr->kind == Ast_TypeidType) {
  351. is_type_param = true;
  352. if (type_expr->TypeidType.specialization != nullptr) {
  353. Ast *s = type_expr->TypeidType.specialization;
  354. specialization = check_type(ctx, s);
  355. }
  356. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  357. } else {
  358. type = check_type(ctx, type_expr);
  359. if (is_type_polymorphic(type)) {
  360. is_type_polymorphic_type = true;
  361. }
  362. }
  363. }
  364. ParameterValue param_value = {};
  365. if (default_value != nullptr) {
  366. Type *out_type = nullptr;
  367. param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
  368. if (type == nullptr && out_type != nullptr) {
  369. type = out_type;
  370. }
  371. if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
  372. error(default_value, "Invalid parameter value");
  373. param_value = {};
  374. }
  375. }
  376. if (type == nullptr) {
  377. error(params[i], "Invalid parameter type");
  378. type = t_invalid;
  379. }
  380. if (is_type_untyped(type)) {
  381. if (is_type_untyped_uninit(type)) {
  382. error(params[i], "Cannot determine parameter type from ---");
  383. } else {
  384. error(params[i], "Cannot determine parameter type from a nil");
  385. }
  386. type = t_invalid;
  387. }
  388. if (is_type_polymorphic_type && !is_type_proc(type)) {
  389. gbString str = type_to_string(type);
  390. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  391. gb_string_free(str);
  392. type = t_invalid;
  393. }
  394. if (!is_type_param && check_constant_parameter_value(type, params[i])) {
  395. // failed
  396. }
  397. Scope *scope = ctx->scope;
  398. for_array(j, p->names) {
  399. Ast *name = p->names[j];
  400. if (!ast_node_expect2(name, Ast_Ident, Ast_PolyType)) {
  401. continue;
  402. }
  403. if (name->kind == Ast_PolyType) {
  404. name = name->PolyType.type;
  405. }
  406. Entity *e = nullptr;
  407. Token token = name->Ident.token;
  408. if (poly_operands != nullptr) {
  409. Operand operand = {};
  410. operand.type = t_invalid;
  411. if (entities.count < poly_operands->count) {
  412. operand = (*poly_operands)[entities.count];
  413. } else if (param_value.kind != ParameterValue_Invalid) {
  414. operand.mode = Addressing_Constant;
  415. operand.value = param_value.value;
  416. }
  417. if (is_type_param) {
  418. if (is_type_polymorphic(base_type(operand.type))) {
  419. *is_polymorphic_ = true;
  420. can_check_fields = false;
  421. } else if (specialization &&
  422. !check_type_specialization_to(ctx, specialization, operand.type, false, /*modify_type*/true)) {
  423. if (!ctx->no_polymorphic_errors) {
  424. gbString t = type_to_string(operand.type);
  425. gbString s = type_to_string(specialization);
  426. error(operand.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  427. gb_string_free(s);
  428. gb_string_free(t);
  429. }
  430. }
  431. e = alloc_entity_type_name(scope, token, operand.type);
  432. e->TypeName.is_type_alias = true;
  433. e->flags |= EntityFlag_PolyConst;
  434. } else {
  435. Type *t = operand.type;
  436. if (is_type_proc(type)) {
  437. t = determine_type_from_polymorphic(ctx, type, operand);
  438. }
  439. if (is_type_polymorphic(base_type(t))) {
  440. *is_polymorphic_ = true;
  441. can_check_fields = false;
  442. }
  443. if (e == nullptr) {
  444. e = alloc_entity_const_param(scope, token, t, operand.value, is_type_polymorphic(t));
  445. e->Constant.param_value = param_value;
  446. e->Constant.field_group_index = field_group_index;
  447. }
  448. }
  449. } else {
  450. if (is_type_param) {
  451. e = alloc_entity_type_name(scope, token, type);
  452. e->TypeName.is_type_alias = true;
  453. e->flags |= EntityFlag_PolyConst;
  454. } else {
  455. e = alloc_entity_const_param(scope, token, type, param_value.value, is_type_polymorphic(type));
  456. e->Constant.field_group_index = field_group_index;
  457. e->Constant.param_value = param_value;
  458. }
  459. }
  460. e->state = EntityState_Resolved;
  461. add_entity(ctx, scope, name, e);
  462. array_add(&entities, e);
  463. }
  464. }
  465. if (entities.count > 0) {
  466. Type *tuple = alloc_type_tuple();
  467. tuple->Tuple.variables = slice_from_array(entities);
  468. polymorphic_params_type = tuple;
  469. }
  470. }
  471. if (!*is_polymorphic_) {
  472. *is_polymorphic_ = polymorphic_params != nullptr && poly_operands == nullptr;
  473. }
  474. return polymorphic_params_type;
  475. }
  476. gb_internal bool check_record_poly_operand_specialization(CheckerContext *ctx, Type *record_type, Array<Operand> *poly_operands, bool *is_polymorphic_) {
  477. if (poly_operands == nullptr) {
  478. return false;
  479. }
  480. for (isize i = 0; i < poly_operands->count; i++) {
  481. Operand o = (*poly_operands)[i];
  482. if (is_type_polymorphic(o.type)) {
  483. return false;
  484. }
  485. if (record_type == o.type) {
  486. // NOTE(bill): Cycle
  487. return false;
  488. }
  489. if (o.mode == Addressing_Type) {
  490. // NOTE(bill): ANNOYING EDGE CASE FOR `where` clauses
  491. // TODO(bill, 2021-03-27): Is this even a valid HACK?!
  492. Entity *entity = entity_of_node(o.expr);
  493. if (entity != nullptr &&
  494. entity->kind == Entity_TypeName &&
  495. entity->type == t_typeid) {
  496. *is_polymorphic_ = true;
  497. return false;
  498. }
  499. }
  500. }
  501. return true;
  502. }
  503. gb_internal Entity *find_polymorphic_record_entity(GenTypesData *found_gen_types, isize param_count, Array<Operand> const &ordered_operands) {
  504. for (Entity *e : found_gen_types->types) {
  505. Type *t = base_type(e->type);
  506. TypeTuple *tuple = get_record_polymorphic_params(t);
  507. GB_ASSERT_MSG(tuple != nullptr, "%s :: %s", type_to_string(e->type), type_to_string(t));
  508. GB_ASSERT(param_count == tuple->variables.count);
  509. bool skip = false;
  510. for (isize j = 0; j < param_count; j++) {
  511. Entity *p = tuple->variables[j];
  512. Operand o = {};
  513. if (j < ordered_operands.count) {
  514. o = ordered_operands[j];
  515. }
  516. if (o.expr == nullptr) {
  517. continue;
  518. }
  519. Entity *oe = entity_of_node(o.expr);
  520. if (p == oe) {
  521. // NOTE(bill): This is the same type, make sure that it will be be same thing and use that
  522. // Saves on a lot of checking too below
  523. continue;
  524. }
  525. if (p->kind == Entity_TypeName) {
  526. if (is_type_polymorphic(o.type)) {
  527. // NOTE(bill): Do not add polymorphic version to the gen_types
  528. skip = true;
  529. break;
  530. }
  531. if (!are_types_identical(o.type, p->type)) {
  532. skip = true;
  533. break;
  534. }
  535. } else if (p->kind == Entity_Constant) {
  536. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  537. skip = true;
  538. break;
  539. }
  540. if (!are_types_identical(o.type, p->type)) {
  541. skip = true;
  542. break;
  543. }
  544. } else {
  545. GB_PANIC("Unknown entity kind");
  546. }
  547. }
  548. if (!skip) {
  549. return e;
  550. }
  551. }
  552. return nullptr;
  553. };
  554. gb_internal void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  555. GB_ASSERT(is_type_struct(struct_type));
  556. ast_node(st, StructType, node);
  557. String context = str_lit("struct");
  558. isize min_field_count = 0;
  559. for_array(field_index, st->fields) {
  560. Ast *field = st->fields[field_index];
  561. switch (field->kind) {
  562. case_ast_node(f, ValueDecl, field);
  563. min_field_count += f->names.count;
  564. case_end;
  565. case_ast_node(f, Field, field);
  566. min_field_count += f->names.count;
  567. case_end;
  568. }
  569. }
  570. scope_reserve(ctx->scope, min_field_count);
  571. if (st->is_raw_union && min_field_count > 1) {
  572. struct_type->Struct.is_raw_union = true;
  573. context = str_lit("struct #raw_union");
  574. }
  575. struct_type->Struct.scope = ctx->scope;
  576. struct_type->Struct.is_packed = st->is_packed;
  577. struct_type->Struct.is_no_copy = st->is_no_copy;
  578. struct_type->Struct.polymorphic_params = check_record_polymorphic_params(
  579. ctx, st->polymorphic_params,
  580. &struct_type->Struct.is_polymorphic,
  581. node, poly_operands
  582. );
  583. wait_signal_set(&struct_type->Struct.polymorphic_wait_signal);
  584. struct_type->Struct.is_poly_specialized = check_record_poly_operand_specialization(ctx, struct_type, poly_operands, &struct_type->Struct.is_polymorphic);
  585. if (original_type_for_poly) {
  586. GB_ASSERT(named_type != nullptr);
  587. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  588. }
  589. if (!struct_type->Struct.is_polymorphic) {
  590. if (st->where_clauses.count > 0 && st->polymorphic_params == nullptr) {
  591. error(st->where_clauses[0], "'where' clauses can only be used on structures with polymorphic parameters");
  592. } else {
  593. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &st->where_clauses, true);
  594. gb_unused(where_clause_ok);
  595. }
  596. check_struct_fields(ctx, node, &struct_type->Struct.fields, &struct_type->Struct.tags, st->fields, min_field_count, struct_type, context);
  597. wait_signal_set(&struct_type->Struct.fields_wait_signal);
  598. }
  599. #define ST_ALIGN(_name) if (st->_name != nullptr) { \
  600. if (st->is_packed) { \
  601. syntax_error(st->_name, "'#%s' cannot be applied with '#packed'", #_name); \
  602. return; \
  603. } \
  604. i64 align = 1; \
  605. if (check_custom_align(ctx, st->_name, &align, #_name)) { \
  606. struct_type->Struct.custom_##_name = align; \
  607. } \
  608. }
  609. ST_ALIGN(field_align);
  610. ST_ALIGN(align);
  611. if (struct_type->Struct.custom_align < struct_type->Struct.custom_field_align) {
  612. warning(st->align, "#align(%lld) is defined to be less than #field_name(%lld)",
  613. cast(long long)struct_type->Struct.custom_align,
  614. cast(long long)struct_type->Struct.custom_field_align);
  615. }
  616. #undef ST_ALIGN
  617. }
  618. gb_internal void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  619. GB_ASSERT(is_type_union(union_type));
  620. ast_node(ut, UnionType, node);
  621. union_type->Union.scope = ctx->scope;
  622. union_type->Union.polymorphic_params = check_record_polymorphic_params(
  623. ctx, ut->polymorphic_params,
  624. &union_type->Union.is_polymorphic,
  625. node, poly_operands
  626. );
  627. wait_signal_set(&union_type->Union.polymorphic_wait_signal);
  628. union_type->Union.is_poly_specialized = check_record_poly_operand_specialization(ctx, union_type, poly_operands, &union_type->Union.is_polymorphic);
  629. if (original_type_for_poly) {
  630. GB_ASSERT(named_type != nullptr);
  631. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  632. }
  633. if (!union_type->Union.is_polymorphic) {
  634. if (ut->where_clauses.count > 0 && ut->polymorphic_params == nullptr) {
  635. error(ut->where_clauses[0], "'where' clauses can only be used on unions with polymorphic parameters");
  636. } else {
  637. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &ut->where_clauses, true);
  638. gb_unused(where_clause_ok);
  639. }
  640. }
  641. auto variants = array_make<Type *>(permanent_allocator(), 0, ut->variants.count);
  642. for_array(i, ut->variants) {
  643. Ast *node = ut->variants[i];
  644. Type *t = check_type_expr(ctx, node, nullptr);
  645. if (union_type->Union.is_polymorphic && poly_operands == nullptr) {
  646. // NOTE(bill): don't add any variants if this is this is an unspecialized polymorphic record
  647. continue;
  648. }
  649. if (t != nullptr && t != t_invalid) {
  650. bool ok = true;
  651. t = default_type(t);
  652. if (is_type_untyped(t) || is_type_empty_union(t)) {
  653. ok = false;
  654. gbString str = type_to_string(t);
  655. error(node, "Invalid variant type in union '%s'", str);
  656. gb_string_free(str);
  657. } else {
  658. for_array(j, variants) {
  659. if (are_types_identical(t, variants[j])) {
  660. ok = false;
  661. ERROR_BLOCK();
  662. gbString str = type_to_string(t);
  663. error(node, "Duplicate variant type '%s'", str);
  664. if (j < ut->variants.count) {
  665. error_line("\tPrevious found at %s\n", token_pos_to_string(ast_token(ut->variants[j]).pos));
  666. }
  667. gb_string_free(str);
  668. break;
  669. }
  670. }
  671. }
  672. if (ok) {
  673. array_add(&variants, t);
  674. if (ut->kind == UnionType_shared_nil) {
  675. if (!type_has_nil(t)) {
  676. gbString s = type_to_string(t);
  677. error(node, "Each variant of a union with #shared_nil must have a 'nil' value, got %s", s);
  678. gb_string_free(s);
  679. }
  680. }
  681. }
  682. }
  683. }
  684. union_type->Union.variants = slice_from_array(variants);
  685. union_type->Union.kind = ut->kind;
  686. switch (ut->kind) {
  687. case UnionType_no_nil:
  688. if (union_type->Union.is_polymorphic && poly_operands == nullptr) {
  689. GB_ASSERT(variants.count == 0);
  690. if (ut->variants.count != 1) {
  691. break;
  692. }
  693. }
  694. if (variants.count < 2) {
  695. error(node, "A union with #no_nil must have at least 2 variants");
  696. }
  697. break;
  698. }
  699. if (ut->align != nullptr) {
  700. i64 custom_align = 1;
  701. if (check_custom_align(ctx, ut->align, &custom_align, "align")) {
  702. if (variants.count == 0) {
  703. error(ut->align, "An empty union cannot have a custom alignment");
  704. } else {
  705. union_type->Union.custom_align = custom_align;
  706. }
  707. }
  708. }
  709. }
  710. gb_internal void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast *node) {
  711. ast_node(et, EnumType, node);
  712. GB_ASSERT(is_type_enum(enum_type));
  713. enum_type->Enum.base_type = t_int;
  714. enum_type->Enum.scope = ctx->scope;
  715. Type *base_type = t_int;
  716. if (unparen_expr(et->base_type) != nullptr) {
  717. base_type = check_type(ctx, et->base_type);
  718. }
  719. if (base_type == nullptr || base_type == t_invalid || !is_type_integer(base_type)) {
  720. error(node, "Base type for enumeration must be an integer");
  721. return;
  722. }
  723. if (is_type_enum(base_type)) {
  724. error(node, "Base type for enumeration cannot be another enumeration");
  725. return;
  726. }
  727. if (is_type_integer_128bit(base_type)) {
  728. error(node, "Base type for enumeration cannot be a 128-bit integer");
  729. return;
  730. }
  731. // NOTE(bill): Must be up here for the 'check_init_constant' system
  732. enum_type->Enum.base_type = base_type;
  733. auto fields = array_make<Entity *>(permanent_allocator(), 0, et->fields.count);
  734. Type *constant_type = enum_type;
  735. if (named_type != nullptr) {
  736. constant_type = named_type;
  737. }
  738. ExactValue iota = exact_value_i64(-1);
  739. ExactValue min_value = exact_value_i64(0);
  740. ExactValue max_value = exact_value_i64(0);
  741. isize min_value_index = 0;
  742. isize max_value_index = 0;
  743. bool min_value_set = false;
  744. bool max_value_set = false;
  745. scope_reserve(ctx->scope, et->fields.count);
  746. for_array(i, et->fields) {
  747. Ast *field = et->fields[i];
  748. Ast *ident = nullptr;
  749. Ast *init = nullptr;
  750. u32 entity_flags = 0;
  751. if (field->kind != Ast_EnumFieldValue) {
  752. error(field, "An enum field's name must be an identifier");
  753. continue;
  754. }
  755. ident = field->EnumFieldValue.name;
  756. init = field->EnumFieldValue.value;
  757. if (ident == nullptr || ident->kind != Ast_Ident) {
  758. error(field, "An enum field's name must be an identifier");
  759. continue;
  760. }
  761. CommentGroup *docs = field->EnumFieldValue.docs;
  762. CommentGroup *comment = field->EnumFieldValue.comment;
  763. String name = ident->Ident.token.string;
  764. if (init != nullptr) {
  765. Operand o = {};
  766. check_expr(ctx, &o, init);
  767. if (o.mode != Addressing_Constant) {
  768. error(init, "Enumeration value must be a constant");
  769. o.mode = Addressing_Invalid;
  770. }
  771. if (o.mode != Addressing_Invalid) {
  772. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  773. }
  774. if (o.mode != Addressing_Invalid) {
  775. iota = o.value;
  776. } else {
  777. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  778. }
  779. } else {
  780. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  781. entity_flags |= EntityConstantFlag_ImplicitEnumValue;
  782. }
  783. // NOTE(bill): Skip blank identifiers
  784. if (is_blank_ident(name)) {
  785. continue;
  786. } else if (name == "names") {
  787. error(field, "'names' is a reserved identifier for enumerations");
  788. continue;
  789. }
  790. if (min_value_set) {
  791. if (compare_exact_values(Token_Gt, min_value, iota)) {
  792. min_value_index = i;
  793. min_value = iota;
  794. }
  795. } else {
  796. min_value_index = i;
  797. min_value = iota;
  798. min_value_set = true;
  799. }
  800. if (max_value_set) {
  801. if (compare_exact_values(Token_Lt, max_value, iota)) {
  802. max_value_index = i;
  803. max_value = iota;
  804. }
  805. } else {
  806. max_value_index = i;
  807. max_value = iota;
  808. max_value_set = true;
  809. }
  810. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  811. e->identifier = ident;
  812. e->flags |= EntityFlag_Visited;
  813. e->state = EntityState_Resolved;
  814. e->Constant.flags |= entity_flags;
  815. e->Constant.docs = docs;
  816. e->Constant.comment = comment;
  817. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  818. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  819. } else {
  820. add_entity(ctx, ctx->scope, nullptr, e);
  821. array_add(&fields, e);
  822. // TODO(bill): Should I add a use for the enum value?
  823. add_entity_use(ctx, field, e);
  824. }
  825. }
  826. GB_ASSERT(fields.count <= et->fields.count);
  827. enum_type->Enum.fields = fields;
  828. *enum_type->Enum.min_value = min_value;
  829. *enum_type->Enum.max_value = max_value;
  830. enum_type->Enum.min_value_index = min_value_index;
  831. enum_type->Enum.max_value_index = max_value_index;
  832. }
  833. gb_internal bool is_valid_bit_field_backing_type(Type *type) {
  834. if (type == nullptr) {
  835. return false;
  836. }
  837. type = base_type(type);
  838. if (is_type_untyped(type)) {
  839. return false;
  840. }
  841. if (is_type_integer(type)) {
  842. return true;
  843. }
  844. if (type->kind == Type_Array) {
  845. return is_type_integer(type->Array.elem);
  846. }
  847. return false;
  848. }
  849. gb_internal void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Type *named_type, Ast *node) {
  850. ast_node(bf, BitFieldType, node);
  851. GB_ASSERT(is_type_bit_field(bit_field_type));
  852. Type *backing_type = check_type(ctx, bf->backing_type);
  853. bit_field_type->BitField.backing_type = backing_type ? backing_type : t_u8;
  854. bit_field_type->BitField.scope = ctx->scope;
  855. if (backing_type == nullptr) {
  856. error(bf->backing_type, "Backing type for a bit_field must be an integer or an array of an integer");
  857. return;
  858. }
  859. if (!is_valid_bit_field_backing_type(backing_type)) {
  860. error(bf->backing_type, "Backing type for a bit_field must be an integer or an array of an integer");
  861. return;
  862. }
  863. auto fields = array_make<Entity *>(permanent_allocator(), 0, bf->fields.count);
  864. auto bit_sizes = array_make<u8> (permanent_allocator(), 0, bf->fields.count);
  865. auto tags = array_make<String> (permanent_allocator(), 0, bf->fields.count);
  866. u64 maximum_bit_size = 8 * type_size_of(backing_type);
  867. u64 total_bit_size = 0;
  868. for_array(i, bf->fields) {
  869. i32 field_src_index = cast(i32)i;
  870. Ast *field = bf->fields[i];
  871. if (field->kind != Ast_BitFieldField) {
  872. error(field, "Invalid AST for a bit_field");
  873. continue;
  874. }
  875. ast_node(f, BitFieldField, field);
  876. if (f->name == nullptr || f->name->kind != Ast_Ident) {
  877. error(field, "A bit_field's field name must be an identifier");
  878. continue;
  879. }
  880. CommentGroup *docs = f->docs;
  881. CommentGroup *comment = f->comment;
  882. String name = f->name->Ident.token.string;
  883. if (f->type == nullptr) {
  884. error(field, "A bit_field's field must have a type");
  885. continue;
  886. }
  887. Type *type = check_type(ctx, f->type);
  888. if (type_size_of(type) > 8) {
  889. error(f->type, "The type of a bit_field's field must be <= 8 bytes, got %lld", cast(long long)type_size_of(type));
  890. }
  891. if (is_type_untyped(type)) {
  892. gbString s = type_to_string(type);
  893. error(f->type, "The type of a bit_field's field must be a typed integer, enum, or boolean, got %s", s);
  894. gb_string_free(s);
  895. } else if (!(is_type_integer(type) || is_type_enum(type) || is_type_boolean(type))) {
  896. gbString s = type_to_string(type);
  897. error(f->type, "The type of a bit_field's field must be an integer, enum, or boolean, got %s", s);
  898. gb_string_free(s);
  899. }
  900. if (f->bit_size == nullptr) {
  901. error(field, "A bit_field's field must have a specified bit size");
  902. continue;
  903. }
  904. Operand o = {};
  905. check_expr(ctx, &o, f->bit_size);
  906. if (o.mode != Addressing_Constant) {
  907. error(f->bit_size, "A bit_field's specified bit size must be a constant");
  908. o.mode = Addressing_Invalid;
  909. }
  910. if (o.value.kind == ExactValue_Float) {
  911. o.value = exact_value_to_integer(o.value);
  912. }
  913. if (f->bit_size->kind == Ast_BinaryExpr && f->bit_size->BinaryExpr.op.kind == Token_Or) {
  914. gbString s = expr_to_string(f->bit_size);
  915. error(f->bit_size, "Wrap the expression in parentheses, e.g. (%s)", s);
  916. gb_string_free(s);
  917. }
  918. ExactValue bit_size = o.value;
  919. if (bit_size.kind != ExactValue_Integer) {
  920. gbString s = expr_to_string(f->bit_size);
  921. error(f->bit_size, "Expected an integer constant value for the specified bit size, got %s", s);
  922. gb_string_free(s);
  923. }
  924. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  925. error(f->name, "'%.*s' is already declared in this bit_field", LIT(name));
  926. } else {
  927. i64 bit_size_i64 = exact_value_to_i64(bit_size);
  928. u8 bit_size_u8 = 0;
  929. if (bit_size_i64 <= 0) {
  930. error(f->bit_size, "A bit_field's specified bit size cannot be <= 0, got %lld", cast(long long)bit_size_i64);
  931. bit_size_i64 = 1;
  932. }
  933. if (bit_size_i64 > 64) {
  934. error(f->bit_size, "A bit_field's specified bit size cannot exceed 64 bits, got %lld", cast(long long)bit_size_i64);
  935. bit_size_i64 = 64;
  936. }
  937. i64 sz = 8*type_size_of(type);
  938. if (bit_size_i64 > sz) {
  939. error(f->bit_size, "A bit_field's specified bit size cannot exceed its type, got %lld, expect <=%lld", cast(long long)bit_size_i64, cast(long long)sz);
  940. bit_size_i64 = sz;
  941. }
  942. bit_size_u8 = cast(u8)bit_size_i64;
  943. Entity *e = alloc_entity_field(ctx->scope, f->name->Ident.token, type, false, field_src_index);
  944. e->Variable.docs = docs;
  945. e->Variable.comment = comment;
  946. e->Variable.bit_field_bit_size = bit_size_u8;
  947. e->flags |= EntityFlag_BitFieldField;
  948. add_entity(ctx, ctx->scope, nullptr, e);
  949. array_add(&fields, e);
  950. array_add(&bit_sizes, bit_size_u8);
  951. String tag = f->tag.string;
  952. if (tag.len != 0 && !unquote_string(permanent_allocator(), &tag, 0, tag.text[0] == '`')) {
  953. error(f->tag, "Invalid string literal");
  954. tag = {};
  955. }
  956. array_add(&tags, tag);
  957. add_entity_use(ctx, field, e);
  958. total_bit_size += bit_size_u8;
  959. }
  960. }
  961. GB_ASSERT(fields.count <= bf->fields.count);
  962. auto bit_offsets = slice_make<i64>(permanent_allocator(), fields.count);
  963. i64 curr_offset = 0;
  964. for_array(i, bit_sizes) {
  965. bit_offsets[i] = curr_offset;
  966. curr_offset += cast(i64)bit_sizes[i];
  967. }
  968. if (total_bit_size > maximum_bit_size) {
  969. gbString s = type_to_string(backing_type);
  970. error(node, "The total bit size of a bit_field's fields (%llu) must fit into its backing type's (%s) bit size of %llu",
  971. cast(unsigned long long)total_bit_size,
  972. s,
  973. cast(unsigned long long)maximum_bit_size);
  974. gb_string_free(s);
  975. }
  976. enum EndianKind {
  977. Endian_Unknown,
  978. Endian_Native,
  979. Endian_Little,
  980. Endian_Big,
  981. };
  982. auto const &determine_endian_kind = [](Type *type) -> EndianKind {
  983. if (is_type_boolean(type)) {
  984. // NOTE(bill): it doesn't matter, and when it does,
  985. // that api is absolutely stupid
  986. return Endian_Unknown;
  987. } else if (is_type_endian_specific(type)) {
  988. if (is_type_endian_little(type)) {
  989. return Endian_Little;
  990. } else {
  991. return Endian_Big;
  992. }
  993. }
  994. return Endian_Native;
  995. };
  996. Type *backing_type_elem = core_array_type(backing_type);
  997. i64 backing_type_elem_size = type_size_of(backing_type_elem);
  998. EndianKind backing_type_endian_kind = determine_endian_kind(backing_type_elem);
  999. EndianKind endian_kind = Endian_Unknown;
  1000. for (Entity *f : fields) {
  1001. EndianKind field_kind = determine_endian_kind(f->type);
  1002. i64 field_size = type_size_of(f->type);
  1003. if (field_kind && backing_type_endian_kind != field_kind && field_size > 1 && backing_type_elem_size > 1) {
  1004. error(f->token, "All 'bit_field' field types must match the same endian kind as the backing type, i.e. all native, all little, or all big");
  1005. }
  1006. if (endian_kind == Endian_Unknown) {
  1007. endian_kind = field_kind;
  1008. } else if (field_kind && endian_kind != field_kind && field_size > 1) {
  1009. error(f->token, "All 'bit_field' field types must be of the same endian variety, i.e. all native, all little, or all big");
  1010. }
  1011. }
  1012. if (bit_sizes.count > 0 && is_type_integer(backing_type)) {
  1013. bool all_booleans = is_type_boolean(fields[0]->type);
  1014. bool all_ones = bit_sizes[0] == 1;
  1015. if (all_ones && all_booleans) {
  1016. for_array(i, bit_sizes) {
  1017. all_ones = bit_sizes[i] == 1;
  1018. if (!all_ones) {
  1019. break;
  1020. }
  1021. all_booleans = is_type_boolean(fields[i]->type);
  1022. if (!all_booleans) {
  1023. break;
  1024. }
  1025. }
  1026. if (all_ones && all_booleans) {
  1027. if (ast_file_vet_style(ctx->file)) {
  1028. char const *msg = "This 'bit_field' is better expressed as a 'bit_set' since all of the fields are booleans, of 1-bit in size, and the backing type is an integer (-vet-style)";
  1029. error(node, msg);
  1030. } else {
  1031. char const *msg = "This 'bit_field' might be better expressed as a 'bit_set' since all of the fields are booleans, of 1-bit in size, and the backing type is an integer";
  1032. warning(node, msg);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. bit_field_type->BitField.fields = slice_from_array(fields);
  1038. bit_field_type->BitField.bit_sizes = slice_from_array(bit_sizes);
  1039. bit_field_type->BitField.bit_offsets = bit_offsets;
  1040. bit_field_type->BitField.tags = tags.data;
  1041. }
  1042. gb_internal bool is_type_valid_bit_set_range(Type *t) {
  1043. if (is_type_integer(t)) {
  1044. return true;
  1045. }
  1046. if (is_type_rune(t)) {
  1047. return true;
  1048. }
  1049. return false;
  1050. }
  1051. gb_internal void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  1052. ast_node(bs, BitSetType, node);
  1053. GB_ASSERT(type->kind == Type_BitSet);
  1054. type->BitSet.node = node;
  1055. /* i64 const DEFAULT_BITS = cast(i64)(8*build_context.word_size); */
  1056. i64 const MAX_BITS = 128;
  1057. Ast *base = unparen_expr(bs->elem);
  1058. if (is_ast_range(base)) {
  1059. ast_node(be, BinaryExpr, base);
  1060. Operand lhs = {};
  1061. Operand rhs = {};
  1062. check_expr(c, &lhs, be->left);
  1063. check_expr(c, &rhs, be->right);
  1064. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  1065. return;
  1066. }
  1067. convert_to_typed(c, &lhs, rhs.type);
  1068. if (lhs.mode == Addressing_Invalid) {
  1069. return;
  1070. }
  1071. convert_to_typed(c, &rhs, lhs.type);
  1072. if (rhs.mode == Addressing_Invalid) {
  1073. return;
  1074. }
  1075. if (!are_types_identical(lhs.type, rhs.type)) {
  1076. if (lhs.type != t_invalid &&
  1077. rhs.type != t_invalid) {
  1078. gbString xt = type_to_string(lhs.type);
  1079. gbString yt = type_to_string(rhs.type);
  1080. gbString expr_str = expr_to_string(bs->elem);
  1081. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  1082. gb_string_free(expr_str);
  1083. gb_string_free(yt);
  1084. gb_string_free(xt);
  1085. }
  1086. return;
  1087. }
  1088. if (!is_type_valid_bit_set_range(lhs.type)) {
  1089. gbString str = type_to_string(lhs.type);
  1090. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  1091. gb_string_free(str);
  1092. return;
  1093. }
  1094. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  1095. error(bs->elem, "Intervals must be constant values");
  1096. return;
  1097. }
  1098. ExactValue iv = exact_value_to_integer(lhs.value);
  1099. ExactValue jv = exact_value_to_integer(rhs.value);
  1100. GB_ASSERT(iv.kind == ExactValue_Integer);
  1101. GB_ASSERT(jv.kind == ExactValue_Integer);
  1102. BigInt i = iv.value_integer;
  1103. BigInt j = jv.value_integer;
  1104. if (big_int_cmp(&i, &j) > 0) {
  1105. gbAllocator a = heap_allocator();
  1106. String si = big_int_to_string(a, &i);
  1107. String sj = big_int_to_string(a, &j);
  1108. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  1109. gb_free(a, si.text);
  1110. gb_free(a, sj.text);
  1111. return;
  1112. }
  1113. Type *t = default_type(lhs.type);
  1114. if (bs->underlying != nullptr) {
  1115. Type *u = check_type(c, bs->underlying);
  1116. if (!is_type_integer(u)) {
  1117. gbString ts = type_to_string(u);
  1118. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1119. gb_string_free(ts);
  1120. return;
  1121. }
  1122. type->BitSet.underlying = u;
  1123. }
  1124. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1125. gbAllocator a = heap_allocator();
  1126. String s = big_int_to_string(a, &i);
  1127. gbString ts = type_to_string(t);
  1128. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1129. gb_string_free(ts);
  1130. gb_free(a, s.text);
  1131. return;
  1132. }
  1133. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1134. gbAllocator a = heap_allocator();
  1135. String s = big_int_to_string(a, &j);
  1136. gbString ts = type_to_string(t);
  1137. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1138. gb_string_free(ts);
  1139. gb_free(a, s.text);
  1140. return;
  1141. }
  1142. i64 lower = big_int_to_i64(&i);
  1143. i64 upper = big_int_to_i64(&j);
  1144. i64 actual_lower = lower;
  1145. i64 bits = MAX_BITS;
  1146. if (type->BitSet.underlying != nullptr) {
  1147. bits = 8*type_size_of(type->BitSet.underlying);
  1148. if (lower > 0) {
  1149. actual_lower = 0;
  1150. } else if (lower < 0) {
  1151. error(bs->elem, "bit_set does not allow a negative lower bound (%lld) when an underlying type is set", lower);
  1152. }
  1153. }
  1154. i64 bits_required = upper-actual_lower;
  1155. switch (be->op.kind) {
  1156. case Token_Ellipsis:
  1157. case Token_RangeFull:
  1158. bits_required += 1;
  1159. break;
  1160. }
  1161. bool is_valid = true;
  1162. switch (be->op.kind) {
  1163. case Token_Ellipsis:
  1164. case Token_RangeFull:
  1165. if (upper - lower >= bits) {
  1166. is_valid = false;
  1167. }
  1168. break;
  1169. case Token_RangeHalf:
  1170. if (upper - lower > bits) {
  1171. is_valid = false;
  1172. }
  1173. upper -= 1;
  1174. break;
  1175. }
  1176. if (!is_valid) {
  1177. if (actual_lower != lower) {
  1178. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required (internally the lower bound was changed to 0 as an underlying type was set)", bits, bits_required);
  1179. } else {
  1180. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  1181. }
  1182. }
  1183. type->BitSet.elem = t;
  1184. type->BitSet.lower = lower;
  1185. type->BitSet.upper = upper;
  1186. } else {
  1187. Type *elem = check_type_expr(c, bs->elem, nullptr);
  1188. type->BitSet.elem = elem;
  1189. if (!is_type_valid_bit_set_elem(elem)) {
  1190. error(bs->elem, "Expected an enum type for a bit_set");
  1191. } else {
  1192. Type *et = base_type(elem);
  1193. if (et->kind == Type_Enum) {
  1194. if (!is_type_integer(et->Enum.base_type)) {
  1195. error(bs->elem, "Enum type for bit_set must be an integer");
  1196. return;
  1197. }
  1198. i64 lower = I64_MAX;
  1199. i64 upper = I64_MIN;
  1200. for_array(i, et->Enum.fields) {
  1201. Entity *e = et->Enum.fields[i];
  1202. if (e->kind != Entity_Constant) {
  1203. continue;
  1204. }
  1205. ExactValue value = exact_value_to_integer(e->Constant.value);
  1206. GB_ASSERT(value.kind == ExactValue_Integer);
  1207. // NOTE(bill): enum types should be able to store i64 values
  1208. i64 x = big_int_to_i64(&value.value_integer);
  1209. lower = gb_min(lower, x);
  1210. upper = gb_max(upper, x);
  1211. }
  1212. if (et->Enum.fields.count == 0) {
  1213. lower = 0;
  1214. upper = 0;
  1215. }
  1216. GB_ASSERT(lower <= upper);
  1217. bool lower_changed = false;
  1218. i64 bits = MAX_BITS
  1219. ; if (bs->underlying != nullptr) {
  1220. Type *u = check_type(c, bs->underlying);
  1221. if (!is_type_integer(u)) {
  1222. gbString ts = type_to_string(u);
  1223. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1224. gb_string_free(ts);
  1225. return;
  1226. }
  1227. type->BitSet.underlying = u;
  1228. bits = 8*type_size_of(u);
  1229. if (lower > 0) {
  1230. lower = 0;
  1231. lower_changed = true;
  1232. } else if (lower < 0) {
  1233. gbString s = type_to_string(elem);
  1234. error(bs->elem, "bit_set does not allow a negative lower bound (%lld) of the element type '%s' when an underlying type is set", lower, s);
  1235. gb_string_free(s);
  1236. }
  1237. }
  1238. if (upper - lower >= bits) {
  1239. i64 bits_required = upper-lower+1;
  1240. if (lower_changed) {
  1241. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required (internally the lower bound was changed to 0 as an underlying type was set)", bits, bits_required);
  1242. } else {
  1243. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  1244. }
  1245. }
  1246. type->BitSet.lower = lower;
  1247. type->BitSet.upper = upper;
  1248. }
  1249. }
  1250. }
  1251. }
  1252. gb_internal bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  1253. if (type == nullptr ||
  1254. type == t_invalid) {
  1255. return true;
  1256. }
  1257. Type *t = base_type(type);
  1258. Type *s = base_type(specialization);
  1259. if (t->kind != s->kind) {
  1260. if (t->kind == Type_EnumeratedArray && s->kind == Type_Array) {
  1261. // Might be okay, check later
  1262. } else {
  1263. return false;
  1264. }
  1265. }
  1266. if (is_type_untyped(t)) {
  1267. Operand o = {Addressing_Value};
  1268. o.type = default_type(type);
  1269. bool can_convert = check_cast_internal(ctx, &o, specialization);
  1270. return can_convert;
  1271. } else if (t->kind == Type_Struct) {
  1272. if (t->Struct.polymorphic_parent == nullptr &&
  1273. t == s) {
  1274. return true;
  1275. }
  1276. if (t->Struct.polymorphic_parent == specialization) {
  1277. return true;
  1278. }
  1279. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  1280. s->Struct.polymorphic_params != nullptr &&
  1281. t->Struct.polymorphic_params != nullptr) {
  1282. TypeTuple *s_tuple = get_record_polymorphic_params(s);
  1283. TypeTuple *t_tuple = get_record_polymorphic_params(t);
  1284. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1285. for_array(i, s_tuple->variables) {
  1286. Entity *s_e = s_tuple->variables[i];
  1287. Entity *t_e = t_tuple->variables[i];
  1288. Type *st = s_e->type;
  1289. Type *tt = t_e->type;
  1290. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1291. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1292. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1293. GB_ASSERT(e != nullptr);
  1294. if (modify_type) {
  1295. e->kind = Entity_Constant;
  1296. e->Constant.value = t_e->Constant.value;
  1297. e->type = t_e->type;
  1298. }
  1299. } else {
  1300. if (st->kind == Type_Basic && tt->kind == Type_Basic &&
  1301. s_e->kind == Entity_Constant && t_e->kind == Entity_Constant) {
  1302. if (!compare_exact_values(Token_CmpEq, s_e->Constant.value, t_e->Constant.value))
  1303. return false;
  1304. } else {
  1305. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1306. if (!ok) {
  1307. // TODO(bill, 2021-08-19): is this logic correct?
  1308. return false;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. if (modify_type) {
  1314. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1315. gb_memmove(specialization, type, gb_size_of(Type));
  1316. }
  1317. return true;
  1318. }
  1319. } else if (t->kind == Type_Union) {
  1320. if (t->Union.polymorphic_parent == nullptr &&
  1321. t == s) {
  1322. return true;
  1323. }
  1324. if (t->Union.polymorphic_parent == specialization) {
  1325. return true;
  1326. }
  1327. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1328. s->Union.polymorphic_params != nullptr &&
  1329. t->Union.polymorphic_params != nullptr) {
  1330. TypeTuple *s_tuple = get_record_polymorphic_params(s);
  1331. TypeTuple *t_tuple = get_record_polymorphic_params(t);
  1332. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1333. for_array(i, s_tuple->variables) {
  1334. Entity *s_e = s_tuple->variables[i];
  1335. Entity *t_e = t_tuple->variables[i];
  1336. Type *st = s_e->type;
  1337. Type *tt = t_e->type;
  1338. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1339. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1340. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1341. GB_ASSERT(e != nullptr);
  1342. if (modify_type) {
  1343. e->kind = Entity_Constant;
  1344. e->Constant.value = t_e->Constant.value;
  1345. e->type = t_e->type;
  1346. }
  1347. } else {
  1348. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1349. if (!ok) {
  1350. // TODO(bill, 2021-08-19): is this logic correct?
  1351. return false;
  1352. }
  1353. }
  1354. }
  1355. if (modify_type) {
  1356. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1357. gb_memmove(specialization, type, gb_size_of(Type));
  1358. }
  1359. return true;
  1360. }
  1361. }
  1362. if (specialization->kind == Type_Named &&
  1363. type->kind != Type_Named) {
  1364. return false;
  1365. }
  1366. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1367. return true;
  1368. }
  1369. return false;
  1370. }
  1371. gb_internal Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand const &operand) {
  1372. bool modify_type = !ctx->no_polymorphic_errors;
  1373. bool show_error = modify_type && !ctx->hide_polymorphic_errors;
  1374. if (!is_operand_value(operand)) {
  1375. if (show_error) {
  1376. gbString pts = type_to_string(poly_type);
  1377. gbString ots = type_to_string(operand.type, true);
  1378. defer (gb_string_free(pts));
  1379. defer (gb_string_free(ots));
  1380. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1381. }
  1382. return t_invalid;
  1383. }
  1384. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1385. return poly_type;
  1386. }
  1387. if (show_error) {
  1388. gbString pts = type_to_string(poly_type);
  1389. gbString ots = type_to_string(operand.type, true);
  1390. defer (gb_string_free(pts));
  1391. defer (gb_string_free(ots));
  1392. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1393. }
  1394. return t_invalid;
  1395. }
  1396. gb_internal bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1397. if (expr == nullptr) {
  1398. return false;
  1399. }
  1400. expr = unparen_expr(expr);
  1401. if (expr->kind == Ast_SelectorExpr) {
  1402. Ast *lhs = expr->SelectorExpr.expr;
  1403. return is_expr_from_a_parameter(ctx, lhs);
  1404. } else if (expr->kind == Ast_Ident) {
  1405. Operand x= {};
  1406. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, true);
  1407. if (e->flags & EntityFlag_Param) {
  1408. return true;
  1409. }
  1410. }
  1411. return false;
  1412. }
  1413. gb_internal ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1414. ParameterValue param_value = {};
  1415. param_value.original_ast_expr = expr;
  1416. if (expr == nullptr) {
  1417. return param_value;
  1418. }
  1419. Operand o = {};
  1420. if (allow_caller_location &&
  1421. expr->kind == Ast_BasicDirective &&
  1422. expr->BasicDirective.name.string == "caller_location") {
  1423. init_core_source_code_location(ctx->checker);
  1424. param_value.kind = ParameterValue_Location;
  1425. o.type = t_source_code_location;
  1426. o.mode = Addressing_Value;
  1427. o.expr = expr;
  1428. if (in_type) {
  1429. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1430. }
  1431. } else {
  1432. if (in_type) {
  1433. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1434. } else {
  1435. check_expr(ctx, &o, expr);
  1436. }
  1437. if (in_type) {
  1438. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1439. }
  1440. if (is_operand_nil(o)) {
  1441. param_value.kind = ParameterValue_Nil;
  1442. } else if (o.mode != Addressing_Constant) {
  1443. if (expr->kind == Ast_ProcLit) {
  1444. param_value.kind = ParameterValue_Constant;
  1445. param_value.value = exact_value_procedure(expr);
  1446. } else {
  1447. Entity *e = entity_from_expr(o.expr);
  1448. if (e != nullptr) {
  1449. if (e->kind == Entity_Procedure) {
  1450. param_value.kind = ParameterValue_Constant;
  1451. param_value.value = exact_value_procedure(e->identifier);
  1452. add_entity_use(ctx, e->identifier, e);
  1453. } else {
  1454. if (e->flags & EntityFlag_Param) {
  1455. error(expr, "Default parameter cannot be another parameter");
  1456. } else {
  1457. if (is_expr_from_a_parameter(ctx, expr)) {
  1458. error(expr, "Default parameter cannot be another parameter");
  1459. } else {
  1460. param_value.kind = ParameterValue_Value;
  1461. param_value.ast_value = expr;
  1462. add_entity_use(ctx, e->identifier, e);
  1463. }
  1464. }
  1465. }
  1466. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1467. param_value.kind = ParameterValue_Value;
  1468. param_value.ast_value = expr;
  1469. } else if (o.value.kind != ExactValue_Invalid) {
  1470. param_value.kind = ParameterValue_Constant;
  1471. param_value.value = o.value;
  1472. } else {
  1473. gbString s = expr_to_string(o.expr);
  1474. error(expr, "Default parameter must be a constant, got %s", s);
  1475. gb_string_free(s);
  1476. }
  1477. }
  1478. } else {
  1479. if (o.value.kind != ExactValue_Invalid) {
  1480. param_value.kind = ParameterValue_Constant;
  1481. param_value.value = o.value;
  1482. } else {
  1483. gbString s = expr_to_string(o.expr);
  1484. error(o.expr, "Invalid constant parameter, got '%s'", s);
  1485. // error(o.expr, "Invalid constant parameter, got '%s' %d %d", s, o.mode, o.value.kind);
  1486. gb_string_free(s);
  1487. }
  1488. }
  1489. }
  1490. if (out_type_) {
  1491. if (in_type != nullptr) {
  1492. *out_type_ = in_type;
  1493. } else {
  1494. *out_type_ = default_type(o.type);
  1495. }
  1496. }
  1497. return param_value;
  1498. }
  1499. gb_internal Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> const *operands) {
  1500. if (_params == nullptr) {
  1501. return nullptr;
  1502. }
  1503. bool success = true;
  1504. ast_node(field_list, FieldList, _params);
  1505. Slice<Ast *> params = field_list->list;
  1506. if (params.count == 0) {
  1507. if (success_) *success_ = success;
  1508. return nullptr;
  1509. }
  1510. isize variable_count = 0;
  1511. for_array(i, params) {
  1512. Ast *field = params[i];
  1513. if (ast_node_expect(field, Ast_Field)) {
  1514. ast_node(f, Field, field);
  1515. variable_count += gb_max(f->names.count, 1);
  1516. }
  1517. }
  1518. isize min_variable_count = variable_count;
  1519. for (isize i = params.count-1; i >= 0; i--) {
  1520. Ast *field = params[i];
  1521. if (field->kind == Ast_Field) {
  1522. ast_node(f, Field, field);
  1523. if (f->default_value == nullptr) {
  1524. break;
  1525. }
  1526. min_variable_count--;
  1527. }
  1528. }
  1529. bool is_variadic = false;
  1530. isize variadic_index = -1;
  1531. bool is_c_vararg = false;
  1532. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1533. i32 field_group_index = -1;
  1534. for_array(i, params) {
  1535. Ast *param = params[i];
  1536. if (param->kind != Ast_Field) {
  1537. continue;
  1538. }
  1539. field_group_index += 1;
  1540. ast_node(p, Field, param);
  1541. Ast *type_expr = unparen_expr(p->type);
  1542. Type *type = nullptr;
  1543. Ast *default_value = unparen_expr(p->default_value);
  1544. ParameterValue param_value = {};
  1545. bool is_type_param = false;
  1546. bool is_type_polymorphic_type = false;
  1547. bool detemine_type_from_operand = false;
  1548. Type *specialization = nullptr;
  1549. bool is_using = (p->flags&FieldFlag_using) != 0;
  1550. if ((check_vet_flags(param) & VetFlag_UsingParam) && is_using) {
  1551. ERROR_BLOCK();
  1552. error(param, "'using' on a procedure parameter is not allowed when '-vet' or '-vet-using-param' is applied");
  1553. error_line("\t'using' is considered bad practice to use as a statement/procedure parameter outside of immediate refactoring\n");
  1554. }
  1555. if (type_expr == nullptr) {
  1556. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1557. } else {
  1558. if (type_expr->kind == Ast_Ellipsis) {
  1559. type_expr = type_expr->Ellipsis.expr;
  1560. is_variadic = true;
  1561. variadic_index = variables.count;
  1562. if (p->names.count != 1) {
  1563. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1564. success = false;
  1565. }
  1566. }
  1567. if (type_expr->kind == Ast_TypeidType) {
  1568. ast_node(tt, TypeidType, type_expr);
  1569. if (tt->specialization) {
  1570. specialization = check_type(ctx, tt->specialization);
  1571. if (specialization == t_invalid){
  1572. specialization = nullptr;
  1573. }
  1574. if (operands != nullptr) {
  1575. detemine_type_from_operand = true;
  1576. type = t_invalid;
  1577. } else {
  1578. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1579. }
  1580. } else {
  1581. type = t_typeid;
  1582. }
  1583. } else {
  1584. bool prev = ctx->allow_polymorphic_types;
  1585. if (operands != nullptr) {
  1586. ctx->allow_polymorphic_types = true;
  1587. }
  1588. type = check_type(ctx, type_expr);
  1589. ctx->allow_polymorphic_types = prev;
  1590. if (is_type_polymorphic(type)) {
  1591. is_type_polymorphic_type = true;
  1592. }
  1593. }
  1594. if (default_value != nullptr) {
  1595. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1596. error(type_expr, "A type parameter may not have a default value");
  1597. } else {
  1598. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1599. }
  1600. }
  1601. }
  1602. if (type == nullptr) {
  1603. error(param, "Invalid parameter type");
  1604. type = t_invalid;
  1605. }
  1606. if (is_type_untyped(type)) {
  1607. if (is_type_untyped_uninit(type)) {
  1608. error(param, "Cannot determine parameter type from ---");
  1609. } else {
  1610. error(param, "Cannot determine parameter type from a nil");
  1611. }
  1612. type = t_invalid;
  1613. }
  1614. if (is_type_empty_union(type)) {
  1615. gbString str = type_to_string(type);
  1616. error(param, "Invalid use of an empty union '%s'", str);
  1617. gb_string_free(str);
  1618. type = t_invalid;
  1619. }
  1620. if (is_type_polymorphic(type)) {
  1621. switch (param_value.kind) {
  1622. case ParameterValue_Invalid:
  1623. case ParameterValue_Constant:
  1624. case ParameterValue_Nil:
  1625. break;
  1626. case ParameterValue_Location:
  1627. case ParameterValue_Value:
  1628. gbString str = type_to_string(type);
  1629. error(params[i], "A default value for a parameter must not be a polymorphic constant type, got %s", str);
  1630. gb_string_free(str);
  1631. break;
  1632. }
  1633. }
  1634. if (p->flags&FieldFlag_c_vararg) {
  1635. if (p->type == nullptr ||
  1636. p->type->kind != Ast_Ellipsis) {
  1637. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1638. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1639. } else {
  1640. is_c_vararg = true;
  1641. }
  1642. }
  1643. for_array(j, p->names) {
  1644. Ast *name = p->names[j];
  1645. bool is_poly_name = false;
  1646. switch (name->kind) {
  1647. case Ast_Ident:
  1648. break;
  1649. case Ast_PolyType:
  1650. GB_ASSERT(name->PolyType.specialization == nullptr);
  1651. is_poly_name = true;
  1652. name = name->PolyType.type;
  1653. break;
  1654. }
  1655. if (!ast_node_expect(name, Ast_Ident)) {
  1656. continue;
  1657. }
  1658. if (is_poly_name) {
  1659. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1660. is_type_param = true;
  1661. } else {
  1662. if (param_value.kind != ParameterValue_Invalid) {
  1663. error(default_value, "Constant parameters cannot have a default value");
  1664. param_value.kind = ParameterValue_Invalid;
  1665. }
  1666. }
  1667. }
  1668. Entity *param = nullptr;
  1669. if (is_type_param) {
  1670. if (operands != nullptr) {
  1671. Operand o = (*operands)[variables.count];
  1672. if (o.mode == Addressing_Type) {
  1673. type = o.type;
  1674. } else {
  1675. if (!ctx->no_polymorphic_errors) {
  1676. error(o.expr, "Expected a type to assign to the type parameter");
  1677. }
  1678. success = false;
  1679. type = t_invalid;
  1680. }
  1681. if (is_type_polymorphic(type)) {
  1682. gbString str = type_to_string(type);
  1683. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1684. gb_string_free(str);
  1685. success = false;
  1686. type = t_invalid;
  1687. }
  1688. if (is_type_untyped(default_type(type))) {
  1689. gbString str = type_to_string(type);
  1690. error(o.expr, "Cannot determine type from the parameter, got '%s'", str);
  1691. gb_string_free(str);
  1692. success = false;
  1693. type = t_invalid;
  1694. }
  1695. bool modify_type = !ctx->no_polymorphic_errors;
  1696. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1697. if (!ctx->no_polymorphic_errors) {
  1698. gbString t = type_to_string(type);
  1699. gbString s = type_to_string(specialization);
  1700. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1701. gb_string_free(s);
  1702. gb_string_free(t);
  1703. }
  1704. success = false;
  1705. type = t_invalid;
  1706. }
  1707. }
  1708. if (p->flags&FieldFlag_const) {
  1709. error(name, "'#const' can only be applied to variable fields");
  1710. p->flags &= ~FieldFlag_const;
  1711. }
  1712. if (p->flags&FieldFlag_any_int) {
  1713. error(name, "'#any_int' can only be applied to variable fields");
  1714. p->flags &= ~FieldFlag_any_int;
  1715. }
  1716. if (p->flags&FieldFlag_no_broadcast) {
  1717. error(name, "'#no_broadcast' can only be applied to variable fields");
  1718. p->flags &= ~FieldFlag_no_broadcast;
  1719. }
  1720. if (p->flags&FieldFlag_by_ptr) {
  1721. error(name, "'#by_ptr' can only be applied to variable fields");
  1722. p->flags &= ~FieldFlag_by_ptr;
  1723. }
  1724. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1725. param->TypeName.is_type_alias = true;
  1726. } else {
  1727. ExactValue poly_const = {};
  1728. if (operands != nullptr && variables.count < operands->count) {
  1729. Operand op = (*operands)[variables.count];
  1730. if (op.expr == nullptr) {
  1731. // NOTE(bill): 2019-03-30
  1732. // This is just to add the error message to determine_type_from_polymorphic which
  1733. // depends on valid position information
  1734. op.expr = _params;
  1735. op.mode = Addressing_Invalid;
  1736. op.type = t_invalid;
  1737. }
  1738. if (is_type_polymorphic_type) {
  1739. type = determine_type_from_polymorphic(ctx, type, op);
  1740. if (type == t_invalid) {
  1741. success = false;
  1742. } else if (!ctx->no_polymorphic_errors) {
  1743. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1744. is_type_polymorphic_type = false;
  1745. Entity *proc_entity = entity_from_expr(op.expr);
  1746. if ((proc_entity != nullptr) && (op.value.kind == ExactValue_Procedure)) {
  1747. if (is_type_polymorphic(proc_entity->type, false)) {
  1748. error(op.expr, "Cannot determine complete type of partial polymorphic procedure");
  1749. }
  1750. }
  1751. }
  1752. }
  1753. if (is_poly_name) {
  1754. bool valid = false;
  1755. if (is_type_proc(op.type)) {
  1756. Ast *expr = unparen_expr(op.expr);
  1757. Entity *proc_entity = entity_from_expr(expr);
  1758. if (proc_entity) {
  1759. poly_const = exact_value_procedure(proc_entity->identifier.load() ? proc_entity->identifier.load() : op.expr);
  1760. valid = true;
  1761. } else if (expr->kind == Ast_ProcLit) {
  1762. poly_const = exact_value_procedure(expr);
  1763. valid = true;
  1764. }
  1765. }
  1766. if (!valid) {
  1767. if (op.mode == Addressing_Constant) {
  1768. poly_const = op.value;
  1769. } else {
  1770. error(op.expr, "Expected a constant value for this polymorphic name parameter, got %s", expr_to_string(op.expr));
  1771. success = false;
  1772. }
  1773. }
  1774. }
  1775. bool allow_array_programming = true;
  1776. if (p->flags&FieldFlag_no_broadcast) {
  1777. allow_array_programming = false;
  1778. }
  1779. if (type != t_invalid && !check_is_assignable_to(ctx, &op, type, allow_array_programming)) {
  1780. bool ok = true;
  1781. if (p->flags&FieldFlag_any_int) {
  1782. if ((!is_type_integer(op.type) && !is_type_enum(op.type)) || (!is_type_integer(type) && !is_type_enum(type))) {
  1783. ok = false;
  1784. } else if (!check_is_castable_to(ctx, &op, type)) {
  1785. ok = false;
  1786. }
  1787. }
  1788. if (!ok) {
  1789. success = false;
  1790. #if 0
  1791. gbString got = type_to_string(op.type);
  1792. gbString expected = type_to_string(type);
  1793. error(op.expr, "Cannot assigned type to parameter, got type '%s', expected '%s'", got, expected);
  1794. gb_string_free(expected);
  1795. gb_string_free(got);
  1796. #endif
  1797. }
  1798. }
  1799. if (is_type_untyped(default_type(type))) {
  1800. gbString str = type_to_string(type);
  1801. error(op.expr, "Cannot determine type from the parameter, got '%s'", str);
  1802. gb_string_free(str);
  1803. success = false;
  1804. type = t_invalid;
  1805. }
  1806. }
  1807. if (p->flags&FieldFlag_no_alias) {
  1808. if (!is_type_pointer(type) && !is_type_multi_pointer(type)) {
  1809. error(name, "'#no_alias' can only be applied pointer or multi-pointer typed parameters");
  1810. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1811. }
  1812. }
  1813. if (p->flags&FieldFlag_by_ptr) {
  1814. if (is_type_internally_pointer_like(type)) {
  1815. error(name, "'#by_ptr' can only be applied to non-pointer-like parameters");
  1816. p->flags &= ~FieldFlag_by_ptr; // Remove the flag
  1817. }
  1818. }
  1819. if (is_poly_name) {
  1820. if (p->flags&FieldFlag_no_alias) {
  1821. error(name, "'#no_alias' can only be applied to non constant values");
  1822. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1823. }
  1824. if (p->flags&FieldFlag_any_int) {
  1825. error(name, "'#any_int' can only be applied to variable fields");
  1826. p->flags &= ~FieldFlag_any_int;
  1827. }
  1828. if (p->flags&FieldFlag_const) {
  1829. error(name, "'#const' can only be applied to variable fields");
  1830. p->flags &= ~FieldFlag_const;
  1831. }
  1832. if (p->flags&FieldFlag_by_ptr) {
  1833. error(name, "'#by_ptr' can only be applied to variable fields");
  1834. p->flags &= ~FieldFlag_by_ptr;
  1835. }
  1836. if (!is_type_polymorphic(type) && check_constant_parameter_value(type, params[i])) {
  1837. // failed
  1838. }
  1839. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1840. param->Constant.field_group_index = field_group_index;
  1841. } else {
  1842. param = alloc_entity_param(scope, name->Ident.token, type, is_using, true);
  1843. param->Variable.param_value = param_value;
  1844. param->Variable.field_group_index = field_group_index;
  1845. param->Variable.type_expr = type_expr;
  1846. }
  1847. }
  1848. if (p->flags&FieldFlag_no_alias) {
  1849. param->flags |= EntityFlag_NoAlias;
  1850. }
  1851. if (p->flags&FieldFlag_no_broadcast) {
  1852. param->flags |= EntityFlag_NoBroadcast;
  1853. }
  1854. if (p->flags&FieldFlag_any_int) {
  1855. if (!is_type_integer(param->type) && !is_type_enum(param->type)) {
  1856. gbString str = type_to_string(param->type);
  1857. error(name, "A parameter with '#any_int' must be an integer, got %s", str);
  1858. gb_string_free(str);
  1859. }
  1860. param->flags |= EntityFlag_AnyInt;
  1861. }
  1862. if (p->flags&FieldFlag_const) {
  1863. param->flags |= EntityFlag_ConstInput;
  1864. }
  1865. if (p->flags&FieldFlag_by_ptr) {
  1866. param->flags |= EntityFlag_ByPtr;
  1867. }
  1868. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1869. add_entity(ctx, scope, name, param);
  1870. if (is_using) {
  1871. add_entity_use(ctx, name, param);
  1872. }
  1873. array_add(&variables, param);
  1874. }
  1875. }
  1876. if (is_variadic) {
  1877. GB_ASSERT(variadic_index >= 0);
  1878. }
  1879. if (is_variadic) {
  1880. GB_ASSERT(params.count > 0);
  1881. // NOTE(bill): Change last variadic parameter to be a slice
  1882. // Custom Calling convention for variadic parameters
  1883. Entity *end = variables[variadic_index];
  1884. end->type = alloc_type_slice(end->type);
  1885. end->flags |= EntityFlag_Ellipsis;
  1886. if (is_c_vararg) {
  1887. end->flags |= EntityFlag_CVarArg;
  1888. }
  1889. }
  1890. isize specialization_count = 0;
  1891. if (scope != nullptr) {
  1892. for (auto const &entry : scope->elements) {
  1893. Entity *e = entry.value;
  1894. if (e->kind == Entity_TypeName) {
  1895. Type *t = e->type;
  1896. if (t->kind == Type_Generic &&
  1897. t->Generic.specialized != nullptr) {
  1898. specialization_count += 1;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. Type *tuple = alloc_type_tuple();
  1904. tuple->Tuple.variables = slice_from_array(variables);
  1905. if (success_) *success_ = success;
  1906. if (specialization_count_) *specialization_count_ = specialization_count;
  1907. if (is_variadic_) *is_variadic_ = is_variadic;
  1908. if (variadic_index_) *variadic_index_ = variadic_index;
  1909. return tuple;
  1910. }
  1911. gb_internal Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1912. if (_results == nullptr) {
  1913. return nullptr;
  1914. }
  1915. ast_node(field_list, FieldList, _results);
  1916. Slice<Ast *> results = field_list->list;
  1917. if (results.count == 0) {
  1918. return nullptr;
  1919. }
  1920. Type *tuple = alloc_type_tuple();
  1921. isize variable_count = 0;
  1922. for_array(i, results) {
  1923. Ast *field = results[i];
  1924. if (ast_node_expect(field, Ast_Field)) {
  1925. ast_node(f, Field, field);
  1926. variable_count += gb_max(f->names.count, 1);
  1927. }
  1928. }
  1929. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1930. i32 field_group_index = -1;
  1931. for_array(i, results) {
  1932. field_group_index += 1;
  1933. ast_node(field, Field, results[i]);
  1934. Ast *default_value = unparen_expr(field->default_value);
  1935. ParameterValue param_value = {};
  1936. Type *type = nullptr;
  1937. if (field->type == nullptr) {
  1938. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1939. } else {
  1940. type = check_type(ctx, field->type);
  1941. if (default_value != nullptr) {
  1942. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1943. }
  1944. }
  1945. if (type == nullptr) {
  1946. error(results[i], "Invalid parameter type");
  1947. type = t_invalid;
  1948. }
  1949. if (is_type_untyped(type)) {
  1950. error(results[i], "Cannot determine parameter type from a nil");
  1951. type = t_invalid;
  1952. }
  1953. if (field->names.count == 0) {
  1954. Token token = ast_token(field->type);
  1955. token.string = str_lit("");
  1956. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1957. param->Variable.param_value = param_value;
  1958. param->Variable.field_group_index = -1;
  1959. array_add(&variables, param);
  1960. } else {
  1961. for_array(j, field->names) {
  1962. Token token = ast_token(results[i]);
  1963. if (field->type != nullptr) {
  1964. token = ast_token(field->type);
  1965. }
  1966. token.string = str_lit("");
  1967. Ast *name = field->names[j];
  1968. if (name->kind != Ast_Ident) {
  1969. error(name, "Expected an identifer for as the field name");
  1970. } else {
  1971. token = name->Ident.token;
  1972. }
  1973. if (is_blank_ident(token)) {
  1974. error(name, "Result value cannot be a blank identifer `_`");
  1975. }
  1976. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1977. param->flags |= EntityFlag_Result;
  1978. param->Variable.param_value = param_value;
  1979. param->Variable.field_group_index = field_group_index;
  1980. array_add(&variables, param);
  1981. add_entity(ctx, scope, name, param);
  1982. // NOTE(bill): Removes `declared but not used` when using -vet
  1983. add_entity_use(ctx, name, param);
  1984. }
  1985. }
  1986. }
  1987. for_array(i, variables) {
  1988. String x = variables[i]->token.string;
  1989. if (x.len == 0 || is_blank_ident(x)) {
  1990. continue;
  1991. }
  1992. for (isize j = i+1; j < variables.count; j++) {
  1993. String y = variables[j]->token.string;
  1994. if (y.len == 0 || is_blank_ident(y)) {
  1995. continue;
  1996. }
  1997. if (x == y) {
  1998. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1999. }
  2000. }
  2001. }
  2002. tuple->Tuple.variables = slice_from_array(variables);
  2003. return tuple;
  2004. }
  2005. // NOTE(bill): 'operands' is for generating non generic procedure type
  2006. gb_internal bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> const *operands) {
  2007. ast_node(pt, ProcType, proc_type_node);
  2008. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  2009. ctx->polymorphic_scope = ctx->scope;
  2010. }
  2011. CheckerContext c_ = *ctx;
  2012. CheckerContext *c = &c_;
  2013. c->curr_proc_sig = type;
  2014. c->in_proc_sig = true;
  2015. ProcCallingConvention cc = pt->calling_convention;
  2016. if (cc == ProcCC_ForeignBlockDefault) {
  2017. cc = ProcCC_CDecl;
  2018. if (c->foreign_context.default_cc > 0) {
  2019. cc = c->foreign_context.default_cc;
  2020. }
  2021. }
  2022. GB_ASSERT(cc > 0);
  2023. if (cc == ProcCC_Odin) {
  2024. c->scope->flags |= ScopeFlag_ContextDefined;
  2025. } else {
  2026. c->scope->flags &= ~ScopeFlag_ContextDefined;
  2027. }
  2028. TargetArchKind arch = build_context.metrics.arch;
  2029. switch (cc) {
  2030. case ProcCC_StdCall:
  2031. case ProcCC_FastCall:
  2032. if (arch != TargetArch_i386 && arch != TargetArch_amd64) {
  2033. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected either i386 or amd64, got %.*s",
  2034. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  2035. }
  2036. break;
  2037. case ProcCC_Win64:
  2038. case ProcCC_SysV:
  2039. if (arch != TargetArch_amd64) {
  2040. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected amd64, got %.*s",
  2041. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  2042. }
  2043. break;
  2044. }
  2045. bool variadic = false;
  2046. isize variadic_index = -1;
  2047. bool success = true;
  2048. isize specialization_count = 0;
  2049. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  2050. Type *results = check_get_results(c, c->scope, pt->results);
  2051. isize param_count = 0;
  2052. isize result_count = 0;
  2053. if (params) param_count = params ->Tuple.variables.count;
  2054. if (results) result_count = results->Tuple.variables.count;
  2055. if (result_count > 0) {
  2056. Entity *first = results->Tuple.variables[0];
  2057. type->Proc.has_named_results = first->token.string != "";
  2058. }
  2059. bool optional_ok = (pt->tags & ProcTag_optional_ok) != 0;
  2060. if (optional_ok) {
  2061. if (result_count != 2) {
  2062. error(proc_type_node, "A procedure type with the #optional_ok tag requires 2 return values, got %td", result_count);
  2063. } else {
  2064. Entity *second = results->Tuple.variables[1];
  2065. if (is_type_polymorphic(second->type)) {
  2066. // ignore
  2067. } else if (is_type_boolean(second->type)) {
  2068. // GOOD
  2069. } else {
  2070. error(second->token, "Second return value of an #optional_ok procedure must be a boolean, got %s", type_to_string(second->type));
  2071. }
  2072. }
  2073. }
  2074. if (pt->tags & ProcTag_optional_allocator_error) {
  2075. if (optional_ok) {
  2076. error(proc_type_node, "A procedure type cannot have both an #optional_ok tag and #optional_allocator_error");
  2077. }
  2078. optional_ok = true;
  2079. if (result_count != 2) {
  2080. error(proc_type_node, "A procedure type with the #optional_allocator_error tag requires 2 return values, got %td", result_count);
  2081. } else {
  2082. init_mem_allocator(c->checker);
  2083. Type *type = results->Tuple.variables[1]->type;
  2084. if (!are_types_identical(type, t_allocator_error)) {
  2085. gbString t = type_to_string(type);
  2086. error(proc_type_node, "A procedure type with the #optional_allocator_error expects a `runtime.Allocator_Error`, got '%s'", t);
  2087. gb_string_free(t);
  2088. }
  2089. }
  2090. }
  2091. type->Proc.node = proc_type_node;
  2092. type->Proc.scope = c->scope;
  2093. type->Proc.params = params;
  2094. type->Proc.param_count = cast(i32)param_count;
  2095. type->Proc.results = results;
  2096. type->Proc.result_count = cast(i32)result_count;
  2097. type->Proc.variadic = variadic;
  2098. type->Proc.variadic_index = cast(i32)variadic_index;
  2099. type->Proc.calling_convention = cc;
  2100. type->Proc.is_polymorphic = pt->generic;
  2101. type->Proc.specialization_count = specialization_count;
  2102. type->Proc.diverging = pt->diverging;
  2103. type->Proc.optional_ok = optional_ok;
  2104. bool is_polymorphic = false;
  2105. for (isize i = 0; i < param_count; i++) {
  2106. Entity *e = params->Tuple.variables[i];
  2107. if (e->kind != Entity_Variable) {
  2108. is_polymorphic = true;
  2109. } else if (is_type_polymorphic(e->type)) {
  2110. is_polymorphic = true;
  2111. }
  2112. if (e->flags&EntityFlag_CVarArg) {
  2113. if (i != param_count - 1) {
  2114. error(e->token, "#c_vararg can only be applied to the last parameter");
  2115. continue;
  2116. }
  2117. switch (cc) {
  2118. default:
  2119. type->Proc.c_vararg = true;
  2120. break;
  2121. case ProcCC_Odin:
  2122. case ProcCC_Contextless:
  2123. error(e->token, "Calling convention does not support #c_vararg");
  2124. break;
  2125. }
  2126. }
  2127. }
  2128. for (isize i = 0; i < result_count; i++) {
  2129. Entity *e = results->Tuple.variables[i];
  2130. if (e->kind != Entity_Variable) {
  2131. is_polymorphic = true;
  2132. break;
  2133. } else if (is_type_polymorphic(e->type)) {
  2134. is_polymorphic = true;
  2135. break;
  2136. }
  2137. }
  2138. type->Proc.is_polymorphic = is_polymorphic;
  2139. return success;
  2140. }
  2141. gb_internal i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  2142. if (e == nullptr) {
  2143. return 0;
  2144. }
  2145. if (e->kind == Ast_UnaryExpr &&
  2146. e->UnaryExpr.op.kind == Token_Question) {
  2147. return -1;
  2148. }
  2149. check_expr_or_type(ctx, o, e);
  2150. if (o->mode == Addressing_Type) {
  2151. Type *ot = base_type(o->type);
  2152. if (ot->kind == Type_Generic) {
  2153. if (ctx->allow_polymorphic_types) {
  2154. if (ot->Generic.specialized) {
  2155. ot->Generic.specialized = nullptr;
  2156. error(o->expr, "Polymorphic array length cannot have a specialization");
  2157. }
  2158. return 0;
  2159. }
  2160. }
  2161. if (is_type_enum(ot)) {
  2162. return -1;
  2163. }
  2164. }
  2165. if (o->mode != Addressing_Constant) {
  2166. if (o->mode != Addressing_Invalid) {
  2167. Entity *entity = entity_of_node(o->expr);
  2168. bool is_poly_type = false;
  2169. if (entity != nullptr) {
  2170. is_poly_type = \
  2171. entity->kind == Entity_TypeName &&
  2172. entity->type == t_typeid &&
  2173. entity->flags&EntityFlag_PolyConst;
  2174. }
  2175. // NOTE(bill, 2021-03-27): Improve error message for parametric polymorphic parameters which want to generate
  2176. // and enumerated array but cannot determine what it ought to be yet
  2177. if (ctx->allow_polymorphic_types && is_poly_type) {
  2178. return 0;
  2179. }
  2180. ERROR_BLOCK();
  2181. gbString s = expr_to_string(o->expr);
  2182. error(e, "Array count must be a constant integer, got %s", s);
  2183. gb_string_free(s);
  2184. if (is_poly_type) {
  2185. error_line("\tSuggestion: 'where' clause may be required to restrict the enumerated array index type to an enum\n");
  2186. error_line("\t 'where intrinsics.type_is_enum(%.*s)'\n", LIT(entity->token.string));
  2187. }
  2188. o->mode = Addressing_Invalid;
  2189. o->type = t_invalid;
  2190. }
  2191. return 0;
  2192. }
  2193. Type *type = core_type(o->type);
  2194. if (is_type_untyped(type) || is_type_integer(type)) {
  2195. if (o->value.kind == ExactValue_Integer) {
  2196. BigInt count = o->value.value_integer;
  2197. if (big_int_is_neg(&o->value.value_integer)) {
  2198. gbAllocator a = heap_allocator();
  2199. String str = big_int_to_string(a, &count);
  2200. error(e, "Invalid negative array count, %.*s", LIT(str));
  2201. gb_free(a, str.text);
  2202. return 0;
  2203. }
  2204. switch (count.used) {
  2205. case 0: return 0;
  2206. case 1: return big_int_to_u64(&count);
  2207. }
  2208. gbAllocator a = heap_allocator();
  2209. String str = big_int_to_string(a, &count);
  2210. error(e, "Array count too large, %.*s", LIT(str));
  2211. gb_free(a, str.text);
  2212. return 0;
  2213. }
  2214. }
  2215. error(e, "Array count must be a constant integer");
  2216. return 0;
  2217. }
  2218. gb_internal Type *make_optional_ok_type(Type *value, bool typed) {
  2219. gbAllocator a = permanent_allocator();
  2220. Type *t = alloc_type_tuple();
  2221. slice_init(&t->Tuple.variables, a, 2);
  2222. t->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, value, false, 0);
  2223. t->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  2224. return t;
  2225. }
  2226. // IMPORTANT NOTE(bill): This must match the definition in dynamic_map_internal.odin
  2227. enum : i64 {
  2228. MAP_CELL_CACHE_LINE_LOG2 = 6,
  2229. MAP_CELL_CACHE_LINE_SIZE = 1 << MAP_CELL_CACHE_LINE_LOG2,
  2230. };
  2231. GB_STATIC_ASSERT(MAP_CELL_CACHE_LINE_SIZE >= 64);
  2232. gb_internal void map_cell_size_and_len(Type *type, i64 *size_, i64 *len_) {
  2233. i64 elem_sz = type_size_of(type);
  2234. i64 len = 1;
  2235. if (0 < elem_sz && elem_sz < MAP_CELL_CACHE_LINE_SIZE) {
  2236. len = MAP_CELL_CACHE_LINE_SIZE / elem_sz;
  2237. }
  2238. i64 size = align_formula(elem_sz * len, MAP_CELL_CACHE_LINE_SIZE);
  2239. if (size_) *size_ = size;
  2240. if (len_) *len_ = len;
  2241. }
  2242. gb_internal Type *get_map_cell_type(Type *type) {
  2243. i64 size, len;
  2244. i64 elem_size = type_size_of(type);
  2245. map_cell_size_and_len(type, &size, &len);
  2246. if (size == len*elem_size) {
  2247. return type;
  2248. }
  2249. i64 padding = size - len*elem_size;
  2250. GB_ASSERT(padding > 0);
  2251. // Padding exists
  2252. Type *s = alloc_type_struct();
  2253. Scope *scope = create_scope(nullptr, nullptr);
  2254. s->Struct.fields = slice_make<Entity *>(permanent_allocator(), 2);
  2255. s->Struct.fields[0] = alloc_entity_field(scope, make_token_ident("v"), alloc_type_array(type, len), false, 0, EntityState_Resolved);
  2256. s->Struct.fields[1] = alloc_entity_field(scope, make_token_ident("_"), alloc_type_array(t_u8, padding), false, 1, EntityState_Resolved);
  2257. s->Struct.scope = scope;
  2258. wait_signal_set(&s->Struct.fields_wait_signal);
  2259. gb_unused(type_size_of(s));
  2260. return s;
  2261. }
  2262. gb_internal void init_map_internal_debug_types(Type *type) {
  2263. GB_ASSERT(type->kind == Type_Map);
  2264. GB_ASSERT(t_allocator != nullptr);
  2265. if (type->Map.debug_metadata_type != nullptr) return;
  2266. Type *key = type->Map.key;
  2267. Type *value = type->Map.value;
  2268. GB_ASSERT(key != nullptr);
  2269. GB_ASSERT(value != nullptr);
  2270. Type *key_cell = get_map_cell_type(key);
  2271. Type *value_cell = get_map_cell_type(value);
  2272. Type *metadata_type = alloc_type_struct();
  2273. Scope *metadata_scope = create_scope(nullptr, nullptr);
  2274. metadata_type->Struct.fields = slice_make<Entity *>(permanent_allocator(), 5);
  2275. metadata_type->Struct.fields[0] = alloc_entity_field(metadata_scope, make_token_ident("key"), key, false, 0, EntityState_Resolved);
  2276. metadata_type->Struct.fields[1] = alloc_entity_field(metadata_scope, make_token_ident("value"), value, false, 1, EntityState_Resolved);
  2277. metadata_type->Struct.fields[2] = alloc_entity_field(metadata_scope, make_token_ident("hash"), t_uintptr, false, 2, EntityState_Resolved);
  2278. metadata_type->Struct.fields[3] = alloc_entity_field(metadata_scope, make_token_ident("key_cell"), key_cell, false, 3, EntityState_Resolved);
  2279. metadata_type->Struct.fields[4] = alloc_entity_field(metadata_scope, make_token_ident("value_cell"), value_cell, false, 4, EntityState_Resolved);
  2280. metadata_type->Struct.scope = metadata_scope;
  2281. metadata_type->Struct.node = nullptr;
  2282. wait_signal_set(&metadata_type->Struct.fields_wait_signal);
  2283. gb_unused(type_size_of(metadata_type));
  2284. // NOTE(bill): ^struct{key: Key, value: Value, hash: uintptr}
  2285. metadata_type = alloc_type_pointer(metadata_type);
  2286. Scope *scope = create_scope(nullptr, nullptr);
  2287. Type *debug_type = alloc_type_struct();
  2288. debug_type->Struct.fields = slice_make<Entity *>(permanent_allocator(), 3);
  2289. debug_type->Struct.fields[0] = alloc_entity_field(scope, make_token_ident("data"), metadata_type, false, 0, EntityState_Resolved);
  2290. debug_type->Struct.fields[1] = alloc_entity_field(scope, make_token_ident("len"), t_int, false, 1, EntityState_Resolved);
  2291. debug_type->Struct.fields[2] = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, 2, EntityState_Resolved);
  2292. debug_type->Struct.scope = scope;
  2293. debug_type->Struct.node = nullptr;
  2294. wait_signal_set(&debug_type->Struct.fields_wait_signal);
  2295. gb_unused(type_size_of(debug_type));
  2296. type->Map.debug_metadata_type = debug_type;
  2297. }
  2298. gb_internal void init_map_internal_types(Type *type) {
  2299. GB_ASSERT(type->kind == Type_Map);
  2300. GB_ASSERT(t_allocator != nullptr);
  2301. if (type->Map.lookup_result_type != nullptr) return;
  2302. Type *key = type->Map.key;
  2303. Type *value = type->Map.value;
  2304. GB_ASSERT(key != nullptr);
  2305. GB_ASSERT(value != nullptr);
  2306. type->Map.lookup_result_type = make_optional_ok_type(value);
  2307. }
  2308. gb_internal void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
  2309. key = core_type(key);
  2310. if (is_type_cstring(key)) {
  2311. add_package_dependency(ctx, "runtime", "default_hasher_cstring");
  2312. } else if (is_type_string(key)) {
  2313. add_package_dependency(ctx, "runtime", "default_hasher_string");
  2314. } else if (!is_type_polymorphic(key)) {
  2315. if (!is_type_comparable(key)) {
  2316. return;
  2317. }
  2318. if (is_type_simple_compare(key)) {
  2319. add_package_dependency(ctx, "runtime", "default_hasher");
  2320. return;
  2321. }
  2322. if (key->kind == Type_Struct) {
  2323. add_package_dependency(ctx, "runtime", "default_hasher");
  2324. for_array(i, key->Struct.fields) {
  2325. Entity *field = key->Struct.fields[i];
  2326. add_map_key_type_dependencies(ctx, field->type);
  2327. }
  2328. } else if (key->kind == Type_Union) {
  2329. add_package_dependency(ctx, "runtime", "default_hasher");
  2330. for_array(i, key->Union.variants) {
  2331. Type *v = key->Union.variants[i];
  2332. add_map_key_type_dependencies(ctx, v);
  2333. }
  2334. } else if (key->kind == Type_EnumeratedArray) {
  2335. add_package_dependency(ctx, "runtime", "default_hasher");
  2336. add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
  2337. } else if (key->kind == Type_Array) {
  2338. add_package_dependency(ctx, "runtime", "default_hasher");
  2339. add_map_key_type_dependencies(ctx, key->Array.elem);
  2340. }
  2341. }
  2342. }
  2343. gb_internal void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  2344. GB_ASSERT(type->kind == Type_Map);
  2345. ast_node(mt, MapType, node);
  2346. Type *key = check_type(ctx, mt->key);
  2347. Type *value = check_type(ctx, mt->value);
  2348. if (!is_type_valid_for_keys(key)) {
  2349. if (is_type_boolean(key)) {
  2350. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  2351. } else {
  2352. gbString str = type_to_string(key);
  2353. error(node, "Invalid type of a key for a map, got '%s'", str);
  2354. gb_string_free(str);
  2355. }
  2356. }
  2357. if (type_size_of(key) == 0) {
  2358. gbString str = type_to_string(key);
  2359. error(node, "Invalid type of a key for a map of size 0, got '%s'", str);
  2360. gb_string_free(str);
  2361. }
  2362. type->Map.key = key;
  2363. type->Map.value = value;
  2364. add_map_key_type_dependencies(ctx, key);
  2365. init_core_map_type(ctx->checker);
  2366. init_map_internal_types(type);
  2367. }
  2368. gb_internal void check_matrix_type(CheckerContext *ctx, Type **type, Ast *node) {
  2369. ast_node(mt, MatrixType, node);
  2370. Operand row = {};
  2371. Operand column = {};
  2372. i64 row_count = check_array_count(ctx, &row, mt->row_count);
  2373. i64 column_count = check_array_count(ctx, &column, mt->column_count);
  2374. Type *generic_row = nullptr;
  2375. Type *generic_column = nullptr;
  2376. if (row.mode == Addressing_Type && row.type->kind == Type_Generic) {
  2377. generic_row = row.type;
  2378. }
  2379. if (column.mode == Addressing_Type && column.type->kind == Type_Generic) {
  2380. generic_column = column.type;
  2381. }
  2382. if (generic_row == nullptr && row_count < MATRIX_ELEMENT_COUNT_MIN) {
  2383. gbString s = expr_to_string(row.expr);
  2384. error(row.expr, "Invalid matrix row count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2385. gb_string_free(s);
  2386. }
  2387. if (generic_column == nullptr && column_count < MATRIX_ELEMENT_COUNT_MIN) {
  2388. gbString s = expr_to_string(column.expr);
  2389. error(column.expr, "Invalid matrix column count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2390. gb_string_free(s);
  2391. }
  2392. if ((generic_row == nullptr && generic_column == nullptr) && row_count*column_count > MATRIX_ELEMENT_COUNT_MAX) {
  2393. i64 element_count = row_count*column_count;
  2394. error(column.expr, "Matrix types are limited to a maximum of %d elements, got %lld", MATRIX_ELEMENT_COUNT_MAX, cast(long long)element_count);
  2395. }
  2396. Type *elem = check_type_expr(ctx, mt->elem, nullptr);
  2397. if (!is_type_valid_for_matrix_elems(elem)) {
  2398. if (elem == t_typeid) {
  2399. Entity *e = entity_of_node(mt->elem);
  2400. if (e && e->kind == Entity_TypeName && e->TypeName.is_type_alias) {
  2401. // HACK TODO(bill): This is to allow polymorphic parameters for matrix elements
  2402. // proc($T: typeid) -> matrix[2, 2]T
  2403. //
  2404. // THIS IS NEEDS TO BE FIXED AND NOT USE THIS HACK
  2405. goto type_assign;
  2406. }
  2407. }
  2408. gbString s = type_to_string(elem);
  2409. error(column.expr, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
  2410. gb_string_free(s);
  2411. }
  2412. type_assign:;
  2413. *type = alloc_type_matrix(elem, row_count, column_count, generic_row, generic_column, mt->is_row_major);
  2414. return;
  2415. }
  2416. struct SoaTypeWorkerData {
  2417. CheckerContext ctx;
  2418. Type * type;
  2419. bool wait_to_finish;
  2420. };
  2421. gb_internal bool complete_soa_type(Checker *checker, Type *t, bool wait_to_finish) {
  2422. Type *original_type = t;
  2423. gb_unused(original_type);
  2424. t = base_type(t);
  2425. if (t == nullptr || !is_type_soa_struct(t)) {
  2426. return true;
  2427. }
  2428. MUTEX_GUARD(&t->Struct.soa_mutex);
  2429. if (t->Struct.fields_wait_signal.futex.load()) {
  2430. return true;
  2431. }
  2432. isize field_count = 0;
  2433. i32 extra_field_count = 0;
  2434. switch (t->Struct.soa_kind) {
  2435. case StructSoa_Fixed: extra_field_count = 0; break;
  2436. case StructSoa_Slice: extra_field_count = 1; break;
  2437. case StructSoa_Dynamic: extra_field_count = 3; break;
  2438. }
  2439. Scope *scope = t->Struct.scope;
  2440. i64 soa_count = t->Struct.soa_count;
  2441. Type *elem = t->Struct.soa_elem;
  2442. Type *old_struct = base_type(elem);
  2443. GB_ASSERT(old_struct->kind == Type_Struct);
  2444. if (wait_to_finish) {
  2445. wait_signal_until_available(&old_struct->Struct.fields_wait_signal);
  2446. } else {
  2447. GB_ASSERT(old_struct->Struct.fields_wait_signal.futex.load());
  2448. }
  2449. field_count = old_struct->Struct.fields.count;
  2450. t->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2451. t->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2452. auto const &add_entity = [](Scope *scope, Entity *entity) {
  2453. String name = entity->token.string;
  2454. if (!is_blank_ident(name)) {
  2455. Entity *ie = scope_insert(scope, entity);
  2456. if (ie != nullptr) {
  2457. redeclaration_error(name, entity, ie);
  2458. }
  2459. }
  2460. };
  2461. for_array(i, old_struct->Struct.fields) {
  2462. Entity *old_field = old_struct->Struct.fields[i];
  2463. if (old_field->kind == Entity_Variable) {
  2464. Type *field_type = nullptr;
  2465. if (t->Struct.soa_kind == StructSoa_Fixed) {
  2466. GB_ASSERT(soa_count >= 0);
  2467. field_type = alloc_type_array(old_field->type, soa_count);
  2468. } else {
  2469. field_type = alloc_type_multi_pointer(old_field->type);
  2470. }
  2471. Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index);
  2472. t->Struct.fields[i] = new_field;
  2473. add_entity(scope, new_field);
  2474. new_field->flags |= EntityFlag_Used;
  2475. if (t->Struct.soa_kind != StructSoa_Fixed) {
  2476. new_field->flags |= EntityFlag_SoaPtrField;
  2477. }
  2478. } else {
  2479. t->Struct.fields[i] = old_field;
  2480. }
  2481. t->Struct.tags[i] = old_struct->Struct.tags[i];
  2482. }
  2483. if (t->Struct.soa_kind != StructSoa_Fixed) {
  2484. Entity *len_field = alloc_entity_field(scope, make_token_ident("__$len"), t_int, false, cast(i32)field_count+0);
  2485. t->Struct.fields[field_count+0] = len_field;
  2486. add_entity(scope, len_field);
  2487. len_field->flags |= EntityFlag_Used;
  2488. if (t->Struct.soa_kind == StructSoa_Dynamic) {
  2489. Entity *cap_field = alloc_entity_field(scope, make_token_ident("__$cap"), t_int, false, cast(i32)field_count+1);
  2490. t->Struct.fields[field_count+1] = cap_field;
  2491. add_entity(scope, cap_field);
  2492. cap_field->flags |= EntityFlag_Used;
  2493. init_mem_allocator(checker);
  2494. Entity *allocator_field = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, cast(i32)field_count+2);
  2495. t->Struct.fields[field_count+2] = allocator_field;
  2496. add_entity(scope, allocator_field);
  2497. allocator_field->flags |= EntityFlag_Used;
  2498. }
  2499. }
  2500. // add_type_info_type(ctx, original_type);
  2501. wait_signal_set(&t->Struct.fields_wait_signal);
  2502. return true;
  2503. }
  2504. gb_internal WORKER_TASK_PROC(complete_soa_type_worker) {
  2505. SoaTypeWorkerData *wd = cast(SoaTypeWorkerData *)data;
  2506. complete_soa_type(wd->ctx.checker, wd->type, wd->wait_to_finish);
  2507. return 0;
  2508. }
  2509. gb_internal Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type, StructSoaKind soa_kind) {
  2510. Type *bt_elem = base_type(elem);
  2511. bool is_polymorphic = is_type_polymorphic(elem);
  2512. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2513. gbString str = type_to_string(elem);
  2514. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2515. gb_string_free(str);
  2516. return alloc_type_array(elem, count, generic_type);
  2517. }
  2518. Type * soa_struct = nullptr;
  2519. Scope *scope = nullptr;
  2520. bool is_complete = false;
  2521. isize field_count = 0;
  2522. i32 extra_field_count = 0;
  2523. switch (soa_kind) {
  2524. case StructSoa_Fixed: extra_field_count = 0; break;
  2525. case StructSoa_Slice: extra_field_count = 1; break;
  2526. case StructSoa_Dynamic: extra_field_count = 3; break;
  2527. }
  2528. soa_struct = alloc_type_struct();
  2529. soa_struct->Struct.soa_kind = soa_kind;
  2530. soa_struct->Struct.soa_elem = elem;
  2531. soa_struct->Struct.is_polymorphic = is_polymorphic;
  2532. soa_struct->Struct.node = array_typ_expr;
  2533. if (count > I32_MAX) {
  2534. count = I32_MAX;
  2535. error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  2536. }
  2537. soa_struct->Struct.soa_count = cast(i32)count;
  2538. scope = create_scope(ctx->info, ctx->scope);
  2539. soa_struct->Struct.scope = scope;
  2540. if (elem && elem->kind == Type_Named) {
  2541. add_declaration_dependency(ctx, elem->Named.type_name);
  2542. }
  2543. if (is_polymorphic) {
  2544. field_count = 0;
  2545. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2546. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2547. soa_struct->Struct.soa_count = 0;
  2548. is_complete = true;
  2549. } else if (is_type_array(elem)) {
  2550. Type *old_array = base_type(elem);
  2551. field_count = cast(isize)old_array->Array.count;
  2552. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2553. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2554. string_map_init(&scope->elements, 8);
  2555. String params_xyzw[4] = {
  2556. str_lit("x"),
  2557. str_lit("y"),
  2558. str_lit("z"),
  2559. str_lit("w")
  2560. };
  2561. for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
  2562. Type *field_type = nullptr;
  2563. if (soa_kind == StructSoa_Fixed) {
  2564. GB_ASSERT(count >= 0);
  2565. field_type = alloc_type_array(old_array->Array.elem, count);
  2566. } else {
  2567. field_type = alloc_type_multi_pointer(old_array->Array.elem);
  2568. }
  2569. Token token = {};
  2570. token.string = params_xyzw[i];
  2571. Entity *new_field = alloc_entity_field(scope, token, field_type, false, cast(i32)i);
  2572. soa_struct->Struct.fields[i] = new_field;
  2573. add_entity(ctx, scope, nullptr, new_field);
  2574. add_entity_use(ctx, nullptr, new_field);
  2575. if (soa_kind != StructSoa_Fixed) {
  2576. new_field->flags |= EntityFlag_SoaPtrField;
  2577. }
  2578. }
  2579. is_complete = true;
  2580. } else {
  2581. GB_ASSERT(is_type_struct(elem));
  2582. Type *old_struct = base_type(elem);
  2583. if (old_struct->Struct.fields_wait_signal.futex.load()) {
  2584. field_count = old_struct->Struct.fields.count;
  2585. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2586. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2587. for_array(i, old_struct->Struct.fields) {
  2588. Entity *old_field = old_struct->Struct.fields[i];
  2589. if (old_field->kind == Entity_Variable) {
  2590. Type *field_type = nullptr;
  2591. if (soa_kind == StructSoa_Fixed) {
  2592. GB_ASSERT(count >= 0);
  2593. field_type = alloc_type_array(old_field->type, count);
  2594. } else {
  2595. field_type = alloc_type_multi_pointer(old_field->type);
  2596. }
  2597. Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index);
  2598. soa_struct->Struct.fields[i] = new_field;
  2599. add_entity(ctx, scope, nullptr, new_field);
  2600. add_entity_use(ctx, nullptr, new_field);
  2601. if (soa_kind != StructSoa_Fixed) {
  2602. new_field->flags |= EntityFlag_SoaPtrField;
  2603. }
  2604. } else {
  2605. soa_struct->Struct.fields[i] = old_field;
  2606. }
  2607. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2608. }
  2609. is_complete = true;
  2610. }
  2611. }
  2612. if (is_complete && soa_kind != StructSoa_Fixed) {
  2613. Entity *len_field = alloc_entity_field(scope, make_token_ident("__$len"), t_int, false, cast(i32)field_count+0);
  2614. soa_struct->Struct.fields[field_count+0] = len_field;
  2615. add_entity(ctx, scope, nullptr, len_field);
  2616. add_entity_use(ctx, nullptr, len_field);
  2617. if (soa_kind == StructSoa_Dynamic) {
  2618. Entity *cap_field = alloc_entity_field(scope, make_token_ident("__$cap"), t_int, false, cast(i32)field_count+1);
  2619. soa_struct->Struct.fields[field_count+1] = cap_field;
  2620. add_entity(ctx, scope, nullptr, cap_field);
  2621. add_entity_use(ctx, nullptr, cap_field);
  2622. init_mem_allocator(ctx->checker);
  2623. Entity *allocator_field = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, cast(i32)field_count+2);
  2624. soa_struct->Struct.fields[field_count+2] = allocator_field;
  2625. add_entity(ctx, scope, nullptr, allocator_field);
  2626. add_entity_use(ctx, nullptr, allocator_field);
  2627. }
  2628. }
  2629. Token token = {};
  2630. token.string = str_lit("Base_Type");
  2631. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2632. add_entity(ctx, scope, nullptr, base_type_entity);
  2633. if (is_complete) {
  2634. add_type_info_type(ctx, soa_struct);
  2635. wait_signal_set(&soa_struct->Struct.fields_wait_signal);
  2636. } else {
  2637. SoaTypeWorkerData *wd = gb_alloc_item(permanent_allocator(), SoaTypeWorkerData);
  2638. wd->ctx = *ctx;
  2639. wd->type = soa_struct;
  2640. wd->wait_to_finish = true;
  2641. mpsc_enqueue(&ctx->checker->soa_types_to_complete, soa_struct);
  2642. thread_pool_add_task(complete_soa_type_worker, wd);
  2643. }
  2644. return soa_struct;
  2645. }
  2646. gb_internal Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type) {
  2647. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, count, generic_type, StructSoa_Fixed);
  2648. }
  2649. gb_internal Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2650. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Slice);
  2651. }
  2652. gb_internal Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2653. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Dynamic);
  2654. }
  2655. gb_internal void check_array_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2656. ast_node(at, ArrayType, e);
  2657. if (at->count != nullptr) {
  2658. Operand o = {};
  2659. i64 count = check_array_count(ctx, &o, at->count);
  2660. Type *generic_type = nullptr;
  2661. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2662. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2663. generic_type = o.type;
  2664. } else if (o.mode == Addressing_Type && is_type_enum(o.type)) {
  2665. Type *index = o.type;
  2666. Type *bt = base_type(index);
  2667. GB_ASSERT(bt->kind == Type_Enum);
  2668. Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, bt->Enum.fields.count, Token_Invalid);
  2669. bool is_sparse = false;
  2670. if (at->tag != nullptr) {
  2671. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2672. String name = at->tag->BasicDirective.name.string;
  2673. if (name == "sparse") {
  2674. is_sparse = true;
  2675. } else {
  2676. error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
  2677. }
  2678. }
  2679. if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count) {
  2680. ERROR_BLOCK();
  2681. error(e, "Non-contiguous enumeration used as an index in an enumerated array");
  2682. long long ea_count = cast(long long)t->EnumeratedArray.count;
  2683. long long enum_count = cast(long long)bt->Enum.fields.count;
  2684. error_line("\tenumerated array length: %lld\n", ea_count);
  2685. error_line("\tenum field count: %lld\n", enum_count);
  2686. error_line("\tSuggestion: prepend #sparse to the enumerated array to allow for non-contiguous elements\n");
  2687. if (2*enum_count < ea_count) {
  2688. error_line("\tWarning: the number of named elements is much smaller than the length of the array, are you sure this is what you want?\n");
  2689. error_line("\t this warning will be removed if #sparse is applied\n");
  2690. }
  2691. }
  2692. t->EnumeratedArray.is_sparse = is_sparse;
  2693. *type = t;
  2694. return;
  2695. }
  2696. if (count < 0) {
  2697. error(at->count, "? can only be used in conjuction with compound literals");
  2698. count = 0;
  2699. }
  2700. if (at->tag != nullptr) {
  2701. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2702. String name = at->tag->BasicDirective.name.string;
  2703. if (name == "soa") {
  2704. *type = make_soa_struct_fixed(ctx, e, at->elem, elem, count, generic_type);
  2705. } else if (name == "simd") {
  2706. if (!is_type_valid_vector_elem(elem) && !is_type_polymorphic(elem)) {
  2707. gbString str = type_to_string(elem);
  2708. error(at->elem, "Invalid element type for #simd, expected an integer, float, or boolean with no specific endianness, got '%s'", str);
  2709. gb_string_free(str);
  2710. *type = alloc_type_array(elem, count, generic_type);
  2711. return;
  2712. }
  2713. if (generic_type != nullptr) {
  2714. // Ignore
  2715. } else if (count < 1 || !is_power_of_two(count)) {
  2716. error(at->count, "Invalid length for #simd, expected a power of two length, got '%lld'", cast(long long)count);
  2717. *type = alloc_type_array(elem, count, generic_type);
  2718. return;
  2719. }
  2720. *type = alloc_type_simd_vector(count, elem, generic_type);
  2721. if (count > SIMD_ELEMENT_COUNT_MAX) {
  2722. error(at->count, "#simd support a maximum element count of %d, got %lld", SIMD_ELEMENT_COUNT_MAX, cast(long long)count);
  2723. }
  2724. } else {
  2725. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2726. *type = alloc_type_array(elem, count, generic_type);
  2727. }
  2728. } else {
  2729. *type = alloc_type_array(elem, count, generic_type);
  2730. }
  2731. } else {
  2732. Type *elem = check_type(ctx, at->elem);
  2733. if (at->tag != nullptr) {
  2734. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2735. String name = at->tag->BasicDirective.name.string;
  2736. if (name == "soa") {
  2737. *type = make_soa_struct_slice(ctx, e, at->elem, elem);
  2738. } else {
  2739. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2740. *type = alloc_type_slice(elem);
  2741. }
  2742. } else {
  2743. *type = alloc_type_slice(elem);
  2744. }
  2745. }
  2746. }
  2747. gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2748. GB_ASSERT_NOT_NULL(type);
  2749. if (e == nullptr) {
  2750. *type = t_invalid;
  2751. return true;
  2752. }
  2753. switch (e->kind) {
  2754. case_ast_node(i, Ident, e);
  2755. Operand o = {};
  2756. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  2757. gb_unused(entity);
  2758. gbString err_str = nullptr;
  2759. defer (gb_string_free(err_str));
  2760. switch (o.mode) {
  2761. case Addressing_Invalid:
  2762. break;
  2763. case Addressing_Type: {
  2764. *type = o.type;
  2765. if (!ctx->in_polymorphic_specialization) {
  2766. Type *t = base_type(o.type);
  2767. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  2768. err_str = expr_to_string(e);
  2769. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  2770. return true;
  2771. }
  2772. }
  2773. return true;
  2774. }
  2775. case Addressing_NoValue:
  2776. err_str = expr_to_string(e);
  2777. error(e, "'%s' used as a type", err_str);
  2778. break;
  2779. default:
  2780. err_str = expr_to_string(e);
  2781. error(e, "'%s' used as a type when not a type", err_str);
  2782. break;
  2783. }
  2784. case_end;
  2785. case_ast_node(ht, HelperType, e);
  2786. return check_type_internal(ctx, ht->type, type, named_type);
  2787. case_end;
  2788. case_ast_node(dt, DistinctType, e);
  2789. error(e, "Invalid use of a distinct type");
  2790. // NOTE(bill): Treat it as a HelperType to remove errors
  2791. return check_type_internal(ctx, dt->type, type, named_type);
  2792. case_end;
  2793. case_ast_node(tt, TypeidType, e);
  2794. e->tav.mode = Addressing_Type;
  2795. e->tav.type = t_typeid;
  2796. *type = t_typeid;
  2797. set_base_type(named_type, *type);
  2798. return true;
  2799. case_end;
  2800. case_ast_node(pt, PolyType, e);
  2801. Ast *ident = pt->type;
  2802. if (ident->kind != Ast_Ident) {
  2803. error(ident, "Expected an identifier after the $");
  2804. *type = t_invalid;
  2805. return false;
  2806. }
  2807. Token token = ident->Ident.token;
  2808. Type *specific = nullptr;
  2809. if (pt->specialization != nullptr) {
  2810. CheckerContext c = *ctx;
  2811. c.in_polymorphic_specialization = true;
  2812. Ast *s = pt->specialization;
  2813. specific = check_type(&c, s);
  2814. }
  2815. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2816. if (ctx->allow_polymorphic_types) {
  2817. Scope *ps = ctx->polymorphic_scope;
  2818. Scope *s = ctx->scope;
  2819. Scope *entity_scope = s;
  2820. if (ps != nullptr && ps != s) {
  2821. // TODO(bill): Is this check needed?
  2822. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2823. entity_scope = ps;
  2824. }
  2825. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2826. t->Generic.entity = e;
  2827. e->TypeName.is_type_alias = true;
  2828. e->state = EntityState_Resolved;
  2829. add_entity(ctx, ps, ident, e);
  2830. add_entity(ctx, s, ident, e);
  2831. } else {
  2832. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2833. *type = t_invalid;
  2834. return false;
  2835. }
  2836. *type = t;
  2837. set_base_type(named_type, *type);
  2838. return true;
  2839. case_end;
  2840. case_ast_node(se, SelectorExpr, e);
  2841. Operand o = {};
  2842. check_selector(ctx, &o, e, nullptr);
  2843. gbString err_str;
  2844. switch (o.mode) {
  2845. case Addressing_Invalid:
  2846. break;
  2847. case Addressing_Type:
  2848. GB_ASSERT(o.type != nullptr);
  2849. *type = o.type;
  2850. return true;
  2851. case Addressing_NoValue:
  2852. err_str = expr_to_string(e);
  2853. error(e, "'%s' used as a type", err_str);
  2854. gb_string_free(err_str);
  2855. break;
  2856. default:
  2857. err_str = expr_to_string(e);
  2858. error(e, "'%s' is not a type", err_str);
  2859. gb_string_free(err_str);
  2860. break;
  2861. }
  2862. case_end;
  2863. case_ast_node(pe, ParenExpr, e);
  2864. if (pe->expr == nullptr) {
  2865. error(e, "Expected an expression or type within the parentheses");
  2866. *type = t_invalid;
  2867. return true;
  2868. }
  2869. *type = check_type_expr(ctx, pe->expr, named_type);
  2870. set_base_type(named_type, *type);
  2871. return true;
  2872. case_end;
  2873. case_ast_node(ue, UnaryExpr, e);
  2874. switch (ue->op.kind) {
  2875. case Token_Pointer:
  2876. {
  2877. Type *elem = check_type(ctx, ue->expr);
  2878. *type = alloc_type_pointer(elem);
  2879. set_base_type(named_type, *type);
  2880. return true;
  2881. }
  2882. }
  2883. case_end;
  2884. case_ast_node(pt, PointerType, e);
  2885. CheckerContext c = *ctx;
  2886. c.type_path = new_checker_type_path();
  2887. defer (destroy_checker_type_path(c.type_path));
  2888. Type *elem = t_invalid;
  2889. Operand o = {};
  2890. if (unparen_expr(pt->type) == nullptr) {
  2891. error(e, "Invalid pointer type");
  2892. return false;
  2893. }
  2894. check_expr_or_type(&c, &o, pt->type);
  2895. if (o.mode != Addressing_Invalid && o.mode != Addressing_Type) {
  2896. if (o.mode == Addressing_Variable) {
  2897. gbString s = expr_to_string(pt->type);
  2898. error(e, "^ is used for pointer types, did you mean '&%s'?", s);
  2899. gb_string_free(s);
  2900. } else {
  2901. // NOTE(bill): call check_type_expr again to get a consistent error message
  2902. elem = check_type_expr(&c, pt->type, nullptr);
  2903. }
  2904. } else {
  2905. elem = o.type;
  2906. }
  2907. if (pt->tag != nullptr) {
  2908. GB_ASSERT(pt->tag->kind == Ast_BasicDirective);
  2909. String name = pt->tag->BasicDirective.name.string;
  2910. if (name == "soa") {
  2911. // TODO(bill): generic #soa pointers
  2912. if (is_type_soa_struct(elem)) {
  2913. *type = alloc_type_soa_pointer(elem);
  2914. } else {
  2915. error(pt->tag, "#soa pointers require an #soa record type as the element");
  2916. *type = alloc_type_pointer(elem);
  2917. }
  2918. } else {
  2919. error(pt->tag, "Invalid tag applied to pointer, got #%.*s", LIT(name));
  2920. *type = alloc_type_pointer(elem);
  2921. }
  2922. } else {
  2923. *type = alloc_type_pointer(elem);
  2924. }
  2925. set_base_type(named_type, *type);
  2926. return true;
  2927. case_end;
  2928. case_ast_node(pt, MultiPointerType, e);
  2929. *type = alloc_type_multi_pointer(check_type(ctx, pt->type));
  2930. set_base_type(named_type, *type);
  2931. return true;
  2932. case_end;
  2933. case_ast_node(rt, RelativeType, e);
  2934. GB_ASSERT(rt->tag->kind == Ast_CallExpr);
  2935. ast_node(ce, CallExpr, rt->tag);
  2936. Type *base_integer = nullptr;
  2937. if (ce->args.count != 1) {
  2938. error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count);
  2939. } else {
  2940. base_integer = check_type(ctx, ce->args[0]);
  2941. if (!is_type_integer(base_integer)) {
  2942. error(rt->type, "#relative base types must be an integer");
  2943. base_integer = nullptr;
  2944. } else if (type_size_of(base_integer) > 64) {
  2945. error(rt->type, "#relative base integer types be less than or equal to 64-bits");
  2946. base_integer = nullptr;
  2947. }
  2948. }
  2949. Type *relative_type = nullptr;
  2950. Type *base_type = check_type(ctx, rt->type);
  2951. if (!is_type_pointer(base_type) && !is_type_multi_pointer(base_type)) {
  2952. error(rt->type, "#relative types can only be a pointer or multi-pointer");
  2953. relative_type = base_type;
  2954. } else if (base_integer == nullptr) {
  2955. relative_type = base_type;
  2956. } else {
  2957. if (is_type_pointer(base_type)) {
  2958. relative_type = alloc_type_relative_pointer(base_type, base_integer);
  2959. } else if (is_type_multi_pointer(base_type)) {
  2960. relative_type = alloc_type_relative_multi_pointer(base_type, base_integer);
  2961. }
  2962. }
  2963. GB_ASSERT(relative_type != nullptr);
  2964. *type = relative_type;
  2965. set_base_type(named_type, *type);
  2966. return true;
  2967. case_end;
  2968. case_ast_node(at, ArrayType, e);
  2969. check_array_type_internal(ctx, e, type, named_type);
  2970. set_base_type(named_type, *type);
  2971. return true;
  2972. case_end;
  2973. case_ast_node(dat, DynamicArrayType, e);
  2974. Type *elem = check_type(ctx, dat->elem);
  2975. if (dat->tag != nullptr) {
  2976. GB_ASSERT(dat->tag->kind == Ast_BasicDirective);
  2977. String name = dat->tag->BasicDirective.name.string;
  2978. if (name == "soa") {
  2979. *type = make_soa_struct_dynamic_array(ctx, e, dat->elem, elem);
  2980. } else {
  2981. error(dat->tag, "Invalid tag applied to dynamic array, got #%.*s", LIT(name));
  2982. *type = alloc_type_dynamic_array(elem);
  2983. }
  2984. } else {
  2985. *type = alloc_type_dynamic_array(elem);
  2986. }
  2987. set_base_type(named_type, *type);
  2988. return true;
  2989. case_end;
  2990. case_ast_node(st, StructType, e);
  2991. CheckerContext c = *ctx;
  2992. c.in_polymorphic_specialization = false;
  2993. c.type_level += 1;
  2994. *type = alloc_type_struct();
  2995. set_base_type(named_type, *type);
  2996. check_open_scope(&c, e);
  2997. check_struct_type(&c, *type, e, nullptr, named_type);
  2998. check_close_scope(&c);
  2999. (*type)->Struct.node = e;
  3000. return true;
  3001. case_end;
  3002. case_ast_node(ut, UnionType, e);
  3003. CheckerContext c = *ctx;
  3004. c.in_polymorphic_specialization = false;
  3005. c.type_level += 1;
  3006. *type = alloc_type_union();
  3007. set_base_type(named_type, *type);
  3008. check_open_scope(&c, e);
  3009. check_union_type(&c, *type, e, nullptr, named_type);
  3010. check_close_scope(&c);
  3011. (*type)->Union.node = e;
  3012. return true;
  3013. case_end;
  3014. case_ast_node(et, EnumType, e);
  3015. bool ips = ctx->in_polymorphic_specialization;
  3016. defer (ctx->in_polymorphic_specialization = ips);
  3017. ctx->in_polymorphic_specialization = false;
  3018. ctx->in_enum_type = true;
  3019. *type = alloc_type_enum();
  3020. set_base_type(named_type, *type);
  3021. check_open_scope(ctx, e);
  3022. check_enum_type(ctx, *type, named_type, e);
  3023. check_close_scope(ctx);
  3024. (*type)->Enum.node = e;
  3025. ctx->in_enum_type = false;
  3026. return true;
  3027. case_end;
  3028. case_ast_node(bs, BitSetType, e);
  3029. *type = alloc_type_bit_set();
  3030. set_base_type(named_type, *type);
  3031. check_bit_set_type(ctx, *type, named_type, e);
  3032. return true;
  3033. case_end;
  3034. case_ast_node(bf, BitFieldType, e);
  3035. bool ips = ctx->in_polymorphic_specialization;
  3036. defer (ctx->in_polymorphic_specialization = ips);
  3037. ctx->in_polymorphic_specialization = false;
  3038. *type = alloc_type_bit_field();
  3039. set_base_type(named_type, *type);
  3040. check_open_scope(ctx, e);
  3041. check_bit_field_type(ctx, *type, named_type, e);
  3042. check_close_scope(ctx);
  3043. (*type)->BitField.node = e;
  3044. return true;
  3045. case_end;
  3046. case_ast_node(pt, ProcType, e);
  3047. bool ips = ctx->in_polymorphic_specialization;
  3048. defer (ctx->in_polymorphic_specialization = ips);
  3049. ctx->in_polymorphic_specialization = false;
  3050. *type = alloc_type(Type_Proc);
  3051. set_base_type(named_type, *type);
  3052. check_open_scope(ctx, e);
  3053. check_procedure_type(ctx, *type, e);
  3054. check_close_scope(ctx);
  3055. return true;
  3056. case_end;
  3057. case_ast_node(mt, MapType, e);
  3058. bool ips = ctx->in_polymorphic_specialization;
  3059. defer (ctx->in_polymorphic_specialization = ips);
  3060. ctx->in_polymorphic_specialization = false;
  3061. *type = alloc_type(Type_Map);
  3062. set_base_type(named_type, *type);
  3063. check_map_type(ctx, *type, e);
  3064. return true;
  3065. case_end;
  3066. case_ast_node(ce, CallExpr, e);
  3067. Operand o = {};
  3068. check_expr_or_type(ctx, &o, e);
  3069. if (o.mode == Addressing_Type) {
  3070. *type = o.type;
  3071. set_base_type(named_type, *type);
  3072. return true;
  3073. }
  3074. case_end;
  3075. case_ast_node(te, TernaryIfExpr, e);
  3076. Operand o = {};
  3077. check_expr_or_type(ctx, &o, e);
  3078. if (o.mode == Addressing_Type) {
  3079. *type = o.type;
  3080. set_base_type(named_type, *type);
  3081. return true;
  3082. }
  3083. case_end;
  3084. case_ast_node(te, TernaryWhenExpr, e);
  3085. Operand o = {};
  3086. check_expr_or_type(ctx, &o, e);
  3087. if (o.mode == Addressing_Type) {
  3088. *type = o.type;
  3089. set_base_type(named_type, *type);
  3090. return true;
  3091. }
  3092. case_end;
  3093. case_ast_node(mt, MatrixType, e);
  3094. check_matrix_type(ctx, type, e);
  3095. set_base_type(named_type, *type);
  3096. return true;
  3097. case_end;
  3098. }
  3099. *type = t_invalid;
  3100. return false;
  3101. }
  3102. gb_internal Type *check_type(CheckerContext *ctx, Ast *e) {
  3103. CheckerContext c = *ctx;
  3104. c.type_path = new_checker_type_path();
  3105. defer (destroy_checker_type_path(c.type_path));
  3106. return check_type_expr(&c, e, nullptr);
  3107. }
  3108. gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  3109. Type *type = nullptr;
  3110. bool ok = check_type_internal(ctx, e, &type, named_type);
  3111. if (!ok) {
  3112. gbString err_str = expr_to_string(e);
  3113. defer (gb_string_free(err_str));
  3114. begin_error_block();
  3115. error(e, "'%s' is not a type", err_str);
  3116. type = t_invalid;
  3117. // NOTE(bill): Check for common mistakes from C programmers
  3118. // e.g. T[] and T[N]
  3119. // e.g. *T
  3120. Ast *node = unparen_expr(e);
  3121. if (node && node->kind == Ast_IndexExpr) {
  3122. gbString index_str = nullptr;
  3123. if (node->IndexExpr.index) {
  3124. index_str = expr_to_string(node->IndexExpr.index);
  3125. }
  3126. defer (gb_string_free(index_str));
  3127. gbString type_str = expr_to_string(node->IndexExpr.expr);
  3128. defer (gb_string_free(type_str));
  3129. error_line("\tSuggestion: Did you mean '[%s]%s'?\n", index_str ? index_str : "", type_str);
  3130. end_error_block();
  3131. // NOTE(bill): Minimize error propagation of bad array syntax by treating this like a type
  3132. if (node->IndexExpr.expr != nullptr) {
  3133. Ast *pseudo_array_expr = ast_array_type(e->file(), ast_token(node->IndexExpr.expr), node->IndexExpr.index, node->IndexExpr.expr);
  3134. check_array_type_internal(ctx, pseudo_array_expr, &type, nullptr);
  3135. }
  3136. } else if (node && node->kind == Ast_UnaryExpr && node->UnaryExpr.op.kind == Token_Mul) {
  3137. gbString type_str = expr_to_string(node->UnaryExpr.expr);
  3138. defer (gb_string_free(type_str));
  3139. error_line("\tSuggestion: Did you mean '^%s'?\n", type_str);
  3140. end_error_block();
  3141. // NOTE(bill): Minimize error propagation of bad array syntax by treating this like a type
  3142. if (node->UnaryExpr.expr != nullptr) {
  3143. Ast *pseudo_pointer_expr = ast_pointer_type(e->file(), ast_token(node->UnaryExpr.expr), node->UnaryExpr.expr);
  3144. return check_type_expr(ctx, pseudo_pointer_expr, named_type);
  3145. }
  3146. } else {
  3147. end_error_block();
  3148. }
  3149. }
  3150. if (type == nullptr) {
  3151. type = t_invalid;
  3152. }
  3153. if (type->kind == Type_Named &&
  3154. type->Named.base == nullptr) {
  3155. // IMPORTANT TODO(bill): Is this a serious error?!
  3156. #if 0
  3157. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  3158. #endif
  3159. type->Named.base = t_invalid;
  3160. }
  3161. if (is_type_polymorphic(type)) {
  3162. type->flags |= TypeFlag_Polymorphic;
  3163. } else if (is_type_polymorphic(type, true)) {
  3164. type->flags |= TypeFlag_PolySpecialized;
  3165. }
  3166. #if 0
  3167. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  3168. gbString str = type_to_string(type);
  3169. error(e, "Invalid use of a polymorphic type '%s'", str);
  3170. gb_string_free(str);
  3171. type = t_invalid;
  3172. }
  3173. #endif
  3174. if (is_type_typed(type)) {
  3175. add_type_and_value(ctx, e, Addressing_Type, type, empty_exact_value);
  3176. } else {
  3177. gbString name = type_to_string(type);
  3178. error(e, "Invalid type definition of %s", name);
  3179. gb_string_free(name);
  3180. type = t_invalid;
  3181. }
  3182. set_base_type(named_type, type);
  3183. check_rtti_type_disallowed(e, type, "Use of a type, %s, which has been disallowed");
  3184. return type;
  3185. }