check_type.cpp 92 KB

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