check_type.cpp 60 KB

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