check_type.cpp 103 KB

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