check_type.cpp 105 KB

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