check_type.cpp 92 KB

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