check_type.cpp 71 KB

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