check_type.cpp 83 KB

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