check_type.cpp 111 KB

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