aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/env.c
blob: 21d9580adf904f7c72f5257dc7a0682f0f25adfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "all.h"


struct decl *
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 (eage >= decls->decl.age && !strcmp(decls->decl.name, name))
         return &decls->decl;
   }
   env = env->parent;
   eage = env && env->parent ? eage : age;
   goto again;
}

static bool
declsshadowable(const struct decl *a, const struct decl *b) {
   return a->t == Dlet && b->t == Dlet;
}

struct decl *
envput(struct env *env, const struct decl *decl) {
   struct decls *decls;
   struct env env_noparent = {
     .decls = env->decls
   };
   struct decl *d0;
   static int age;

   if ((d0 = envfind(&env_noparent, INT_MAX, decl->name))) {
      // modify existing forward declarations?
      if (decl->t != d0->t)
         return NULL;

      if (decl == d0 || !memcmp(d0, decl, sizeof *d0))
         return d0;
      if (d0->t == Ddef && decl->t == Ddef && !memcmp(&d0->var, &decl->var, sizeof d0->var))
         return d0;
      if (d0->t == Dmacro && !memcmp(&decl->macro, &d0->macro, sizeof decl->macro))
         return d0;
      if (d0->t == Dtype && d0->ty == decl->ty)
         return d0;
      if (d0->t == Dfn && !memcmp(&decl->fn, &d0->fn, sizeof decl->fn))
         return d0;
      if (d0->t == Dtepl && !memcmp(&decl->tepl, &d0->tepl, sizeof decl->tepl))
         return d0;
      for (int kind = TYstruct; kind <= TYeunion; ++kind) {
         if (d0->t == Dtype && d0->ty->t == kind
          && decl->t == Dtype && decl->ty->t == kind && d0->ty->agg.fwd) {
            *(size_t *)&d0->ty->size = decl->ty->size;
            *(size_t *)&d0->ty->align = decl->ty->align;
            *(bool *)&d0->ty->agg.fwd = decl->ty->agg.fwd;
            memcpy((void *)&d0->ty->agg.flds, &decl->ty->agg.flds,
                  sizeof d0->ty->agg.flds);
            memcpy((void *)&d0->ty->agg.decls, &decl->ty->agg.decls,
                  sizeof d0->ty->agg.decls);
            d0->span = decl->span;
            return d0;
         }
      }
      if (d0->t == Dfn && d0->externp == decl->externp && d0->fn.selfty == decl->fn.selfty && !d0->fn.body && decl->fn.body) {
         *(int *)&decl->fn.id = d0->fn.id;
         goto ok;
      }
      if (d0->t == Dfn && d0->externp == decl->externp && d0->fn.selfty == decl->fn.selfty && !d0->fn.body)
         return d0;
      if (d0->t == Dstatic && d0->externp == decl->externp && d0->var.ty == decl->var.ty && !d0->var.ini)
         return d0;
      if (d0->t == Dtype && d0->ty == decl->ty)
         return d0;
      if (!declsshadowable(d0, decl)) {
         return NULL;
      }
   }
ok:

   decls = xcalloc(1, sizeof *decls);
   decls->next = env->decls;
   decls->decl = *decl;
   decls->decl.age = age++;
   env->decls = decls;
   return &decls->decl;
}