expr.cpp 103 KB

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