expr.cpp 85 KB

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