diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-07-08 23:25:12 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-07-08 23:25:12 +0200 |
commit | af6c653c673103addae060872017574dfd41f5a2 (patch) | |
tree | 8f6bcae3cfa0cda9a975602ff19b078563e5f824 | |
parent | ca42c26f51117ae83fd9c2be3f7d35169b93e9aa (diff) |
Fix issues with shadowing
-rw-r--r-- | src/codegen.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/codegen.c b/src/codegen.c index 14d7c20..5c81721 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -51,7 +51,7 @@ struct cgctx { LLVMValueRef func; idx_t scpi; - strview_t namespace; + strview_t decl, namespace; }; static void codegenast(struct cgctx); @@ -207,6 +207,11 @@ codegentypedexpr(struct cgctx ctx, idx_t i, type_t *T, LLVMValueRef *outv) switch (ctx.ast.kinds[i]) { case ASTIDENT: { strview_t sv = ctx.toks.strs[ctx.ast.lexemes[i]]; + + /* Allow shadowing in decls like ‘x := x + 1’ */ + if (strview_eq(sv, ctx.decl)) + ctx.scpi--; + LLVMTypeRef t = type2llvm(ctx, ctx.types[i]); LLVMValueRef ptrval = symtab_get_from_scopes(ctx, sv)->v; *outv = LLVMBuildLoad2(ctx.bob, t, ptrval, "load"); @@ -437,7 +442,7 @@ idx_t codegendecl(struct cgctx ctx, idx_t i) { pair_t p = ctx.ast.kids[i]; - strview_t sv = ctx.toks.strs[ctx.ast.lexemes[i]]; + strview_t sv = ctx.decl = ctx.toks.strs[ctx.ast.lexemes[i]]; if (ctx.ast.kinds[i] == ASTCDECL) { /* Constants are purely a compiler concept; they aren’t generated |