check_type.cpp 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680
  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 bool is_valid_bit_field_backing_type(Type *type) {
  836. if (type == nullptr) {
  837. return false;
  838. }
  839. type = base_type(type);
  840. if (is_type_untyped(type)) {
  841. return false;
  842. }
  843. if (is_type_integer(type)) {
  844. return true;
  845. }
  846. if (type->kind == Type_Array) {
  847. return is_type_integer(type->Array.elem);
  848. }
  849. return false;
  850. }
  851. gb_internal void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Type *named_type, Ast *node) {
  852. ast_node(bf, BitFieldType, node);
  853. GB_ASSERT(is_type_bit_field(bit_field_type));
  854. Type *backing_type = check_type(ctx, bf->backing_type);
  855. bit_field_type->BitField.backing_type = backing_type ? backing_type : t_u8;
  856. bit_field_type->BitField.scope = ctx->scope;
  857. if (backing_type == nullptr) {
  858. error(bf->backing_type, "Backing type for a bit_field must be an integer or an array of an integer");
  859. return;
  860. }
  861. if (!is_valid_bit_field_backing_type(backing_type)) {
  862. error(bf->backing_type, "Backing type for a bit_field must be an integer or an array of an integer");
  863. return;
  864. }
  865. auto fields = array_make<Entity *>(permanent_allocator(), 0, bf->fields.count);
  866. auto bit_sizes = array_make<u8> (permanent_allocator(), 0, bf->fields.count);
  867. auto tags = array_make<String> (permanent_allocator(), 0, bf->fields.count);
  868. u64 maximum_bit_size = 8 * type_size_of(backing_type);
  869. u64 total_bit_size = 0;
  870. for_array(i, bf->fields) {
  871. i32 field_src_index = cast(i32)i;
  872. Ast *field = bf->fields[i];
  873. if (field->kind != Ast_BitFieldField) {
  874. error(field, "Invalid AST for a bit_field");
  875. continue;
  876. }
  877. ast_node(f, BitFieldField, field);
  878. if (f->name == nullptr || f->name->kind != Ast_Ident) {
  879. error(field, "A bit_field's field name must be an identifier");
  880. continue;
  881. }
  882. CommentGroup *docs = f->docs;
  883. CommentGroup *comment = f->comment;
  884. String name = f->name->Ident.token.string;
  885. if (f->type == nullptr) {
  886. error(field, "A bit_field's field must have a type");
  887. continue;
  888. }
  889. Type *type = check_type(ctx, f->type);
  890. if (type_size_of(type) > 8) {
  891. error(f->type, "The type of a bit_field's field must be <= 8 bytes, got %lld", cast(long long)type_size_of(type));
  892. }
  893. if (is_type_untyped(type)) {
  894. gbString s = type_to_string(type);
  895. error(f->type, "The type of a bit_field's field must be a typed integer, enum, or boolean, got %s", s);
  896. gb_string_free(s);
  897. } else if (!(is_type_integer(type) || is_type_enum(type) || is_type_boolean(type))) {
  898. gbString s = type_to_string(type);
  899. error(f->type, "The type of a bit_field's field must be an integer, enum, or boolean, got %s", s);
  900. gb_string_free(s);
  901. }
  902. if (f->bit_size == nullptr) {
  903. error(field, "A bit_field's field must have a specified bit size");
  904. continue;
  905. }
  906. Operand o = {};
  907. check_expr(ctx, &o, f->bit_size);
  908. if (o.mode != Addressing_Constant) {
  909. error(f->bit_size, "A bit_field's specified bit size must be a constant");
  910. o.mode = Addressing_Invalid;
  911. }
  912. if (o.value.kind == ExactValue_Float) {
  913. o.value = exact_value_to_integer(o.value);
  914. }
  915. if (f->bit_size->kind == Ast_BinaryExpr && f->bit_size->BinaryExpr.op.kind == Token_Or) {
  916. gbString s = expr_to_string(f->bit_size);
  917. error(f->bit_size, "Wrap the expression in parentheses, e.g. (%s)", s);
  918. gb_string_free(s);
  919. }
  920. ExactValue bit_size = o.value;
  921. if (bit_size.kind != ExactValue_Integer) {
  922. gbString s = expr_to_string(f->bit_size);
  923. error(f->bit_size, "Expected an integer constant value for the specified bit size, got %s", s);
  924. gb_string_free(s);
  925. }
  926. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  927. error(f->name, "'%.*s' is already declared in this bit_field", LIT(name));
  928. } else {
  929. i64 bit_size_i64 = exact_value_to_i64(bit_size);
  930. u8 bit_size_u8 = 0;
  931. if (bit_size_i64 <= 0) {
  932. error(f->bit_size, "A bit_field's specified bit size cannot be <= 0, got %lld", cast(long long)bit_size_i64);
  933. bit_size_i64 = 1;
  934. }
  935. if (bit_size_i64 > 64) {
  936. error(f->bit_size, "A bit_field's specified bit size cannot exceed 64 bits, got %lld", cast(long long)bit_size_i64);
  937. bit_size_i64 = 64;
  938. }
  939. i64 sz = 8*type_size_of(type);
  940. if (bit_size_i64 > sz) {
  941. 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);
  942. bit_size_i64 = sz;
  943. }
  944. bit_size_u8 = cast(u8)bit_size_i64;
  945. Entity *e = alloc_entity_field(ctx->scope, f->name->Ident.token, type, false, field_src_index);
  946. e->Variable.docs = docs;
  947. e->Variable.comment = comment;
  948. e->Variable.bit_field_bit_size = bit_size_u8;
  949. e->flags |= EntityFlag_BitFieldField;
  950. add_entity(ctx, ctx->scope, nullptr, e);
  951. array_add(&fields, e);
  952. array_add(&bit_sizes, bit_size_u8);
  953. String tag = f->tag.string;
  954. if (tag.len != 0 && !unquote_string(permanent_allocator(), &tag, 0, tag.text[0] == '`')) {
  955. error(f->tag, "Invalid string literal");
  956. tag = {};
  957. }
  958. array_add(&tags, tag);
  959. add_entity_use(ctx, field, e);
  960. total_bit_size += bit_size_u8;
  961. }
  962. }
  963. GB_ASSERT(fields.count <= bf->fields.count);
  964. auto bit_offsets = slice_make<i64>(permanent_allocator(), fields.count);
  965. i64 curr_offset = 0;
  966. for_array(i, bit_sizes) {
  967. bit_offsets[i] = curr_offset;
  968. curr_offset += cast(i64)bit_sizes[i];
  969. }
  970. if (total_bit_size > maximum_bit_size) {
  971. gbString s = type_to_string(backing_type);
  972. 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",
  973. cast(unsigned long long)total_bit_size,
  974. s,
  975. cast(unsigned long long)maximum_bit_size);
  976. gb_string_free(s);
  977. }
  978. enum EndianKind {
  979. Endian_Unknown,
  980. Endian_Native,
  981. Endian_Little,
  982. Endian_Big,
  983. };
  984. auto const &determine_endian_kind = [](Type *type) -> EndianKind {
  985. if (is_type_boolean(type)) {
  986. // NOTE(bill): it doesn't matter, and when it does,
  987. // that api is absolutely stupid
  988. return Endian_Unknown;
  989. } else if (type_size_of(type) < 2) {
  990. return Endian_Unknown;
  991. } else if (is_type_endian_specific(type)) {
  992. if (is_type_endian_little(type)) {
  993. return Endian_Little;
  994. } else {
  995. return Endian_Big;
  996. }
  997. }
  998. return Endian_Native;
  999. };
  1000. Type *backing_type_elem = core_array_type(backing_type);
  1001. i64 backing_type_elem_size = type_size_of(backing_type_elem);
  1002. EndianKind backing_type_endian_kind = determine_endian_kind(backing_type_elem);
  1003. EndianKind endian_kind = Endian_Unknown;
  1004. for (Entity *f : fields) {
  1005. EndianKind field_kind = determine_endian_kind(f->type);
  1006. i64 field_size = type_size_of(f->type);
  1007. if (field_kind && backing_type_endian_kind != field_kind && field_size > 1 && backing_type_elem_size > 1) {
  1008. 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");
  1009. }
  1010. if (endian_kind == Endian_Unknown) {
  1011. endian_kind = field_kind;
  1012. } else if (field_kind && endian_kind != field_kind && field_size > 1) {
  1013. error(f->token, "All 'bit_field' field types must be of the same endian variety, i.e. all native, all little, or all big");
  1014. }
  1015. }
  1016. if (bit_sizes.count > 0 && is_type_integer(backing_type)) {
  1017. bool all_booleans = is_type_boolean(fields[0]->type);
  1018. bool all_ones = bit_sizes[0] == 1;
  1019. if (all_ones && all_booleans) {
  1020. for_array(i, bit_sizes) {
  1021. all_ones = bit_sizes[i] == 1;
  1022. if (!all_ones) {
  1023. break;
  1024. }
  1025. all_booleans = is_type_boolean(fields[i]->type);
  1026. if (!all_booleans) {
  1027. break;
  1028. }
  1029. }
  1030. if (all_ones && all_booleans) {
  1031. if (ast_file_vet_style(ctx->file)) {
  1032. 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)";
  1033. error(node, msg);
  1034. } else {
  1035. 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";
  1036. warning(node, msg);
  1037. }
  1038. }
  1039. }
  1040. }
  1041. bit_field_type->BitField.fields = slice_from_array(fields);
  1042. bit_field_type->BitField.bit_sizes = slice_from_array(bit_sizes);
  1043. bit_field_type->BitField.bit_offsets = bit_offsets;
  1044. bit_field_type->BitField.tags = tags.data;
  1045. }
  1046. gb_internal bool is_type_valid_bit_set_range(Type *t) {
  1047. if (is_type_integer(t)) {
  1048. return true;
  1049. }
  1050. if (is_type_rune(t)) {
  1051. return true;
  1052. }
  1053. return false;
  1054. }
  1055. gb_internal void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  1056. ast_node(bs, BitSetType, node);
  1057. GB_ASSERT(type->kind == Type_BitSet);
  1058. type->BitSet.node = node;
  1059. /* i64 const DEFAULT_BITS = cast(i64)(8*build_context.word_size); */
  1060. i64 const MAX_BITS = 128;
  1061. Ast *base = unparen_expr(bs->elem);
  1062. if (is_ast_range(base)) {
  1063. ast_node(be, BinaryExpr, base);
  1064. Operand lhs = {};
  1065. Operand rhs = {};
  1066. check_expr(c, &lhs, be->left);
  1067. check_expr(c, &rhs, be->right);
  1068. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  1069. return;
  1070. }
  1071. convert_to_typed(c, &lhs, rhs.type);
  1072. if (lhs.mode == Addressing_Invalid) {
  1073. return;
  1074. }
  1075. convert_to_typed(c, &rhs, lhs.type);
  1076. if (rhs.mode == Addressing_Invalid) {
  1077. return;
  1078. }
  1079. if (!are_types_identical(lhs.type, rhs.type)) {
  1080. if (lhs.type != t_invalid &&
  1081. rhs.type != t_invalid) {
  1082. gbString xt = type_to_string(lhs.type);
  1083. gbString yt = type_to_string(rhs.type);
  1084. gbString expr_str = expr_to_string(bs->elem);
  1085. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  1086. gb_string_free(expr_str);
  1087. gb_string_free(yt);
  1088. gb_string_free(xt);
  1089. }
  1090. return;
  1091. }
  1092. if (!is_type_valid_bit_set_range(lhs.type)) {
  1093. gbString str = type_to_string(lhs.type);
  1094. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  1095. gb_string_free(str);
  1096. return;
  1097. }
  1098. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  1099. error(bs->elem, "Intervals must be constant values");
  1100. return;
  1101. }
  1102. ExactValue iv = exact_value_to_integer(lhs.value);
  1103. ExactValue jv = exact_value_to_integer(rhs.value);
  1104. GB_ASSERT(iv.kind == ExactValue_Integer);
  1105. GB_ASSERT(jv.kind == ExactValue_Integer);
  1106. BigInt i = iv.value_integer;
  1107. BigInt j = jv.value_integer;
  1108. if (big_int_cmp(&i, &j) > 0) {
  1109. gbAllocator a = heap_allocator();
  1110. String si = big_int_to_string(a, &i);
  1111. String sj = big_int_to_string(a, &j);
  1112. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  1113. gb_free(a, si.text);
  1114. gb_free(a, sj.text);
  1115. return;
  1116. }
  1117. Type *t = default_type(lhs.type);
  1118. if (bs->underlying != nullptr) {
  1119. Type *u = check_type(c, bs->underlying);
  1120. if (!is_type_integer(u)) {
  1121. gbString ts = type_to_string(u);
  1122. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1123. gb_string_free(ts);
  1124. return;
  1125. }
  1126. type->BitSet.underlying = u;
  1127. }
  1128. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1129. gbAllocator a = heap_allocator();
  1130. String s = big_int_to_string(a, &i);
  1131. gbString ts = type_to_string(t);
  1132. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1133. gb_string_free(ts);
  1134. gb_free(a, s.text);
  1135. return;
  1136. }
  1137. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1138. gbAllocator a = heap_allocator();
  1139. String s = big_int_to_string(a, &j);
  1140. gbString ts = type_to_string(t);
  1141. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1142. gb_string_free(ts);
  1143. gb_free(a, s.text);
  1144. return;
  1145. }
  1146. i64 lower = big_int_to_i64(&i);
  1147. i64 upper = big_int_to_i64(&j);
  1148. i64 actual_lower = lower;
  1149. i64 bits = MAX_BITS;
  1150. if (type->BitSet.underlying != nullptr) {
  1151. bits = 8*type_size_of(type->BitSet.underlying);
  1152. if (lower > 0) {
  1153. actual_lower = 0;
  1154. } else if (lower < 0) {
  1155. error(bs->elem, "bit_set does not allow a negative lower bound (%lld) when an underlying type is set", lower);
  1156. }
  1157. }
  1158. i64 bits_required = upper-actual_lower;
  1159. switch (be->op.kind) {
  1160. case Token_Ellipsis:
  1161. case Token_RangeFull:
  1162. bits_required += 1;
  1163. break;
  1164. }
  1165. bool is_valid = true;
  1166. switch (be->op.kind) {
  1167. case Token_Ellipsis:
  1168. case Token_RangeFull:
  1169. if (upper - lower >= bits) {
  1170. is_valid = false;
  1171. }
  1172. break;
  1173. case Token_RangeHalf:
  1174. if (upper - lower > bits) {
  1175. is_valid = false;
  1176. }
  1177. upper -= 1;
  1178. break;
  1179. }
  1180. if (!is_valid) {
  1181. if (actual_lower != lower) {
  1182. 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);
  1183. } else {
  1184. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  1185. }
  1186. }
  1187. type->BitSet.elem = t;
  1188. type->BitSet.lower = lower;
  1189. type->BitSet.upper = upper;
  1190. } else {
  1191. Type *elem = check_type_expr(c, bs->elem, nullptr);
  1192. type->BitSet.elem = elem;
  1193. if (!is_type_valid_bit_set_elem(elem)) {
  1194. error(bs->elem, "Expected an enum type for a bit_set");
  1195. } else {
  1196. Type *et = base_type(elem);
  1197. if (et->kind == Type_Enum) {
  1198. if (!is_type_integer(et->Enum.base_type)) {
  1199. error(bs->elem, "Enum type for bit_set must be an integer");
  1200. return;
  1201. }
  1202. i64 lower = I64_MAX;
  1203. i64 upper = I64_MIN;
  1204. for_array(i, et->Enum.fields) {
  1205. Entity *e = et->Enum.fields[i];
  1206. if (e->kind != Entity_Constant) {
  1207. continue;
  1208. }
  1209. ExactValue value = exact_value_to_integer(e->Constant.value);
  1210. GB_ASSERT(value.kind == ExactValue_Integer);
  1211. // NOTE(bill): enum types should be able to store i64 values
  1212. i64 x = big_int_to_i64(&value.value_integer);
  1213. lower = gb_min(lower, x);
  1214. upper = gb_max(upper, x);
  1215. }
  1216. if (et->Enum.fields.count == 0) {
  1217. lower = 0;
  1218. upper = 0;
  1219. }
  1220. GB_ASSERT(lower <= upper);
  1221. bool lower_changed = false;
  1222. i64 bits = MAX_BITS
  1223. ; if (bs->underlying != nullptr) {
  1224. Type *u = check_type(c, bs->underlying);
  1225. if (!is_type_integer(u)) {
  1226. gbString ts = type_to_string(u);
  1227. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1228. gb_string_free(ts);
  1229. return;
  1230. }
  1231. type->BitSet.underlying = u;
  1232. bits = 8*type_size_of(u);
  1233. if (lower > 0) {
  1234. lower = 0;
  1235. lower_changed = true;
  1236. } else if (lower < 0) {
  1237. gbString s = type_to_string(elem);
  1238. 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);
  1239. gb_string_free(s);
  1240. }
  1241. }
  1242. if (upper - lower >= bits) {
  1243. i64 bits_required = upper-lower+1;
  1244. if (lower_changed) {
  1245. 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);
  1246. } else {
  1247. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  1248. }
  1249. }
  1250. type->BitSet.lower = lower;
  1251. type->BitSet.upper = upper;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. gb_internal bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  1257. if (type == nullptr ||
  1258. type == t_invalid) {
  1259. return true;
  1260. }
  1261. Type *t = base_type(type);
  1262. Type *s = base_type(specialization);
  1263. if (t->kind != s->kind) {
  1264. if (t->kind == Type_EnumeratedArray && s->kind == Type_Array) {
  1265. // Might be okay, check later
  1266. } else {
  1267. return false;
  1268. }
  1269. }
  1270. if (is_type_untyped(t)) {
  1271. Operand o = {Addressing_Value};
  1272. o.type = default_type(type);
  1273. bool can_convert = check_cast_internal(ctx, &o, specialization);
  1274. return can_convert;
  1275. } else if (t->kind == Type_Struct) {
  1276. if (t->Struct.polymorphic_parent == nullptr &&
  1277. t == s) {
  1278. return true;
  1279. }
  1280. if (t->Struct.polymorphic_parent == specialization) {
  1281. return true;
  1282. }
  1283. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  1284. s->Struct.polymorphic_params != nullptr &&
  1285. t->Struct.polymorphic_params != nullptr) {
  1286. TypeTuple *s_tuple = get_record_polymorphic_params(s);
  1287. TypeTuple *t_tuple = get_record_polymorphic_params(t);
  1288. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1289. for_array(i, s_tuple->variables) {
  1290. Entity *s_e = s_tuple->variables[i];
  1291. Entity *t_e = t_tuple->variables[i];
  1292. Type *st = s_e->type;
  1293. Type *tt = t_e->type;
  1294. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1295. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1296. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1297. GB_ASSERT(e != nullptr);
  1298. if (modify_type) {
  1299. e->kind = Entity_Constant;
  1300. e->Constant.value = t_e->Constant.value;
  1301. e->type = t_e->type;
  1302. }
  1303. } else {
  1304. if (st->kind == Type_Basic && tt->kind == Type_Basic &&
  1305. s_e->kind == Entity_Constant && t_e->kind == Entity_Constant) {
  1306. if (!compare_exact_values(Token_CmpEq, s_e->Constant.value, t_e->Constant.value))
  1307. return false;
  1308. } else {
  1309. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1310. if (!ok) {
  1311. // TODO(bill, 2021-08-19): is this logic correct?
  1312. return false;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. if (modify_type) {
  1318. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1319. gb_memmove(specialization, type, gb_size_of(Type));
  1320. }
  1321. return true;
  1322. }
  1323. } else if (t->kind == Type_Union) {
  1324. if (t->Union.polymorphic_parent == nullptr &&
  1325. t == s) {
  1326. return true;
  1327. }
  1328. if (t->Union.polymorphic_parent == specialization) {
  1329. return true;
  1330. }
  1331. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1332. s->Union.polymorphic_params != nullptr &&
  1333. t->Union.polymorphic_params != nullptr) {
  1334. TypeTuple *s_tuple = get_record_polymorphic_params(s);
  1335. TypeTuple *t_tuple = get_record_polymorphic_params(t);
  1336. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1337. for_array(i, s_tuple->variables) {
  1338. Entity *s_e = s_tuple->variables[i];
  1339. Entity *t_e = t_tuple->variables[i];
  1340. Type *st = s_e->type;
  1341. Type *tt = t_e->type;
  1342. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1343. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1344. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1345. GB_ASSERT(e != nullptr);
  1346. if (modify_type) {
  1347. e->kind = Entity_Constant;
  1348. e->Constant.value = t_e->Constant.value;
  1349. e->type = t_e->type;
  1350. }
  1351. } else {
  1352. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1353. if (!ok) {
  1354. // TODO(bill, 2021-08-19): is this logic correct?
  1355. return false;
  1356. }
  1357. }
  1358. }
  1359. if (modify_type) {
  1360. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1361. gb_memmove(specialization, type, gb_size_of(Type));
  1362. }
  1363. return true;
  1364. }
  1365. }
  1366. if (specialization->kind == Type_Named &&
  1367. type->kind != Type_Named) {
  1368. return false;
  1369. }
  1370. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1371. return true;
  1372. }
  1373. return false;
  1374. }
  1375. gb_internal Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand const &operand) {
  1376. bool modify_type = !ctx->no_polymorphic_errors;
  1377. bool show_error = modify_type && !ctx->hide_polymorphic_errors;
  1378. if (!is_operand_value(operand)) {
  1379. if (show_error) {
  1380. gbString pts = type_to_string(poly_type);
  1381. gbString ots = type_to_string(operand.type, true);
  1382. defer (gb_string_free(pts));
  1383. defer (gb_string_free(ots));
  1384. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1385. }
  1386. return t_invalid;
  1387. }
  1388. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1389. return poly_type;
  1390. }
  1391. if (show_error) {
  1392. gbString pts = type_to_string(poly_type);
  1393. gbString ots = type_to_string(operand.type, true);
  1394. defer (gb_string_free(pts));
  1395. defer (gb_string_free(ots));
  1396. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1397. }
  1398. return t_invalid;
  1399. }
  1400. gb_internal bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1401. if (expr == nullptr) {
  1402. return false;
  1403. }
  1404. expr = unparen_expr(expr);
  1405. if (expr->kind == Ast_SelectorExpr) {
  1406. Ast *lhs = expr->SelectorExpr.expr;
  1407. return is_expr_from_a_parameter(ctx, lhs);
  1408. } else if (expr->kind == Ast_Ident) {
  1409. Operand x= {};
  1410. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, true);
  1411. if (e->flags & EntityFlag_Param) {
  1412. return true;
  1413. }
  1414. }
  1415. return false;
  1416. }
  1417. gb_internal ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1418. ParameterValue param_value = {};
  1419. param_value.original_ast_expr = expr;
  1420. if (expr == nullptr) {
  1421. return param_value;
  1422. }
  1423. Operand o = {};
  1424. if (allow_caller_location &&
  1425. expr->kind == Ast_BasicDirective &&
  1426. expr->BasicDirective.name.string == "caller_location") {
  1427. init_core_source_code_location(ctx->checker);
  1428. param_value.kind = ParameterValue_Location;
  1429. o.type = t_source_code_location;
  1430. o.mode = Addressing_Value;
  1431. o.expr = expr;
  1432. if (in_type) {
  1433. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1434. }
  1435. } else {
  1436. if (in_type) {
  1437. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1438. } else {
  1439. check_expr(ctx, &o, expr);
  1440. }
  1441. if (in_type) {
  1442. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1443. }
  1444. if (is_operand_nil(o)) {
  1445. param_value.kind = ParameterValue_Nil;
  1446. } else if (o.mode != Addressing_Constant) {
  1447. if (expr->kind == Ast_ProcLit) {
  1448. param_value.kind = ParameterValue_Constant;
  1449. param_value.value = exact_value_procedure(expr);
  1450. } else {
  1451. Entity *e = entity_from_expr(o.expr);
  1452. if (e != nullptr) {
  1453. if (e->kind == Entity_Procedure) {
  1454. param_value.kind = ParameterValue_Constant;
  1455. param_value.value = exact_value_procedure(e->identifier);
  1456. add_entity_use(ctx, e->identifier, e);
  1457. } else {
  1458. if (e->flags & EntityFlag_Param) {
  1459. error(expr, "Default parameter cannot be another parameter");
  1460. } else {
  1461. if (is_expr_from_a_parameter(ctx, expr)) {
  1462. error(expr, "Default parameter cannot be another parameter");
  1463. } else {
  1464. param_value.kind = ParameterValue_Value;
  1465. param_value.ast_value = expr;
  1466. add_entity_use(ctx, e->identifier, e);
  1467. }
  1468. }
  1469. }
  1470. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1471. param_value.kind = ParameterValue_Value;
  1472. param_value.ast_value = expr;
  1473. } else if (o.value.kind != ExactValue_Invalid) {
  1474. param_value.kind = ParameterValue_Constant;
  1475. param_value.value = o.value;
  1476. } else {
  1477. gbString s = expr_to_string(o.expr);
  1478. error(expr, "Default parameter must be a constant, got %s", s);
  1479. gb_string_free(s);
  1480. }
  1481. }
  1482. } else {
  1483. if (o.value.kind != ExactValue_Invalid) {
  1484. param_value.kind = ParameterValue_Constant;
  1485. param_value.value = o.value;
  1486. } else {
  1487. gbString s = expr_to_string(o.expr);
  1488. error(o.expr, "Invalid constant parameter, got '%s'", s);
  1489. // error(o.expr, "Invalid constant parameter, got '%s' %d %d", s, o.mode, o.value.kind);
  1490. gb_string_free(s);
  1491. }
  1492. }
  1493. }
  1494. if (out_type_) {
  1495. if (in_type != nullptr) {
  1496. *out_type_ = in_type;
  1497. } else {
  1498. *out_type_ = default_type(o.type);
  1499. }
  1500. }
  1501. return param_value;
  1502. }
  1503. 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) {
  1504. if (_params == nullptr) {
  1505. return nullptr;
  1506. }
  1507. bool success = true;
  1508. ast_node(field_list, FieldList, _params);
  1509. Slice<Ast *> params = field_list->list;
  1510. if (params.count == 0) {
  1511. if (success_) *success_ = success;
  1512. return nullptr;
  1513. }
  1514. isize variable_count = 0;
  1515. for_array(i, params) {
  1516. Ast *field = params[i];
  1517. if (ast_node_expect(field, Ast_Field)) {
  1518. ast_node(f, Field, field);
  1519. variable_count += gb_max(f->names.count, 1);
  1520. }
  1521. }
  1522. isize min_variable_count = variable_count;
  1523. for (isize i = params.count-1; i >= 0; i--) {
  1524. Ast *field = params[i];
  1525. if (field->kind == Ast_Field) {
  1526. ast_node(f, Field, field);
  1527. if (f->default_value == nullptr) {
  1528. break;
  1529. }
  1530. min_variable_count--;
  1531. }
  1532. }
  1533. bool is_variadic = false;
  1534. isize variadic_index = -1;
  1535. bool is_c_vararg = false;
  1536. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1537. i32 field_group_index = -1;
  1538. for_array(i, params) {
  1539. Ast *param = params[i];
  1540. if (param->kind != Ast_Field) {
  1541. continue;
  1542. }
  1543. field_group_index += 1;
  1544. ast_node(p, Field, param);
  1545. Ast *type_expr = unparen_expr(p->type);
  1546. Type *type = nullptr;
  1547. Ast *default_value = unparen_expr(p->default_value);
  1548. ParameterValue param_value = {};
  1549. bool is_type_param = false;
  1550. bool is_type_polymorphic_type = false;
  1551. bool detemine_type_from_operand = false;
  1552. Type *specialization = nullptr;
  1553. bool is_using = (p->flags&FieldFlag_using) != 0;
  1554. if ((check_vet_flags(param) & VetFlag_UsingParam) && is_using) {
  1555. ERROR_BLOCK();
  1556. error(param, "'using' on a procedure parameter is not allowed when '-vet' or '-vet-using-param' is applied");
  1557. error_line("\t'using' is considered bad practice to use as a statement/procedure parameter outside of immediate refactoring\n");
  1558. }
  1559. if (type_expr == nullptr) {
  1560. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1561. } else {
  1562. Ast *original_type_expr = type_expr;
  1563. if (type_expr->kind == Ast_Ellipsis) {
  1564. type_expr = type_expr->Ellipsis.expr;
  1565. is_variadic = true;
  1566. variadic_index = variables.count;
  1567. if (p->names.count != 1) {
  1568. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1569. success = false;
  1570. }
  1571. GB_ASSERT(original_type_expr->kind == Ast_Ellipsis);
  1572. type_expr = ast_array_type(type_expr->file(), original_type_expr->Ellipsis.token, nullptr, type_expr);
  1573. }
  1574. if (type_expr->kind == Ast_TypeidType) {
  1575. ast_node(tt, TypeidType, type_expr);
  1576. if (tt->specialization) {
  1577. specialization = check_type(ctx, tt->specialization);
  1578. if (specialization == t_invalid){
  1579. specialization = nullptr;
  1580. }
  1581. if (operands != nullptr) {
  1582. detemine_type_from_operand = true;
  1583. type = t_invalid;
  1584. } else {
  1585. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1586. }
  1587. } else {
  1588. type = t_typeid;
  1589. }
  1590. } else {
  1591. bool prev = ctx->allow_polymorphic_types;
  1592. if (operands != nullptr) {
  1593. ctx->allow_polymorphic_types = true;
  1594. }
  1595. type = check_type(ctx, type_expr);
  1596. ctx->allow_polymorphic_types = prev;
  1597. if (is_type_polymorphic(type)) {
  1598. is_type_polymorphic_type = true;
  1599. }
  1600. }
  1601. if (default_value != nullptr) {
  1602. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1603. error(type_expr, "A type parameter may not have a default value");
  1604. } else {
  1605. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1606. }
  1607. }
  1608. }
  1609. if (type == nullptr) {
  1610. error(param, "Invalid parameter type");
  1611. type = t_invalid;
  1612. }
  1613. if (is_type_untyped(type)) {
  1614. if (is_type_untyped_uninit(type)) {
  1615. error(param, "Cannot determine parameter type from ---");
  1616. } else {
  1617. error(param, "Cannot determine parameter type from a nil");
  1618. }
  1619. type = t_invalid;
  1620. }
  1621. // if (is_type_empty_union(type)) {
  1622. // gbString str = type_to_string(type);
  1623. // error(param, "Invalid use of an empty union '%s'", str);
  1624. // gb_string_free(str);
  1625. // type = t_invalid;
  1626. // }
  1627. if (is_type_polymorphic(type)) {
  1628. switch (param_value.kind) {
  1629. case ParameterValue_Invalid:
  1630. case ParameterValue_Constant:
  1631. case ParameterValue_Nil:
  1632. break;
  1633. case ParameterValue_Location:
  1634. case ParameterValue_Value:
  1635. gbString str = type_to_string(type);
  1636. error(params[i], "A default value for a parameter must not be a polymorphic constant type, got %s", str);
  1637. gb_string_free(str);
  1638. break;
  1639. }
  1640. }
  1641. if (p->flags&FieldFlag_c_vararg) {
  1642. if (p->type == nullptr ||
  1643. p->type->kind != Ast_Ellipsis) {
  1644. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1645. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1646. } else {
  1647. is_c_vararg = true;
  1648. }
  1649. }
  1650. for_array(j, p->names) {
  1651. Ast *name = p->names[j];
  1652. bool is_poly_name = false;
  1653. switch (name->kind) {
  1654. case Ast_Ident:
  1655. break;
  1656. case Ast_PolyType:
  1657. GB_ASSERT(name->PolyType.specialization == nullptr);
  1658. is_poly_name = true;
  1659. name = name->PolyType.type;
  1660. break;
  1661. }
  1662. if (!ast_node_expect(name, Ast_Ident)) {
  1663. continue;
  1664. }
  1665. if (is_poly_name) {
  1666. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1667. is_type_param = true;
  1668. } else {
  1669. if (param_value.kind != ParameterValue_Invalid) {
  1670. error(default_value, "Constant parameters cannot have a default value");
  1671. param_value.kind = ParameterValue_Invalid;
  1672. }
  1673. }
  1674. }
  1675. Entity *param = nullptr;
  1676. if (is_type_param) {
  1677. if (operands != nullptr) {
  1678. Operand o = (*operands)[variables.count];
  1679. if (o.mode == Addressing_Type) {
  1680. type = o.type;
  1681. } else {
  1682. if (!ctx->no_polymorphic_errors) {
  1683. error(o.expr, "Expected a type to assign to the type parameter");
  1684. }
  1685. success = false;
  1686. type = t_invalid;
  1687. }
  1688. if (is_type_polymorphic(type)) {
  1689. gbString str = type_to_string(type);
  1690. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1691. gb_string_free(str);
  1692. success = false;
  1693. type = t_invalid;
  1694. }
  1695. if (is_type_untyped(default_type(type))) {
  1696. gbString str = type_to_string(type);
  1697. error(o.expr, "Cannot determine type from the parameter, got '%s'", str);
  1698. gb_string_free(str);
  1699. success = false;
  1700. type = t_invalid;
  1701. }
  1702. bool modify_type = !ctx->no_polymorphic_errors;
  1703. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1704. if (!ctx->no_polymorphic_errors) {
  1705. gbString t = type_to_string(type);
  1706. gbString s = type_to_string(specialization);
  1707. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1708. gb_string_free(s);
  1709. gb_string_free(t);
  1710. }
  1711. success = false;
  1712. type = t_invalid;
  1713. }
  1714. }
  1715. if (p->flags&FieldFlag_const) {
  1716. error(name, "'#const' can only be applied to variable fields");
  1717. p->flags &= ~FieldFlag_const;
  1718. }
  1719. if (p->flags&FieldFlag_any_int) {
  1720. error(name, "'#any_int' can only be applied to variable fields");
  1721. p->flags &= ~FieldFlag_any_int;
  1722. }
  1723. if (p->flags&FieldFlag_no_broadcast) {
  1724. error(name, "'#no_broadcast' can only be applied to variable fields");
  1725. p->flags &= ~FieldFlag_no_broadcast;
  1726. }
  1727. if (p->flags&FieldFlag_by_ptr) {
  1728. error(name, "'#by_ptr' can only be applied to variable fields");
  1729. p->flags &= ~FieldFlag_by_ptr;
  1730. }
  1731. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1732. param->TypeName.is_type_alias = true;
  1733. } else {
  1734. ExactValue poly_const = {};
  1735. if (operands != nullptr && variables.count < operands->count) {
  1736. Operand op = (*operands)[variables.count];
  1737. if (op.expr == nullptr) {
  1738. // NOTE(bill): 2019-03-30
  1739. // This is just to add the error message to determine_type_from_polymorphic which
  1740. // depends on valid position information
  1741. op.expr = _params;
  1742. op.mode = Addressing_Invalid;
  1743. op.type = t_invalid;
  1744. }
  1745. if (is_type_polymorphic_type) {
  1746. type = determine_type_from_polymorphic(ctx, type, op);
  1747. if (type == t_invalid) {
  1748. success = false;
  1749. } else if (!ctx->no_polymorphic_errors) {
  1750. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1751. is_type_polymorphic_type = false;
  1752. Entity *proc_entity = entity_from_expr(op.expr);
  1753. if ((proc_entity != nullptr) && (op.value.kind == ExactValue_Procedure)) {
  1754. if (is_type_polymorphic(proc_entity->type, false)) {
  1755. error(op.expr, "Cannot determine complete type of partial polymorphic procedure");
  1756. }
  1757. }
  1758. }
  1759. }
  1760. if (is_poly_name) {
  1761. bool valid = false;
  1762. if (is_type_proc(op.type)) {
  1763. Ast *expr = unparen_expr(op.expr);
  1764. Entity *proc_entity = entity_from_expr(expr);
  1765. if (proc_entity) {
  1766. poly_const = exact_value_procedure(proc_entity->identifier.load() ? proc_entity->identifier.load() : op.expr);
  1767. valid = true;
  1768. } else if (expr->kind == Ast_ProcLit) {
  1769. poly_const = exact_value_procedure(expr);
  1770. valid = true;
  1771. }
  1772. }
  1773. if (!valid) {
  1774. if (op.mode == Addressing_Constant) {
  1775. poly_const = op.value;
  1776. } else {
  1777. error(op.expr, "Expected a constant value for this polymorphic name parameter, got %s", expr_to_string(op.expr));
  1778. success = false;
  1779. }
  1780. }
  1781. }
  1782. bool allow_array_programming = true;
  1783. if (p->flags&FieldFlag_no_broadcast) {
  1784. allow_array_programming = false;
  1785. }
  1786. if (type != t_invalid && !check_is_assignable_to(ctx, &op, type, allow_array_programming)) {
  1787. bool ok = true;
  1788. if (p->flags&FieldFlag_any_int) {
  1789. if ((!is_type_integer(op.type) && !is_type_enum(op.type)) || (!is_type_integer(type) && !is_type_enum(type))) {
  1790. ok = false;
  1791. } else if (!check_is_castable_to(ctx, &op, type)) {
  1792. ok = false;
  1793. }
  1794. }
  1795. if (!ok) {
  1796. success = false;
  1797. #if 0
  1798. gbString got = type_to_string(op.type);
  1799. gbString expected = type_to_string(type);
  1800. error(op.expr, "Cannot assigned type to parameter, got type '%s', expected '%s'", got, expected);
  1801. gb_string_free(expected);
  1802. gb_string_free(got);
  1803. #endif
  1804. }
  1805. }
  1806. if (is_type_untyped(default_type(type))) {
  1807. gbString str = type_to_string(type);
  1808. error(op.expr, "Cannot determine type from the parameter, got '%s'", str);
  1809. gb_string_free(str);
  1810. success = false;
  1811. type = t_invalid;
  1812. }
  1813. }
  1814. if (p->flags&FieldFlag_no_alias) {
  1815. if (!is_type_pointer(type) && !is_type_multi_pointer(type)) {
  1816. error(name, "'#no_alias' can only be applied pointer or multi-pointer typed parameters");
  1817. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1818. }
  1819. }
  1820. if (p->flags&FieldFlag_by_ptr) {
  1821. if (is_type_internally_pointer_like(type)) {
  1822. error(name, "'#by_ptr' can only be applied to non-pointer-like parameters");
  1823. p->flags &= ~FieldFlag_by_ptr; // Remove the flag
  1824. }
  1825. }
  1826. if (is_poly_name) {
  1827. if (p->flags&FieldFlag_no_alias) {
  1828. error(name, "'#no_alias' can only be applied to non constant values");
  1829. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1830. }
  1831. if (p->flags&FieldFlag_any_int) {
  1832. error(name, "'#any_int' can only be applied to variable fields");
  1833. p->flags &= ~FieldFlag_any_int;
  1834. }
  1835. if (p->flags&FieldFlag_const) {
  1836. error(name, "'#const' can only be applied to variable fields");
  1837. p->flags &= ~FieldFlag_const;
  1838. }
  1839. if (p->flags&FieldFlag_by_ptr) {
  1840. error(name, "'#by_ptr' can only be applied to variable fields");
  1841. p->flags &= ~FieldFlag_by_ptr;
  1842. }
  1843. if (!is_type_polymorphic(type) && check_constant_parameter_value(type, params[i])) {
  1844. // failed
  1845. }
  1846. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1847. param->Constant.field_group_index = field_group_index;
  1848. } else {
  1849. param = alloc_entity_param(scope, name->Ident.token, type, is_using, true);
  1850. param->Variable.param_value = param_value;
  1851. param->Variable.field_group_index = field_group_index;
  1852. param->Variable.type_expr = type_expr;
  1853. }
  1854. }
  1855. if (is_variadic && variadic_index == variables.count) {
  1856. param->flags |= EntityFlag_Ellipsis;
  1857. if (is_c_vararg) {
  1858. param->flags |= EntityFlag_CVarArg;
  1859. }
  1860. }
  1861. if (p->flags&FieldFlag_no_alias) {
  1862. param->flags |= EntityFlag_NoAlias;
  1863. }
  1864. if (p->flags&FieldFlag_no_broadcast) {
  1865. param->flags |= EntityFlag_NoBroadcast;
  1866. }
  1867. if (p->flags&FieldFlag_any_int) {
  1868. if (!is_type_integer(param->type) && !is_type_enum(param->type)) {
  1869. gbString str = type_to_string(param->type);
  1870. error(name, "A parameter with '#any_int' must be an integer, got %s", str);
  1871. gb_string_free(str);
  1872. }
  1873. param->flags |= EntityFlag_AnyInt;
  1874. }
  1875. if (p->flags&FieldFlag_const) {
  1876. param->flags |= EntityFlag_ConstInput;
  1877. }
  1878. if (p->flags&FieldFlag_by_ptr) {
  1879. param->flags |= EntityFlag_ByPtr;
  1880. }
  1881. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1882. add_entity(ctx, scope, name, param);
  1883. if (is_using) {
  1884. add_entity_use(ctx, name, param);
  1885. }
  1886. array_add(&variables, param);
  1887. }
  1888. }
  1889. if (is_variadic) {
  1890. GB_ASSERT(variadic_index >= 0);
  1891. GB_ASSERT(params.count > 0);
  1892. }
  1893. isize specialization_count = 0;
  1894. if (scope != nullptr) {
  1895. for (auto const &entry : scope->elements) {
  1896. Entity *e = entry.value;
  1897. if (e->kind == Entity_TypeName) {
  1898. Type *t = e->type;
  1899. if (t->kind == Type_Generic &&
  1900. t->Generic.specialized != nullptr) {
  1901. specialization_count += 1;
  1902. }
  1903. }
  1904. }
  1905. }
  1906. Type *tuple = alloc_type_tuple();
  1907. tuple->Tuple.variables = slice_from_array(variables);
  1908. if (success_) *success_ = success;
  1909. if (specialization_count_) *specialization_count_ = specialization_count;
  1910. if (is_variadic_) *is_variadic_ = is_variadic;
  1911. if (variadic_index_) *variadic_index_ = variadic_index;
  1912. return tuple;
  1913. }
  1914. gb_internal Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1915. if (_results == nullptr) {
  1916. return nullptr;
  1917. }
  1918. ast_node(field_list, FieldList, _results);
  1919. Slice<Ast *> results = field_list->list;
  1920. if (results.count == 0) {
  1921. return nullptr;
  1922. }
  1923. Type *tuple = alloc_type_tuple();
  1924. isize variable_count = 0;
  1925. for_array(i, results) {
  1926. Ast *field = results[i];
  1927. if (ast_node_expect(field, Ast_Field)) {
  1928. ast_node(f, Field, field);
  1929. variable_count += gb_max(f->names.count, 1);
  1930. }
  1931. }
  1932. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1933. i32 field_group_index = -1;
  1934. for_array(i, results) {
  1935. field_group_index += 1;
  1936. ast_node(field, Field, results[i]);
  1937. Ast *default_value = unparen_expr(field->default_value);
  1938. ParameterValue param_value = {};
  1939. Type *type = nullptr;
  1940. if (field->type == nullptr) {
  1941. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1942. } else {
  1943. type = check_type(ctx, field->type);
  1944. if (default_value != nullptr) {
  1945. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1946. }
  1947. }
  1948. if (type == nullptr) {
  1949. error(results[i], "Invalid parameter type");
  1950. type = t_invalid;
  1951. }
  1952. if (is_type_untyped(type)) {
  1953. error(results[i], "Cannot determine parameter type from a nil");
  1954. type = t_invalid;
  1955. }
  1956. if (field->names.count == 0) {
  1957. Token token = ast_token(field->type);
  1958. token.string = str_lit("");
  1959. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1960. param->Variable.param_value = param_value;
  1961. param->Variable.field_group_index = -1;
  1962. array_add(&variables, param);
  1963. } else {
  1964. for_array(j, field->names) {
  1965. Token token = ast_token(results[i]);
  1966. if (field->type != nullptr) {
  1967. token = ast_token(field->type);
  1968. }
  1969. token.string = str_lit("");
  1970. Ast *name = field->names[j];
  1971. if (name->kind != Ast_Ident) {
  1972. error(name, "Expected an identifer for as the field name");
  1973. } else {
  1974. token = name->Ident.token;
  1975. }
  1976. if (is_blank_ident(token)) {
  1977. error(name, "Result value cannot be a blank identifer `_`");
  1978. }
  1979. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1980. param->flags |= EntityFlag_Result;
  1981. param->Variable.param_value = param_value;
  1982. param->Variable.field_group_index = field_group_index;
  1983. array_add(&variables, param);
  1984. add_entity(ctx, scope, name, param);
  1985. // NOTE(bill): Removes `declared but not used` when using -vet
  1986. add_entity_use(ctx, name, param);
  1987. }
  1988. }
  1989. }
  1990. for_array(i, variables) {
  1991. String x = variables[i]->token.string;
  1992. if (x.len == 0 || is_blank_ident(x)) {
  1993. continue;
  1994. }
  1995. for (isize j = i+1; j < variables.count; j++) {
  1996. String y = variables[j]->token.string;
  1997. if (y.len == 0 || is_blank_ident(y)) {
  1998. continue;
  1999. }
  2000. if (x == y) {
  2001. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  2002. }
  2003. }
  2004. }
  2005. tuple->Tuple.variables = slice_from_array(variables);
  2006. return tuple;
  2007. }
  2008. // NOTE(bill): 'operands' is for generating non generic procedure type
  2009. gb_internal bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> const *operands) {
  2010. ast_node(pt, ProcType, proc_type_node);
  2011. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  2012. ctx->polymorphic_scope = ctx->scope;
  2013. }
  2014. CheckerContext c_ = *ctx;
  2015. CheckerContext *c = &c_;
  2016. c->curr_proc_sig = type;
  2017. c->in_proc_sig = true;
  2018. ProcCallingConvention cc = pt->calling_convention;
  2019. if (cc == ProcCC_ForeignBlockDefault) {
  2020. cc = ProcCC_CDecl;
  2021. if (c->foreign_context.default_cc > 0) {
  2022. cc = c->foreign_context.default_cc;
  2023. }
  2024. }
  2025. GB_ASSERT(cc > 0);
  2026. if (cc == ProcCC_Odin) {
  2027. c->scope->flags |= ScopeFlag_ContextDefined;
  2028. } else {
  2029. c->scope->flags &= ~ScopeFlag_ContextDefined;
  2030. }
  2031. TargetArchKind arch = build_context.metrics.arch;
  2032. switch (cc) {
  2033. case ProcCC_StdCall:
  2034. case ProcCC_FastCall:
  2035. if (arch != TargetArch_i386 && arch != TargetArch_amd64) {
  2036. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected either i386 or amd64, got %.*s",
  2037. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  2038. }
  2039. break;
  2040. case ProcCC_Win64:
  2041. case ProcCC_SysV:
  2042. if (arch != TargetArch_amd64) {
  2043. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected amd64, got %.*s",
  2044. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  2045. }
  2046. break;
  2047. }
  2048. bool variadic = false;
  2049. isize variadic_index = -1;
  2050. bool success = true;
  2051. isize specialization_count = 0;
  2052. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  2053. Type *results = check_get_results(c, c->scope, pt->results);
  2054. isize param_count = 0;
  2055. isize result_count = 0;
  2056. if (params) param_count = params ->Tuple.variables.count;
  2057. if (results) result_count = results->Tuple.variables.count;
  2058. if (result_count > 0) {
  2059. Entity *first = results->Tuple.variables[0];
  2060. type->Proc.has_named_results = first->token.string != "";
  2061. }
  2062. bool optional_ok = (pt->tags & ProcTag_optional_ok) != 0;
  2063. if (optional_ok) {
  2064. if (result_count != 2) {
  2065. error(proc_type_node, "A procedure type with the #optional_ok tag requires 2 return values, got %td", result_count);
  2066. } else {
  2067. Entity *second = results->Tuple.variables[1];
  2068. if (is_type_polymorphic(second->type)) {
  2069. // ignore
  2070. } else if (is_type_boolean(second->type)) {
  2071. // GOOD
  2072. } else {
  2073. error(second->token, "Second return value of an #optional_ok procedure must be a boolean, got %s", type_to_string(second->type));
  2074. }
  2075. }
  2076. }
  2077. if (pt->tags & ProcTag_optional_allocator_error) {
  2078. if (optional_ok) {
  2079. error(proc_type_node, "A procedure type cannot have both an #optional_ok tag and #optional_allocator_error");
  2080. }
  2081. optional_ok = true;
  2082. if (result_count != 2) {
  2083. error(proc_type_node, "A procedure type with the #optional_allocator_error tag requires 2 return values, got %td", result_count);
  2084. } else {
  2085. init_mem_allocator(c->checker);
  2086. Type *type = results->Tuple.variables[1]->type;
  2087. if (!are_types_identical(type, t_allocator_error)) {
  2088. gbString t = type_to_string(type);
  2089. error(proc_type_node, "A procedure type with the #optional_allocator_error expects a `runtime.Allocator_Error`, got '%s'", t);
  2090. gb_string_free(t);
  2091. }
  2092. }
  2093. }
  2094. type->Proc.node = proc_type_node;
  2095. type->Proc.scope = c->scope;
  2096. type->Proc.params = params;
  2097. type->Proc.param_count = cast(i32)param_count;
  2098. type->Proc.results = results;
  2099. type->Proc.result_count = cast(i32)result_count;
  2100. type->Proc.variadic = variadic;
  2101. type->Proc.variadic_index = cast(i32)variadic_index;
  2102. type->Proc.calling_convention = cc;
  2103. type->Proc.is_polymorphic = pt->generic;
  2104. type->Proc.specialization_count = specialization_count;
  2105. type->Proc.diverging = pt->diverging;
  2106. type->Proc.optional_ok = optional_ok;
  2107. bool is_polymorphic = false;
  2108. for (isize i = 0; i < param_count; i++) {
  2109. Entity *e = params->Tuple.variables[i];
  2110. if (e->kind != Entity_Variable) {
  2111. is_polymorphic = true;
  2112. } else if (is_type_polymorphic(e->type)) {
  2113. is_polymorphic = true;
  2114. }
  2115. if (e->flags&EntityFlag_CVarArg) {
  2116. if (i != param_count - 1) {
  2117. error(e->token, "#c_vararg can only be applied to the last parameter");
  2118. continue;
  2119. }
  2120. switch (cc) {
  2121. default:
  2122. type->Proc.c_vararg = true;
  2123. break;
  2124. case ProcCC_Odin:
  2125. case ProcCC_Contextless:
  2126. error(e->token, "Calling convention does not support #c_vararg");
  2127. break;
  2128. }
  2129. }
  2130. }
  2131. for (isize i = 0; i < result_count; i++) {
  2132. Entity *e = results->Tuple.variables[i];
  2133. if (e->kind != Entity_Variable) {
  2134. is_polymorphic = true;
  2135. break;
  2136. } else if (is_type_polymorphic(e->type)) {
  2137. is_polymorphic = true;
  2138. break;
  2139. }
  2140. }
  2141. type->Proc.is_polymorphic = is_polymorphic;
  2142. return success;
  2143. }
  2144. gb_internal i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  2145. if (e == nullptr) {
  2146. return 0;
  2147. }
  2148. if (e->kind == Ast_UnaryExpr &&
  2149. e->UnaryExpr.op.kind == Token_Question) {
  2150. return -1;
  2151. }
  2152. check_expr_or_type(ctx, o, e);
  2153. if (o->mode == Addressing_Type) {
  2154. Type *ot = base_type(o->type);
  2155. if (ot->kind == Type_Generic) {
  2156. if (ctx->allow_polymorphic_types) {
  2157. if (ot->Generic.specialized) {
  2158. ot->Generic.specialized = nullptr;
  2159. error(o->expr, "Polymorphic array length cannot have a specialization");
  2160. }
  2161. return 0;
  2162. }
  2163. }
  2164. if (is_type_enum(ot)) {
  2165. return -1;
  2166. }
  2167. }
  2168. if (o->mode != Addressing_Constant) {
  2169. if (o->mode != Addressing_Invalid) {
  2170. Entity *entity = entity_of_node(o->expr);
  2171. bool is_poly_type = false;
  2172. if (entity != nullptr) {
  2173. is_poly_type = \
  2174. entity->kind == Entity_TypeName &&
  2175. entity->type == t_typeid &&
  2176. entity->flags&EntityFlag_PolyConst;
  2177. }
  2178. // NOTE(bill, 2021-03-27): Improve error message for parametric polymorphic parameters which want to generate
  2179. // and enumerated array but cannot determine what it ought to be yet
  2180. if (ctx->allow_polymorphic_types && is_poly_type) {
  2181. return 0;
  2182. }
  2183. ERROR_BLOCK();
  2184. gbString s = expr_to_string(o->expr);
  2185. error(e, "Array count must be a constant integer, got %s", s);
  2186. gb_string_free(s);
  2187. if (is_poly_type) {
  2188. error_line("\tSuggestion: 'where' clause may be required to restrict the enumerated array index type to an enum\n");
  2189. error_line("\t 'where intrinsics.type_is_enum(%.*s)'\n", LIT(entity->token.string));
  2190. }
  2191. o->mode = Addressing_Invalid;
  2192. o->type = t_invalid;
  2193. }
  2194. return 0;
  2195. }
  2196. Type *type = core_type(o->type);
  2197. if (is_type_untyped(type) || is_type_integer(type)) {
  2198. if (o->value.kind == ExactValue_Integer) {
  2199. BigInt count = o->value.value_integer;
  2200. if (big_int_is_neg(&o->value.value_integer)) {
  2201. gbAllocator a = heap_allocator();
  2202. String str = big_int_to_string(a, &count);
  2203. error(e, "Invalid negative array count, %.*s", LIT(str));
  2204. gb_free(a, str.text);
  2205. return 0;
  2206. }
  2207. switch (count.used) {
  2208. case 0: return 0;
  2209. case 1: return big_int_to_u64(&count);
  2210. }
  2211. gbAllocator a = heap_allocator();
  2212. String str = big_int_to_string(a, &count);
  2213. error(e, "Array count too large, %.*s", LIT(str));
  2214. gb_free(a, str.text);
  2215. return 0;
  2216. }
  2217. }
  2218. error(e, "Array count must be a constant integer");
  2219. return 0;
  2220. }
  2221. gb_internal Type *make_optional_ok_type(Type *value, bool typed) {
  2222. gbAllocator a = permanent_allocator();
  2223. Type *t = alloc_type_tuple();
  2224. slice_init(&t->Tuple.variables, a, 2);
  2225. t->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, value, false, 0);
  2226. t->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  2227. return t;
  2228. }
  2229. // IMPORTANT NOTE(bill): This must match the definition in dynamic_map_internal.odin
  2230. enum : i64 {
  2231. MAP_CELL_CACHE_LINE_LOG2 = 6,
  2232. MAP_CELL_CACHE_LINE_SIZE = 1 << MAP_CELL_CACHE_LINE_LOG2,
  2233. };
  2234. GB_STATIC_ASSERT(MAP_CELL_CACHE_LINE_SIZE >= 64);
  2235. gb_internal void map_cell_size_and_len(Type *type, i64 *size_, i64 *len_) {
  2236. i64 elem_sz = type_size_of(type);
  2237. i64 len = 1;
  2238. if (0 < elem_sz && elem_sz < MAP_CELL_CACHE_LINE_SIZE) {
  2239. len = MAP_CELL_CACHE_LINE_SIZE / elem_sz;
  2240. }
  2241. i64 size = align_formula(elem_sz * len, MAP_CELL_CACHE_LINE_SIZE);
  2242. if (size_) *size_ = size;
  2243. if (len_) *len_ = len;
  2244. }
  2245. gb_internal Type *get_map_cell_type(Type *type) {
  2246. i64 size, len;
  2247. i64 elem_size = type_size_of(type);
  2248. map_cell_size_and_len(type, &size, &len);
  2249. if (size == len*elem_size) {
  2250. return type;
  2251. }
  2252. i64 padding = size - len*elem_size;
  2253. GB_ASSERT(padding > 0);
  2254. // Padding exists
  2255. Type *s = alloc_type_struct();
  2256. Scope *scope = create_scope(nullptr, nullptr);
  2257. s->Struct.fields = slice_make<Entity *>(permanent_allocator(), 2);
  2258. s->Struct.fields[0] = alloc_entity_field(scope, make_token_ident("v"), alloc_type_array(type, len), false, 0, EntityState_Resolved);
  2259. s->Struct.fields[1] = alloc_entity_field(scope, make_token_ident("_"), alloc_type_array(t_u8, padding), false, 1, EntityState_Resolved);
  2260. s->Struct.scope = scope;
  2261. wait_signal_set(&s->Struct.fields_wait_signal);
  2262. gb_unused(type_size_of(s));
  2263. return s;
  2264. }
  2265. gb_internal void init_map_internal_debug_types(Type *type) {
  2266. GB_ASSERT(type->kind == Type_Map);
  2267. GB_ASSERT(t_allocator != nullptr);
  2268. if (type->Map.debug_metadata_type != nullptr) return;
  2269. Type *key = type->Map.key;
  2270. Type *value = type->Map.value;
  2271. GB_ASSERT(key != nullptr);
  2272. GB_ASSERT(value != nullptr);
  2273. Type *key_cell = get_map_cell_type(key);
  2274. Type *value_cell = get_map_cell_type(value);
  2275. Type *metadata_type = alloc_type_struct();
  2276. Scope *metadata_scope = create_scope(nullptr, nullptr);
  2277. metadata_type->Struct.fields = slice_make<Entity *>(permanent_allocator(), 5);
  2278. metadata_type->Struct.fields[0] = alloc_entity_field(metadata_scope, make_token_ident("key"), key, false, 0, EntityState_Resolved);
  2279. metadata_type->Struct.fields[1] = alloc_entity_field(metadata_scope, make_token_ident("value"), value, false, 1, EntityState_Resolved);
  2280. metadata_type->Struct.fields[2] = alloc_entity_field(metadata_scope, make_token_ident("hash"), t_uintptr, false, 2, EntityState_Resolved);
  2281. metadata_type->Struct.fields[3] = alloc_entity_field(metadata_scope, make_token_ident("key_cell"), key_cell, false, 3, EntityState_Resolved);
  2282. metadata_type->Struct.fields[4] = alloc_entity_field(metadata_scope, make_token_ident("value_cell"), value_cell, false, 4, EntityState_Resolved);
  2283. metadata_type->Struct.scope = metadata_scope;
  2284. metadata_type->Struct.node = nullptr;
  2285. wait_signal_set(&metadata_type->Struct.fields_wait_signal);
  2286. gb_unused(type_size_of(metadata_type));
  2287. // NOTE(bill): ^struct{key: Key, value: Value, hash: uintptr}
  2288. metadata_type = alloc_type_pointer(metadata_type);
  2289. Scope *scope = create_scope(nullptr, nullptr);
  2290. Type *debug_type = alloc_type_struct();
  2291. debug_type->Struct.fields = slice_make<Entity *>(permanent_allocator(), 3);
  2292. debug_type->Struct.fields[0] = alloc_entity_field(scope, make_token_ident("data"), metadata_type, false, 0, EntityState_Resolved);
  2293. debug_type->Struct.fields[1] = alloc_entity_field(scope, make_token_ident("len"), t_int, false, 1, EntityState_Resolved);
  2294. debug_type->Struct.fields[2] = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, 2, EntityState_Resolved);
  2295. debug_type->Struct.scope = scope;
  2296. debug_type->Struct.node = nullptr;
  2297. wait_signal_set(&debug_type->Struct.fields_wait_signal);
  2298. gb_unused(type_size_of(debug_type));
  2299. type->Map.debug_metadata_type = debug_type;
  2300. }
  2301. gb_internal void init_map_internal_types(Type *type) {
  2302. GB_ASSERT(type->kind == Type_Map);
  2303. GB_ASSERT(t_allocator != nullptr);
  2304. if (type->Map.lookup_result_type != nullptr) return;
  2305. Type *key = type->Map.key;
  2306. Type *value = type->Map.value;
  2307. GB_ASSERT(key != nullptr);
  2308. GB_ASSERT(value != nullptr);
  2309. type->Map.lookup_result_type = make_optional_ok_type(value);
  2310. }
  2311. gb_internal void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
  2312. key = core_type(key);
  2313. if (is_type_cstring(key)) {
  2314. add_package_dependency(ctx, "runtime", "default_hasher_cstring");
  2315. } else if (is_type_string(key)) {
  2316. add_package_dependency(ctx, "runtime", "default_hasher_string");
  2317. } else if (!is_type_polymorphic(key)) {
  2318. if (!is_type_comparable(key)) {
  2319. return;
  2320. }
  2321. if (is_type_simple_compare(key)) {
  2322. add_package_dependency(ctx, "runtime", "default_hasher");
  2323. return;
  2324. }
  2325. if (key->kind == Type_Struct) {
  2326. add_package_dependency(ctx, "runtime", "default_hasher");
  2327. for_array(i, key->Struct.fields) {
  2328. Entity *field = key->Struct.fields[i];
  2329. add_map_key_type_dependencies(ctx, field->type);
  2330. }
  2331. } else if (key->kind == Type_Union) {
  2332. add_package_dependency(ctx, "runtime", "default_hasher");
  2333. for_array(i, key->Union.variants) {
  2334. Type *v = key->Union.variants[i];
  2335. add_map_key_type_dependencies(ctx, v);
  2336. }
  2337. } else if (key->kind == Type_EnumeratedArray) {
  2338. add_package_dependency(ctx, "runtime", "default_hasher");
  2339. add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
  2340. } else if (key->kind == Type_Array) {
  2341. add_package_dependency(ctx, "runtime", "default_hasher");
  2342. add_map_key_type_dependencies(ctx, key->Array.elem);
  2343. }
  2344. }
  2345. }
  2346. gb_internal void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  2347. GB_ASSERT(type->kind == Type_Map);
  2348. ast_node(mt, MapType, node);
  2349. Type *key = check_type(ctx, mt->key);
  2350. Type *value = check_type(ctx, mt->value);
  2351. if (!is_type_valid_for_keys(key)) {
  2352. if (is_type_boolean(key)) {
  2353. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  2354. } else {
  2355. gbString str = type_to_string(key);
  2356. error(node, "Invalid type of a key for a map, got '%s'", str);
  2357. gb_string_free(str);
  2358. }
  2359. }
  2360. if (type_size_of(key) == 0) {
  2361. gbString str = type_to_string(key);
  2362. error(node, "Invalid type of a key for a map of size 0, got '%s'", str);
  2363. gb_string_free(str);
  2364. }
  2365. type->Map.key = key;
  2366. type->Map.value = value;
  2367. add_map_key_type_dependencies(ctx, key);
  2368. init_core_map_type(ctx->checker);
  2369. init_map_internal_types(type);
  2370. }
  2371. gb_internal void check_matrix_type(CheckerContext *ctx, Type **type, Ast *node) {
  2372. ast_node(mt, MatrixType, node);
  2373. Operand row = {};
  2374. Operand column = {};
  2375. i64 row_count = check_array_count(ctx, &row, mt->row_count);
  2376. i64 column_count = check_array_count(ctx, &column, mt->column_count);
  2377. Type *generic_row = nullptr;
  2378. Type *generic_column = nullptr;
  2379. if (row.mode == Addressing_Type && row.type->kind == Type_Generic) {
  2380. generic_row = row.type;
  2381. }
  2382. if (column.mode == Addressing_Type && column.type->kind == Type_Generic) {
  2383. generic_column = column.type;
  2384. }
  2385. if (generic_row == nullptr && row_count < MATRIX_ELEMENT_COUNT_MIN) {
  2386. gbString s = expr_to_string(row.expr);
  2387. error(row.expr, "Invalid matrix row count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2388. gb_string_free(s);
  2389. }
  2390. if (generic_column == nullptr && column_count < MATRIX_ELEMENT_COUNT_MIN) {
  2391. gbString s = expr_to_string(column.expr);
  2392. error(column.expr, "Invalid matrix column count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2393. gb_string_free(s);
  2394. }
  2395. if ((generic_row == nullptr && generic_column == nullptr) && row_count*column_count > MATRIX_ELEMENT_COUNT_MAX) {
  2396. i64 element_count = row_count*column_count;
  2397. error(column.expr, "Matrix types are limited to a maximum of %d elements, got %lld", MATRIX_ELEMENT_COUNT_MAX, cast(long long)element_count);
  2398. }
  2399. Type *elem = check_type_expr(ctx, mt->elem, nullptr);
  2400. if (!is_type_valid_for_matrix_elems(elem)) {
  2401. if (elem == t_typeid) {
  2402. Entity *e = entity_of_node(mt->elem);
  2403. if (e && e->kind == Entity_TypeName && e->TypeName.is_type_alias) {
  2404. // HACK TODO(bill): This is to allow polymorphic parameters for matrix elements
  2405. // proc($T: typeid) -> matrix[2, 2]T
  2406. //
  2407. // THIS IS NEEDS TO BE FIXED AND NOT USE THIS HACK
  2408. goto type_assign;
  2409. }
  2410. }
  2411. gbString s = type_to_string(elem);
  2412. error(column.expr, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
  2413. gb_string_free(s);
  2414. }
  2415. type_assign:;
  2416. *type = alloc_type_matrix(elem, row_count, column_count, generic_row, generic_column, mt->is_row_major);
  2417. return;
  2418. }
  2419. struct SoaTypeWorkerData {
  2420. CheckerContext ctx;
  2421. Type * type;
  2422. bool wait_to_finish;
  2423. };
  2424. gb_internal bool complete_soa_type(Checker *checker, Type *t, bool wait_to_finish) {
  2425. Type *original_type = t;
  2426. gb_unused(original_type);
  2427. t = base_type(t);
  2428. if (t == nullptr || !is_type_soa_struct(t)) {
  2429. return true;
  2430. }
  2431. MUTEX_GUARD(&t->Struct.soa_mutex);
  2432. if (t->Struct.fields_wait_signal.futex.load()) {
  2433. return true;
  2434. }
  2435. isize field_count = 0;
  2436. i32 extra_field_count = 0;
  2437. switch (t->Struct.soa_kind) {
  2438. case StructSoa_Fixed: extra_field_count = 0; break;
  2439. case StructSoa_Slice: extra_field_count = 1; break;
  2440. case StructSoa_Dynamic: extra_field_count = 3; break;
  2441. }
  2442. Scope *scope = t->Struct.scope;
  2443. i64 soa_count = t->Struct.soa_count;
  2444. Type *elem = t->Struct.soa_elem;
  2445. Type *old_struct = base_type(elem);
  2446. GB_ASSERT(old_struct->kind == Type_Struct);
  2447. if (wait_to_finish) {
  2448. wait_signal_until_available(&old_struct->Struct.fields_wait_signal);
  2449. } else {
  2450. GB_ASSERT(old_struct->Struct.fields_wait_signal.futex.load());
  2451. }
  2452. field_count = old_struct->Struct.fields.count;
  2453. t->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2454. t->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2455. auto const &add_entity = [](Scope *scope, Entity *entity) {
  2456. String name = entity->token.string;
  2457. if (!is_blank_ident(name)) {
  2458. Entity *ie = scope_insert(scope, entity);
  2459. if (ie != nullptr) {
  2460. redeclaration_error(name, entity, ie);
  2461. }
  2462. }
  2463. };
  2464. for_array(i, old_struct->Struct.fields) {
  2465. Entity *old_field = old_struct->Struct.fields[i];
  2466. if (old_field->kind == Entity_Variable) {
  2467. Type *field_type = nullptr;
  2468. if (t->Struct.soa_kind == StructSoa_Fixed) {
  2469. GB_ASSERT(soa_count >= 0);
  2470. field_type = alloc_type_array(old_field->type, soa_count);
  2471. } else {
  2472. field_type = alloc_type_multi_pointer(old_field->type);
  2473. }
  2474. Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index);
  2475. t->Struct.fields[i] = new_field;
  2476. add_entity(scope, new_field);
  2477. new_field->flags |= EntityFlag_Used;
  2478. if (t->Struct.soa_kind != StructSoa_Fixed) {
  2479. new_field->flags |= EntityFlag_SoaPtrField;
  2480. }
  2481. } else {
  2482. t->Struct.fields[i] = old_field;
  2483. }
  2484. t->Struct.tags[i] = old_struct->Struct.tags[i];
  2485. }
  2486. if (t->Struct.soa_kind != StructSoa_Fixed) {
  2487. Entity *len_field = alloc_entity_field(scope, make_token_ident("__$len"), t_int, false, cast(i32)field_count+0);
  2488. t->Struct.fields[field_count+0] = len_field;
  2489. add_entity(scope, len_field);
  2490. len_field->flags |= EntityFlag_Used;
  2491. if (t->Struct.soa_kind == StructSoa_Dynamic) {
  2492. Entity *cap_field = alloc_entity_field(scope, make_token_ident("__$cap"), t_int, false, cast(i32)field_count+1);
  2493. t->Struct.fields[field_count+1] = cap_field;
  2494. add_entity(scope, cap_field);
  2495. cap_field->flags |= EntityFlag_Used;
  2496. init_mem_allocator(checker);
  2497. Entity *allocator_field = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, cast(i32)field_count+2);
  2498. t->Struct.fields[field_count+2] = allocator_field;
  2499. add_entity(scope, allocator_field);
  2500. allocator_field->flags |= EntityFlag_Used;
  2501. }
  2502. }
  2503. // add_type_info_type(ctx, original_type);
  2504. wait_signal_set(&t->Struct.fields_wait_signal);
  2505. return true;
  2506. }
  2507. gb_internal WORKER_TASK_PROC(complete_soa_type_worker) {
  2508. SoaTypeWorkerData *wd = cast(SoaTypeWorkerData *)data;
  2509. complete_soa_type(wd->ctx.checker, wd->type, wd->wait_to_finish);
  2510. return 0;
  2511. }
  2512. 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) {
  2513. Type *bt_elem = base_type(elem);
  2514. bool is_polymorphic = is_type_polymorphic(elem);
  2515. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2516. gbString str = type_to_string(elem);
  2517. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2518. gb_string_free(str);
  2519. return alloc_type_array(elem, count, generic_type);
  2520. }
  2521. Type * soa_struct = nullptr;
  2522. Scope *scope = nullptr;
  2523. bool is_complete = false;
  2524. isize field_count = 0;
  2525. i32 extra_field_count = 0;
  2526. switch (soa_kind) {
  2527. case StructSoa_Fixed: extra_field_count = 0; break;
  2528. case StructSoa_Slice: extra_field_count = 1; break;
  2529. case StructSoa_Dynamic: extra_field_count = 3; break;
  2530. }
  2531. soa_struct = alloc_type_struct();
  2532. soa_struct->Struct.soa_kind = soa_kind;
  2533. soa_struct->Struct.soa_elem = elem;
  2534. soa_struct->Struct.is_polymorphic = is_polymorphic;
  2535. soa_struct->Struct.node = array_typ_expr;
  2536. if (count > I32_MAX) {
  2537. count = I32_MAX;
  2538. error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  2539. }
  2540. soa_struct->Struct.soa_count = cast(i32)count;
  2541. scope = create_scope(ctx->info, ctx->scope);
  2542. soa_struct->Struct.scope = scope;
  2543. if (elem && elem->kind == Type_Named) {
  2544. add_declaration_dependency(ctx, elem->Named.type_name);
  2545. }
  2546. if (is_polymorphic) {
  2547. field_count = 0;
  2548. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2549. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2550. soa_struct->Struct.soa_count = 0;
  2551. is_complete = true;
  2552. } else if (is_type_array(elem)) {
  2553. Type *old_array = base_type(elem);
  2554. field_count = cast(isize)old_array->Array.count;
  2555. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2556. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2557. string_map_init(&scope->elements, 8);
  2558. String params_xyzw[4] = {
  2559. str_lit("x"),
  2560. str_lit("y"),
  2561. str_lit("z"),
  2562. str_lit("w")
  2563. };
  2564. for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
  2565. Type *field_type = nullptr;
  2566. if (soa_kind == StructSoa_Fixed) {
  2567. GB_ASSERT(count >= 0);
  2568. field_type = alloc_type_array(old_array->Array.elem, count);
  2569. } else {
  2570. field_type = alloc_type_multi_pointer(old_array->Array.elem);
  2571. }
  2572. Token token = {};
  2573. token.string = params_xyzw[i];
  2574. Entity *new_field = alloc_entity_field(scope, token, field_type, false, cast(i32)i);
  2575. soa_struct->Struct.fields[i] = new_field;
  2576. add_entity(ctx, scope, nullptr, new_field);
  2577. add_entity_use(ctx, nullptr, new_field);
  2578. if (soa_kind != StructSoa_Fixed) {
  2579. new_field->flags |= EntityFlag_SoaPtrField;
  2580. }
  2581. }
  2582. is_complete = true;
  2583. } else {
  2584. GB_ASSERT(is_type_struct(elem));
  2585. Type *old_struct = base_type(elem);
  2586. if (old_struct->Struct.fields_wait_signal.futex.load()) {
  2587. field_count = old_struct->Struct.fields.count;
  2588. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2589. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2590. for_array(i, old_struct->Struct.fields) {
  2591. Entity *old_field = old_struct->Struct.fields[i];
  2592. if (old_field->kind == Entity_Variable) {
  2593. Type *field_type = nullptr;
  2594. if (soa_kind == StructSoa_Fixed) {
  2595. GB_ASSERT(count >= 0);
  2596. field_type = alloc_type_array(old_field->type, count);
  2597. } else {
  2598. field_type = alloc_type_multi_pointer(old_field->type);
  2599. }
  2600. Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index);
  2601. soa_struct->Struct.fields[i] = new_field;
  2602. add_entity(ctx, scope, nullptr, new_field);
  2603. add_entity_use(ctx, nullptr, new_field);
  2604. if (soa_kind != StructSoa_Fixed) {
  2605. new_field->flags |= EntityFlag_SoaPtrField;
  2606. }
  2607. } else {
  2608. soa_struct->Struct.fields[i] = old_field;
  2609. }
  2610. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2611. }
  2612. is_complete = true;
  2613. }
  2614. }
  2615. if (is_complete && soa_kind != StructSoa_Fixed) {
  2616. Entity *len_field = alloc_entity_field(scope, make_token_ident("__$len"), t_int, false, cast(i32)field_count+0);
  2617. soa_struct->Struct.fields[field_count+0] = len_field;
  2618. add_entity(ctx, scope, nullptr, len_field);
  2619. add_entity_use(ctx, nullptr, len_field);
  2620. if (soa_kind == StructSoa_Dynamic) {
  2621. Entity *cap_field = alloc_entity_field(scope, make_token_ident("__$cap"), t_int, false, cast(i32)field_count+1);
  2622. soa_struct->Struct.fields[field_count+1] = cap_field;
  2623. add_entity(ctx, scope, nullptr, cap_field);
  2624. add_entity_use(ctx, nullptr, cap_field);
  2625. init_mem_allocator(ctx->checker);
  2626. Entity *allocator_field = alloc_entity_field(scope, make_token_ident("allocator"), t_allocator, false, cast(i32)field_count+2);
  2627. soa_struct->Struct.fields[field_count+2] = allocator_field;
  2628. add_entity(ctx, scope, nullptr, allocator_field);
  2629. add_entity_use(ctx, nullptr, allocator_field);
  2630. }
  2631. }
  2632. Token token = {};
  2633. token.string = str_lit("Base_Type");
  2634. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2635. add_entity(ctx, scope, nullptr, base_type_entity);
  2636. if (is_complete) {
  2637. add_type_info_type(ctx, soa_struct);
  2638. wait_signal_set(&soa_struct->Struct.fields_wait_signal);
  2639. } else {
  2640. SoaTypeWorkerData *wd = gb_alloc_item(permanent_allocator(), SoaTypeWorkerData);
  2641. wd->ctx = *ctx;
  2642. wd->type = soa_struct;
  2643. wd->wait_to_finish = true;
  2644. mpsc_enqueue(&ctx->checker->soa_types_to_complete, soa_struct);
  2645. thread_pool_add_task(complete_soa_type_worker, wd);
  2646. }
  2647. return soa_struct;
  2648. }
  2649. gb_internal Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type) {
  2650. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, count, generic_type, StructSoa_Fixed);
  2651. }
  2652. gb_internal Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2653. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Slice);
  2654. }
  2655. gb_internal Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2656. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Dynamic);
  2657. }
  2658. gb_internal void check_array_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2659. ast_node(at, ArrayType, e);
  2660. if (at->count != nullptr) {
  2661. Operand o = {};
  2662. i64 count = check_array_count(ctx, &o, at->count);
  2663. Type *generic_type = nullptr;
  2664. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2665. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2666. generic_type = o.type;
  2667. } else if (o.mode == Addressing_Type && is_type_enum(o.type)) {
  2668. Type *index = o.type;
  2669. Type *bt = base_type(index);
  2670. GB_ASSERT(bt->kind == Type_Enum);
  2671. Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, bt->Enum.fields.count, Token_Invalid);
  2672. bool is_sparse = false;
  2673. if (at->tag != nullptr) {
  2674. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2675. String name = at->tag->BasicDirective.name.string;
  2676. if (name == "sparse") {
  2677. is_sparse = true;
  2678. } else {
  2679. error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
  2680. }
  2681. }
  2682. if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count) {
  2683. ERROR_BLOCK();
  2684. error(e, "Non-contiguous enumeration used as an index in an enumerated array");
  2685. long long ea_count = cast(long long)t->EnumeratedArray.count;
  2686. long long enum_count = cast(long long)bt->Enum.fields.count;
  2687. error_line("\tenumerated array length: %lld\n", ea_count);
  2688. error_line("\tenum field count: %lld\n", enum_count);
  2689. error_line("\tSuggestion: prepend #sparse to the enumerated array to allow for non-contiguous elements\n");
  2690. if (2*enum_count < ea_count) {
  2691. 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");
  2692. error_line("\t this warning will be removed if #sparse is applied\n");
  2693. }
  2694. }
  2695. t->EnumeratedArray.is_sparse = is_sparse;
  2696. *type = t;
  2697. return;
  2698. }
  2699. if (count < 0) {
  2700. error(at->count, "? can only be used in conjuction with compound literals");
  2701. count = 0;
  2702. }
  2703. if (at->tag != nullptr) {
  2704. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2705. String name = at->tag->BasicDirective.name.string;
  2706. if (name == "soa") {
  2707. *type = make_soa_struct_fixed(ctx, e, at->elem, elem, count, generic_type);
  2708. } else if (name == "simd") {
  2709. if (!is_type_valid_vector_elem(elem) && !is_type_polymorphic(elem)) {
  2710. gbString str = type_to_string(elem);
  2711. error(at->elem, "Invalid element type for #simd, expected an integer, float, or boolean with no specific endianness, got '%s'", str);
  2712. gb_string_free(str);
  2713. *type = alloc_type_array(elem, count, generic_type);
  2714. return;
  2715. }
  2716. if (generic_type != nullptr) {
  2717. // Ignore
  2718. } else if (count < 1 || !is_power_of_two(count)) {
  2719. error(at->count, "Invalid length for #simd, expected a power of two length, got '%lld'", cast(long long)count);
  2720. *type = alloc_type_array(elem, count, generic_type);
  2721. return;
  2722. }
  2723. *type = alloc_type_simd_vector(count, elem, generic_type);
  2724. if (count > SIMD_ELEMENT_COUNT_MAX) {
  2725. error(at->count, "#simd support a maximum element count of %d, got %lld", SIMD_ELEMENT_COUNT_MAX, cast(long long)count);
  2726. }
  2727. } else {
  2728. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2729. *type = alloc_type_array(elem, count, generic_type);
  2730. }
  2731. } else {
  2732. *type = alloc_type_array(elem, count, generic_type);
  2733. }
  2734. } else {
  2735. Type *elem = check_type(ctx, at->elem);
  2736. if (at->tag != nullptr) {
  2737. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2738. String name = at->tag->BasicDirective.name.string;
  2739. if (name == "soa") {
  2740. *type = make_soa_struct_slice(ctx, e, at->elem, elem);
  2741. } else {
  2742. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2743. *type = alloc_type_slice(elem);
  2744. }
  2745. } else {
  2746. *type = alloc_type_slice(elem);
  2747. }
  2748. }
  2749. }
  2750. gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2751. GB_ASSERT_NOT_NULL(type);
  2752. if (e == nullptr) {
  2753. *type = t_invalid;
  2754. return true;
  2755. }
  2756. switch (e->kind) {
  2757. case_ast_node(i, Ident, e);
  2758. Operand o = {};
  2759. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  2760. gb_unused(entity);
  2761. gbString err_str = nullptr;
  2762. defer (gb_string_free(err_str));
  2763. switch (o.mode) {
  2764. case Addressing_Invalid:
  2765. break;
  2766. case Addressing_Type: {
  2767. *type = o.type;
  2768. if (!ctx->in_polymorphic_specialization) {
  2769. Type *t = base_type(o.type);
  2770. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  2771. err_str = expr_to_string(e);
  2772. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  2773. return true;
  2774. }
  2775. }
  2776. return true;
  2777. }
  2778. case Addressing_NoValue:
  2779. err_str = expr_to_string(e);
  2780. error(e, "'%s' used as a type", err_str);
  2781. break;
  2782. default:
  2783. err_str = expr_to_string(e);
  2784. error(e, "'%s' used as a type when not a type", err_str);
  2785. break;
  2786. }
  2787. case_end;
  2788. case_ast_node(ht, HelperType, e);
  2789. return check_type_internal(ctx, ht->type, type, named_type);
  2790. case_end;
  2791. case_ast_node(dt, DistinctType, e);
  2792. error(e, "Invalid use of a distinct type");
  2793. // NOTE(bill): Treat it as a HelperType to remove errors
  2794. return check_type_internal(ctx, dt->type, type, named_type);
  2795. case_end;
  2796. case_ast_node(tt, TypeidType, e);
  2797. e->tav.mode = Addressing_Type;
  2798. e->tav.type = t_typeid;
  2799. *type = t_typeid;
  2800. set_base_type(named_type, *type);
  2801. return true;
  2802. case_end;
  2803. case_ast_node(pt, PolyType, e);
  2804. Ast *ident = pt->type;
  2805. if (ident->kind != Ast_Ident) {
  2806. error(ident, "Expected an identifier after the $");
  2807. *type = t_invalid;
  2808. return false;
  2809. }
  2810. Token token = ident->Ident.token;
  2811. Type *specific = nullptr;
  2812. if (pt->specialization != nullptr) {
  2813. CheckerContext c = *ctx;
  2814. c.in_polymorphic_specialization = true;
  2815. Ast *s = pt->specialization;
  2816. specific = check_type(&c, s);
  2817. }
  2818. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2819. if (ctx->allow_polymorphic_types) {
  2820. Scope *ps = ctx->polymorphic_scope;
  2821. Scope *s = ctx->scope;
  2822. Scope *entity_scope = s;
  2823. if (ps != nullptr && ps != s) {
  2824. // TODO(bill): Is this check needed?
  2825. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2826. entity_scope = ps;
  2827. }
  2828. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2829. t->Generic.entity = e;
  2830. e->TypeName.is_type_alias = true;
  2831. e->state = EntityState_Resolved;
  2832. add_entity(ctx, ps, ident, e);
  2833. add_entity(ctx, s, ident, e);
  2834. } else {
  2835. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2836. *type = t_invalid;
  2837. return false;
  2838. }
  2839. *type = t;
  2840. set_base_type(named_type, *type);
  2841. return true;
  2842. case_end;
  2843. case_ast_node(se, SelectorExpr, e);
  2844. Operand o = {};
  2845. check_selector(ctx, &o, e, nullptr);
  2846. gbString err_str;
  2847. switch (o.mode) {
  2848. case Addressing_Invalid:
  2849. break;
  2850. case Addressing_Type:
  2851. GB_ASSERT(o.type != nullptr);
  2852. *type = o.type;
  2853. return true;
  2854. case Addressing_NoValue:
  2855. err_str = expr_to_string(e);
  2856. error(e, "'%s' used as a type", err_str);
  2857. gb_string_free(err_str);
  2858. break;
  2859. default:
  2860. err_str = expr_to_string(e);
  2861. error(e, "'%s' is not a type", err_str);
  2862. gb_string_free(err_str);
  2863. break;
  2864. }
  2865. case_end;
  2866. case_ast_node(pe, ParenExpr, e);
  2867. if (pe->expr == nullptr) {
  2868. error(e, "Expected an expression or type within the parentheses");
  2869. *type = t_invalid;
  2870. return true;
  2871. }
  2872. *type = check_type_expr(ctx, pe->expr, named_type);
  2873. set_base_type(named_type, *type);
  2874. return true;
  2875. case_end;
  2876. case_ast_node(ue, UnaryExpr, e);
  2877. switch (ue->op.kind) {
  2878. case Token_Pointer:
  2879. {
  2880. Type *elem = check_type(ctx, ue->expr);
  2881. *type = alloc_type_pointer(elem);
  2882. set_base_type(named_type, *type);
  2883. return true;
  2884. }
  2885. }
  2886. case_end;
  2887. case_ast_node(pt, PointerType, e);
  2888. CheckerContext c = *ctx;
  2889. c.type_path = new_checker_type_path();
  2890. defer (destroy_checker_type_path(c.type_path));
  2891. Type *elem = t_invalid;
  2892. Operand o = {};
  2893. if (unparen_expr(pt->type) == nullptr) {
  2894. error(e, "Invalid pointer type");
  2895. return false;
  2896. }
  2897. check_expr_or_type(&c, &o, pt->type);
  2898. if (o.mode != Addressing_Invalid && o.mode != Addressing_Type) {
  2899. if (o.mode == Addressing_Variable) {
  2900. gbString s = expr_to_string(pt->type);
  2901. error(e, "^ is used for pointer types, did you mean '&%s'?", s);
  2902. gb_string_free(s);
  2903. } else if (is_type_pointer(o.type)) {
  2904. gbString s = expr_to_string(pt->type);
  2905. error(e, "^ is used for pointer types, did you mean a dereference: '%s^'?", s);
  2906. gb_string_free(s);
  2907. } else {
  2908. // NOTE(bill): call check_type_expr again to get a consistent error message
  2909. elem = check_type_expr(&c, pt->type, nullptr);
  2910. }
  2911. } else {
  2912. elem = o.type;
  2913. }
  2914. if (pt->tag != nullptr) {
  2915. GB_ASSERT(pt->tag->kind == Ast_BasicDirective);
  2916. String name = pt->tag->BasicDirective.name.string;
  2917. if (name == "soa") {
  2918. // TODO(bill): generic #soa pointers
  2919. if (is_type_soa_struct(elem)) {
  2920. *type = alloc_type_soa_pointer(elem);
  2921. } else {
  2922. error(pt->tag, "#soa pointers require an #soa record type as the element");
  2923. *type = alloc_type_pointer(elem);
  2924. }
  2925. } else {
  2926. error(pt->tag, "Invalid tag applied to pointer, got #%.*s", LIT(name));
  2927. *type = alloc_type_pointer(elem);
  2928. }
  2929. } else {
  2930. *type = alloc_type_pointer(elem);
  2931. }
  2932. set_base_type(named_type, *type);
  2933. return true;
  2934. case_end;
  2935. case_ast_node(pt, MultiPointerType, e);
  2936. *type = alloc_type_multi_pointer(check_type(ctx, pt->type));
  2937. set_base_type(named_type, *type);
  2938. return true;
  2939. case_end;
  2940. case_ast_node(rt, RelativeType, e);
  2941. GB_ASSERT(rt->tag->kind == Ast_CallExpr);
  2942. ast_node(ce, CallExpr, rt->tag);
  2943. Type *base_integer = nullptr;
  2944. if (ce->args.count != 1) {
  2945. error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count);
  2946. } else {
  2947. base_integer = check_type(ctx, ce->args[0]);
  2948. if (!is_type_integer(base_integer)) {
  2949. error(rt->type, "#relative base types must be an integer");
  2950. base_integer = nullptr;
  2951. } else if (type_size_of(base_integer) > 64) {
  2952. error(rt->type, "#relative base integer types be less than or equal to 64-bits");
  2953. base_integer = nullptr;
  2954. }
  2955. }
  2956. Type *relative_type = nullptr;
  2957. Type *base_type = check_type(ctx, rt->type);
  2958. if (!is_type_pointer(base_type) && !is_type_multi_pointer(base_type)) {
  2959. error(rt->type, "#relative types can only be a pointer or multi-pointer");
  2960. relative_type = base_type;
  2961. } else if (base_integer == nullptr) {
  2962. relative_type = base_type;
  2963. } else {
  2964. if (is_type_pointer(base_type)) {
  2965. relative_type = alloc_type_relative_pointer(base_type, base_integer);
  2966. } else if (is_type_multi_pointer(base_type)) {
  2967. relative_type = alloc_type_relative_multi_pointer(base_type, base_integer);
  2968. }
  2969. }
  2970. GB_ASSERT(relative_type != nullptr);
  2971. *type = relative_type;
  2972. set_base_type(named_type, *type);
  2973. return true;
  2974. case_end;
  2975. case_ast_node(at, ArrayType, e);
  2976. check_array_type_internal(ctx, e, type, named_type);
  2977. set_base_type(named_type, *type);
  2978. return true;
  2979. case_end;
  2980. case_ast_node(dat, DynamicArrayType, e);
  2981. Type *elem = check_type(ctx, dat->elem);
  2982. if (dat->tag != nullptr) {
  2983. GB_ASSERT(dat->tag->kind == Ast_BasicDirective);
  2984. String name = dat->tag->BasicDirective.name.string;
  2985. if (name == "soa") {
  2986. *type = make_soa_struct_dynamic_array(ctx, e, dat->elem, elem);
  2987. } else {
  2988. error(dat->tag, "Invalid tag applied to dynamic array, got #%.*s", LIT(name));
  2989. *type = alloc_type_dynamic_array(elem);
  2990. }
  2991. } else {
  2992. *type = alloc_type_dynamic_array(elem);
  2993. }
  2994. set_base_type(named_type, *type);
  2995. return true;
  2996. case_end;
  2997. case_ast_node(st, StructType, e);
  2998. CheckerContext c = *ctx;
  2999. c.in_polymorphic_specialization = false;
  3000. c.type_level += 1;
  3001. *type = alloc_type_struct();
  3002. set_base_type(named_type, *type);
  3003. check_open_scope(&c, e);
  3004. check_struct_type(&c, *type, e, nullptr, named_type);
  3005. check_close_scope(&c);
  3006. (*type)->Struct.node = e;
  3007. return true;
  3008. case_end;
  3009. case_ast_node(ut, UnionType, e);
  3010. CheckerContext c = *ctx;
  3011. c.in_polymorphic_specialization = false;
  3012. c.type_level += 1;
  3013. *type = alloc_type_union();
  3014. set_base_type(named_type, *type);
  3015. check_open_scope(&c, e);
  3016. check_union_type(&c, *type, e, nullptr, named_type);
  3017. check_close_scope(&c);
  3018. (*type)->Union.node = e;
  3019. return true;
  3020. case_end;
  3021. case_ast_node(et, EnumType, e);
  3022. bool ips = ctx->in_polymorphic_specialization;
  3023. defer (ctx->in_polymorphic_specialization = ips);
  3024. ctx->in_polymorphic_specialization = false;
  3025. ctx->in_enum_type = true;
  3026. *type = alloc_type_enum();
  3027. set_base_type(named_type, *type);
  3028. check_open_scope(ctx, e);
  3029. check_enum_type(ctx, *type, named_type, e);
  3030. check_close_scope(ctx);
  3031. (*type)->Enum.node = e;
  3032. ctx->in_enum_type = false;
  3033. return true;
  3034. case_end;
  3035. case_ast_node(bs, BitSetType, e);
  3036. *type = alloc_type_bit_set();
  3037. set_base_type(named_type, *type);
  3038. check_bit_set_type(ctx, *type, named_type, e);
  3039. return true;
  3040. case_end;
  3041. case_ast_node(bf, BitFieldType, e);
  3042. bool ips = ctx->in_polymorphic_specialization;
  3043. defer (ctx->in_polymorphic_specialization = ips);
  3044. ctx->in_polymorphic_specialization = false;
  3045. *type = alloc_type_bit_field();
  3046. set_base_type(named_type, *type);
  3047. check_open_scope(ctx, e);
  3048. check_bit_field_type(ctx, *type, named_type, e);
  3049. check_close_scope(ctx);
  3050. (*type)->BitField.node = e;
  3051. return true;
  3052. case_end;
  3053. case_ast_node(pt, ProcType, e);
  3054. bool ips = ctx->in_polymorphic_specialization;
  3055. defer (ctx->in_polymorphic_specialization = ips);
  3056. ctx->in_polymorphic_specialization = false;
  3057. *type = alloc_type(Type_Proc);
  3058. set_base_type(named_type, *type);
  3059. check_open_scope(ctx, e);
  3060. check_procedure_type(ctx, *type, e);
  3061. check_close_scope(ctx);
  3062. return true;
  3063. case_end;
  3064. case_ast_node(mt, MapType, e);
  3065. bool ips = ctx->in_polymorphic_specialization;
  3066. defer (ctx->in_polymorphic_specialization = ips);
  3067. ctx->in_polymorphic_specialization = false;
  3068. *type = alloc_type(Type_Map);
  3069. set_base_type(named_type, *type);
  3070. check_map_type(ctx, *type, e);
  3071. return true;
  3072. case_end;
  3073. case_ast_node(ce, CallExpr, e);
  3074. Operand o = {};
  3075. check_expr_or_type(ctx, &o, e);
  3076. if (o.mode == Addressing_Type) {
  3077. *type = o.type;
  3078. set_base_type(named_type, *type);
  3079. return true;
  3080. }
  3081. case_end;
  3082. case_ast_node(te, TernaryIfExpr, e);
  3083. Operand o = {};
  3084. check_expr_or_type(ctx, &o, e);
  3085. if (o.mode == Addressing_Type) {
  3086. *type = o.type;
  3087. set_base_type(named_type, *type);
  3088. return true;
  3089. }
  3090. case_end;
  3091. case_ast_node(te, TernaryWhenExpr, e);
  3092. Operand o = {};
  3093. check_expr_or_type(ctx, &o, e);
  3094. if (o.mode == Addressing_Type) {
  3095. *type = o.type;
  3096. set_base_type(named_type, *type);
  3097. return true;
  3098. }
  3099. case_end;
  3100. case_ast_node(mt, MatrixType, e);
  3101. check_matrix_type(ctx, type, e);
  3102. set_base_type(named_type, *type);
  3103. return true;
  3104. case_end;
  3105. }
  3106. *type = t_invalid;
  3107. return false;
  3108. }
  3109. gb_internal Type *check_type(CheckerContext *ctx, Ast *e) {
  3110. CheckerContext c = *ctx;
  3111. c.type_path = new_checker_type_path();
  3112. defer (destroy_checker_type_path(c.type_path));
  3113. return check_type_expr(&c, e, nullptr);
  3114. }
  3115. gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  3116. Type *type = nullptr;
  3117. bool ok = check_type_internal(ctx, e, &type, named_type);
  3118. if (!ok) {
  3119. gbString err_str = expr_to_string(e);
  3120. defer (gb_string_free(err_str));
  3121. begin_error_block();
  3122. error(e, "'%s' is not a type", err_str);
  3123. type = t_invalid;
  3124. // NOTE(bill): Check for common mistakes from C programmers
  3125. // e.g. T[] and T[N]
  3126. // e.g. *T
  3127. Ast *node = unparen_expr(e);
  3128. if (node && node->kind == Ast_IndexExpr) {
  3129. gbString index_str = nullptr;
  3130. if (node->IndexExpr.index) {
  3131. index_str = expr_to_string(node->IndexExpr.index);
  3132. }
  3133. defer (gb_string_free(index_str));
  3134. gbString type_str = expr_to_string(node->IndexExpr.expr);
  3135. defer (gb_string_free(type_str));
  3136. error_line("\tSuggestion: Did you mean '[%s]%s'?\n", index_str ? index_str : "", type_str);
  3137. end_error_block();
  3138. // NOTE(bill): Minimize error propagation of bad array syntax by treating this like a type
  3139. if (node->IndexExpr.expr != nullptr) {
  3140. Ast *pseudo_array_expr = ast_array_type(e->file(), ast_token(node->IndexExpr.expr), node->IndexExpr.index, node->IndexExpr.expr);
  3141. check_array_type_internal(ctx, pseudo_array_expr, &type, nullptr);
  3142. }
  3143. } else if (node && node->kind == Ast_UnaryExpr && node->UnaryExpr.op.kind == Token_Mul) {
  3144. gbString type_str = expr_to_string(node->UnaryExpr.expr);
  3145. defer (gb_string_free(type_str));
  3146. error_line("\tSuggestion: Did you mean '^%s'?\n", type_str);
  3147. end_error_block();
  3148. // NOTE(bill): Minimize error propagation of bad array syntax by treating this like a type
  3149. if (node->UnaryExpr.expr != nullptr) {
  3150. Ast *pseudo_pointer_expr = ast_pointer_type(e->file(), ast_token(node->UnaryExpr.expr), node->UnaryExpr.expr);
  3151. return check_type_expr(ctx, pseudo_pointer_expr, named_type);
  3152. }
  3153. } else {
  3154. end_error_block();
  3155. }
  3156. }
  3157. if (type == nullptr) {
  3158. type = t_invalid;
  3159. }
  3160. if (type->kind == Type_Named &&
  3161. type->Named.base == nullptr) {
  3162. // IMPORTANT TODO(bill): Is this a serious error?!
  3163. #if 0
  3164. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  3165. #endif
  3166. type->Named.base = t_invalid;
  3167. }
  3168. if (is_type_polymorphic(type)) {
  3169. type->flags |= TypeFlag_Polymorphic;
  3170. } else if (is_type_polymorphic(type, true)) {
  3171. type->flags |= TypeFlag_PolySpecialized;
  3172. }
  3173. #if 0
  3174. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  3175. gbString str = type_to_string(type);
  3176. error(e, "Invalid use of a polymorphic type '%s'", str);
  3177. gb_string_free(str);
  3178. type = t_invalid;
  3179. }
  3180. #endif
  3181. if (is_type_typed(type)) {
  3182. add_type_and_value(ctx, e, Addressing_Type, type, empty_exact_value);
  3183. } else {
  3184. gbString name = type_to_string(type);
  3185. error(e, "Invalid type definition of %s", name);
  3186. gb_string_free(name);
  3187. type = t_invalid;
  3188. }
  3189. set_base_type(named_type, type);
  3190. check_rtti_type_disallowed(e, type, "Use of a type, %s, which has been disallowed");
  3191. return type;
  3192. }