check_type.cpp 114 KB

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