aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/ast.c
blob: 943463fa1c4d08fc6b62157ad771b28cb763a4df (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

/*
 * Functions for constructing/accessing the Abstract Syntax Tree.
 *
 * Copyright © 2025-2026 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include <assert.h>
#include <string.h>
#include "compiler.h"


struct TreeNode *types = NULL;
struct TreeNode *toplevel_funcs = NULL;
/*struct TreeNode *toplevel_consts = NULL;*/
struct Type *types_list = NULL; /* including exported ones */
struct Func *toplevel_funcs_list = NULL;

struct Type *unbound_types_list = NULL;
struct Func *unbound_funcs_list = NULL;

struct Type *current_type = NULL;
struct Func *current_func = NULL;
static struct Type **type_nextptr, **inner_type_nextptr;
static struct Func **func_nextptr;
static struct Func **type_func_nextptr, **innertype_func_nextptr;
static struct Func **type_ctor_nextptr, **innertype_ctor_nextptr;
/* TODO should constants in "utility files" be allowed? */
/*static struct Var **var_nextptr;*/
static struct Var **type_var_nextptr, **innertype_var_nextptr;

bool is_library = false;
bool interfaces_done = false;
bool in_exported_interface = false;
unsigned current_module_id = 0;


void ast_init(void)
{
    type_nextptr = &types_list;
    func_nextptr = &toplevel_funcs_list;
    /*var_nextptr = &toplevel_consts_list;*/
}

static void register_type(struct Type *t)
{
    assert(t->next == NULL);
    assert(t->prev == NULL);
    if (current_type) {
        *inner_type_nextptr = t;
        inner_type_nextptr = &t->next;
    } else {
        *type_nextptr = t;
        type_nextptr = &t->next;
    }
}

static void check_visibility(const struct Type *type)
{
    if (!interfaces_done && type->module_id != current_module_id) {
        error_ident("Type has not been imported with `usetype` into the "
                    "interface", &type->ident);
    }
}

static void set_type_defaults(struct Type *t)
{
    t->module_id = current_module_id;
    /*t->outer = current_type;*/
    t->outer = NULL; /* TODO nested types */
    t->funcs = NULL;
    t->funcs_list = NULL;
    t->funcs_exported = NULL;
    t->ctors = NULL;
    t->ctors_list = NULL;
    t->ctors_exported = NULL;
    t->ctor_default = NULL;
    t->inner_types = NULL;
    t->inner_types_list = NULL;
    t->inner_types_exported = NULL;
    t->vars = NULL;
    t->vars_list = NULL;
    t->params_exported = NULL;
    t->svcspecs = NULL;
    t->svcspecs_list = NULL;
    t->is_export = false;
    t->is_implemented = false;
    t->next = NULL;
    t->prev = NULL;
    t->sincever_next = NULL;
}

static struct Type *map_named_type(const char *name, size_t len)
{
    struct TreeNode **root;
    struct Type *t;

    if (current_type &&
            len == current_type->ident.node.length &&
            !memcmp(name, current_type->ident.node.name, len)) {
        assert(current_type->ident.node.is_defined);
        return current_type;
    }

    /* If inside an type definition, then the sought type might be an
       inner/nested type defined later within the type being defined.
       So in that case we ONLY search among the inner types at this point,
       and only at the end we can search among the top-level types. */
/*    root = (current_type ?
        &current_type->inner_types :
        &module->types);*/
    /* TODO hack until nested types work. consider distinguishing
            nested vs top-level types with a leading underscore. */
    root = &types;

    t = (struct Type *)tree_insert_str(root,
            name, len, NULL, sizeof(struct Type));
    if (!t->ident.node.is_new) {
        check_visibility(t);
        return t;
    }
    set_type_defaults(t);
    return t;
}

struct Type *reference_type(const char *name, size_t len)
{
    struct Type *t = map_named_type(name, len);
    if (t->ident.node.is_new && !t->ident.node.is_defined) {
        srcloc_init(&t->ident.srcloc); /* for "Undefined type" errors */
        assert(t->prev == NULL);
        assert(t->next == NULL);
        t->next = unbound_types_list;
        if (unbound_types_list) {
            unbound_types_list->prev = t;
        }
        unbound_types_list = t;
    }
    return t;
}

static void remove_type_from_unbound(struct Type *t)
{
    assert(t != NULL);
    if (t == unbound_types_list) {
        assert(t->prev == NULL);
        unbound_types_list = t->next;
    } else {
        assert(t->prev != NULL);
        t->prev->next = t->next;
    }
    if (t->next) {
        t->next->prev = t->prev;
    }
    t->next = NULL;
    t->prev = NULL;
}

void srcloc_init(struct SourceLocation *srcloc)
{
    assert(current_filename != NULL);
    srcloc->filename = current_filename;
    srcloc->line = current_line;
}

void type_start(const char *name, size_t len)
{
    struct Type *t;

    t = map_named_type(name, len);
    if (t->ident.node.is_defined) {
        if (!interfaces_done || t->is_implemented) {
            error_len(t->module_id == current_module_id ?
                        "Duplicate type name" :
                        "The bootstrap compiler does not support types "
                        "with the same name in different modules",
                      name, len);
        }
    }
    srcloc_init(&t->ident.srcloc);
    t->is_implemented = !!interfaces_done;
    if (in_exported_interface) {
        t->is_export = true;
    }
    t->funcs_exported = t->funcs_list;
    t->ctors_exported = t->ctors_list;
    t->inner_types_exported = t->inner_types_list;
    t->params_exported = t->vars_list;
    t->funcs_list = NULL;
    t->ctors_list = NULL;
    t->vars_list = NULL;
    t->inner_types_list = NULL;
    if (current_type) {
        innertype_func_nextptr = &t->funcs_list;
        innertype_ctor_nextptr = &t->ctors_list;
        innertype_var_nextptr = &t->vars_list;
    } else {
        type_func_nextptr = &t->funcs_list;
        type_ctor_nextptr = &t->ctors_list;
        type_var_nextptr = &t->vars_list;
        inner_type_nextptr = &t->inner_types_list;
    }
    if (!t->ident.node.is_defined) {
        if (!t->ident.node.is_new) {
            remove_type_from_unbound(t);
        }
        register_type(t);
        t->ident.node.is_defined = true;
    }
    current_type = t;
}

static void add_default_constructor(void)
{
    func_start("new", 3, FK_CONSTRUCTOR);
    func_end();
}

void type_end(void)
{
    if (!current_type->ctors) {
        add_default_constructor();
    }
    current_type = current_type->outer;
    innertype_func_nextptr = NULL;
    innertype_ctor_nextptr = NULL;
    innertype_var_nextptr = NULL;
    if (!current_type) {
        type_func_nextptr = NULL;
        type_ctor_nextptr = NULL;
        type_var_nextptr = NULL;
    }
}

static void set_func_defaults(struct Func *f)
{
    f->class_ = current_type;
    f->params = NULL;
    f->returns = NULL;
    f->vardecls = NULL;
    f->code = NULL;
    f->section_first = NULL;
    f->section_by_name = NULL;
    f->is_noreturn = false;
    f->is_modifying = false;
    f->is_constructor = false;
    f->is_service_ctor = false;
    f->is_entry = false;
    f->is_external = false;
    f->is_export = false;
    f->is_implemented = false;
    f->num_params = 0;
    f->num_returns = 0;
    f->next = NULL;
    f->prev = NULL;
    f->sincever_next = NULL;
}

static void register_func(struct Func *f)
{
    assert(!f->is_constructor);
    assert(f->next == NULL);
    assert(f->prev == NULL);
    if (current_type && current_type->outer) {
        *innertype_func_nextptr = f;
        innertype_func_nextptr = &f->next;
    } else if (current_type) {
        *type_func_nextptr = f;
        type_func_nextptr = &f->next;
    } else {
        *func_nextptr = f;
        func_nextptr = &f->next;
    }
}

static void register_ctor(struct Func *f)
{
    assert(f->is_constructor);
    assert(f->next == NULL);
    assert(f->prev == NULL);
    if (current_type->outer) {
        *innertype_ctor_nextptr = f;
        innertype_ctor_nextptr = &f->next;
    } else {
        *type_ctor_nextptr = f;
        type_ctor_nextptr = &f->next;
    }
}

static struct Func *map_named_func(const char *name, size_t len,
                                   enum FuncKind kind)
{
    struct Func *f;
    struct TreeNode **root;

    if (kind == FK_CONSTRUCTOR) {
        assert(current_type != NULL);
        root = &current_type->ctors;
    } else {
        root = (current_type ?
            &current_type->funcs : &toplevel_funcs);
    }

    f = (struct Func *)tree_insert_str(root,
            name, len, NULL, sizeof(struct Func));
    if (!f->ident.node.is_new) {
        assert(kind != FK_ENTRY); /* not supported in bootstrap */
        if (f->is_entry) {
            error_len("Attempt to call an `entry` function", name, len);
        }
        return f;
    }
    set_func_defaults(f);
    if (kind == FK_CONSTRUCTOR) {
        f->is_constructor = true;
        f->is_modifying = true;
        if (current_type->svcspecs) {
            f->is_service_ctor = true;
        }
    } else {
        f->is_entry = (kind == FK_ENTRY);
    }
    return f;
}

struct Func *reference_func(const char *name, size_t len, enum FuncKind kind)
{
    struct Func *f = map_named_func(name, len, kind);
    if (f->ident.node.is_new && !f->ident.node.is_defined) {
        srcloc_init(&f->ident.srcloc); /* for "Undefined function" errors */
        assert(f->prev == NULL);
        assert(f->next == NULL);
        f->next = unbound_funcs_list;
        if (unbound_funcs_list) {
            unbound_funcs_list->prev = f;
        }
        unbound_funcs_list = f;
    }
    return f;
}

static void remove_func_from_unbound(struct Func *f)
{
    assert(f != NULL);
    if (f == unbound_funcs_list) {
        assert(f->prev == NULL);
        unbound_funcs_list = f->next;
    } else {
        assert(f->prev != NULL);
        f->prev->next = f->next;
    }
    if (f->next) {
        f->next->prev = f->prev;
    }
    f->next = NULL;
    f->prev = NULL;
}

void func_start(const char *name, size_t len, enum FuncKind kind)
{
    struct Func *f;

    f = map_named_func(name, len, kind);
    if (f->ident.node.is_defined &&
            (!interfaces_done || f->is_implemented)) {
        error_len(kind != FK_CONSTRUCTOR ?
                "Duplicate function name" :
                "Duplicate constructor name",
              name, len);
    }
    srcloc_init(&f->ident.srcloc);
    f->is_external = !interfaces_done && !in_exported_interface;
    if (in_exported_interface) {
        f->is_export = true;
    }
    f->is_implemented = !!interfaces_done;
    if (!f->ident.node.is_defined) {
        if (!f->ident.node.is_new) {
            remove_func_from_unbound(f);
        }
        if (kind == FK_CONSTRUCTOR) {
            register_ctor(f);
        } else {
            register_func(f);
        }
        f->ident.node.is_defined = true;
    }
    current_func = f;
}

void func_end(void)
{
    current_func = NULL;
}

void toplevel_var_add(struct Var *var)
{
    struct Var ***nextptr;
    if (current_type && current_type->outer) {
        nextptr = &innertype_var_nextptr;
    } else if (current_type) {
        nextptr = &type_var_nextptr;
    } else {
        /* TODO should constants in "utility files" be allowed? */
        /*nextptr = &var_nextptr;*/
        assert(0);
    }
    var->next = NULL;
    **nextptr = var;
    *nextptr = &var->next;
}

struct Var *lookup_instance_var(const char *name, size_t len)
{
    HashCode h;

    assert(current_type != NULL);
    assert(name != NULL);
    assert(len != 0);
    h = hash_str(name, len);
    return (struct Var *)tree_search(current_type->vars, h, len, name);
}