checker.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. #include "../exact_value.cpp"
  2. #include "entity.cpp"
  3. #include "type.cpp"
  4. #define ADDRESSING_KINDS \
  5. ADDRESSING_MODE(Invalid), \
  6. ADDRESSING_MODE(NoValue), \
  7. ADDRESSING_MODE(Value), \
  8. ADDRESSING_MODE(Variable), \
  9. ADDRESSING_MODE(Constant), \
  10. ADDRESSING_MODE(Type), \
  11. ADDRESSING_MODE(Builtin), \
  12. ADDRESSING_MODE(Count), \
  13. enum AddressingMode {
  14. #define ADDRESSING_MODE(x) GB_JOIN2(Addressing_, x)
  15. ADDRESSING_KINDS
  16. #undef ADDRESSING_MODE
  17. };
  18. String const addressing_mode_strings[] = {
  19. #define ADDRESSING_MODE(x) {cast(u8 *)#x, gb_size_of(#x)-1}
  20. ADDRESSING_KINDS
  21. #undef ADDRESSING_MODE
  22. };
  23. struct Operand {
  24. AddressingMode mode;
  25. Type *type;
  26. ExactValue value;
  27. AstNode *expr;
  28. BuiltinProcId builtin_id;
  29. };
  30. struct TypeAndValue {
  31. AddressingMode mode;
  32. Type *type;
  33. ExactValue value;
  34. };
  35. struct DeclInfo {
  36. Scope *scope;
  37. Entity **entities;
  38. isize entity_count;
  39. AstNode *type_expr;
  40. AstNode *init_expr;
  41. AstNode *proc_decl; // AstNode_ProcDecl
  42. u32 var_decl_tags;
  43. Map<b32> deps; // Key: Entity *
  44. };
  45. void init_declaration_info(DeclInfo *d, Scope *scope) {
  46. d->scope = scope;
  47. map_init(&d->deps, gb_heap_allocator());
  48. }
  49. DeclInfo *make_declaration_info(gbAllocator a, Scope *scope) {
  50. DeclInfo *d = gb_alloc_item(a, DeclInfo);
  51. init_declaration_info(d, scope);
  52. return d;
  53. }
  54. void destroy_declaration_info(DeclInfo *d) {
  55. map_destroy(&d->deps);
  56. }
  57. b32 has_init(DeclInfo *d) {
  58. if (d->init_expr != NULL)
  59. return true;
  60. if (d->proc_decl != NULL) {
  61. ast_node(pd, ProcDecl, d->proc_decl);
  62. if (pd->body != NULL)
  63. return true;
  64. }
  65. return false;
  66. }
  67. struct ExpressionInfo {
  68. b32 is_lhs; // Debug info
  69. AddressingMode mode;
  70. Type *type; // Type_Basic
  71. ExactValue value;
  72. };
  73. ExpressionInfo make_expression_info(b32 is_lhs, AddressingMode mode, Type *type, ExactValue value) {
  74. ExpressionInfo ei = {};
  75. ei.is_lhs = is_lhs;
  76. ei.mode = mode;
  77. ei.type = type;
  78. ei.value = value;
  79. return ei;
  80. }
  81. struct ProcedureInfo {
  82. AstFile * file;
  83. Token token;
  84. DeclInfo *decl;
  85. Type * type; // Type_Procedure
  86. AstNode * body; // AstNode_BlockStatement
  87. };
  88. struct Scope {
  89. b32 is_proc;
  90. Scope *parent;
  91. Scope *prev, *next;
  92. Scope *first_child, *last_child;
  93. Map<Entity *> elements; // Key: String
  94. };
  95. enum ExprKind {
  96. Expr_Expr,
  97. Expr_Stmt,
  98. };
  99. enum BuiltinProcId {
  100. BuiltinProc_Invalid,
  101. BuiltinProc_new,
  102. BuiltinProc_new_slice,
  103. BuiltinProc_delete,
  104. BuiltinProc_size_of,
  105. BuiltinProc_size_of_val,
  106. BuiltinProc_align_of,
  107. BuiltinProc_align_of_val,
  108. BuiltinProc_offset_of,
  109. BuiltinProc_offset_of_val,
  110. BuiltinProc_static_assert,
  111. BuiltinProc_len,
  112. BuiltinProc_cap,
  113. BuiltinProc_copy,
  114. BuiltinProc_append,
  115. BuiltinProc_swizzle,
  116. BuiltinProc_ptr_offset,
  117. BuiltinProc_ptr_sub,
  118. BuiltinProc_slice_ptr,
  119. BuiltinProc_Count,
  120. };
  121. struct BuiltinProc {
  122. String name;
  123. isize arg_count;
  124. b32 variadic;
  125. ExprKind kind;
  126. };
  127. gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
  128. {STR_LIT(""), 0, false, Expr_Stmt},
  129. {STR_LIT("new"), 1, false, Expr_Expr},
  130. {STR_LIT("new_slice"), 2, true, Expr_Expr},
  131. {STR_LIT("delete"), 1, false, Expr_Stmt},
  132. {STR_LIT("size_of"), 1, false, Expr_Expr},
  133. {STR_LIT("size_of_val"), 1, false, Expr_Expr},
  134. {STR_LIT("align_of"), 1, false, Expr_Expr},
  135. {STR_LIT("align_of_val"), 1, false, Expr_Expr},
  136. {STR_LIT("offset_of"), 2, false, Expr_Expr},
  137. {STR_LIT("offset_of_val"), 1, false, Expr_Expr},
  138. {STR_LIT("static_assert"), 1, false, Expr_Stmt},
  139. {STR_LIT("len"), 1, false, Expr_Expr},
  140. {STR_LIT("cap"), 1, false, Expr_Expr},
  141. {STR_LIT("copy"), 2, false, Expr_Expr},
  142. {STR_LIT("append"), 2, false, Expr_Expr},
  143. {STR_LIT("swizzle"), 1, true, Expr_Expr},
  144. {STR_LIT("ptr_offset"), 2, false, Expr_Expr},
  145. {STR_LIT("ptr_sub"), 2, false, Expr_Expr},
  146. {STR_LIT("slice_ptr"), 2, true, Expr_Expr},
  147. };
  148. struct CheckerContext {
  149. Scope *scope;
  150. DeclInfo *decl;
  151. };
  152. // NOTE(bill): Symbol tables
  153. struct CheckerInfo {
  154. Map<TypeAndValue> types; // Key: AstNode * | Expression -> Type (and value)
  155. Map<Entity *> definitions; // Key: AstNode * | Identifier -> Entity
  156. Map<Entity *> uses; // Key: AstNode * | Identifier -> Entity
  157. Map<Scope *> scopes; // Key: AstNode * | Node -> Scope
  158. Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
  159. Map<DeclInfo *> entities; // Key: Entity *
  160. };
  161. struct Checker {
  162. Parser * parser;
  163. CheckerInfo info;
  164. AstFile * curr_ast_file;
  165. BaseTypeSizes sizes;
  166. Scope * global_scope;
  167. gbArray(ProcedureInfo) procs; // NOTE(bill): Procedures to check
  168. gbArena arena;
  169. gbAllocator allocator;
  170. CheckerContext context;
  171. gbArray(Type *) proc_stack;
  172. b32 in_defer; // TODO(bill): Actually handle correctly
  173. ErrorCollector error_collector;
  174. };
  175. gb_global Scope *universal_scope = NULL;
  176. struct CycleChecker {
  177. gbArray(Entity *) path; // Entity_TypeName
  178. };
  179. CycleChecker *cycle_checker_add(CycleChecker *cc, Entity *e) {
  180. GB_ASSERT(cc != NULL);
  181. if (cc->path == NULL) {
  182. gb_array_init(cc->path, gb_heap_allocator());
  183. }
  184. GB_ASSERT(e != NULL && e->kind == Entity_TypeName);
  185. gb_array_append(cc->path, e);
  186. return cc;
  187. }
  188. Scope *make_scope(Scope *parent, gbAllocator allocator) {
  189. Scope *s = gb_alloc_item(allocator, Scope);
  190. s->parent = parent;
  191. map_init(&s->elements, gb_heap_allocator());
  192. if (parent != NULL && parent != universal_scope) {
  193. DLIST_APPEND(parent->first_child, parent->last_child, s);
  194. }
  195. return s;
  196. }
  197. void destroy_scope(Scope *scope) {
  198. gb_for_array(i, scope->elements.entries) {
  199. Entity *e =scope->elements.entries[i].value;
  200. if (e->kind == Entity_Variable) {
  201. if (!e->Variable.used) {
  202. #if 0
  203. warning(e->token, "Unused variable `%.*s`", LIT(e->token.string));
  204. #endif
  205. }
  206. }
  207. }
  208. for (Scope *child = scope->first_child; child != NULL; child = child->next) {
  209. destroy_scope(child);
  210. }
  211. map_destroy(&scope->elements);
  212. // NOTE(bill): No need to free scope as it "should" be allocated in an arena (except for the global scope)
  213. }
  214. void add_scope(Checker *c, AstNode *node, Scope *scope) {
  215. GB_ASSERT(node != NULL);
  216. GB_ASSERT(scope != NULL);
  217. map_set(&c->info.scopes, hash_pointer(node), scope);
  218. }
  219. void check_open_scope(Checker *c, AstNode *stmt) {
  220. GB_ASSERT(is_ast_node_stmt(stmt) || is_ast_node_type(stmt));
  221. Scope *scope = make_scope(c->context.scope, c->allocator);
  222. add_scope(c, stmt, scope);
  223. if (stmt->kind == AstNode_ProcType) {
  224. scope->is_proc = true;
  225. }
  226. c->context.scope = scope;
  227. }
  228. void check_close_scope(Checker *c) {
  229. c->context.scope = c->context.scope->parent;
  230. }
  231. void scope_lookup_parent_entity(Checker *c, Scope *s, String name, Scope **scope, Entity **entity) {
  232. b32 gone_thru_proc = false;
  233. HashKey key = hash_string(name);
  234. for (; s != NULL; s = s->parent) {
  235. Entity **found = map_get(&s->elements, key);
  236. if (found) {
  237. Entity *e = *found;
  238. if (gone_thru_proc) {
  239. if (e->kind == Entity_Variable && e->scope != c->global_scope) {
  240. continue;
  241. }
  242. }
  243. if (entity) *entity = e;
  244. if (scope) *scope = s;
  245. return;
  246. }
  247. if (s->is_proc) {
  248. gone_thru_proc = true;
  249. }
  250. }
  251. if (entity) *entity = NULL;
  252. if (scope) *scope = NULL;
  253. }
  254. Entity *scope_lookup_entity(Checker *c, Scope *s, String name) {
  255. Entity *entity = NULL;
  256. scope_lookup_parent_entity(c, s, name, NULL, &entity);
  257. return entity;
  258. }
  259. Entity *current_scope_lookup_entity(Scope *s, String name) {
  260. HashKey key = hash_string(name);
  261. Entity **found = map_get(&s->elements, key);
  262. if (found)
  263. return *found;
  264. return NULL;
  265. }
  266. Entity *scope_insert_entity(Scope *s, Entity *entity) {
  267. String name = entity->token.string;
  268. HashKey key = hash_string(name);
  269. Entity **found = map_get(&s->elements, key);
  270. if (found)
  271. return *found;
  272. map_set(&s->elements, key, entity);
  273. if (entity->scope == NULL)
  274. entity->scope = s;
  275. return NULL;
  276. }
  277. void add_dependency(DeclInfo *d, Entity *e) {
  278. map_set(&d->deps, hash_pointer(e), cast(b32)true);
  279. }
  280. void add_declaration_dependency(Checker *c, Entity *e) {
  281. if (c->context.decl) {
  282. auto found = map_get(&c->info.entities, hash_pointer(e));
  283. if (found) {
  284. add_dependency(c->context.decl, e);
  285. }
  286. }
  287. }
  288. void add_global_entity(Entity *entity) {
  289. String name = entity->token.string;
  290. if (gb_memchr(name.text, ' ', name.len)) {
  291. return; // NOTE(bill): `untyped thing`
  292. }
  293. if (scope_insert_entity(universal_scope, entity)) {
  294. GB_PANIC("Compiler error: double declaration");
  295. }
  296. }
  297. void add_global_constant(gbAllocator a, String name, Type *type, ExactValue value) {
  298. Token token = {Token_Identifier};
  299. token.string = name;
  300. Entity *entity = alloc_entity(a, Entity_Constant, NULL, token, type);
  301. entity->Constant.value = value;
  302. add_global_entity(entity);
  303. }
  304. void init_universal_scope(void) {
  305. // NOTE(bill): No need to free these
  306. gbAllocator a = gb_heap_allocator();
  307. universal_scope = make_scope(NULL, a);
  308. // Types
  309. for (isize i = 0; i < gb_count_of(basic_types); i++) {
  310. Token token = {Token_Identifier};
  311. token.string = basic_types[i].Basic.name;
  312. add_global_entity(make_entity_type_name(a, NULL, token, &basic_types[i]));
  313. }
  314. for (isize i = 0; i < gb_count_of(basic_type_aliases); i++) {
  315. Token token = {Token_Identifier};
  316. token.string = basic_type_aliases[i].Basic.name;
  317. add_global_entity(make_entity_type_name(a, NULL, token, &basic_type_aliases[i]));
  318. }
  319. // Constants
  320. add_global_constant(a, make_string("true"), t_untyped_bool, make_exact_value_bool(true));
  321. add_global_constant(a, make_string("false"), t_untyped_bool, make_exact_value_bool(false));
  322. add_global_constant(a, make_string("null"), t_untyped_pointer, make_exact_value_pointer(NULL));
  323. // Builtin Procedures
  324. for (isize i = 0; i < gb_count_of(builtin_procs); i++) {
  325. BuiltinProcId id = cast(BuiltinProcId)i;
  326. Token token = {Token_Identifier};
  327. token.string = builtin_procs[i].name;
  328. Entity *entity = alloc_entity(a, Entity_Builtin, NULL, token, t_invalid);
  329. entity->Builtin.id = id;
  330. add_global_entity(entity);
  331. }
  332. // Custom Runtime Types
  333. {
  334. }
  335. }
  336. void init_checker_info(CheckerInfo *i) {
  337. gbAllocator a = gb_heap_allocator();
  338. map_init(&i->types, a);
  339. map_init(&i->definitions, a);
  340. map_init(&i->uses, a);
  341. map_init(&i->scopes, a);
  342. map_init(&i->entities, a);
  343. map_init(&i->untyped, a);
  344. }
  345. void destroy_checker_info(CheckerInfo *i) {
  346. map_destroy(&i->types);
  347. map_destroy(&i->definitions);
  348. map_destroy(&i->uses);
  349. map_destroy(&i->scopes);
  350. map_destroy(&i->entities);
  351. map_destroy(&i->untyped);
  352. }
  353. void init_checker(Checker *c, Parser *parser) {
  354. gbAllocator a = gb_heap_allocator();
  355. c->parser = parser;
  356. init_checker_info(&c->info);
  357. c->sizes.word_size = 8;
  358. c->sizes.max_align = 8;
  359. gb_array_init(c->proc_stack, a);
  360. gb_array_init(c->procs, a);
  361. // NOTE(bill): Is this big enough or too small?
  362. isize item_size = gb_max(gb_max(gb_size_of(Entity), gb_size_of(Type)), gb_size_of(Scope));
  363. isize total_token_count = 0;
  364. gb_for_array(i, c->parser->files) {
  365. AstFile *f = &c->parser->files[i];
  366. total_token_count += gb_array_count(f->tokens);
  367. }
  368. isize arena_size = 2 * item_size * total_token_count;
  369. gb_arena_init_from_allocator(&c->arena, a, arena_size);
  370. c->allocator = gb_arena_allocator(&c->arena);
  371. c->global_scope = make_scope(universal_scope, c->allocator);
  372. c->context.scope = c->global_scope;
  373. }
  374. void destroy_checker(Checker *c) {
  375. destroy_checker_info(&c->info);
  376. destroy_scope(c->global_scope);
  377. gb_array_free(c->proc_stack);
  378. gb_array_free(c->procs);
  379. gb_arena_free(&c->arena);
  380. }
  381. TypeAndValue *type_and_value_of_expression(CheckerInfo *i, AstNode *expression) {
  382. TypeAndValue *found = map_get(&i->types, hash_pointer(expression));
  383. return found;
  384. }
  385. Entity *entity_of_ident(CheckerInfo *i, AstNode *identifier) {
  386. GB_ASSERT(identifier->kind == AstNode_Ident);
  387. Entity **found = map_get(&i->definitions, hash_pointer(identifier));
  388. if (found)
  389. return *found;
  390. found = map_get(&i->uses, hash_pointer(identifier));
  391. if (found)
  392. return *found;
  393. return NULL;
  394. }
  395. Type *type_of_expr(CheckerInfo *i, AstNode *expression) {
  396. TypeAndValue *found = type_and_value_of_expression(i, expression);
  397. if (found)
  398. return found->type;
  399. if (expression->kind == AstNode_Ident) {
  400. Entity *entity = entity_of_ident(i, expression);
  401. if (entity)
  402. return entity->type;
  403. }
  404. return NULL;
  405. }
  406. void add_untyped(CheckerInfo *i, AstNode *expression, b32 lhs, AddressingMode mode, Type *basic_type, ExactValue value) {
  407. map_set(&i->untyped, hash_pointer(expression), make_expression_info(lhs, mode, basic_type, value));
  408. }
  409. void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
  410. GB_ASSERT(expression != NULL);
  411. if (mode == Addressing_Invalid)
  412. return;
  413. if (mode == Addressing_Constant) {
  414. GB_ASSERT(value.kind != ExactValue_Invalid);
  415. GB_ASSERT_MSG(type != t_invalid || is_type_constant_type(type),
  416. "type: %s", type_to_string(type));
  417. }
  418. TypeAndValue tv = {};
  419. tv.type = type;
  420. tv.value = value;
  421. tv.mode = mode;
  422. map_set(&i->types, hash_pointer(expression), tv);
  423. }
  424. void add_entity_definition(CheckerInfo *i, AstNode *identifier, Entity *entity) {
  425. GB_ASSERT(identifier != NULL);
  426. GB_ASSERT(identifier->kind == AstNode_Ident);
  427. HashKey key = hash_pointer(identifier);
  428. map_set(&i->definitions, key, entity);
  429. }
  430. void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
  431. if (!are_strings_equal(entity->token.string, make_string("_"))) {
  432. Entity *insert_entity = scope_insert_entity(scope, entity);
  433. if (insert_entity) {
  434. Entity *up = insert_entity->using_parent;
  435. if (up != NULL) {
  436. error(&c->error_collector, entity->token,
  437. "Redeclararation of `%.*s` in this scope through `using`\n"
  438. "\tat %.*s(%td:%td)",
  439. LIT(entity->token.string),
  440. LIT(up->token.pos.file), up->token.pos.line, up->token.pos.column);
  441. } else {
  442. error(&c->error_collector, entity->token,
  443. "Redeclararation of `%.*s` in this scope\n"
  444. "\tat %.*s(%td:%td)",
  445. LIT(entity->token.string),
  446. LIT(entity->token.pos.file), entity->token.pos.line, entity->token.pos.column);
  447. }
  448. return;
  449. }
  450. }
  451. if (identifier != NULL)
  452. add_entity_definition(&c->info, identifier, entity);
  453. }
  454. void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
  455. GB_ASSERT(identifier != NULL);
  456. GB_ASSERT(identifier->kind == AstNode_Ident);
  457. HashKey key = hash_pointer(identifier);
  458. map_set(&i->uses, key, entity);
  459. }
  460. void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
  461. GB_ASSERT(are_strings_equal(identifier->Ident.token.string, e->token.string));
  462. add_entity(c, c->global_scope, identifier, e);
  463. map_set(&c->info.entities, hash_pointer(e), d);
  464. }
  465. void check_procedure_later(Checker *c, AstFile *file, Token token, DeclInfo *decl, Type *type, AstNode *body) {
  466. ProcedureInfo info = {};
  467. info.file = file;
  468. info.token = token;
  469. info.decl = decl;
  470. info.type = type;
  471. info.body = body;
  472. gb_array_append(c->procs, info);
  473. }
  474. void push_procedure(Checker *c, Type *type) {
  475. gb_array_append(c->proc_stack, type);
  476. }
  477. void pop_procedure(Checker *c) {
  478. gb_array_pop(c->proc_stack);
  479. }
  480. Type *const curr_procedure(Checker *c) {
  481. isize count = gb_array_count(c->proc_stack);
  482. if (count > 0) {
  483. return c->proc_stack[count-1];
  484. }
  485. return NULL;
  486. }
  487. void add_curr_ast_file(Checker *c, AstFile *file) {
  488. TokenPos zero_pos = {};
  489. c->error_collector.prev = zero_pos;
  490. c->curr_ast_file = file;
  491. }
  492. #include "expr.cpp"
  493. #include "stmt.cpp"
  494. struct CycleCheck {
  495. gbArray(Entity *) path; // HACK(bill): Memory Leak
  496. };
  497. void cycle_check_add(CycleCheck *cc, Entity *entity) {
  498. if (cc == NULL)
  499. return;
  500. if (cc->path == NULL) {
  501. gb_array_init(cc->path, gb_heap_allocator());
  502. }
  503. GB_ASSERT(entity->kind == Entity_TypeName);
  504. gb_array_append(cc->path, entity);
  505. }
  506. void check_type_name_cycles(Checker *c, CycleCheck *cc, Entity *e) {
  507. GB_ASSERT(e->kind == Entity_TypeName);
  508. Type *t = e->type;
  509. // if (t->kind == Type_Named) {
  510. // if (t->Named.type_name == e) {
  511. // gb_printf("Illegal cycle %.*s!!!\n", LIT(e->token.string));
  512. // }
  513. // }
  514. }
  515. void check_parsed_files(Checker *c) {
  516. // Collect Entities
  517. gb_for_array(i, c->parser->files) {
  518. AstFile *f = &c->parser->files[i];
  519. add_curr_ast_file(c, f);
  520. for (AstNode *decl = f->decls; decl != NULL; decl = decl->next) {
  521. if (!is_ast_node_decl(decl))
  522. continue;
  523. switch (decl->kind) {
  524. case_ast_node(bd, BadDecl, decl);
  525. case_end;
  526. case_ast_node(vd, VarDecl, decl);
  527. switch (vd->kind) {
  528. case Declaration_Immutable: {
  529. for (AstNode *name = vd->name_list, *value = vd->value_list;
  530. name != NULL && value != NULL;
  531. name = name->next, value = value->next) {
  532. ast_node(n, Ident, name);
  533. ExactValue v = {ExactValue_Invalid};
  534. Entity *e = make_entity_constant(c->allocator, c->context.scope, n->token, NULL, v);
  535. DeclInfo *di = make_declaration_info(c->allocator, c->global_scope);
  536. di->type_expr = vd->type;
  537. di->init_expr = value;
  538. add_file_entity(c, name, e, di);
  539. }
  540. isize lhs_count = vd->name_count;
  541. isize rhs_count = vd->value_count;
  542. if (rhs_count == 0 && vd->type == NULL) {
  543. error(&c->error_collector, ast_node_token(decl), "Missing type or initial expression");
  544. } else if (lhs_count < rhs_count) {
  545. error(&c->error_collector, ast_node_token(decl), "Extra initial expression");
  546. }
  547. } break;
  548. case Declaration_Mutable: {
  549. isize entity_count = vd->name_count;
  550. isize entity_index = 0;
  551. Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
  552. DeclInfo *di = NULL;
  553. if (vd->value_count > 0) {
  554. di = make_declaration_info(gb_heap_allocator(), c->global_scope);
  555. di->entities = entities;
  556. di->entity_count = entity_count;
  557. di->type_expr = vd->type;
  558. di->init_expr = vd->value_list;
  559. }
  560. AstNode *value = vd->value_list;
  561. for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
  562. ast_node(n, Ident, name);
  563. Entity *e = make_entity_variable(c->allocator, c->global_scope, n->token, NULL);
  564. entities[entity_index++] = e;
  565. DeclInfo *d = di;
  566. if (d == NULL) {
  567. AstNode *init_expr = value;
  568. d = make_declaration_info(gb_heap_allocator(), c->global_scope);
  569. d->type_expr = vd->type;
  570. d->init_expr = init_expr;
  571. d->var_decl_tags = vd->tags;
  572. }
  573. add_file_entity(c, name, e, d);
  574. if (value != NULL)
  575. value = value->next;
  576. }
  577. } break;
  578. }
  579. case_end;
  580. case_ast_node(td, TypeDecl, decl);
  581. ast_node(n, Ident, td->name);
  582. Entity *e = make_entity_type_name(c->allocator, c->global_scope, n->token, NULL);
  583. DeclInfo *d = make_declaration_info(c->allocator, e->scope);
  584. d->type_expr = td->type;
  585. add_file_entity(c, td->name, e, d);
  586. case_end;
  587. case_ast_node(pd, ProcDecl, decl);
  588. ast_node(n, Ident, pd->name);
  589. Token token = n->token;
  590. Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
  591. add_entity(c, c->global_scope, pd->name, e);
  592. DeclInfo *d = make_declaration_info(c->allocator, e->scope);
  593. d->proc_decl = decl;
  594. map_set(&c->info.entities, hash_pointer(e), d);
  595. case_end;
  596. case_ast_node(ld, LoadDecl, decl);
  597. // NOTE(bill): ignore
  598. case_end;
  599. default:
  600. error(&c->error_collector, ast_node_token(decl), "Only declarations are allowed at file scope");
  601. break;
  602. }
  603. }
  604. }
  605. gb_for_array(i, c->info.entities.entries) {
  606. auto *entry = &c->info.entities.entries[i];
  607. Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
  608. DeclInfo *d = entry->value;
  609. check_entity_decl(c, e, d, NULL);
  610. }
  611. // Check procedure bodies
  612. gb_for_array(i, c->procs) {
  613. ProcedureInfo *pi = &c->procs[i];
  614. add_curr_ast_file(c, pi->file);
  615. check_proc_body(c, pi->token, pi->decl, pi->type, pi->body);
  616. }
  617. // Add untyped expression values
  618. gb_for_array(i, c->info.untyped.entries) {
  619. auto *entry = c->info.untyped.entries + i;
  620. HashKey key = entry->key;
  621. AstNode *expr = cast(AstNode *)cast(uintptr)key.key;
  622. ExpressionInfo *info = &entry->value;
  623. if (info != NULL && expr != NULL) {
  624. if (is_type_typed(info->type)) {
  625. GB_PANIC("%s (type %s) is typed!", expr_to_string(expr), info->type);
  626. }
  627. add_type_and_value(&c->info, expr, info->mode, info->type, info->value);
  628. }
  629. }
  630. }