check_type.cpp 84 KB

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