check_type.cpp 114 KB

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