aboutsummaryrefslogtreecommitdiff
path: root/storage.c
blob: 8ab1ad4c19adbbb94b4d1ca9c4ea340f22066595 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "storage.h"
#include "errloc.h"
#include "fn.h"
#include "lex.h"
#include "prv.h"
#include "type.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

static int id(const struct lex *l, struct prv *p);

static const struct seq seqs[] =
{
    {(struct step[]){{ID}, {0}}, .fn = id},
    {0}
};

static int compat(const struct fn *fn, const struct prv *p,
    const struct tk *name, const struct type *t, const struct type **ot)
{
    const struct stentry *oe;
    const struct fn *top = fn_cur(p);

    /* allow global entries if already found on linkage section
     * and vice versa, as long as the types are compatible. */
    if (p->st == top->gl)
    {
        for (size_t i = 0; i < fn->nimps; i++)
        {
            const struct im *im = &fn->imps[i];

            if (compat(im->ast.fns, p, name, t, ot))
                return 1;
        }

        if (fn->lk && (oe = storage_find(name->s, fn->lk)))
        {
            if (t == oe->t)
                return 1;

            *ot = oe->t;
            return -1;
        }
    }
    else if (p->st == top->lk)
    {
        if (fn->gl && (oe = storage_find(name->s, fn->gl)))
        {
            if (t == oe->t)
                return 1;

            *ot = oe->t;
            return -1;
        }
    }

    return 0;
}

static int stpush(const struct lex *l, struct prv *p, const struct tk *name,
    const struct type *t)
{
    int ret = -1;
    struct stentry *entries;
    struct storage *st = p->st;
    size_t n = st->nentries + 1;
    const struct fn *fn = fn_cur(p);
    const struct loc *loc = &name->loc;
    const struct stentry *e;
    char *tname = type_name(t);

    if (!t->sz && p->st != fn->lk)
    {
        errloc(name + 1, "type \"%s\" has null size", tname);
        goto end;
    }
    else if (st->check && (e = fn_var(fn, name)))
    {
        const struct loc *loc = &e->tk->loc;
        const struct type *ot;
        int ret = compat(fn, p, name, t, &ot);

        if (ret < 0)
        {
            errloc(name, "variable \"%s\" (\"%s\") already defined at "
                "%s:%d:%d with incompatible type (\"%s\")",
                name->s, type_name(t), loc->f, loc->line, loc->col,
                type_name(ot));
            goto end;
        }
        else if (!ret)
        {
            errloc(name, "variable \"%s\" already defined at %s:%d:%d",
                name->s, loc->f, loc->line, loc->col);
            goto end;
        }
    }

    /* find var in currently known symbols */
    for (size_t i = 0; i < st->nentries; i++)
    {
        const struct stentry *e = &st->entries[i];
        const struct tk *tk = e->tk;

        if (!strcmp(name->s, tk->s))
        {
            const struct loc *loc = &tk->loc;

            errloc(tk, "variable \"%s\" already defined at %s:%d:%d",
                tk->s, loc->f, loc->line, loc->col);
            goto end;
        }
    }

    if (pop(l, p))
        goto end;
    else if (!(entries = realloc(st->entries, n * sizeof *entries)))
    {
        perror("realloc(3)");
        goto end;
    }

    if (t->sz)
        st->offset += st->offset % t->sz;

    entries[st->nentries++] = (struct stentry)
    {
        .t = t,
        .tk = name,
        .offset = st->offset
    };

    st->entries = entries;
    st->offset += t->sz;
    p->stk = p->tk;

    if (p->proto)
        fputc('\t', stderr);

    fprintf(stderr, "\tadding %s (%d:%d) to %s, type %s, offset %zu, "
        "size %zu\n", name->s, loc->line, loc->col, st->name, tname,
        entries[st->nentries - 1].offset, t->sz);
    ret = 1;
end:
    free(tname);
    return ret;
}

static int v(const struct lex *l, struct prv *p, const struct type *t)
{
    return stpush(l, p, p->id, t);
}

static int id(const struct lex *l, struct prv *p)
{
    p->id = p->stk;
    return type(l, p, v);
}

void storage_free(struct storage *s)
{
    if (s)
        free(s->entries);

    free(s);
}

const struct stentry *storage_find(const char *name, const struct storage *s)
{
    for (size_t i = 0; i < s->nentries; i++)
    {
        const struct stentry *e = &s->entries[i];

        if (!strcmp(name, e->tk->s))
            return e;
    }

    return NULL;
}

int storage(const struct lex *l, struct prv *p, const char *name, int check,
    struct storage **out)
{
    struct storage *st = NULL;
    struct pos init =
    {
        .seq = seqs,
        .stseq = seqs,
        .step = seqs->steps
    };

    if (*out)
    {
        const struct loc *loc = &(*out)->tk->loc;

        errloc(p->stk, "%s already defined at %s:%d:%d", name, loc->f,
            loc->line, loc->col);
        goto failure;
    }
    else if (!(st = malloc(sizeof *st)))
    {
        perror("malloc(3)");
        goto failure;
    }
    else if (push(&init, p))
        goto failure;

    *st = (struct storage){.name = name, .tk = p->stk, .check = check};
    *out = p->st = st;
    p->stk = p->tk;
    return 1;

failure:
    free(st);
    return -1;
}