check_type.cpp 77 KB

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