check_type.cpp 108 KB

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