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
|
/*
* Functions for constructing/accessing the Abstract Syntax Tree.
*
* Copyright © 2025 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"
#include "token.h"
struct Module *module, *modules;
struct Type *current_type;
struct Func *current_func;
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;
struct ExprString *string_constants = NULL;
static struct ExprString *empty_string = NULL;
static unsigned next_string_const_id = 1;
void module_start(void)
{
struct Module *mod = malloc(sizeof(struct Module));
NO_NULL(mod);
mod->types = NULL;
mod->funcs = NULL;
/*mod->consts = NULL;*/
mod->types_list = NULL;
mod->funcs_list = NULL;
/*mod->consts_list = NULL;*/
mod->next = modules;
modules = mod;
module = mod;
current_type = NULL;
current_func = NULL;
type_nextptr = &module->types_list;
func_nextptr = &module->funcs_list;
/*var_nextptr = &module->consts_list;*/
}
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 ?
¤t_type->inner_types :
&module->types);*/
/* TODO hack until nested types work. consider distinguishing
nested vs top-level types with a leading underscore. */
root = &module->types;
t = (struct Type *)tree_insert_str(root,
name, len, NULL, sizeof(struct Type));
if (!t->ident.node.is_new) {
return t;
}
/*t->outer = current_type;*/
t->outer = NULL; /* TODO nested types */
t->funcs = NULL;
t->funcs_list = NULL;
t->ctors = NULL;
t->ctors_list = NULL;
t->ctor_default = NULL;
t->inner_types = NULL;
t->inner_types_list = NULL;
t->vars = NULL;
t->vars_list = NULL;
t->svcspecs = NULL;
t->svcspecs_list = NULL;
t->next = NULL;
if (current_type) {
*inner_type_nextptr = t;
inner_type_nextptr = &t->next;
} else {
*type_nextptr = t;
type_nextptr = &t->next;
}
return t;
}
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) {
error("Duplicate type name");
}
srcloc_init(&t->ident.srcloc);
t->ident.node.is_defined = true;
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;
}
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->num_params = 0;
f->num_returns = 0;
f->next = NULL;
}
static void register_func(struct Func *f)
{
assert(!f->is_constructor);
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);
if (current_type->outer) {
*innertype_ctor_nextptr = f;
innertype_ctor_nextptr = &f->next;
} else {
*type_ctor_nextptr = f;
type_ctor_nextptr = &f->next;
}
}
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 = ¤t_type->ctors;
} else {
root = (current_type ?
¤t_type->funcs : &module->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("Attempt to call an `entry` function");
}
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;
}
register_ctor(f);
} else {
f->is_entry = (kind == FK_ENTRY);
register_func(f);
}
return f;
}
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) {
error(kind != FK_CONSTRUCTOR ?
"Duplicate function name" :
"Duplicate constructor name");
}
srcloc_init(&f->ident.srcloc);
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;
}
bool instancedefs_seen(void)
{
/* TODO should constants in "utility files" be allowed? */
assert(current_type != NULL);
return current_type->vars != NULL || funcdefs_seen();
}
bool funcdefs_seen(void)
{
/* TODO should constants in "utility files" be allowed? */
/*if (!current_type) {
return module->funcs != NULL;
} else {
return current_type->funcs != NULL;
}*/
assert(current_type != NULL);
return current_type->funcs != NULL;
}
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);
}
struct ExprString *new_string_literal(const char *string, size_t len)
{
if (len == 0 && empty_string && current_func) {
return empty_string;
} else {
struct ExprString *es = malloc(sizeof(struct ExprString));
NO_NULL(es);
es->len = len;
/* TODO unescape string */
es->s = (len != 0 ?
dupmemz(string, len) : NULL);
/* Add to list of string literals */
es->id = next_string_const_id++;
es->next = string_constants;
string_constants = es;
if (len == 0) {
assert(empty_string == NULL);
empty_string = es;
}
return es;
}
}
|