aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/env.c
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-07 17:57:22 +0200
committerlemon <lsof@mailbox.org>2022-08-07 17:57:22 +0200
commit9460f9f14dd68eb59d36f758272be936300a0440 (patch)
treefbc50be52e28c1ad3cc05da58f9d5d4c315d4711 /bootstrap/env.c
parent855834ed40250f35fe5de5e304173ec69059e2fc (diff)
fix template accessing future environment
Diffstat (limited to 'bootstrap/env.c')
-rw-r--r--bootstrap/env.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/bootstrap/env.c b/bootstrap/env.c
index ddd06b2..b3652fa 100644
--- a/bootstrap/env.c
+++ b/bootstrap/env.c
@@ -2,14 +2,19 @@
struct decl *
-envfind(const struct env *env, const char *name) {
+envfind(const struct env *env, int age, const char *name) {
+ int eage = INT_MAX;
+
+again:
if (!env)
return NULL;
for (struct decls *decls = env->decls; decls; decls = decls->next) {
- if (!strcmp(decls->decl.name, name))
+ if (eage >= decls->decl.age && !strcmp(decls->decl.name, name))
return &decls->decl;
}
- return envfind(env->parent, name);
+ env = env->parent;
+ eage = env && env->parent ? eage : age;
+ goto again;
}
static bool
@@ -24,7 +29,9 @@ envput(struct env *env, const struct decl *decl) {
.decls = env->decls
};
struct decl *d0;
- if ((d0 = envfind(&env_noparent, decl->name))) {
+ static int age;
+
+ if ((d0 = envfind(&env_noparent, INT_MAX, decl->name))) {
// modify existing forward declarations?
for (int kind = TYstruct; kind <= TYunion; ++kind) {
if (d0->t == Dtype && d0->ty->t == kind
@@ -58,6 +65,7 @@ envput(struct env *env, const struct decl *decl) {
decls = xcalloc(1, sizeof *decls);
decls->next = env->decls;
decls->decl = *decl;
+ decls->decl.age = age++;
env->decls = decls;
return &decls->decl;
}