expr.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. void check_assignment (Checker *c, Operand *operand, Type *type, String context_name);
  2. b32 check_is_assignable_to (Checker *c, Operand *operand, Type *type);
  3. void check_expr (Checker *c, Operand *operand, AstNode *expression);
  4. void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
  5. void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression);
  6. ExpressionKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint = NULL);
  7. Type * check_type (Checker *c, AstNode *expression, Type *named_type = NULL);
  8. void check_selector (Checker *c, Operand *operand, AstNode *node);
  9. void check_not_tuple (Checker *c, Operand *operand);
  10. void convert_to_typed (Checker *c, Operand *operand, Type *target_type);
  11. gbString expr_to_string (AstNode *expression);
  12. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type);
  13. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  14. void update_expr_type (Checker *c, AstNode *e, Type *type, b32 final);
  15. void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
  16. GB_ASSERT(node->kind == AstNode_StructType);
  17. GB_ASSERT(struct_type->kind == Type_Structure);
  18. ast_node(st, StructType, node);
  19. Map<Entity *> entity_map = {};
  20. map_init(&entity_map, gb_heap_allocator());
  21. defer (map_destroy(&entity_map));
  22. isize field_count = 0;
  23. for (AstNode *field = st->field_list; field != NULL; field = field->next) {
  24. for (AstNode *name = field->Field.name_list; name != NULL; name = name->next) {
  25. GB_ASSERT(name->kind == AstNode_Ident);
  26. field_count++;
  27. }
  28. }
  29. Entity **fields = gb_alloc_array(c->allocator, Entity *, st->field_count);
  30. isize field_index = 0;
  31. for (AstNode *field = st->field_list; field != NULL; field = field->next) {
  32. ast_node(f, Field, field);
  33. Type *type = check_type(c, f->type);
  34. for (AstNode *name = f->name_list; name != NULL; name = name->next) {
  35. ast_node(i, Ident, name);
  36. Token name_token = i->token;
  37. // TODO(bill): is the curr_scope correct?
  38. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type);
  39. HashKey key = hash_string(name_token.string);
  40. if (map_get(&entity_map, key)) {
  41. // TODO(bill): Scope checking already checks the declaration
  42. error(&c->error_collector, name_token, "`%.*s` is already declared in this structure", LIT(name_token.string));
  43. } else {
  44. map_set(&entity_map, key, e);
  45. fields[field_index++] = e;
  46. }
  47. add_entity_use(&c->info, name, e);
  48. }
  49. }
  50. struct_type->structure.fields = fields;
  51. struct_type->structure.field_count = field_count;
  52. struct_type->structure.is_packed = st->is_packed;
  53. }
  54. Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
  55. if (field_list == NULL || field_count == 0)
  56. return NULL;
  57. Type *tuple = make_type_tuple(c->allocator);
  58. Entity **variables = gb_alloc_array(c->allocator, Entity *, field_count);
  59. isize variable_index = 0;
  60. for (AstNode *field = field_list; field != NULL; field = field->next) {
  61. ast_node(f, Field, field);
  62. AstNode *type_expr = f->type;
  63. if (type_expr) {
  64. Type *type = check_type(c, type_expr);
  65. for (AstNode *name = f->name_list; name != NULL; name = name->next) {
  66. if (name->kind == AstNode_Ident) {
  67. ast_node(i, Ident, name);
  68. Entity *param = make_entity_param(c->allocator, scope, i->token, type);
  69. add_entity(c, scope, name, param);
  70. variables[variable_index++] = param;
  71. } else {
  72. error(&c->error_collector, ast_node_token(name), "Invalid parameter (invalid AST)");
  73. }
  74. }
  75. }
  76. }
  77. tuple->tuple.variables = variables;
  78. tuple->tuple.variable_count = field_count;
  79. return tuple;
  80. }
  81. Type *check_get_results(Checker *c, Scope *scope, AstNode *list, isize list_count) {
  82. if (list == NULL)
  83. return NULL;
  84. Type *tuple = make_type_tuple(c->allocator);
  85. Entity **variables = gb_alloc_array(c->allocator, Entity *, list_count);
  86. isize variable_index = 0;
  87. for (AstNode *item = list; item != NULL; item = item->next) {
  88. Type *type = check_type(c, item);
  89. Token token = ast_node_token(item);
  90. token.string = make_string(""); // NOTE(bill): results are not named
  91. // TODO(bill): Should I have named results?
  92. Entity *param = make_entity_param(c->allocator, scope, token, type);
  93. // NOTE(bill): No need to record
  94. variables[variable_index++] = param;
  95. }
  96. tuple->tuple.variables = variables;
  97. tuple->tuple.variable_count = list_count;
  98. return tuple;
  99. }
  100. void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
  101. ast_node(pt, ProcType, proc_type_node);
  102. isize param_count = pt->param_count;
  103. isize result_count = pt->result_count;
  104. // gb_printf("%td -> %td\n", param_count, result_count);
  105. Type *params = check_get_params(c, c->context.scope, pt->param_list, param_count);
  106. Type *results = check_get_results(c, c->context.scope, pt->result_list, result_count);
  107. type->proc.scope = c->context.scope;
  108. type->proc.params = params;
  109. type->proc.param_count = pt->param_count;
  110. type->proc.results = results;
  111. type->proc.result_count = pt->result_count;
  112. }
  113. void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
  114. GB_ASSERT(n->kind == AstNode_Ident);
  115. o->mode = Addressing_Invalid;
  116. o->expr = n;
  117. ast_node(i, Ident, n);
  118. Entity *e = scope_lookup_entity(c->context.scope, i->token.string);
  119. if (e == NULL) {
  120. error(&c->error_collector, i->token,
  121. "Undeclared type or identifier `%.*s`", LIT(i->token.string));
  122. return;
  123. }
  124. add_entity_use(&c->info, n, e);
  125. if (e->type == NULL) {
  126. auto *found = map_get(&c->info.entities, hash_pointer(e));
  127. if (found != NULL) {
  128. check_entity_decl(c, e, *found, named_type);
  129. } else {
  130. GB_PANIC("Internal Compiler Error: DeclInfo not found!");
  131. }
  132. }
  133. if (e->type == NULL) {
  134. GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(i->token.string));
  135. return;
  136. }
  137. switch (e->kind) {
  138. case Entity_Constant:
  139. add_declaration_dependency(c, e);
  140. if (e->type == t_invalid)
  141. return;
  142. o->value = e->constant.value;
  143. GB_ASSERT(o->value.kind != ExactValue_Invalid);
  144. o->mode = Addressing_Constant;
  145. break;
  146. case Entity_Variable:
  147. add_declaration_dependency(c, e);
  148. e->variable.used = true;
  149. if (e->type == t_invalid)
  150. return;
  151. o->mode = Addressing_Variable;
  152. break;
  153. case Entity_TypeName:
  154. case Entity_AliasName:
  155. o->mode = Addressing_Type;
  156. break;
  157. case Entity_Procedure:
  158. add_declaration_dependency(c, e);
  159. o->mode = Addressing_Value;
  160. break;
  161. case Entity_Builtin:
  162. o->builtin_id = e->builtin.id;
  163. o->mode = Addressing_Builtin;
  164. break;
  165. default:
  166. GB_PANIC("Compiler error: Unknown EntityKind");
  167. break;
  168. }
  169. o->type = e->type;
  170. }
  171. i64 check_array_count(Checker *c, AstNode *e) {
  172. if (e) {
  173. Operand o = {};
  174. check_expr(c, &o, e);
  175. if (o.mode != Addressing_Constant) {
  176. if (o.mode != Addressing_Invalid) {
  177. error(&c->error_collector, ast_node_token(e), "Array count must be a constant");
  178. }
  179. return 0;
  180. }
  181. if (is_type_untyped(o.type) || is_type_integer(o.type)) {
  182. if (o.value.kind == ExactValue_Integer) {
  183. i64 count = o.value.value_integer;
  184. if (count >= 0)
  185. return count;
  186. error(&c->error_collector, ast_node_token(e), "Invalid array count");
  187. return 0;
  188. }
  189. }
  190. error(&c->error_collector, ast_node_token(e), "Array count must be an integer");
  191. }
  192. return 0;
  193. }
  194. Type *check_type_expr_extra(Checker *c, AstNode *e, Type *named_type) {
  195. gbString err_str = NULL;
  196. defer (gb_string_free(err_str));
  197. switch (e->kind) {
  198. case_ast_node(i, Ident, e);
  199. Operand o = {};
  200. check_identifier(c, &o, e, named_type);
  201. switch (o.mode) {
  202. case Addressing_Type: {
  203. Type *t = o.type;
  204. set_base_type(named_type, t);
  205. return t;
  206. } break;
  207. case Addressing_Invalid:
  208. break;
  209. case Addressing_NoValue:
  210. err_str = expr_to_string(e);
  211. error(&c->error_collector, ast_node_token(e), "`%s` used as a type", err_str);
  212. break;
  213. default:
  214. err_str = expr_to_string(e);
  215. error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
  216. break;
  217. }
  218. case_end;
  219. case_ast_node(pe, ParenExpr, e);
  220. return check_type(c, pe->expr, named_type);
  221. case_end;
  222. case_ast_node(at, ArrayType, e);
  223. if (at->count != NULL) {
  224. Type *t = make_type_array(c->allocator,
  225. check_type(c, at->elem),
  226. check_array_count(c, at->count));
  227. set_base_type(named_type, t);
  228. return t;
  229. } else {
  230. Type *t = make_type_slice(c->allocator, check_type(c, at->elem));
  231. set_base_type(named_type, t);
  232. return t;
  233. }
  234. case_end;
  235. case_ast_node(vt, VectorType, e);
  236. Type *elem = check_type(c, vt->elem);
  237. Type *be = get_base_type(elem);
  238. if (!is_type_vector(be) &&
  239. !(is_type_boolean(be) || is_type_numeric(be))) {
  240. err_str = type_to_string(elem);
  241. error(&c->error_collector, ast_node_token(vt->elem), "Vector element type must be a boolean, numerical, or vector. Got `%s`", err_str);
  242. break;
  243. } else {
  244. i64 count = check_array_count(c, vt->count);
  245. Type *t = make_type_vector(c->allocator, elem, count);
  246. set_base_type(named_type, t);
  247. return t;
  248. }
  249. case_end;
  250. case_ast_node(st, StructType, e);
  251. Type *t = make_type_structure(c->allocator);
  252. set_base_type(named_type, t);
  253. check_struct_type(c, t, e);
  254. return t;
  255. case_end;
  256. case_ast_node(pt, PointerType, e);
  257. Type *t = make_type_pointer(c->allocator, check_type(c, pt->type));
  258. set_base_type(named_type, t);
  259. return t;
  260. case_end;
  261. case_ast_node(pt, ProcType, e);
  262. Type *t = alloc_type(c->allocator, Type_Proc);
  263. set_base_type(named_type, t);
  264. check_open_scope(c, e);
  265. check_procedure_type(c, t, e);
  266. check_close_scope(c);
  267. return t;
  268. case_end;
  269. default:
  270. err_str = expr_to_string(e);
  271. error(&c->error_collector, ast_node_token(e), "`%s` is not a type", err_str);
  272. break;
  273. }
  274. Type *t = t_invalid;
  275. set_base_type(named_type, t);
  276. return t;
  277. }
  278. Type *check_type(Checker *c, AstNode *e, Type *named_type) {
  279. ExactValue null_value = {ExactValue_Invalid};
  280. Type *type = NULL;
  281. gbString err_str = NULL;
  282. defer (gb_string_free(err_str));
  283. switch (e->kind) {
  284. case_ast_node(i, Ident, e);
  285. Operand operand = {};
  286. check_identifier(c, &operand, e, named_type);
  287. switch (operand.mode) {
  288. case Addressing_Type: {
  289. type = operand.type;
  290. set_base_type(named_type, type);
  291. goto end;
  292. } break;
  293. case Addressing_Invalid:
  294. break;
  295. case Addressing_NoValue:
  296. err_str = expr_to_string(e);
  297. error(&c->error_collector, ast_node_token(e), "`%s` used as a type", err_str);
  298. break;
  299. default:
  300. err_str = expr_to_string(e);
  301. error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
  302. break;
  303. }
  304. case_end;
  305. case_ast_node(se, SelectorExpr, e);
  306. Operand o = {};
  307. check_selector(c, &o, e);
  308. if (o.mode == Addressing_Type) {
  309. set_base_type(type, o.type);
  310. return o.type;
  311. }
  312. case_end;
  313. case_ast_node(pe, ParenExpr, e);
  314. return check_type(c, pe->expr, named_type);
  315. case_end;
  316. case_ast_node(at, ArrayType, e);
  317. if (at->count != NULL) {
  318. type = make_type_array(c->allocator,
  319. check_type(c, at->elem),
  320. check_array_count(c, at->count));
  321. set_base_type(named_type, type);
  322. } else {
  323. type = make_type_slice(c->allocator, check_type(c, at->elem));
  324. set_base_type(named_type, type);
  325. }
  326. goto end;
  327. case_end;
  328. case_ast_node(vt, VectorType, e);
  329. Type *elem = check_type(c, vt->elem);
  330. Type *be = get_base_type(elem);
  331. i64 count = check_array_count(c, vt->count);
  332. if (!is_type_boolean(be) && !is_type_numeric(be)) {
  333. err_str = type_to_string(elem);
  334. error(&c->error_collector, ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
  335. }
  336. type = make_type_vector(c->allocator, elem, count);
  337. set_base_type(named_type, type);
  338. goto end;
  339. case_end;
  340. case_ast_node(st, StructType, e);
  341. type = make_type_structure(c->allocator);
  342. set_base_type(named_type, type);
  343. check_struct_type(c, type, e);
  344. goto end;
  345. case_end;
  346. case_ast_node(pt, PointerType, e);
  347. type = make_type_pointer(c->allocator, check_type(c, pt->type));
  348. set_base_type(named_type, type);
  349. goto end;
  350. case_end;
  351. case_ast_node(pt, ProcType, e);
  352. type = alloc_type(c->allocator, Type_Proc);
  353. set_base_type(named_type, type);
  354. check_procedure_type(c, type, e);
  355. goto end;
  356. case_end;
  357. default:
  358. err_str = expr_to_string(e);
  359. error(&c->error_collector, ast_node_token(e), "`%s` is not a type", err_str);
  360. break;
  361. }
  362. type = t_invalid;
  363. set_base_type(named_type, type);
  364. end:
  365. GB_ASSERT(is_type_typed(type));
  366. add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
  367. return type;
  368. }
  369. b32 check_unary_op(Checker *c, Operand *o, Token op) {
  370. // TODO(bill): Handle errors correctly
  371. Type *type = get_base_type(base_vector_type(get_base_type(o->type)));
  372. gbString str = NULL;
  373. defer (gb_string_free(str));
  374. switch (op.kind) {
  375. case Token_Add:
  376. case Token_Sub:
  377. if (!is_type_numeric(type)) {
  378. str = expr_to_string(o->expr);
  379. error(&c->error_collector, op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  380. }
  381. break;
  382. case Token_Xor:
  383. if (!is_type_integer(type)) {
  384. error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  385. }
  386. break;
  387. case Token_Not:
  388. if (!is_type_boolean(type)) {
  389. str = expr_to_string(o->expr);
  390. error(&c->error_collector, op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  391. }
  392. break;
  393. default:
  394. error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
  395. return false;
  396. }
  397. return true;
  398. }
  399. b32 check_binary_op(Checker *c, Operand *o, Token op) {
  400. // TODO(bill): Handle errors correctly
  401. Type *type = get_base_type(base_vector_type(o->type));
  402. switch (op.kind) {
  403. case Token_Add:
  404. case Token_Sub:
  405. case Token_Mul:
  406. case Token_Quo:
  407. case Token_AddEq:
  408. case Token_SubEq:
  409. case Token_MulEq:
  410. case Token_QuoEq:
  411. if (!is_type_numeric(type)) {
  412. error(&c->error_collector, op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  413. return false;
  414. }
  415. break;
  416. case Token_Mod:
  417. case Token_And:
  418. case Token_Or:
  419. case Token_Xor:
  420. case Token_AndNot:
  421. case Token_ModEq:
  422. case Token_AndEq:
  423. case Token_OrEq:
  424. case Token_XorEq:
  425. case Token_AndNotEq:
  426. if (!is_type_integer(type)) {
  427. error(&c->error_collector, op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  428. return false;
  429. }
  430. break;
  431. case Token_CmpAnd:
  432. case Token_CmpOr:
  433. case Token_CmpAndEq:
  434. case Token_CmpOrEq:
  435. if (!is_type_boolean(type)) {
  436. error(&c->error_collector, op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  437. return false;
  438. }
  439. break;
  440. default:
  441. error(&c->error_collector, op, "Unknown operator `%.*s`", LIT(op.string));
  442. return false;
  443. }
  444. return true;
  445. }
  446. b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  447. if (in_value.kind == ExactValue_Invalid)
  448. return true;
  449. if (is_type_boolean(type)) {
  450. return in_value.kind == ExactValue_Bool;
  451. } else if (is_type_string(type)) {
  452. return in_value.kind == ExactValue_String;
  453. } else if (is_type_integer(type)) {
  454. if (in_value.kind != ExactValue_Integer)
  455. return false;
  456. if (out_value) *out_value = in_value;
  457. i64 i = in_value.value_integer;
  458. i64 s = 8*type_size_of(c->sizes, c->allocator, type);
  459. u64 umax = ~0ull;
  460. if (s < 64) {
  461. umax = (1ull << s) - 1ull;
  462. }
  463. i64 imax = (1ll << (s-1ll));
  464. switch (type->basic.kind) {
  465. case Basic_i8:
  466. case Basic_i16:
  467. case Basic_i32:
  468. case Basic_i64:
  469. case Basic_int:
  470. return gb_is_between(i, -imax, imax-1);
  471. case Basic_u8:
  472. case Basic_u16:
  473. case Basic_u32:
  474. case Basic_u64:
  475. case Basic_uint:
  476. return !(i < 0 || cast(u64)i > umax);
  477. case Basic_UntypedInteger:
  478. return true;
  479. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  480. }
  481. } else if (is_type_float(type)) {
  482. ExactValue v = exact_value_to_float(in_value);
  483. if (v.kind != ExactValue_Float)
  484. return false;
  485. switch (type->basic.kind) {
  486. case Basic_f32:
  487. if (out_value) *out_value = v;
  488. return true;
  489. case Basic_f64:
  490. if (out_value) *out_value = v;
  491. return true;
  492. case Basic_UntypedFloat:
  493. return true;
  494. }
  495. } else if (is_type_pointer(type)) {
  496. if (in_value.kind == ExactValue_Pointer)
  497. return true;
  498. if (in_value.kind == ExactValue_Integer)
  499. return true;
  500. if (out_value) *out_value = in_value;
  501. }
  502. return false;
  503. }
  504. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  505. GB_ASSERT(type->kind == Type_Basic);
  506. GB_ASSERT(o->mode == Addressing_Constant);
  507. if (!check_value_is_expressible(c, o->value, type, &o->value)) {
  508. gbString a = expr_to_string(o->expr);
  509. gbString b = type_to_string(type);
  510. defer (gb_string_free(a));
  511. defer (gb_string_free(b));
  512. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  513. if (!is_type_integer(o->type) && is_type_integer(type)) {
  514. error(&c->error_collector, ast_node_token(o->expr), "`%s` truncated to `%s`", a, b);
  515. } else {
  516. error(&c->error_collector, ast_node_token(o->expr), "`%s` overflows `%s`", a, b);
  517. }
  518. } else {
  519. error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
  520. }
  521. o->mode = Addressing_Invalid;
  522. }
  523. }
  524. b32 check_is_expr_vector_index(Checker *c, AstNode *expr) {
  525. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  526. expr = unparen_expr(expr);
  527. if (expr->kind == AstNode_IndexExpr) {
  528. ast_node(ie, IndexExpr, expr);
  529. Type *t = type_of_expr(&c->info, ie->expr);
  530. if (t != NULL) {
  531. return is_type_vector(get_base_type(t));
  532. }
  533. }
  534. return false;
  535. }
  536. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  537. if (op.kind == Token_Pointer) { // Pointer address
  538. if (o->mode != Addressing_Variable ||
  539. check_is_expr_vector_index(c, o->expr)) {
  540. ast_node(ue, UnaryExpr, node);
  541. gbString str = expr_to_string(ue->expr);
  542. defer (gb_string_free(str));
  543. error(&c->error_collector, op, "Cannot take the pointer address of `%s`", str);
  544. o->mode = Addressing_Invalid;
  545. return;
  546. }
  547. o->mode = Addressing_Value;
  548. o->type = make_type_pointer(c->allocator, o->type);
  549. return;
  550. }
  551. if (!check_unary_op(c, o, op)) {
  552. o->mode = Addressing_Invalid;
  553. return;
  554. }
  555. if (o->mode == Addressing_Constant) {
  556. Type *type = get_base_type(o->type);
  557. GB_ASSERT(type->kind == Type_Basic);
  558. i32 precision = 0;
  559. if (is_type_unsigned(type))
  560. precision = cast(i32)(8 * type_size_of(c->sizes, c->allocator, type));
  561. o->value = exact_unary_operator_value(op, o->value, precision);
  562. if (is_type_typed(type)) {
  563. if (node != NULL)
  564. o->expr = node;
  565. check_is_expressible(c, o, type);
  566. }
  567. return;
  568. }
  569. o->mode = Addressing_Value;
  570. }
  571. void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
  572. gbString err_str = NULL;
  573. defer (gb_string_free(err_str));
  574. if (check_is_assignable_to(c, x, y->type) ||
  575. check_is_assignable_to(c, y, x->type)) {
  576. b32 defined = false;
  577. switch (op.kind) {
  578. case Token_CmpEq:
  579. case Token_NotEq:
  580. defined = is_type_comparable(x->type);
  581. break;
  582. case Token_Lt:
  583. case Token_Gt:
  584. case Token_LtEq:
  585. case Token_GtEq: {
  586. defined = is_type_ordered(x->type);
  587. } break;
  588. }
  589. if (!defined) {
  590. gbString type_string = type_to_string(x->type);
  591. err_str = gb_string_make(gb_heap_allocator(),
  592. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(op.string), type_string));
  593. gb_string_free(type_string);
  594. }
  595. } else {
  596. gbString xt = type_to_string(x->type);
  597. gbString yt = type_to_string(y->type);
  598. defer(gb_string_free(xt));
  599. defer(gb_string_free(yt));
  600. err_str = gb_string_make(gb_heap_allocator(),
  601. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  602. }
  603. if (err_str) {
  604. error(&c->error_collector, op, "Cannot compare expression, %s", err_str);
  605. return;
  606. }
  607. if (x->mode == Addressing_Constant &&
  608. y->mode == Addressing_Constant) {
  609. x->value = make_exact_value_bool(compare_exact_values(op, x->value, y->value));
  610. } else {
  611. x->mode = Addressing_Value;
  612. update_expr_type(c, x->expr, default_type(x->type), true);
  613. update_expr_type(c, y->expr, default_type(y->type), true);
  614. }
  615. if (is_type_vector(get_base_type(y->type))) {
  616. x->type = make_type_vector(c->allocator, t_bool, get_base_type(y->type)->vector.count);
  617. } else {
  618. x->type = t_untyped_bool;
  619. }
  620. }
  621. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  622. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  623. ast_node(be, BinaryExpr, node);
  624. ExactValue x_val = {};
  625. if (x->mode == Addressing_Constant) {
  626. x_val = exact_value_to_integer(x->value);
  627. }
  628. b32 x_is_untyped = is_type_untyped(x->type);
  629. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  630. gbString err_str = expr_to_string(x->expr);
  631. defer (gb_string_free(err_str));
  632. error(&c->error_collector, ast_node_token(node),
  633. "Shifted operand `%s` must be an integer", err_str);
  634. x->mode = Addressing_Invalid;
  635. return;
  636. }
  637. if (is_type_unsigned(y->type)) {
  638. } else if (is_type_untyped(y->type)) {
  639. convert_to_typed(c, y, t_untyped_integer);
  640. if (y->mode == Addressing_Invalid) {
  641. x->mode = Addressing_Invalid;
  642. return;
  643. }
  644. } else {
  645. gbString err_str = expr_to_string(y->expr);
  646. defer (gb_string_free(err_str));
  647. error(&c->error_collector, ast_node_token(node),
  648. "Shift amount `%s` must be an unsigned integer", err_str);
  649. x->mode = Addressing_Invalid;
  650. return;
  651. }
  652. if (x->mode == Addressing_Constant) {
  653. if (y->mode == Addressing_Constant) {
  654. ExactValue y_val = exact_value_to_integer(y->value);
  655. if (y_val.kind != ExactValue_Integer) {
  656. gbString err_str = expr_to_string(y->expr);
  657. defer (gb_string_free(err_str));
  658. error(&c->error_collector, ast_node_token(node),
  659. "Shift amount `%s` must be an unsigned integer", err_str);
  660. x->mode = Addressing_Invalid;
  661. return;
  662. }
  663. u64 amount = cast(u64)y_val.value_integer;
  664. if (amount > 1074) {
  665. gbString err_str = expr_to_string(y->expr);
  666. defer (gb_string_free(err_str));
  667. error(&c->error_collector, ast_node_token(node),
  668. "Shift amount too large: `%s`", err_str);
  669. x->mode = Addressing_Invalid;
  670. return;
  671. }
  672. if (!is_type_integer(x->type)) {
  673. // NOTE(bill): It could be an untyped float but still representable
  674. // as an integer
  675. x->type = t_untyped_integer;
  676. }
  677. x->value = exact_value_shift(be->op, x_val, make_exact_value_integer(amount));
  678. if (is_type_typed(x->type)) {
  679. check_is_expressible(c, x, get_base_type(x->type));
  680. }
  681. return;
  682. }
  683. if (x_is_untyped) {
  684. ExpressionInfo *info = map_get(&c->info.untyped, hash_pointer(x->expr));
  685. if (info != NULL) {
  686. info->is_lhs = true;
  687. }
  688. x->mode = Addressing_Value;
  689. return;
  690. }
  691. }
  692. if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
  693. gbString err_str = expr_to_string(y->expr);
  694. defer (gb_string_free(err_str));
  695. error(&c->error_collector, ast_node_token(node),
  696. "Shift amount cannot be negative: `%s`", err_str);
  697. }
  698. x->mode = Addressing_Value;
  699. }
  700. b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
  701. if (check_is_assignable_to(c, operand, y))
  702. return true;
  703. Type *x = operand->type;
  704. Type *xb = get_base_type(x);
  705. Type *yb = get_base_type(y);
  706. if (are_types_identical(xb, yb))
  707. return true;
  708. // Cast between booleans and integers
  709. if (is_type_boolean(x) || is_type_integer(x)) {
  710. if (is_type_boolean(y) || is_type_integer(y))
  711. return true;
  712. }
  713. // Cast between numbers
  714. if (is_type_integer(x) || is_type_float(x)) {
  715. if (is_type_integer(y) || is_type_float(y))
  716. return true;
  717. }
  718. // Cast between pointers
  719. if (is_type_pointer(x)) {
  720. if (is_type_pointer(y))
  721. return true;
  722. }
  723. // untyped integers -> pointers
  724. if (is_type_untyped(xb) && is_type_integer(xb)) {
  725. if (is_type_pointer(yb))
  726. return true;
  727. }
  728. // (u)int <-> pointer
  729. if (is_type_pointer(xb) || is_type_int_or_uint(xb)) {
  730. if (is_type_pointer(yb))
  731. return true;
  732. }
  733. if (is_type_pointer(xb)) {
  734. if (is_type_pointer(yb) || is_type_int_or_uint(yb))
  735. return true;
  736. }
  737. // []byte/[]u8 <-> string
  738. if (is_type_u8_slice(xb) && is_type_string(yb)) {
  739. return true;
  740. }
  741. if (is_type_string(xb) && is_type_u8_slice(yb)) {
  742. return true;
  743. }
  744. return false;
  745. }
  746. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  747. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  748. Operand y_ = {}, *y = &y_;
  749. gbString err_str = NULL;
  750. defer (gb_string_free(err_str));
  751. ast_node(be, BinaryExpr, node);
  752. if (be->op.kind == Token_as) {
  753. check_expr(c, x, be->left);
  754. Type *type = check_type(c, be->right);
  755. if (x->mode == Addressing_Invalid)
  756. return;
  757. b32 is_const_expr = x->mode == Addressing_Constant;
  758. b32 can_convert = false;
  759. if (is_const_expr && is_type_constant_type(type)) {
  760. Type *t = get_base_type(type);
  761. if (t->kind == Type_Basic) {
  762. if (check_value_is_expressible(c, x->value, t, &x->value)) {
  763. can_convert = true;
  764. }
  765. }
  766. } else if (check_castable_to(c, x, type)) {
  767. x->mode = Addressing_Value;
  768. can_convert = true;
  769. }
  770. if (!can_convert) {
  771. gbString expr_str = expr_to_string(x->expr);
  772. gbString type_str = type_to_string(type);
  773. defer (gb_string_free(expr_str));
  774. defer (gb_string_free(type_str));
  775. error(&c->error_collector, ast_node_token(x->expr), "Cannot cast `%s` to `%s`", expr_str, type_str);
  776. x->mode = Addressing_Invalid;
  777. return;
  778. }
  779. if (is_type_untyped(x->type)) {
  780. Type *final_type = type;
  781. if (is_const_expr && !is_type_constant_type(type)) {
  782. final_type = default_type(x->type);
  783. }
  784. update_expr_type(c, x->expr, final_type, true);
  785. }
  786. x->type = type;
  787. return;
  788. } else if (be->op.kind == Token_transmute) {
  789. check_expr(c, x, be->left);
  790. Type *type = check_type(c, be->right);
  791. if (x->mode == Addressing_Invalid)
  792. return;
  793. if (x->mode == Addressing_Constant) {
  794. gbString expr_str = expr_to_string(x->expr);
  795. defer (gb_string_free(expr_str));
  796. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute constant expression: `%s`", expr_str);
  797. x->mode = Addressing_Invalid;
  798. return;
  799. }
  800. if (is_type_untyped(x->type)) {
  801. gbString expr_str = expr_to_string(x->expr);
  802. defer (gb_string_free(expr_str));
  803. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute untyped expression: `%s`", expr_str);
  804. x->mode = Addressing_Invalid;
  805. return;
  806. }
  807. i64 otz = type_size_of(c->sizes, c->allocator, x->type);
  808. i64 ttz = type_size_of(c->sizes, c->allocator, type);
  809. if (otz != ttz) {
  810. gbString expr_str = expr_to_string(x->expr);
  811. gbString type_str = type_to_string(type);
  812. defer (gb_string_free(expr_str));
  813. defer (gb_string_free(type_str));
  814. error(&c->error_collector, ast_node_token(x->expr), "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, otz, ttz);
  815. x->mode = Addressing_Invalid;
  816. return;
  817. }
  818. x->type = type;
  819. return;
  820. }
  821. check_expr(c, x, be->left);
  822. check_expr(c, y, be->right);
  823. if (x->mode == Addressing_Invalid) {
  824. return;
  825. }
  826. if (y->mode == Addressing_Invalid) {
  827. x->mode = Addressing_Invalid;
  828. x->expr = y->expr;
  829. return;
  830. }
  831. if (token_is_shift(be->op)) {
  832. check_shift(c, x, y, node);
  833. return;
  834. }
  835. convert_to_typed(c, x, y->type);
  836. if (x->mode == Addressing_Invalid) return;
  837. convert_to_typed(c, y, x->type);
  838. if (y->mode == Addressing_Invalid) {
  839. x->mode = Addressing_Invalid;
  840. return;
  841. }
  842. Token op = be->op;
  843. if (token_is_comparison(op)) {
  844. check_comparison(c, x, y, op);
  845. return;
  846. }
  847. if (!are_types_identical(x->type, y->type)) {
  848. if (x->type != t_invalid &&
  849. y->type != t_invalid) {
  850. gbString xt = type_to_string(x->type);
  851. gbString yt = type_to_string(y->type);
  852. defer (gb_string_free(xt));
  853. defer (gb_string_free(yt));
  854. err_str = expr_to_string(x->expr);
  855. error(&c->error_collector, op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", err_str, xt, yt);
  856. }
  857. x->mode = Addressing_Invalid;
  858. return;
  859. }
  860. if (!check_binary_op(c, x, op)) {
  861. x->mode = Addressing_Invalid;
  862. return;
  863. }
  864. switch (op.kind) {
  865. case Token_Quo:
  866. case Token_Mod:
  867. case Token_QuoEq:
  868. case Token_ModEq:
  869. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  870. y->mode == Addressing_Constant) {
  871. b32 fail = false;
  872. switch (y->value.kind) {
  873. case ExactValue_Integer:
  874. if (y->value.value_integer == 0)
  875. fail = true;
  876. break;
  877. case ExactValue_Float:
  878. if (y->value.value_float == 0.0)
  879. fail = true;
  880. break;
  881. }
  882. if (fail) {
  883. error(&c->error_collector, ast_node_token(y->expr), "Division by zero not allowed");
  884. x->mode = Addressing_Invalid;
  885. return;
  886. }
  887. }
  888. }
  889. if (x->mode == Addressing_Constant &&
  890. y->mode == Addressing_Constant) {
  891. ExactValue a = x->value;
  892. ExactValue b = y->value;
  893. Type *type = get_base_type(x->type);
  894. GB_ASSERT(type->kind == Type_Basic);
  895. if (op.kind == Token_Quo && is_type_integer(type)) {
  896. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  897. }
  898. x->value = exact_binary_operator_value(op, a, b);
  899. if (is_type_typed(type)) {
  900. if (node != NULL)
  901. x->expr = node;
  902. check_is_expressible(c, x, type);
  903. }
  904. return;
  905. }
  906. x->mode = Addressing_Value;
  907. }
  908. void update_expr_type(Checker *c, AstNode *e, Type *type, b32 final) {
  909. HashKey key = hash_pointer(e);
  910. ExpressionInfo *found = map_get(&c->info.untyped, key);
  911. if (found == NULL)
  912. return;
  913. switch (e->kind) {
  914. case_ast_node(ue, UnaryExpr, e);
  915. if (found->value.kind != ExactValue_Invalid)
  916. break;
  917. update_expr_type(c, ue->expr, type, final);
  918. case_end;
  919. case_ast_node(be, BinaryExpr, e);
  920. if (found->value.kind != ExactValue_Invalid)
  921. break;
  922. if (!token_is_comparison(be->op)) {
  923. if (token_is_shift(be->op)) {
  924. update_expr_type(c, be->left, type, final);
  925. } else {
  926. update_expr_type(c, be->left, type, final);
  927. update_expr_type(c, be->right, type, final);
  928. }
  929. }
  930. case_end;
  931. }
  932. if (!final && is_type_untyped(type)) {
  933. found->type = get_base_type(type);
  934. map_set(&c->info.untyped, key, *found);
  935. } else {
  936. ExpressionInfo old = *found;
  937. map_remove(&c->info.untyped, key);
  938. if (old.is_lhs && !is_type_integer(type)) {
  939. gbString expr_str = expr_to_string(e);
  940. gbString type_str = type_to_string(type);
  941. defer (gb_string_free(expr_str));
  942. defer (gb_string_free(type_str));
  943. error(&c->error_collector, ast_node_token(e), "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  944. return;
  945. }
  946. add_type_and_value(&c->info, e, found->mode, type, found->value);
  947. }
  948. }
  949. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  950. ExpressionInfo *found = map_get(&c->info.untyped, hash_pointer(e));
  951. if (found)
  952. found->value = value;
  953. }
  954. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  955. gbString expr_str = expr_to_string(operand->expr);
  956. gbString type_str = type_to_string(target_type);
  957. char *extra_text = "";
  958. defer (gb_string_free(expr_str));
  959. defer (gb_string_free(type_str));
  960. if (operand->mode == Addressing_Constant) {
  961. if (operand->value.value_integer == 0) {
  962. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  963. extra_text = " - Did you want `null`?";
  964. }
  965. }
  966. error(&c->error_collector, ast_node_token(operand->expr), "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  967. operand->mode = Addressing_Invalid;
  968. }
  969. void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
  970. GB_ASSERT_NOT_NULL(target_type);
  971. if (operand->mode == Addressing_Invalid ||
  972. is_type_typed(operand->type) ||
  973. target_type == t_invalid) {
  974. return;
  975. }
  976. if (is_type_untyped(target_type)) {
  977. Type *x = operand->type;
  978. Type *y = target_type;
  979. if (is_type_numeric(x) && is_type_numeric(y)) {
  980. if (x < y) {
  981. operand->type = target_type;
  982. update_expr_type(c, operand->expr, target_type, false);
  983. }
  984. } else if (x != y) {
  985. convert_untyped_error(c, operand, target_type);
  986. }
  987. return;
  988. }
  989. Type *t = get_base_type(target_type);
  990. switch (t->kind) {
  991. case Type_Basic:
  992. if (operand->mode == Addressing_Constant) {
  993. check_is_expressible(c, operand, t);
  994. if (operand->mode == Addressing_Invalid) {
  995. return;
  996. }
  997. update_expr_value(c, operand->expr, operand->value);
  998. } else {
  999. // TODO(bill): Is this really needed?
  1000. switch (operand->type->basic.kind) {
  1001. case Basic_UntypedBool:
  1002. if (!is_type_boolean(target_type)) {
  1003. convert_untyped_error(c, operand, target_type);
  1004. return;
  1005. }
  1006. break;
  1007. case Basic_UntypedInteger:
  1008. case Basic_UntypedFloat:
  1009. case Basic_UntypedRune:
  1010. if (!is_type_numeric(target_type)) {
  1011. convert_untyped_error(c, operand, target_type);
  1012. return;
  1013. }
  1014. break;
  1015. }
  1016. }
  1017. break;
  1018. case Type_Pointer:
  1019. switch (operand->type->basic.kind) {
  1020. case Basic_UntypedPointer:
  1021. target_type = t_untyped_pointer;
  1022. break;
  1023. default:
  1024. convert_untyped_error(c, operand, target_type);
  1025. return;
  1026. }
  1027. break;
  1028. default:
  1029. convert_untyped_error(c, operand, target_type);
  1030. return;
  1031. }
  1032. operand->type = target_type;
  1033. }
  1034. b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *value) {
  1035. Operand operand = {Addressing_Invalid};
  1036. check_expr(c, &operand, index_value);
  1037. if (operand.mode == Addressing_Invalid) {
  1038. if (value) *value = 0;
  1039. return false;
  1040. }
  1041. convert_to_typed(c, &operand, t_int);
  1042. if (operand.mode == Addressing_Invalid) {
  1043. if (value) *value = 0;
  1044. return false;
  1045. }
  1046. if (!is_type_integer(operand.type)) {
  1047. gbString expr_str = expr_to_string(operand.expr);
  1048. error(&c->error_collector, ast_node_token(operand.expr),
  1049. "Index `%s` must be an integer", expr_str);
  1050. gb_string_free(expr_str);
  1051. if (value) *value = 0;
  1052. return false;
  1053. }
  1054. if (operand.mode == Addressing_Constant) {
  1055. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  1056. i64 i = exact_value_to_integer(operand.value).value_integer;
  1057. if (i < 0) {
  1058. gbString expr_str = expr_to_string(operand.expr);
  1059. error(&c->error_collector, ast_node_token(operand.expr),
  1060. "Index `%s` cannot be a negative value", expr_str);
  1061. gb_string_free(expr_str);
  1062. if (value) *value = 0;
  1063. return false;
  1064. }
  1065. if (value) *value = i;
  1066. if (i >= max_count) {
  1067. gbString expr_str = expr_to_string(operand.expr);
  1068. error(&c->error_collector, ast_node_token(operand.expr),
  1069. "Index `%s` is out of bounds range [0, %lld)", expr_str, max_count);
  1070. gb_string_free(expr_str);
  1071. return false;
  1072. }
  1073. return true;
  1074. }
  1075. }
  1076. // NOTE(bill): It's alright :D
  1077. if (value) *value = -1;
  1078. return true;
  1079. }
  1080. Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
  1081. GB_ASSERT(type != NULL);
  1082. GB_ASSERT(field_node->kind == AstNode_Ident);
  1083. type = get_base_type(type);
  1084. if (type->kind == Type_Pointer)
  1085. type = get_base_type(type->pointer.elem);
  1086. ast_node(i, Ident, field_node);
  1087. String field_str = i->token.string;
  1088. switch (type->kind) {
  1089. case Type_Structure:
  1090. for (isize i = 0; i < type->structure.field_count; i++) {
  1091. Entity *f = type->structure.fields[i];
  1092. GB_ASSERT(f->kind == Entity_Variable && f->variable.is_field);
  1093. String str = f->token.string;
  1094. if (are_strings_equal(field_str, str)) {
  1095. if (index) *index = i;
  1096. return f;
  1097. }
  1098. }
  1099. break;
  1100. // TODO(bill): Other types and extra "hidden" fields (e.g. introspection stuff)
  1101. // TODO(bill): Allow for access of field through index? e.g. `x.3` will get member of index 3
  1102. // Or is this only suitable if tuples are first-class?
  1103. }
  1104. return NULL;
  1105. }
  1106. void check_selector(Checker *c, Operand *operand, AstNode *node) {
  1107. GB_ASSERT(node->kind == AstNode_SelectorExpr);
  1108. ast_node(se, SelectorExpr, node);
  1109. AstNode *op_expr = se->expr;
  1110. AstNode *selector = se->selector;
  1111. if (selector) {
  1112. Entity *entity = lookup_field(operand->type, selector);
  1113. if (entity == NULL) {
  1114. gbString op_str = expr_to_string(op_expr);
  1115. gbString sel_str = expr_to_string(selector);
  1116. defer (gb_string_free(op_str));
  1117. defer (gb_string_free(sel_str));
  1118. error(&c->error_collector, ast_node_token(op_expr), "`%s` has no field `%s`", op_str, sel_str);
  1119. operand->mode = Addressing_Invalid;
  1120. operand->expr = node;
  1121. return;
  1122. }
  1123. add_entity_use(&c->info, selector, entity);
  1124. operand->type = entity->type;
  1125. operand->expr = node;
  1126. if (operand->mode != Addressing_Variable)
  1127. operand->mode = Addressing_Value;
  1128. } else {
  1129. operand->mode = Addressing_Invalid;
  1130. operand->expr = node;
  1131. }
  1132. }
  1133. b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  1134. GB_ASSERT(call->kind == AstNode_CallExpr);
  1135. ast_node(ce, CallExpr, call);
  1136. BuiltinProc *bp = &builtin_procs[id];
  1137. {
  1138. char *err = NULL;
  1139. if (ce->arg_list_count < bp->arg_count)
  1140. err = "Too few";
  1141. if (ce->arg_list_count > bp->arg_count && !bp->variadic)
  1142. err = "Too many";
  1143. if (err) {
  1144. ast_node(proc, Ident, ce->proc);
  1145. error(&c->error_collector, ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
  1146. err, LIT(proc->token.string),
  1147. bp->arg_count, ce->arg_list_count);
  1148. return false;
  1149. }
  1150. }
  1151. switch (id) {
  1152. case BuiltinProc_size_of:
  1153. case BuiltinProc_align_of:
  1154. case BuiltinProc_offset_of:
  1155. // NOTE(bill): The first arg is a Type, this will be checked case by case
  1156. break;
  1157. default:
  1158. check_multi_expr(c, operand, ce->arg_list);
  1159. }
  1160. switch (id) {
  1161. case BuiltinProc_size_of: {
  1162. // size_of :: proc(Type)
  1163. Type *type = check_type(c, ce->arg_list);
  1164. if (!type) {
  1165. error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `size_of`");
  1166. return false;
  1167. }
  1168. operand->mode = Addressing_Constant;
  1169. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, type));
  1170. operand->type = t_int;
  1171. } break;
  1172. case BuiltinProc_size_of_val:
  1173. // size_of_val :: proc(val)
  1174. check_assignment(c, operand, NULL, make_string("argument of `size_of`"));
  1175. if (operand->mode == Addressing_Invalid)
  1176. return false;
  1177. operand->mode = Addressing_Constant;
  1178. operand->value = make_exact_value_integer(type_size_of(c->sizes, c->allocator, operand->type));
  1179. operand->type = t_int;
  1180. break;
  1181. case BuiltinProc_align_of: {
  1182. // align_of :: proc(Type)
  1183. Type *type = check_type(c, ce->arg_list);
  1184. if (!type) {
  1185. error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a type for `align_of`");
  1186. return false;
  1187. }
  1188. operand->mode = Addressing_Constant;
  1189. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, type));
  1190. operand->type = t_int;
  1191. } break;
  1192. case BuiltinProc_align_of_val:
  1193. // align_of_val :: proc(val)
  1194. check_assignment(c, operand, NULL, make_string("argument of `align_of`"));
  1195. if (operand->mode == Addressing_Invalid)
  1196. return false;
  1197. operand->mode = Addressing_Constant;
  1198. operand->value = make_exact_value_integer(type_align_of(c->sizes, c->allocator, operand->type));
  1199. operand->type = t_int;
  1200. break;
  1201. case BuiltinProc_offset_of: {
  1202. // offset_val :: proc(Type, field)
  1203. Type *type = get_base_type(check_type(c, ce->arg_list));
  1204. AstNode *field_arg = unparen_expr(ce->arg_list->next);
  1205. if (type) {
  1206. if (type->kind != Type_Structure) {
  1207. error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a structure type for `offset_of`");
  1208. return false;
  1209. }
  1210. if (field_arg == NULL ||
  1211. field_arg->kind != AstNode_Ident) {
  1212. error(&c->error_collector, ast_node_token(field_arg), "Expected an identifier for field argument");
  1213. return false;
  1214. }
  1215. }
  1216. isize index = 0;
  1217. Entity *entity = lookup_field(type, field_arg, &index);
  1218. if (entity == NULL) {
  1219. ast_node(arg, Ident, field_arg);
  1220. gbString type_str = type_to_string(type);
  1221. error(&c->error_collector, ast_node_token(ce->arg_list),
  1222. "`%s` has no field named `%.*s`", type_str, LIT(arg->token.string));
  1223. return false;
  1224. }
  1225. operand->mode = Addressing_Constant;
  1226. operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, index));
  1227. operand->type = t_int;
  1228. } break;
  1229. case BuiltinProc_offset_of_val: {
  1230. // offset_val :: proc(val)
  1231. AstNode *arg = unparen_expr(ce->arg_list);
  1232. if (arg->kind != AstNode_SelectorExpr) {
  1233. gbString str = expr_to_string(arg);
  1234. error(&c->error_collector, ast_node_token(arg), "`%s` is not a selector expression", str);
  1235. return false;
  1236. }
  1237. ast_node(s, SelectorExpr, arg);
  1238. check_expr(c, operand, s->expr);
  1239. if (operand->mode == Addressing_Invalid)
  1240. return false;
  1241. Type *type = operand->type;
  1242. if (get_base_type(type)->kind == Type_Pointer) {
  1243. Type *p = get_base_type(type);
  1244. if (get_base_type(p)->kind == Type_Structure)
  1245. type = p->pointer.elem;
  1246. }
  1247. isize index = 0;
  1248. Entity *entity = lookup_field(type, s->selector, &index);
  1249. if (entity == NULL) {
  1250. ast_node(i, Ident, s->selector);
  1251. gbString type_str = type_to_string(type);
  1252. error(&c->error_collector, ast_node_token(arg),
  1253. "`%s` has no field named `%.*s`", type_str, LIT(i->token.string));
  1254. return false;
  1255. }
  1256. operand->mode = Addressing_Constant;
  1257. operand->value = make_exact_value_integer(type_offset_of(c->sizes, c->allocator, type, index));
  1258. operand->type = t_int;
  1259. } break;
  1260. case BuiltinProc_static_assert:
  1261. // static_assert :: proc(cond: bool)
  1262. // TODO(bill): Should `static_assert` and `assert` be unified?
  1263. if (operand->mode != Addressing_Constant ||
  1264. !is_type_boolean(operand->type)) {
  1265. gbString str = expr_to_string(ce->arg_list);
  1266. defer (gb_string_free(str));
  1267. error(&c->error_collector, ast_node_token(call),
  1268. "`%s` is not a constant boolean", str);
  1269. return false;
  1270. }
  1271. if (!operand->value.value_bool) {
  1272. gbString str = expr_to_string(ce->arg_list);
  1273. defer (gb_string_free(str));
  1274. error(&c->error_collector, ast_node_token(call),
  1275. "Static assertion: `%s`", str);
  1276. return true;
  1277. }
  1278. break;
  1279. // TODO(bill): Should these be procedures and are their names appropriate?
  1280. case BuiltinProc_len:
  1281. case BuiltinProc_cap: {
  1282. Type *t = get_base_type(operand->type);
  1283. AddressingMode mode = Addressing_Invalid;
  1284. ExactValue value = {};
  1285. switch (t->kind) {
  1286. case Type_Basic:
  1287. if (id == BuiltinProc_len) {
  1288. if (is_type_string(t)) {
  1289. if (operand->mode == Addressing_Constant) {
  1290. mode = Addressing_Constant;
  1291. value = make_exact_value_integer(operand->value.value_string);
  1292. } else {
  1293. mode = Addressing_Value;
  1294. }
  1295. }
  1296. }
  1297. break;
  1298. case Type_Array:
  1299. mode = Addressing_Constant;
  1300. value = make_exact_value_integer(t->array.count);
  1301. break;
  1302. case Type_Vector:
  1303. mode = Addressing_Constant;
  1304. value = make_exact_value_integer(t->vector.count);
  1305. break;
  1306. case Type_Slice:
  1307. mode = Addressing_Value;
  1308. break;
  1309. }
  1310. if (mode == Addressing_Invalid) {
  1311. gbString str = expr_to_string(operand->expr);
  1312. error(&c->error_collector, ast_node_token(operand->expr),
  1313. "Invalid expression `%s` for `%.*s`",
  1314. str, LIT(bp->name));
  1315. gb_string_free(str);
  1316. return false;
  1317. }
  1318. operand->mode = mode;
  1319. operand->type = t_int;
  1320. operand->value = value;
  1321. } break;
  1322. case BuiltinProc_copy: {
  1323. // copy :: proc(x, y: []Type) -> int
  1324. Type *dest_type = NULL, *src_type = NULL;
  1325. Type *d = get_base_type(operand->type);
  1326. if (d->kind == Type_Slice)
  1327. dest_type = d->slice.elem;
  1328. Operand op = {};
  1329. check_expr(c, &op, ce->arg_list->next);
  1330. if (op.mode == Addressing_Invalid)
  1331. return false;
  1332. Type *s = get_base_type(op.type);
  1333. if (s->kind == Type_Slice)
  1334. src_type = s->slice.elem;
  1335. if (dest_type == NULL || src_type == NULL) {
  1336. error(&c->error_collector, ast_node_token(call), "`copy` only expects slices as arguments");
  1337. return false;
  1338. }
  1339. if (!are_types_identical(dest_type, src_type)) {
  1340. gbString d_arg = expr_to_string(ce->arg_list);
  1341. gbString s_arg = expr_to_string(ce->arg_list->next);
  1342. gbString d_str = type_to_string(dest_type);
  1343. gbString s_str = type_to_string(src_type);
  1344. defer (gb_string_free(d_arg));
  1345. defer (gb_string_free(s_arg));
  1346. defer (gb_string_free(d_str));
  1347. defer (gb_string_free(s_str));
  1348. error(&c->error_collector, ast_node_token(call),
  1349. "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
  1350. d_arg, s_arg, d_str, s_str);
  1351. return false;
  1352. }
  1353. operand->type = t_int; // Returns number of elems copied
  1354. operand->mode = Addressing_Value;
  1355. } break;
  1356. case BuiltinProc_append: {
  1357. // append :: proc(x : ^[]Type, y : Type) -> bool
  1358. Type *x_type = NULL, *y_type = NULL;
  1359. x_type = get_base_type(operand->type);
  1360. Operand op = {};
  1361. check_expr(c, &op, ce->arg_list->next);
  1362. if (op.mode == Addressing_Invalid)
  1363. return false;
  1364. y_type = get_base_type(op.type);
  1365. if (!(is_type_pointer(x_type) && is_type_slice(x_type->pointer.elem))) {
  1366. error(&c->error_collector, ast_node_token(call), "First argument to `append` must be a pointer to a slice");
  1367. return false;
  1368. }
  1369. Type *elem_type = x_type->pointer.elem->slice.elem;
  1370. if (!check_is_assignable_to(c, &op, elem_type)) {
  1371. gbString d_arg = expr_to_string(ce->arg_list);
  1372. gbString s_arg = expr_to_string(ce->arg_list->next);
  1373. gbString d_str = type_to_string(elem_type);
  1374. gbString s_str = type_to_string(y_type);
  1375. defer (gb_string_free(d_arg));
  1376. defer (gb_string_free(s_arg));
  1377. defer (gb_string_free(d_str));
  1378. defer (gb_string_free(s_str));
  1379. error(&c->error_collector, ast_node_token(call),
  1380. "Arguments to `append`, %s, %s, have different element types: %s vs %s",
  1381. d_arg, s_arg, d_str, s_str);
  1382. return false;
  1383. }
  1384. operand->type = t_bool; // Returns if it was successful
  1385. operand->mode = Addressing_Value;
  1386. } break;
  1387. case BuiltinProc_print:
  1388. case BuiltinProc_println: {
  1389. for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
  1390. // TOOD(bill): `check_assignment` doesn't allow tuples at the moment, should it?
  1391. // Or should we destruct the tuple and use each elem?
  1392. check_assignment(c, operand, NULL, make_string("argument"));
  1393. if (operand->mode == Addressing_Invalid)
  1394. return false;
  1395. }
  1396. } break;
  1397. }
  1398. return true;
  1399. }
  1400. void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  1401. GB_ASSERT(call->kind == AstNode_CallExpr);
  1402. GB_ASSERT(proc_type->kind == Type_Proc);
  1403. ast_node(ce, CallExpr, call);
  1404. isize error_code = 0;
  1405. isize param_index = 0;
  1406. isize param_count = 0;
  1407. if (proc_type->proc.params)
  1408. param_count = proc_type->proc.params->tuple.variable_count;
  1409. if (ce->arg_list_count == 0 && param_count == 0)
  1410. return;
  1411. if (ce->arg_list_count > param_count) {
  1412. error_code = +1;
  1413. } else {
  1414. Entity **sig_params = proc_type->proc.params->tuple.variables;
  1415. AstNode *call_arg = ce->arg_list;
  1416. for (; call_arg != NULL; call_arg = call_arg->next) {
  1417. check_multi_expr(c, operand, call_arg);
  1418. if (operand->mode == Addressing_Invalid)
  1419. continue;
  1420. if (operand->type->kind != Type_Tuple) {
  1421. check_not_tuple(c, operand);
  1422. check_assignment(c, operand, sig_params[param_index]->type, make_string("argument"));
  1423. param_index++;
  1424. } else {
  1425. auto *tuple = &operand->type->tuple;
  1426. isize i = 0;
  1427. for (;
  1428. i < tuple->variable_count && param_index < param_count;
  1429. i++, param_index++) {
  1430. Entity *e = tuple->variables[i];
  1431. operand->type = e->type;
  1432. operand->mode = Addressing_Value;
  1433. check_not_tuple(c, operand);
  1434. check_assignment(c, operand, sig_params[param_index]->type, make_string("argument"));
  1435. }
  1436. if (i < tuple->variable_count && param_index == param_count) {
  1437. error_code = +1;
  1438. break;
  1439. }
  1440. }
  1441. if (param_index >= param_count)
  1442. break;
  1443. }
  1444. if (param_index < param_count) {
  1445. error_code = -1;
  1446. } else if (call_arg != NULL && call_arg->next != NULL) {
  1447. error_code = +1;
  1448. }
  1449. }
  1450. if (error_code != 0) {
  1451. char *err_fmt = "";
  1452. if (error_code < 0) {
  1453. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  1454. } else {
  1455. err_fmt = "Too many arguments for `%s`, expected %td arguments";
  1456. }
  1457. gbString proc_str = expr_to_string(ce->proc);
  1458. error(&c->error_collector, ast_node_token(call), err_fmt, proc_str, param_count);
  1459. gb_string_free(proc_str);
  1460. operand->mode = Addressing_Invalid;
  1461. }
  1462. }
  1463. ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  1464. GB_ASSERT(call->kind == AstNode_CallExpr);
  1465. ast_node(ce, CallExpr, call);
  1466. check_expr_or_type(c, operand, ce->proc);
  1467. if (operand->mode == Addressing_Invalid) {
  1468. for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next)
  1469. check_expr_base(c, operand, arg);
  1470. operand->mode = Addressing_Invalid;
  1471. operand->expr = call;
  1472. return Expression_Statement;
  1473. }
  1474. if (operand->mode == Addressing_Builtin) {
  1475. i32 id = operand->builtin_id;
  1476. if (!check_builtin_procedure(c, operand, call, id))
  1477. operand->mode = Addressing_Invalid;
  1478. operand->expr = call;
  1479. return builtin_procs[id].kind;
  1480. }
  1481. Type *proc_type = get_base_type(operand->type);
  1482. if (proc_type == NULL || proc_type->kind != Type_Proc) {
  1483. AstNode *e = operand->expr;
  1484. gbString str = expr_to_string(e);
  1485. defer (gb_string_free(str));
  1486. error(&c->error_collector, ast_node_token(e), "Cannot call a non-procedure: `%s`", str);
  1487. operand->mode = Addressing_Invalid;
  1488. operand->expr = call;
  1489. return Expression_Statement;
  1490. }
  1491. if (curr_procedure(c) == NULL) {
  1492. AstNode *e = operand->expr;
  1493. gbString str = expr_to_string(e);
  1494. defer (gb_string_free(str));
  1495. error(&c->error_collector, ast_node_token(e), "Can ony call procedure within a procedure: `%s`", str);
  1496. operand->mode = Addressing_Invalid;
  1497. operand->expr = call;
  1498. return Expression_Statement;
  1499. }
  1500. check_call_arguments(c, operand, proc_type, call);
  1501. auto *proc = &proc_type->proc;
  1502. if (proc->result_count == 0) {
  1503. operand->mode = Addressing_NoValue;
  1504. } else if (proc->result_count == 1) {
  1505. operand->mode = Addressing_Value;
  1506. operand->type = proc->results->tuple.variables[0]->type;
  1507. } else {
  1508. operand->mode = Addressing_Value;
  1509. operand->type = proc->results;
  1510. }
  1511. operand->expr = call;
  1512. return Expression_Statement;
  1513. }
  1514. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  1515. check_expr_base(c, o, e, t);
  1516. check_not_tuple(c, o);
  1517. char *err_str = NULL;
  1518. switch (o->mode) {
  1519. case Addressing_NoValue:
  1520. err_str = "used as a value";
  1521. break;
  1522. case Addressing_Type:
  1523. err_str = "is not an expression";
  1524. break;
  1525. case Addressing_Builtin:
  1526. err_str = "must be called";
  1527. break;
  1528. }
  1529. if (err_str != NULL) {
  1530. gbString str = expr_to_string(e);
  1531. defer (gb_string_free(str));
  1532. error(&c->error_collector, ast_node_token(e), "`%s` %s", str, err_str);
  1533. o->mode = Addressing_Invalid;
  1534. }
  1535. }
  1536. ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  1537. ExpressionKind kind = Expression_Statement;
  1538. o->mode = Addressing_Invalid;
  1539. o->type = t_invalid;
  1540. switch (node->kind) {
  1541. case_ast_node(be, BadExpr, node)
  1542. goto error;
  1543. case_end;
  1544. case_ast_node(i, Ident, node);
  1545. check_identifier(c, o, node, type_hint);
  1546. case_end;
  1547. case_ast_node(bl, BasicLit, node);
  1548. Type *t = t_invalid;
  1549. switch (bl->kind) {
  1550. case Token_Integer: t = t_untyped_integer; break;
  1551. case Token_Float: t = t_untyped_float; break;
  1552. case Token_String: t = t_untyped_string; break;
  1553. case Token_Rune: t = t_untyped_rune; break;
  1554. default: GB_PANIC("Unknown literal"); break;
  1555. }
  1556. o->mode = Addressing_Constant;
  1557. o->type = t;
  1558. o->value = make_exact_value_from_basic_literal(*bl);
  1559. case_end;
  1560. case_ast_node(pl, ProcLit, node);
  1561. Type *proc_type = check_type(c, pl->type);
  1562. if (proc_type != NULL) {
  1563. auto context = c->context;
  1564. c->context.scope = c->global_scope;
  1565. check_open_scope(c, pl->type);
  1566. c->context.decl = make_declaration_info(c->allocator, c->context.scope);
  1567. defer ({
  1568. c->context = context;
  1569. check_close_scope(c);
  1570. });
  1571. check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body);
  1572. o->mode = Addressing_Value;
  1573. o->type = proc_type;
  1574. } else {
  1575. gbString str = expr_to_string(node);
  1576. error(&c->error_collector, ast_node_token(node), "Invalid procedure literal `%s`", str);
  1577. gb_string_free(str);
  1578. goto error;
  1579. }
  1580. case_end;
  1581. case_ast_node(cl, CompoundLit, node);
  1582. Type *type = type_hint;
  1583. b32 ellipsis_array = false;
  1584. if (cl->type != NULL) {
  1585. type = NULL;
  1586. // [..]Type
  1587. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
  1588. if (cl->type->ArrayType.count->kind == AstNode_Ellipsis) {
  1589. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  1590. ellipsis_array = true;
  1591. }
  1592. }
  1593. if (type == NULL) {
  1594. type = check_type(c, cl->type);
  1595. }
  1596. }
  1597. if (type == NULL) {
  1598. error(&c->error_collector, ast_node_token(node), "Missing type in compound literal");
  1599. goto error;
  1600. }
  1601. Type *t = get_base_type(type);
  1602. switch (t->kind) {
  1603. case Type_Structure: {
  1604. if (cl->elem_count == 0)
  1605. break; // NOTE(bill): No need to init
  1606. { // Checker values
  1607. AstNode *elem = cl->elem_list;
  1608. isize field_count = t->structure.field_count;
  1609. isize index = 0;
  1610. for (;
  1611. elem != NULL;
  1612. elem = elem->next, index++) {
  1613. Entity *field = t->structure.fields[index];
  1614. check_expr(c, o, elem);
  1615. if (index >= field_count) {
  1616. error(&c->error_collector, ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count);
  1617. break;
  1618. }
  1619. check_assignment(c, o, field->type, make_string("structure literal"));
  1620. }
  1621. if (cl->elem_count < field_count) {
  1622. error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count);
  1623. }
  1624. }
  1625. } break;
  1626. case Type_Slice:
  1627. case Type_Array:
  1628. case Type_Vector:
  1629. {
  1630. Type *elem_type = NULL;
  1631. String context_name = {};
  1632. if (t->kind == Type_Slice) {
  1633. elem_type = t->slice.elem;
  1634. context_name = make_string("slice literal");
  1635. } else if (t->kind == Type_Vector) {
  1636. elem_type = t->vector.elem;
  1637. context_name = make_string("vector literal");
  1638. } else {
  1639. elem_type = t->array.elem;
  1640. context_name = make_string("array literal");
  1641. }
  1642. i64 index = 0;
  1643. i64 max = 0;
  1644. for (AstNode *elem = cl->elem_list; elem != NULL; elem = elem->next, index++) {
  1645. AstNode *e = elem;
  1646. if (t->kind == Type_Array &&
  1647. t->array.count >= 0 &&
  1648. index >= t->array.count) {
  1649. error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld)", index, t->array.count);
  1650. }
  1651. Operand o = {};
  1652. check_expr_with_type_hint(c, &o, e, elem_type);
  1653. check_assignment(c, &o, elem_type, context_name);
  1654. }
  1655. if (max < index)
  1656. max = index;
  1657. if (t->kind == Type_Array && ellipsis_array) {
  1658. t->array.count = max;
  1659. }
  1660. } break;
  1661. default: {
  1662. gbString str = type_to_string(t);
  1663. error(&c->error_collector, ast_node_token(node), "Invalid compound literal type `%s`", str);
  1664. gb_string_free(str);
  1665. goto error;
  1666. } break;
  1667. }
  1668. o->mode = Addressing_Value;
  1669. o->type = type;
  1670. case_end;
  1671. case_ast_node(pe, ParenExpr, node);
  1672. kind = check_expr_base(c, o, pe->expr, type_hint);
  1673. o->expr = node;
  1674. case_end;
  1675. case_ast_node(te, TagExpr, node);
  1676. // TODO(bill): Tag expressions
  1677. error(&c->error_collector, ast_node_token(node), "Tag expressions are not supported yet");
  1678. kind = check_expr_base(c, o, te->expr, type_hint);
  1679. o->expr = node;
  1680. case_end;
  1681. case_ast_node(ue, UnaryExpr, node);
  1682. check_expr(c, o, ue->expr);
  1683. if (o->mode == Addressing_Invalid)
  1684. goto error;
  1685. check_unary_expr(c, o, ue->op, node);
  1686. if (o->mode == Addressing_Invalid)
  1687. goto error;
  1688. case_end;
  1689. case_ast_node(be, BinaryExpr, node);
  1690. check_binary_expr(c, o, node);
  1691. if (o->mode == Addressing_Invalid)
  1692. goto error;
  1693. case_end;
  1694. case_ast_node(se, SelectorExpr, node);
  1695. check_expr_base(c, o, se->expr);
  1696. check_selector(c, o, node);
  1697. case_end;
  1698. case_ast_node(ie, IndexExpr, node);
  1699. check_expr(c, o, ie->expr);
  1700. if (o->mode == Addressing_Invalid)
  1701. goto error;
  1702. b32 valid = false;
  1703. i64 max_count = -1;
  1704. Type *t = get_base_type(o->type);
  1705. switch (t->kind) {
  1706. case Type_Basic:
  1707. if (is_type_string(t)) {
  1708. valid = true;
  1709. if (o->mode == Addressing_Constant) {
  1710. max_count = o->value.value_string.len;
  1711. }
  1712. if (o->mode != Addressing_Variable)
  1713. o->mode = Addressing_Value;
  1714. o->type = t_u8;
  1715. }
  1716. break;
  1717. case Type_Array:
  1718. valid = true;
  1719. max_count = t->array.count;
  1720. if (o->mode != Addressing_Variable)
  1721. o->mode = Addressing_Value;
  1722. o->type = t->array.elem;
  1723. break;
  1724. case Type_Vector:
  1725. valid = true;
  1726. max_count = t->vector.count;
  1727. if (o->mode != Addressing_Variable)
  1728. o->mode = Addressing_Value;
  1729. o->type = t->vector.elem;
  1730. break;
  1731. case Type_Slice:
  1732. valid = true;
  1733. o->type = t->slice.elem;
  1734. o->mode = Addressing_Variable;
  1735. break;
  1736. case Type_Pointer:
  1737. valid = true;
  1738. o->mode = Addressing_Variable;
  1739. o->type = get_base_type(t->pointer.elem);
  1740. break;
  1741. }
  1742. if (!valid) {
  1743. gbString str = expr_to_string(o->expr);
  1744. error(&c->error_collector, ast_node_token(o->expr), "Cannot index `%s`", str);
  1745. gb_string_free(str);
  1746. goto error;
  1747. }
  1748. if (ie->index == NULL) {
  1749. gbString str = expr_to_string(o->expr);
  1750. error(&c->error_collector, ast_node_token(o->expr), "Missing index for `%s`", str);
  1751. gb_string_free(str);
  1752. goto error;
  1753. }
  1754. check_index_value(c, ie->index, max_count, NULL);
  1755. case_end;
  1756. case_ast_node(se, SliceExpr, node);
  1757. check_expr(c, o, se->expr);
  1758. if (o->mode == Addressing_Invalid)
  1759. goto error;
  1760. b32 valid = false;
  1761. i64 max_count = -1;
  1762. Type *t = get_base_type(o->type);
  1763. switch (t->kind) {
  1764. case Type_Basic:
  1765. if (is_type_string(t)) {
  1766. valid = true;
  1767. if (o->mode == Addressing_Constant) {
  1768. max_count = o->value.value_string.len;
  1769. }
  1770. o->type = t_string;
  1771. o->mode = Addressing_Value;
  1772. }
  1773. break;
  1774. case Type_Array:
  1775. valid = true;
  1776. max_count = t->array.count;
  1777. if (o->mode != Addressing_Variable) {
  1778. gbString str = expr_to_string(node);
  1779. error(&c->error_collector, ast_node_token(node), "Cannot slice array `%s`, value is not addressable", str);
  1780. gb_string_free(str);
  1781. goto error;
  1782. }
  1783. o->type = make_type_slice(c->allocator, t->array.elem);
  1784. o->mode = Addressing_Value;
  1785. break;
  1786. case Type_Slice:
  1787. valid = true;
  1788. o->mode = Addressing_Value;
  1789. break;
  1790. case Type_Pointer:
  1791. valid = true;
  1792. o->type = make_type_slice(c->allocator, get_base_type(t->pointer.elem));
  1793. o->mode = Addressing_Value;
  1794. break;
  1795. }
  1796. if (!valid) {
  1797. gbString str = expr_to_string(o->expr);
  1798. error(&c->error_collector, ast_node_token(o->expr), "Cannot slice `%s`", str);
  1799. gb_string_free(str);
  1800. goto error;
  1801. }
  1802. i64 indices[3] = {};
  1803. AstNode *nodes[3] = {se->low, se->high, se->max};
  1804. for (isize i = 0; i < gb_count_of(nodes); i++) {
  1805. i64 index = max_count;
  1806. if (nodes[i] != NULL) {
  1807. i64 capacity = -1;
  1808. if (max_count >= 0)
  1809. capacity = max_count;
  1810. i64 j = 0;
  1811. if (check_index_value(c, nodes[i], capacity, &j)) {
  1812. index = j;
  1813. }
  1814. } else if (i == 0) {
  1815. index = 0;
  1816. }
  1817. indices[i] = index;
  1818. }
  1819. for (isize i = 0; i < gb_count_of(indices); i++) {
  1820. i64 a = indices[i];
  1821. for (isize j = i+1; j < gb_count_of(indices); j++) {
  1822. i64 b = indices[j];
  1823. if (a > b && b >= 0) {
  1824. error(&c->error_collector, se->close, "Invalid slice indices: [%td > %td]", a, b);
  1825. }
  1826. }
  1827. }
  1828. case_end;
  1829. case_ast_node(ce, CallExpr, node);
  1830. return check_call_expr(c, o, node);
  1831. case_end;
  1832. case_ast_node(de, DerefExpr, node);
  1833. check_expr_or_type(c, o, de->expr);
  1834. if (o->mode == Addressing_Invalid) {
  1835. goto error;
  1836. } else {
  1837. Type *t = get_base_type(o->type);
  1838. if (t->kind == Type_Pointer) {
  1839. o->mode = Addressing_Variable;
  1840. o->type = t->pointer.elem;
  1841. } else {
  1842. gbString str = expr_to_string(o->expr);
  1843. error(&c->error_collector, ast_node_token(o->expr), "Cannot dereference `%s`", str);
  1844. gb_string_free(str);
  1845. goto error;
  1846. }
  1847. }
  1848. case_end;
  1849. case AstNode_ProcType:
  1850. case AstNode_PointerType:
  1851. case AstNode_ArrayType:
  1852. case AstNode_VectorType:
  1853. case AstNode_StructType:
  1854. o->mode = Addressing_Type;
  1855. o->type = check_type(c, node);
  1856. break;
  1857. }
  1858. kind = Expression_Expression;
  1859. o->expr = node;
  1860. return kind;
  1861. error:
  1862. o->mode = Addressing_Invalid;
  1863. o->expr = node;
  1864. return kind;
  1865. }
  1866. ExpressionKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  1867. ExpressionKind kind = check__expr_base(c, o, node, type_hint);
  1868. Type *type = NULL;
  1869. ExactValue value = {ExactValue_Invalid};
  1870. switch (o->mode) {
  1871. case Addressing_Invalid:
  1872. type = t_invalid;
  1873. break;
  1874. case Addressing_NoValue:
  1875. type = NULL;
  1876. break;
  1877. case Addressing_Constant:
  1878. type = o->type;
  1879. value = o->value;
  1880. break;
  1881. default:
  1882. type = o->type;
  1883. break;
  1884. }
  1885. if (type != NULL && is_type_untyped(type)) {
  1886. add_untyped(&c->info, node, false, o->mode, type, value);
  1887. } else {
  1888. add_type_and_value(&c->info, node, o->mode, type, value);
  1889. }
  1890. return kind;
  1891. }
  1892. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  1893. gbString err_str = NULL;
  1894. defer (gb_string_free(err_str));
  1895. check_expr_base(c, o, e);
  1896. switch (o->mode) {
  1897. default:
  1898. return; // NOTE(bill): Valid
  1899. case Addressing_NoValue:
  1900. err_str = expr_to_string(e);
  1901. error(&c->error_collector, ast_node_token(e), "`%s` used as value", err_str);
  1902. break;
  1903. case Addressing_Type:
  1904. err_str = expr_to_string(e);
  1905. error(&c->error_collector, ast_node_token(e), "`%s` is not an expression", err_str);
  1906. break;
  1907. }
  1908. o->mode = Addressing_Invalid;
  1909. }
  1910. void check_not_tuple(Checker *c, Operand *o) {
  1911. if (o->mode == Addressing_Value) {
  1912. // NOTE(bill): Tuples are not first class thus never named
  1913. if (o->type->kind == Type_Tuple) {
  1914. isize count = o->type->tuple.variable_count;
  1915. GB_ASSERT(count != 1);
  1916. error(&c->error_collector, ast_node_token(o->expr),
  1917. "%td-valued tuple found where single value expected", count);
  1918. o->mode = Addressing_Invalid;
  1919. }
  1920. }
  1921. }
  1922. void check_expr(Checker *c, Operand *o, AstNode *e) {
  1923. check_multi_expr(c, o, e);
  1924. check_not_tuple(c, o);
  1925. }
  1926. void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
  1927. check_expr_base(c, o, e);
  1928. check_not_tuple(c, o);
  1929. if (o->mode == Addressing_NoValue) {
  1930. AstNode *e = o->expr;
  1931. gbString str = expr_to_string(e);
  1932. defer (gb_string_free(str));
  1933. error(&c->error_collector, ast_node_token(e),
  1934. "`%s` used as value or type", str);
  1935. o->mode = Addressing_Invalid;
  1936. }
  1937. }
  1938. gbString write_expr_to_string(gbString str, AstNode *node);
  1939. gbString write_field_list_to_string(gbString str, AstNode *field_list, char *sep) {
  1940. isize i = 0;
  1941. for (AstNode *field = field_list; field != NULL; field = field->next) {
  1942. ast_node(f, Field, field);
  1943. if (i > 0)
  1944. str = gb_string_appendc(str, sep);
  1945. isize j = 0;
  1946. for (AstNode *name = f->name_list; name != NULL; name = name->next) {
  1947. if (j > 0)
  1948. str = gb_string_appendc(str, ", ");
  1949. str = write_expr_to_string(str, name);
  1950. j++;
  1951. }
  1952. str = gb_string_appendc(str, ": ");
  1953. str = write_expr_to_string(str, f->type);
  1954. i++;
  1955. }
  1956. return str;
  1957. }
  1958. gbString string_append_token(gbString str, Token token) {
  1959. if (token.string.len > 0)
  1960. return gb_string_append_length(str, token.string.text, token.string.len);
  1961. return str;
  1962. }
  1963. gbString write_expr_to_string(gbString str, AstNode *node) {
  1964. if (node == NULL)
  1965. return str;
  1966. if (is_ast_node_stmt(node)) {
  1967. GB_ASSERT("stmt passed to write_expr_to_string");
  1968. }
  1969. switch (node->kind) {
  1970. default:
  1971. str = gb_string_appendc(str, "(BadExpr)");
  1972. break;
  1973. case_ast_node(i, Ident, node);
  1974. str = string_append_token(str, i->token);
  1975. case_end;
  1976. case_ast_node(bl, BasicLit, node);
  1977. str = string_append_token(str, *bl);
  1978. case_end;
  1979. case_ast_node(pl, ProcLit, node);
  1980. str = write_expr_to_string(str, pl->type);
  1981. case_end;
  1982. case_ast_node(cl, CompoundLit, node);
  1983. str = gb_string_appendc(str, "(");
  1984. str = write_expr_to_string(str, cl->type);
  1985. str = gb_string_appendc(str, " lit)");
  1986. case_end;
  1987. case_ast_node(te, TagExpr, node);
  1988. str = gb_string_appendc(str, "#");
  1989. str = string_append_token(str, te->name);
  1990. str = write_expr_to_string(str, te->expr);
  1991. case_end;
  1992. case_ast_node(ue, UnaryExpr, node);
  1993. str = string_append_token(str, ue->op);
  1994. str = write_expr_to_string(str, ue->expr);
  1995. case_end;
  1996. case_ast_node(be, BinaryExpr, node);
  1997. str = write_expr_to_string(str, be->left);
  1998. str = gb_string_appendc(str, " ");
  1999. str = string_append_token(str, be->op);
  2000. str = gb_string_appendc(str, " ");
  2001. str = write_expr_to_string(str, be->right);
  2002. case_end;
  2003. case_ast_node(pe, ParenExpr, node);
  2004. str = gb_string_appendc(str, "(");
  2005. str = write_expr_to_string(str, pe->expr);
  2006. str = gb_string_appendc(str, ")");
  2007. case_end;
  2008. case_ast_node(se, SelectorExpr, node);
  2009. str = write_expr_to_string(str, se->expr);
  2010. str = gb_string_appendc(str, ".");
  2011. str = write_expr_to_string(str, se->selector);
  2012. case_end;
  2013. case_ast_node(ie, IndexExpr, node);
  2014. str = write_expr_to_string(str, ie->expr);
  2015. str = gb_string_appendc(str, "[");
  2016. str = write_expr_to_string(str, ie->index);
  2017. str = gb_string_appendc(str, "]");
  2018. case_end;
  2019. case_ast_node(se, SliceExpr, node);
  2020. str = write_expr_to_string(str, se->expr);
  2021. str = gb_string_appendc(str, "[");
  2022. str = write_expr_to_string(str, se->low);
  2023. str = gb_string_appendc(str, ":");
  2024. str = write_expr_to_string(str, se->high);
  2025. if (se->triple_indexed) {
  2026. str = gb_string_appendc(str, ":");
  2027. str = write_expr_to_string(str, se->max);
  2028. }
  2029. str = gb_string_appendc(str, "]");
  2030. case_end;
  2031. case_ast_node(e, Ellipsis, node);
  2032. str = gb_string_appendc(str, "..");
  2033. case_end;
  2034. case_ast_node(pt, PointerType, node);
  2035. str = gb_string_appendc(str, "^");
  2036. str = write_expr_to_string(str, pt->type);
  2037. case_end;
  2038. case_ast_node(at, ArrayType, node);
  2039. str = gb_string_appendc(str, "[");
  2040. str = write_expr_to_string(str, at->count);
  2041. str = gb_string_appendc(str, "]");
  2042. str = write_expr_to_string(str, at->elem);
  2043. case_end;
  2044. case_ast_node(vt, VectorType, node);
  2045. str = gb_string_appendc(str, "{");
  2046. str = write_expr_to_string(str, vt->count);
  2047. str = gb_string_appendc(str, "}");
  2048. str = write_expr_to_string(str, vt->elem);
  2049. case_end;
  2050. case_ast_node(ce, CallExpr, node);
  2051. str = write_expr_to_string(str, ce->proc);
  2052. str = gb_string_appendc(str, "(");
  2053. isize i = 0;
  2054. for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
  2055. if (i > 0) {
  2056. str = gb_string_appendc(str, ", ");
  2057. }
  2058. str = write_expr_to_string(str, arg);
  2059. i++;
  2060. }
  2061. str = gb_string_appendc(str, ")");
  2062. case_end;
  2063. case_ast_node(pt, ProcType, node);
  2064. str = gb_string_appendc(str, "proc(");
  2065. str = write_field_list_to_string(str, pt->param_list, ", ");
  2066. str = gb_string_appendc(str, ")");
  2067. case_end;
  2068. case_ast_node(st, StructType, node);
  2069. str = gb_string_appendc(str, "struct{");
  2070. str = write_field_list_to_string(str, st->field_list, ", ");
  2071. str = gb_string_appendc(str, "}");
  2072. case_end;
  2073. }
  2074. return str;
  2075. }
  2076. gbString expr_to_string(AstNode *expression) {
  2077. return write_expr_to_string(gb_string_make(gb_heap_allocator(), ""), expression);
  2078. }