check_type.cpp 90 KB

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