expr.cpp 92 KB

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