check_type.cpp 83 KB

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