expr.cpp 104 KB

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