-
Notifications
You must be signed in to change notification settings - Fork 4
/
order.c
683 lines (627 loc) · 22.8 KB
/
order.c
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// SPDX-License-Identifier: Apache-2.0
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "sunder.h"
// Top-Level Declaration
struct tldecl {
enum {
TLDECL_UNORDERED,
TLDECL_ORDERING,
TLDECL_ORDERED,
} state;
struct cst_decl const* decl;
};
struct orderer {
struct module* module;
// Top-level declarations and their associated ordering state in the order
// that they were seen by the parser. Initialized and populated within
// order new. This stretchy buffer is not resized after it is initialized,
// so addresses of each element are stable for the duration of the ordering
// phase.
sbuf(struct tldecl) tldecls;
// Top-level declarations, topologically sorted such that any declaration
// with index k does not depend on any declaration with index k+n for all
// n. Initialized as an empty sbuf in orderer_new and populated during the
// order phase.
sbuf(struct cst_decl const*) topological_order;
// List of declaration dependencies.
sbuf(struct cst_decl const*) dependencies;
};
static struct orderer*
orderer_new(struct module* module);
static void
orderer_del(struct orderer* self);
static struct tldecl*
orderer_tldecl_lookup(struct orderer* orderer, char const* name);
static void
order_tldecl(struct orderer* orderer, struct tldecl* tldecl);
static void
order_decl(struct orderer* orderer, struct cst_decl const* decl);
static void
order_expr(struct orderer* orderer, struct cst_expr const* expr);
static void
order_template_argument_list(
struct orderer* orderer, struct cst_type const* const* arguments);
static void
order_type(struct orderer* orderer, struct cst_type const* type);
static void
order_symbol(struct orderer* orderer, struct cst_symbol const* symbol);
static void
order_identifier(
struct orderer* orderer, struct cst_identifier const* identifier);
static void
order_name(struct orderer* orderer, char const* name);
static struct orderer*
orderer_new(struct module* module)
{
assert(module != NULL);
struct orderer* const self = xalloc(NULL, sizeof(*self));
memset(self, 0x00, sizeof(*self));
self->module = module;
for (size_t i = 0; i < sbuf_count(module->cst->decls); ++i) {
struct tldecl const tldecl = {TLDECL_UNORDERED, module->cst->decls[i]};
struct tldecl const* const existing =
orderer_tldecl_lookup(self, module->cst->decls[i]->name);
if (existing != NULL && existing->decl->name == tldecl.decl->name
&& existing->decl->kind == CST_DECL_EXTERN_VARIABLE
&& tldecl.decl->kind == CST_DECL_EXTERN_VARIABLE) {
// Duplicate extern variable declaration.
continue;
}
if (existing != NULL && existing->decl->name == tldecl.decl->name
&& existing->decl->kind == CST_DECL_EXTERN_FUNCTION
&& tldecl.decl->kind == CST_DECL_EXTERN_FUNCTION) {
// Duplicate extern function declaration.
continue;
}
if (existing != NULL && existing->decl->name == tldecl.decl->name
&& existing->decl->kind == CST_DECL_EXTERN_TYPE
&& tldecl.decl->kind == CST_DECL_EXTERN_TYPE) {
// Duplicate extern type declaration.
continue;
}
if (existing != NULL && tldecl.decl->kind != CST_DECL_EXTEND) {
fatal(
module->cst->decls[i]->location,
"redeclaration of `%s` previously declared at [%s:%zu]",
existing->decl->name,
existing->decl->location.path,
existing->decl->location.line);
}
sbuf_push(self->tldecls, tldecl);
}
return self;
}
static void
orderer_del(struct orderer* self)
{
assert(self != NULL);
assert(sbuf_count(self->dependencies) == 0);
sbuf_fini(self->tldecls);
sbuf_fini(self->topological_order);
sbuf_fini(self->dependencies);
memset(self, 0x00, sizeof(*self));
xalloc(self, XALLOC_FREE);
}
static struct tldecl*
orderer_tldecl_lookup(struct orderer* orderer, char const* name)
{
assert(orderer != NULL);
assert(name != NULL);
for (size_t i = 0; i < sbuf_count(orderer->tldecls); ++i) {
struct cst_decl const* const decl = orderer->tldecls[i].decl;
if (decl->name == name && decl->kind != CST_DECL_EXTEND) {
return &orderer->tldecls[i];
}
}
return NULL;
}
static void
order_tldecl(struct orderer* orderer, struct tldecl* tldecl)
{
assert(orderer != NULL);
assert(tldecl != NULL);
if (tldecl->state == TLDECL_ORDERED) {
// Top-level declaration is already ordered.
return;
}
if (tldecl->state == TLDECL_ORDERING) {
// Top-level declaration is currently in the process of being ordered.
error(
tldecl->decl->location,
"circular dependency created by declaration of `%s`",
tldecl->decl->name);
for (size_t i = 0; i < sbuf_count(orderer->dependencies); ++i) {
size_t j = i + 1 != sbuf_count(orderer->dependencies) ? i + 1 : 0;
info(
NO_LOCATION,
"declaration of `%s` (line %zu) depends on `%s` (line %zu)",
orderer->dependencies[i]->name,
orderer->dependencies[i]->location.line,
orderer->dependencies[j]->name,
orderer->dependencies[j]->location.line);
}
exit(EXIT_FAILURE);
}
// Change the state from UNORDERED to ORDERING so that cyclic dependencies
// will be detected if another attempt is made to order this declaration.
tldecl->state = TLDECL_ORDERING;
// Perform ordering on the top level declaration.
sbuf_push(orderer->dependencies, tldecl->decl);
order_decl(orderer, tldecl->decl);
sbuf_resize(orderer->dependencies, sbuf_count(orderer->dependencies) - 1);
// Change the state from ORDERING TO ORDERED after ordering the top level
// declaration as well as all of top level declaration's dependencies.
tldecl->state = TLDECL_ORDERED;
sbuf_push(orderer->topological_order, tldecl->decl);
}
static void
order_decl(struct orderer* orderer, struct cst_decl const* decl)
{
assert(order != NULL);
assert(decl != NULL);
switch (decl->kind) {
case CST_DECL_VARIABLE: {
if (decl->data.variable.type != NULL) {
order_type(orderer, decl->data.variable.type);
}
if (decl->data.variable.expr != NULL) {
order_expr(orderer, decl->data.variable.expr);
}
return;
}
case CST_DECL_CONSTANT: {
if (decl->data.constant.type != NULL) {
order_type(orderer, decl->data.constant.type);
}
if (decl->data.constant.expr != NULL) {
order_expr(orderer, decl->data.constant.expr);
}
return;
}
case CST_DECL_FUNCTION: {
sbuf(struct cst_identifier const) const template_parameters =
decl->data.function.template_parameters;
if (sbuf_count(template_parameters) != 0) {
return;
}
struct cst_function_parameter const* const* const function_parameters =
decl->data.function.function_parameters;
for (size_t i = 0; i < sbuf_count(function_parameters); ++i) {
order_type(orderer, function_parameters[i]->type);
}
order_type(orderer, decl->data.function.return_type);
return;
}
case CST_DECL_STRUCT: {
sbuf(struct cst_identifier const) const template_parameters =
decl->data.struct_.template_parameters;
if (sbuf_count(template_parameters) != 0) {
return;
}
// Set this struct's state to ordered to allow for self-referential
// members. This behavior mimics the behavior of the resolve phase
// where all structs/unions are completed after their symbols have been
// added to the relevant symbol table.
struct tldecl* const tldecl =
orderer_tldecl_lookup(orderer, decl->name);
if (tldecl == NULL) {
// The returned tldecl may be NULL if this struct declaration is
// part of an extend declaration. We return early here since an
// error will be reported when the extend declaration is resolved.
return;
}
tldecl->state = TLDECL_ORDERED;
// Order the struct's members.
sbuf(struct cst_member const* const) const members =
decl->data.struct_.members;
for (size_t i = 0; i < sbuf_count(members); ++i) {
struct cst_member const* const member = members[i];
switch (member->kind) {
case CST_MEMBER_VARIABLE: {
order_type(orderer, member->data.variable.type);
continue;
}
case CST_MEMBER_CONSTANT: {
order_decl(orderer, member->data.constant.decl);
continue;
}
case CST_MEMBER_FUNCTION: {
order_decl(orderer, member->data.function.decl);
continue;
}
}
}
return;
}
case CST_DECL_UNION: {
sbuf(struct cst_identifier const) const template_parameters =
decl->data.union_.template_parameters;
if (sbuf_count(template_parameters) != 0) {
return;
}
// Set this union's state to ordered to allow for self-referential
// members. This behavior mimics the behavior of the resolve phase
// where all structs/unions are completed after their symbols have been
// added to the relevant symbol table.
struct tldecl* const tldecl =
orderer_tldecl_lookup(orderer, decl->name);
if (tldecl == NULL) {
// The returned tldecl may be NULL if this union declaration is
// part of an extend declaration. We return early here since an
// error will be reported when the extend declaration is resolved.
return;
}
tldecl->state = TLDECL_ORDERED;
// Order the struct's members.
sbuf(struct cst_member const* const) const members =
decl->data.union_.members;
for (size_t i = 0; i < sbuf_count(members); ++i) {
struct cst_member const* const member = members[i];
switch (member->kind) {
case CST_MEMBER_VARIABLE: {
order_type(orderer, member->data.variable.type);
continue;
}
case CST_MEMBER_CONSTANT: {
order_decl(orderer, member->data.constant.decl);
continue;
}
case CST_MEMBER_FUNCTION: {
order_decl(orderer, member->data.function.decl);
continue;
}
}
}
return;
}
case CST_DECL_ENUM: {
sbuf(struct cst_enum_value const* const) const values =
decl->data.enum_.values;
for (size_t i = 0; i < sbuf_count(values); ++i) {
if (values[i]->expr == NULL) {
continue;
}
order_expr(orderer, values[i]->expr);
}
// Set this enum's state to ordered to allow for self-referential
// member functions. This behavior mimics the behavior of the resolve
// phase where all enums have their values processed before member
// functions are completed.
struct tldecl* const tldecl =
orderer_tldecl_lookup(orderer, decl->name);
if (tldecl == NULL) {
// The returned tldecl may be NULL if this enum declaration is part
// of an extend declaration. We return early here since an error
// will be reported when the extend declaration is resolved.
return;
}
tldecl->state = TLDECL_ORDERED;
sbuf(struct cst_member const* const) const member_functions =
decl->data.enum_.member_functions;
for (size_t i = 0; i < sbuf_count(member_functions); ++i) {
assert(member_functions[i]->kind == CST_MEMBER_FUNCTION);
order_decl(orderer, member_functions[i]->data.function.decl);
}
return;
}
case CST_DECL_EXTEND: {
// Extend declarations are resolved in declaration order after all
// module-level declarations, so no ordering of the extended type
// specifier or the extending declaration is required.
return;
}
case CST_DECL_ALIAS: {
order_type(orderer, decl->data.alias.type);
return;
}
case CST_DECL_EXTERN_VARIABLE: {
order_type(orderer, decl->data.extern_variable.type);
return;
}
case CST_DECL_EXTERN_FUNCTION: {
struct cst_function_parameter const* const* const function_parameters =
decl->data.extern_function.function_parameters;
for (size_t i = 0; i < sbuf_count(function_parameters); ++i) {
order_type(orderer, function_parameters[i]->type);
}
order_type(orderer, decl->data.extern_function.return_type);
return;
}
case CST_DECL_EXTERN_TYPE: {
// Extern type declarations have no dependencies.
return;
}
}
UNREACHABLE();
}
static void
order_expr(struct orderer* orderer, struct cst_expr const* expr)
{
assert(orderer != NULL);
assert(expr != NULL);
switch (expr->kind) {
case CST_EXPR_SYMBOL: {
order_symbol(orderer, expr->data.symbol);
return;
}
case CST_EXPR_BOOLEAN: /* fallthrough */
case CST_EXPR_INTEGER: /* fallthrough */
case CST_EXPR_IEEE754: /* fallthrough */
case CST_EXPR_CHARACTER: /* fallthrough */
case CST_EXPR_BYTES: {
return;
}
case CST_EXPR_LIST: {
sbuf(struct cst_expr const* const) const elements =
expr->data.list.elements;
for (size_t i = 0; i < sbuf_count(elements); ++i) {
order_expr(orderer, elements[i]);
}
if (expr->data.list.ellipsis != NULL) {
order_expr(orderer, expr->data.list.ellipsis);
}
return;
}
case CST_EXPR_SLICE: {
order_expr(orderer, expr->data.slice.start);
order_expr(orderer, expr->data.slice.count);
return;
}
case CST_EXPR_INIT: {
order_type(orderer, expr->data.init.type);
sbuf(struct cst_member_initializer const* const) const initializers =
expr->data.init.initializers;
for (size_t i = 0; i < sbuf_count(initializers); ++i) {
if (initializers[i]->expr != NULL) {
order_expr(orderer, initializers[i]->expr);
}
}
return;
}
case CST_EXPR_CAST: {
order_type(orderer, expr->data.cast.type);
order_expr(orderer, expr->data.cast.expr);
return;
}
case CST_EXPR_GROUPED: {
order_expr(orderer, expr->data.grouped.expr);
return;
}
case CST_EXPR_CALL: {
order_expr(orderer, expr->data.call.func);
sbuf(struct cst_expr const* const) const arguments =
expr->data.call.arguments;
for (size_t i = 0; i < sbuf_count(arguments); ++i) {
order_expr(orderer, arguments[i]);
}
return;
}
case CST_EXPR_ACCESS_INDEX: {
order_expr(orderer, expr->data.access_index.lhs);
order_expr(orderer, expr->data.access_index.idx);
return;
}
case CST_EXPR_ACCESS_SLICE: {
order_expr(orderer, expr->data.access_slice.lhs);
order_expr(orderer, expr->data.access_slice.begin);
order_expr(orderer, expr->data.access_slice.end);
return;
}
case CST_EXPR_ACCESS_MEMBER: {
order_expr(orderer, expr->data.access_member.lhs);
return;
}
case CST_EXPR_ACCESS_DEREFERENCE: {
order_expr(orderer, expr->data.access_dereference.lhs);
return;
}
case CST_EXPR_DEFINED: {
order_symbol(orderer, expr->data.defined.symbol);
return;
}
case CST_EXPR_SIZEOF: {
order_type(orderer, expr->data.sizeof_.rhs);
return;
}
case CST_EXPR_ALIGNOF: {
order_type(orderer, expr->data.alignof_.rhs);
return;
}
case CST_EXPR_FILEOF: {
return;
}
case CST_EXPR_LINEOF: {
return;
}
case CST_EXPR_EMBED: {
return;
}
case CST_EXPR_UNARY: {
order_expr(orderer, expr->data.unary.rhs);
return;
}
case CST_EXPR_BINARY: {
order_expr(orderer, expr->data.binary.lhs);
order_expr(orderer, expr->data.binary.rhs);
return;
}
}
UNREACHABLE();
}
static void
order_template_argument_list(
struct orderer* orderer, struct cst_type const* const* arguments)
{
assert(orderer != NULL);
for (size_t i = 0; i < sbuf_count(arguments); ++i) {
order_type(orderer, arguments[i]);
}
}
static void
order_type(struct orderer* orderer, struct cst_type const* type)
{
assert(orderer != NULL);
assert(type != NULL);
switch (type->kind) {
case CST_TYPE_SYMBOL: {
order_symbol(orderer, type->data.symbol);
return;
}
case CST_TYPE_FUNCTION: {
sbuf(struct cst_type const* const) const parameter_types =
type->data.function.parameter_types;
for (size_t i = 0; i < sbuf_count(parameter_types); ++i) {
order_type(orderer, parameter_types[i]);
}
order_type(orderer, type->data.function.return_type);
return;
}
case CST_TYPE_POINTER: {
order_type(orderer, type->data.pointer.base);
return;
}
case CST_TYPE_ARRAY: {
order_expr(orderer, type->data.array.count);
order_type(orderer, type->data.array.base);
return;
}
case CST_TYPE_SLICE: {
order_type(orderer, type->data.slice.base);
return;
}
case CST_TYPE_STRUCT: {
sbuf(struct cst_member const* const) const members =
type->data.struct_.members;
for (size_t i = 0; i < sbuf_count(members); ++i) {
struct cst_member const* const member = members[i];
assert(member->kind == CST_MEMBER_VARIABLE);
order_type(orderer, member->data.variable.type);
}
return;
}
case CST_TYPE_UNION: {
sbuf(struct cst_member const* const) const members =
type->data.union_.members;
for (size_t i = 0; i < sbuf_count(members); ++i) {
struct cst_member const* const member = members[i];
assert(member->kind == CST_MEMBER_VARIABLE);
order_type(orderer, member->data.variable.type);
}
return;
}
case CST_TYPE_ENUM: {
sbuf(struct cst_enum_value const* const) const values =
type->data.enum_.values;
for (size_t i = 0; i < sbuf_count(values); ++i) {
if (values[i]->expr == NULL) {
continue;
}
order_expr(orderer, values[i]->expr);
}
return;
}
case CST_TYPE_TYPEOF: {
order_expr(orderer, type->data.typeof_.expr);
return;
}
}
UNREACHABLE();
}
static void
order_symbol(struct orderer* orderer, struct cst_symbol const* symbol)
{
assert(orderer != NULL);
assert(sbuf_count(symbol->elements) > 0);
// Always attempt to order all symbol template arguments, regardless of
// whether the symbol belongs to the current module or not, since symbol
// template arguments may refer to symbols that *are* in this module.
for (size_t i = 0; i < sbuf_count(symbol->elements); ++i) {
order_template_argument_list(
orderer, symbol->elements[i]->template_arguments);
}
char const* const symbol_elem0_name = symbol->elements[0]->identifier.name;
bool const symbol_elem0_defined_in_current_module =
orderer_tldecl_lookup(orderer, symbol_elem0_name) != NULL;
bool const search_qualified_symbol = symbol->start == CST_SYMBOL_START_ROOT
|| !symbol_elem0_defined_in_current_module;
if (search_qualified_symbol) {
struct cst_namespace const* const namespace =
orderer->module->cst->namespace;
size_t const namespace_count =
namespace != NULL ? sbuf_count(namespace->identifiers) : 0;
for (size_t i = 0; i < namespace_count; ++i) {
char const* const element_name =
symbol->elements[i]->identifier.name;
char const* const namespace_name = namespace->identifiers[i].name;
if (element_name == namespace_name) {
// Continue matching against the current module namespace.
continue;
}
// Module namespace does not fully match the current module
// namespace. Assume that the symbol refers to a construct defined
// under a parent namespace in some other module.
return;
}
// Perform ordering based on the non-prefix portion of the symbol.
order_identifier(
orderer, &symbol->elements[namespace_count]->identifier);
return;
}
if (symbol->start == CST_SYMBOL_START_TYPE) {
order_type(orderer, symbol->type);
return;
}
// Perform ordering based on the first element of the symbol.
assert(symbol->start != CST_SYMBOL_START_ROOT);
order_identifier(orderer, &symbol->elements[0]->identifier);
}
static void
order_identifier(
struct orderer* orderer, struct cst_identifier const* identifier)
{
assert(orderer != NULL);
assert(identifier != NULL);
order_name(orderer, identifier->name);
}
static void
order_name(struct orderer* orderer, char const* name)
{
assert(orderer != NULL);
assert(name != NULL);
struct tldecl* const tldecl = orderer_tldecl_lookup(orderer, name);
if (tldecl == NULL) {
// Top-level declaration with the provided name does not exist. Assume
// that it is a builtin and allow future stages of semantic analysis to
// raise an unknown identifier error if necessary.
return;
}
order_tldecl(orderer, tldecl);
}
void
order(struct module* module)
{
assert(module != NULL);
struct orderer* const orderer = orderer_new(module);
size_t const decl_count = sbuf_count(orderer->tldecls);
// Make sure that extend declarations come *after* all other declarations.
for (size_t i = 1; i < decl_count; ++i) {
struct cst_decl const* const prev = orderer->tldecls[i - 1].decl;
struct cst_decl const* const decl = orderer->tldecls[i].decl;
if (decl->kind != CST_DECL_EXTEND && prev->kind == CST_DECL_EXTEND) {
fatal(
prev->location,
"extend declaration must appear after all module-level declarations");
}
}
for (size_t i = 0; i < decl_count; ++i) {
order_tldecl(orderer, &orderer->tldecls[i]);
}
assert(decl_count == sbuf_count(orderer->topological_order));
for (size_t i = 0; i < decl_count; ++i) {
struct cst_decl const* const decl = orderer->topological_order[i];
sbuf_push(module->ordered, decl);
}
orderer_del(orderer);
}