check_type.cpp 119 KB

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