expr.cpp 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  1. void check_expr (Checker *c, Operand *operand, AstNode *expression);
  2. void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
  3. void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression);
  4. ExprKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint = NULL);
  5. Type * check_type (Checker *c, AstNode *expression, Type *named_type = NULL, CycleChecker *cycle_checker = NULL);
  6. void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
  7. Entity * check_selector (Checker *c, Operand *operand, AstNode *node);
  8. void check_not_tuple (Checker *c, Operand *operand);
  9. b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
  10. void convert_to_typed (Checker *c, Operand *operand, Type *target_type);
  11. gbString expr_to_string (AstNode *expression);
  12. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type, CycleChecker *cycle_checker = NULL);
  13. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  14. void update_expr_type (Checker *c, AstNode *e, Type *type, b32 final);
  15. b32 check_is_assignable_to_using_subtype(Type *dst, Type *src) {
  16. Type *prev_src = src;
  17. // Type *prev_dst = dst;
  18. src = get_base_type(type_deref(src));
  19. // dst = get_base_type(type_deref(dst));
  20. b32 src_is_ptr = src != prev_src;
  21. // b32 dst_is_ptr = dst != prev_dst;
  22. if (is_type_struct(src)) {
  23. for (isize i = 0; i < src->Record.field_count; i++) {
  24. Entity *f = src->Record.fields[i];
  25. if (f->kind == Entity_Variable && f->Variable.anonymous) {
  26. if (are_types_identical(dst, f->type)) {
  27. return true;
  28. }
  29. if (src_is_ptr && is_type_pointer(dst)) {
  30. if (are_types_identical(type_deref(dst), f->type)) {
  31. return true;
  32. }
  33. }
  34. b32 ok = check_is_assignable_to_using_subtype(dst, f->type);
  35. if (ok) {
  36. return true;
  37. }
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argument = false) {
  44. if (operand->mode == Addressing_Invalid ||
  45. type == t_invalid) {
  46. return true;
  47. }
  48. if (operand->mode == Addressing_Builtin) {
  49. return false;
  50. }
  51. Type *s = operand->type;
  52. if (are_types_identical(s, type)) {
  53. return true;
  54. }
  55. Type *src = get_base_type(s);
  56. Type *dst = get_base_type(type);
  57. if (is_type_untyped(src)) {
  58. switch (dst->kind) {
  59. case Type_Basic:
  60. if (operand->mode == Addressing_Constant)
  61. return check_value_is_expressible(c, operand->value, dst, NULL);
  62. if (src->kind == Type_Basic)
  63. return src->Basic.kind == Basic_UntypedBool && is_type_boolean(dst);
  64. break;
  65. case Type_Pointer:
  66. return src->Basic.kind == Basic_UntypedPointer;
  67. }
  68. }
  69. if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
  70. if (is_type_enum(dst) && is_type_enum(src)) {
  71. return are_types_identical(s, type);
  72. }
  73. return true;
  74. }
  75. if (is_type_pointer(dst) && is_type_rawptr(src))
  76. return true;
  77. if (is_type_rawptr(dst) && is_type_pointer(src))
  78. return true;
  79. if (dst->kind == Type_Array && src->kind == Type_Array) {
  80. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  81. return dst->Array.count == src->Array.count;
  82. }
  83. }
  84. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  85. if (are_types_identical(dst->Slice.elem, src->Slice.elem)) {
  86. return true;
  87. }
  88. }
  89. if (is_type_union(dst)) {
  90. for (isize i = 0; i < dst->Record.field_count; i++) {
  91. Entity *f = dst->Record.fields[i];
  92. if (are_types_identical(f->type, s)) {
  93. return true;
  94. }
  95. }
  96. }
  97. if (dst == t_any) {
  98. // NOTE(bill): Anything can cast to `Any`
  99. add_type_info_type(c, s);
  100. return true;
  101. }
  102. if (is_argument) {
  103. // NOTE(bill): Polymorphism for subtyping
  104. if (check_is_assignable_to_using_subtype(type, src)) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. // NOTE(bill): `content_name` is for debugging
  111. // TODO(bill): Maybe allow assignment to tuples?
  112. void check_assignment(Checker *c, Operand *operand, Type *type, String context_name, b32 is_argument = false) {
  113. check_not_tuple(c, operand);
  114. if (operand->mode == Addressing_Invalid)
  115. return;
  116. if (is_type_untyped(operand->type)) {
  117. Type *target_type = type;
  118. if (type == NULL || is_type_any(type)) {
  119. add_type_info_type(c, type);
  120. target_type = default_type(operand->type);
  121. }
  122. convert_to_typed(c, operand, target_type);
  123. if (operand->mode == Addressing_Invalid) {
  124. return;
  125. }
  126. }
  127. if (type != NULL) {
  128. if (!check_is_assignable_to(c, operand, type, is_argument)) {
  129. gbString type_string = type_to_string(type);
  130. gbString op_type_string = type_to_string(operand->type);
  131. gbString expr_str = expr_to_string(operand->expr);
  132. defer (gb_string_free(type_string));
  133. defer (gb_string_free(op_type_string));
  134. defer (gb_string_free(expr_str));
  135. if (operand->mode == Addressing_Builtin) {
  136. // TODO(bill): is this a good enough error message?
  137. error(&c->error_collector, ast_node_token(operand->expr),
  138. "Cannot assign builtin procedure `%s` in %.*s",
  139. expr_str,
  140. LIT(context_name));
  141. } else {
  142. // TODO(bill): is this a good enough error message?
  143. error(&c->error_collector, ast_node_token(operand->expr),
  144. "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
  145. expr_str,
  146. op_type_string,
  147. type_string,
  148. LIT(context_name));
  149. }
  150. operand->mode = Addressing_Invalid;
  151. return;
  152. }
  153. }
  154. }
  155. void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *> *entity_map) {
  156. t = get_base_type(type_deref(t));
  157. gbString str = expr_to_string(node);
  158. defer (gb_string_free(str));
  159. if (t->kind == Type_Record) {
  160. for (isize i = 0; i < t->Record.field_count; i++) {
  161. Entity *f = t->Record.fields[i];
  162. GB_ASSERT(f->kind == Entity_Variable);
  163. String name = f->token.string;
  164. HashKey key = hash_string(name);
  165. Entity **found = map_get(entity_map, key);
  166. if (found != NULL) {
  167. Entity *e = *found;
  168. // TODO(bill): Better type error
  169. error(&c->error_collector, e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
  170. } else {
  171. map_set(entity_map, key, f);
  172. add_entity(c, c->context.scope, NULL, f);
  173. if (f->Variable.anonymous) {
  174. populate_using_entity_map(c, node, f->type, entity_map);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
  181. void check_fields(Checker *c, AstNode *node, AstNodeArray decls,
  182. Entity **fields, isize field_count,
  183. Entity **other_fields, isize other_field_count,
  184. CycleChecker *cycle_checker, String context) {
  185. Map<Entity *> entity_map = {};
  186. map_init(&entity_map, gb_heap_allocator());
  187. defer (map_destroy(&entity_map));
  188. isize other_field_index = 0;
  189. // TODO(bill): Random declarations with DeclInfo
  190. #if 0
  191. Entity *e;
  192. DeclInfo *d;d
  193. check_entity_decl(c, e, d, NULL);
  194. #endif
  195. gb_for_array(decl_index, decls) {
  196. AstNode *decl = decls[decl_index];
  197. if (decl->kind == AstNode_VarDecl) {
  198. ast_node(vd, VarDecl, decl);
  199. if (vd->kind != Declaration_Immutable)
  200. continue;
  201. isize entity_count = gb_array_count(vd->names);
  202. isize entity_index = 0;
  203. Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
  204. gb_for_array(i, vd->values) {
  205. AstNode *name = vd->names[i];
  206. AstNode *value = vd->values[i];
  207. GB_ASSERT(name->kind == AstNode_Ident);
  208. ExactValue v = {ExactValue_Invalid};
  209. Token name_token = name->Ident;
  210. Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, NULL, v);
  211. entities[entity_index++] = e;
  212. check_const_decl(c, e, vd->type, value);
  213. }
  214. isize lhs_count = gb_array_count(vd->names);
  215. isize rhs_count = gb_array_count(vd->values);
  216. // TODO(bill): Better error messages or is this good enough?
  217. if (rhs_count == 0 && vd->type == NULL) {
  218. error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
  219. } else if (lhs_count < rhs_count) {
  220. error(&c->error_collector, ast_node_token(node), "Extra initial expression");
  221. }
  222. gb_for_array(i, vd->names) {
  223. AstNode *name = vd->names[i];
  224. Entity *e = entities[i];
  225. Token name_token = name->Ident;
  226. HashKey key = hash_string(name_token.string);
  227. if (map_get(&entity_map, key) != NULL) {
  228. // TODO(bill): Scope checking already checks the declaration
  229. error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
  230. } else {
  231. map_set(&entity_map, key, e);
  232. other_fields[other_field_index++] = e;
  233. }
  234. add_entity(c, c->context.scope, name, e);
  235. }
  236. } else if (decl->kind == AstNode_TypeDecl) {
  237. ast_node(td, TypeDecl, decl);
  238. Token name_token = td->name->Ident;
  239. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, NULL);
  240. check_type_decl(c, e, td->type, NULL, NULL);
  241. add_entity(c, c->context.scope, td->name, e);
  242. HashKey key = hash_string(name_token.string);
  243. if (map_get(&entity_map, key) != NULL) {
  244. // TODO(bill): Scope checking already checks the declaration
  245. error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
  246. } else {
  247. map_set(&entity_map, key, e);
  248. other_fields[other_field_index++] = e;
  249. }
  250. add_entity_use(&c->info, td->name, e);
  251. }
  252. }
  253. if (node->kind == AstNode_UnionType) {
  254. isize field_index = 0;
  255. fields[field_index++] = make_entity_type_name(c->allocator, c->context.scope, empty_token, NULL);
  256. gb_for_array(decl_index, decls) {
  257. AstNode *decl = decls[decl_index];
  258. if (decl->kind != AstNode_VarDecl) {
  259. continue;
  260. }
  261. ast_node(vd, VarDecl, decl);
  262. if (vd->kind != Declaration_Mutable) {
  263. continue;
  264. }
  265. Type *base_type = check_type(c, vd->type, NULL, cycle_checker);
  266. gb_for_array(name_index, vd->names) {
  267. AstNode *name = vd->names[name_index];
  268. Token name_token = name->Ident;
  269. Type *type = make_type_named(c->allocator, name_token.string, base_type, NULL);
  270. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, type);
  271. type->Named.type_name = e;
  272. add_entity(c, c->context.scope, name, e);
  273. HashKey key = hash_string(name_token.string);
  274. if (map_get(&entity_map, key) != NULL) {
  275. // TODO(bill): Scope checking already checks the declaration
  276. error(&c->error_collector, name_token, "`%.*s` is already declared in this union", LIT(name_token.string));
  277. } else {
  278. map_set(&entity_map, key, e);
  279. fields[field_index++] = e;
  280. }
  281. add_entity_use(&c->info, name, e);
  282. }
  283. }
  284. } else {
  285. isize field_index = 0;
  286. gb_for_array(decl_index, decls) {
  287. AstNode *decl = decls[decl_index];
  288. if (decl->kind != AstNode_VarDecl) {
  289. continue;
  290. }
  291. ast_node(vd, VarDecl, decl);
  292. if (vd->kind != Declaration_Mutable) {
  293. continue;
  294. }
  295. Type *type = check_type(c, vd->type, NULL, cycle_checker);
  296. if (vd->is_using) {
  297. if (gb_array_count(vd->names) > 1) {
  298. error(&c->error_collector, ast_node_token(vd->names[0]),
  299. "Cannot apply `using` to more than one of the same type");
  300. }
  301. }
  302. gb_for_array(name_index, vd->names) {
  303. AstNode *name = vd->names[name_index];
  304. Token name_token = name->Ident;
  305. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, vd->is_using, cast(i32)field_index);
  306. HashKey key = hash_string(name_token.string);
  307. if (map_get(&entity_map, key) != NULL) {
  308. // TODO(bill): Scope checking already checks the declaration
  309. error(&c->error_collector, name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
  310. } else {
  311. map_set(&entity_map, key, e);
  312. fields[field_index] = e;
  313. field_index++;
  314. add_entity(c, c->context.scope, name, e);
  315. }
  316. add_entity_use(&c->info, name, e);
  317. }
  318. if (vd->is_using) {
  319. Type *t = get_base_type(type_deref(type));
  320. if (!is_type_struct(t) && !is_type_raw_union(t)) {
  321. Token name_token = vd->names[0]->Ident;
  322. error(&c->error_collector, name_token, "`using` on a field `%.*s` must be a type", LIT(name_token.string));
  323. continue;
  324. }
  325. populate_using_entity_map(c, node, type, &entity_map);
  326. }
  327. }
  328. }
  329. }
  330. // TODO(bill): Cleanup struct field reordering
  331. gb_global BaseTypeSizes __checker_sizes = {};
  332. gb_global gbAllocator __checker_allocator = {};
  333. GB_COMPARE_PROC(cmp_struct_entity_size) {
  334. // Rule: Biggest to smallest
  335. // if same size, order by order in source
  336. Entity *x = *(Entity **)a;
  337. Entity *y = *(Entity **)b;
  338. GB_ASSERT(x != NULL);
  339. GB_ASSERT(y != NULL);
  340. GB_ASSERT(x->kind == Entity_Variable);
  341. GB_ASSERT(y->kind == Entity_Variable);
  342. i64 xs = type_size_of(__checker_sizes, __checker_allocator, x->type);
  343. i64 ys = type_size_of(__checker_sizes, __checker_allocator, y->type);
  344. if (xs == ys) {
  345. i32 diff = x->Variable.field_index - y->Variable.field_index;
  346. return diff < 0 ? -1 : diff > 0;
  347. }
  348. return xs > ys ? -1 : xs < ys;
  349. }
  350. void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) {
  351. GB_ASSERT(is_type_struct(struct_type));
  352. ast_node(st, StructType, node);
  353. isize field_count = 0;
  354. isize other_field_count = 0;
  355. gb_for_array(decl_index, st->decls) {
  356. AstNode *decl = st->decls[decl_index];
  357. switch (decl->kind) {
  358. case_ast_node(vd, VarDecl, decl);
  359. if (vd->kind == Declaration_Mutable) {
  360. field_count += gb_array_count(vd->names);
  361. } else {
  362. other_field_count += gb_array_count(vd->names);
  363. }
  364. case_end;
  365. case_ast_node(td, TypeDecl, decl);
  366. other_field_count += 1;
  367. case_end;
  368. }
  369. }
  370. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  371. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  372. check_fields(c, node, st->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("struct"));
  373. struct_type->Record.struct_is_packed = st->is_packed;
  374. struct_type->Record.struct_is_ordered = st->is_ordered;
  375. struct_type->Record.fields = fields;
  376. struct_type->Record.fields_in_src_order = fields;
  377. struct_type->Record.field_count = field_count;
  378. struct_type->Record.other_fields = other_fields;
  379. struct_type->Record.other_field_count = other_field_count;
  380. if (!st->is_packed && !st->is_ordered) {
  381. // NOTE(bill): Reorder fields for reduced size/performance
  382. Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, field_count);
  383. for (isize i = 0; i < field_count; i++) {
  384. reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
  385. }
  386. // NOTE(bill): Hacky thing
  387. __checker_sizes = c->sizes;
  388. __checker_allocator = c->allocator;
  389. // TODO(bill): Figure out rules more
  390. // sorting rules
  391. // compound literal order must match source not layout
  392. gb_sort_array(reordered_fields, field_count, cmp_struct_entity_size);
  393. struct_type->Record.fields = reordered_fields;
  394. }
  395. }
  396. void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
  397. GB_ASSERT(is_type_union(union_type));
  398. ast_node(ut, UnionType, node);
  399. isize field_count = 1;
  400. isize other_field_count = 0;
  401. gb_for_array(decl_index, ut->decls) {
  402. AstNode *decl = ut->decls[decl_index];
  403. switch (decl->kind) {
  404. case_ast_node(vd, VarDecl, decl);
  405. if (vd->kind == Declaration_Mutable) {
  406. field_count += gb_array_count(vd->names);
  407. } else {
  408. other_field_count += gb_array_count(vd->names);
  409. }
  410. case_end;
  411. case_ast_node(td, TypeDecl, decl);
  412. other_field_count += 1;
  413. case_end;
  414. }
  415. }
  416. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  417. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  418. check_fields(c, node, ut->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("union"));
  419. union_type->Record.fields = fields;
  420. union_type->Record.field_count = field_count;
  421. union_type->Record.other_fields = other_fields;
  422. union_type->Record.other_field_count = other_field_count;
  423. }
  424. void check_raw_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
  425. GB_ASSERT(node->kind == AstNode_RawUnionType);
  426. GB_ASSERT(is_type_raw_union(union_type));
  427. ast_node(ut, RawUnionType, node);
  428. isize field_count = 0;
  429. isize other_field_count = 0;
  430. gb_for_array(decl_index, ut->decls) {
  431. AstNode *decl = ut->decls[decl_index];
  432. switch (decl->kind) {
  433. case_ast_node(vd, VarDecl, decl);
  434. if (vd->kind == Declaration_Mutable) {
  435. field_count += gb_array_count(vd->names);
  436. } else {
  437. other_field_count += gb_array_count(vd->names);
  438. }
  439. case_end;
  440. case_ast_node(td, TypeDecl, decl);
  441. other_field_count += 1;
  442. case_end;
  443. }
  444. }
  445. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  446. Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
  447. check_fields(c, node, ut->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("raw union"));
  448. union_type->Record.fields = fields;
  449. union_type->Record.field_count = field_count;
  450. union_type->Record.other_fields = other_fields;
  451. union_type->Record.other_field_count = other_field_count;
  452. }
  453. void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *node) {
  454. GB_ASSERT(node->kind == AstNode_EnumType);
  455. GB_ASSERT(is_type_enum(enum_type));
  456. ast_node(et, EnumType, node);
  457. Map<Entity *> entity_map = {};
  458. map_init(&entity_map, gb_heap_allocator());
  459. defer (map_destroy(&entity_map));
  460. Type *base_type = t_int;
  461. if (et->base_type != NULL) {
  462. base_type = check_type(c, et->base_type);
  463. }
  464. if (base_type == NULL || !is_type_integer(base_type)) {
  465. error(&c->error_collector, et->token, "Base type for enumeration must be an integer");
  466. return;
  467. } else
  468. if (base_type == NULL) {
  469. base_type = t_int;
  470. }
  471. enum_type->Record.enum_base = base_type;
  472. Entity **fields = gb_alloc_array(c->allocator, Entity *, gb_array_count(et->fields));
  473. isize field_index = 0;
  474. ExactValue iota = make_exact_value_integer(-1);
  475. gb_for_array(i, et->fields) {
  476. AstNode *field = et->fields[i];
  477. ast_node(f, FieldValue, field);
  478. Token name_token = f->field->Ident;
  479. Operand o = {};
  480. if (f->value != NULL) {
  481. check_expr(c, &o, f->value);
  482. if (o.mode != Addressing_Constant) {
  483. error(&c->error_collector, ast_node_token(f->value), "Enumeration value must be a constant integer");
  484. o.mode = Addressing_Invalid;
  485. }
  486. if (o.mode != Addressing_Invalid) {
  487. check_assignment(c, &o, base_type, make_string("enumeration"));
  488. }
  489. if (o.mode != Addressing_Invalid) {
  490. iota = o.value;
  491. } else {
  492. Token add_token = {Token_Add};
  493. iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
  494. }
  495. } else {
  496. Token add_token = {Token_Add};
  497. iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
  498. }
  499. Type *constant_type = enum_type;
  500. if (named_type != NULL) {
  501. constant_type = named_type;
  502. }
  503. Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, constant_type, iota);
  504. HashKey key = hash_string(name_token.string);
  505. if (map_get(&entity_map, key)) {
  506. // TODO(bill): Scope checking already checks the declaration
  507. error(&c->error_collector, name_token, "`%.*s` is already declared in this enumeration", LIT(name_token.string));
  508. } else {
  509. map_set(&entity_map, key, e);
  510. fields[field_index++] = e;
  511. }
  512. add_entity_use(&c->info, f->field, e);
  513. }
  514. enum_type->Record.other_fields = fields;
  515. enum_type->Record.other_field_count = gb_array_count(et->fields);
  516. }
  517. Type *check_get_params(Checker *c, Scope *scope, AstNodeArray fields, b32 *is_variadic_) {
  518. if (fields == NULL || gb_array_count(fields) == 0)
  519. return NULL;
  520. b32 is_variadic = false;
  521. Type *tuple = make_type_tuple(c->allocator);
  522. isize variable_count = 0;
  523. gb_for_array(i, fields) {
  524. AstNode *field = fields[i];
  525. ast_node(f, Field, field);
  526. variable_count += gb_array_count(f->names);
  527. }
  528. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  529. isize variable_index = 0;
  530. gb_for_array(i, fields) {
  531. AstNode *field = fields[i];
  532. ast_node(f, Field, field);
  533. AstNode *type_expr = f->type;
  534. if (type_expr) {
  535. if (type_expr->kind == AstNode_Ellipsis) {
  536. type_expr = type_expr->Ellipsis.expr;
  537. if (i+1 == gb_array_count(fields)) {
  538. is_variadic = true;
  539. } else {
  540. error(&c->error_collector, ast_node_token(field), "Invalid AST: Invalid variadic parameter");
  541. }
  542. }
  543. Type *type = check_type(c, type_expr);
  544. gb_for_array(j, f->names) {
  545. AstNode *name = f->names[j];
  546. if (name->kind == AstNode_Ident) {
  547. Entity *param = make_entity_param(c->allocator, scope, name->Ident, type, f->is_using);
  548. add_entity(c, scope, name, param);
  549. variables[variable_index++] = param;
  550. } else {
  551. error(&c->error_collector, ast_node_token(name), "Invalid AST: Invalid parameter");
  552. }
  553. }
  554. }
  555. }
  556. if (is_variadic && gb_array_count(fields) > 0) {
  557. // NOTE(bill): Change last variadic parameter to be a slice
  558. // Custom Calling convention for variadic parameters
  559. Entity *end = variables[gb_array_count(fields)-1];
  560. end->type = make_type_slice(c->allocator, end->type);
  561. }
  562. tuple->Tuple.variables = variables;
  563. tuple->Tuple.variable_count = variable_count;
  564. if (is_variadic_) *is_variadic_ = is_variadic;
  565. return tuple;
  566. }
  567. Type *check_get_results(Checker *c, Scope *scope, AstNodeArray results) {
  568. if (results == NULL || gb_array_count(results) == 0) {
  569. return NULL;
  570. }
  571. Type *tuple = make_type_tuple(c->allocator);
  572. Entity **variables = gb_alloc_array(c->allocator, Entity *, gb_array_count(results));
  573. isize variable_index = 0;
  574. gb_for_array(i, results) {
  575. AstNode *item = results[i];
  576. Type *type = check_type(c, item);
  577. Token token = ast_node_token(item);
  578. token.string = make_string(""); // NOTE(bill): results are not named
  579. // TODO(bill): Should I have named results?
  580. Entity *param = make_entity_param(c->allocator, scope, token, type, false);
  581. // NOTE(bill): No need to record
  582. variables[variable_index++] = param;
  583. }
  584. tuple->Tuple.variables = variables;
  585. tuple->Tuple.variable_count = gb_array_count(results);
  586. return tuple;
  587. }
  588. void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
  589. ast_node(pt, ProcType, proc_type_node);
  590. b32 variadic = false;
  591. Type *params = check_get_params(c, c->context.scope, pt->params, &variadic);
  592. Type *results = check_get_results(c, c->context.scope, pt->results);
  593. isize param_count = 0;
  594. isize result_count = 0;
  595. if (params) param_count = params ->Tuple.variable_count;
  596. if (results) result_count = results->Tuple.variable_count;
  597. type->Proc.scope = c->context.scope;
  598. type->Proc.params = params;
  599. type->Proc.param_count = param_count;
  600. type->Proc.results = results;
  601. type->Proc.result_count = result_count;
  602. type->Proc.variadic = variadic;
  603. // type->Proc.implicit_context = implicit_context;
  604. }
  605. void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type, CycleChecker *cycle_checker = NULL) {
  606. GB_ASSERT(n->kind == AstNode_Ident);
  607. o->mode = Addressing_Invalid;
  608. o->expr = n;
  609. Entity *e = scope_lookup_entity(c, c->context.scope, n->Ident.string);
  610. if (e == NULL) {
  611. error(&c->error_collector, n->Ident,
  612. "Undeclared type or identifier `%.*s`", LIT(n->Ident.string));
  613. return;
  614. }
  615. add_entity_use(&c->info, n, e);
  616. CycleChecker local_cycle_checker = {};
  617. if (cycle_checker == NULL) {
  618. cycle_checker = &local_cycle_checker;
  619. }
  620. defer (if (local_cycle_checker.path != NULL) {
  621. gb_array_free(local_cycle_checker.path);
  622. });
  623. if (e->type == NULL) {
  624. auto *found = map_get(&c->info.entities, hash_pointer(e));
  625. if (found != NULL) {
  626. check_entity_decl(c, e, *found, named_type, cycle_checker);
  627. } else {
  628. GB_PANIC("Internal Compiler Error: DeclInfo not found!");
  629. }
  630. }
  631. if (e->type == NULL) {
  632. GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(n->Ident.string));
  633. return;
  634. }
  635. Type *type = e->type;
  636. switch (e->kind) {
  637. case Entity_Constant:
  638. add_declaration_dependency(c, e);
  639. if (type == t_invalid)
  640. return;
  641. o->value = e->Constant.value;
  642. GB_ASSERT(o->value.kind != ExactValue_Invalid);
  643. o->mode = Addressing_Constant;
  644. break;
  645. case Entity_Variable:
  646. add_declaration_dependency(c, e);
  647. e->Variable.used = true;
  648. if (type == t_invalid)
  649. return;
  650. o->mode = Addressing_Variable;
  651. break;
  652. case Entity_TypeName: {
  653. o->mode = Addressing_Type;
  654. #if 0
  655. // TODO(bill): Fix cyclical dependancy checker
  656. gb_for_array(i, cycle_checker->path) {
  657. Entity *prev = cycle_checker->path[i];
  658. if (prev == e) {
  659. error(&c->error_collector, e->token, "Illegal declaration cycle for %.*s", LIT(e->token.string));
  660. for (isize j = i; j < gb_array_count(cycle_checker->path); j++) {
  661. Entity *ref = cycle_checker->path[j];
  662. error(&c->error_collector, ref->token, "\t%.*s refers to", LIT(ref->token.string));
  663. }
  664. error(&c->error_collector, e->token, "\t%.*s", LIT(e->token.string));
  665. type = t_invalid;
  666. break;
  667. }
  668. }
  669. #endif
  670. } break;
  671. case Entity_Procedure:
  672. add_declaration_dependency(c, e);
  673. o->mode = Addressing_Value;
  674. break;
  675. case Entity_Builtin:
  676. o->builtin_id = e->Builtin.id;
  677. o->mode = Addressing_Builtin;
  678. break;
  679. default:
  680. GB_PANIC("Compiler error: Unknown EntityKind");
  681. break;
  682. }
  683. o->type = type;
  684. }
  685. i64 check_array_count(Checker *c, AstNode *e) {
  686. if (e) {
  687. Operand o = {};
  688. check_expr(c, &o, e);
  689. if (o.mode != Addressing_Constant) {
  690. if (o.mode != Addressing_Invalid) {
  691. error(&c->error_collector, ast_node_token(e), "Array count must be a constant");
  692. }
  693. return 0;
  694. }
  695. if (is_type_untyped(o.type) || is_type_integer(o.type)) {
  696. if (o.value.kind == ExactValue_Integer) {
  697. i64 count = o.value.value_integer;
  698. if (count >= 0)
  699. return count;
  700. error(&c->error_collector, ast_node_token(e), "Invalid array count");
  701. return 0;
  702. }
  703. }
  704. error(&c->error_collector, ast_node_token(e), "Array count must be an integer");
  705. }
  706. return 0;
  707. }
  708. Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_checker) {
  709. ExactValue null_value = {ExactValue_Invalid};
  710. Type *type = NULL;
  711. gbString err_str = NULL;
  712. defer (gb_string_free(err_str));
  713. switch (e->kind) {
  714. case_ast_node(i, Ident, e);
  715. Operand operand = {};
  716. check_identifier(c, &operand, e, named_type, cycle_checker);
  717. switch (operand.mode) {
  718. case Addressing_Type: {
  719. type = operand.type;
  720. type->flags |= e->type_flags;
  721. set_base_type(named_type, type);
  722. goto end;
  723. } break;
  724. case Addressing_Invalid:
  725. break;
  726. case Addressing_NoValue:
  727. err_str = expr_to_string(e);
  728. error(&c->error_collector, ast_node_token(e), "`%s` used as a type", err_str);
  729. break;
  730. default:
  731. err_str = expr_to_string(e);
  732. error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
  733. break;
  734. }
  735. case_end;
  736. case_ast_node(se, SelectorExpr, e);
  737. Operand o = {};
  738. o.mode = Addressing_Type;
  739. o.type = check_type(c, se->expr, named_type, cycle_checker);
  740. // gb_printf_err("mode: %.*s\n", LIT(addressing_mode_strings[o.mode]));
  741. check_selector(c, &o, e);
  742. // gb_printf_err("%s.%s\n", expr_to_string(se->expr), expr_to_string(se->selector));
  743. // gb_printf_err("%s\n", type_to_string(o.type));
  744. // gb_printf_err("mode: %.*s\n", LIT(addressing_mode_strings[o.mode]));
  745. if (o.mode == Addressing_Type) {
  746. set_base_type(type, o.type);
  747. o.type->flags |= e->type_flags;
  748. return o.type;
  749. }
  750. case_end;
  751. case_ast_node(pe, ParenExpr, e);
  752. type = check_type(c, pe->expr, named_type, cycle_checker);
  753. type->flags |= e->type_flags;
  754. return type;
  755. case_end;
  756. case_ast_node(at, ArrayType, e);
  757. if (at->count != NULL) {
  758. type = make_type_array(c->allocator,
  759. check_type(c, at->elem, NULL, cycle_checker),
  760. check_array_count(c, at->count));
  761. type->flags |= e->type_flags;
  762. set_base_type(named_type, type);
  763. } else {
  764. type = make_type_slice(c->allocator, check_type(c, at->elem));
  765. type->flags |= e->type_flags;
  766. set_base_type(named_type, type);
  767. }
  768. goto end;
  769. case_end;
  770. case_ast_node(vt, VectorType, e);
  771. Type *elem = check_type(c, vt->elem);
  772. Type *be = get_base_type(elem);
  773. i64 count = check_array_count(c, vt->count);
  774. if (!is_type_boolean(be) && !is_type_numeric(be)) {
  775. err_str = type_to_string(elem);
  776. error(&c->error_collector, ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
  777. }
  778. type = make_type_vector(c->allocator, elem, count);
  779. type->flags |= e->type_flags;
  780. set_base_type(named_type, type);
  781. goto end;
  782. case_end;
  783. case_ast_node(st, StructType, e);
  784. type = make_type_struct(c->allocator);
  785. set_base_type(named_type, type);
  786. check_open_scope(c, e);
  787. check_struct_type(c, type, e, cycle_checker);
  788. check_close_scope(c);
  789. type->Record.node = e;
  790. type->flags |= e->type_flags;
  791. goto end;
  792. case_end;
  793. case_ast_node(ut, UnionType, e);
  794. type = make_type_union(c->allocator);
  795. set_base_type(named_type, type);
  796. check_open_scope(c, e);
  797. check_union_type(c, type, e, cycle_checker);
  798. check_close_scope(c);
  799. type->Record.node = e;
  800. type->flags |= e->type_flags;
  801. goto end;
  802. case_end;
  803. case_ast_node(rut, RawUnionType, e);
  804. type = make_type_raw_union(c->allocator);
  805. set_base_type(named_type, type);
  806. check_open_scope(c, e);
  807. check_raw_union_type(c, type, e, cycle_checker);
  808. check_close_scope(c);
  809. type->Record.node = e;
  810. type->flags |= e->type_flags;
  811. goto end;
  812. case_end;
  813. case_ast_node(et, EnumType, e);
  814. type = make_type_enum(c->allocator);
  815. set_base_type(named_type, type);
  816. check_enum_type(c, type, named_type, e);
  817. type->Record.node = e;
  818. type->flags |= e->type_flags;
  819. goto end;
  820. case_end;
  821. case_ast_node(pt, PointerType, e);
  822. type = make_type_pointer(c->allocator, check_type(c, pt->type));
  823. type->flags |= e->type_flags;
  824. set_base_type(named_type, type);
  825. goto end;
  826. case_end;
  827. case_ast_node(pt, ProcType, e);
  828. type = alloc_type(c->allocator, Type_Proc);
  829. type->flags |= e->type_flags;
  830. set_base_type(named_type, type);
  831. check_procedure_type(c, type, e);
  832. goto end;
  833. case_end;
  834. default: {
  835. if (e->kind == AstNode_CallExpr) {
  836. Operand o = {};
  837. check_expr_or_type(c, &o, e);
  838. if (o.mode == Addressing_Type) {
  839. type = o.type;
  840. type->flags |= e->type_flags;
  841. goto end;
  842. }
  843. }
  844. err_str = expr_to_string(e);
  845. error(&c->error_collector, ast_node_token(e), "`%s` is not a type", err_str);
  846. } break;
  847. }
  848. type = t_invalid;
  849. set_base_type(named_type, type);
  850. end:
  851. GB_ASSERT(is_type_typed(type));
  852. type->flags |= e->type_flags;
  853. add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
  854. return type;
  855. }
  856. b32 check_unary_op(Checker *c, Operand *o, Token op) {
  857. // TODO(bill): Handle errors correctly
  858. Type *type = get_base_type(base_vector_type(get_base_type(o->type)));
  859. gbString str = NULL;
  860. defer (gb_string_free(str));
  861. switch (op.kind) {
  862. case Token_Add:
  863. case Token_Sub:
  864. if (!is_type_numeric(type)) {
  865. str = expr_to_string(o->expr);
  866. error(&c->error_collector, op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  867. }
  868. break;
  869. case Token_Xor:
  870. if (!is_type_integer(type)) {
  871. error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  872. }
  873. break;
  874. case Token_Not:
  875. if (!is_type_boolean(type)) {
  876. str = expr_to_string(o->expr);
  877. error(&c->error_collector, op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  878. }
  879. break;
  880. default:
  881. error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
  882. return false;
  883. }
  884. return true;
  885. }
  886. b32 check_binary_op(Checker *c, Operand *o, Token op) {
  887. // TODO(bill): Handle errors correctly
  888. Type *type = get_base_type(base_vector_type(o->type));
  889. switch (op.kind) {
  890. case Token_Add:
  891. case Token_Sub:
  892. case Token_Mul:
  893. case Token_Quo:
  894. case Token_AddEq:
  895. case Token_SubEq:
  896. case Token_MulEq:
  897. case Token_QuoEq:
  898. if (!is_type_numeric(type)) {
  899. error(&c->error_collector, op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  900. return false;
  901. }
  902. break;
  903. case Token_Mod:
  904. case Token_And:
  905. case Token_Or:
  906. case Token_Xor:
  907. case Token_AndNot:
  908. case Token_ModEq:
  909. case Token_AndEq:
  910. case Token_OrEq:
  911. case Token_XorEq:
  912. case Token_AndNotEq:
  913. if (!is_type_integer(type)) {
  914. error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  915. return false;
  916. }
  917. break;
  918. case Token_CmpAnd:
  919. case Token_CmpOr:
  920. case Token_CmpAndEq:
  921. case Token_CmpOrEq:
  922. if (!is_type_boolean(type)) {
  923. error(&c->error_collector, op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  924. return false;
  925. }
  926. break;
  927. default:
  928. error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
  929. return false;
  930. }
  931. return true;
  932. }
  933. b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  934. if (in_value.kind == ExactValue_Invalid)
  935. return true;
  936. if (is_type_boolean(type)) {
  937. return in_value.kind == ExactValue_Bool;
  938. } else if (is_type_string(type)) {
  939. return in_value.kind == ExactValue_String;
  940. } else if (is_type_integer(type)) {
  941. ExactValue v = exact_value_to_integer(in_value);
  942. if (v.kind != ExactValue_Integer)
  943. return false;
  944. if (out_value) *out_value = v;
  945. i64 i = v.value_integer;
  946. i64 s = 8*type_size_of(c->sizes, c->allocator, type);
  947. u64 umax = ~0ull;
  948. if (s < 64) {
  949. umax = (1ull << s) - 1ull;
  950. }
  951. i64 imax = (1ll << (s-1ll));
  952. switch (type->Basic.kind) {
  953. case Basic_i8:
  954. case Basic_i16:
  955. case Basic_i32:
  956. case Basic_i64:
  957. case Basic_int:
  958. return gb_is_between(i, -imax, imax-1);
  959. case Basic_u8:
  960. case Basic_u16:
  961. case Basic_u32:
  962. case Basic_u64:
  963. case Basic_uint:
  964. return !(i < 0 || cast(u64)i > umax);
  965. case Basic_UntypedInteger:
  966. return true;
  967. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  968. }
  969. } else if (is_type_float(type)) {
  970. ExactValue v = exact_value_to_float(in_value);
  971. if (v.kind != ExactValue_Float)
  972. return false;
  973. switch (type->Basic.kind) {
  974. case Basic_f32:
  975. if (out_value) *out_value = v;
  976. return true;
  977. case Basic_f64:
  978. if (out_value) *out_value = v;
  979. return true;
  980. case Basic_UntypedFloat:
  981. return true;
  982. }
  983. } else if (is_type_pointer(type)) {
  984. if (in_value.kind == ExactValue_Pointer)
  985. return true;
  986. if (in_value.kind == ExactValue_Integer)
  987. return true;
  988. if (out_value) *out_value = in_value;
  989. }
  990. return false;
  991. }
  992. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  993. GB_ASSERT(type->kind == Type_Basic);
  994. GB_ASSERT(o->mode == Addressing_Constant);
  995. if (!check_value_is_expressible(c, o->value, type, &o->value)) {
  996. gbString a = expr_to_string(o->expr);
  997. gbString b = type_to_string(type);
  998. defer (gb_string_free(a));
  999. defer (gb_string_free(b));
  1000. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1001. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1002. error(&c->error_collector, ast_node_token(o->expr), "`%s` truncated to `%s`", a, b);
  1003. } else {
  1004. error(&c->error_collector, ast_node_token(o->expr), "`%s = %lld` overflows `%s`", a, o->value.value_integer, b);
  1005. }
  1006. } else {
  1007. error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
  1008. }
  1009. o->mode = Addressing_Invalid;
  1010. }
  1011. }
  1012. b32 check_is_expr_vector_index(Checker *c, AstNode *expr) {
  1013. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1014. expr = unparen_expr(expr);
  1015. if (expr->kind == AstNode_IndexExpr) {
  1016. ast_node(ie, IndexExpr, expr);
  1017. Type *t = type_of_expr(&c->info, ie->expr);
  1018. if (t != NULL) {
  1019. return is_type_vector(get_base_type(t));
  1020. }
  1021. }
  1022. return false;
  1023. }
  1024. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  1025. if (op.kind == Token_Pointer) { // Pointer address
  1026. if (o->mode != Addressing_Variable ||
  1027. check_is_expr_vector_index(c, o->expr)) {
  1028. ast_node(ue, UnaryExpr, node);
  1029. gbString str = expr_to_string(ue->expr);
  1030. defer (gb_string_free(str));
  1031. error(&c->error_collector, op, "Cannot take the pointer address of `%s`", str);
  1032. o->mode = Addressing_Invalid;
  1033. return;
  1034. }
  1035. o->mode = Addressing_Value;
  1036. o->type = make_type_pointer(c->allocator, o->type);
  1037. return;
  1038. }
  1039. if (!check_unary_op(c, o, op)) {
  1040. o->mode = Addressing_Invalid;
  1041. return;
  1042. }
  1043. if (o->mode == Addressing_Constant) {
  1044. Type *type = get_base_type(o->type);
  1045. GB_ASSERT(type->kind == Type_Basic);
  1046. i32 precision = 0;
  1047. if (is_type_unsigned(type))
  1048. precision = cast(i32)(8 * type_size_of(c->sizes, c->allocator, type));
  1049. o->value = exact_unary_operator_value(op, o->value, precision);
  1050. if (is_type_typed(type)) {
  1051. if (node != NULL)
  1052. o->expr = node;
  1053. check_is_expressible(c, o, type);
  1054. }
  1055. return;
  1056. }
  1057. o->mode = Addressing_Value;
  1058. }
  1059. void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
  1060. gbString err_str = NULL;
  1061. defer ({
  1062. if (err_str != NULL)
  1063. gb_string_free(err_str);
  1064. });
  1065. if (check_is_assignable_to(c, x, y->type) ||
  1066. check_is_assignable_to(c, y, x->type)) {
  1067. b32 defined = false;
  1068. switch (op.kind) {
  1069. case Token_CmpEq:
  1070. case Token_NotEq:
  1071. defined = is_type_comparable(get_base_type(x->type));
  1072. break;
  1073. case Token_Lt:
  1074. case Token_Gt:
  1075. case Token_LtEq:
  1076. case Token_GtEq: {
  1077. defined = is_type_ordered(get_base_type(x->type));
  1078. } break;
  1079. }
  1080. if (!defined) {
  1081. gbString type_string = type_to_string(x->type);
  1082. err_str = gb_string_make(gb_heap_allocator(),
  1083. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(op.string), type_string));
  1084. gb_string_free(type_string);
  1085. }
  1086. } else {
  1087. gbString xt = type_to_string(x->type);
  1088. gbString yt = type_to_string(y->type);
  1089. defer(gb_string_free(xt));
  1090. defer(gb_string_free(yt));
  1091. err_str = gb_string_make(gb_heap_allocator(),
  1092. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  1093. }
  1094. if (err_str != NULL) {
  1095. error(&c->error_collector, op, "Cannot compare expression, %s", err_str);
  1096. x->type = t_untyped_bool;
  1097. return;
  1098. }
  1099. if (x->mode == Addressing_Constant &&
  1100. y->mode == Addressing_Constant) {
  1101. x->value = make_exact_value_bool(compare_exact_values(op, x->value, y->value));
  1102. } else {
  1103. x->mode = Addressing_Value;
  1104. update_expr_type(c, x->expr, default_type(x->type), true);
  1105. update_expr_type(c, y->expr, default_type(y->type), true);
  1106. }
  1107. if (is_type_vector(get_base_type(y->type))) {
  1108. x->type = make_type_vector(c->allocator, t_bool, get_base_type(y->type)->Vector.count);
  1109. } else {
  1110. x->type = t_untyped_bool;
  1111. }
  1112. }
  1113. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  1114. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1115. ast_node(be, BinaryExpr, node);
  1116. ExactValue x_val = {};
  1117. if (x->mode == Addressing_Constant) {
  1118. x_val = exact_value_to_integer(x->value);
  1119. }
  1120. b32 x_is_untyped = is_type_untyped(x->type);
  1121. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  1122. gbString err_str = expr_to_string(x->expr);
  1123. defer (gb_string_free(err_str));
  1124. error(&c->error_collector, ast_node_token(node),
  1125. "Shifted operand `%s` must be an integer", err_str);
  1126. x->mode = Addressing_Invalid;
  1127. return;
  1128. }
  1129. if (is_type_unsigned(y->type)) {
  1130. } else if (is_type_untyped(y->type)) {
  1131. convert_to_typed(c, y, t_untyped_integer);
  1132. if (y->mode == Addressing_Invalid) {
  1133. x->mode = Addressing_Invalid;
  1134. return;
  1135. }
  1136. } else {
  1137. gbString err_str = expr_to_string(y->expr);
  1138. defer (gb_string_free(err_str));
  1139. error(&c->error_collector, ast_node_token(node),
  1140. "Shift amount `%s` must be an unsigned integer", err_str);
  1141. x->mode = Addressing_Invalid;
  1142. return;
  1143. }
  1144. if (x->mode == Addressing_Constant) {
  1145. if (y->mode == Addressing_Constant) {
  1146. ExactValue y_val = exact_value_to_integer(y->value);
  1147. if (y_val.kind != ExactValue_Integer) {
  1148. gbString err_str = expr_to_string(y->expr);
  1149. defer (gb_string_free(err_str));
  1150. error(&c->error_collector, ast_node_token(node),
  1151. "Shift amount `%s` must be an unsigned integer", err_str);
  1152. x->mode = Addressing_Invalid;
  1153. return;
  1154. }
  1155. u64 amount = cast(u64)y_val.value_integer;
  1156. if (amount > 1074) {
  1157. gbString err_str = expr_to_string(y->expr);
  1158. defer (gb_string_free(err_str));
  1159. error(&c->error_collector, ast_node_token(node),
  1160. "Shift amount too large: `%s`", err_str);
  1161. x->mode = Addressing_Invalid;
  1162. return;
  1163. }
  1164. if (!is_type_integer(x->type)) {
  1165. // NOTE(bill): It could be an untyped float but still representable
  1166. // as an integer
  1167. x->type = t_untyped_integer;
  1168. }
  1169. x->value = exact_value_shift(be->op, x_val, make_exact_value_integer(amount));
  1170. if (is_type_typed(x->type)) {
  1171. check_is_expressible(c, x, get_base_type(x->type));
  1172. }
  1173. return;
  1174. }
  1175. if (x_is_untyped) {
  1176. ExpressionInfo *info = map_get(&c->info.untyped, hash_pointer(x->expr));
  1177. if (info != NULL) {
  1178. info->is_lhs = true;
  1179. }
  1180. x->mode = Addressing_Value;
  1181. return;
  1182. }
  1183. }
  1184. if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
  1185. gbString err_str = expr_to_string(y->expr);
  1186. defer (gb_string_free(err_str));
  1187. error(&c->error_collector, ast_node_token(node),
  1188. "Shift amount cannot be negative: `%s`", err_str);
  1189. }
  1190. x->mode = Addressing_Value;
  1191. }
  1192. b32 check_is_castable_to(Checker *c, Operand *operand, Type *y) {
  1193. if (check_is_assignable_to(c, operand, y))
  1194. return true;
  1195. Type *x = operand->type;
  1196. Type *xb = get_base_type(x);
  1197. Type *yb = get_base_type(y);
  1198. if (are_types_identical(xb, yb)) {
  1199. return true;
  1200. }
  1201. xb = get_enum_base_type(x);
  1202. yb = get_enum_base_type(y);
  1203. // Cast between booleans and integers
  1204. if (is_type_boolean(xb) || is_type_integer(xb)) {
  1205. if (is_type_boolean(yb) || is_type_integer(yb))
  1206. return true;
  1207. }
  1208. // Cast between numbers
  1209. if (is_type_integer(xb) || is_type_float(xb)) {
  1210. if (is_type_integer(yb) || is_type_float(yb))
  1211. return true;
  1212. }
  1213. // Cast between pointers
  1214. if (is_type_pointer(xb)) {
  1215. if (is_type_pointer(yb))
  1216. return true;
  1217. }
  1218. // // untyped integers -> pointers
  1219. // if (is_type_untyped(xb) && is_type_integer(xb)) {
  1220. // if (is_type_pointer(yb))
  1221. // return true;
  1222. // }
  1223. // (u)int <-> pointer
  1224. if (is_type_pointer(xb) || (is_type_int_or_uint(xb) && !is_type_untyped(xb))) {
  1225. if (is_type_pointer(yb))
  1226. return true;
  1227. }
  1228. if (is_type_pointer(xb)) {
  1229. if (is_type_pointer(yb) || (is_type_int_or_uint(yb) && !is_type_untyped(yb)))
  1230. return true;
  1231. }
  1232. // []byte/[]u8 <-> string
  1233. if (is_type_u8_slice(xb) && is_type_string(yb)) {
  1234. return true;
  1235. }
  1236. if (is_type_string(xb) && is_type_u8_slice(yb)) {
  1237. return true;
  1238. }
  1239. // proc <-> proc
  1240. if (is_type_proc(xb) && is_type_proc(yb)) {
  1241. return true;
  1242. }
  1243. // proc -> rawptr
  1244. if (is_type_proc(xb) && is_type_rawptr(yb)) {
  1245. return true;
  1246. }
  1247. return false;
  1248. }
  1249. String check_down_cast_name(Type *dst_, Type *src_) {
  1250. String result = {};
  1251. Type *dst = type_deref(dst_);
  1252. Type *src = type_deref(src_);
  1253. Type *dst_s = get_base_type(dst);
  1254. GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
  1255. for (isize i = 0; i < dst_s->Record.field_count; i++) {
  1256. Entity *f = dst_s->Record.fields[i];
  1257. GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
  1258. if (f->Variable.anonymous) {
  1259. if (are_types_identical(f->type, src_)) {
  1260. return f->token.string;
  1261. }
  1262. if (are_types_identical(type_deref(f->type), src_)) {
  1263. return f->token.string;
  1264. }
  1265. if (!is_type_pointer(f->type)) {
  1266. result = check_down_cast_name(f->type, src_);
  1267. if (result.len > 0)
  1268. return result;
  1269. }
  1270. }
  1271. }
  1272. return result;
  1273. }
  1274. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  1275. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  1276. Operand y_ = {}, *y = &y_;
  1277. gbString err_str = NULL;
  1278. defer (gb_string_free(err_str));
  1279. ast_node(be, BinaryExpr, node);
  1280. if (be->op.kind == Token_as) {
  1281. check_expr(c, x, be->left);
  1282. Type *type = check_type(c, be->right);
  1283. if (x->mode == Addressing_Invalid)
  1284. return;
  1285. b32 is_const_expr = x->mode == Addressing_Constant;
  1286. b32 can_convert = false;
  1287. Type *base_type = get_base_type(type);
  1288. if (is_const_expr && is_type_constant_type(base_type)) {
  1289. if (base_type->kind == Type_Basic) {
  1290. if (check_value_is_expressible(c, x->value, base_type, &x->value)) {
  1291. can_convert = true;
  1292. }
  1293. }
  1294. } else if (check_is_castable_to(c, x, type)) {
  1295. x->mode = Addressing_Value;
  1296. can_convert = true;
  1297. }
  1298. if (!can_convert) {
  1299. gbString expr_str = expr_to_string(x->expr);
  1300. gbString to_type = type_to_string(type);
  1301. gbString from_type = type_to_string(x->type);
  1302. defer (gb_string_free(expr_str));
  1303. defer (gb_string_free(to_type));
  1304. defer (gb_string_free(from_type));
  1305. error(&c->error_collector, ast_node_token(x->expr), "Cannot cast `%s` as `%s` from `%s`", expr_str, to_type, from_type);
  1306. x->mode = Addressing_Invalid;
  1307. return;
  1308. }
  1309. if (is_type_untyped(x->type)) {
  1310. Type *final_type = type;
  1311. if (is_const_expr && !is_type_constant_type(type)) {
  1312. final_type = default_type(x->type);
  1313. }
  1314. update_expr_type(c, x->expr, final_type, true);
  1315. }
  1316. x->type = type;
  1317. return;
  1318. } else if (be->op.kind == Token_transmute) {
  1319. check_expr(c, x, be->left);
  1320. Type *type = check_type(c, be->right);
  1321. if (x->mode == Addressing_Invalid)
  1322. return;
  1323. if (x->mode == Addressing_Constant) {
  1324. gbString expr_str = expr_to_string(x->expr);
  1325. defer (gb_string_free(expr_str));
  1326. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute constant expression: `%s`", expr_str);
  1327. x->mode = Addressing_Invalid;
  1328. return;
  1329. }
  1330. if (is_type_untyped(x->type)) {
  1331. gbString expr_str = expr_to_string(x->expr);
  1332. defer (gb_string_free(expr_str));
  1333. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute untyped expression: `%s`", expr_str);
  1334. x->mode = Addressing_Invalid;
  1335. return;
  1336. }
  1337. i64 otz = type_size_of(c->sizes, c->allocator, x->type);
  1338. i64 ttz = type_size_of(c->sizes, c->allocator, type);
  1339. if (otz != ttz) {
  1340. gbString expr_str = expr_to_string(x->expr);
  1341. gbString type_str = type_to_string(type);
  1342. defer (gb_string_free(expr_str));
  1343. defer (gb_string_free(type_str));
  1344. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, otz, ttz);
  1345. x->mode = Addressing_Invalid;
  1346. return;
  1347. }
  1348. x->type = type;
  1349. return;
  1350. } else if (be->op.kind == Token_down_cast) {
  1351. check_expr(c, x, be->left);
  1352. Type *type = check_type(c, be->right);
  1353. if (x->mode == Addressing_Invalid)
  1354. return;
  1355. if (x->mode == Addressing_Constant) {
  1356. gbString expr_str = expr_to_string(node);
  1357. defer (gb_string_free(expr_str));
  1358. error(&c->error_collector, ast_node_token(node), "Cannot `down_cast` a constant expression: `%s`", expr_str);
  1359. x->mode = Addressing_Invalid;
  1360. return;
  1361. }
  1362. if (is_type_untyped(x->type)) {
  1363. gbString expr_str = expr_to_string(node);
  1364. defer (gb_string_free(expr_str));
  1365. error(&c->error_collector, ast_node_token(node), "Cannot `down_cast` an untyped expression: `%s`", expr_str);
  1366. x->mode = Addressing_Invalid;
  1367. return;
  1368. }
  1369. if (!(is_type_pointer(x->type) && is_type_pointer(type))) {
  1370. gbString expr_str = expr_to_string(node);
  1371. defer (gb_string_free(expr_str));
  1372. error(&c->error_collector, ast_node_token(node), "Can only `down_cast` pointers: `%s`", expr_str);
  1373. x->mode = Addressing_Invalid;
  1374. return;
  1375. }
  1376. Type *src = type_deref(x->type);
  1377. Type *dst = type_deref(type);
  1378. Type *bsrc = get_base_type(src);
  1379. Type *bdst = get_base_type(dst);
  1380. if (!(is_type_struct(bsrc) || is_type_raw_union(bsrc))) {
  1381. gbString expr_str = expr_to_string(node);
  1382. defer (gb_string_free(expr_str));
  1383. error(&c->error_collector, ast_node_token(node), "Can only `down_cast` pointer from structs or unions: `%s`", expr_str);
  1384. x->mode = Addressing_Invalid;
  1385. return;
  1386. }
  1387. if (!(is_type_struct(bdst) || is_type_struct(bdst))) {
  1388. gbString expr_str = expr_to_string(node);
  1389. defer (gb_string_free(expr_str));
  1390. error(&c->error_collector, ast_node_token(node), "Can only `down_cast` pointer to structs or unions: `%s`", expr_str);
  1391. x->mode = Addressing_Invalid;
  1392. return;
  1393. }
  1394. String param_name = check_down_cast_name(dst, src);
  1395. if (param_name.len == 0) {
  1396. gbString expr_str = expr_to_string(node);
  1397. defer (gb_string_free(expr_str));
  1398. error(&c->error_collector, ast_node_token(node), "Illegal `down_cast`: `%s`", expr_str);
  1399. x->mode = Addressing_Invalid;
  1400. return;
  1401. }
  1402. x->mode = Addressing_Value;
  1403. x->type = type;
  1404. return;
  1405. }
  1406. check_expr(c, x, be->left);
  1407. check_expr(c, y, be->right);
  1408. if (x->mode == Addressing_Invalid) {
  1409. return;
  1410. }
  1411. if (y->mode == Addressing_Invalid) {
  1412. x->mode = Addressing_Invalid;
  1413. x->expr = y->expr;
  1414. return;
  1415. }
  1416. Token op = be->op;
  1417. if (token_is_shift(op)) {
  1418. check_shift(c, x, y, node);
  1419. return;
  1420. }
  1421. convert_to_typed(c, x, y->type);
  1422. if (x->mode == Addressing_Invalid) return;
  1423. convert_to_typed(c, y, x->type);
  1424. if (y->mode == Addressing_Invalid) {
  1425. x->mode = Addressing_Invalid;
  1426. return;
  1427. }
  1428. if (token_is_comparison(op)) {
  1429. check_comparison(c, x, y, op);
  1430. return;
  1431. }
  1432. if (!are_types_identical(x->type, y->type)) {
  1433. if (x->type != t_invalid &&
  1434. y->type != t_invalid) {
  1435. gbString xt = type_to_string(x->type);
  1436. gbString yt = type_to_string(y->type);
  1437. defer (gb_string_free(xt));
  1438. defer (gb_string_free(yt));
  1439. err_str = expr_to_string(x->expr);
  1440. error(&c->error_collector, op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", err_str, xt, yt);
  1441. }
  1442. x->mode = Addressing_Invalid;
  1443. return;
  1444. }
  1445. if (!check_binary_op(c, x, op)) {
  1446. x->mode = Addressing_Invalid;
  1447. return;
  1448. }
  1449. switch (op.kind) {
  1450. case Token_Quo:
  1451. case Token_Mod:
  1452. case Token_QuoEq:
  1453. case Token_ModEq:
  1454. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  1455. y->mode == Addressing_Constant) {
  1456. b32 fail = false;
  1457. switch (y->value.kind) {
  1458. case ExactValue_Integer:
  1459. if (y->value.value_integer == 0)
  1460. fail = true;
  1461. break;
  1462. case ExactValue_Float:
  1463. if (y->value.value_float == 0.0)
  1464. fail = true;
  1465. break;
  1466. }
  1467. if (fail) {
  1468. error(&c->error_collector, ast_node_token(y->expr), "Division by zero not allowed");
  1469. x->mode = Addressing_Invalid;
  1470. return;
  1471. }
  1472. }
  1473. }
  1474. if (x->mode == Addressing_Constant &&
  1475. y->mode == Addressing_Constant) {
  1476. ExactValue a = x->value;
  1477. ExactValue b = y->value;
  1478. Type *type = get_base_type(x->type);
  1479. GB_ASSERT(type->kind == Type_Basic);
  1480. if (op.kind == Token_Quo && is_type_integer(type)) {
  1481. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  1482. }
  1483. x->value = exact_binary_operator_value(op, a, b);
  1484. if (is_type_typed(type)) {
  1485. if (node != NULL)
  1486. x->expr = node;
  1487. check_is_expressible(c, x, type);
  1488. }
  1489. return;
  1490. }
  1491. x->mode = Addressing_Value;
  1492. }
  1493. void update_expr_type(Checker *c, AstNode *e, Type *type, b32 final) {
  1494. HashKey key = hash_pointer(e);
  1495. ExpressionInfo *found = map_get(&c->info.untyped, key);
  1496. if (found == NULL)
  1497. return;
  1498. switch (e->kind) {
  1499. case_ast_node(ue, UnaryExpr, e);
  1500. if (found->value.kind != ExactValue_Invalid)
  1501. break;
  1502. update_expr_type(c, ue->expr, type, final);
  1503. case_end;
  1504. case_ast_node(be, BinaryExpr, e);
  1505. if (found->value.kind != ExactValue_Invalid)
  1506. break;
  1507. if (!token_is_comparison(be->op)) {
  1508. if (token_is_shift(be->op)) {
  1509. update_expr_type(c, be->left, type, final);
  1510. } else {
  1511. update_expr_type(c, be->left, type, final);
  1512. update_expr_type(c, be->right, type, final);
  1513. }
  1514. }
  1515. case_end;
  1516. }
  1517. if (!final && is_type_untyped(type)) {
  1518. found->type = get_base_type(type);
  1519. map_set(&c->info.untyped, key, *found);
  1520. } else {
  1521. ExpressionInfo old = *found;
  1522. map_remove(&c->info.untyped, key);
  1523. if (old.is_lhs && !is_type_integer(type)) {
  1524. gbString expr_str = expr_to_string(e);
  1525. gbString type_str = type_to_string(type);
  1526. defer (gb_string_free(expr_str));
  1527. defer (gb_string_free(type_str));
  1528. error(&c->error_collector, ast_node_token(e), "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  1529. return;
  1530. }
  1531. add_type_and_value(&c->info, e, found->mode, type, found->value);
  1532. }
  1533. }
  1534. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  1535. ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
  1536. if (found)
  1537. found->value = value;
  1538. }
  1539. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  1540. gbString expr_str = expr_to_string(operand->expr);
  1541. gbString type_str = type_to_string(target_type);
  1542. char *extra_text = "";
  1543. defer (gb_string_free(expr_str));
  1544. defer (gb_string_free(type_str));
  1545. if (operand->mode == Addressing_Constant) {
  1546. if (operand->value.value_integer == 0) {
  1547. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  1548. extra_text = " - Did you want `null`?";
  1549. }
  1550. }
  1551. error(&c->error_collector, ast_node_token(operand->expr), "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  1552. operand->mode = Addressing_Invalid;
  1553. }
  1554. void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
  1555. GB_ASSERT_NOT_NULL(target_type);
  1556. if (operand->mode == Addressing_Invalid ||
  1557. is_type_typed(operand->type) ||
  1558. target_type == t_invalid) {
  1559. return;
  1560. }
  1561. if (is_type_untyped(target_type)) {
  1562. Type *x = operand->type;
  1563. Type *y = target_type;
  1564. if (is_type_numeric(x) && is_type_numeric(y)) {
  1565. if (x < y) {
  1566. operand->type = target_type;
  1567. update_expr_type(c, operand->expr, target_type, false);
  1568. }
  1569. } else if (x != y) {
  1570. convert_untyped_error(c, operand, target_type);
  1571. }
  1572. return;
  1573. }
  1574. Type *t = get_enum_base_type(get_base_type(target_type));
  1575. switch (t->kind) {
  1576. case Type_Basic:
  1577. if (operand->mode == Addressing_Constant) {
  1578. check_is_expressible(c, operand, t);
  1579. if (operand->mode == Addressing_Invalid) {
  1580. return;
  1581. }
  1582. update_expr_value(c, operand->expr, operand->value);
  1583. } else {
  1584. // TODO(bill): Is this really needed?
  1585. switch (operand->type->Basic.kind) {
  1586. case Basic_UntypedBool:
  1587. if (!is_type_boolean(target_type)) {
  1588. convert_untyped_error(c, operand, target_type);
  1589. return;
  1590. }
  1591. break;
  1592. case Basic_UntypedInteger:
  1593. case Basic_UntypedFloat:
  1594. case Basic_UntypedRune:
  1595. if (!is_type_numeric(target_type)) {
  1596. convert_untyped_error(c, operand, target_type);
  1597. return;
  1598. }
  1599. break;
  1600. }
  1601. }
  1602. break;
  1603. case Type_Pointer:
  1604. switch (operand->type->Basic.kind) {
  1605. case Basic_UntypedPointer:
  1606. target_type = t_untyped_pointer;
  1607. break;
  1608. default:
  1609. convert_untyped_error(c, operand, target_type);
  1610. return;
  1611. }
  1612. break;
  1613. case Type_Proc:
  1614. switch (operand->type->Basic.kind) {
  1615. case Basic_UntypedPointer:
  1616. break;
  1617. default:
  1618. convert_untyped_error(c, operand, target_type);
  1619. return;
  1620. }
  1621. break;
  1622. default:
  1623. convert_untyped_error(c, operand, target_type);
  1624. return;
  1625. }
  1626. operand->type = target_type;
  1627. }
  1628. b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *value) {
  1629. Operand operand = {Addressing_Invalid};
  1630. check_expr(c, &operand, index_value);
  1631. if (operand.mode == Addressing_Invalid) {
  1632. if (value) *value = 0;
  1633. return false;
  1634. }
  1635. convert_to_typed(c, &operand, t_int);
  1636. if (operand.mode == Addressing_Invalid) {
  1637. if (value) *value = 0;
  1638. return false;
  1639. }
  1640. if (!is_type_integer(get_enum_base_type(operand.type))) {
  1641. gbString expr_str = expr_to_string(operand.expr);
  1642. error(&c->error_collector, ast_node_token(operand.expr),
  1643. "Index `%s` must be an integer", expr_str);
  1644. gb_string_free(expr_str);
  1645. if (value) *value = 0;
  1646. return false;
  1647. }
  1648. if (operand.mode == Addressing_Constant) {
  1649. i64 i = exact_value_to_integer(operand.value).value_integer;
  1650. if (i < 0) {
  1651. gbString expr_str = expr_to_string(operand.expr);
  1652. error(&c->error_collector, ast_node_token(operand.expr),
  1653. "Index `%s` cannot be a negative value", expr_str);
  1654. gb_string_free(expr_str);
  1655. if (value) *value = 0;
  1656. return false;
  1657. }
  1658. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  1659. if (value) *value = i;
  1660. if (i >= max_count) {
  1661. gbString expr_str = expr_to_string(operand.expr);
  1662. error(&c->error_collector, ast_node_token(operand.expr),
  1663. "Index `%s` is out of bounds range [0, %lld)", expr_str, max_count);
  1664. gb_string_free(expr_str);
  1665. return false;
  1666. }
  1667. return true;
  1668. }
  1669. }
  1670. // NOTE(bill): It's alright :D
  1671. if (value) *value = -1;
  1672. return true;
  1673. }
  1674. Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
  1675. GB_ASSERT(node->kind == AstNode_SelectorExpr);
  1676. ast_node(se, SelectorExpr, node);
  1677. AstNode *op_expr = se->expr;
  1678. AstNode *selector = se->selector;
  1679. if (selector) {
  1680. Entity *entity = lookup_field(operand->type, selector->Ident.string, operand->mode == Addressing_Type).entity;
  1681. if (entity == NULL) {
  1682. gbString op_str = expr_to_string(op_expr);
  1683. gbString type_str = type_to_string(operand->type);
  1684. gbString sel_str = expr_to_string(selector);
  1685. defer (gb_string_free(op_str));
  1686. defer (gb_string_free(type_str));
  1687. defer (gb_string_free(sel_str));
  1688. error(&c->error_collector, ast_node_token(op_expr), "`%s` (`%s`) has no field `%s`", op_str, type_str, sel_str);
  1689. operand->mode = Addressing_Invalid;
  1690. operand->expr = node;
  1691. return NULL;
  1692. }
  1693. add_entity_use(&c->info, selector, entity);
  1694. operand->type = entity->type;
  1695. operand->expr = node;
  1696. switch (entity->kind) {
  1697. case Entity_Constant:
  1698. operand->mode = Addressing_Constant;
  1699. operand->value = entity->Constant.value;
  1700. break;
  1701. case Entity_Variable:
  1702. operand->mode = Addressing_Variable;
  1703. break;
  1704. case Entity_TypeName:
  1705. operand->mode = Addressing_Type;
  1706. break;
  1707. case Entity_Procedure:
  1708. operand->mode = Addressing_Value;
  1709. break;
  1710. case Entity_Builtin:
  1711. operand->mode = Addressing_Builtin;
  1712. operand->builtin_id = entity->Builtin.id;
  1713. break;
  1714. }
  1715. return entity;
  1716. } else {
  1717. operand->mode = Addressing_Invalid;
  1718. operand->expr = node;
  1719. }
  1720. return NULL;
  1721. }
  1722. b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  1723. GB_ASSERT(call->kind == AstNode_CallExpr);
  1724. ast_node(ce, CallExpr, call);
  1725. BuiltinProc *bp = &builtin_procs[id];
  1726. {
  1727. char *err = NULL;
  1728. if (gb_array_count(ce->args) < bp->arg_count)
  1729. err = "Too few";
  1730. if (gb_array_count(ce->args) > bp->arg_count && !bp->variadic)
  1731. err = "Too many";
  1732. if (err) {
  1733. ast_node(proc, Ident, ce->proc);
  1734. error(&c->error_collector, ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
  1735. err, LIT(proc->string),
  1736. bp->arg_count, gb_array_count(ce->args));
  1737. return false;
  1738. }
  1739. }
  1740. switch (id) {
  1741. case BuiltinProc_new:
  1742. case BuiltinProc_new_slice:
  1743. case BuiltinProc_size_of:
  1744. case BuiltinProc_align_of:
  1745. case BuiltinProc_offset_of:
  1746. case BuiltinProc_type_info:
  1747. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  1748. break;
  1749. default:
  1750. check_multi_expr(c, operand, ce->args[0]);
  1751. }
  1752. switch (id) {
  1753. case BuiltinProc_new: {
  1754. // new :: proc(Type) -> ^Type
  1755. Operand op = {};
  1756. check_expr_or_type(c, &op, ce->args[0]);
  1757. Type *type = op.type;
  1758. if (op.mode != Addressing_Type && type == NULL || type == t_invalid) {
  1759. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a type for `new`");
  1760. return false;
  1761. }
  1762. operand->mode = Addressing_Value;
  1763. operand->type = make_type_pointer(c->allocator, type);
  1764. } break;
  1765. case BuiltinProc_new_slice: {
  1766. // new_slice :: proc(Type, len: int[, cap: int]) -> []Type
  1767. Operand op = {};
  1768. check_expr_or_type(c, &op, ce->args[0]);
  1769. Type *type = op.type;
  1770. if (op.mode != Addressing_Type && type == NULL || type == t_invalid) {
  1771. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a type for `new_slice`");
  1772. return false;
  1773. }
  1774. AstNode *len = ce->args[1];
  1775. AstNode *cap = NULL;
  1776. if (gb_array_count(ce->args) > 2) {
  1777. cap = ce->args[2];
  1778. }
  1779. check_expr(c, &op, len);
  1780. if (op.mode == Addressing_Invalid)
  1781. return false;
  1782. if (!is_type_integer(op.type)) {
  1783. gbString type_str = type_to_string(operand->type);
  1784. defer (gb_string_free(type_str));
  1785. error(&c->error_collector, ast_node_token(call),
  1786. "Length for `new_slice` must be an integer, got `%s`",
  1787. type_str);
  1788. return false;
  1789. }
  1790. if (cap != NULL) {
  1791. check_expr(c, &op, cap);
  1792. if (op.mode == Addressing_Invalid)
  1793. return false;
  1794. if (!is_type_integer(op.type)) {
  1795. gbString type_str = type_to_string(operand->type);
  1796. defer (gb_string_free(type_str));
  1797. error(&c->error_collector, ast_node_token(call),
  1798. "Capacity for `new_slice` must be an integer, got `%s`",
  1799. type_str);
  1800. return false;
  1801. }
  1802. if (ce->args[3] != NULL) {
  1803. error(&c->error_collector, ast_node_token(call),
  1804. "Too many arguments to `new_slice`, expected either 2 or 3");
  1805. return false;
  1806. }
  1807. }
  1808. operand->mode = Addressing_Value;
  1809. operand->type = make_type_slice(c->allocator, type);
  1810. } break;
  1811. case BuiltinProc_size_of: {
  1812. // size_of :: proc(Type) -> int
  1813. Operand op = {};
  1814. check_expr_or_type(c, &op, ce->args[0]);
  1815. Type *type = op.type;
  1816. if (!type) {
  1817. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a type for `size_of`");
  1818. return false;
  1819. }
  1820. operand->mode = Addressing_Constant;
  1821. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, type));
  1822. operand->type = t_int;
  1823. } break;
  1824. case BuiltinProc_size_of_val:
  1825. // size_of_val :: proc(val: Type) -> int
  1826. check_assignment(c, operand, NULL, make_string("argument of `size_of_val`"));
  1827. if (operand->mode == Addressing_Invalid)
  1828. return false;
  1829. operand->mode = Addressing_Constant;
  1830. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, operand->type));
  1831. operand->type = t_int;
  1832. break;
  1833. case BuiltinProc_align_of: {
  1834. // align_of :: proc(Type) -> int
  1835. Operand op = {};
  1836. check_expr_or_type(c, &op, ce->args[0]);
  1837. Type *type = op.type;
  1838. if (!type) {
  1839. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a type for `align_of`");
  1840. return false;
  1841. }
  1842. operand->mode = Addressing_Constant;
  1843. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, type));
  1844. operand->type = t_int;
  1845. } break;
  1846. case BuiltinProc_align_of_val:
  1847. // align_of_val :: proc(val: Type) -> int
  1848. check_assignment(c, operand, NULL, make_string("argument of `align_of_val`"));
  1849. if (operand->mode == Addressing_Invalid)
  1850. return false;
  1851. operand->mode = Addressing_Constant;
  1852. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, operand->type));
  1853. operand->type = t_int;
  1854. break;
  1855. case BuiltinProc_offset_of: {
  1856. // offset_val :: proc(Type, field) -> int
  1857. Operand op = {};
  1858. check_expr_or_type(c, &op, ce->args[0]);
  1859. Type *type = get_base_type(op.type);
  1860. AstNode *field_arg = unparen_expr(ce->args[1]);
  1861. if (type != NULL) {
  1862. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a type for `offset_of`");
  1863. return false;
  1864. }
  1865. if (!is_type_struct(type)) {
  1866. error(&c->error_collector, ast_node_token(ce->args[0]), "Expected a structure type for `offset_of`");
  1867. return false;
  1868. }
  1869. if (field_arg == NULL ||
  1870. field_arg->kind != AstNode_Ident) {
  1871. error(&c->error_collector, ast_node_token(field_arg), "Expected an identifier for field argument");
  1872. return false;
  1873. }
  1874. ast_node(arg, Ident, field_arg);
  1875. Selection sel = lookup_field(type, arg->string, operand->mode == Addressing_Type);
  1876. if (sel.entity == NULL) {
  1877. gbString type_str = type_to_string(type);
  1878. error(&c->error_collector, ast_node_token(ce->args[0]),
  1879. "`%s` has no field named `%.*s`", type_str, LIT(arg->string));
  1880. return false;
  1881. }
  1882. operand->mode = Addressing_Constant;
  1883. // IMPORTANT TODO(bill): Fix for anonymous fields
  1884. operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0]));
  1885. operand->type = t_int;
  1886. } break;
  1887. case BuiltinProc_offset_of_val: {
  1888. // offset_val :: proc(val: expression) -> int
  1889. AstNode *arg = unparen_expr(ce->args[0]);
  1890. if (arg->kind != AstNode_SelectorExpr) {
  1891. gbString str = expr_to_string(arg);
  1892. error(&c->error_collector, ast_node_token(arg), "`%s` is not a selector expression", str);
  1893. return false;
  1894. }
  1895. ast_node(s, SelectorExpr, arg);
  1896. check_expr(c, operand, s->expr);
  1897. if (operand->mode == Addressing_Invalid)
  1898. return false;
  1899. Type *type = operand->type;
  1900. if (get_base_type(type)->kind == Type_Pointer) {
  1901. Type *p = get_base_type(type);
  1902. if (is_type_struct(p)) {
  1903. type = p->Pointer.elem;
  1904. }
  1905. }
  1906. ast_node(i, Ident, s->selector);
  1907. Selection sel = lookup_field(type, i->string, operand->mode == Addressing_Type);
  1908. if (sel.entity == NULL) {
  1909. gbString type_str = type_to_string(type);
  1910. error(&c->error_collector, ast_node_token(arg),
  1911. "`%s` has no field named `%.*s`", type_str, LIT(i->string));
  1912. return false;
  1913. }
  1914. operand->mode = Addressing_Constant;
  1915. // IMPORTANT TODO(bill): Fix for anonymous fields
  1916. operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0]));
  1917. operand->type = t_int;
  1918. } break;
  1919. case BuiltinProc_type_of_val:
  1920. // type_of_val :: proc(val: Type) -> type(Type)
  1921. check_assignment(c, operand, NULL, make_string("argument of `type_of_val`"));
  1922. if (operand->mode == Addressing_Invalid)
  1923. return false;
  1924. operand->mode = Addressing_Type;
  1925. break;
  1926. case BuiltinProc_type_info: {
  1927. // type_info :: proc(val_or_type) -> ^Type_Info
  1928. Operand op = {};
  1929. check_expr_or_type(c, &op, ce->args[0]);
  1930. add_type_info_type(c, op.type);
  1931. operand->mode = Addressing_Value;
  1932. operand->type = t_type_info_ptr;
  1933. } break;
  1934. case BuiltinProc_compile_assert:
  1935. // compile_assert :: proc(cond: bool)
  1936. if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
  1937. gbString str = expr_to_string(ce->args[0]);
  1938. defer (gb_string_free(str));
  1939. error(&c->error_collector, ast_node_token(call),
  1940. "`%s` is not a constant boolean", str);
  1941. return false;
  1942. }
  1943. if (!operand->value.value_bool) {
  1944. gbString str = expr_to_string(ce->args[0]);
  1945. defer (gb_string_free(str));
  1946. error(&c->error_collector, ast_node_token(call),
  1947. "Compile time assertion: `%s`", str);
  1948. }
  1949. break;
  1950. case BuiltinProc_assert:
  1951. // assert :: proc(cond: bool)
  1952. if (!is_type_boolean(operand->type)) {
  1953. gbString str = expr_to_string(ce->args[0]);
  1954. defer (gb_string_free(str));
  1955. error(&c->error_collector, ast_node_token(call),
  1956. "`%s` is not a boolean", str);
  1957. return false;
  1958. }
  1959. if (operand->mode != Addressing_Constant) {
  1960. operand->mode = Addressing_NoValue;
  1961. }
  1962. break;
  1963. case BuiltinProc_copy: {
  1964. // copy :: proc(x, y: []Type) -> int
  1965. Type *dest_type = NULL, *src_type = NULL;
  1966. Type *d = get_base_type(operand->type);
  1967. if (d->kind == Type_Slice)
  1968. dest_type = d->Slice.elem;
  1969. Operand op = {};
  1970. check_expr(c, &op, ce->args[1]);
  1971. if (op.mode == Addressing_Invalid)
  1972. return false;
  1973. Type *s = get_base_type(op.type);
  1974. if (s->kind == Type_Slice)
  1975. src_type = s->Slice.elem;
  1976. if (dest_type == NULL || src_type == NULL) {
  1977. error(&c->error_collector, ast_node_token(call), "`copy` only expects slices as arguments");
  1978. return false;
  1979. }
  1980. if (!are_types_identical(dest_type, src_type)) {
  1981. gbString d_arg = expr_to_string(ce->args[0]);
  1982. gbString s_arg = expr_to_string(ce->args[1]);
  1983. gbString d_str = type_to_string(dest_type);
  1984. gbString s_str = type_to_string(src_type);
  1985. defer (gb_string_free(d_arg));
  1986. defer (gb_string_free(s_arg));
  1987. defer (gb_string_free(d_str));
  1988. defer (gb_string_free(s_str));
  1989. error(&c->error_collector, ast_node_token(call),
  1990. "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
  1991. d_arg, s_arg, d_str, s_str);
  1992. return false;
  1993. }
  1994. operand->type = t_int; // Returns number of elems copied
  1995. operand->mode = Addressing_Value;
  1996. } break;
  1997. case BuiltinProc_append: {
  1998. // append :: proc(x : ^[]Type, y : Type) -> bool
  1999. Type *x_type = NULL, *y_type = NULL;
  2000. x_type = get_base_type(operand->type);
  2001. Operand op = {};
  2002. check_expr(c, &op, ce->args[1]);
  2003. if (op.mode == Addressing_Invalid)
  2004. return false;
  2005. y_type = get_base_type(op.type);
  2006. if (!(is_type_pointer(x_type) && is_type_slice(x_type->Pointer.elem))) {
  2007. error(&c->error_collector, ast_node_token(call), "First argument to `append` must be a pointer to a slice");
  2008. return false;
  2009. }
  2010. Type *elem_type = x_type->Pointer.elem->Slice.elem;
  2011. if (!check_is_assignable_to(c, &op, elem_type)) {
  2012. gbString d_arg = expr_to_string(ce->args[0]);
  2013. gbString s_arg = expr_to_string(ce->args[1]);
  2014. gbString d_str = type_to_string(elem_type);
  2015. gbString s_str = type_to_string(y_type);
  2016. defer (gb_string_free(d_arg));
  2017. defer (gb_string_free(s_arg));
  2018. defer (gb_string_free(d_str));
  2019. defer (gb_string_free(s_str));
  2020. error(&c->error_collector, ast_node_token(call),
  2021. "Arguments to `append`, %s, %s, have different element types: %s vs %s",
  2022. d_arg, s_arg, d_str, s_str);
  2023. return false;
  2024. }
  2025. operand->type = t_bool; // Returns if it was successful
  2026. operand->mode = Addressing_Value;
  2027. } break;
  2028. case BuiltinProc_swizzle: {
  2029. // swizzle :: proc(v: {N}T, T...) -> {M}T
  2030. Type *vector_type = get_base_type(operand->type);
  2031. if (!is_type_vector(vector_type)) {
  2032. gbString type_str = type_to_string(operand->type);
  2033. defer (gb_string_free(type_str));
  2034. error(&c->error_collector, ast_node_token(call),
  2035. "You can only `swizzle` a vector, got `%s`",
  2036. type_str);
  2037. return false;
  2038. }
  2039. isize max_count = vector_type->Vector.count;
  2040. isize arg_count = 0;
  2041. gb_for_array(i, ce->args) {
  2042. if (i == 0) continue;
  2043. AstNode *arg = ce->args[i];
  2044. Operand op = {};
  2045. check_expr(c, &op, arg);
  2046. if (op.mode == Addressing_Invalid)
  2047. return false;
  2048. Type *arg_type = get_base_type(op.type);
  2049. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  2050. error(&c->error_collector, ast_node_token(op.expr), "Indices to `swizzle` must be constant integers");
  2051. return false;
  2052. }
  2053. if (op.value.value_integer < 0) {
  2054. error(&c->error_collector, ast_node_token(op.expr), "Negative `swizzle` index");
  2055. return false;
  2056. }
  2057. if (max_count <= op.value.value_integer) {
  2058. error(&c->error_collector, ast_node_token(op.expr), "`swizzle` index exceeds vector length");
  2059. return false;
  2060. }
  2061. arg_count++;
  2062. }
  2063. if (arg_count > max_count) {
  2064. error(&c->error_collector, ast_node_token(call), "Too many `swizzle` indices, %td > %td", arg_count, max_count);
  2065. return false;
  2066. }
  2067. Type *elem_type = vector_type->Vector.elem;
  2068. operand->type = make_type_vector(c->allocator, elem_type, arg_count);
  2069. operand->mode = Addressing_Value;
  2070. } break;
  2071. case BuiltinProc_ptr_offset: {
  2072. // ptr_offset :: proc(ptr: ^T, offset: int) -> ^T
  2073. // ^T cannot be rawptr
  2074. Type *ptr_type = get_base_type(operand->type);
  2075. if (!is_type_pointer(ptr_type)) {
  2076. gbString type_str = type_to_string(operand->type);
  2077. defer (gb_string_free(type_str));
  2078. error(&c->error_collector, ast_node_token(call),
  2079. "Expected a pointer to `ptr_offset`, got `%s`",
  2080. type_str);
  2081. return false;
  2082. }
  2083. if (ptr_type == t_rawptr) {
  2084. error(&c->error_collector, ast_node_token(call),
  2085. "`rawptr` cannot have pointer arithmetic");
  2086. return false;
  2087. }
  2088. AstNode *offset = ce->args[1];
  2089. Operand op = {};
  2090. check_expr(c, &op, offset);
  2091. if (op.mode == Addressing_Invalid)
  2092. return false;
  2093. Type *offset_type = get_base_type(op.type);
  2094. if (!is_type_integer(offset_type)) {
  2095. error(&c->error_collector, ast_node_token(op.expr), "Pointer offsets for `ptr_offset` must be an integer");
  2096. return false;
  2097. }
  2098. if (operand->mode == Addressing_Constant &&
  2099. op.mode == Addressing_Constant) {
  2100. u8 *ptr = cast(u8 *)operand->value.value_pointer;
  2101. isize elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
  2102. ptr += elem_size * op.value.value_integer;
  2103. operand->value.value_pointer = ptr;
  2104. } else {
  2105. operand->mode = Addressing_Value;
  2106. }
  2107. } break;
  2108. case BuiltinProc_ptr_sub: {
  2109. // ptr_sub :: proc(a, b: ^T) -> int
  2110. // ^T cannot be rawptr
  2111. Type *ptr_type = get_base_type(operand->type);
  2112. if (!is_type_pointer(ptr_type)) {
  2113. gbString type_str = type_to_string(operand->type);
  2114. defer (gb_string_free(type_str));
  2115. error(&c->error_collector, ast_node_token(call),
  2116. "Expected a pointer to `ptr_add`, got `%s`",
  2117. type_str);
  2118. return false;
  2119. }
  2120. if (ptr_type == t_rawptr) {
  2121. error(&c->error_collector, ast_node_token(call),
  2122. "`rawptr` cannot have pointer arithmetic");
  2123. return false;
  2124. }
  2125. AstNode *offset = ce->args[1];
  2126. Operand op = {};
  2127. check_expr(c, &op, offset);
  2128. if (op.mode == Addressing_Invalid)
  2129. return false;
  2130. if (!is_type_pointer(op.type)) {
  2131. gbString type_str = type_to_string(operand->type);
  2132. defer (gb_string_free(type_str));
  2133. error(&c->error_collector, ast_node_token(call),
  2134. "Expected a pointer to `ptr_add`, got `%s`",
  2135. type_str);
  2136. return false;
  2137. }
  2138. if (get_base_type(op.type) == t_rawptr) {
  2139. error(&c->error_collector, ast_node_token(call),
  2140. "`rawptr` cannot have pointer arithmetic");
  2141. return false;
  2142. }
  2143. if (!are_types_identical(operand->type, op.type)) {
  2144. gbString a = type_to_string(operand->type);
  2145. gbString b = type_to_string(op.type);
  2146. defer (gb_string_free(a));
  2147. defer (gb_string_free(b));
  2148. error(&c->error_collector, ast_node_token(op.expr),
  2149. "`ptr_sub` requires to pointer of the same type. Got `%s` and `%s`.", a, b);
  2150. return false;
  2151. }
  2152. operand->type = t_int;
  2153. if (operand->mode == Addressing_Constant &&
  2154. op.mode == Addressing_Constant) {
  2155. u8 *ptr_a = cast(u8 *)operand->value.value_pointer;
  2156. u8 *ptr_b = cast(u8 *)op.value.value_pointer;
  2157. isize elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
  2158. operand->value = make_exact_value_integer((ptr_a - ptr_b) / elem_size);
  2159. } else {
  2160. operand->mode = Addressing_Value;
  2161. }
  2162. } break;
  2163. case BuiltinProc_slice_ptr: {
  2164. // slice_ptr :: proc(a: ^T, len: int[, cap: int]) -> []T
  2165. // ^T cannot be rawptr
  2166. Type *ptr_type = get_base_type(operand->type);
  2167. if (!is_type_pointer(ptr_type)) {
  2168. gbString type_str = type_to_string(operand->type);
  2169. defer (gb_string_free(type_str));
  2170. error(&c->error_collector, ast_node_token(call),
  2171. "Expected a pointer to `slice_ptr`, got `%s`",
  2172. type_str);
  2173. return false;
  2174. }
  2175. if (ptr_type == t_rawptr) {
  2176. error(&c->error_collector, ast_node_token(call),
  2177. "`rawptr` cannot have pointer arithmetic");
  2178. return false;
  2179. }
  2180. AstNode *len = ce->args[1];
  2181. AstNode *cap = NULL;
  2182. if (gb_array_count(ce->args) > 2) {
  2183. cap = ce->args[2];
  2184. }
  2185. Operand op = {};
  2186. check_expr(c, &op, len);
  2187. if (op.mode == Addressing_Invalid)
  2188. return false;
  2189. if (!is_type_integer(op.type)) {
  2190. gbString type_str = type_to_string(operand->type);
  2191. defer (gb_string_free(type_str));
  2192. error(&c->error_collector, ast_node_token(call),
  2193. "Length for `slice_ptr` must be an integer, got `%s`",
  2194. type_str);
  2195. return false;
  2196. }
  2197. if (cap != NULL) {
  2198. check_expr(c, &op, cap);
  2199. if (op.mode == Addressing_Invalid)
  2200. return false;
  2201. if (!is_type_integer(op.type)) {
  2202. gbString type_str = type_to_string(operand->type);
  2203. defer (gb_string_free(type_str));
  2204. error(&c->error_collector, ast_node_token(call),
  2205. "Capacity for `slice_ptr` must be an integer, got `%s`",
  2206. type_str);
  2207. return false;
  2208. }
  2209. if (ce->args[2] != NULL) {
  2210. error(&c->error_collector, ast_node_token(call),
  2211. "Too many arguments to `slice_ptr`, expected either 2 or 3");
  2212. return false;
  2213. }
  2214. }
  2215. operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
  2216. operand->mode = Addressing_Value;
  2217. } break;
  2218. case BuiltinProc_min: {
  2219. // min :: proc(a, b: comparable) -> comparable
  2220. Type *type = get_base_type(operand->type);
  2221. if (!is_type_comparable(type) || !is_type_numeric(type)) {
  2222. gbString type_str = type_to_string(operand->type);
  2223. defer (gb_string_free(type_str));
  2224. error(&c->error_collector, ast_node_token(call),
  2225. "Expected a comparable numeric type to `min`, got `%s`",
  2226. type_str);
  2227. return false;
  2228. }
  2229. AstNode *other_arg = ce->args[1];
  2230. Operand a = *operand;
  2231. Operand b = {};
  2232. check_expr(c, &b, other_arg);
  2233. if (b.mode == Addressing_Invalid)
  2234. return false;
  2235. if (!is_type_comparable(b.type) || !is_type_numeric(type)) {
  2236. gbString type_str = type_to_string(b.type);
  2237. defer (gb_string_free(type_str));
  2238. error(&c->error_collector, ast_node_token(call),
  2239. "Expected a comparable numeric type to `min`, got `%s`",
  2240. type_str);
  2241. return false;
  2242. }
  2243. if (a.mode == Addressing_Constant &&
  2244. b.mode == Addressing_Constant) {
  2245. ExactValue x = a.value;
  2246. ExactValue y = b.value;
  2247. Token lt = {Token_Lt};
  2248. operand->mode = Addressing_Constant;
  2249. if (compare_exact_values(lt, x, y)) {
  2250. operand->value = x;
  2251. operand->type = a.type;
  2252. } else {
  2253. operand->value = y;
  2254. operand->type = b.type;
  2255. }
  2256. } else {
  2257. operand->mode = Addressing_Value;
  2258. operand->type = type;
  2259. if (!are_types_identical(operand->type, b.type)) {
  2260. gbString type_a = type_to_string(a.type);
  2261. gbString type_b = type_to_string(b.type);
  2262. defer (gb_string_free(type_a));
  2263. defer (gb_string_free(type_b));
  2264. error(&c->error_collector, ast_node_token(call),
  2265. "Mismatched types to `min`, `%s` vs `%s`",
  2266. type_a, type_b);
  2267. return false;
  2268. }
  2269. }
  2270. } break;
  2271. case BuiltinProc_max: {
  2272. // min :: proc(a, b: comparable) -> comparable
  2273. Type *type = get_base_type(operand->type);
  2274. if (!is_type_comparable(type) || !is_type_numeric(type)) {
  2275. gbString type_str = type_to_string(operand->type);
  2276. defer (gb_string_free(type_str));
  2277. error(&c->error_collector, ast_node_token(call),
  2278. "Expected a comparable numeric type to `max`, got `%s`",
  2279. type_str);
  2280. return false;
  2281. }
  2282. AstNode *other_arg = ce->args[1];
  2283. Operand a = *operand;
  2284. Operand b = {};
  2285. check_expr(c, &b, other_arg);
  2286. if (b.mode == Addressing_Invalid)
  2287. return false;
  2288. if (!is_type_comparable(b.type) || !is_type_numeric(type)) {
  2289. gbString type_str = type_to_string(b.type);
  2290. defer (gb_string_free(type_str));
  2291. error(&c->error_collector, ast_node_token(call),
  2292. "Expected a comparable numeric type to `max`, got `%s`",
  2293. type_str);
  2294. return false;
  2295. }
  2296. if (a.mode == Addressing_Constant &&
  2297. b.mode == Addressing_Constant) {
  2298. ExactValue x = a.value;
  2299. ExactValue y = b.value;
  2300. Token gt = {Token_Gt};
  2301. operand->mode = Addressing_Constant;
  2302. if (compare_exact_values(gt, x, y)) {
  2303. operand->value = x;
  2304. operand->type = a.type;
  2305. } else {
  2306. operand->value = y;
  2307. operand->type = b.type;
  2308. }
  2309. } else {
  2310. operand->mode = Addressing_Value;
  2311. operand->type = type;
  2312. if (!are_types_identical(operand->type, b.type)) {
  2313. gbString type_a = type_to_string(a.type);
  2314. gbString type_b = type_to_string(b.type);
  2315. defer (gb_string_free(type_a));
  2316. defer (gb_string_free(type_b));
  2317. error(&c->error_collector, ast_node_token(call),
  2318. "Mismatched types to `max`, `%s` vs `%s`",
  2319. type_a, type_b);
  2320. return false;
  2321. }
  2322. }
  2323. } break;
  2324. case BuiltinProc_abs: {
  2325. // abs :: proc(n: numeric) -> numeric
  2326. Type *type = get_base_type(operand->type);
  2327. if (!is_type_numeric(type)) {
  2328. gbString type_str = type_to_string(operand->type);
  2329. defer (gb_string_free(type_str));
  2330. error(&c->error_collector, ast_node_token(call),
  2331. "Expected a numeric type to `abs`, got `%s`",
  2332. type_str);
  2333. return false;
  2334. }
  2335. if (operand->mode == Addressing_Constant) {
  2336. switch (operand->value.kind) {
  2337. case ExactValue_Integer:
  2338. operand->value.value_integer = gb_abs(operand->value.value_integer);
  2339. break;
  2340. case ExactValue_Float:
  2341. operand->value.value_float = gb_abs(operand->value.value_float);
  2342. break;
  2343. default:
  2344. GB_PANIC("Invalid numeric constant");
  2345. break;
  2346. }
  2347. } else {
  2348. operand->mode = Addressing_Value;
  2349. }
  2350. operand->type = type;
  2351. } break;
  2352. }
  2353. return true;
  2354. }
  2355. void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  2356. GB_ASSERT(call->kind == AstNode_CallExpr);
  2357. GB_ASSERT(proc_type->kind == Type_Proc);
  2358. ast_node(ce, CallExpr, call);
  2359. isize error_code = 0;
  2360. isize param_index = 0;
  2361. isize param_count = 0;
  2362. b32 variadic = proc_type->Proc.variadic;
  2363. b32 vari_expand = (ce->ellipsis.pos.line != 0);
  2364. if (proc_type->Proc.params) {
  2365. param_count = proc_type->Proc.params->Tuple.variable_count;
  2366. }
  2367. if (ce->ellipsis.pos.line != 0) {
  2368. if (!variadic) {
  2369. error(&c->error_collector, ce->ellipsis,
  2370. "Cannot use `..` in call to a non-variadic procedure: `%.*s`",
  2371. LIT(ce->proc->Ident.string));
  2372. return;
  2373. }
  2374. }
  2375. if (gb_array_count(ce->args) == 0) {
  2376. if (variadic && param_count-1 == 0)
  2377. return;
  2378. if (param_count == 0)
  2379. return;
  2380. }
  2381. // TODO(bill): Completely redo this entire code.
  2382. // It's from when I used linked lists instead of arrays in the parser
  2383. if (gb_array_count(ce->args) > param_count && !variadic) {
  2384. error_code = +1;
  2385. } else {
  2386. Entity **sig_params = proc_type->Proc.params->Tuple.variables;
  2387. gb_for_array(arg_index, ce->args) {
  2388. check_multi_expr(c, operand, ce->args[arg_index]);
  2389. if (operand->mode == Addressing_Invalid)
  2390. continue;
  2391. if (operand->type->kind != Type_Tuple) {
  2392. check_not_tuple(c, operand);
  2393. isize index = param_index;
  2394. b32 end_variadic = false;
  2395. b32 variadic_expand = false;
  2396. if (variadic && param_index >= param_count-1) {
  2397. index = param_count-1;
  2398. end_variadic = true;
  2399. if (vari_expand) {
  2400. variadic_expand = true;
  2401. if (param_index != param_count-1) {
  2402. error(&c->error_collector, ast_node_token(operand->expr),
  2403. "`..` in a variadic procedure can only have one variadic argument at the end");
  2404. break;
  2405. }
  2406. }
  2407. }
  2408. Type *arg_type = sig_params[index]->type;
  2409. if (end_variadic && is_type_slice(arg_type)) {
  2410. if (variadic_expand) {
  2411. check_assignment(c, operand, arg_type, make_string("argument"), true);
  2412. } else {
  2413. arg_type = get_base_type(arg_type)->Slice.elem;
  2414. check_assignment(c, operand, arg_type, make_string("argument"), true);
  2415. }
  2416. } else {
  2417. check_assignment(c, operand, arg_type, make_string("argument"), true);
  2418. }
  2419. param_index++;
  2420. } else {
  2421. auto *tuple = &operand->type->Tuple;
  2422. isize i = 0;
  2423. for (;
  2424. i < tuple->variable_count && (param_index < param_count && !variadic);
  2425. i++) {
  2426. Entity *e = tuple->variables[i];
  2427. operand->type = e->type;
  2428. operand->mode = Addressing_Value;
  2429. check_not_tuple(c, operand);
  2430. isize index = param_index;
  2431. b32 end_variadic = false;
  2432. if (variadic && param_index >= param_count-1) {
  2433. index = param_count-1;
  2434. end_variadic = true;
  2435. if (vari_expand) {
  2436. error(&c->error_collector, ast_node_token(operand->expr),
  2437. "`..` in a variadic procedure cannot be applied to a %td-valued expression", tuple->variable_count);
  2438. goto end;
  2439. }
  2440. }
  2441. Type *arg_type = sig_params[index]->type;
  2442. if (end_variadic && is_type_slice(arg_type)) {
  2443. arg_type = get_base_type(arg_type)->Slice.elem;
  2444. }
  2445. check_assignment(c, operand, arg_type, make_string("argument"), true);
  2446. param_index++;
  2447. }
  2448. end:
  2449. if (i < tuple->variable_count && param_index == param_count) {
  2450. error_code = +1;
  2451. break;
  2452. }
  2453. }
  2454. if (!variadic && param_index >= param_count)
  2455. break;
  2456. }
  2457. if ((!variadic && param_index < param_count) ||
  2458. (variadic && param_index < param_count-1)) {
  2459. error_code = -1;
  2460. }
  2461. }
  2462. if (error_code != 0) {
  2463. char *err_fmt = "";
  2464. if (error_code < 0) {
  2465. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  2466. } else {
  2467. err_fmt = "Too many arguments for `%s`, expected %td arguments";
  2468. }
  2469. gbString proc_str = expr_to_string(ce->proc);
  2470. error(&c->error_collector, ast_node_token(call), err_fmt, proc_str, param_count);
  2471. gb_string_free(proc_str);
  2472. operand->mode = Addressing_Invalid;
  2473. }
  2474. }
  2475. ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  2476. GB_ASSERT(call->kind == AstNode_CallExpr);
  2477. ast_node(ce, CallExpr, call);
  2478. check_expr_or_type(c, operand, ce->proc);
  2479. if (operand->mode == Addressing_Invalid) {
  2480. gb_for_array(i, ce->args) {
  2481. check_expr_base(c, operand, ce->args[i]);
  2482. }
  2483. operand->mode = Addressing_Invalid;
  2484. operand->expr = call;
  2485. return Expr_Stmt;
  2486. }
  2487. if (operand->mode == Addressing_Builtin) {
  2488. i32 id = operand->builtin_id;
  2489. if (!check_builtin_procedure(c, operand, call, id)) {
  2490. operand->mode = Addressing_Invalid;
  2491. }
  2492. operand->expr = call;
  2493. return builtin_procs[id].kind;
  2494. }
  2495. Type *proc_type = get_base_type(operand->type);
  2496. if (proc_type == NULL || proc_type->kind != Type_Proc) {
  2497. AstNode *e = operand->expr;
  2498. gbString str = expr_to_string(e);
  2499. defer (gb_string_free(str));
  2500. error(&c->error_collector, ast_node_token(e), "Cannot call a non-procedure: `%s`", str);
  2501. operand->mode = Addressing_Invalid;
  2502. operand->expr = call;
  2503. return Expr_Stmt;
  2504. }
  2505. check_call_arguments(c, operand, proc_type, call);
  2506. switch (proc_type->Proc.result_count) {
  2507. case 0:
  2508. operand->mode = Addressing_NoValue;
  2509. break;
  2510. case 1:
  2511. operand->mode = Addressing_Value;
  2512. operand->type = proc_type->Proc.results->Tuple.variables[0]->type;
  2513. break;
  2514. default:
  2515. operand->mode = Addressing_Value;
  2516. operand->type = proc_type->Proc.results;
  2517. break;
  2518. }
  2519. operand->expr = call;
  2520. return Expr_Stmt;
  2521. }
  2522. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  2523. check_expr_base(c, o, e, t);
  2524. check_not_tuple(c, o);
  2525. char *err_str = NULL;
  2526. switch (o->mode) {
  2527. case Addressing_NoValue:
  2528. err_str = "used as a value";
  2529. break;
  2530. case Addressing_Type:
  2531. err_str = "is not an expression";
  2532. break;
  2533. case Addressing_Builtin:
  2534. err_str = "must be called";
  2535. break;
  2536. }
  2537. if (err_str != NULL) {
  2538. gbString str = expr_to_string(e);
  2539. defer (gb_string_free(str));
  2540. error(&c->error_collector, ast_node_token(e), "`%s` %s", str, err_str);
  2541. o->mode = Addressing_Invalid;
  2542. }
  2543. }
  2544. ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  2545. ExprKind kind = Expr_Stmt;
  2546. o->mode = Addressing_Invalid;
  2547. o->type = t_invalid;
  2548. switch (node->kind) {
  2549. case_ast_node(be, BadExpr, node)
  2550. goto error;
  2551. case_end;
  2552. case_ast_node(i, Ident, node);
  2553. check_identifier(c, o, node, type_hint);
  2554. case_end;
  2555. case_ast_node(bl, BasicLit, node);
  2556. Type *t = t_invalid;
  2557. switch (bl->kind) {
  2558. case Token_Integer: t = t_untyped_integer; break;
  2559. case Token_Float: t = t_untyped_float; break;
  2560. case Token_String: t = t_untyped_string; break;
  2561. case Token_Rune: t = t_untyped_rune; break;
  2562. default: GB_PANIC("Unknown literal"); break;
  2563. }
  2564. o->mode = Addressing_Constant;
  2565. o->type = t;
  2566. o->value = make_exact_value_from_basic_literal(*bl);
  2567. case_end;
  2568. case_ast_node(pl, ProcLit, node);
  2569. check_open_scope(c, pl->type);
  2570. c->context.decl = make_declaration_info(c->allocator, c->context.scope);
  2571. defer (check_close_scope(c));
  2572. Type *proc_type = check_type(c, pl->type);
  2573. if (proc_type != NULL) {
  2574. check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body);
  2575. o->mode = Addressing_Value;
  2576. o->type = proc_type;
  2577. } else {
  2578. gbString str = expr_to_string(node);
  2579. error(&c->error_collector, ast_node_token(node), "Invalid procedure literal `%s`", str);
  2580. gb_string_free(str);
  2581. goto error;
  2582. }
  2583. case_end;
  2584. case_ast_node(cl, CompoundLit, node);
  2585. Type *type = type_hint;
  2586. b32 ellipsis_array = false;
  2587. if (cl->type != NULL) {
  2588. type = NULL;
  2589. // [..]Type
  2590. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
  2591. if (cl->type->ArrayType.count->kind == AstNode_Ellipsis) {
  2592. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  2593. ellipsis_array = true;
  2594. }
  2595. }
  2596. if (type == NULL) {
  2597. type = check_type(c, cl->type);
  2598. }
  2599. }
  2600. if (type == NULL) {
  2601. error(&c->error_collector, ast_node_token(node), "Missing type in compound literal");
  2602. goto error;
  2603. }
  2604. Type *t = get_base_type(type);
  2605. switch (t->kind) {
  2606. case Type_Record: {
  2607. if (!is_type_struct(t))
  2608. break;
  2609. if (gb_array_count(cl->elems) == 0) {
  2610. break; // NOTE(bill): No need to init
  2611. }
  2612. { // Checker values
  2613. isize field_count = t->Record.field_count;
  2614. if (cl->elems[0]->kind == AstNode_FieldValue) {
  2615. b32 *fields_visited = gb_alloc_array(c->allocator, b32, field_count);
  2616. gb_for_array(i, cl->elems) {
  2617. AstNode *elem = cl->elems[i];
  2618. if (elem->kind != AstNode_FieldValue) {
  2619. error(&c->error_collector, ast_node_token(elem),
  2620. "Mixture of `field = value` and value elements in a structure literal is not allowed");
  2621. continue;
  2622. }
  2623. ast_node(kv, FieldValue, elem);
  2624. if (kv->field->kind != AstNode_Ident) {
  2625. gbString expr_str = expr_to_string(kv->field);
  2626. defer (gb_string_free(expr_str));
  2627. error(&c->error_collector, ast_node_token(elem),
  2628. "Invalid field name `%s` in structure literal", expr_str);
  2629. continue;
  2630. }
  2631. String name = kv->field->Ident.string;
  2632. Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
  2633. if (sel.entity == NULL) {
  2634. error(&c->error_collector, ast_node_token(elem),
  2635. "Unknown field `%.*s` in structure literal", LIT(name));
  2636. continue;
  2637. }
  2638. if (gb_array_count(sel.index) > 1) {
  2639. error(&c->error_collector, ast_node_token(elem),
  2640. "Cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
  2641. continue;
  2642. }
  2643. Entity *field = t->Record.fields[sel.index[0]];
  2644. add_entity_use(&c->info, kv->field, field);
  2645. if (fields_visited[sel.index[0]]) {
  2646. error(&c->error_collector, ast_node_token(elem),
  2647. "Duplicate field `%.*s` in structure literal", LIT(name));
  2648. continue;
  2649. }
  2650. fields_visited[sel.index[0]] = true;
  2651. check_expr(c, o, kv->value);
  2652. check_assignment(c, o, field->type, make_string("structure literal"));
  2653. }
  2654. } else {
  2655. gb_for_array(index, cl->elems) {
  2656. AstNode *elem = cl->elems[index];
  2657. if (elem->kind == AstNode_FieldValue) {
  2658. error(&c->error_collector, ast_node_token(elem),
  2659. "Mixture of `field = value` and value elements in a structure literal is not allowed");
  2660. continue;
  2661. }
  2662. Entity *field = t->Record.fields_in_src_order[index];
  2663. check_expr(c, o, elem);
  2664. if (index >= field_count) {
  2665. error(&c->error_collector, ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count);
  2666. break;
  2667. }
  2668. check_assignment(c, o, field->type, make_string("structure literal"));
  2669. }
  2670. if (gb_array_count(cl->elems) < field_count) {
  2671. error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, gb_array_count(cl->elems));
  2672. }
  2673. }
  2674. }
  2675. } break;
  2676. case Type_Slice:
  2677. case Type_Array:
  2678. case Type_Vector:
  2679. {
  2680. Type *elem_type = NULL;
  2681. String context_name = {};
  2682. if (t->kind == Type_Slice) {
  2683. elem_type = t->Slice.elem;
  2684. context_name = make_string("slice literal");
  2685. } else if (t->kind == Type_Vector) {
  2686. elem_type = t->Vector.elem;
  2687. context_name = make_string("vector literal");
  2688. } else {
  2689. elem_type = t->Array.elem;
  2690. context_name = make_string("array literal");
  2691. }
  2692. i64 max = 0;
  2693. isize index = 0;
  2694. for (; index < gb_array_count(cl->elems); index++) {
  2695. AstNode *e = cl->elems[index];
  2696. if (e->kind == AstNode_FieldValue) {
  2697. error(&c->error_collector, ast_node_token(e),
  2698. "`field = value` is only allowed in structure literals");
  2699. continue;
  2700. }
  2701. if (t->kind == Type_Array &&
  2702. t->Array.count >= 0 &&
  2703. index >= t->Array.count) {
  2704. error(&c->error_collector, ast_node_token(e), "Index %lld is out of bounds (>= %lld) for array literal", index, t->Array.count);
  2705. }
  2706. if (t->kind == Type_Vector &&
  2707. t->Vector.count >= 0 &&
  2708. index >= t->Vector.count) {
  2709. error(&c->error_collector, ast_node_token(e), "Index %lld is out of bounds (>= %lld) for vector literal", index, t->Vector.count);
  2710. }
  2711. Operand o = {};
  2712. check_expr_with_type_hint(c, &o, e, elem_type);
  2713. check_assignment(c, &o, elem_type, context_name);
  2714. }
  2715. if (max < index)
  2716. max = index;
  2717. if (t->kind == Type_Vector) {
  2718. if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
  2719. error(&c->error_collector, ast_node_token(cl->elems[0]),
  2720. "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
  2721. }
  2722. }
  2723. if (t->kind == Type_Array && ellipsis_array) {
  2724. t->Array.count = max;
  2725. }
  2726. } break;
  2727. default: {
  2728. gbString str = type_to_string(type);
  2729. error(&c->error_collector, ast_node_token(node), "Invalid compound literal type `%s`", str);
  2730. gb_string_free(str);
  2731. goto error;
  2732. } break;
  2733. }
  2734. o->mode = Addressing_Value;
  2735. o->type = type;
  2736. case_end;
  2737. case_ast_node(pe, ParenExpr, node);
  2738. kind = check_expr_base(c, o, pe->expr, type_hint);
  2739. o->expr = node;
  2740. case_end;
  2741. case_ast_node(te, TagExpr, node);
  2742. // TODO(bill): Tag expressions
  2743. error(&c->error_collector, ast_node_token(node), "Tag expressions are not supported yet");
  2744. kind = check_expr_base(c, o, te->expr, type_hint);
  2745. o->expr = node;
  2746. case_end;
  2747. case_ast_node(ue, UnaryExpr, node);
  2748. check_expr(c, o, ue->expr);
  2749. if (o->mode == Addressing_Invalid)
  2750. goto error;
  2751. check_unary_expr(c, o, ue->op, node);
  2752. if (o->mode == Addressing_Invalid)
  2753. goto error;
  2754. case_end;
  2755. case_ast_node(be, BinaryExpr, node);
  2756. check_binary_expr(c, o, node);
  2757. if (o->mode == Addressing_Invalid)
  2758. goto error;
  2759. case_end;
  2760. case_ast_node(se, SelectorExpr, node);
  2761. check_expr_base(c, o, se->expr);
  2762. check_selector(c, o, node);
  2763. case_end;
  2764. case_ast_node(ie, IndexExpr, node);
  2765. check_expr(c, o, ie->expr);
  2766. if (o->mode == Addressing_Invalid)
  2767. goto error;
  2768. b32 valid = false;
  2769. i64 max_count = -1;
  2770. Type *t = get_base_type(o->type);
  2771. switch (t->kind) {
  2772. case Type_Basic:
  2773. if (is_type_string(t)) {
  2774. valid = true;
  2775. if (o->mode == Addressing_Constant) {
  2776. max_count = o->value.value_string.len;
  2777. }
  2778. if (o->mode != Addressing_Variable)
  2779. o->mode = Addressing_Value;
  2780. o->type = t_u8;
  2781. }
  2782. break;
  2783. case Type_Array:
  2784. valid = true;
  2785. max_count = t->Array.count;
  2786. if (o->mode != Addressing_Variable)
  2787. o->mode = Addressing_Value;
  2788. o->type = t->Array.elem;
  2789. break;
  2790. case Type_Vector:
  2791. valid = true;
  2792. max_count = t->Vector.count;
  2793. if (o->mode != Addressing_Variable)
  2794. o->mode = Addressing_Value;
  2795. o->type = t->Vector.elem;
  2796. break;
  2797. case Type_Slice:
  2798. valid = true;
  2799. o->type = t->Slice.elem;
  2800. o->mode = Addressing_Variable;
  2801. break;
  2802. case Type_Pointer: {
  2803. Type *bt = get_base_type(t->Pointer.elem);
  2804. if (bt->kind == Type_Array) {
  2805. valid = true;
  2806. max_count = bt->Array.count;
  2807. o->mode = Addressing_Variable;
  2808. o->type = bt->Array.elem;
  2809. }
  2810. } break;
  2811. }
  2812. if (!valid) {
  2813. gbString str = expr_to_string(o->expr);
  2814. error(&c->error_collector, ast_node_token(o->expr), "Cannot index `%s`", str);
  2815. gb_string_free(str);
  2816. goto error;
  2817. }
  2818. if (ie->index == NULL) {
  2819. gbString str = expr_to_string(o->expr);
  2820. error(&c->error_collector, ast_node_token(o->expr), "Missing index for `%s`", str);
  2821. gb_string_free(str);
  2822. goto error;
  2823. }
  2824. i64 index = 0;
  2825. b32 ok = check_index_value(c, ie->index, max_count, &index);
  2826. case_end;
  2827. case_ast_node(se, SliceExpr, node);
  2828. check_expr(c, o, se->expr);
  2829. if (o->mode == Addressing_Invalid)
  2830. goto error;
  2831. b32 valid = false;
  2832. i64 max_count = -1;
  2833. Type *t = get_base_type(o->type);
  2834. switch (t->kind) {
  2835. case Type_Basic:
  2836. if (is_type_string(t)) {
  2837. valid = true;
  2838. if (o->mode == Addressing_Constant) {
  2839. max_count = o->value.value_string.len;
  2840. }
  2841. if (se->max != NULL) {
  2842. error(&c->error_collector, ast_node_token(se->max), "Max (3rd) index not needed in substring expression");
  2843. }
  2844. o->type = t_string;
  2845. }
  2846. break;
  2847. case Type_Array:
  2848. valid = true;
  2849. max_count = t->Array.count;
  2850. if (o->mode != Addressing_Variable) {
  2851. gbString str = expr_to_string(node);
  2852. error(&c->error_collector, ast_node_token(node), "Cannot slice array `%s`, value is not addressable", str);
  2853. gb_string_free(str);
  2854. goto error;
  2855. }
  2856. o->type = make_type_slice(c->allocator, t->Array.elem);
  2857. break;
  2858. case Type_Slice:
  2859. valid = true;
  2860. break;
  2861. case Type_Pointer: {
  2862. Type *bt = get_base_type(t->Pointer.elem);
  2863. if (bt->kind == Type_Array) {
  2864. valid = true;
  2865. max_count = bt->Array.count;
  2866. o->type = make_type_slice(c->allocator, bt->Array.elem);
  2867. }
  2868. } break;
  2869. }
  2870. if (!valid) {
  2871. gbString str = expr_to_string(o->expr);
  2872. error(&c->error_collector, ast_node_token(o->expr), "Cannot slice `%s`", str);
  2873. gb_string_free(str);
  2874. goto error;
  2875. }
  2876. o->mode = Addressing_Value;
  2877. i64 indices[3] = {};
  2878. AstNode *nodes[3] = {se->low, se->high, se->max};
  2879. for (isize i = 0; i < gb_count_of(nodes); i++) {
  2880. i64 index = max_count;
  2881. if (nodes[i] != NULL) {
  2882. i64 capacity = -1;
  2883. if (max_count >= 0)
  2884. capacity = max_count;
  2885. i64 j = 0;
  2886. if (check_index_value(c, nodes[i], capacity, &j)) {
  2887. index = j;
  2888. }
  2889. } else if (i == 0) {
  2890. index = 0;
  2891. }
  2892. indices[i] = index;
  2893. }
  2894. for (isize i = 0; i < gb_count_of(indices); i++) {
  2895. i64 a = indices[i];
  2896. for (isize j = i+1; j < gb_count_of(indices); j++) {
  2897. i64 b = indices[j];
  2898. if (a > b && b >= 0) {
  2899. error(&c->error_collector, se->close, "Invalid slice indices: [%td > %td]", a, b);
  2900. }
  2901. }
  2902. }
  2903. case_end;
  2904. case_ast_node(ce, CallExpr, node);
  2905. return check_call_expr(c, o, node);
  2906. case_end;
  2907. case_ast_node(de, DerefExpr, node);
  2908. check_expr_or_type(c, o, de->expr);
  2909. if (o->mode == Addressing_Invalid) {
  2910. goto error;
  2911. } else {
  2912. Type *t = get_base_type(o->type);
  2913. if (t->kind == Type_Pointer) {
  2914. o->mode = Addressing_Variable;
  2915. o->type = t->Pointer.elem;
  2916. } else {
  2917. gbString str = expr_to_string(o->expr);
  2918. error(&c->error_collector, ast_node_token(o->expr), "Cannot dereference `%s`", str);
  2919. gb_string_free(str);
  2920. goto error;
  2921. }
  2922. }
  2923. case_end;
  2924. case AstNode_ProcType:
  2925. case AstNode_PointerType:
  2926. case AstNode_ArrayType:
  2927. case AstNode_VectorType:
  2928. case AstNode_StructType:
  2929. case AstNode_RawUnionType:
  2930. o->mode = Addressing_Type;
  2931. o->type = check_type(c, node);
  2932. break;
  2933. }
  2934. kind = Expr_Expr;
  2935. o->expr = node;
  2936. return kind;
  2937. error:
  2938. o->mode = Addressing_Invalid;
  2939. o->expr = node;
  2940. return kind;
  2941. }
  2942. ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  2943. ExprKind kind = check__expr_base(c, o, node, type_hint);
  2944. Type *type = NULL;
  2945. ExactValue value = {ExactValue_Invalid};
  2946. switch (o->mode) {
  2947. case Addressing_Invalid:
  2948. type = t_invalid;
  2949. break;
  2950. case Addressing_NoValue:
  2951. type = NULL;
  2952. break;
  2953. case Addressing_Constant:
  2954. type = o->type;
  2955. value = o->value;
  2956. break;
  2957. default:
  2958. type = o->type;
  2959. break;
  2960. }
  2961. if (type != NULL && is_type_untyped(type)) {
  2962. add_untyped(&c->info, node, false, o->mode, type, value);
  2963. } else {
  2964. add_type_and_value(&c->info, node, o->mode, type, value);
  2965. }
  2966. return kind;
  2967. }
  2968. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  2969. gbString err_str = NULL;
  2970. defer (gb_string_free(err_str));
  2971. check_expr_base(c, o, e);
  2972. switch (o->mode) {
  2973. default:
  2974. return; // NOTE(bill): Valid
  2975. case Addressing_NoValue:
  2976. err_str = expr_to_string(e);
  2977. error(&c->error_collector, ast_node_token(e), "`%s` used as value", err_str);
  2978. break;
  2979. case Addressing_Type:
  2980. err_str = expr_to_string(e);
  2981. error(&c->error_collector, ast_node_token(e), "`%s` is not an expression", err_str);
  2982. break;
  2983. }
  2984. o->mode = Addressing_Invalid;
  2985. }
  2986. void check_not_tuple(Checker *c, Operand *o) {
  2987. if (o->mode == Addressing_Value) {
  2988. // NOTE(bill): Tuples are not first class thus never named
  2989. if (o->type->kind == Type_Tuple) {
  2990. isize count = o->type->Tuple.variable_count;
  2991. GB_ASSERT(count != 1);
  2992. error(&c->error_collector, ast_node_token(o->expr),
  2993. "%td-valued tuple found where single value expected", count);
  2994. o->mode = Addressing_Invalid;
  2995. }
  2996. }
  2997. }
  2998. void check_expr(Checker *c, Operand *o, AstNode *e) {
  2999. check_multi_expr(c, o, e);
  3000. check_not_tuple(c, o);
  3001. }
  3002. void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
  3003. check_expr_base(c, o, e);
  3004. check_not_tuple(c, o);
  3005. if (o->mode == Addressing_NoValue) {
  3006. AstNode *e = o->expr;
  3007. gbString str = expr_to_string(e);
  3008. defer (gb_string_free(str));
  3009. error(&c->error_collector, ast_node_token(e),
  3010. "`%s` used as value or type", str);
  3011. o->mode = Addressing_Invalid;
  3012. }
  3013. }
  3014. gbString write_expr_to_string(gbString str, AstNode *node);
  3015. gbString write_fields_to_string(gbString str, AstNodeArray fields, char *sep) {
  3016. gb_for_array(i, fields) {
  3017. AstNode *field = fields[i];
  3018. ast_node(f, Field, field);
  3019. if (i > 0)
  3020. str = gb_string_appendc(str, sep);
  3021. str = write_expr_to_string(str, field);
  3022. }
  3023. return str;
  3024. }
  3025. gbString string_append_token(gbString str, Token token) {
  3026. if (token.string.len > 0)
  3027. return gb_string_append_length(str, token.string.text, token.string.len);
  3028. return str;
  3029. }
  3030. gbString write_expr_to_string(gbString str, AstNode *node) {
  3031. if (node == NULL)
  3032. return str;
  3033. if (is_ast_node_stmt(node)) {
  3034. GB_ASSERT("stmt passed to write_expr_to_string");
  3035. }
  3036. switch (node->kind) {
  3037. default:
  3038. str = gb_string_appendc(str, "(BadExpr)");
  3039. break;
  3040. case_ast_node(i, Ident, node);
  3041. str = string_append_token(str, *i);
  3042. case_end;
  3043. case_ast_node(bl, BasicLit, node);
  3044. str = string_append_token(str, *bl);
  3045. case_end;
  3046. case_ast_node(pl, ProcLit, node);
  3047. str = write_expr_to_string(str, pl->type);
  3048. case_end;
  3049. case_ast_node(cl, CompoundLit, node);
  3050. str = write_expr_to_string(str, cl->type);
  3051. str = gb_string_appendc(str, "{");
  3052. gb_for_array(i, cl->elems) {
  3053. if (i > 0) {
  3054. str = gb_string_appendc(str, ", ");
  3055. }
  3056. str = write_expr_to_string(str, cl->elems[i]);
  3057. }
  3058. str = gb_string_appendc(str, "}");
  3059. case_end;
  3060. case_ast_node(te, TagExpr, node);
  3061. str = gb_string_appendc(str, "#");
  3062. str = string_append_token(str, te->name);
  3063. str = write_expr_to_string(str, te->expr);
  3064. case_end;
  3065. case_ast_node(ue, UnaryExpr, node);
  3066. str = string_append_token(str, ue->op);
  3067. str = write_expr_to_string(str, ue->expr);
  3068. case_end;
  3069. case_ast_node(de, DerefExpr, node);
  3070. str = write_expr_to_string(str, de->expr);
  3071. str = gb_string_appendc(str, "^");
  3072. case_end;
  3073. case_ast_node(be, BinaryExpr, node);
  3074. str = write_expr_to_string(str, be->left);
  3075. str = gb_string_appendc(str, " ");
  3076. str = string_append_token(str, be->op);
  3077. str = gb_string_appendc(str, " ");
  3078. str = write_expr_to_string(str, be->right);
  3079. case_end;
  3080. case_ast_node(pe, ParenExpr, node);
  3081. str = gb_string_appendc(str, "(");
  3082. str = write_expr_to_string(str, pe->expr);
  3083. str = gb_string_appendc(str, ")");
  3084. case_end;
  3085. case_ast_node(se, SelectorExpr, node);
  3086. str = write_expr_to_string(str, se->expr);
  3087. str = gb_string_appendc(str, ".");
  3088. str = write_expr_to_string(str, se->selector);
  3089. case_end;
  3090. case_ast_node(ie, IndexExpr, node);
  3091. str = write_expr_to_string(str, ie->expr);
  3092. str = gb_string_appendc(str, "[");
  3093. str = write_expr_to_string(str, ie->index);
  3094. str = gb_string_appendc(str, "]");
  3095. case_end;
  3096. case_ast_node(se, SliceExpr, node);
  3097. str = write_expr_to_string(str, se->expr);
  3098. str = gb_string_appendc(str, "[");
  3099. str = write_expr_to_string(str, se->low);
  3100. str = gb_string_appendc(str, ":");
  3101. str = write_expr_to_string(str, se->high);
  3102. if (se->triple_indexed) {
  3103. str = gb_string_appendc(str, ":");
  3104. str = write_expr_to_string(str, se->max);
  3105. }
  3106. str = gb_string_appendc(str, "]");
  3107. case_end;
  3108. case_ast_node(e, Ellipsis, node);
  3109. str = gb_string_appendc(str, "..");
  3110. case_end;
  3111. case_ast_node(fv, FieldValue, node);
  3112. str = write_expr_to_string(str, fv->field);
  3113. str = gb_string_appendc(str, " = ");
  3114. str = write_expr_to_string(str, fv->value);
  3115. case_end;
  3116. case_ast_node(pt, PointerType, node);
  3117. str = gb_string_appendc(str, "^");
  3118. str = write_expr_to_string(str, pt->type);
  3119. case_end;
  3120. case_ast_node(at, ArrayType, node);
  3121. str = gb_string_appendc(str, "[");
  3122. str = write_expr_to_string(str, at->count);
  3123. str = gb_string_appendc(str, "]");
  3124. str = write_expr_to_string(str, at->elem);
  3125. case_end;
  3126. case_ast_node(vt, VectorType, node);
  3127. str = gb_string_appendc(str, "{");
  3128. str = write_expr_to_string(str, vt->count);
  3129. str = gb_string_appendc(str, "}");
  3130. str = write_expr_to_string(str, vt->elem);
  3131. case_end;
  3132. case_ast_node(f, Field, node);
  3133. if (f->is_using) {
  3134. str = gb_string_appendc(str, "using ");
  3135. }
  3136. gb_for_array(i, f->names) {
  3137. AstNode *name = f->names[i];
  3138. if (i > 0)
  3139. str = gb_string_appendc(str, ", ");
  3140. str = write_expr_to_string(str, name);
  3141. }
  3142. str = gb_string_appendc(str, ": ");
  3143. str = write_expr_to_string(str, f->type);
  3144. case_end;
  3145. case_ast_node(ce, CallExpr, node);
  3146. str = write_expr_to_string(str, ce->proc);
  3147. str = gb_string_appendc(str, "(");
  3148. gb_for_array(i, ce->args) {
  3149. AstNode *arg = ce->args[i];
  3150. if (i > 0) {
  3151. str = gb_string_appendc(str, ", ");
  3152. }
  3153. str = write_expr_to_string(str, arg);
  3154. }
  3155. str = gb_string_appendc(str, ")");
  3156. case_end;
  3157. case_ast_node(pt, ProcType, node);
  3158. str = gb_string_appendc(str, "proc(");
  3159. str = write_fields_to_string(str, pt->params, ", ");
  3160. str = gb_string_appendc(str, ")");
  3161. case_end;
  3162. case_ast_node(st, StructType, node);
  3163. str = gb_string_appendc(str, "struct{");
  3164. // str = write_fields_to_string(str, st->decl_list, ", ");
  3165. str = gb_string_appendc(str, "}");
  3166. case_end;
  3167. case_ast_node(st, RawUnionType, node);
  3168. str = gb_string_appendc(str, "raw_union{");
  3169. // str = write_fields_to_string(str, st->decl_list, ", ");
  3170. str = gb_string_appendc(str, "}");
  3171. case_end;
  3172. case_ast_node(et, EnumType, node);
  3173. str = gb_string_appendc(str, "enum ");
  3174. if (et->base_type != NULL) {
  3175. str = write_expr_to_string(str, et->base_type);
  3176. str = gb_string_appendc(str, " ");
  3177. }
  3178. str = gb_string_appendc(str, "{");
  3179. str = gb_string_appendc(str, "}");
  3180. case_end;
  3181. }
  3182. return str;
  3183. }
  3184. gbString expr_to_string(AstNode *expression) {
  3185. return write_expr_to_string(gb_string_make(gb_heap_allocator(), ""), expression);
  3186. }