check_type.cpp 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  1. void populate_using_array_index(CheckerContext *ctx, Ast *node, AstField *field, Type *t, String name, i32 idx) {
  2. t = base_type(t);
  3. GB_ASSERT(t->kind == Type_Array);
  4. Entity *e = scope_lookup_current(ctx->scope, name);
  5. if (e != nullptr) {
  6. gbString str = nullptr;
  7. defer (gb_string_free(str));
  8. if (node != nullptr) {
  9. str = expr_to_string(node);
  10. }
  11. if (str != nullptr) {
  12. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  13. } else {
  14. error(e->token, "'%.*s' is already declared", LIT(name));
  15. }
  16. } else {
  17. Token tok = make_token_ident(name);
  18. if (field->names.count > 0) {
  19. tok.pos = ast_token(field->names[0]).pos;
  20. } else {
  21. tok.pos = ast_token(field->type).pos;
  22. }
  23. Entity *f = alloc_entity_array_elem(nullptr, tok, t->Array.elem, idx);
  24. add_entity(ctx->checker, ctx->scope, nullptr, f);
  25. }
  26. }
  27. void populate_using_entity_scope(CheckerContext *ctx, Ast *node, AstField *field, Type *t) {
  28. if (t == nullptr) {
  29. return;
  30. }
  31. t = base_type(type_deref(t));
  32. gbString str = nullptr;
  33. defer (gb_string_free(str));
  34. if (node != nullptr) {
  35. str = expr_to_string(node);
  36. }
  37. if (t->kind == Type_Struct) {
  38. for_array(i, t->Struct.fields) {
  39. Entity *f = t->Struct.fields[i];
  40. GB_ASSERT(f->kind == Entity_Variable);
  41. String name = f->token.string;
  42. Entity *e = scope_lookup_current(ctx->scope, name);
  43. if (e != nullptr && name != "_") {
  44. // TODO(bill): Better type error
  45. if (str != nullptr) {
  46. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  47. } else {
  48. error(e->token, "'%.*s' is already declared", LIT(name));
  49. }
  50. } else {
  51. add_entity(ctx->checker, ctx->scope, nullptr, f);
  52. if (f->flags & EntityFlag_Using) {
  53. populate_using_entity_scope(ctx, node, field, f->type);
  54. }
  55. }
  56. }
  57. } else if (t->kind == Type_BitField) {
  58. for_array(i, t->BitField.fields) {
  59. Entity *f = t->BitField.fields[i];
  60. String name = f->token.string;
  61. Entity *e = scope_lookup_current(ctx->scope, name);
  62. if ((e != nullptr && name != "_") && (e != f)) {
  63. // TODO(bill): Better type error
  64. if (str != nullptr) {
  65. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  66. } else {
  67. error(e->token, "'%.*s' is already declared", LIT(name));
  68. }
  69. } else {
  70. add_entity(ctx->checker, ctx->scope, nullptr, f);
  71. }
  72. }
  73. } else if (t->kind == Type_Array && t->Array.count <= 4) {
  74. Entity *e = nullptr;
  75. String name = {};
  76. i32 idx = 0;
  77. switch (t->Array.count) {
  78. case 4:
  79. populate_using_array_index(ctx, node, field, t, str_lit("w"), 3);
  80. populate_using_array_index(ctx, node, field, t, str_lit("a"), 3);
  81. /*fallthrough*/
  82. case 3:
  83. populate_using_array_index(ctx, node, field, t, str_lit("z"), 2);
  84. populate_using_array_index(ctx, node, field, t, str_lit("b"), 2);
  85. /*fallthrough*/
  86. case 2:
  87. populate_using_array_index(ctx, node, field, t, str_lit("y"), 1);
  88. populate_using_array_index(ctx, node, field, t, str_lit("g"), 1);
  89. /*fallthrough*/
  90. case 1:
  91. populate_using_array_index(ctx, node, field, t, str_lit("x"), 0);
  92. populate_using_array_index(ctx, node, field, t, str_lit("r"), 0);
  93. /*fallthrough*/
  94. default:
  95. break;
  96. }
  97. }
  98. }
  99. bool does_field_type_allow_using(Type *t) {
  100. t = base_type(t);
  101. if (is_type_struct(t)) {
  102. return true;
  103. } else if (is_type_raw_union(t)) {
  104. return true;
  105. } else if (is_type_bit_field(t)) {
  106. return true;
  107. } else if (is_type_array(t)) {
  108. return t->Array.count <= 4;
  109. } else if (is_type_typeid(t)) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. void check_struct_fields(CheckerContext *ctx, Ast *node, Array<Entity *> *fields, Array<String> *tags, Array<Ast *> const &params,
  115. isize init_field_capacity, Type *struct_type, String context) {
  116. *fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
  117. *tags = array_make<String>(heap_allocator(), 0, init_field_capacity);
  118. GB_ASSERT(node->kind == Ast_StructType);
  119. GB_ASSERT(struct_type->kind == Type_Struct);
  120. isize variable_count = 0;
  121. for_array(i, params) {
  122. Ast *field = params[i];
  123. if (ast_node_expect(field, Ast_Field)) {
  124. ast_node(f, Field, field);
  125. variable_count += gb_max(f->names.count, 1);
  126. }
  127. }
  128. i32 field_src_index = 0;
  129. for_array(i, params) {
  130. Ast *param = params[i];
  131. if (param->kind != Ast_Field) {
  132. continue;
  133. }
  134. ast_node(p, Field, param);
  135. Ast *type_expr = p->type;
  136. Type *type = nullptr;
  137. bool detemine_type_from_operand = false;
  138. if (type_expr != nullptr) {
  139. type = check_type_expr(ctx, type_expr, nullptr);
  140. if (is_type_polymorphic(type)) {
  141. struct_type->Struct.is_polymorphic = true;
  142. type = nullptr;
  143. }
  144. }
  145. if (type == nullptr) {
  146. error(params[i], "Invalid parameter type");
  147. type = t_invalid;
  148. }
  149. if (is_type_untyped(type)) {
  150. if (is_type_untyped_undef(type)) {
  151. error(params[i], "Cannot determine parameter type from ---");
  152. } else {
  153. error(params[i], "Cannot determine parameter type from a nil");
  154. }
  155. type = t_invalid;
  156. }
  157. bool is_using = (p->flags&FieldFlag_using) != 0;
  158. for_array(j, p->names) {
  159. Ast *name = p->names[j];
  160. if (!ast_node_expect(name, Ast_Ident)) {
  161. continue;
  162. }
  163. Token name_token = name->Ident.token;
  164. Entity *field = alloc_entity_field(ctx->scope, name_token, type, is_using, field_src_index);
  165. add_entity(ctx->checker, ctx->scope, name, field);
  166. array_add(fields, field);
  167. array_add(tags, p->tag.string);
  168. field_src_index += 1;
  169. }
  170. if (is_using && p->names.count > 0) {
  171. Type *first_type = (*fields)[fields->count-1]->type;
  172. Type *t = base_type(type_deref(first_type));
  173. if (!does_field_type_allow_using(t) &&
  174. p->names.count >= 1 &&
  175. p->names[0]->kind == Ast_Ident) {
  176. Token name_token = p->names[0]->Ident.token;
  177. gbString type_str = type_to_string(first_type);
  178. error(name_token, "'using' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  179. gb_string_free(type_str);
  180. continue;
  181. }
  182. populate_using_entity_scope(ctx, node, p, type);
  183. }
  184. }
  185. }
  186. Entity *make_names_field_for_struct(CheckerContext *ctx, Scope *scope) {
  187. Entity *e = alloc_entity_field(scope, make_token_ident(str_lit("names")), t_string_slice, false, 0);
  188. e->flags |= EntityFlag_TypeField;
  189. e->flags |= EntityFlag_Value;
  190. return e;
  191. }
  192. bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) {
  193. GB_ASSERT(align_ != nullptr);
  194. Operand o = {};
  195. check_expr(ctx, &o, node);
  196. if (o.mode != Addressing_Constant) {
  197. if (o.mode != Addressing_Invalid) {
  198. error(node, "#align must be a constant");
  199. }
  200. return false;
  201. }
  202. Type *type = base_type(o.type);
  203. if (is_type_untyped(type) || is_type_integer(type)) {
  204. if (o.value.kind == ExactValue_Integer) {
  205. BigInt v = o.value.value_integer;
  206. if (v.len > 1) {
  207. gbAllocator a = heap_allocator();
  208. String str = big_int_to_string(a, &v);
  209. error(node, "#align too large, %.*s", LIT(str));
  210. gb_free(a, str.text);
  211. return false;
  212. }
  213. i64 align = big_int_to_i64(&v);
  214. if (align < 1 || !gb_is_power_of_two(cast(isize)align)) {
  215. error(node, "#align must be a power of 2, got %lld", align);
  216. return false;
  217. }
  218. // NOTE(bill): Success!!!
  219. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  220. if (custom_align < align) {
  221. warning(node, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  222. }
  223. *align_ = custom_align;
  224. return true;
  225. }
  226. }
  227. error(node, "#align must be an integer");
  228. return false;
  229. }
  230. Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure) {
  231. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  232. if (found_gen_types != nullptr) {
  233. for_array(i, *found_gen_types) {
  234. Entity *e = (*found_gen_types)[i];
  235. Type *t = base_type(e->type);
  236. TypeTuple *tuple = get_record_polymorphic_params(t);
  237. GB_ASSERT(param_count == tuple->variables.count);
  238. bool skip = false;
  239. for (isize j = 0; j < param_count; j++) {
  240. Entity *p = tuple->variables[j];
  241. Operand o = ordered_operands[j];
  242. Entity *oe = entity_of_node(o.expr);
  243. if (p == oe) {
  244. // NOTE(bill): This is the same type, make sure that it will be be same thing and use that
  245. // Saves on a lot of checking too below
  246. continue;
  247. }
  248. if (p->kind == Entity_TypeName) {
  249. if (is_type_polymorphic(o.type)) {
  250. // NOTE(bill): Do not add polymorphic version to the gen_types
  251. skip = true;
  252. break;
  253. }
  254. if (!are_types_identical(o.type, p->type)) {
  255. skip = true;
  256. break;
  257. }
  258. } else if (p->kind == Entity_Constant) {
  259. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  260. skip = true;
  261. break;
  262. }
  263. if (!are_types_identical(o.type, p->type)) {
  264. skip = true;
  265. break;
  266. }
  267. } else {
  268. GB_PANIC("Unknown entity kind");
  269. }
  270. }
  271. if (!skip) {
  272. return e;
  273. }
  274. }
  275. }
  276. return nullptr;
  277. }
  278. void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, Type *named_type, Type *original_type) {
  279. GB_ASSERT(is_type_named(named_type));
  280. gbAllocator a = heap_allocator();
  281. Scope *s = ctx->scope->parent;
  282. Entity *e = nullptr;
  283. {
  284. Token token = ast_token(node);
  285. token.kind = Token_String;
  286. token.string = named_type->Named.name;
  287. Ast *node = ast_ident(nullptr, token);
  288. e = alloc_entity_type_name(s, token, named_type);
  289. e->state = EntityState_Resolved;
  290. e->file = ctx->file;
  291. e->pkg = ctx->pkg;
  292. add_entity_use(ctx, node, e);
  293. }
  294. named_type->Named.type_name = e;
  295. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  296. if (found_gen_types) {
  297. array_add(found_gen_types, e);
  298. } else {
  299. auto array = array_make<Entity *>(heap_allocator());
  300. array_add(&array, e);
  301. map_set(&ctx->checker->info.gen_types, hash_pointer(original_type), array);
  302. }
  303. {
  304. Type *dst_bt = base_type(named_type);
  305. Type *src_bt = base_type(original_type);
  306. if ((dst_bt != nullptr && src_bt != nullptr) &&
  307. (dst_bt->kind == src_bt->kind)){
  308. if (dst_bt->kind == Type_Struct) {
  309. if (dst_bt->Struct.atom_op_table == nullptr) {
  310. dst_bt->Struct.atom_op_table = src_bt->Struct.atom_op_table;
  311. }
  312. }
  313. }
  314. }
  315. }
  316. void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  317. GB_ASSERT(is_type_struct(struct_type));
  318. ast_node(st, StructType, node);
  319. String context = str_lit("struct");
  320. isize min_field_count = 0;
  321. for_array(field_index, st->fields) {
  322. Ast *field = st->fields[field_index];
  323. switch (field->kind) {
  324. case_ast_node(f, ValueDecl, field);
  325. min_field_count += f->names.count;
  326. case_end;
  327. case_ast_node(f, Field, field);
  328. min_field_count += f->names.count;
  329. case_end;
  330. }
  331. }
  332. struct_type->Struct.names = make_names_field_for_struct(ctx, ctx->scope);
  333. scope_reserve(ctx->scope, min_field_count);
  334. if (st->is_raw_union && min_field_count > 1) {
  335. struct_type->Struct.is_raw_union = true;
  336. context = str_lit("struct #raw_union");
  337. }
  338. // NOTE(bill): Yes I know it's a non-const reference, what you gonna do?
  339. bool &is_polymorphic = struct_type->Struct.is_polymorphic;
  340. Type *polymorphic_params = nullptr;
  341. bool can_check_fields = true;
  342. bool is_poly_specialized = false;
  343. if (st->polymorphic_params != nullptr) {
  344. ast_node(field_list, FieldList, st->polymorphic_params);
  345. Array<Ast *> params = field_list->list;
  346. if (params.count != 0) {
  347. isize variable_count = 0;
  348. for_array(i, params) {
  349. Ast *field = params[i];
  350. if (ast_node_expect(field, Ast_Field)) {
  351. ast_node(f, Field, field);
  352. variable_count += gb_max(f->names.count, 1);
  353. }
  354. }
  355. auto entities = array_make<Entity *>(ctx->allocator, 0, variable_count);
  356. for_array(i, params) {
  357. Ast *param = params[i];
  358. if (param->kind != Ast_Field) {
  359. continue;
  360. }
  361. ast_node(p, Field, param);
  362. Ast *type_expr = p->type;
  363. Type *type = nullptr;
  364. bool is_type_param = false;
  365. bool is_type_polymorphic_type = false;
  366. if (type_expr == nullptr) {
  367. error(param, "Expected a type for this parameter");
  368. continue;
  369. }
  370. if (type_expr->kind == Ast_Ellipsis) {
  371. type_expr = type_expr->Ellipsis.expr;
  372. error(param, "A polymorphic parameter cannot be variadic");
  373. }
  374. if (type_expr->kind == Ast_TypeidType) {
  375. is_type_param = true;
  376. Type *specialization = nullptr;
  377. if (type_expr->TypeidType.specialization != nullptr) {
  378. Ast *s = type_expr->TypeidType.specialization;
  379. specialization = check_type(ctx, s);
  380. }
  381. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  382. } else {
  383. type = check_type(ctx, type_expr);
  384. if (is_type_polymorphic(type)) {
  385. is_type_polymorphic_type = true;
  386. }
  387. }
  388. if (type == nullptr) {
  389. error(params[i], "Invalid parameter type");
  390. type = t_invalid;
  391. }
  392. if (is_type_untyped(type)) {
  393. if (is_type_untyped_undef(type)) {
  394. error(params[i], "Cannot determine parameter type from ---");
  395. } else {
  396. error(params[i], "Cannot determine parameter type from a nil");
  397. }
  398. type = t_invalid;
  399. }
  400. if (is_type_polymorphic_type) {
  401. gbString str = type_to_string(type);
  402. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  403. gb_string_free(str);
  404. type = t_invalid;
  405. }
  406. if (!is_type_param && !is_type_constant_type(type)) {
  407. gbString str = type_to_string(type);
  408. error(params[i], "A parameter must be a valid constant type, got %s", str);
  409. gb_string_free(str);
  410. }
  411. Scope *scope = ctx->scope;
  412. for_array(j, p->names) {
  413. Ast *name = p->names[j];
  414. if (!ast_node_expect(name, Ast_Ident)) {
  415. continue;
  416. }
  417. Entity *e = nullptr;
  418. Token token = name->Ident.token;
  419. if (poly_operands != nullptr) {
  420. Operand operand = (*poly_operands)[entities.count];
  421. if (is_type_param) {
  422. if (is_type_polymorphic(base_type(operand.type))) {
  423. is_polymorphic = true;
  424. can_check_fields = false;
  425. }
  426. e = alloc_entity_type_name(scope, token, operand.type);
  427. e->TypeName.is_type_alias = true;
  428. } else {
  429. if (is_type_polymorphic(base_type(operand.type))) {
  430. is_polymorphic = true;
  431. can_check_fields = false;
  432. }
  433. if (e == nullptr) {
  434. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  435. }
  436. }
  437. } else {
  438. if (is_type_param) {
  439. e = alloc_entity_type_name(scope, token, type);
  440. e->TypeName.is_type_alias = true;
  441. } else {
  442. e = alloc_entity_constant(scope, token, type, empty_exact_value);
  443. }
  444. }
  445. e->state = EntityState_Resolved;
  446. add_entity(ctx->checker, scope, name, e);
  447. array_add(&entities, e);
  448. }
  449. }
  450. if (entities.count > 0) {
  451. Type *tuple = alloc_type_tuple();
  452. tuple->Tuple.variables = entities;
  453. polymorphic_params = tuple;
  454. }
  455. }
  456. if (original_type_for_poly != nullptr) {
  457. GB_ASSERT(named_type != nullptr);
  458. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  459. }
  460. }
  461. if (!is_polymorphic) {
  462. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  463. }
  464. if (poly_operands != nullptr) {
  465. is_poly_specialized = true;
  466. for (isize i = 0; i < poly_operands->count; i++) {
  467. Operand o = (*poly_operands)[i];
  468. if (is_type_polymorphic(o.type)) {
  469. is_poly_specialized = false;
  470. break;
  471. }
  472. if (struct_type == o.type) {
  473. // NOTE(bill): Cycle
  474. is_poly_specialized = false;
  475. break;
  476. }
  477. }
  478. }
  479. struct_type->Struct.scope = ctx->scope;
  480. struct_type->Struct.is_packed = st->is_packed;
  481. struct_type->Struct.polymorphic_params = polymorphic_params;
  482. struct_type->Struct.is_poly_specialized = is_poly_specialized;
  483. if (!is_polymorphic) {
  484. if (st->where_clauses.count > 0 && st->polymorphic_params == nullptr) {
  485. error(st->where_clauses[0], "'where' clauses can only be used on structures with polymorphic parameters");
  486. } else {
  487. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &st->where_clauses, true);
  488. }
  489. check_struct_fields(ctx, node, &struct_type->Struct.fields, &struct_type->Struct.tags, st->fields, min_field_count, struct_type, context);
  490. }
  491. if (st->align != nullptr) {
  492. if (st->is_packed) {
  493. syntax_error(st->align, "'#align' cannot be applied with '#packed'");
  494. return;
  495. }
  496. i64 custom_align = 1;
  497. if (check_custom_align(ctx, st->align, &custom_align)) {
  498. struct_type->Struct.custom_align = custom_align;
  499. }
  500. }
  501. }
  502. void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  503. GB_ASSERT(is_type_union(union_type));
  504. ast_node(ut, UnionType, node);
  505. isize variant_count = ut->variants.count;
  506. Entity *using_index_expr = nullptr;
  507. auto variants = array_make<Type *>(ctx->allocator, 0, variant_count);
  508. union_type->Union.scope = ctx->scope;
  509. Type *polymorphic_params = nullptr;
  510. bool is_polymorphic = false;
  511. bool can_check_fields = true;
  512. bool is_poly_specialized = false;
  513. if (ut->polymorphic_params != nullptr) {
  514. ast_node(field_list, FieldList, ut->polymorphic_params);
  515. Array<Ast *> params = field_list->list;
  516. if (params.count != 0) {
  517. isize variable_count = 0;
  518. for_array(i, params) {
  519. Ast *field = params[i];
  520. if (ast_node_expect(field, Ast_Field)) {
  521. ast_node(f, Field, field);
  522. variable_count += gb_max(f->names.count, 1);
  523. }
  524. }
  525. auto entities = array_make<Entity *>(ctx->allocator, 0, variable_count);
  526. for_array(i, params) {
  527. Ast *param = params[i];
  528. if (param->kind != Ast_Field) {
  529. continue;
  530. }
  531. ast_node(p, Field, param);
  532. Ast *type_expr = p->type;
  533. Type *type = nullptr;
  534. bool is_type_param = false;
  535. bool is_type_polymorphic_type = false;
  536. if (type_expr == nullptr) {
  537. error(param, "Expected a type for this parameter");
  538. continue;
  539. }
  540. if (type_expr->kind == Ast_Ellipsis) {
  541. type_expr = type_expr->Ellipsis.expr;
  542. error(param, "A polymorphic parameter cannot be variadic");
  543. }
  544. if (type_expr->kind == Ast_TypeidType) {
  545. is_type_param = true;
  546. Type *specialization = nullptr;
  547. if (type_expr->TypeidType.specialization != nullptr) {
  548. Ast *s = type_expr->TypeidType.specialization;
  549. specialization = check_type(ctx, s);
  550. }
  551. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  552. } else {
  553. type = check_type(ctx, type_expr);
  554. if (is_type_polymorphic(type)) {
  555. is_type_polymorphic_type = true;
  556. }
  557. }
  558. if (type == nullptr) {
  559. error(params[i], "Invalid parameter type");
  560. type = t_invalid;
  561. }
  562. if (is_type_untyped(type)) {
  563. if (is_type_untyped_undef(type)) {
  564. error(params[i], "Cannot determine parameter type from ---");
  565. } else {
  566. error(params[i], "Cannot determine parameter type from a nil");
  567. }
  568. type = t_invalid;
  569. }
  570. if (is_type_polymorphic_type) {
  571. gbString str = type_to_string(type);
  572. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  573. gb_string_free(str);
  574. type = t_invalid;
  575. }
  576. if (!is_type_param && !is_type_constant_type(type)) {
  577. gbString str = type_to_string(type);
  578. error(params[i], "A parameter must be a valid constant type, got %s", str);
  579. gb_string_free(str);
  580. }
  581. Scope *scope = ctx->scope;
  582. for_array(j, p->names) {
  583. Ast *name = p->names[j];
  584. if (!ast_node_expect(name, Ast_Ident)) {
  585. continue;
  586. }
  587. Entity *e = nullptr;
  588. Token token = name->Ident.token;
  589. if (poly_operands != nullptr) {
  590. Operand operand = (*poly_operands)[entities.count];
  591. if (is_type_param) {
  592. GB_ASSERT(operand.mode == Addressing_Type ||
  593. operand.mode == Addressing_Invalid);
  594. if (is_type_polymorphic(base_type(operand.type))) {
  595. is_polymorphic = true;
  596. can_check_fields = false;
  597. }
  598. e = alloc_entity_type_name(scope, token, operand.type);
  599. e->TypeName.is_type_alias = true;
  600. } else {
  601. // GB_ASSERT(operand.mode == Addressing_Constant);
  602. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  603. }
  604. } else {
  605. if (is_type_param) {
  606. e = alloc_entity_type_name(scope, token, type);
  607. e->TypeName.is_type_alias = true;
  608. } else {
  609. e = alloc_entity_constant(scope, token, type, empty_exact_value);
  610. }
  611. }
  612. e->state = EntityState_Resolved;
  613. add_entity(ctx->checker, scope, name, e);
  614. array_add(&entities, e);
  615. }
  616. }
  617. if (entities.count > 0) {
  618. Type *tuple = alloc_type_tuple();
  619. tuple->Tuple.variables = entities;
  620. polymorphic_params = tuple;
  621. }
  622. }
  623. if (original_type_for_poly != nullptr) {
  624. GB_ASSERT(named_type != nullptr);
  625. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  626. }
  627. }
  628. if (!is_polymorphic) {
  629. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  630. }
  631. if (poly_operands != nullptr) {
  632. is_poly_specialized = true;
  633. for (isize i = 0; i < poly_operands->count; i++) {
  634. Operand o = (*poly_operands)[i];
  635. if (is_type_polymorphic(o.type)) {
  636. is_poly_specialized = false;
  637. break;
  638. }
  639. if (union_type == o.type) {
  640. // NOTE(bill): Cycle
  641. is_poly_specialized = false;
  642. break;
  643. }
  644. }
  645. }
  646. union_type->Union.scope = ctx->scope;
  647. union_type->Union.polymorphic_params = polymorphic_params;
  648. union_type->Union.is_polymorphic = is_polymorphic;
  649. union_type->Union.is_poly_specialized = is_poly_specialized;
  650. if (ut->where_clauses.count > 0 && ut->polymorphic_params == nullptr) {
  651. error(ut->where_clauses[0], "'where' clauses can only be used on unions with polymorphic parameters");
  652. } else {
  653. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &ut->where_clauses, true);
  654. }
  655. for_array(i, ut->variants) {
  656. Ast *node = ut->variants[i];
  657. Type *t = check_type_expr(ctx, node, nullptr);
  658. if (t != nullptr && t != t_invalid) {
  659. bool ok = true;
  660. t = default_type(t);
  661. if (is_type_untyped(t) || is_type_empty_union(t)) {
  662. ok = false;
  663. gbString str = type_to_string(t);
  664. error(node, "Invalid variant type in union '%s'", str);
  665. gb_string_free(str);
  666. } else {
  667. for_array(j, variants) {
  668. if (are_types_identical(t, variants[j])) {
  669. ok = false;
  670. gbString str = type_to_string(t);
  671. error(node, "Duplicate variant type '%s'", str);
  672. gb_string_free(str);
  673. break;
  674. }
  675. }
  676. }
  677. if (ok) {
  678. array_add(&variants, t);
  679. }
  680. }
  681. }
  682. union_type->Union.variants = variants;
  683. union_type->Union.no_nil = ut->no_nil;
  684. union_type->Union.maybe = ut->maybe;
  685. if (union_type->Union.no_nil) {
  686. if (variants.count < 2) {
  687. error(ut->align, "A union with #no_nil must have at least 2 variants");
  688. }
  689. }
  690. if (union_type->Union.maybe) {
  691. if (variants.count != 1) {
  692. error(ut->align, "A union with #maybe must have at 1 variant, got %lld", cast(long long)variants.count);
  693. }
  694. }
  695. if (ut->align != nullptr) {
  696. i64 custom_align = 1;
  697. if (check_custom_align(ctx, ut->align, &custom_align)) {
  698. if (variants.count == 0) {
  699. error(ut->align, "An empty union cannot have a custom alignment");
  700. } else {
  701. union_type->Union.custom_align = custom_align;
  702. }
  703. }
  704. }
  705. }
  706. void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast *node) {
  707. ast_node(et, EnumType, node);
  708. GB_ASSERT(is_type_enum(enum_type));
  709. Type *base_type = t_int;
  710. if (et->base_type != nullptr) {
  711. base_type = check_type(ctx, et->base_type);
  712. }
  713. if (base_type == nullptr || !is_type_integer(base_type)) {
  714. error(node, "Base type for enumeration must be an integer");
  715. return;
  716. }
  717. if (is_type_enum(base_type)) {
  718. error(node, "Base type for enumeration cannot be another enumeration");
  719. return;
  720. }
  721. if (is_type_integer_128bit(base_type)) {
  722. error(node, "Base type for enumeration cannot be a 128-bit integer");
  723. return;
  724. }
  725. // NOTE(bill): Must be up here for the 'check_init_constant' system
  726. enum_type->Enum.base_type = base_type;
  727. enum_type->Enum.scope = ctx->scope;
  728. auto fields = array_make<Entity *>(ctx->allocator, 0, et->fields.count);
  729. Type *constant_type = enum_type;
  730. if (named_type != nullptr) {
  731. constant_type = named_type;
  732. }
  733. ExactValue iota = exact_value_i64(-1);
  734. ExactValue min_value = exact_value_i64(0);
  735. ExactValue max_value = exact_value_i64(0);
  736. isize min_value_index = 0;
  737. isize max_value_index = 0;
  738. bool min_value_set = false;
  739. bool max_value_set = false;
  740. scope_reserve(ctx->scope, et->fields.count);
  741. for_array(i, et->fields) {
  742. Ast *field = et->fields[i];
  743. Ast *ident = nullptr;
  744. Ast *init = nullptr;
  745. if (field->kind == Ast_FieldValue) {
  746. ast_node(fv, FieldValue, field);
  747. if (fv->field == nullptr || fv->field->kind != Ast_Ident) {
  748. error(field, "An enum field's name must be an identifier");
  749. continue;
  750. }
  751. ident = fv->field;
  752. init = fv->value;
  753. } else if (field->kind == Ast_Ident) {
  754. ident = field;
  755. } else {
  756. error(field, "An enum field's name must be an identifier");
  757. continue;
  758. }
  759. String name = ident->Ident.token.string;
  760. if (init != nullptr) {
  761. Operand o = {};
  762. check_expr(ctx, &o, init);
  763. if (o.mode != Addressing_Constant) {
  764. error(init, "Enumeration value must be a constant");
  765. o.mode = Addressing_Invalid;
  766. }
  767. if (o.mode != Addressing_Invalid) {
  768. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  769. }
  770. if (o.mode != Addressing_Invalid) {
  771. iota = o.value;
  772. } else {
  773. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  774. }
  775. } else {
  776. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  777. }
  778. // NOTE(bill): Skip blank identifiers
  779. if (is_blank_ident(name)) {
  780. continue;
  781. } else if (name == "names") {
  782. error(field, "'names' is a reserved identifier for enumerations");
  783. continue;
  784. }
  785. if (min_value_set) {
  786. if (compare_exact_values(Token_Gt, min_value, iota)) {
  787. min_value_index = i;
  788. min_value = iota;
  789. }
  790. } else {
  791. min_value_index = i;
  792. min_value = iota;
  793. min_value_set = true;
  794. }
  795. if (max_value_set) {
  796. if (compare_exact_values(Token_Lt, max_value, iota)) {
  797. max_value_index = i;
  798. max_value = iota;
  799. }
  800. } else {
  801. max_value_index = i;
  802. max_value = iota;
  803. max_value_set = true;
  804. }
  805. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  806. e->identifier = ident;
  807. e->flags |= EntityFlag_Visited;
  808. e->state = EntityState_Resolved;
  809. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  810. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  811. } else {
  812. add_entity(ctx->checker, ctx->scope, nullptr, e);
  813. array_add(&fields, e);
  814. // TODO(bill): Should I add a use for the enum value?
  815. add_entity_use(ctx, field, e);
  816. }
  817. }
  818. GB_ASSERT(fields.count <= et->fields.count);
  819. enum_type->Enum.fields = fields;
  820. enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope);
  821. enum_type->Enum.min_value = min_value;
  822. enum_type->Enum.max_value = max_value;
  823. enum_type->Enum.min_value_index = min_value_index;
  824. enum_type->Enum.max_value_index = max_value_index;
  825. }
  826. void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Ast *node) {
  827. ast_node(bft, BitFieldType, node);
  828. GB_ASSERT(is_type_bit_field(bit_field_type));
  829. auto fields = array_make<Entity*>(ctx->allocator, 0, bft->fields.count);
  830. auto sizes = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  831. auto offsets = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  832. scope_reserve(ctx->scope, bft->fields.count);
  833. u32 curr_offset = 0;
  834. for_array(i, bft->fields) {
  835. Ast *field = bft->fields[i];
  836. GB_ASSERT(field->kind == Ast_FieldValue);
  837. Ast *ident = field->FieldValue.field;
  838. Ast *value = field->FieldValue.value;
  839. if (ident->kind != Ast_Ident) {
  840. error(field, "A bit field value's name must be an identifier");
  841. continue;
  842. }
  843. String name = ident->Ident.token.string;
  844. Operand o = {};
  845. check_expr(ctx, &o, value);
  846. if (o.mode != Addressing_Constant) {
  847. error(value, "Bit field bit size must be a constant");
  848. continue;
  849. }
  850. ExactValue v = exact_value_to_integer(o.value);
  851. if (v.kind != ExactValue_Integer) {
  852. error(value, "Bit field bit size must be a constant integer");
  853. continue;
  854. }
  855. i64 bits_ = big_int_to_i64(&v.value_integer); // TODO(bill): what if the integer is huge?
  856. if (bits_ < 0 || bits_ > 64) {
  857. error(value, "Bit field's bit size must be within the range 1...64, got %lld", cast(long long)bits_);
  858. continue;
  859. }
  860. u32 bits = cast(u32)bits_;
  861. Type *value_type = alloc_type_bit_field_value(bits);
  862. Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type);
  863. e->identifier = ident;
  864. e->flags |= EntityFlag_BitFieldValue;
  865. if (!is_blank_ident(name) &&
  866. scope_lookup_current(ctx->scope, name) != nullptr) {
  867. error(ident, "'%.*s' is already declared in this bit field", LIT(name));
  868. } else {
  869. add_entity(ctx->checker, ctx->scope, nullptr, e);
  870. // TODO(bill): Should this entity be "used"?
  871. add_entity_use(ctx, field, e);
  872. array_add(&fields, e);
  873. array_add(&offsets, curr_offset);
  874. array_add(&sizes, bits);
  875. curr_offset += bits;
  876. }
  877. }
  878. GB_ASSERT(fields.count <= bft->fields.count);
  879. bit_field_type->BitField.fields = fields;
  880. bit_field_type->BitField.sizes = sizes;
  881. bit_field_type->BitField.offsets = offsets;
  882. if (bft->align != nullptr) {
  883. i64 custom_align = 1;
  884. if (check_custom_align(ctx, bft->align, &custom_align)) {
  885. bit_field_type->BitField.custom_align = custom_align;
  886. }
  887. }
  888. }
  889. bool is_type_valid_bit_set_range(Type *t) {
  890. if (is_type_integer(t)) {
  891. return true;
  892. }
  893. if (is_type_rune(t)) {
  894. return true;
  895. }
  896. return false;
  897. }
  898. void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  899. ast_node(bs, BitSetType, node);
  900. GB_ASSERT(type->kind == Type_BitSet);
  901. i64 const DEFAULT_BITS = cast(i64)(8*build_context.word_size);
  902. i64 const MAX_BITS = 128;
  903. Ast *base = unparen_expr(bs->elem);
  904. if (is_ast_range(base)) {
  905. ast_node(be, BinaryExpr, base);
  906. Operand lhs = {};
  907. Operand rhs = {};
  908. check_expr(c, &lhs, be->left);
  909. check_expr(c, &rhs, be->right);
  910. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  911. return;
  912. }
  913. convert_to_typed(c, &lhs, rhs.type);
  914. if (lhs.mode == Addressing_Invalid) {
  915. return;
  916. }
  917. convert_to_typed(c, &rhs, lhs.type);
  918. if (rhs.mode == Addressing_Invalid) {
  919. return;
  920. }
  921. if (!are_types_identical(lhs.type, rhs.type)) {
  922. if (lhs.type != t_invalid &&
  923. rhs.type != t_invalid) {
  924. gbString xt = type_to_string(lhs.type);
  925. gbString yt = type_to_string(rhs.type);
  926. gbString expr_str = expr_to_string(bs->elem);
  927. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  928. gb_string_free(expr_str);
  929. gb_string_free(yt);
  930. gb_string_free(xt);
  931. }
  932. return;
  933. }
  934. if (!is_type_valid_bit_set_range(lhs.type)) {
  935. gbString str = type_to_string(lhs.type);
  936. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  937. gb_string_free(str);
  938. return;
  939. }
  940. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  941. error(bs->elem, "Intervals must be constant values");
  942. return;
  943. }
  944. ExactValue iv = exact_value_to_integer(lhs.value);
  945. ExactValue jv = exact_value_to_integer(rhs.value);
  946. GB_ASSERT(iv.kind == ExactValue_Integer);
  947. GB_ASSERT(jv.kind == ExactValue_Integer);
  948. BigInt i = iv.value_integer;
  949. BigInt j = jv.value_integer;
  950. if (big_int_cmp(&i, &j) > 0) {
  951. gbAllocator a = heap_allocator();
  952. String si = big_int_to_string(a, &i);
  953. String sj = big_int_to_string(a, &j);
  954. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  955. gb_free(a, si.text);
  956. gb_free(a, sj.text);
  957. return;
  958. }
  959. Type *t = default_type(lhs.type);
  960. if (bs->underlying != nullptr) {
  961. Type *u = check_type(c, bs->underlying);
  962. if (!is_type_integer(u)) {
  963. gbString ts = type_to_string(u);
  964. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  965. gb_string_free(ts);
  966. return;
  967. }
  968. type->BitSet.underlying = u;
  969. }
  970. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  971. gbAllocator a = heap_allocator();
  972. String s = big_int_to_string(a, &i);
  973. gbString ts = type_to_string(t);
  974. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  975. gb_string_free(ts);
  976. gb_free(a, s.text);
  977. return;
  978. }
  979. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  980. gbAllocator a = heap_allocator();
  981. String s = big_int_to_string(a, &j);
  982. gbString ts = type_to_string(t);
  983. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  984. gb_string_free(ts);
  985. gb_free(a, s.text);
  986. return;
  987. }
  988. i64 lower = big_int_to_i64(&i);
  989. i64 upper = big_int_to_i64(&j);
  990. i64 bits = MAX_BITS;
  991. if (type->BitSet.underlying != nullptr) {
  992. bits = 8*type_size_of(type->BitSet.underlying);
  993. }
  994. switch (be->op.kind) {
  995. case Token_Ellipsis:
  996. if (upper - lower >= bits) {
  997. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, (upper-lower+1));
  998. }
  999. break;
  1000. case Token_RangeHalf:
  1001. if (upper - lower > bits) {
  1002. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, (upper-lower));
  1003. }
  1004. upper -= 1;
  1005. break;
  1006. }
  1007. type->BitSet.elem = t;
  1008. type->BitSet.lower = lower;
  1009. type->BitSet.upper = upper;
  1010. } else {
  1011. Type *elem = check_type_expr(c, bs->elem, nullptr);
  1012. #if 0
  1013. if (named_type != nullptr && named_type->kind == Type_Named &&
  1014. elem->kind == Type_Enum) {
  1015. // NOTE(bill): Anonymous enumeration
  1016. String prefix = named_type->Named.name;
  1017. String enum_name = concatenate_strings(heap_allocator(), prefix, str_lit(".enum"));
  1018. Token token = make_token_ident(enum_name);
  1019. Entity *e = alloc_entity_type_name(nullptr, token, nullptr, EntityState_Resolved);
  1020. Type *named = alloc_type_named(enum_name, elem, e);
  1021. e->type = named;
  1022. e->TypeName.is_type_alias = true;
  1023. elem = named;
  1024. }
  1025. #endif
  1026. type->BitSet.elem = elem;
  1027. if (!is_type_valid_bit_set_elem(elem)) {
  1028. error(bs->elem, "Expected an enum type for a bit_set");
  1029. } else {
  1030. Type *et = base_type(elem);
  1031. if (et->kind == Type_Enum) {
  1032. if (!is_type_integer(et->Enum.base_type)) {
  1033. error(bs->elem, "Enum type for bit_set must be an integer");
  1034. return;
  1035. }
  1036. i64 lower = 0;
  1037. i64 upper = 0;
  1038. for_array(i, et->Enum.fields) {
  1039. Entity *e = et->Enum.fields[i];
  1040. if (e->kind != Entity_Constant) {
  1041. continue;
  1042. }
  1043. ExactValue value = exact_value_to_integer(e->Constant.value);
  1044. GB_ASSERT(value.kind == ExactValue_Integer);
  1045. // NOTE(bill): enum types should be able to store i64 values
  1046. i64 x = big_int_to_i64(&value.value_integer);
  1047. lower = gb_min(lower, x);
  1048. upper = gb_max(upper, x);
  1049. }
  1050. GB_ASSERT(lower <= upper);
  1051. i64 bits = MAX_BITS;
  1052. if (bs->underlying != nullptr) {
  1053. Type *u = check_type(c, bs->underlying);
  1054. if (!is_type_integer(u)) {
  1055. gbString ts = type_to_string(u);
  1056. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1057. gb_string_free(ts);
  1058. return;
  1059. }
  1060. type->BitSet.underlying = u;
  1061. bits = 8*type_size_of(u);
  1062. }
  1063. if (upper - lower >= MAX_BITS) {
  1064. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", MAX_BITS, (upper-lower+1));
  1065. }
  1066. type->BitSet.lower = lower;
  1067. type->BitSet.upper = upper;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  1073. if (type == nullptr ||
  1074. type == t_invalid) {
  1075. return true;
  1076. }
  1077. Type *t = base_type(type);
  1078. Type *s = base_type(specialization);
  1079. if (t->kind != s->kind) {
  1080. return false;
  1081. }
  1082. if (is_type_untyped(t)) {
  1083. Operand o = {Addressing_Value};
  1084. o.type = default_type(type);
  1085. bool can_convert = check_cast_internal(ctx, &o, specialization);
  1086. return can_convert;
  1087. } else if (t->kind == Type_Struct) {
  1088. if (t->Struct.polymorphic_parent == specialization) {
  1089. return true;
  1090. }
  1091. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  1092. s->Struct.polymorphic_params != nullptr &&
  1093. t->Struct.polymorphic_params != nullptr) {
  1094. TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
  1095. TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
  1096. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1097. for_array(i, s_tuple->variables) {
  1098. Entity *s_e = s_tuple->variables[i];
  1099. Entity *t_e = t_tuple->variables[i];
  1100. Type *st = s_e->type;
  1101. Type *tt = t_e->type;
  1102. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1103. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1104. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1105. GB_ASSERT(e != nullptr);
  1106. if (modify_type) {
  1107. e->kind = Entity_Constant;
  1108. e->Constant.value = t_e->Constant.value;
  1109. e->type = t_e->type;
  1110. }
  1111. } else {
  1112. if (st->kind == Type_Basic && tt->kind == Type_Basic &&
  1113. s_e->kind == Entity_Constant && t_e->kind == Entity_Constant) {
  1114. if (!compare_exact_values(Token_CmpEq, s_e->Constant.value, t_e->Constant.value))
  1115. return false;
  1116. } else {
  1117. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1118. }
  1119. }
  1120. }
  1121. if (modify_type) {
  1122. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1123. gb_memmove(specialization, type, gb_size_of(Type));
  1124. }
  1125. return true;
  1126. }
  1127. } else if (t->kind == Type_Union) {
  1128. if (t->Union.polymorphic_parent == specialization) {
  1129. return true;
  1130. }
  1131. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1132. s->Union.polymorphic_params != nullptr &&
  1133. t->Union.polymorphic_params != nullptr) {
  1134. TypeTuple *s_tuple = &s->Union.polymorphic_params->Tuple;
  1135. TypeTuple *t_tuple = &t->Union.polymorphic_params->Tuple;
  1136. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1137. for_array(i, s_tuple->variables) {
  1138. Entity *s_e = s_tuple->variables[i];
  1139. Entity *t_e = t_tuple->variables[i];
  1140. Type *st = s_e->type;
  1141. Type *tt = t_e->type;
  1142. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1143. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1144. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1145. GB_ASSERT(e != nullptr);
  1146. if (modify_type) {
  1147. e->kind = Entity_Constant;
  1148. e->Constant.value = t_e->Constant.value;
  1149. e->type = t_e->type;
  1150. }
  1151. } else {
  1152. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1153. }
  1154. }
  1155. if (modify_type) {
  1156. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1157. gb_memmove(specialization, type, gb_size_of(Type));
  1158. }
  1159. return true;
  1160. }
  1161. }
  1162. if (specialization->kind == Type_Named &&
  1163. type->kind != Type_Named) {
  1164. return false;
  1165. }
  1166. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1167. return true;
  1168. }
  1169. return false;
  1170. }
  1171. Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand operand) {
  1172. bool modify_type = !ctx->no_polymorphic_errors;
  1173. bool show_error = modify_type && !ctx->hide_polymorphic_errors;
  1174. if (!is_operand_value(operand)) {
  1175. if (show_error) {
  1176. error(operand.expr, "Cannot determine polymorphic type from parameter");
  1177. }
  1178. return t_invalid;
  1179. }
  1180. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1181. if (show_error) {
  1182. set_procedure_abi_types(ctx->allocator, poly_type);
  1183. }
  1184. return poly_type;
  1185. }
  1186. if (show_error) {
  1187. gbString pts = type_to_string(poly_type);
  1188. gbString ots = type_to_string(operand.type);
  1189. defer (gb_string_free(pts));
  1190. defer (gb_string_free(ots));
  1191. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1192. }
  1193. return t_invalid;
  1194. }
  1195. bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1196. if (expr == nullptr) {
  1197. return false;
  1198. }
  1199. expr = unparen_expr(expr);
  1200. if (expr->kind == Ast_SelectorExpr) {
  1201. Ast *lhs = expr->SelectorExpr.expr;
  1202. return is_expr_from_a_parameter(ctx, lhs);
  1203. } else if (expr->kind == Ast_Ident) {
  1204. Operand x= {};
  1205. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1206. if (e->flags & EntityFlag_Param) {
  1207. return true;
  1208. }
  1209. }
  1210. return false;
  1211. }
  1212. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1213. ParameterValue param_value = {};
  1214. if (expr == nullptr) {
  1215. return param_value;
  1216. }
  1217. Operand o = {};
  1218. if (allow_caller_location &&
  1219. expr->kind == Ast_BasicDirective &&
  1220. expr->BasicDirective.name == "caller_location") {
  1221. init_core_source_code_location(ctx->checker);
  1222. param_value.kind = ParameterValue_Location;
  1223. o.type = t_source_code_location;
  1224. } else {
  1225. if (in_type) {
  1226. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1227. } else {
  1228. check_expr(ctx, &o, expr);
  1229. }
  1230. if (is_operand_nil(o)) {
  1231. param_value.kind = ParameterValue_Nil;
  1232. } else if (o.mode != Addressing_Constant) {
  1233. if (expr->kind == Ast_ProcLit) {
  1234. param_value.kind = ParameterValue_Constant;
  1235. param_value.value = exact_value_procedure(expr);
  1236. } else {
  1237. Entity *e = nullptr;
  1238. // if (o.mode == Addressing_Value && is_type_proc(o.type)) {
  1239. if (o.mode == Addressing_Value || o.mode == Addressing_Variable) {
  1240. Operand x = {};
  1241. if (expr->kind == Ast_Ident) {
  1242. e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1243. } else if (expr->kind == Ast_SelectorExpr) {
  1244. e = check_selector(ctx, &x, expr, nullptr);
  1245. }
  1246. }
  1247. if (e != nullptr) {
  1248. if (e->kind == Entity_Procedure) {
  1249. param_value.kind = ParameterValue_Constant;
  1250. param_value.value = exact_value_procedure(e->identifier);
  1251. add_entity_use(ctx, e->identifier, e);
  1252. } else {
  1253. if (e->flags & EntityFlag_Param) {
  1254. error(expr, "Default parameter cannot be another parameter");
  1255. } else {
  1256. if (is_expr_from_a_parameter(ctx, expr)) {
  1257. error(expr, "Default parameter cannot be another parameter");
  1258. } else {
  1259. param_value.kind = ParameterValue_Value;
  1260. param_value.ast_value = expr;
  1261. add_entity_use(ctx, e->identifier, e);
  1262. }
  1263. }
  1264. }
  1265. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1266. param_value.kind = ParameterValue_Value;
  1267. param_value.ast_value = expr;
  1268. } else {
  1269. error(expr, "Default parameter must be a constant");
  1270. }
  1271. }
  1272. } else {
  1273. if (o.value.kind != ExactValue_Invalid) {
  1274. param_value.kind = ParameterValue_Constant;
  1275. param_value.value = o.value;
  1276. } else {
  1277. error(o.expr, "Invalid constant parameter");
  1278. }
  1279. }
  1280. }
  1281. if (in_type) {
  1282. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1283. }
  1284. if (out_type_) *out_type_ = default_type(o.type);
  1285. return param_value;
  1286. }
  1287. Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> *operands) {
  1288. if (_params == nullptr) {
  1289. return nullptr;
  1290. }
  1291. bool allow_polymorphic_types = ctx->allow_polymorphic_types;
  1292. bool success = true;
  1293. ast_node(field_list, FieldList, _params);
  1294. Array<Ast *> params = field_list->list;
  1295. if (params.count == 0) {
  1296. if (success_) *success_ = success;
  1297. return nullptr;
  1298. }
  1299. isize variable_count = 0;
  1300. for_array(i, params) {
  1301. Ast *field = params[i];
  1302. if (ast_node_expect(field, Ast_Field)) {
  1303. ast_node(f, Field, field);
  1304. variable_count += gb_max(f->names.count, 1);
  1305. }
  1306. }
  1307. isize min_variable_count = variable_count;
  1308. for (isize i = params.count-1; i >= 0; i--) {
  1309. Ast *field = params[i];
  1310. if (field->kind == Ast_Field) {
  1311. ast_node(f, Field, field);
  1312. if (f->default_value == nullptr) {
  1313. break;
  1314. }
  1315. min_variable_count--;
  1316. }
  1317. }
  1318. bool is_variadic = false;
  1319. isize variadic_index = -1;
  1320. bool is_c_vararg = false;
  1321. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  1322. for_array(i, params) {
  1323. Ast *param = params[i];
  1324. if (param->kind != Ast_Field) {
  1325. continue;
  1326. }
  1327. ast_node(p, Field, param);
  1328. Ast *type_expr = unparen_expr(p->type);
  1329. Type *type = nullptr;
  1330. Ast *default_value = unparen_expr(p->default_value);
  1331. ParameterValue param_value = {};
  1332. bool is_type_param = false;
  1333. bool is_type_polymorphic_type = false;
  1334. bool detemine_type_from_operand = false;
  1335. Type *specialization = nullptr;
  1336. bool is_using = (p->flags&FieldFlag_using) != 0;
  1337. if (type_expr == nullptr) {
  1338. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1339. } else {
  1340. if (type_expr->kind == Ast_Ellipsis) {
  1341. type_expr = type_expr->Ellipsis.expr;
  1342. is_variadic = true;
  1343. variadic_index = variables.count;
  1344. if (p->names.count != 1) {
  1345. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1346. success = false;
  1347. }
  1348. }
  1349. if (type_expr->kind == Ast_TypeidType) {
  1350. ast_node(tt, TypeidType, type_expr);
  1351. if (tt->specialization) {
  1352. specialization = check_type(ctx, tt->specialization);
  1353. if (specialization == t_invalid){
  1354. specialization = nullptr;
  1355. }
  1356. if (operands != nullptr) {
  1357. detemine_type_from_operand = true;
  1358. type = t_invalid;
  1359. } else {
  1360. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1361. }
  1362. } else {
  1363. type = t_typeid;
  1364. }
  1365. } else {
  1366. bool prev = ctx->allow_polymorphic_types;
  1367. if (operands != nullptr) {
  1368. ctx->allow_polymorphic_types = true;
  1369. }
  1370. type = check_type(ctx, type_expr);
  1371. ctx->allow_polymorphic_types = prev;
  1372. if (is_type_polymorphic(type)) {
  1373. is_type_polymorphic_type = true;
  1374. }
  1375. }
  1376. if (default_value != nullptr) {
  1377. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1378. error(type_expr, "A type parameter may not have a default value");
  1379. } else {
  1380. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1381. }
  1382. }
  1383. }
  1384. if (type == nullptr) {
  1385. error(param, "Invalid parameter type");
  1386. type = t_invalid;
  1387. }
  1388. if (is_type_untyped(type)) {
  1389. if (is_type_untyped_undef(type)) {
  1390. error(param, "Cannot determine parameter type from ---");
  1391. } else {
  1392. error(param, "Cannot determine parameter type from a nil");
  1393. }
  1394. type = t_invalid;
  1395. }
  1396. if (is_type_empty_union(type)) {
  1397. gbString str = type_to_string(type);
  1398. error(param, "Invalid use of an empty union '%s'", str);
  1399. gb_string_free(str);
  1400. type = t_invalid;
  1401. }
  1402. if (p->flags&FieldFlag_c_vararg) {
  1403. if (p->type == nullptr ||
  1404. p->type->kind != Ast_Ellipsis) {
  1405. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1406. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1407. } else {
  1408. is_c_vararg = true;
  1409. }
  1410. }
  1411. for_array(j, p->names) {
  1412. Ast *name = p->names[j];
  1413. bool is_poly_name = false;
  1414. switch (name->kind) {
  1415. case Ast_Ident:
  1416. break;
  1417. case Ast_PolyType:
  1418. GB_ASSERT(name->PolyType.specialization == nullptr);
  1419. is_poly_name = true;
  1420. name = name->PolyType.type;
  1421. break;
  1422. }
  1423. if (!ast_node_expect(name, Ast_Ident)) {
  1424. continue;
  1425. }
  1426. if (is_poly_name) {
  1427. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1428. is_type_param = true;
  1429. } else {
  1430. if (param_value.kind != ParameterValue_Invalid) {
  1431. error(default_value, "Constant parameters cannot have a default value");
  1432. param_value.kind = ParameterValue_Invalid;
  1433. }
  1434. }
  1435. }
  1436. Entity *param = nullptr;
  1437. if (is_type_param) {
  1438. if (operands != nullptr) {
  1439. Operand o = (*operands)[variables.count];
  1440. if (o.mode == Addressing_Type) {
  1441. type = o.type;
  1442. } else {
  1443. if (!ctx->no_polymorphic_errors) {
  1444. error(o.expr, "Expected a type to assign to the type parameter");
  1445. }
  1446. success = false;
  1447. type = t_invalid;
  1448. }
  1449. if (is_type_polymorphic(type)) {
  1450. gbString str = type_to_string(type);
  1451. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1452. gb_string_free(str);
  1453. success = false;
  1454. type = t_invalid;
  1455. }
  1456. if (is_type_untyped(default_type(type))) {
  1457. gbString str = type_to_string(type);
  1458. error(o.expr, "Cannot determine type from the parameter, got '%s'", str);
  1459. gb_string_free(str);
  1460. success = false;
  1461. type = t_invalid;
  1462. }
  1463. bool modify_type = !ctx->no_polymorphic_errors;
  1464. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1465. if (!ctx->no_polymorphic_errors) {
  1466. gbString t = type_to_string(type);
  1467. gbString s = type_to_string(specialization);
  1468. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1469. gb_string_free(s);
  1470. gb_string_free(t);
  1471. }
  1472. success = false;
  1473. type = t_invalid;
  1474. }
  1475. }
  1476. if (p->flags&FieldFlag_auto_cast) {
  1477. error(name, "'auto_cast' can only be applied to variable fields");
  1478. p->flags &= ~FieldFlag_auto_cast;
  1479. }
  1480. if (p->flags&FieldFlag_const) {
  1481. error(name, "'#const' can only be applied to variable fields");
  1482. p->flags &= ~FieldFlag_const;
  1483. }
  1484. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1485. param->TypeName.is_type_alias = true;
  1486. } else {
  1487. ExactValue poly_const = {};
  1488. if (operands != nullptr && variables.count < operands->count) {
  1489. Operand op = (*operands)[variables.count];
  1490. if (op.expr == nullptr) {
  1491. // NOTE(bill): 2019-03-30
  1492. // This is just to add the error message to determine_type_from_polymorphic which
  1493. // depends on valid position information
  1494. op.expr = _params;
  1495. }
  1496. if (is_type_polymorphic_type) {
  1497. type = determine_type_from_polymorphic(ctx, type, op);
  1498. if (type == t_invalid) {
  1499. success = false;
  1500. } else if (!ctx->no_polymorphic_errors) {
  1501. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1502. is_type_polymorphic_type = false;
  1503. }
  1504. }
  1505. if (is_poly_name) {
  1506. bool valid = false;
  1507. if (is_type_proc(op.type)) {
  1508. Entity *proc_entity = entity_from_expr(op.expr);
  1509. valid = proc_entity != nullptr;
  1510. poly_const = exact_value_procedure(proc_entity->identifier ? proc_entity->identifier : op.expr);
  1511. }
  1512. if (!valid) {
  1513. if (op.mode == Addressing_Constant) {
  1514. poly_const = op.value;
  1515. } else {
  1516. error(op.expr, "Expected a constant value for this polymorphic name parameter");
  1517. success = false;
  1518. }
  1519. }
  1520. }
  1521. if (is_type_untyped(default_type(type))) {
  1522. gbString str = type_to_string(type);
  1523. error(op.expr, "Cannot determine type from the parameter, got '%s'", str);
  1524. gb_string_free(str);
  1525. success = false;
  1526. type = t_invalid;
  1527. }
  1528. }
  1529. if (p->flags&FieldFlag_no_alias) {
  1530. if (!is_type_pointer(type)) {
  1531. error(name, "'#no_alias' can only be applied to fields of pointer type");
  1532. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1533. }
  1534. }
  1535. if (is_poly_name) {
  1536. if (p->flags&FieldFlag_no_alias) {
  1537. error(name, "'#no_alias' can only be applied to non constant values");
  1538. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1539. }
  1540. if (p->flags&FieldFlag_auto_cast) {
  1541. error(name, "'auto_cast' can only be applied to variable fields");
  1542. p->flags &= ~FieldFlag_auto_cast;
  1543. }
  1544. if (p->flags&FieldFlag_const) {
  1545. error(name, "'#const' can only be applied to variable fields");
  1546. p->flags &= ~FieldFlag_const;
  1547. }
  1548. if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
  1549. gbString str = type_to_string(type);
  1550. error(params[i], "A parameter must be a valid constant type, got %s", str);
  1551. gb_string_free(str);
  1552. }
  1553. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1554. } else {
  1555. param = alloc_entity_param(scope, name->Ident.token, type, is_using, true);
  1556. param->Variable.param_value = param_value;
  1557. }
  1558. }
  1559. if (p->flags&FieldFlag_no_alias) {
  1560. param->flags |= EntityFlag_NoAlias;
  1561. }
  1562. if (p->flags&FieldFlag_auto_cast) {
  1563. param->flags |= EntityFlag_AutoCast;
  1564. }
  1565. if (p->flags&FieldFlag_const) {
  1566. param->flags |= EntityFlag_ConstInput;
  1567. }
  1568. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1569. add_entity(ctx->checker, scope, name, param);
  1570. if (is_using) {
  1571. add_entity_use(ctx, name, param);
  1572. }
  1573. array_add(&variables, param);
  1574. }
  1575. }
  1576. if (is_variadic) {
  1577. GB_ASSERT(variadic_index >= 0);
  1578. }
  1579. if (is_variadic) {
  1580. GB_ASSERT(params.count > 0);
  1581. // NOTE(bill): Change last variadic parameter to be a slice
  1582. // Custom Calling convention for variadic parameters
  1583. Entity *end = variables[variadic_index];
  1584. end->type = alloc_type_slice(end->type);
  1585. end->flags |= EntityFlag_Ellipsis;
  1586. if (is_c_vararg) {
  1587. end->flags |= EntityFlag_CVarArg;
  1588. }
  1589. }
  1590. isize specialization_count = 0;
  1591. if (scope != nullptr) {
  1592. for_array(i, scope->elements.entries) {
  1593. Entity *e = scope->elements.entries[i].value;
  1594. if (e->kind == Entity_TypeName) {
  1595. Type *t = e->type;
  1596. if (t->kind == Type_Generic &&
  1597. t->Generic.specialized != nullptr) {
  1598. specialization_count += 1;
  1599. }
  1600. }
  1601. }
  1602. }
  1603. Type *tuple = alloc_type_tuple();
  1604. tuple->Tuple.variables = variables;
  1605. if (success_) *success_ = success;
  1606. if (specialization_count_) *specialization_count_ = specialization_count;
  1607. if (is_variadic_) *is_variadic_ = is_variadic;
  1608. if (variadic_index_) *variadic_index_ = variadic_index;
  1609. return tuple;
  1610. }
  1611. Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1612. if (_results == nullptr) {
  1613. return nullptr;
  1614. }
  1615. ast_node(field_list, FieldList, _results);
  1616. Array<Ast *> results = field_list->list;
  1617. if (results.count == 0) {
  1618. return nullptr;
  1619. }
  1620. Type *tuple = alloc_type_tuple();
  1621. isize variable_count = 0;
  1622. for_array(i, results) {
  1623. Ast *field = results[i];
  1624. if (ast_node_expect(field, Ast_Field)) {
  1625. ast_node(f, Field, field);
  1626. variable_count += gb_max(f->names.count, 1);
  1627. }
  1628. }
  1629. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  1630. for_array(i, results) {
  1631. ast_node(field, Field, results[i]);
  1632. Ast *default_value = unparen_expr(field->default_value);
  1633. ParameterValue param_value = {};
  1634. Type *type = nullptr;
  1635. if (field->type == nullptr) {
  1636. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1637. } else {
  1638. type = check_type(ctx, field->type);
  1639. if (default_value != nullptr) {
  1640. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1641. }
  1642. }
  1643. if (type == nullptr) {
  1644. error(results[i], "Invalid parameter type");
  1645. type = t_invalid;
  1646. }
  1647. if (is_type_untyped(type)) {
  1648. error(results[i], "Cannot determine parameter type from a nil");
  1649. type = t_invalid;
  1650. }
  1651. if (field->names.count == 0) {
  1652. Token token = ast_token(field->type);
  1653. token.string = str_lit("");
  1654. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1655. param->Variable.param_value = param_value;
  1656. array_add(&variables, param);
  1657. } else {
  1658. for_array(j, field->names) {
  1659. Token token = ast_token(results[i]);
  1660. if (field->type != nullptr) {
  1661. token = ast_token(field->type);
  1662. }
  1663. token.string = str_lit("");
  1664. Ast *name = field->names[j];
  1665. if (name->kind != Ast_Ident) {
  1666. error(name, "Expected an identifer for as the field name");
  1667. } else {
  1668. token = name->Ident.token;
  1669. }
  1670. if (is_blank_ident(token)) {
  1671. error(name, "Result value cannot be a blank identifer `_`");
  1672. }
  1673. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1674. param->flags |= EntityFlag_Result;
  1675. param->Variable.param_value = param_value;
  1676. array_add(&variables, param);
  1677. add_entity(ctx->checker, scope, name, param);
  1678. // NOTE(bill): Removes `declared but not used` when using -vet
  1679. add_entity_use(ctx, name, param);
  1680. }
  1681. }
  1682. }
  1683. for_array(i, variables) {
  1684. String x = variables[i]->token.string;
  1685. if (x.len == 0 || is_blank_ident(x)) {
  1686. continue;
  1687. }
  1688. for (isize j = i+1; j < variables.count; j++) {
  1689. String y = variables[j]->token.string;
  1690. if (y.len == 0 || is_blank_ident(y)) {
  1691. continue;
  1692. }
  1693. if (x == y) {
  1694. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1695. }
  1696. }
  1697. }
  1698. tuple->Tuple.variables = variables;
  1699. return tuple;
  1700. }
  1701. Array<Type *> systemv_distribute_struct_fields(Type *t) {
  1702. Type *bt = core_type(t);
  1703. isize distributed_cap = 1;
  1704. if (bt->kind == Type_Struct) {
  1705. distributed_cap = bt->Struct.fields.count;
  1706. }
  1707. auto distributed = array_make<Type *>(heap_allocator(), 0, distributed_cap);
  1708. i64 sz = type_size_of(bt);
  1709. switch (bt->kind) {
  1710. case Type_Basic:
  1711. switch (bt->Basic.kind){
  1712. case Basic_complex64:
  1713. array_add(&distributed, t_f32);
  1714. array_add(&distributed, t_f32);
  1715. break;
  1716. case Basic_complex128:
  1717. array_add(&distributed, t_f64);
  1718. array_add(&distributed, t_f64);
  1719. break;
  1720. case Basic_quaternion128:
  1721. array_add(&distributed, t_f32);
  1722. array_add(&distributed, t_f32);
  1723. array_add(&distributed, t_f32);
  1724. array_add(&distributed, t_f32);
  1725. break;
  1726. case Basic_quaternion256:
  1727. goto DEFAULT;
  1728. case Basic_string:
  1729. array_add(&distributed, t_u8_ptr);
  1730. array_add(&distributed, t_int);
  1731. break;
  1732. case Basic_any:
  1733. GB_ASSERT(type_size_of(t_uintptr) == type_size_of(t_typeid));
  1734. array_add(&distributed, t_rawptr);
  1735. array_add(&distributed, t_uintptr);
  1736. break;
  1737. case Basic_u128:
  1738. case Basic_i128:
  1739. if (build_context.ODIN_OS == "windows") {
  1740. array_add(&distributed, alloc_type_simd_vector(2, t_u64));
  1741. } else {
  1742. array_add(&distributed, bt);
  1743. }
  1744. break;
  1745. default:
  1746. goto DEFAULT;
  1747. }
  1748. break;
  1749. case Type_Struct:
  1750. if (bt->Struct.is_raw_union) {
  1751. goto DEFAULT;
  1752. } else {
  1753. // IMPORTANT TOOD(bill): handle #packed structs correctly
  1754. // IMPORTANT TODO(bill): handle #align structs correctly
  1755. for_array(field_index, bt->Struct.fields) {
  1756. Entity *f = bt->Struct.fields[field_index];
  1757. auto nested = systemv_distribute_struct_fields(f->type);
  1758. array_add_elems(&distributed, nested.data, nested.count);
  1759. array_free(&nested);
  1760. }
  1761. }
  1762. break;
  1763. case Type_Array:
  1764. for (i64 i = 0; i < bt->Array.count; i++) {
  1765. array_add(&distributed, bt->Array.elem);
  1766. }
  1767. break;
  1768. case Type_BitSet:
  1769. array_add(&distributed, bit_set_to_int(bt));
  1770. break;
  1771. case Type_Tuple:
  1772. GB_PANIC("Invalid struct field type");
  1773. break;
  1774. case Type_Slice:
  1775. array_add(&distributed, t_rawptr);
  1776. array_add(&distributed, t_int);
  1777. break;
  1778. case Type_Union:
  1779. case Type_DynamicArray:
  1780. case Type_Map:
  1781. case Type_BitField: // TODO(bill): Ignore?
  1782. // NOTE(bill, 2019-10-10): Odin specific, don't worry about C calling convention yet
  1783. goto DEFAULT;
  1784. case Type_Pointer:
  1785. case Type_Proc:
  1786. case Type_SimdVector: // TODO(bill): Is this correct logic?
  1787. default:
  1788. DEFAULT:;
  1789. if (sz > 0) {
  1790. array_add(&distributed, bt);
  1791. }
  1792. break;
  1793. }
  1794. return distributed;
  1795. }
  1796. Type *struct_type_from_systemv_distribute_struct_fields(Type *abi_type) {
  1797. GB_ASSERT(is_type_tuple(abi_type));
  1798. Type *final_type = alloc_type_struct();
  1799. final_type->Struct.fields = abi_type->Tuple.variables;
  1800. return final_type;
  1801. }
  1802. Type *handle_single_distributed_type_parameter(Array<Type *> const &types, bool packed, isize *offset) {
  1803. GB_ASSERT(types.count > 0);
  1804. if (types.count == 1) {
  1805. if (offset) *offset = 1;
  1806. i64 sz = type_size_of(types[0]);
  1807. if (is_type_float(types[0])) {
  1808. return types[0];
  1809. }
  1810. switch (sz) {
  1811. case 0:
  1812. GB_PANIC("Zero sized type found!");
  1813. case 1: return t_u8;
  1814. case 2: return t_u16;
  1815. case 4: return t_u32;
  1816. case 8: return t_u64;
  1817. default:
  1818. return types[0];
  1819. }
  1820. } else if (types.count >= 2) {
  1821. if (types[0] == t_f32 && types[1] == t_f32) {
  1822. if (offset) *offset = 2;
  1823. return alloc_type_simd_vector(2, t_f32);
  1824. } else if (type_size_of(types[0]) == 8) {
  1825. if (offset) *offset = 1;
  1826. return types[0];
  1827. }
  1828. i64 total_size = 0;
  1829. isize i = 0;
  1830. if (packed) {
  1831. for (; i < types.count && total_size < 8; i += 1) {
  1832. Type *t = types[i];
  1833. i64 s = type_size_of(t);
  1834. total_size += s;
  1835. }
  1836. } else {
  1837. for (; i < types.count && total_size < 8; i += 1) {
  1838. Type *t = types[i];
  1839. i64 s = gb_max(type_size_of(t), 0);
  1840. i64 a = gb_max(type_align_of(t), 1);
  1841. isize ts = align_formula(total_size, a);
  1842. if (ts >= 8) {
  1843. break;
  1844. }
  1845. total_size = ts + s;
  1846. }
  1847. }
  1848. if (offset) *offset = i;
  1849. switch (total_size) {
  1850. case 1: return t_u8;
  1851. case 2: return t_u16;
  1852. case 4: return t_u32;
  1853. case 8: return t_u64;
  1854. }
  1855. return t_u64;
  1856. }
  1857. return nullptr;
  1858. }
  1859. Type *handle_struct_system_v_amd64_abi_type(Type *t) {
  1860. if (type_size_of(t) > 16) {
  1861. return alloc_type_pointer(t);
  1862. }
  1863. Type *original_type = t;
  1864. Type *bt = core_type(t);
  1865. t = base_type(t);
  1866. i64 size = type_size_of(bt);
  1867. switch (t->kind) {
  1868. case Type_Slice:
  1869. case Type_Struct:
  1870. break;
  1871. case Type_Basic:
  1872. switch (bt->Basic.kind) {
  1873. case Basic_string:
  1874. case Basic_any:
  1875. case Basic_complex64:
  1876. case Basic_complex128:
  1877. case Basic_quaternion128:
  1878. break;
  1879. default:
  1880. return original_type;
  1881. }
  1882. break;
  1883. default:
  1884. return original_type;
  1885. }
  1886. bool is_packed = false;
  1887. if (is_type_struct(bt)) {
  1888. is_packed = bt->Struct.is_packed;
  1889. }
  1890. if (is_type_raw_union(bt)) {
  1891. // TODO(bill): Handle raw union correctly for
  1892. return t;
  1893. } else {
  1894. auto field_types = systemv_distribute_struct_fields(bt);
  1895. defer (array_free(&field_types));
  1896. GB_ASSERT(field_types.count <= 16);
  1897. Type *final_type = nullptr;
  1898. if (field_types.count == 0) {
  1899. final_type = t;
  1900. } else if (field_types.count == 1) {
  1901. final_type = field_types[0];
  1902. } else {
  1903. if (size <= 8) {
  1904. isize offset = 0;
  1905. final_type = handle_single_distributed_type_parameter(field_types, is_packed, &offset);
  1906. } else {
  1907. isize offset = 0;
  1908. isize next_offset = 0;
  1909. Type *two_types[2] = {};
  1910. two_types[0] = handle_single_distributed_type_parameter(field_types, is_packed, &offset);
  1911. auto remaining = array_slice(field_types, offset, field_types.count);
  1912. two_types[1] = handle_single_distributed_type_parameter(remaining, is_packed, &next_offset);
  1913. GB_ASSERT(offset + next_offset == field_types.count);
  1914. auto variables = array_make<Entity *>(heap_allocator(), 2);
  1915. variables[0] = alloc_entity_param(nullptr, empty_token, two_types[0], false, false);
  1916. variables[1] = alloc_entity_param(nullptr, empty_token, two_types[1], false, false);
  1917. final_type = alloc_type_tuple();
  1918. final_type->Tuple.variables = variables;
  1919. if (t->kind == Type_Struct) {
  1920. // NOTE(bill): Make this packed
  1921. final_type->Tuple.is_packed = t->Struct.is_packed;
  1922. }
  1923. }
  1924. }
  1925. GB_ASSERT(final_type != nullptr);
  1926. i64 ftsz = type_size_of(final_type);
  1927. i64 otsz = type_size_of(original_type);
  1928. if (ftsz != otsz) {
  1929. // TODO(bill): Handle this case which will be caused by #packed most likely
  1930. switch (otsz) {
  1931. case 1:
  1932. case 2:
  1933. case 4:
  1934. case 8:
  1935. GB_PANIC("Incorrectly handled case for handle_struct_system_v_amd64_abi_type, %s %lld vs %s %lld", type_to_string(final_type), ftsz, type_to_string(original_type), otsz);
  1936. }
  1937. }
  1938. return final_type;
  1939. }
  1940. }
  1941. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) {
  1942. Type *new_type = original_type;
  1943. if (is_type_boolean(original_type)) {
  1944. Type *t = core_type(base_type(new_type));
  1945. if (t == t_bool) {
  1946. return t_llvm_bool;
  1947. }
  1948. return new_type;
  1949. }
  1950. if (cc == ProcCC_None || cc == ProcCC_PureNone) {
  1951. return new_type;
  1952. }
  1953. if (build_context.ODIN_ARCH == "386") {
  1954. return new_type;
  1955. }
  1956. if (is_type_simd_vector(original_type)) {
  1957. return new_type;
  1958. }
  1959. if (build_context.ODIN_ARCH == "amd64") {
  1960. if (is_type_integer_128bit(original_type)) {
  1961. if (build_context.ODIN_OS == "windows") {
  1962. return alloc_type_simd_vector(2, t_u64);
  1963. } else {
  1964. return original_type;
  1965. }
  1966. }
  1967. }
  1968. if (build_context.ODIN_OS == "windows") {
  1969. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  1970. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  1971. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  1972. Type *bt = core_type(original_type);
  1973. switch (bt->kind) {
  1974. // Okay to pass by value (usually)
  1975. // Especially the only Odin types
  1976. case Type_Basic: {
  1977. i64 sz = bt->Basic.size;
  1978. // if (sz > 8 && build_context.word_size < 8) {
  1979. if (sz > 8) {
  1980. new_type = alloc_type_pointer(original_type);
  1981. }
  1982. break;
  1983. }
  1984. case Type_Pointer: break;
  1985. case Type_Proc: break; // NOTE(bill): Just a pointer
  1986. // Odin specific
  1987. case Type_Slice:
  1988. case Type_Array:
  1989. case Type_DynamicArray:
  1990. case Type_Map:
  1991. case Type_Union:
  1992. // Could be in C too
  1993. case Type_Struct:
  1994. {
  1995. i64 align = type_align_of(original_type);
  1996. i64 size = type_size_of(original_type);
  1997. switch (8*size) {
  1998. case 8: new_type = t_u8; break;
  1999. case 16: new_type = t_u16; break;
  2000. case 32: new_type = t_u32; break;
  2001. case 64: new_type = t_u64; break;
  2002. default:
  2003. new_type = alloc_type_pointer(original_type);
  2004. break;
  2005. }
  2006. break;
  2007. }
  2008. }
  2009. } else if (build_context.ODIN_OS == "linux" ||
  2010. build_context.ODIN_OS == "darwin") {
  2011. Type *bt = core_type(original_type);
  2012. switch (bt->kind) {
  2013. // Okay to pass by value (usually)
  2014. // Especially the only Odin types
  2015. case Type_Basic: {
  2016. i64 sz = bt->Basic.size;
  2017. // if (sz > 8 && build_context.word_size < 8) {
  2018. if (sz > 8) {
  2019. new_type = alloc_type_pointer(original_type);
  2020. }
  2021. break;
  2022. }
  2023. case Type_Pointer: break;
  2024. case Type_Proc: break; // NOTE(bill): Just a pointer
  2025. default: {
  2026. i64 size = type_size_of(original_type);
  2027. if (size > 16) {
  2028. new_type = alloc_type_pointer(original_type);
  2029. } else if (build_context.ODIN_ARCH == "amd64") {
  2030. // NOTE(bill): System V AMD64 ABI
  2031. new_type = handle_struct_system_v_amd64_abi_type(bt);
  2032. if (are_types_identical(core_type(original_type), new_type)) {
  2033. new_type = original_type;
  2034. }
  2035. return new_type;
  2036. }
  2037. break;
  2038. }
  2039. }
  2040. } else {
  2041. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  2042. // their architectures
  2043. }
  2044. return new_type;
  2045. }
  2046. Type *reduce_tuple_to_single_type(Type *original_type) {
  2047. if (original_type != nullptr) {
  2048. Type *t = core_type(original_type);
  2049. if (t->kind == Type_Tuple && t->Tuple.variables.count == 1) {
  2050. return t->Tuple.variables[0]->type;
  2051. }
  2052. }
  2053. return original_type;
  2054. }
  2055. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) {
  2056. Type *new_type = original_type;
  2057. if (new_type == nullptr) {
  2058. return nullptr;
  2059. }
  2060. GB_ASSERT(is_type_tuple(original_type));
  2061. Type *single_type = reduce_tuple_to_single_type(original_type);
  2062. if (is_type_simd_vector(single_type)) {
  2063. return new_type;
  2064. }
  2065. if (build_context.ODIN_OS == "windows") {
  2066. if (build_context.ODIN_ARCH == "amd64") {
  2067. if (is_type_integer_128bit(single_type)) {
  2068. if (cc == ProcCC_None || cc == ProcCC_PureNone) {
  2069. return original_type;
  2070. } else {
  2071. return alloc_type_simd_vector(2, t_u64);
  2072. }
  2073. }
  2074. }
  2075. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  2076. // NOTE(bill): This is just reversed engineered from LLVM IR output
  2077. switch (bt->kind) {
  2078. // Okay to pass by value
  2079. // Especially the only Odin types
  2080. case Type_Pointer: break;
  2081. case Type_Proc: break; // NOTE(bill): Just a pointer
  2082. case Type_Basic: break;
  2083. default: {
  2084. i64 align = type_align_of(original_type);
  2085. i64 size = type_size_of(original_type);
  2086. switch (8*size) {
  2087. #if 1
  2088. case 8: new_type = t_u8; break;
  2089. case 16: new_type = t_u16; break;
  2090. case 32: new_type = t_u32; break;
  2091. case 64: new_type = t_u64; break;
  2092. #endif
  2093. }
  2094. break;
  2095. }
  2096. }
  2097. } else if (build_context.ODIN_OS == "linux" || build_context.ODIN_OS == "darwin") {
  2098. if (build_context.ODIN_ARCH == "amd64") {
  2099. }
  2100. } else {
  2101. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  2102. // their architectures
  2103. }
  2104. if (is_type_integer_128bit(single_type)) {
  2105. if (build_context.word_size == 8) {
  2106. return original_type;
  2107. }
  2108. }
  2109. if (new_type != original_type) {
  2110. Type *tuple = alloc_type_tuple();
  2111. auto variables = array_make<Entity *>(a, 0, 1);
  2112. array_add(&variables, alloc_entity_param(original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false));
  2113. tuple->Tuple.variables = variables;
  2114. new_type = tuple;
  2115. }
  2116. new_type->cached_size = -1;
  2117. new_type->cached_align = -1;
  2118. return new_type;
  2119. }
  2120. bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  2121. if (abi_return_type == nullptr) {
  2122. return false;
  2123. }
  2124. if (cc == ProcCC_None || cc == ProcCC_PureNone) {
  2125. return false;
  2126. }
  2127. Type *single_type = reduce_tuple_to_single_type(abi_return_type);
  2128. if (is_type_simd_vector(single_type)) {
  2129. return false;
  2130. }
  2131. if (build_context.word_size == 8) {
  2132. if (is_type_integer_128bit(single_type)) {
  2133. return false;
  2134. }
  2135. }
  2136. if (build_context.ODIN_OS == "windows") {
  2137. i64 size = 8*type_size_of(abi_return_type);
  2138. switch (size) {
  2139. case 0:
  2140. case 8:
  2141. case 16:
  2142. case 32:
  2143. case 64:
  2144. return false;
  2145. default:
  2146. return true;
  2147. }
  2148. } else {
  2149. if (is_type_integer_128bit(single_type)) {
  2150. return build_context.word_size < 8;
  2151. }
  2152. }
  2153. return false;
  2154. }
  2155. void set_procedure_abi_types(gbAllocator allocator, Type *type) {
  2156. type = base_type(type);
  2157. if (type->kind != Type_Proc) {
  2158. return;
  2159. }
  2160. if (type->Proc.abi_types_set) {
  2161. return;
  2162. }
  2163. type->Proc.abi_compat_params = array_make<Type *>(allocator, cast(isize)type->Proc.param_count);
  2164. for (i32 i = 0; i < type->Proc.param_count; i++) {
  2165. Entity *e = type->Proc.params->Tuple.variables[i];
  2166. if (e->kind == Entity_Variable) {
  2167. Type *original_type = e->type;
  2168. Type *new_type = type_to_abi_compat_param_type(allocator, original_type, type->Proc.calling_convention);
  2169. type->Proc.abi_compat_params[i] = new_type;
  2170. switch (type->Proc.calling_convention) {
  2171. case ProcCC_Odin:
  2172. case ProcCC_Contextless:
  2173. case ProcCC_Pure:
  2174. if (is_type_pointer(new_type) & !is_type_pointer(e->type)) {
  2175. e->flags |= EntityFlag_ImplicitReference;
  2176. }
  2177. break;
  2178. }
  2179. if (build_context.ODIN_OS == "linux" ||
  2180. build_context.ODIN_OS == "darwin") {
  2181. if (is_type_pointer(new_type) & !is_type_pointer(e->type)) {
  2182. e->flags |= EntityFlag_ByVal;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. for (i32 i = 0; i < type->Proc.param_count; i++) {
  2188. Entity *e = type->Proc.params->Tuple.variables[i];
  2189. if (e->kind == Entity_Variable) {
  2190. set_procedure_abi_types(allocator, e->type);
  2191. }
  2192. }
  2193. for (i32 i = 0; i < type->Proc.result_count; i++) {
  2194. Entity *e = type->Proc.results->Tuple.variables[i];
  2195. if (e->kind == Entity_Variable) {
  2196. set_procedure_abi_types(allocator, e->type);
  2197. }
  2198. }
  2199. // NOTE(bill): The types are the same
  2200. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(allocator, type->Proc.results, type->Proc.calling_convention);
  2201. type->Proc.return_by_pointer = abi_compat_return_by_pointer(allocator, type->Proc.calling_convention, type->Proc.abi_compat_result_type);
  2202. type->Proc.abi_types_set = true;
  2203. }
  2204. // NOTE(bill): 'operands' is for generating non generic procedure type
  2205. bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> *operands) {
  2206. ast_node(pt, ProcType, proc_type_node);
  2207. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  2208. ctx->polymorphic_scope = ctx->scope;
  2209. }
  2210. CheckerContext c_ = *ctx;
  2211. CheckerContext *c = &c_;
  2212. c->curr_proc_sig = type;
  2213. c->in_proc_sig = true;
  2214. ProcCallingConvention cc = pt->calling_convention;
  2215. if (cc == ProcCC_ForeignBlockDefault) {
  2216. cc = ProcCC_CDecl;
  2217. if (c->foreign_context.default_cc > 0) {
  2218. cc = c->foreign_context.default_cc;
  2219. }
  2220. }
  2221. GB_ASSERT(cc > 0);
  2222. if (cc == ProcCC_Odin) {
  2223. c->scope->flags |= ScopeFlag_ContextDefined;
  2224. } else {
  2225. c->scope->flags &= ~ScopeFlag_ContextDefined;
  2226. }
  2227. bool variadic = false;
  2228. isize variadic_index = -1;
  2229. bool success = true;
  2230. isize specialization_count = 0;
  2231. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  2232. Type *results = check_get_results(c, c->scope, pt->results);
  2233. isize param_count = 0;
  2234. isize result_count = 0;
  2235. if (params) param_count = params ->Tuple.variables.count;
  2236. if (results) result_count = results->Tuple.variables.count;
  2237. if (param_count > 0) {
  2238. for_array(i, params->Tuple.variables) {
  2239. Entity *param = params->Tuple.variables[i];
  2240. if (param->kind == Entity_Variable) {
  2241. ParameterValue pv = param->Variable.param_value;
  2242. if (pv.kind == ParameterValue_Constant &&
  2243. pv.value.kind == ExactValue_Procedure) {
  2244. type->Proc.has_proc_default_values = true;
  2245. break;
  2246. }
  2247. }
  2248. }
  2249. }
  2250. if (result_count > 0) {
  2251. Entity *first = results->Tuple.variables[0];
  2252. type->Proc.has_named_results = first->token.string != "";
  2253. }
  2254. if (result_count == 0 && cc == ProcCC_Pure) {
  2255. error(proc_type_node, "\"pure\" procedures must have at least 1 return value");
  2256. }
  2257. bool optional_ok = (pt->tags & ProcTag_optional_ok) != 0;
  2258. if (optional_ok) {
  2259. if (result_count != 2) {
  2260. error(proc_type_node, "A procedure type with the #optional_ok tag requires 2 return values, got %td", result_count);
  2261. } else {
  2262. Entity *second = results->Tuple.variables[1];
  2263. if (is_type_polymorphic(second->type)) {
  2264. // ignore
  2265. } else if (is_type_boolean(second->type)) {
  2266. // GOOD
  2267. } else {
  2268. error(second->token, "Second return value of an #optional_ok procedure must be a boolean, got %s", type_to_string(second->type));
  2269. }
  2270. }
  2271. }
  2272. type->Proc.node = proc_type_node;
  2273. type->Proc.scope = c->scope;
  2274. type->Proc.params = params;
  2275. type->Proc.param_count = cast(i32)param_count;
  2276. type->Proc.results = results;
  2277. type->Proc.result_count = cast(i32)result_count;
  2278. type->Proc.variadic = variadic;
  2279. type->Proc.variadic_index = cast(i32)variadic_index;
  2280. type->Proc.calling_convention = cc;
  2281. type->Proc.is_polymorphic = pt->generic;
  2282. type->Proc.specialization_count = specialization_count;
  2283. type->Proc.diverging = pt->diverging;
  2284. type->Proc.optional_ok = optional_ok;
  2285. type->Proc.tags = pt->tags;
  2286. if (param_count > 0) {
  2287. Entity *end = params->Tuple.variables[param_count-1];
  2288. if (end->flags&EntityFlag_CVarArg) {
  2289. if (cc == ProcCC_StdCall || cc == ProcCC_CDecl) {
  2290. type->Proc.c_vararg = true;
  2291. } else {
  2292. error(end->token, "Calling convention does not support #c_vararg");
  2293. }
  2294. }
  2295. }
  2296. bool is_polymorphic = false;
  2297. for (isize i = 0; i < param_count; i++) {
  2298. Entity *e = params->Tuple.variables[i];
  2299. if (e->kind != Entity_Variable) {
  2300. is_polymorphic = true;
  2301. break;
  2302. } else if (is_type_polymorphic(e->type)) {
  2303. is_polymorphic = true;
  2304. break;
  2305. }
  2306. }
  2307. for (isize i = 0; i < result_count; i++) {
  2308. Entity *e = results->Tuple.variables[i];
  2309. if (e->kind != Entity_Variable) {
  2310. is_polymorphic = true;
  2311. break;
  2312. } else if (is_type_polymorphic(e->type)) {
  2313. is_polymorphic = true;
  2314. break;
  2315. }
  2316. }
  2317. type->Proc.is_polymorphic = is_polymorphic;
  2318. return success;
  2319. }
  2320. i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  2321. if (e == nullptr) {
  2322. return 0;
  2323. }
  2324. if (e->kind == Ast_UnaryExpr &&
  2325. e->UnaryExpr.op.kind == Token_Question) {
  2326. return -1;
  2327. }
  2328. check_expr_or_type(ctx, o, e);
  2329. if (o->mode == Addressing_Type && o->type->kind == Type_Generic) {
  2330. if (ctx->allow_polymorphic_types) {
  2331. if (o->type->Generic.specialized) {
  2332. o->type->Generic.specialized = nullptr;
  2333. error(o->expr, "Polymorphic array length cannot have a specialization");
  2334. }
  2335. return 0;
  2336. }
  2337. }
  2338. if (o->mode == Addressing_Type) {
  2339. if (is_type_enum(o->type)) {
  2340. return -1;
  2341. }
  2342. }
  2343. if (o->mode != Addressing_Constant) {
  2344. if (o->mode != Addressing_Invalid) {
  2345. o->mode = Addressing_Invalid;
  2346. error(e, "Array count must be a constant");
  2347. }
  2348. return 0;
  2349. }
  2350. Type *type = core_type(o->type);
  2351. if (is_type_untyped(type) || is_type_integer(type)) {
  2352. if (o->value.kind == ExactValue_Integer) {
  2353. BigInt count = o->value.value_integer;
  2354. if (o->value.value_integer.neg) {
  2355. gbAllocator a = heap_allocator();
  2356. String str = big_int_to_string(a, &count);
  2357. error(e, "Invalid negative array count, %.*s", LIT(str));
  2358. gb_free(a, str.text);
  2359. return 0;
  2360. }
  2361. switch (count.len) {
  2362. case 0: return 0;
  2363. case 1: return count.d.word;
  2364. }
  2365. gbAllocator a = heap_allocator();
  2366. String str = big_int_to_string(a, &count);
  2367. error(e, "Array count too large, %.*s", LIT(str));
  2368. gb_free(a, str.text);
  2369. return 0;
  2370. }
  2371. }
  2372. error(e, "Array count must be an integer");
  2373. return 0;
  2374. }
  2375. Type *make_optional_ok_type(Type *value, bool typed) {
  2376. // LEAK TODO(bill): probably don't reallocate everything here and reuse the same one for the same type if possible
  2377. gbAllocator a = heap_allocator();
  2378. Type *t = alloc_type_tuple();
  2379. array_init(&t->Tuple.variables, a, 0, 2);
  2380. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, value, false, 0));
  2381. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1));
  2382. return t;
  2383. }
  2384. void init_map_entry_type(Type *type) {
  2385. GB_ASSERT(type->kind == Type_Map);
  2386. if (type->Map.entry_type != nullptr) return;
  2387. // NOTE(bill): The preload types may have not been set yet
  2388. GB_ASSERT(t_map_key != nullptr);
  2389. gbAllocator a = heap_allocator();
  2390. Type *entry_type = alloc_type_struct();
  2391. /*
  2392. struct {
  2393. hash: __MapKey;
  2394. next: int;
  2395. key: Key;
  2396. value: Value;
  2397. }
  2398. */
  2399. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  2400. Scope *s = create_scope(builtin_pkg->scope, a);
  2401. auto fields = array_make<Entity *>(a, 0, 3);
  2402. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_key, false, 0, EntityState_Resolved));
  2403. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, EntityState_Resolved));
  2404. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, 2, EntityState_Resolved));
  2405. entry_type->Struct.fields = fields;
  2406. // type_set_offsets(a, entry_type);
  2407. type->Map.entry_type = entry_type;
  2408. }
  2409. void init_map_internal_types(Type *type) {
  2410. GB_ASSERT(type->kind == Type_Map);
  2411. init_map_entry_type(type);
  2412. if (type->Map.internal_type != nullptr) return;
  2413. if (type->Map.generated_struct_type != nullptr) return;
  2414. Type *key = type->Map.key;
  2415. Type *value = type->Map.value;
  2416. GB_ASSERT(key != nullptr);
  2417. GB_ASSERT(value != nullptr);
  2418. Type *generated_struct_type = alloc_type_struct();
  2419. /*
  2420. struct {
  2421. hashes: []int;
  2422. entries: [dynamic]EntryType;
  2423. }
  2424. */
  2425. gbAllocator a = heap_allocator();
  2426. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  2427. Scope *s = create_scope(builtin_pkg->scope, a);
  2428. Type *hashes_type = alloc_type_slice(t_int);
  2429. Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
  2430. auto fields = array_make<Entity *>(a, 0, 2);
  2431. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved));
  2432. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved));
  2433. generated_struct_type->Struct.fields = fields;
  2434. type_set_offsets(generated_struct_type);
  2435. type->Map.generated_struct_type = generated_struct_type;
  2436. type->Map.internal_type = generated_struct_type;
  2437. type->Map.lookup_result_type = make_optional_ok_type(value);
  2438. }
  2439. void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  2440. GB_ASSERT(type->kind == Type_Map);
  2441. ast_node(mt, MapType, node);
  2442. Type *key = check_type(ctx, mt->key);
  2443. Type *value = check_type(ctx, mt->value);
  2444. if (!is_type_valid_for_keys(key)) {
  2445. if (is_type_boolean(key)) {
  2446. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  2447. } else {
  2448. gbString str = type_to_string(key);
  2449. error(node, "Invalid type of a key for a map, got '%s'", str);
  2450. gb_string_free(str);
  2451. }
  2452. }
  2453. type->Map.key = key;
  2454. type->Map.value = value;
  2455. if (is_type_string(key)) {
  2456. add_package_dependency(ctx, "runtime", "default_hash_string");
  2457. } else {
  2458. add_package_dependency(ctx, "runtime", "default_hash_ptr");
  2459. }
  2460. init_core_map_type(ctx->checker);
  2461. init_map_internal_types(type);
  2462. // error(node, "'map' types are not yet implemented");
  2463. }
  2464. Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type) {
  2465. Type *bt_elem = base_type(elem);
  2466. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2467. gbString str = type_to_string(elem);
  2468. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2469. gb_string_free(str);
  2470. return alloc_type_array(elem, count, generic_type);
  2471. }
  2472. Type *soa_struct = nullptr;
  2473. Scope *scope = nullptr;
  2474. if (is_type_array(elem)) {
  2475. Type *old_array = base_type(elem);
  2476. soa_struct = alloc_type_struct();
  2477. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
  2478. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
  2479. soa_struct->Struct.node = array_typ_expr;
  2480. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2481. soa_struct->Struct.soa_elem = elem;
  2482. soa_struct->Struct.soa_count = count;
  2483. scope = create_scope(ctx->scope, ctx->allocator);
  2484. soa_struct->Struct.scope = scope;
  2485. String params_xyzw[4] = {
  2486. str_lit("x"),
  2487. str_lit("y"),
  2488. str_lit("z"),
  2489. str_lit("w")
  2490. };
  2491. for (i64 i = 0; i < old_array->Array.count; i++) {
  2492. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  2493. Token token = {};
  2494. token.string = params_xyzw[i];
  2495. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2496. soa_struct->Struct.fields[i] = new_field;
  2497. add_entity(ctx->checker, scope, nullptr, new_field);
  2498. add_entity_use(ctx, nullptr, new_field);
  2499. }
  2500. } else {
  2501. GB_ASSERT(is_type_struct(elem));
  2502. Type *old_struct = base_type(elem);
  2503. soa_struct = alloc_type_struct();
  2504. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  2505. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count);
  2506. soa_struct->Struct.node = array_typ_expr;
  2507. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2508. soa_struct->Struct.soa_elem = elem;
  2509. soa_struct->Struct.soa_count = count;
  2510. scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
  2511. soa_struct->Struct.scope = scope;
  2512. for_array(i, old_struct->Struct.fields) {
  2513. Entity *old_field = old_struct->Struct.fields[i];
  2514. if (old_field->kind == Entity_Variable) {
  2515. Type *array_type = alloc_type_array(old_field->type, count);
  2516. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2517. soa_struct->Struct.fields[i] = new_field;
  2518. add_entity(ctx->checker, scope, nullptr, new_field);
  2519. add_entity_use(ctx, nullptr, new_field);
  2520. } else {
  2521. soa_struct->Struct.fields[i] = old_field;
  2522. }
  2523. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2524. }
  2525. }
  2526. Token token = {};
  2527. token.string = str_lit("Base_Type");
  2528. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2529. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2530. add_type_info_type(ctx, soa_struct);
  2531. return soa_struct;
  2532. }
  2533. Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2534. Type *bt_elem = base_type(elem);
  2535. bool is_polymorphic = is_type_polymorphic(elem);
  2536. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2537. GB_ASSERT(elem_expr != nullptr);
  2538. gbString str = type_to_string(elem);
  2539. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2540. gb_string_free(str);
  2541. return alloc_type_slice(elem);
  2542. }
  2543. Type *soa_struct = nullptr;
  2544. Scope *scope = nullptr;
  2545. isize field_count = 0;
  2546. if (is_polymorphic) {
  2547. field_count = 0;
  2548. soa_struct = alloc_type_struct();
  2549. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2550. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+1);
  2551. soa_struct->Struct.node = array_typ_expr;
  2552. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2553. soa_struct->Struct.soa_elem = elem;
  2554. soa_struct->Struct.soa_count = 0;
  2555. soa_struct->Struct.is_polymorphic = true;
  2556. scope = create_scope(ctx->scope, ctx->allocator);
  2557. soa_struct->Struct.scope = scope;
  2558. } else if (is_type_array(elem)) {
  2559. Type *old_array = base_type(elem);
  2560. field_count = old_array->Array.count;
  2561. soa_struct = alloc_type_struct();
  2562. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2563. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+1);
  2564. soa_struct->Struct.node = array_typ_expr;
  2565. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2566. soa_struct->Struct.soa_elem = elem;
  2567. soa_struct->Struct.soa_count = 0;
  2568. scope = create_scope(ctx->scope, ctx->allocator);
  2569. soa_struct->Struct.scope = scope;
  2570. String params_xyzw[4] = {
  2571. str_lit("x"),
  2572. str_lit("y"),
  2573. str_lit("z"),
  2574. str_lit("w")
  2575. };
  2576. for (i64 i = 0; i < field_count; i++) {
  2577. Type *array_type = alloc_type_pointer(old_array->Array.elem);
  2578. Token token = {};
  2579. token.string = params_xyzw[i];
  2580. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2581. new_field->flags |= EntityFlag_SoaPtrField;
  2582. soa_struct->Struct.fields[i] = new_field;
  2583. add_entity(ctx->checker, scope, nullptr, new_field);
  2584. add_entity_use(ctx, nullptr, new_field);
  2585. }
  2586. } else {
  2587. GB_ASSERT(is_type_struct(elem));
  2588. Type *old_struct = base_type(elem);
  2589. field_count = old_struct->Struct.fields.count;
  2590. soa_struct = alloc_type_struct();
  2591. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2592. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count+1);
  2593. soa_struct->Struct.node = array_typ_expr;
  2594. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2595. soa_struct->Struct.soa_elem = elem;
  2596. soa_struct->Struct.soa_count = 0;
  2597. scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
  2598. soa_struct->Struct.scope = scope;
  2599. for_array(i, old_struct->Struct.fields) {
  2600. Entity *old_field = old_struct->Struct.fields[i];
  2601. if (old_field->kind == Entity_Variable) {
  2602. Type *array_type = alloc_type_pointer(old_field->type);
  2603. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2604. new_field->flags |= EntityFlag_SoaPtrField;
  2605. soa_struct->Struct.fields[i] = new_field;
  2606. add_entity(ctx->checker, scope, nullptr, new_field);
  2607. add_entity_use(ctx, nullptr, new_field);
  2608. } else {
  2609. soa_struct->Struct.fields[i] = old_field;
  2610. }
  2611. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2612. }
  2613. }
  2614. Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2615. soa_struct->Struct.fields[field_count] = len_field;
  2616. add_entity(ctx->checker, scope, nullptr, len_field);
  2617. add_entity_use(ctx, nullptr, len_field);
  2618. Token token = {};
  2619. token.string = str_lit("Base_Type");
  2620. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2621. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2622. add_type_info_type(ctx, soa_struct);
  2623. return soa_struct;
  2624. }
  2625. Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2626. Type *bt_elem = base_type(elem);
  2627. bool is_polymorphic = is_type_polymorphic(elem);
  2628. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2629. GB_ASSERT(elem_expr != nullptr);
  2630. gbString str = type_to_string(elem);
  2631. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2632. gb_string_free(str);
  2633. return alloc_type_dynamic_array(elem);
  2634. }
  2635. Type *soa_struct = nullptr;
  2636. Scope *scope = nullptr;
  2637. isize field_count = 0;
  2638. if (is_polymorphic) {
  2639. field_count = 0;
  2640. soa_struct = alloc_type_struct();
  2641. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2642. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+3);
  2643. soa_struct->Struct.node = array_typ_expr;
  2644. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2645. soa_struct->Struct.soa_elem = elem;
  2646. soa_struct->Struct.soa_count = 0;
  2647. soa_struct->Struct.is_polymorphic = true;
  2648. scope = create_scope(ctx->scope, ctx->allocator);
  2649. soa_struct->Struct.scope = scope;
  2650. } else if (is_type_array(elem)) {
  2651. Type *old_array = base_type(elem);
  2652. field_count = old_array->Array.count;
  2653. soa_struct = alloc_type_struct();
  2654. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2655. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+3);
  2656. soa_struct->Struct.node = array_typ_expr;
  2657. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2658. soa_struct->Struct.soa_elem = elem;
  2659. soa_struct->Struct.soa_count = 0;
  2660. scope = create_scope(ctx->scope, ctx->allocator);
  2661. soa_struct->Struct.scope = scope;
  2662. String params_xyzw[4] = {
  2663. str_lit("x"),
  2664. str_lit("y"),
  2665. str_lit("z"),
  2666. str_lit("w")
  2667. };
  2668. for (i64 i = 0; i < field_count; i++) {
  2669. Type *array_type = alloc_type_pointer(old_array->Array.elem);
  2670. Token token = {};
  2671. token.string = params_xyzw[i];
  2672. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2673. new_field->flags |= EntityFlag_SoaPtrField;
  2674. soa_struct->Struct.fields[i] = new_field;
  2675. add_entity(ctx->checker, scope, nullptr, new_field);
  2676. add_entity_use(ctx, nullptr, new_field);
  2677. }
  2678. } else {
  2679. GB_ASSERT(is_type_struct(elem));
  2680. Type *old_struct = base_type(elem);
  2681. field_count = old_struct->Struct.fields.count;
  2682. soa_struct = alloc_type_struct();
  2683. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2684. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count+3);
  2685. soa_struct->Struct.node = array_typ_expr;
  2686. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2687. soa_struct->Struct.soa_elem = elem;
  2688. soa_struct->Struct.soa_count = 0;
  2689. scope = create_scope(old_struct->Struct.scope->parent, ctx->allocator);
  2690. soa_struct->Struct.scope = scope;
  2691. for_array(i, old_struct->Struct.fields) {
  2692. Entity *old_field = old_struct->Struct.fields[i];
  2693. if (old_field->kind == Entity_Variable) {
  2694. Type *array_type = alloc_type_pointer(old_field->type);
  2695. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2696. new_field->flags |= EntityFlag_SoaPtrField;
  2697. soa_struct->Struct.fields[i] = new_field;
  2698. add_entity(ctx->checker, scope, nullptr, new_field);
  2699. add_entity_use(ctx, nullptr, new_field);
  2700. } else {
  2701. soa_struct->Struct.fields[i] = old_field;
  2702. }
  2703. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2704. }
  2705. }
  2706. Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2707. soa_struct->Struct.fields[field_count+0] = len_field;
  2708. add_entity(ctx->checker, scope, nullptr, len_field);
  2709. add_entity_use(ctx, nullptr, len_field);
  2710. Entity *cap_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2711. soa_struct->Struct.fields[field_count+1] = cap_field;
  2712. add_entity(ctx->checker, scope, nullptr, cap_field);
  2713. add_entity_use(ctx, nullptr, cap_field);
  2714. Token token = {};
  2715. token.string = str_lit("allocator");
  2716. Entity *allocator_field = alloc_entity_field(scope, token, t_allocator, false, cast(i32)field_count);
  2717. soa_struct->Struct.fields[field_count+2] = allocator_field;
  2718. add_entity(ctx->checker, scope, nullptr, allocator_field);
  2719. add_entity_use(ctx, nullptr, allocator_field);
  2720. token.string = str_lit("Base_Type");
  2721. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2722. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2723. add_type_info_type(ctx, soa_struct);
  2724. return soa_struct;
  2725. }
  2726. bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2727. GB_ASSERT_NOT_NULL(type);
  2728. if (e == nullptr) {
  2729. *type = t_invalid;
  2730. return true;
  2731. }
  2732. switch (e->kind) {
  2733. case_ast_node(i, Ident, e);
  2734. Operand o = {};
  2735. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  2736. gbString err_str = nullptr;
  2737. defer (gb_string_free(err_str));
  2738. switch (o.mode) {
  2739. case Addressing_Invalid:
  2740. break;
  2741. case Addressing_Type: {
  2742. *type = o.type;
  2743. if (!ctx->in_polymorphic_specialization) {
  2744. Type *t = base_type(o.type);
  2745. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  2746. err_str = expr_to_string(e);
  2747. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  2748. return true;
  2749. }
  2750. }
  2751. // if (ctx->type_level == 0 && entity->state == EntityState_InProgress) {
  2752. // error(entity->token, "Illegal declaration cycle of `%.*s`", LIT(entity->token.string));
  2753. // for_array(j, *ctx->type_path) {
  2754. // Entity *k = (*ctx->type_path)[j];
  2755. // error(k->token, "\t%.*s refers to", LIT(k->token.string));
  2756. // }
  2757. // error(entity->token, "\t%.*s", LIT(entity->token.string));
  2758. // *type = t_invalid;
  2759. // }
  2760. return true;
  2761. }
  2762. case Addressing_NoValue:
  2763. err_str = expr_to_string(e);
  2764. error(e, "'%s' used as a type", err_str);
  2765. break;
  2766. default:
  2767. err_str = expr_to_string(e);
  2768. error(e, "'%s' used as a type when not a type", err_str);
  2769. break;
  2770. }
  2771. case_end;
  2772. case_ast_node(ht, HelperType, e);
  2773. return check_type_internal(ctx, ht->type, type, named_type);
  2774. case_end;
  2775. case_ast_node(dt, DistinctType, e);
  2776. error(e, "Invalid use of a distinct type");
  2777. // NOTE(bill): Treat it as a HelperType to remove errors
  2778. return check_type_internal(ctx, dt->type, type, named_type);
  2779. case_end;
  2780. case_ast_node(tt, TypeidType, e);
  2781. e->tav.mode = Addressing_Type;
  2782. e->tav.type = t_typeid;
  2783. *type = t_typeid;
  2784. set_base_type(named_type, *type);
  2785. return true;
  2786. case_end;
  2787. case_ast_node(pt, PolyType, e);
  2788. Ast *ident = pt->type;
  2789. if (ident->kind != Ast_Ident) {
  2790. error(ident, "Expected an identifier after the $");
  2791. *type = t_invalid;
  2792. return false;
  2793. }
  2794. Token token = ident->Ident.token;
  2795. Type *specific = nullptr;
  2796. if (pt->specialization != nullptr) {
  2797. CheckerContext c = *ctx;
  2798. c.in_polymorphic_specialization = true;
  2799. Ast *s = pt->specialization;
  2800. specific = check_type(&c, s);
  2801. }
  2802. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2803. if (ctx->allow_polymorphic_types) {
  2804. Scope *ps = ctx->polymorphic_scope;
  2805. Scope *s = ctx->scope;
  2806. Scope *entity_scope = s;
  2807. if (ps != nullptr && ps != s) {
  2808. // TODO(bill): Is this check needed?
  2809. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2810. entity_scope = ps;
  2811. }
  2812. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2813. t->Generic.entity = e;
  2814. e->TypeName.is_type_alias = true;
  2815. e->state = EntityState_Resolved;
  2816. add_entity(ctx->checker, ps, ident, e);
  2817. add_entity(ctx->checker, s, ident, e);
  2818. } else {
  2819. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2820. *type = t_invalid;
  2821. return false;
  2822. }
  2823. *type = t;
  2824. set_base_type(named_type, *type);
  2825. return true;
  2826. case_end;
  2827. case_ast_node(se, SelectorExpr, e);
  2828. Operand o = {};
  2829. check_selector(ctx, &o, e, nullptr);
  2830. gbString err_str;
  2831. switch (o.mode) {
  2832. case Addressing_Invalid:
  2833. break;
  2834. case Addressing_Type:
  2835. GB_ASSERT(o.type != nullptr);
  2836. *type = o.type;
  2837. return true;
  2838. case Addressing_NoValue:
  2839. err_str = expr_to_string(e);
  2840. error(e, "'%s' used as a type", err_str);
  2841. gb_string_free(err_str);
  2842. break;
  2843. default:
  2844. err_str = expr_to_string(e);
  2845. error(e, "'%s' is not a type", err_str);
  2846. gb_string_free(err_str);
  2847. break;
  2848. }
  2849. case_end;
  2850. case_ast_node(pe, ParenExpr, e);
  2851. *type = check_type_expr(ctx, pe->expr, named_type);
  2852. set_base_type(named_type, *type);
  2853. return true;
  2854. case_end;
  2855. case_ast_node(ue, UnaryExpr, e);
  2856. switch (ue->op.kind) {
  2857. case Token_Pointer:
  2858. *type = alloc_type_pointer(check_type(ctx, ue->expr));
  2859. set_base_type(named_type, *type);
  2860. return true;
  2861. }
  2862. case_end;
  2863. case_ast_node(pt, PointerType, e);
  2864. *type = alloc_type_pointer(check_type(ctx, pt->type));
  2865. set_base_type(named_type, *type);
  2866. return true;
  2867. case_end;
  2868. case_ast_node(rt, RelativeType, e);
  2869. GB_ASSERT(rt->tag->kind == Ast_CallExpr);
  2870. ast_node(ce, CallExpr, rt->tag);
  2871. Type *base_integer = nullptr;
  2872. if (ce->args.count != 1) {
  2873. error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count);
  2874. } else {
  2875. base_integer = check_type(ctx, ce->args[0]);
  2876. if (!is_type_integer(base_integer)) {
  2877. error(rt->type, "#relative base types must be an integer");
  2878. base_integer = nullptr;
  2879. } else if (type_size_of(base_integer) > 64) {
  2880. error(rt->type, "#relative base integer types be less than or equal to 64-bits");
  2881. base_integer = nullptr;
  2882. }
  2883. }
  2884. Type *relative_type = nullptr;
  2885. Type *base_type = check_type(ctx, rt->type);
  2886. if (!is_type_pointer(base_type) && !is_type_slice(base_type)) {
  2887. error(rt->type, "#relative types can only be a pointer or slice");
  2888. relative_type = base_type;
  2889. } else if (base_integer == nullptr) {
  2890. relative_type = base_type;
  2891. } else {
  2892. if (is_type_pointer(base_type)) {
  2893. relative_type = alloc_type_relative_pointer(base_type, base_integer);
  2894. } else if (is_type_slice(base_type)) {
  2895. relative_type = alloc_type_relative_slice(base_type, base_integer);
  2896. }
  2897. }
  2898. GB_ASSERT(relative_type != nullptr);
  2899. *type = relative_type;
  2900. set_base_type(named_type, *type);
  2901. return true;
  2902. case_end;
  2903. case_ast_node(ot, OpaqueType, e);
  2904. Type *elem = strip_opaque_type(check_type_expr(ctx, ot->type, nullptr));
  2905. *type = alloc_type_opaque(elem);
  2906. set_base_type(named_type, *type);
  2907. return true;
  2908. case_end;
  2909. case_ast_node(at, ArrayType, e);
  2910. if (at->count != nullptr) {
  2911. Operand o = {};
  2912. i64 count = check_array_count(ctx, &o, at->count);
  2913. Type *generic_type = nullptr;
  2914. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2915. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2916. generic_type = o.type;
  2917. } else if (o.mode == Addressing_Type && is_type_enum(o.type)) {
  2918. Type *index = o.type;
  2919. Type *bt = base_type(index);
  2920. GB_ASSERT(bt->kind == Type_Enum);
  2921. Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, Token_Invalid);
  2922. bool is_partial = false;
  2923. if (at->tag != nullptr) {
  2924. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2925. String name = at->tag->BasicDirective.name;
  2926. if (name == "partial") {
  2927. is_partial = true;
  2928. } else {
  2929. error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
  2930. }
  2931. }
  2932. if (!is_partial && t->EnumeratedArray.count > bt->Enum.fields.count) {
  2933. error(e, "Non-contiguous enumeration used as an index in an enumerated array");
  2934. long long ea_count = cast(long long)t->EnumeratedArray.count;
  2935. long long enum_count = cast(long long)t->Enum.fields.count;
  2936. error_line("\tenumerated array length: %lld\n", ea_count);
  2937. error_line("\tenum field count: %lld\n", enum_count);
  2938. error_line("\tSuggestion: prepend #partial to the enumerated array to allow for non-named elements\n");
  2939. if (2*enum_count < ea_count) {
  2940. 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");
  2941. error_line("\t this warning will be removed if #partial is applied\n");
  2942. }
  2943. }
  2944. *type = t;
  2945. goto array_end;
  2946. }
  2947. if (count < 0) {
  2948. error(at->count, "? can only be used in conjuction with compound literals");
  2949. count = 0;
  2950. }
  2951. if (at->tag != nullptr) {
  2952. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2953. String name = at->tag->BasicDirective.name;
  2954. if (name == "soa") {
  2955. *type = make_soa_struct_fixed(ctx, e, at->elem, elem, count, generic_type);
  2956. } else if (name == "simd") {
  2957. if (!is_type_valid_vector_elem(elem)) {
  2958. gbString str = type_to_string(elem);
  2959. error(at->elem, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  2960. gb_string_free(str);
  2961. *type = alloc_type_array(elem, count, generic_type);
  2962. goto array_end;
  2963. }
  2964. *type = alloc_type_simd_vector(count, elem);
  2965. } else {
  2966. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2967. *type = alloc_type_array(elem, count, generic_type);
  2968. }
  2969. } else {
  2970. *type = alloc_type_array(elem, count, generic_type);
  2971. }
  2972. } else {
  2973. Type *elem = check_type(ctx, at->elem);
  2974. if (at->tag != nullptr) {
  2975. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2976. String name = at->tag->BasicDirective.name;
  2977. if (name == "soa") {
  2978. *type = make_soa_struct_slice(ctx, e, at->elem, elem);
  2979. } else {
  2980. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2981. *type = alloc_type_slice(elem);
  2982. }
  2983. } else {
  2984. *type = alloc_type_slice(elem);
  2985. }
  2986. }
  2987. array_end:
  2988. set_base_type(named_type, *type);
  2989. return true;
  2990. case_end;
  2991. case_ast_node(dat, DynamicArrayType, e);
  2992. Type *elem = check_type(ctx, dat->elem);
  2993. if (dat->tag != nullptr) {
  2994. GB_ASSERT(dat->tag->kind == Ast_BasicDirective);
  2995. String name = dat->tag->BasicDirective.name;
  2996. if (name == "soa") {
  2997. *type = make_soa_struct_dynamic_array(ctx, e, dat->elem, elem);
  2998. } else {
  2999. error(dat->tag, "Invalid tag applied to dynamic array, got #%.*s", LIT(name));
  3000. *type = alloc_type_dynamic_array(elem);
  3001. }
  3002. } else {
  3003. *type = alloc_type_dynamic_array(elem);
  3004. }
  3005. set_base_type(named_type, *type);
  3006. return true;
  3007. case_end;
  3008. case_ast_node(st, StructType, e);
  3009. CheckerContext c = *ctx;
  3010. c.in_polymorphic_specialization = false;
  3011. c.type_level += 1;
  3012. *type = alloc_type_struct();
  3013. set_base_type(named_type, *type);
  3014. check_open_scope(&c, e);
  3015. check_struct_type(&c, *type, e, nullptr, named_type);
  3016. check_close_scope(&c);
  3017. (*type)->Struct.node = e;
  3018. return true;
  3019. case_end;
  3020. case_ast_node(ut, UnionType, e);
  3021. CheckerContext c = *ctx;
  3022. c.in_polymorphic_specialization = false;
  3023. c.type_level += 1;
  3024. *type = alloc_type_union();
  3025. set_base_type(named_type, *type);
  3026. check_open_scope(&c, e);
  3027. check_union_type(&c, *type, e, nullptr, named_type);
  3028. check_close_scope(&c);
  3029. (*type)->Union.node = e;
  3030. return true;
  3031. case_end;
  3032. case_ast_node(et, EnumType, e);
  3033. bool ips = ctx->in_polymorphic_specialization;
  3034. defer (ctx->in_polymorphic_specialization = ips);
  3035. ctx->in_polymorphic_specialization = false;
  3036. ctx->in_enum_type = true;
  3037. *type = alloc_type_enum();
  3038. set_base_type(named_type, *type);
  3039. check_open_scope(ctx, e);
  3040. check_enum_type(ctx, *type, named_type, e);
  3041. check_close_scope(ctx);
  3042. (*type)->Enum.node = e;
  3043. ctx->in_enum_type = false;
  3044. return true;
  3045. case_end;
  3046. case_ast_node(et, BitFieldType, e);
  3047. *type = alloc_type_bit_field();
  3048. set_base_type(named_type, *type);
  3049. check_open_scope(ctx, e);
  3050. check_bit_field_type(ctx, *type, e);
  3051. check_close_scope(ctx);
  3052. return true;
  3053. case_end;
  3054. case_ast_node(bs, BitSetType, e);
  3055. *type = alloc_type_bit_set();
  3056. set_base_type(named_type, *type);
  3057. check_bit_set_type(ctx, *type, named_type, e);
  3058. return true;
  3059. case_end;
  3060. case_ast_node(pt, ProcType, e);
  3061. bool ips = ctx->in_polymorphic_specialization;
  3062. defer (ctx->in_polymorphic_specialization = ips);
  3063. ctx->in_polymorphic_specialization = false;
  3064. *type = alloc_type(Type_Proc);
  3065. set_base_type(named_type, *type);
  3066. check_open_scope(ctx, e);
  3067. check_procedure_type(ctx, *type, e);
  3068. check_close_scope(ctx);
  3069. return true;
  3070. case_end;
  3071. case_ast_node(mt, MapType, e);
  3072. bool ips = ctx->in_polymorphic_specialization;
  3073. defer (ctx->in_polymorphic_specialization = ips);
  3074. ctx->in_polymorphic_specialization = false;
  3075. *type = alloc_type(Type_Map);
  3076. set_base_type(named_type, *type);
  3077. check_map_type(ctx, *type, e);
  3078. return true;
  3079. case_end;
  3080. case_ast_node(ce, CallExpr, e);
  3081. Operand o = {};
  3082. check_expr_or_type(ctx, &o, e);
  3083. if (o.mode == Addressing_Type) {
  3084. *type = o.type;
  3085. set_base_type(named_type, *type);
  3086. return true;
  3087. }
  3088. case_end;
  3089. case_ast_node(te, TernaryExpr, e);
  3090. Operand o = {};
  3091. check_expr_or_type(ctx, &o, e);
  3092. if (o.mode == Addressing_Type) {
  3093. *type = o.type;
  3094. set_base_type(named_type, *type);
  3095. return true;
  3096. }
  3097. case_end;
  3098. case_ast_node(te, TernaryIfExpr, e);
  3099. Operand o = {};
  3100. check_expr_or_type(ctx, &o, e);
  3101. if (o.mode == Addressing_Type) {
  3102. *type = o.type;
  3103. set_base_type(named_type, *type);
  3104. return true;
  3105. }
  3106. case_end;
  3107. case_ast_node(te, TernaryWhenExpr, e);
  3108. Operand o = {};
  3109. check_expr_or_type(ctx, &o, e);
  3110. if (o.mode == Addressing_Type) {
  3111. *type = o.type;
  3112. set_base_type(named_type, *type);
  3113. return true;
  3114. }
  3115. case_end;
  3116. }
  3117. *type = t_invalid;
  3118. return false;
  3119. }
  3120. Type *check_type(CheckerContext *ctx, Ast *e) {
  3121. CheckerContext c = *ctx;
  3122. c.type_path = new_checker_type_path();
  3123. defer (destroy_checker_type_path(c.type_path));
  3124. return check_type_expr(&c, e, nullptr);
  3125. }
  3126. Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  3127. Type *type = nullptr;
  3128. bool ok = check_type_internal(ctx, e, &type, named_type);
  3129. if (!ok) {
  3130. gbString err_str = expr_to_string(e);
  3131. error(e, "'%s' is not a type", err_str);
  3132. gb_string_free(err_str);
  3133. type = t_invalid;
  3134. }
  3135. if (type == nullptr) {
  3136. type = t_invalid;
  3137. }
  3138. if (type->kind == Type_Named &&
  3139. type->Named.base == nullptr) {
  3140. // IMPORTANT TODO(bill): Is this a serious error?!
  3141. #if 0
  3142. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  3143. #endif
  3144. type->Named.base = t_invalid;
  3145. }
  3146. if (is_type_polymorphic(type)) {
  3147. type->flags |= TypeFlag_Polymorphic;
  3148. } else if (is_type_polymorphic(type, true)) {
  3149. type->flags |= TypeFlag_PolySpecialized;
  3150. }
  3151. #if 0
  3152. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  3153. gbString str = type_to_string(type);
  3154. error(e, "Invalid use of a polymorphic type '%s'", str);
  3155. gb_string_free(str);
  3156. type = t_invalid;
  3157. }
  3158. #endif
  3159. if (is_type_typed(type)) {
  3160. add_type_and_value(&ctx->checker->info, e, Addressing_Type, type, empty_exact_value);
  3161. } else {
  3162. gbString name = type_to_string(type);
  3163. error(e, "Invalid type definition of %s", name);
  3164. gb_string_free(name);
  3165. type = t_invalid;
  3166. }
  3167. set_base_type(named_type, type);
  3168. return type;
  3169. }