expr.cpp 91 KB

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