check_type.cpp 85 KB

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