check_type.cpp 92 KB

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