1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277 |
- void check_expr (Checker *c, Operand *operand, AstNode *expression);
- void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
- void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression);
- ExprKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint = NULL);
- Type * check_type (Checker *c, AstNode *expression, Type *named_type = NULL, CycleChecker *cycle_checker = NULL);
- void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
- Entity * check_selector (Checker *c, Operand *operand, AstNode *node);
- void check_not_tuple (Checker *c, Operand *operand);
- b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
- void convert_to_typed (Checker *c, Operand *operand, Type *target_type);
- gbString expr_to_string (AstNode *expression);
- void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type, CycleChecker *cycle_checker = NULL);
- void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
- void update_expr_type (Checker *c, AstNode *e, Type *type, b32 final);
- b32 check_is_assignable_to_using_subtype(Checker *c, Type *dst, Type *src) {
- return false;
- }
- b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argument = false) {
- if (operand->mode == Addressing_Invalid ||
- type == t_invalid) {
- return true;
- }
- Type *s = operand->type;
- if (are_types_identical(s, type))
- return true;
- Type *src = get_base_type(s);
- Type *dst = get_base_type(type);
- if (is_type_untyped(src)) {
- switch (dst->kind) {
- case Type_Basic:
- if (operand->mode == Addressing_Constant)
- return check_value_is_expressible(c, operand->value, dst, NULL);
- if (src->kind == Type_Basic)
- return src->Basic.kind == Basic_UntypedBool && is_type_boolean(dst);
- break;
- case Type_Pointer:
- return src->Basic.kind == Basic_UntypedPointer;
- }
- }
- if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src)))
- return true;
- if (is_type_pointer(dst) && is_type_rawptr(src))
- return true;
- if (is_type_rawptr(dst) && is_type_pointer(src))
- return true;
- if (dst->kind == Type_Array && src->kind == Type_Array) {
- if (are_types_identical(dst->Array.elem, src->Array.elem)) {
- return dst->Array.count == src->Array.count;
- }
- }
- if (dst->kind == Type_Slice && src->kind == Type_Slice) {
- if (are_types_identical(dst->Slice.elem, src->Slice.elem)) {
- return true;
- }
- }
- if (is_argument) {
- // Polymorphism for subtyping
- if (check_is_assignable_to_using_subtype(c, dst, src)) {
- return true;
- }
- }
- return false;
- }
- // NOTE(bill): `content_name` is for debugging
- // TODO(bill): Maybe allow assignment to tuples?
- void check_assignment(Checker *c, Operand *operand, Type *type, String context_name, b32 is_argument = false) {
- check_not_tuple(c, operand);
- if (operand->mode == Addressing_Invalid)
- return;
- if (is_type_untyped(operand->type)) {
- Type *target_type = type;
- if (type == NULL)
- target_type = default_type(operand->type);
- convert_to_typed(c, operand, target_type);
- if (operand->mode == Addressing_Invalid)
- return;
- }
- if (type != NULL) {
- if (!check_is_assignable_to(c, operand, type, is_argument)) {
- gbString type_string = type_to_string(type);
- gbString op_type_string = type_to_string(operand->type);
- gbString expr_str = expr_to_string(operand->expr);
- defer (gb_string_free(type_string));
- defer (gb_string_free(op_type_string));
- defer (gb_string_free(expr_str));
- // TODO(bill): is this a good enough error message?
- error(&c->error_collector, ast_node_token(operand->expr),
- "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
- expr_str,
- op_type_string,
- type_string,
- LIT(context_name));
- operand->mode = Addressing_Invalid;
- }
- }
- }
- void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *> *entity_map) {
- t = get_base_type(type_deref(t));
- gbString str = expr_to_string(node);
- defer (gb_string_free(str));
- switch (t->kind) {
- // IMPORTANT HACK(bill): The positions of fields and field_count
- // must be same for Struct and Union
- case Type_Struct:
- case Type_Union:
- for (isize i = 0; i < t->Struct.field_count; i++) {
- Entity *f = t->Struct.fields[i];
- GB_ASSERT(f->kind == Entity_Variable);
- String name = f->token.string;
- HashKey key = hash_string(name);
- Entity **found = map_get(entity_map, key);
- if (found != NULL) {
- Entity *e = *found;
- // TODO(bill): Better type error
- error(&c->error_collector, e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
- } else {
- map_set(entity_map, key, f);
- add_entity(c, c->context.scope, NULL, f);
- if (f->Variable.anonymous) {
- populate_using_entity_map(c, node, f->type, entity_map);
- }
- }
- }
- break;
- }
- }
- void check_fields(Checker *c, AstNode *node,
- AstNode *field_list, Entity **fields, isize field_count,
- CycleChecker *cycle_checker, String context) {
- Map<Entity *> entity_map = {};
- map_init(&entity_map, gb_heap_allocator());
- defer (map_destroy(&entity_map));
- isize field_index = 0;
- for (AstNode *field = field_list; field != NULL; field = field->next) {
- ast_node(f, Field, field);
- Type *type = check_type(c, f->type, NULL, cycle_checker);
- if (f->is_using) {
- if (f->name_count > 1) {
- error(&c->error_collector, ast_node_token(f->name_list),
- "Cannot apply `using` to more than one of the same type");
- }
- }
- for (AstNode *name = f->name_list; name != NULL; name = name->next) {
- ast_node(i, Ident, name);
- Token name_token = i->token;
- Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, f->is_using);
- HashKey key = hash_string(name_token.string);
- if (map_get(&entity_map, key) != NULL) {
- // TODO(bill): Scope checking already checks the declaration
- error(&c->error_collector, name_token, "`%.*s` is already declared in this %.*s", LIT(name_token.string), LIT(context));
- } else {
- map_set(&entity_map, key, e);
- fields[field_index++] = e;
- }
- add_entity_use(&c->info, name, e);
- }
- if (f->is_using) {
- Type *t = get_base_type(type_deref(type));
- if (t->kind != Type_Struct &&
- t->kind != Type_Union) {
- Token name_token = f->name_list->Ident.token;
- error(&c->error_collector, name_token, "`using` on a field `%.*s` must be a structure or union", LIT(name_token.string));
- continue;
- }
- populate_using_entity_map(c, node, type, &entity_map);
- }
- }
- }
- void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
- void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) {
- GB_ASSERT(node->kind == AstNode_StructType);
- GB_ASSERT(struct_type->kind == Type_Struct);
- ast_node(st, StructType, node);
- isize field_count = 0;
- isize other_field_count = 0;
- for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
- switch (decl->kind) {
- case_ast_node(vd, VarDecl, decl);
- if (vd->kind == Declaration_Mutable) {
- field_count += vd->name_count;
- } else {
- other_field_count += vd->name_count;
- }
- case_end;
- case_ast_node(td, TypeDecl, decl);
- other_field_count += 1;
- case_end;
- }
- }
- Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
- Map<Entity *> entity_map = {};
- map_init(&entity_map, gb_heap_allocator());
- defer (map_destroy(&entity_map));
- Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
- isize other_field_index = 0;
- // TODO(bill): Random declarations with DeclInfo
- #if 0
- Entity *e;
- DeclInfo *d;d
- check_entity_decl(c, e, d, NULL);
- #endif
- for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
- if (decl->kind == AstNode_VarDecl) {
- ast_node(vd, VarDecl, decl);
- if (vd->kind != Declaration_Immutable)
- continue;
- isize entity_count = vd->name_count;
- isize entity_index = 0;
- Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
- for (AstNode *name = vd->name_list, *value = vd->value_list;
- name != NULL && value != NULL;
- name = name->next, value = value->next) {
- GB_ASSERT(name->kind == AstNode_Ident);
- ExactValue v = {ExactValue_Invalid};
- ast_node(i, Ident, name);
- Token name_token = i->token;
- Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, NULL, v);
- entities[entity_index++] = e;
- check_const_decl(c, e, vd->type, value);
- }
- isize lhs_count = vd->name_count;
- isize rhs_count = vd->value_count;
- // TODO(bill): Better error messages or is this good enough?
- if (rhs_count == 0 && vd->type == NULL) {
- error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
- } else if (lhs_count < rhs_count) {
- error(&c->error_collector, ast_node_token(node), "Extra initial expression");
- }
- AstNode *name = vd->name_list;
- for (isize i = 0; i < entity_count; i++, name = name->next) {
- Entity *e = entities[i];
- Token name_token = name->Ident.token;
- HashKey key = hash_string(name_token.string);
- if (map_get(&entity_map, key) != NULL) {
- // TODO(bill): Scope checking already checks the declaration
- error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
- } else {
- map_set(&entity_map, key, e);
- other_fields[other_field_index++] = e;
- }
- add_entity(c, c->context.scope, name, e);
- }
- } else if (decl->kind == AstNode_TypeDecl) {
- ast_node(td, TypeDecl, decl);
- ast_node(name, Ident, td->name);
- Token name_token = name->token;
- Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->token, NULL);
- check_type_decl(c, e, td->type, NULL, NULL);
- add_entity(c, c->context.scope, td->name, e);
- HashKey key = hash_string(name_token.string);
- if (map_get(&entity_map, key) != NULL) {
- // TODO(bill): Scope checking already checks the declaration
- error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
- } else {
- map_set(&entity_map, key, e);
- other_fields[other_field_index++] = e;
- }
- add_entity_use(&c->info, td->name, e);
- }
- }
- isize field_index = 0;
- for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
- if (decl->kind != AstNode_VarDecl)
- continue;
- ast_node(vd, VarDecl, decl);
- if (vd->kind != Declaration_Mutable)
- continue;
- Type *type = check_type(c, vd->type, NULL, cycle_checker);
- if (vd->is_using) {
- if (vd->name_count > 1) {
- error(&c->error_collector, ast_node_token(vd->name_list),
- "Cannot apply `using` to more than one of the same type");
- }
- }
- for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
- ast_node(i, Ident, name);
- Token name_token = i->token;
- Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, vd->is_using);
- HashKey key = hash_string(name_token.string);
- if (map_get(&entity_map, key) != NULL) {
- // TODO(bill): Scope checking already checks the declaration
- error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
- } else {
- map_set(&entity_map, key, e);
- fields[field_index++] = e;
- add_entity(c, c->context.scope, name, e);
- }
- add_entity_use(&c->info, name, e);
- }
- if (vd->is_using) {
- Type *t = get_base_type(type_deref(type));
- if (t->kind != Type_Struct &&
- t->kind != Type_Union) {
- Token name_token = vd->name_list->Ident.token;
- error(&c->error_collector, name_token, "`using` on a field `%.*s` must be a structure or union", LIT(name_token.string));
- continue;
- }
- populate_using_entity_map(c, node, type, &entity_map);
- }
- }
- struct_type->Struct.is_packed = st->is_packed;
- struct_type->Struct.fields = fields;
- struct_type->Struct.field_count = field_count;
- struct_type->Struct.other_fields = other_fields;
- struct_type->Struct.other_field_count = other_field_count;
- }
- void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
- GB_ASSERT(node->kind == AstNode_UnionType);
- GB_ASSERT(union_type->kind == Type_Union);
- ast_node(ut, UnionType, node);
- isize field_count = 0;
- for (AstNode *field = ut->field_list; field != NULL; field = field->next) {
- for (AstNode *name = field->Field.name_list; name != NULL; name = name->next) {
- GB_ASSERT(name->kind == AstNode_Ident);
- field_count++;
- }
- }
- Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
- check_fields(c, node, ut->field_list, fields, field_count, cycle_checker, make_string("union"));
- union_type->Union.fields = fields;
- union_type->Union.field_count = field_count;
- }
- void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
- GB_ASSERT(node->kind == AstNode_EnumType);
- GB_ASSERT(enum_type->kind == Type_Enum);
- ast_node(et, EnumType, node);
- Map<Entity *> entity_map = {};
- map_init(&entity_map, gb_heap_allocator());
- defer (map_destroy(&entity_map));
- Type *base_type = t_int;
- if (et->base_type != NULL) {
- base_type = check_type(c, et->base_type);
- }
- if (base_type == NULL || !is_type_integer(base_type)) {
- error(&c->error_collector, et->token, "Base type for enumeration must be an integer");
- return;
- } else
- if (base_type == NULL) {
- base_type = t_int;
- }
- enum_type->Enum.base = base_type;
- Entity **fields = gb_alloc_array(c->allocator, Entity *, et->field_count);
- isize field_index = 0;
- ExactValue iota = make_exact_value_integer(-1);
- for (AstNode *field = et->field_list; field != NULL; field = field->next) {
- ast_node(f, FieldValue, field);
- Token name_token = f->field->Ident.token;
- Operand o = {};
- if (f->value != NULL) {
- check_expr(c, &o, f->value);
- if (o.mode != Addressing_Constant) {
- error(&c->error_collector, ast_node_token(f->value), "Enumeration value must be a constant integer");
- o.mode = Addressing_Invalid;
- }
- if (o.mode != Addressing_Invalid) {
- check_assignment(c, &o, base_type, make_string("enumeration"));
- }
- if (o.mode != Addressing_Invalid) {
- iota = o.value;
- } else {
- Token add_token = {Token_Add};
- iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
- }
- } else {
- Token add_token = {Token_Add};
- iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
- }
- Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, enum_type, iota);
- HashKey key = hash_string(name_token.string);
- if (map_get(&entity_map, key)) {
- // TODO(bill): Scope checking already checks the declaration
- error(&c->error_collector, name_token, "`%.*s` is already declared in this enumeration", LIT(name_token.string));
- } else {
- map_set(&entity_map, key, e);
- fields[field_index++] = e;
- }
- add_entity_use(&c->info, f->field, e);
- }
- enum_type->Enum.fields = fields;
- enum_type->Enum.field_count = et->field_count;
- }
- Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
- if (field_list == NULL || field_count == 0)
- return NULL;
- Type *tuple = make_type_tuple(c->allocator);
- Entity **variables = gb_alloc_array(c->allocator, Entity *, field_count);
- isize variable_index = 0;
- for (AstNode *field = field_list; field != NULL; field = field->next) {
- ast_node(f, Field, field);
- AstNode *type_expr = f->type;
- if (type_expr) {
- Type *type = check_type(c, type_expr);
- for (AstNode *name = f->name_list; name != NULL; name = name->next) {
- if (name->kind == AstNode_Ident) {
- ast_node(i, Ident, name);
- Entity *param = make_entity_param(c->allocator, scope, i->token, type);
- add_entity(c, scope, name, param);
- variables[variable_index++] = param;
- } else {
- error(&c->error_collector, ast_node_token(name), "Invalid parameter (invalid AST)");
- }
- }
- }
- }
- tuple->Tuple.variables = variables;
- tuple->Tuple.variable_count = field_count;
- return tuple;
- }
- Type *check_get_results(Checker *c, Scope *scope, AstNode *list, isize list_count) {
- if (list == NULL)
- return NULL;
- Type *tuple = make_type_tuple(c->allocator);
- Entity **variables = gb_alloc_array(c->allocator, Entity *, list_count);
- isize variable_index = 0;
- for (AstNode *item = list; item != NULL; item = item->next) {
- Type *type = check_type(c, item);
- Token token = ast_node_token(item);
- token.string = make_string(""); // NOTE(bill): results are not named
- // TODO(bill): Should I have named results?
- Entity *param = make_entity_param(c->allocator, scope, token, type);
- // NOTE(bill): No need to record
- variables[variable_index++] = param;
- }
- tuple->Tuple.variables = variables;
- tuple->Tuple.variable_count = list_count;
- return tuple;
- }
- void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
- ast_node(pt, ProcType, proc_type_node);
- isize param_count = pt->param_count;
- isize result_count = pt->result_count;
- // gb_printf("%td -> %td\n", param_count, result_count);
- Type *params = check_get_params(c, c->context.scope, pt->param_list, param_count);
- Type *results = check_get_results(c, c->context.scope, pt->result_list, result_count);
- type->Proc.scope = c->context.scope;
- type->Proc.params = params;
- type->Proc.param_count = pt->param_count;
- type->Proc.results = results;
- type->Proc.result_count = pt->result_count;
- }
- void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type, CycleChecker *cycle_checker = NULL) {
- GB_ASSERT(n->kind == AstNode_Ident);
- o->mode = Addressing_Invalid;
- o->expr = n;
- ast_node(i, Ident, n);
- Entity *e = scope_lookup_entity(c, c->context.scope, i->token.string);
- if (e == NULL) {
- error(&c->error_collector, i->token,
- "Undeclared type or identifier `%.*s`", LIT(i->token.string));
- return;
- }
- add_entity_use(&c->info, n, e);
- CycleChecker local_cycle_checker = {};
- if (cycle_checker == NULL) {
- cycle_checker = &local_cycle_checker;
- }
- defer (if (local_cycle_checker.path != NULL) {
- gb_array_free(local_cycle_checker.path);
- });
- if (e->type == NULL) {
- auto *found = map_get(&c->info.entities, hash_pointer(e));
- if (found != NULL) {
- check_entity_decl(c, e, *found, named_type, cycle_checker);
- } else {
- GB_PANIC("Internal Compiler Error: DeclInfo not found!");
- }
- }
- if (e->type == NULL) {
- GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(i->token.string));
- return;
- }
- Type *type = e->type;
- switch (e->kind) {
- case Entity_Constant:
- add_declaration_dependency(c, e);
- if (type == t_invalid)
- return;
- o->value = e->Constant.value;
- GB_ASSERT(o->value.kind != ExactValue_Invalid);
- o->mode = Addressing_Constant;
- break;
- case Entity_Variable:
- add_declaration_dependency(c, e);
- e->Variable.used = true;
- if (type == t_invalid)
- return;
- o->mode = Addressing_Variable;
- break;
- case Entity_TypeName: {
- o->mode = Addressing_Type;
- #if 0
- // TODO(bill): Fix cyclical dependancy checker
- gb_for_array(i, cycle_checker->path) {
- Entity *prev = cycle_checker->path[i];
- if (prev == e) {
- error(&c->error_collector, e->token, "Illegal declaration cycle for %.*s", LIT(e->token.string));
- for (isize j = i; j < gb_array_count(cycle_checker->path); j++) {
- Entity *ref = cycle_checker->path[j];
- error(&c->error_collector, ref->token, "\t%.*s refers to", LIT(ref->token.string));
- }
- error(&c->error_collector, e->token, "\t%.*s", LIT(e->token.string));
- type = t_invalid;
- break;
- }
- }
- #endif
- } break;
- case Entity_Procedure:
- add_declaration_dependency(c, e);
- o->mode = Addressing_Value;
- break;
- case Entity_Builtin:
- o->builtin_id = e->Builtin.id;
- o->mode = Addressing_Builtin;
- break;
- case Entity_UsingVariable:
- // TODO(bill): Entity_UsingVariable: is this correct?
- o->mode = Addressing_Variable;
- break;
- default:
- GB_PANIC("Compiler error: Unknown EntityKind");
- break;
- }
- o->type = type;
- }
- i64 check_array_count(Checker *c, AstNode *e) {
- if (e) {
- Operand o = {};
- check_expr(c, &o, e);
- if (o.mode != Addressing_Constant) {
- if (o.mode != Addressing_Invalid) {
- error(&c->error_collector, ast_node_token(e), "Array count must be a constant");
- }
- return 0;
- }
- if (is_type_untyped(o.type) || is_type_integer(o.type)) {
- if (o.value.kind == ExactValue_Integer) {
- i64 count = o.value.value_integer;
- if (count >= 0)
- return count;
- error(&c->error_collector, ast_node_token(e), "Invalid array count");
- return 0;
- }
- }
- error(&c->error_collector, ast_node_token(e), "Array count must be an integer");
- }
- return 0;
- }
- Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_checker) {
- ExactValue null_value = {ExactValue_Invalid};
- Type *type = NULL;
- gbString err_str = NULL;
- defer (gb_string_free(err_str));
- switch (e->kind) {
- case_ast_node(i, Ident, e);
- Operand operand = {};
- check_identifier(c, &operand, e, named_type, cycle_checker);
- switch (operand.mode) {
- case Addressing_Type: {
- type = operand.type;
- set_base_type(named_type, type);
- goto end;
- } break;
- case Addressing_Invalid:
- break;
- case Addressing_NoValue:
- err_str = expr_to_string(e);
- error(&c->error_collector, ast_node_token(e), "`%s` used as a type", err_str);
- break;
- default:
- err_str = expr_to_string(e);
- error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
- break;
- }
- case_end;
- case_ast_node(se, SelectorExpr, e);
- Operand o = {};
- o.mode = Addressing_Type;
- o.type = check_type(c, se->expr, named_type, cycle_checker);
- // gb_printf_err("mode: %.*s\n", LIT(addressing_mode_strings[o.mode]));
- check_selector(c, &o, e);
- // gb_printf_err("%s.%s\n", expr_to_string(se->expr), expr_to_string(se->selector));
- // gb_printf_err("%s\n", type_to_string(o.type));
- // gb_printf_err("mode: %.*s\n", LIT(addressing_mode_strings[o.mode]));
- if (o.mode == Addressing_Type) {
- set_base_type(type, o.type);
- return o.type;
- }
- case_end;
- case_ast_node(pe, ParenExpr, e);
- return check_type(c, pe->expr, named_type, cycle_checker);
- case_end;
- case_ast_node(at, ArrayType, e);
- if (at->count != NULL) {
- type = make_type_array(c->allocator,
- check_type(c, at->elem, NULL, cycle_checker),
- check_array_count(c, at->count));
- set_base_type(named_type, type);
- } else {
- type = make_type_slice(c->allocator, check_type(c, at->elem));
- set_base_type(named_type, type);
- }
- goto end;
- case_end;
- case_ast_node(vt, VectorType, e);
- Type *elem = check_type(c, vt->elem);
- Type *be = get_base_type(elem);
- i64 count = check_array_count(c, vt->count);
- if (!is_type_boolean(be) && !is_type_numeric(be)) {
- err_str = type_to_string(elem);
- error(&c->error_collector, ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
- }
- type = make_type_vector(c->allocator, elem, count);
- set_base_type(named_type, type);
- goto end;
- case_end;
- case_ast_node(st, StructType, e);
- type = make_type_struct(c->allocator);
- set_base_type(named_type, type);
- check_open_scope(c, e);
- check_struct_type(c, type, e, cycle_checker);
- check_close_scope(c);
- type->Struct.node = e;
- goto end;
- case_end;
- case_ast_node(st, UnionType, e);
- type = make_type_union(c->allocator);
- set_base_type(named_type, type);
- check_open_scope(c, e);
- check_union_type(c, type, e, cycle_checker);
- check_close_scope(c);
- type->Union.node = e;
- goto end;
- case_end;
- case_ast_node(et, EnumType, e);
- type = make_type_enum(c->allocator);
- set_base_type(named_type, type);
- check_enum_type(c, type, e);
- goto end;
- case_end;
- case_ast_node(pt, PointerType, e);
- type = make_type_pointer(c->allocator, check_type(c, pt->type));
- set_base_type(named_type, type);
- goto end;
- case_end;
- case_ast_node(pt, ProcType, e);
- type = alloc_type(c->allocator, Type_Proc);
- set_base_type(named_type, type);
- check_procedure_type(c, type, e);
- goto end;
- case_end;
- default:
- err_str = expr_to_string(e);
- error(&c->error_collector, ast_node_token(e), "`%s` is not a type", err_str);
- break;
- }
- type = t_invalid;
- set_base_type(named_type, type);
- end:
- GB_ASSERT(is_type_typed(type));
- add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
- return type;
- }
- b32 check_unary_op(Checker *c, Operand *o, Token op) {
- // TODO(bill): Handle errors correctly
- Type *type = get_base_type(base_vector_type(get_base_type(o->type)));
- gbString str = NULL;
- defer (gb_string_free(str));
- switch (op.kind) {
- case Token_Add:
- case Token_Sub:
- if (!is_type_numeric(type)) {
- str = expr_to_string(o->expr);
- error(&c->error_collector, op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
- }
- break;
- case Token_Xor:
- if (!is_type_integer(type)) {
- error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
- }
- break;
- case Token_Not:
- if (!is_type_boolean(type)) {
- str = expr_to_string(o->expr);
- error(&c->error_collector, op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
- }
- break;
- default:
- error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
- return false;
- }
- return true;
- }
- b32 check_binary_op(Checker *c, Operand *o, Token op) {
- // TODO(bill): Handle errors correctly
- Type *type = get_base_type(base_vector_type(o->type));
- switch (op.kind) {
- case Token_Add:
- case Token_Sub:
- case Token_Mul:
- case Token_Quo:
- case Token_AddEq:
- case Token_SubEq:
- case Token_MulEq:
- case Token_QuoEq:
- if (!is_type_numeric(type)) {
- error(&c->error_collector, op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_Mod:
- case Token_And:
- case Token_Or:
- case Token_Xor:
- case Token_AndNot:
- case Token_ModEq:
- case Token_AndEq:
- case Token_OrEq:
- case Token_XorEq:
- case Token_AndNotEq:
- if (!is_type_integer(type)) {
- error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
- return false;
- }
- break;
- case Token_CmpAnd:
- case Token_CmpOr:
- case Token_CmpAndEq:
- case Token_CmpOrEq:
- if (!is_type_boolean(type)) {
- error(&c->error_collector, op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
- return false;
- }
- break;
- default:
- error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
- return false;
- }
- return true;
- }
- b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
- if (in_value.kind == ExactValue_Invalid)
- return true;
- if (is_type_boolean(type)) {
- return in_value.kind == ExactValue_Bool;
- } else if (is_type_string(type)) {
- return in_value.kind == ExactValue_String;
- } else if (is_type_integer(type)) {
- if (in_value.kind != ExactValue_Integer)
- return false;
- if (out_value) *out_value = in_value;
- i64 i = in_value.value_integer;
- i64 s = 8*type_size_of(c->sizes, c->allocator, type);
- u64 umax = ~0ull;
- if (s < 64) {
- umax = (1ull << s) - 1ull;
- }
- i64 imax = (1ll << (s-1ll));
- switch (type->Basic.kind) {
- case Basic_i8:
- case Basic_i16:
- case Basic_i32:
- case Basic_i64:
- case Basic_int:
- return gb_is_between(i, -imax, imax-1);
- case Basic_u8:
- case Basic_u16:
- case Basic_u32:
- case Basic_u64:
- case Basic_uint:
- return !(i < 0 || cast(u64)i > umax);
- case Basic_UntypedInteger:
- return true;
- default: GB_PANIC("Compiler error: Unknown integer type!"); break;
- }
- } else if (is_type_float(type)) {
- ExactValue v = exact_value_to_float(in_value);
- if (v.kind != ExactValue_Float)
- return false;
- switch (type->Basic.kind) {
- case Basic_f32:
- if (out_value) *out_value = v;
- return true;
- case Basic_f64:
- if (out_value) *out_value = v;
- return true;
- case Basic_UntypedFloat:
- return true;
- }
- } else if (is_type_pointer(type)) {
- if (in_value.kind == ExactValue_Pointer)
- return true;
- if (in_value.kind == ExactValue_Integer)
- return true;
- if (out_value) *out_value = in_value;
- }
- return false;
- }
- void check_is_expressible(Checker *c, Operand *o, Type *type) {
- GB_ASSERT(type->kind == Type_Basic);
- GB_ASSERT(o->mode == Addressing_Constant);
- if (!check_value_is_expressible(c, o->value, type, &o->value)) {
- gbString a = expr_to_string(o->expr);
- gbString b = type_to_string(type);
- defer (gb_string_free(a));
- defer (gb_string_free(b));
- if (is_type_numeric(o->type) && is_type_numeric(type)) {
- if (!is_type_integer(o->type) && is_type_integer(type)) {
- error(&c->error_collector, ast_node_token(o->expr), "`%s` truncated to `%s`", a, b);
- } else {
- error(&c->error_collector, ast_node_token(o->expr), "`%s = %lld` overflows `%s`", a, o->value.value_integer, b);
- }
- } else {
- error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
- }
- o->mode = Addressing_Invalid;
- }
- }
- b32 check_is_expr_vector_index(Checker *c, AstNode *expr) {
- // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
- expr = unparen_expr(expr);
- if (expr->kind == AstNode_IndexExpr) {
- ast_node(ie, IndexExpr, expr);
- Type *t = type_of_expr(&c->info, ie->expr);
- if (t != NULL) {
- return is_type_vector(get_base_type(t));
- }
- }
- return false;
- }
- void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
- if (op.kind == Token_Pointer) { // Pointer address
- if (o->mode != Addressing_Variable ||
- check_is_expr_vector_index(c, o->expr)) {
- ast_node(ue, UnaryExpr, node);
- gbString str = expr_to_string(ue->expr);
- defer (gb_string_free(str));
- error(&c->error_collector, op, "Cannot take the pointer address of `%s`", str);
- o->mode = Addressing_Invalid;
- return;
- }
- o->mode = Addressing_Value;
- o->type = make_type_pointer(c->allocator, o->type);
- return;
- }
- if (!check_unary_op(c, o, op)) {
- o->mode = Addressing_Invalid;
- return;
- }
- if (o->mode == Addressing_Constant) {
- Type *type = get_base_type(o->type);
- GB_ASSERT(type->kind == Type_Basic);
- i32 precision = 0;
- if (is_type_unsigned(type))
- precision = cast(i32)(8 * type_size_of(c->sizes, c->allocator, type));
- o->value = exact_unary_operator_value(op, o->value, precision);
- if (is_type_typed(type)) {
- if (node != NULL)
- o->expr = node;
- check_is_expressible(c, o, type);
- }
- return;
- }
- o->mode = Addressing_Value;
- }
- void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
- gbString err_str = NULL;
- defer ({
- if (err_str != NULL)
- gb_string_free(err_str);
- });
- if (check_is_assignable_to(c, x, y->type) ||
- check_is_assignable_to(c, y, x->type)) {
- b32 defined = false;
- switch (op.kind) {
- case Token_CmpEq:
- case Token_NotEq:
- defined = is_type_comparable(get_base_type(x->type));
- break;
- case Token_Lt:
- case Token_Gt:
- case Token_LtEq:
- case Token_GtEq: {
- defined = is_type_ordered(get_base_type(x->type));
- } break;
- }
- if (!defined) {
- gbString type_string = type_to_string(x->type);
- err_str = gb_string_make(gb_heap_allocator(),
- gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(op.string), type_string));
- gb_string_free(type_string);
- }
- } else {
- gbString xt = type_to_string(x->type);
- gbString yt = type_to_string(y->type);
- defer(gb_string_free(xt));
- defer(gb_string_free(yt));
- err_str = gb_string_make(gb_heap_allocator(),
- gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
- }
- if (err_str != NULL) {
- error(&c->error_collector, op, "Cannot compare expression, %s", err_str);
- return;
- }
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- x->value = make_exact_value_bool(compare_exact_values(op, x->value, y->value));
- } else {
- x->mode = Addressing_Value;
- update_expr_type(c, x->expr, default_type(x->type), true);
- update_expr_type(c, y->expr, default_type(y->type), true);
- }
- if (is_type_vector(get_base_type(y->type))) {
- x->type = make_type_vector(c->allocator, t_bool, get_base_type(y->type)->Vector.count);
- } else {
- x->type = t_untyped_bool;
- }
- }
- void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
- GB_ASSERT(node->kind == AstNode_BinaryExpr);
- ast_node(be, BinaryExpr, node);
- ExactValue x_val = {};
- if (x->mode == Addressing_Constant) {
- x_val = exact_value_to_integer(x->value);
- }
- b32 x_is_untyped = is_type_untyped(x->type);
- if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
- gbString err_str = expr_to_string(x->expr);
- defer (gb_string_free(err_str));
- error(&c->error_collector, ast_node_token(node),
- "Shifted operand `%s` must be an integer", err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (is_type_unsigned(y->type)) {
- } else if (is_type_untyped(y->type)) {
- convert_to_typed(c, y, t_untyped_integer);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- } else {
- gbString err_str = expr_to_string(y->expr);
- defer (gb_string_free(err_str));
- error(&c->error_collector, ast_node_token(node),
- "Shift amount `%s` must be an unsigned integer", err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (x->mode == Addressing_Constant) {
- if (y->mode == Addressing_Constant) {
- ExactValue y_val = exact_value_to_integer(y->value);
- if (y_val.kind != ExactValue_Integer) {
- gbString err_str = expr_to_string(y->expr);
- defer (gb_string_free(err_str));
- error(&c->error_collector, ast_node_token(node),
- "Shift amount `%s` must be an unsigned integer", err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- u64 amount = cast(u64)y_val.value_integer;
- if (amount > 1074) {
- gbString err_str = expr_to_string(y->expr);
- defer (gb_string_free(err_str));
- error(&c->error_collector, ast_node_token(node),
- "Shift amount too large: `%s`", err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (!is_type_integer(x->type)) {
- // NOTE(bill): It could be an untyped float but still representable
- // as an integer
- x->type = t_untyped_integer;
- }
- x->value = exact_value_shift(be->op, x_val, make_exact_value_integer(amount));
- if (is_type_typed(x->type)) {
- check_is_expressible(c, x, get_base_type(x->type));
- }
- return;
- }
- if (x_is_untyped) {
- ExpressionInfo *info = map_get(&c->info.untyped, hash_pointer(x->expr));
- if (info != NULL) {
- info->is_lhs = true;
- }
- x->mode = Addressing_Value;
- return;
- }
- }
- if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
- gbString err_str = expr_to_string(y->expr);
- defer (gb_string_free(err_str));
- error(&c->error_collector, ast_node_token(node),
- "Shift amount cannot be negative: `%s`", err_str);
- }
- x->mode = Addressing_Value;
- }
- b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
- if (check_is_assignable_to(c, operand, y))
- return true;
- Type *x = operand->type;
- Type *xb = get_enum_base_type(get_base_type(x));
- Type *yb = get_enum_base_type(get_base_type(y));
- if (are_types_identical(xb, yb))
- return true;
- // Cast between booleans and integers
- if (is_type_boolean(x) || is_type_integer(x)) {
- if (is_type_boolean(y) || is_type_integer(y))
- return true;
- }
- // Cast between numbers
- if (is_type_integer(x) || is_type_float(x)) {
- if (is_type_integer(y) || is_type_float(y))
- return true;
- }
- // Cast between pointers
- if (is_type_pointer(x)) {
- if (is_type_pointer(y))
- return true;
- }
- // // untyped integers -> pointers
- // if (is_type_untyped(xb) && is_type_integer(xb)) {
- // if (is_type_pointer(yb))
- // return true;
- // }
- // (u)int <-> pointer
- if (is_type_pointer(xb) || (is_type_int_or_uint(xb) && !is_type_untyped(xb))) {
- if (is_type_pointer(yb))
- return true;
- }
- if (is_type_pointer(xb)) {
- if (is_type_pointer(yb) || (is_type_int_or_uint(yb) && !is_type_untyped(yb)))
- return true;
- }
- // []byte/[]u8 <-> string
- if (is_type_u8_slice(xb) && is_type_string(yb)) {
- return true;
- }
- if (is_type_string(xb) && is_type_u8_slice(yb)) {
- return true;
- }
- // proc <-> proc
- if (is_type_proc(xb) && is_type_proc(yb)) {
- return true;
- }
- // proc -> rawptr
- if (is_type_proc(xb) && is_type_rawptr(yb)) {
- return true;
- }
- return false;
- }
- void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
- GB_ASSERT(node->kind == AstNode_BinaryExpr);
- Operand y_ = {}, *y = &y_;
- gbString err_str = NULL;
- defer (gb_string_free(err_str));
- ast_node(be, BinaryExpr, node);
- if (be->op.kind == Token_as) {
- check_expr(c, x, be->left);
- Type *type = check_type(c, be->right);
- if (x->mode == Addressing_Invalid)
- return;
- b32 is_const_expr = x->mode == Addressing_Constant;
- b32 can_convert = false;
- Type *base_type = get_base_type(type);
- if (is_const_expr && is_type_constant_type(base_type)) {
- if (base_type->kind == Type_Basic) {
- if (check_value_is_expressible(c, x->value, base_type, &x->value)) {
- can_convert = true;
- }
- }
- } else if (check_castable_to(c, x, type)) {
- x->mode = Addressing_Value;
- can_convert = true;
- }
- if (!can_convert) {
- gbString expr_str = expr_to_string(x->expr);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(expr_str));
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(x->expr), "Cannot cast `%s` as `%s`", expr_str, type_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (is_type_untyped(x->type)) {
- Type *final_type = type;
- if (is_const_expr && !is_type_constant_type(type)) {
- final_type = default_type(x->type);
- }
- update_expr_type(c, x->expr, final_type, true);
- }
- x->type = type;
- return;
- } else if (be->op.kind == Token_transmute) {
- check_expr(c, x, be->left);
- Type *type = check_type(c, be->right);
- if (x->mode == Addressing_Invalid)
- return;
- if (x->mode == Addressing_Constant) {
- gbString expr_str = expr_to_string(x->expr);
- defer (gb_string_free(expr_str));
- error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute constant expression: `%s`", expr_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (is_type_untyped(x->type)) {
- gbString expr_str = expr_to_string(x->expr);
- defer (gb_string_free(expr_str));
- error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute untyped expression: `%s`", expr_str);
- x->mode = Addressing_Invalid;
- return;
- }
- i64 otz = type_size_of(c->sizes, c->allocator, x->type);
- i64 ttz = type_size_of(c->sizes, c->allocator, type);
- if (otz != ttz) {
- gbString expr_str = expr_to_string(x->expr);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(expr_str));
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, otz, ttz);
- x->mode = Addressing_Invalid;
- return;
- }
- x->type = type;
- return;
- }
- check_expr(c, x, be->left);
- check_expr(c, y, be->right);
- if (x->mode == Addressing_Invalid) {
- return;
- }
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- x->expr = y->expr;
- return;
- }
- Token op = be->op;
- if (token_is_shift(op)) {
- check_shift(c, x, y, node);
- return;
- }
- convert_to_typed(c, x, y->type);
- if (x->mode == Addressing_Invalid) return;
- convert_to_typed(c, y, x->type);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- if (token_is_comparison(op)) {
- check_comparison(c, x, y, op);
- return;
- }
- if (!are_types_identical(x->type, y->type)) {
- if (x->type != t_invalid &&
- y->type != t_invalid) {
- gbString xt = type_to_string(x->type);
- gbString yt = type_to_string(y->type);
- defer (gb_string_free(xt));
- defer (gb_string_free(yt));
- err_str = expr_to_string(x->expr);
- error(&c->error_collector, op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", err_str, xt, yt);
- }
- x->mode = Addressing_Invalid;
- return;
- }
- if (!check_binary_op(c, x, op)) {
- x->mode = Addressing_Invalid;
- return;
- }
- switch (op.kind) {
- case Token_Quo:
- case Token_Mod:
- case Token_QuoEq:
- case Token_ModEq:
- if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
- y->mode == Addressing_Constant) {
- b32 fail = false;
- switch (y->value.kind) {
- case ExactValue_Integer:
- if (y->value.value_integer == 0)
- fail = true;
- break;
- case ExactValue_Float:
- if (y->value.value_float == 0.0)
- fail = true;
- break;
- }
- if (fail) {
- error(&c->error_collector, ast_node_token(y->expr), "Division by zero not allowed");
- x->mode = Addressing_Invalid;
- return;
- }
- }
- }
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- ExactValue a = x->value;
- ExactValue b = y->value;
- Type *type = get_base_type(x->type);
- GB_ASSERT(type->kind == Type_Basic);
- if (op.kind == Token_Quo && is_type_integer(type)) {
- op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
- }
- x->value = exact_binary_operator_value(op, a, b);
- if (is_type_typed(type)) {
- if (node != NULL)
- x->expr = node;
- check_is_expressible(c, x, type);
- }
- return;
- }
- x->mode = Addressing_Value;
- }
- void update_expr_type(Checker *c, AstNode *e, Type *type, b32 final) {
- HashKey key = hash_pointer(e);
- ExpressionInfo *found = map_get(&c->info.untyped, key);
- if (found == NULL)
- return;
- switch (e->kind) {
- case_ast_node(ue, UnaryExpr, e);
- if (found->value.kind != ExactValue_Invalid)
- break;
- update_expr_type(c, ue->expr, type, final);
- case_end;
- case_ast_node(be, BinaryExpr, e);
- if (found->value.kind != ExactValue_Invalid)
- break;
- if (!token_is_comparison(be->op)) {
- if (token_is_shift(be->op)) {
- update_expr_type(c, be->left, type, final);
- } else {
- update_expr_type(c, be->left, type, final);
- update_expr_type(c, be->right, type, final);
- }
- }
- case_end;
- }
- if (!final && is_type_untyped(type)) {
- found->type = get_base_type(type);
- map_set(&c->info.untyped, key, *found);
- } else {
- ExpressionInfo old = *found;
- map_remove(&c->info.untyped, key);
- if (old.is_lhs && !is_type_integer(type)) {
- gbString expr_str = expr_to_string(e);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(expr_str));
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(e), "Shifted operand %s must be an integer, got %s", expr_str, type_str);
- return;
- }
- add_type_and_value(&c->info, e, found->mode, type, found->value);
- }
- }
- void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
- ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
- if (found)
- found->value = value;
- }
- void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString type_str = type_to_string(target_type);
- char *extra_text = "";
- defer (gb_string_free(expr_str));
- defer (gb_string_free(type_str));
- if (operand->mode == Addressing_Constant) {
- if (operand->value.value_integer == 0) {
- // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
- extra_text = " - Did you want `null`?";
- }
- }
- error(&c->error_collector, ast_node_token(operand->expr), "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
- operand->mode = Addressing_Invalid;
- }
- void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
- GB_ASSERT_NOT_NULL(target_type);
- if (operand->mode == Addressing_Invalid ||
- is_type_typed(operand->type) ||
- target_type == t_invalid) {
- return;
- }
- if (is_type_untyped(target_type)) {
- Type *x = operand->type;
- Type *y = target_type;
- if (is_type_numeric(x) && is_type_numeric(y)) {
- if (x < y) {
- operand->type = target_type;
- update_expr_type(c, operand->expr, target_type, false);
- }
- } else if (x != y) {
- convert_untyped_error(c, operand, target_type);
- }
- return;
- }
- Type *t = get_enum_base_type(get_base_type(target_type));
- switch (t->kind) {
- case Type_Basic:
- if (operand->mode == Addressing_Constant) {
- check_is_expressible(c, operand, t);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- update_expr_value(c, operand->expr, operand->value);
- } else {
- // TODO(bill): Is this really needed?
- switch (operand->type->Basic.kind) {
- case Basic_UntypedBool:
- if (!is_type_boolean(target_type)) {
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Basic_UntypedInteger:
- case Basic_UntypedFloat:
- case Basic_UntypedRune:
- if (!is_type_numeric(target_type)) {
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
- }
- break;
- case Type_Pointer:
- switch (operand->type->Basic.kind) {
- case Basic_UntypedPointer:
- target_type = t_untyped_pointer;
- break;
- default:
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Type_Proc:
- switch (operand->type->Basic.kind) {
- case Basic_UntypedPointer:
- break;
- default:
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- default:
- convert_untyped_error(c, operand, target_type);
- return;
- }
- operand->type = target_type;
- }
- b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *value) {
- Operand operand = {Addressing_Invalid};
- check_expr(c, &operand, index_value);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- return false;
- }
- convert_to_typed(c, &operand, t_int);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- return false;
- }
- if (!is_type_integer(get_enum_base_type(operand.type))) {
- gbString expr_str = expr_to_string(operand.expr);
- error(&c->error_collector, ast_node_token(operand.expr),
- "Index `%s` must be an integer", expr_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- if (operand.mode == Addressing_Constant) {
- if (max_count >= 0) { // NOTE(bill): Do array bound checking
- i64 i = exact_value_to_integer(operand.value).value_integer;
- if (i < 0) {
- gbString expr_str = expr_to_string(operand.expr);
- error(&c->error_collector, ast_node_token(operand.expr),
- "Index `%s` cannot be a negative value", expr_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- if (value) *value = i;
- if (i >= max_count) {
- gbString expr_str = expr_to_string(operand.expr);
- error(&c->error_collector, ast_node_token(operand.expr),
- "Index `%s` is out of bounds range [0, %lld)", expr_str, max_count);
- gb_string_free(expr_str);
- return false;
- }
- return true;
- }
- }
- // NOTE(bill): It's alright :D
- if (value) *value = -1;
- return true;
- }
- struct Selection {
- Entity *entity;
- gbArray(isize) index;
- b32 indirect; // Set if there was a pointer deref anywhere down the line
- };
- Selection empty_selection = {};
- Selection make_selection(Entity *entity, gbArray(isize) index, b32 indirect) {
- Selection s = {entity, index, indirect};
- return s;
- }
- void selection_add_index(Selection *s, isize index) {
- if (s->index == NULL) {
- gb_array_init(s->index, gb_heap_allocator());
- }
- gb_array_append(s->index, index);
- }
- Selection lookup_field(Type *type_, String field_name, AddressingMode mode, Selection sel = empty_selection) {
- GB_ASSERT(type_ != NULL);
- if (are_strings_equal(field_name, make_string("_"))) {
- return empty_selection;
- }
- Type *type = type_deref(type_);
- b32 is_ptr = type != type_;
- type = get_base_type(type);
- switch (type->kind) {
- case Type_Struct:
- if (mode == Addressing_Type) {
- for (isize i = 0; i < type->Struct.other_field_count; i++) {
- Entity *f = type->Struct.other_fields[i];
- GB_ASSERT(f->kind != Entity_Variable);
- String str = f->token.string;
- if (are_strings_equal(field_name, str)) {
- selection_add_index(&sel, i);
- sel.entity = f;
- return sel;
- }
- }
- } else {
- for (isize i = 0; i < type->Struct.field_count; i++) {
- Entity *f = type->Struct.fields[i];
- GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
- String str = f->token.string;
- if (are_strings_equal(field_name, str)) {
- selection_add_index(&sel, i);
- sel.entity = f;
- return sel;
- }
- if (f->Variable.anonymous) {
- isize prev_count = 0;
- if (sel.index != NULL) {
- prev_count = gb_array_count(sel.index);
- }
- selection_add_index(&sel, i); // HACK(bill): Leaky memory
- sel = lookup_field(f->type, field_name, mode, sel);
- if (sel.entity != NULL) {
- // gb_printf("%.*s, %.*s, %.*s\n", LIT(field_name), LIT(str), LIT(sel.entity->token.string));
- if (is_type_pointer(f->type))
- sel.indirect = true;
- return sel;
- }
- gb_array_count(sel.index) = prev_count;
- }
- }
- }
- break;
- case Type_Union:
- for (isize i = 0; i < type->Union.field_count; i++) {
- Entity *f = type->Union.fields[i];
- GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
- String str = f->token.string;
- if (are_strings_equal(field_name, str)) {
- selection_add_index(&sel, i);
- sel.entity = f;
- return sel;
- }
- }
- for (isize i = 0; i < type->Union.field_count; i++) {
- Entity *f = type->Union.fields[i];
- GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
- String str = f->token.string;
- if (f->Variable.anonymous) {
- selection_add_index(&sel, i); // HACK(bill): Leaky memory
- sel = lookup_field(f->type, field_name, mode, sel);
- if (sel.entity != NULL && is_type_pointer(f->type)) {
- sel.indirect = true;
- }
- return sel;
- }
- }
- break;
- case Type_Enum:
- if (mode == Addressing_Type) {
- for (isize i = 0; i < type->Enum.field_count; i++) {
- Entity *f = type->Enum.fields[i];
- GB_ASSERT(f->kind == Entity_Constant);
- String str = f->token.string;
- if (are_strings_equal(field_name, str)) {
- // Enums are constant expression
- return make_selection(f, NULL, i);
- }
- }
- }
- break;
- }
- return sel;
- }
- Entity *check_selector(Checker *c, Operand *operand, AstNode *node) {
- GB_ASSERT(node->kind == AstNode_SelectorExpr);
- ast_node(se, SelectorExpr, node);
- AstNode *op_expr = se->expr;
- AstNode *selector = se->selector;
- if (selector) {
- Entity *entity = lookup_field(operand->type, selector->Ident.token.string, operand->mode).entity;
- if (entity == NULL) {
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string(operand->type);
- gbString sel_str = expr_to_string(selector);
- defer (gb_string_free(op_str));
- defer (gb_string_free(type_str));
- defer (gb_string_free(sel_str));
- error(&c->error_collector, ast_node_token(op_expr), "`%s` (`%s`) has no field `%s`", op_str, type_str, sel_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return NULL;
- }
- add_entity_use(&c->info, selector, entity);
- operand->type = entity->type;
- operand->expr = node;
- if (entity->kind == Entity_Constant) {
- operand->mode = Addressing_Constant;
- operand->value = entity->Constant.value;
- } else if (entity->kind == Entity_TypeName) {
- operand->mode = Addressing_Type;
- } else {
- if (operand->mode != Addressing_Variable)
- operand->mode = Addressing_Value;
- }
- return entity;
- } else {
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- }
- return NULL;
- }
- b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
- GB_ASSERT(call->kind == AstNode_CallExpr);
- ast_node(ce, CallExpr, call);
- BuiltinProc *bp = &builtin_procs[id];
- {
- char *err = NULL;
- if (ce->arg_list_count < bp->arg_count)
- err = "Too few";
- if (ce->arg_list_count > bp->arg_count && !bp->variadic)
- err = "Too many";
- if (err) {
- ast_node(proc, Ident, ce->proc);
- error(&c->error_collector, ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
- err, LIT(proc->token.string),
- bp->arg_count, ce->arg_list_count);
- return false;
- }
- }
- switch (id) {
- case BuiltinProc_new:
- case BuiltinProc_new_slice:
- case BuiltinProc_size_of:
- case BuiltinProc_align_of:
- case BuiltinProc_offset_of:
- // NOTE(bill): The first arg is a Type, this will be checked case by case
- break;
- default:
- check_multi_expr(c, operand, ce->arg_list);
- }
- switch (id) {
- case BuiltinProc_new: {
- // new :: proc(Type) -> ^Type
- Type *type = check_type(c, ce->arg_list);
- if (type == NULL || type == t_invalid) {
- error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `size_of`");
- return false;
- }
- operand->mode = Addressing_Value;
- operand->type = make_type_pointer(c->allocator, type);
- } break;
- case BuiltinProc_new_slice: {
- // new_slice :: proc(Type, len: int[, cap: int]) -> []Type
- Type *type = check_type(c, ce->arg_list);
- if (type == NULL || type == t_invalid) {
- error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `size_of`");
- return false;
- }
- AstNode *len = ce->arg_list->next;
- AstNode *cap = len->next;
- Operand op = {};
- check_expr(c, &op, len);
- if (op.mode == Addressing_Invalid)
- return false;
- if (!is_type_integer(op.type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Length for `new_slice` must be an integer, got `%s`",
- type_str);
- return false;
- }
- if (cap != NULL) {
- check_expr(c, &op, len);
- if (op.mode == Addressing_Invalid)
- return false;
- if (!is_type_integer(op.type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Capacity for `new_slice` must be an integer, got `%s`",
- type_str);
- return false;
- }
- if (cap->next != NULL) {
- error(&c->error_collector, ast_node_token(call),
- "Too many arguments to `new_slice`, expected either 2 or 3");
- return false;
- }
- }
- operand->mode = Addressing_Value;
- operand->type = make_type_slice(c->allocator, type);
- } break;
- case BuiltinProc_delete: {
- // delete :: proc(ptr: ^T)
- Type *type = get_base_type(operand->type);
- if (!is_type_pointer(type) && !is_type_slice(type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Expected a pointer or slice to `delete`, got `%s`",
- type_str);
- return false;
- }
- operand->mode = Addressing_NoValue;
- operand->type = NULL;
- } break;
- case BuiltinProc_size_of: {
- // size_of :: proc(Type) -> int
- Type *type = check_type(c, ce->arg_list);
- if (!type) {
- error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `size_of`");
- return false;
- }
- operand->mode = Addressing_Constant;
- operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, type));
- operand->type = t_int;
- } break;
- case BuiltinProc_size_of_val:
- // size_of_val :: proc(val: Type) -> int
- check_assignment(c, operand, NULL, make_string("argument of `size_of`"));
- if (operand->mode == Addressing_Invalid)
- return false;
- operand->mode = Addressing_Constant;
- operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, operand->type));
- operand->type = t_int;
- break;
- case BuiltinProc_align_of: {
- // align_of :: proc(Type) -> int
- Type *type = check_type(c, ce->arg_list);
- if (!type) {
- error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `align_of`");
- return false;
- }
- operand->mode = Addressing_Constant;
- operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, type));
- operand->type = t_int;
- } break;
- case BuiltinProc_align_of_val:
- // align_of_val :: proc(val: Type) -> int
- check_assignment(c, operand, NULL, make_string("argument of `align_of`"));
- if (operand->mode == Addressing_Invalid)
- return false;
- operand->mode = Addressing_Constant;
- operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, operand->type));
- operand->type = t_int;
- break;
- case BuiltinProc_offset_of: {
- // offset_val :: proc(Type, field) -> int
- Type *type = get_base_type(check_type(c, ce->arg_list));
- AstNode *field_arg = unparen_expr(ce->arg_list->next);
- if (type) {
- if (type->kind != Type_Struct) {
- error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a structure type for `offset_of`");
- return false;
- }
- if (field_arg == NULL ||
- field_arg->kind != AstNode_Ident) {
- error(&c->error_collector, ast_node_token(field_arg), "Expected an identifier for field argument");
- return false;
- }
- }
- ast_node(arg, Ident, field_arg);
- Selection sel = lookup_field(type, arg->token.string, operand->mode);
- if (sel.entity == NULL) {
- gbString type_str = type_to_string(type);
- error(&c->error_collector, ast_node_token(ce->arg_list),
- "`%s` has no field named `%.*s`", type_str, LIT(arg->token.string));
- return false;
- }
- operand->mode = Addressing_Constant;
- // IMPORTANT TODO(bill): Fix for anonymous fields
- operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0]));
- operand->type = t_int;
- } break;
- case BuiltinProc_offset_of_val: {
- // offset_val :: proc(val: expression) -> int
- AstNode *arg = unparen_expr(ce->arg_list);
- if (arg->kind != AstNode_SelectorExpr) {
- gbString str = expr_to_string(arg);
- error(&c->error_collector, ast_node_token(arg), "`%s` is not a selector expression", str);
- return false;
- }
- ast_node(s, SelectorExpr, arg);
- check_expr(c, operand, s->expr);
- if (operand->mode == Addressing_Invalid)
- return false;
- Type *type = operand->type;
- if (get_base_type(type)->kind == Type_Pointer) {
- Type *p = get_base_type(type);
- if (get_base_type(p)->kind == Type_Struct)
- type = p->Pointer.elem;
- }
- ast_node(i, Ident, s->selector);
- Selection sel = lookup_field(type, i->token.string, operand->mode);
- if (sel.entity == NULL) {
- gbString type_str = type_to_string(type);
- error(&c->error_collector, ast_node_token(arg),
- "`%s` has no field named `%.*s`", type_str, LIT(i->token.string));
- return false;
- }
- operand->mode = Addressing_Constant;
- // IMPORTANT TODO(bill): Fix for anonymous fields
- operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, sel.index[0]));
- operand->type = t_int;
- } break;
- case BuiltinProc_static_assert:
- // static_assert :: proc(cond: bool)
- if (operand->mode != Addressing_Constant ||
- !is_type_boolean(operand->type)) {
- gbString str = expr_to_string(ce->arg_list);
- defer (gb_string_free(str));
- error(&c->error_collector, ast_node_token(call),
- "`%s` is not a constant boolean", str);
- return false;
- }
- if (!operand->value.value_bool) {
- gbString str = expr_to_string(ce->arg_list);
- defer (gb_string_free(str));
- error(&c->error_collector, ast_node_token(call),
- "Static assertion: `%s`", str);
- return true;
- }
- break;
- // TODO(bill): Should these be procedures and are their names appropriate?
- case BuiltinProc_len:
- case BuiltinProc_cap: {
- Type *t = get_base_type(operand->type);
- AddressingMode mode = Addressing_Invalid;
- ExactValue value = {};
- switch (t->kind) {
- case Type_Basic:
- if (id == BuiltinProc_len) {
- if (is_type_string(t)) {
- if (operand->mode == Addressing_Constant) {
- mode = Addressing_Constant;
- value = make_exact_value_integer(operand->value.value_string);
- } else {
- mode = Addressing_Value;
- }
- }
- }
- break;
- case Type_Array:
- mode = Addressing_Constant;
- value = make_exact_value_integer(t->Array.count);
- break;
- case Type_Vector:
- mode = Addressing_Constant;
- value = make_exact_value_integer(t->Vector.count);
- break;
- case Type_Slice:
- mode = Addressing_Value;
- break;
- }
- if (mode == Addressing_Invalid) {
- gbString str = expr_to_string(operand->expr);
- error(&c->error_collector, ast_node_token(operand->expr),
- "Invalid expression `%s` for `%.*s`",
- str, LIT(bp->name));
- gb_string_free(str);
- return false;
- }
- operand->mode = mode;
- operand->type = t_int;
- operand->value = value;
- } break;
- case BuiltinProc_copy: {
- // copy :: proc(x, y: []Type) -> int
- Type *dest_type = NULL, *src_type = NULL;
- Type *d = get_base_type(operand->type);
- if (d->kind == Type_Slice)
- dest_type = d->Slice.elem;
- Operand op = {};
- check_expr(c, &op, ce->arg_list->next);
- if (op.mode == Addressing_Invalid)
- return false;
- Type *s = get_base_type(op.type);
- if (s->kind == Type_Slice)
- src_type = s->Slice.elem;
- if (dest_type == NULL || src_type == NULL) {
- error(&c->error_collector, ast_node_token(call), "`copy` only expects slices as arguments");
- return false;
- }
- if (!are_types_identical(dest_type, src_type)) {
- gbString d_arg = expr_to_string(ce->arg_list);
- gbString s_arg = expr_to_string(ce->arg_list->next);
- gbString d_str = type_to_string(dest_type);
- gbString s_str = type_to_string(src_type);
- defer (gb_string_free(d_arg));
- defer (gb_string_free(s_arg));
- defer (gb_string_free(d_str));
- defer (gb_string_free(s_str));
- error(&c->error_collector, ast_node_token(call),
- "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
- d_arg, s_arg, d_str, s_str);
- return false;
- }
- operand->type = t_int; // Returns number of elems copied
- operand->mode = Addressing_Value;
- } break;
- case BuiltinProc_append: {
- // append :: proc(x : ^[]Type, y : Type) -> bool
- Type *x_type = NULL, *y_type = NULL;
- x_type = get_base_type(operand->type);
- Operand op = {};
- check_expr(c, &op, ce->arg_list->next);
- if (op.mode == Addressing_Invalid)
- return false;
- y_type = get_base_type(op.type);
- if (!(is_type_pointer(x_type) && is_type_slice(x_type->Pointer.elem))) {
- error(&c->error_collector, ast_node_token(call), "First argument to `append` must be a pointer to a slice");
- return false;
- }
- Type *elem_type = x_type->Pointer.elem->Slice.elem;
- if (!check_is_assignable_to(c, &op, elem_type)) {
- gbString d_arg = expr_to_string(ce->arg_list);
- gbString s_arg = expr_to_string(ce->arg_list->next);
- gbString d_str = type_to_string(elem_type);
- gbString s_str = type_to_string(y_type);
- defer (gb_string_free(d_arg));
- defer (gb_string_free(s_arg));
- defer (gb_string_free(d_str));
- defer (gb_string_free(s_str));
- error(&c->error_collector, ast_node_token(call),
- "Arguments to `append`, %s, %s, have different element types: %s vs %s",
- d_arg, s_arg, d_str, s_str);
- return false;
- }
- operand->type = t_bool; // Returns if it was successful
- operand->mode = Addressing_Value;
- } break;
- case BuiltinProc_swizzle: {
- // swizzle :: proc(v: {N}T, T...) -> {M}T
- Type *vector_type = get_base_type(operand->type);
- if (!is_type_vector(vector_type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "You can only `swizzle` a vector, got `%s`",
- type_str);
- return false;
- }
- isize max_count = vector_type->Vector.count;
- isize arg_count = 0;
- for (AstNode *arg = ce->arg_list->next; arg != NULL; arg = arg->next) {
- Operand op = {};
- check_expr(c, &op, arg);
- if (op.mode == Addressing_Invalid)
- return false;
- Type *arg_type = get_base_type(op.type);
- if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
- error(&c->error_collector, ast_node_token(op.expr), "Indices to `swizzle` must be constant integers");
- return false;
- }
- if (op.value.value_integer < 0) {
- error(&c->error_collector, ast_node_token(op.expr), "Negative `swizzle` index");
- return false;
- }
- if (max_count <= op.value.value_integer) {
- error(&c->error_collector, ast_node_token(op.expr), "`swizzle` index exceeds vector length");
- return false;
- }
- arg_count++;
- }
- if (arg_count > max_count) {
- error(&c->error_collector, ast_node_token(call), "Too many `swizzle` indices, %td > %td", arg_count, max_count);
- return false;
- }
- Type *elem_type = vector_type->Vector.elem;
- operand->type = make_type_vector(c->allocator, elem_type, arg_count);
- operand->mode = Addressing_Value;
- } break;
- case BuiltinProc_ptr_offset: {
- // ptr_offset :: proc(ptr: ^T, offset: int) -> ^T
- // ^T cannot be rawptr
- Type *ptr_type = get_base_type(operand->type);
- if (!is_type_pointer(ptr_type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Expected a pointer to `ptr_offset`, got `%s`",
- type_str);
- return false;
- }
- if (ptr_type == t_rawptr) {
- error(&c->error_collector, ast_node_token(call),
- "`rawptr` cannot have pointer arithmetic");
- return false;
- }
- AstNode *offset = ce->arg_list->next;
- Operand op = {};
- check_expr(c, &op, offset);
- if (op.mode == Addressing_Invalid)
- return false;
- Type *offset_type = get_base_type(op.type);
- if (!is_type_integer(offset_type)) {
- error(&c->error_collector, ast_node_token(op.expr), "Pointer offsets for `ptr_offset` must be an integer");
- return false;
- }
- if (operand->mode == Addressing_Constant &&
- op.mode == Addressing_Constant) {
- u8 *ptr = cast(u8 *)operand->value.value_pointer;
- isize elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
- ptr += elem_size * op.value.value_integer;
- operand->value.value_pointer = ptr;
- } else {
- operand->mode = Addressing_Value;
- }
- } break;
- case BuiltinProc_ptr_sub: {
- // ptr_sub :: proc(a, b: ^T) -> int
- // ^T cannot be rawptr
- Type *ptr_type = get_base_type(operand->type);
- if (!is_type_pointer(ptr_type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Expected a pointer to `ptr_add`, got `%s`",
- type_str);
- return false;
- }
- if (ptr_type == t_rawptr) {
- error(&c->error_collector, ast_node_token(call),
- "`rawptr` cannot have pointer arithmetic");
- return false;
- }
- AstNode *offset = ce->arg_list->next;
- Operand op = {};
- check_expr(c, &op, offset);
- if (op.mode == Addressing_Invalid)
- return false;
- if (!is_type_pointer(op.type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Expected a pointer to `ptr_add`, got `%s`",
- type_str);
- return false;
- }
- if (get_base_type(op.type) == t_rawptr) {
- error(&c->error_collector, ast_node_token(call),
- "`rawptr` cannot have pointer arithmetic");
- return false;
- }
- if (!are_types_identical(operand->type, op.type)) {
- gbString a = type_to_string(operand->type);
- gbString b = type_to_string(op.type);
- defer (gb_string_free(a));
- defer (gb_string_free(b));
- error(&c->error_collector, ast_node_token(op.expr),
- "`ptr_sub` requires to pointer of the same type. Got `%s` and `%s`.", a, b);
- return false;
- }
- operand->type = t_int;
- if (operand->mode == Addressing_Constant &&
- op.mode == Addressing_Constant) {
- u8 *ptr_a = cast(u8 *)operand->value.value_pointer;
- u8 *ptr_b = cast(u8 *)op.value.value_pointer;
- isize elem_size = type_size_of(c->sizes, c->allocator, ptr_type->Pointer.elem);
- operand->value = make_exact_value_integer((ptr_a - ptr_b) / elem_size);
- } else {
- operand->mode = Addressing_Value;
- }
- } break;
- case BuiltinProc_slice_ptr: {
- // slice_ptr :: proc(a: ^T, len: int[, cap: int]) -> []T
- // ^T cannot be rawptr
- Type *ptr_type = get_base_type(operand->type);
- if (!is_type_pointer(ptr_type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Expected a pointer to `slice_ptr`, got `%s`",
- type_str);
- return false;
- }
- if (ptr_type == t_rawptr) {
- error(&c->error_collector, ast_node_token(call),
- "`rawptr` cannot have pointer arithmetic");
- return false;
- }
- AstNode *len = ce->arg_list->next;
- AstNode *cap = len->next;
- Operand op = {};
- check_expr(c, &op, len);
- if (op.mode == Addressing_Invalid)
- return false;
- if (!is_type_integer(op.type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Length for `slice_ptr` must be an integer, got `%s`",
- type_str);
- return false;
- }
- if (cap != NULL) {
- check_expr(c, &op, len);
- if (op.mode == Addressing_Invalid)
- return false;
- if (!is_type_integer(op.type)) {
- gbString type_str = type_to_string(operand->type);
- defer (gb_string_free(type_str));
- error(&c->error_collector, ast_node_token(call),
- "Capacity for `slice_ptr` must be an integer, got `%s`",
- type_str);
- return false;
- }
- if (cap->next != NULL) {
- error(&c->error_collector, ast_node_token(call),
- "Too many arguments to `slice_ptr`, expected either 2 or 3");
- return false;
- }
- }
- operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
- operand->mode = Addressing_Value;
- } break;
- }
- return true;
- }
- void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
- GB_ASSERT(call->kind == AstNode_CallExpr);
- GB_ASSERT(proc_type->kind == Type_Proc);
- ast_node(ce, CallExpr, call);
- isize error_code = 0;
- isize param_index = 0;
- isize param_count = 0;
- if (proc_type->Proc.params)
- param_count = proc_type->Proc.params->Tuple.variable_count;
- if (ce->arg_list_count == 0 && param_count == 0)
- return;
- if (ce->arg_list_count > param_count) {
- error_code = +1;
- } else {
- Entity **sig_params = proc_type->Proc.params->Tuple.variables;
- AstNode *call_arg = ce->arg_list;
- for (; call_arg != NULL; call_arg = call_arg->next) {
- check_multi_expr(c, operand, call_arg);
- if (operand->mode == Addressing_Invalid)
- continue;
- if (operand->type->kind != Type_Tuple) {
- check_not_tuple(c, operand);
- check_assignment(c, operand, sig_params[param_index]->type, make_string("argument"), true);
- param_index++;
- } else {
- auto *tuple = &operand->type->Tuple;
- isize i = 0;
- for (;
- i < tuple->variable_count && param_index < param_count;
- i++, param_index++) {
- Entity *e = tuple->variables[i];
- operand->type = e->type;
- operand->mode = Addressing_Value;
- check_not_tuple(c, operand);
- check_assignment(c, operand, sig_params[param_index]->type, make_string("argument"), true);
- }
- if (i < tuple->variable_count && param_index == param_count) {
- error_code = +1;
- break;
- }
- }
- if (param_index >= param_count)
- break;
- }
- if (param_index < param_count) {
- error_code = -1;
- } else if (call_arg != NULL && call_arg->next != NULL) {
- error_code = +1;
- }
- }
- if (error_code != 0) {
- char *err_fmt = "";
- if (error_code < 0) {
- err_fmt = "Too few arguments for `%s`, expected %td arguments";
- } else {
- err_fmt = "Too many arguments for `%s`, expected %td arguments";
- }
- gbString proc_str = expr_to_string(ce->proc);
- error(&c->error_collector, ast_node_token(call), err_fmt, proc_str, param_count);
- gb_string_free(proc_str);
- operand->mode = Addressing_Invalid;
- }
- }
- ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
- GB_ASSERT(call->kind == AstNode_CallExpr);
- ast_node(ce, CallExpr, call);
- check_expr_or_type(c, operand, ce->proc);
- if (operand->mode == Addressing_Invalid) {
- for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next)
- check_expr_base(c, operand, arg);
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- if (operand->mode == Addressing_Builtin) {
- i32 id = operand->builtin_id;
- if (!check_builtin_procedure(c, operand, call, id))
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return builtin_procs[id].kind;
- }
- Type *proc_type = get_base_type(operand->type);
- if (proc_type == NULL || proc_type->kind != Type_Proc) {
- AstNode *e = operand->expr;
- gbString str = expr_to_string(e);
- defer (gb_string_free(str));
- error(&c->error_collector, ast_node_token(e), "Cannot call a non-procedure: `%s`", str);
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- check_call_arguments(c, operand, proc_type, call);
- auto *proc = &proc_type->Proc;
- if (proc->result_count == 0) {
- operand->mode = Addressing_NoValue;
- } else if (proc->result_count == 1) {
- operand->mode = Addressing_Value;
- operand->type = proc->results->Tuple.variables[0]->type;
- } else {
- operand->mode = Addressing_Value;
- operand->type = proc->results;
- }
- operand->expr = call;
- return Expr_Stmt;
- }
- void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
- check_expr_base(c, o, e, t);
- check_not_tuple(c, o);
- char *err_str = NULL;
- switch (o->mode) {
- case Addressing_NoValue:
- err_str = "used as a value";
- break;
- case Addressing_Type:
- err_str = "is not an expression";
- break;
- case Addressing_Builtin:
- err_str = "must be called";
- break;
- }
- if (err_str != NULL) {
- gbString str = expr_to_string(e);
- defer (gb_string_free(str));
- error(&c->error_collector, ast_node_token(e), "`%s` %s", str, err_str);
- o->mode = Addressing_Invalid;
- }
- }
- ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
- ExprKind kind = Expr_Stmt;
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- switch (node->kind) {
- case_ast_node(be, BadExpr, node)
- goto error;
- case_end;
- case_ast_node(i, Ident, node);
- check_identifier(c, o, node, type_hint);
- case_end;
- case_ast_node(bl, BasicLit, node);
- Type *t = t_invalid;
- switch (bl->kind) {
- case Token_Integer: t = t_untyped_integer; break;
- case Token_Float: t = t_untyped_float; break;
- case Token_String: t = t_untyped_string; break;
- case Token_Rune: t = t_untyped_rune; break;
- default: GB_PANIC("Unknown literal"); break;
- }
- o->mode = Addressing_Constant;
- o->type = t;
- o->value = make_exact_value_from_basic_literal(*bl);
- case_end;
- case_ast_node(pl, ProcLit, node);
- check_open_scope(c, pl->type);
- c->context.decl = make_declaration_info(c->allocator, c->context.scope);
- defer (check_close_scope(c));
- Type *proc_type = check_type(c, pl->type);
- if (proc_type != NULL) {
- check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body);
- o->mode = Addressing_Value;
- o->type = proc_type;
- } else {
- gbString str = expr_to_string(node);
- error(&c->error_collector, ast_node_token(node), "Invalid procedure literal `%s`", str);
- gb_string_free(str);
- goto error;
- }
- case_end;
- case_ast_node(cl, CompoundLit, node);
- Type *type = type_hint;
- b32 ellipsis_array = false;
- if (cl->type != NULL) {
- type = NULL;
- // [..]Type
- if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
- if (cl->type->ArrayType.count->kind == AstNode_Ellipsis) {
- type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
- ellipsis_array = true;
- }
- }
- if (type == NULL) {
- type = check_type(c, cl->type);
- }
- }
- if (type == NULL) {
- error(&c->error_collector, ast_node_token(node), "Missing type in compound literal");
- goto error;
- }
- Type *t = get_base_type(type);
- switch (t->kind) {
- case Type_Struct: {
- if (cl->elem_count == 0)
- break; // NOTE(bill): No need to init
- { // Checker values
- AstNode *elem = cl->elem_list;
- isize field_count = t->Struct.field_count;
- if (elem->kind == AstNode_FieldValue) {
- b32 *fields_visited = gb_alloc_array(c->allocator, b32, field_count);
- for (;
- elem != NULL;
- elem = elem->next) {
- if (elem->kind != AstNode_FieldValue) {
- error(&c->error_collector, ast_node_token(elem),
- "Mixture of `field = value` and value elements in a structure literal is not allowed");
- continue;
- }
- ast_node(kv, FieldValue, elem);
- if (kv->field->kind != AstNode_Ident) {
- gbString expr_str = expr_to_string(kv->field);
- defer (gb_string_free(expr_str));
- error(&c->error_collector, ast_node_token(elem),
- "Invalid field name `%s` in structure literal", expr_str);
- continue;
- }
- String name = kv->field->Ident.token.string;
- Selection sel = lookup_field(type, kv->field->Ident.token.string, o->mode);
- if (sel.entity == NULL) {
- error(&c->error_collector, ast_node_token(elem),
- "Unknown field `%.*s` in structure literal", LIT(name));
- continue;
- }
- if (gb_array_count(sel.index) > 1) {
- error(&c->error_collector, ast_node_token(elem),
- "You cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
- continue;
- }
- Entity *field = t->Struct.fields[sel.index[0]];
- add_entity_use(&c->info, kv->field, field);
- if (fields_visited[sel.index[0]]) {
- error(&c->error_collector, ast_node_token(elem),
- "Duplicate field `%.*s` in structure literal", LIT(name));
- continue;
- }
- fields_visited[sel.index[0]] = true;
- check_expr(c, o, kv->value);
- check_assignment(c, o, field->type, make_string("structure literal"));
- }
- } else {
- isize index = 0;
- for (;
- elem != NULL;
- elem = elem->next, index++) {
- if (elem->kind == AstNode_FieldValue) {
- error(&c->error_collector, ast_node_token(elem),
- "Mixture of `field = value` and value elements in a structure literal is not allowed");
- continue;
- }
- Entity *field = t->Struct.fields[index];
- check_expr(c, o, elem);
- if (index >= field_count) {
- error(&c->error_collector, ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count);
- break;
- }
- check_assignment(c, o, field->type, make_string("structure literal"));
- }
- if (cl->elem_count < field_count) {
- error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count);
- }
- }
- }
- } break;
- case Type_Slice:
- case Type_Array:
- case Type_Vector:
- {
- Type *elem_type = NULL;
- String context_name = {};
- if (t->kind == Type_Slice) {
- elem_type = t->Slice.elem;
- context_name = make_string("slice literal");
- } else if (t->kind == Type_Vector) {
- elem_type = t->Vector.elem;
- context_name = make_string("vector literal");
- } else {
- elem_type = t->Array.elem;
- context_name = make_string("array literal");
- }
- i64 index = 0;
- i64 max = 0;
- for (AstNode *elem = cl->elem_list; elem != NULL; elem = elem->next, index++) {
- AstNode *e = elem;
- if (e->kind == AstNode_FieldValue) {
- error(&c->error_collector, ast_node_token(e),
- "`field = value` is only allowed in structure literals");
- continue;
- }
- if (t->kind == Type_Array &&
- t->Array.count >= 0 &&
- index >= t->Array.count) {
- error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for array literal", index, t->Array.count);
- }
- if (t->kind == Type_Vector &&
- t->Vector.count >= 0 &&
- index >= t->Vector.count) {
- error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for vector literal", index, t->Vector.count);
- }
- Operand o = {};
- check_expr_with_type_hint(c, &o, e, elem_type);
- check_assignment(c, &o, elem_type, context_name);
- }
- if (max < index)
- max = index;
- if (t->kind == Type_Vector) {
- if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
- error(&c->error_collector, ast_node_token(cl->elem_list),
- "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
- }
- }
- if (t->kind == Type_Array && ellipsis_array) {
- t->Array.count = max;
- }
- } break;
- default: {
- gbString str = type_to_string(type);
- error(&c->error_collector, ast_node_token(node), "Invalid compound literal type `%s`", str);
- gb_string_free(str);
- goto error;
- } break;
- }
- o->mode = Addressing_Value;
- o->type = type;
- case_end;
- case_ast_node(pe, ParenExpr, node);
- kind = check_expr_base(c, o, pe->expr, type_hint);
- o->expr = node;
- case_end;
- case_ast_node(te, TagExpr, node);
- // TODO(bill): Tag expressions
- error(&c->error_collector, ast_node_token(node), "Tag expressions are not supported yet");
- kind = check_expr_base(c, o, te->expr, type_hint);
- o->expr = node;
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- check_expr(c, o, ue->expr);
- if (o->mode == Addressing_Invalid)
- goto error;
- check_unary_expr(c, o, ue->op, node);
- if (o->mode == Addressing_Invalid)
- goto error;
- case_end;
- case_ast_node(be, BinaryExpr, node);
- check_binary_expr(c, o, node);
- if (o->mode == Addressing_Invalid)
- goto error;
- case_end;
- case_ast_node(se, SelectorExpr, node);
- check_expr_base(c, o, se->expr);
- check_selector(c, o, node);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- check_expr(c, o, ie->expr);
- if (o->mode == Addressing_Invalid)
- goto error;
- b32 valid = false;
- i64 max_count = -1;
- Type *t = get_base_type(o->type);
- switch (t->kind) {
- case Type_Basic:
- if (is_type_string(t)) {
- valid = true;
- if (o->mode == Addressing_Constant) {
- max_count = o->value.value_string.len;
- }
- if (o->mode != Addressing_Variable)
- o->mode = Addressing_Value;
- o->type = t_u8;
- }
- break;
- case Type_Array:
- valid = true;
- max_count = t->Array.count;
- if (o->mode != Addressing_Variable)
- o->mode = Addressing_Value;
- o->type = t->Array.elem;
- break;
- case Type_Vector:
- valid = true;
- max_count = t->Vector.count;
- if (o->mode != Addressing_Variable)
- o->mode = Addressing_Value;
- o->type = t->Vector.elem;
- break;
- case Type_Slice:
- valid = true;
- o->type = t->Slice.elem;
- o->mode = Addressing_Variable;
- break;
- case Type_Pointer: {
- Type *bt = get_base_type(t->Pointer.elem);
- if (bt->kind == Type_Array) {
- valid = true;
- max_count = bt->Array.count;
- o->mode = Addressing_Variable;
- o->type = bt->Array.elem;
- }
- } break;
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- error(&c->error_collector, ast_node_token(o->expr), "Cannot index `%s`", str);
- gb_string_free(str);
- goto error;
- }
- if (ie->index == NULL) {
- gbString str = expr_to_string(o->expr);
- error(&c->error_collector, ast_node_token(o->expr), "Missing index for `%s`", str);
- gb_string_free(str);
- goto error;
- }
- check_index_value(c, ie->index, max_count, NULL);
- case_end;
- case_ast_node(se, SliceExpr, node);
- check_expr(c, o, se->expr);
- if (o->mode == Addressing_Invalid)
- goto error;
- b32 valid = false;
- i64 max_count = -1;
- Type *t = get_base_type(o->type);
- switch (t->kind) {
- case Type_Basic:
- if (is_type_string(t)) {
- valid = true;
- if (o->mode == Addressing_Constant) {
- max_count = o->value.value_string.len;
- }
- o->type = t_string;
- }
- break;
- case Type_Array:
- valid = true;
- max_count = t->Array.count;
- if (o->mode != Addressing_Variable) {
- gbString str = expr_to_string(node);
- error(&c->error_collector, ast_node_token(node), "Cannot slice array `%s`, value is not addressable", str);
- gb_string_free(str);
- goto error;
- }
- o->type = make_type_slice(c->allocator, t->Array.elem);
- break;
- case Type_Slice:
- valid = true;
- break;
- case Type_Pointer: {
- Type *bt = get_base_type(t->Pointer.elem);
- if (bt->kind == Type_Array) {
- valid = true;
- max_count = bt->Array.count;
- o->type = make_type_slice(c->allocator, bt->Array.elem);
- }
- } break;
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- error(&c->error_collector, ast_node_token(o->expr), "Cannot slice `%s`", str);
- gb_string_free(str);
- goto error;
- }
- o->mode = Addressing_Value;
- i64 indices[3] = {};
- AstNode *nodes[3] = {se->low, se->high, se->max};
- for (isize i = 0; i < gb_count_of(nodes); i++) {
- i64 index = max_count;
- if (nodes[i] != NULL) {
- i64 capacity = -1;
- if (max_count >= 0)
- capacity = max_count;
- i64 j = 0;
- if (check_index_value(c, nodes[i], capacity, &j)) {
- index = j;
- }
- } else if (i == 0) {
- index = 0;
- }
- indices[i] = index;
- }
- for (isize i = 0; i < gb_count_of(indices); i++) {
- i64 a = indices[i];
- for (isize j = i+1; j < gb_count_of(indices); j++) {
- i64 b = indices[j];
- if (a > b && b >= 0) {
- error(&c->error_collector, se->close, "Invalid slice indices: [%td > %td]", a, b);
- }
- }
- }
- case_end;
- case_ast_node(ce, CallExpr, node);
- return check_call_expr(c, o, node);
- case_end;
- case_ast_node(de, DerefExpr, node);
- check_expr_or_type(c, o, de->expr);
- if (o->mode == Addressing_Invalid) {
- goto error;
- } else {
- Type *t = get_base_type(o->type);
- if (t->kind == Type_Pointer) {
- o->mode = Addressing_Variable;
- o->type = t->Pointer.elem;
- } else {
- gbString str = expr_to_string(o->expr);
- error(&c->error_collector, ast_node_token(o->expr), "Cannot dereference `%s`", str);
- gb_string_free(str);
- goto error;
- }
- }
- case_end;
- case AstNode_ProcType:
- case AstNode_PointerType:
- case AstNode_ArrayType:
- case AstNode_VectorType:
- case AstNode_StructType:
- case AstNode_UnionType:
- o->mode = Addressing_Type;
- o->type = check_type(c, node);
- break;
- }
- kind = Expr_Expr;
- o->expr = node;
- return kind;
- error:
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
- ExprKind kind = check__expr_base(c, o, node, type_hint);
- Type *type = NULL;
- ExactValue value = {ExactValue_Invalid};
- switch (o->mode) {
- case Addressing_Invalid:
- type = t_invalid;
- break;
- case Addressing_NoValue:
- type = NULL;
- break;
- case Addressing_Constant:
- type = o->type;
- value = o->value;
- break;
- default:
- type = o->type;
- break;
- }
- if (type != NULL && is_type_untyped(type)) {
- add_untyped(&c->info, node, false, o->mode, type, value);
- } else {
- add_type_and_value(&c->info, node, o->mode, type, value);
- }
- return kind;
- }
- void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
- gbString err_str = NULL;
- defer (gb_string_free(err_str));
- check_expr_base(c, o, e);
- switch (o->mode) {
- default:
- return; // NOTE(bill): Valid
- case Addressing_NoValue:
- err_str = expr_to_string(e);
- error(&c->error_collector, ast_node_token(e), "`%s` used as value", err_str);
- break;
- case Addressing_Type:
- err_str = expr_to_string(e);
- error(&c->error_collector, ast_node_token(e), "`%s` is not an expression", err_str);
- break;
- }
- o->mode = Addressing_Invalid;
- }
- void check_not_tuple(Checker *c, Operand *o) {
- if (o->mode == Addressing_Value) {
- // NOTE(bill): Tuples are not first class thus never named
- if (o->type->kind == Type_Tuple) {
- isize count = o->type->Tuple.variable_count;
- GB_ASSERT(count != 1);
- error(&c->error_collector, ast_node_token(o->expr),
- "%td-valued tuple found where single value expected", count);
- o->mode = Addressing_Invalid;
- }
- }
- }
- void check_expr(Checker *c, Operand *o, AstNode *e) {
- check_multi_expr(c, o, e);
- check_not_tuple(c, o);
- }
- void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
- check_expr_base(c, o, e);
- check_not_tuple(c, o);
- if (o->mode == Addressing_NoValue) {
- AstNode *e = o->expr;
- gbString str = expr_to_string(e);
- defer (gb_string_free(str));
- error(&c->error_collector, ast_node_token(e),
- "`%s` used as value or type", str);
- o->mode = Addressing_Invalid;
- }
- }
- gbString write_expr_to_string(gbString str, AstNode *node);
- gbString write_field_list_to_string(gbString str, AstNode *field_list, char *sep) {
- isize i = 0;
- for (AstNode *field = field_list; field != NULL; field = field->next) {
- ast_node(f, Field, field);
- if (i > 0)
- str = gb_string_appendc(str, sep);
- str = write_expr_to_string(str, field);
- i++;
- }
- return str;
- }
- gbString string_append_token(gbString str, Token token) {
- if (token.string.len > 0)
- return gb_string_append_length(str, token.string.text, token.string.len);
- return str;
- }
- gbString write_expr_to_string(gbString str, AstNode *node) {
- if (node == NULL)
- return str;
- if (is_ast_node_stmt(node)) {
- GB_ASSERT("stmt passed to write_expr_to_string");
- }
- switch (node->kind) {
- default:
- str = gb_string_appendc(str, "(BadExpr)");
- break;
- case_ast_node(i, Ident, node);
- str = string_append_token(str, i->token);
- case_end;
- case_ast_node(bl, BasicLit, node);
- str = string_append_token(str, *bl);
- case_end;
- case_ast_node(pl, ProcLit, node);
- str = write_expr_to_string(str, pl->type);
- case_end;
- case_ast_node(cl, CompoundLit, node);
- str = gb_string_appendc(str, "(");
- str = write_expr_to_string(str, cl->type);
- str = gb_string_appendc(str, " lit)");
- case_end;
- case_ast_node(te, TagExpr, node);
- str = gb_string_appendc(str, "#");
- str = string_append_token(str, te->name);
- str = write_expr_to_string(str, te->expr);
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- str = string_append_token(str, ue->op);
- str = write_expr_to_string(str, ue->expr);
- case_end;
- case_ast_node(be, BinaryExpr, node);
- str = write_expr_to_string(str, be->left);
- str = gb_string_appendc(str, " ");
- str = string_append_token(str, be->op);
- str = gb_string_appendc(str, " ");
- str = write_expr_to_string(str, be->right);
- case_end;
- case_ast_node(pe, ParenExpr, node);
- str = gb_string_appendc(str, "(");
- str = write_expr_to_string(str, pe->expr);
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(se, SelectorExpr, node);
- str = write_expr_to_string(str, se->expr);
- str = gb_string_appendc(str, ".");
- str = write_expr_to_string(str, se->selector);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- str = write_expr_to_string(str, ie->expr);
- str = gb_string_appendc(str, "[");
- str = write_expr_to_string(str, ie->index);
- str = gb_string_appendc(str, "]");
- case_end;
- case_ast_node(se, SliceExpr, node);
- str = write_expr_to_string(str, se->expr);
- str = gb_string_appendc(str, "[");
- str = write_expr_to_string(str, se->low);
- str = gb_string_appendc(str, ":");
- str = write_expr_to_string(str, se->high);
- if (se->triple_indexed) {
- str = gb_string_appendc(str, ":");
- str = write_expr_to_string(str, se->max);
- }
- str = gb_string_appendc(str, "]");
- case_end;
- case_ast_node(e, Ellipsis, node);
- str = gb_string_appendc(str, "..");
- case_end;
- case_ast_node(fv, FieldValue, node);
- str = write_expr_to_string(str, fv->field);
- str = gb_string_appendc(str, " = ");
- str = write_expr_to_string(str, fv->value);
- case_end;
- case_ast_node(pt, PointerType, node);
- str = gb_string_appendc(str, "^");
- str = write_expr_to_string(str, pt->type);
- case_end;
- case_ast_node(at, ArrayType, node);
- str = gb_string_appendc(str, "[");
- str = write_expr_to_string(str, at->count);
- str = gb_string_appendc(str, "]");
- str = write_expr_to_string(str, at->elem);
- case_end;
- case_ast_node(vt, VectorType, node);
- str = gb_string_appendc(str, "{");
- str = write_expr_to_string(str, vt->count);
- str = gb_string_appendc(str, "}");
- str = write_expr_to_string(str, vt->elem);
- case_end;
- case_ast_node(f, Field, node);
- if (f->is_using) {
- str = gb_string_appendc(str, "using ");
- }
- isize i = 0;
- for (AstNode *name = f->name_list; name != NULL; name = name->next) {
- if (i > 0)
- str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, name);
- i++;
- }
- str = gb_string_appendc(str, ": ");
- str = write_expr_to_string(str, f->type);
- case_end;
- case_ast_node(ce, CallExpr, node);
- str = write_expr_to_string(str, ce->proc);
- str = gb_string_appendc(str, "(");
- isize i = 0;
- for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, arg);
- i++;
- }
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(pt, ProcType, node);
- str = gb_string_appendc(str, "proc(");
- str = write_field_list_to_string(str, pt->param_list, ", ");
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(st, StructType, node);
- str = gb_string_appendc(str, "struct{");
- // str = write_field_list_to_string(str, st->decl_list, ", ");
- str = gb_string_appendc(str, "}");
- case_end;
- case_ast_node(st, UnionType, node);
- str = gb_string_appendc(str, "union{");
- // str = write_field_list_to_string(str, st->decl_list, ", ");
- str = gb_string_appendc(str, "}");
- case_end;
- case_ast_node(et, EnumType, node);
- str = gb_string_appendc(str, "enum ");
- if (et->base_type != NULL) {
- str = write_expr_to_string(str, et->base_type);
- str = gb_string_appendc(str, " ");
- }
- str = gb_string_appendc(str, "{");
- str = gb_string_appendc(str, "}");
- case_end;
- }
- return str;
- }
- gbString expr_to_string(AstNode *expression) {
- return write_expr_to_string(gb_string_make(gb_heap_allocator(), ""), expression);
- }
|