-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathast.c
758 lines (663 loc) · 17.3 KB
/
ast.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#include <config.h>
#include "ast_t.h"
#include "type_t.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "adt/error.h"
struct obstack ast_obstack;
static FILE *out;
static int indent = 0;
static void print_statement(const statement_t *statement);
static void print_int_const(const int_const_t *int_const)
{
fprintf(out, "%d", int_const->value);
}
static void print_string_const(const string_const_t *string_const)
{
/* TODO escape " and non-printable chars */
fputc('"', out);
for (const char *c = string_const->value; *c != 0; ++c) {
switch (*c) {
case '\a': fputs("\\a", out); break;
case '\b': fputs("\\b", out); break;
case '\f': fputs("\\f", out); break;
case '\n': fputs("\\n", out); break;
case '\r': fputs("\\r", out); break;
case '\t': fputs("\\t", out); break;
case '\v': fputs("\\v", out); break;
case '\\': fputs("\\\\", out); break;
case '"': fputs("\\\"", out); break;
default: fputc(*c, out); break;
}
}
fputc('"', out);
}
static void print_call_expression(const call_expression_t *call)
{
print_expression(call->function);
fprintf(out, "(");
call_argument_t *argument = call->arguments;
int first = 1;
while (argument != NULL) {
if (!first) {
fprintf(out, ", ");
} else {
first = 0;
}
print_expression(argument->expression);
argument = argument->next;
}
fprintf(out, ")");
}
static void print_type_arguments(const type_argument_t *type_arguments)
{
const type_argument_t *argument = type_arguments;
int first = 1;
while (argument != NULL) {
if (first) {
fprintf(out, "<$");
first = 0;
} else {
fprintf(out, ", ");
}
print_type(argument->type);
argument = argument->next;
}
if (type_arguments != NULL) {
fprintf(out, ">");
}
}
static void print_reference_expression(const reference_expression_t *ref)
{
if (ref->entity == NULL) {
fprintf(out, "?%s", ref->symbol->string);
} else {
fprintf(out, "%s", ref->entity->base.symbol->string);
}
print_type_arguments(ref->type_arguments);
}
static void print_select_expression(const select_expression_t *select)
{
fprintf(out, "(");
print_expression(select->compound);
fprintf(out, ").");
if (select->compound_entry != NULL) {
fputs(select->compound_entry->symbol->string, out);
} else {
fprintf(out, "?%s", select->symbol->string);
}
}
static void print_array_access_expression(const array_access_expression_t *access)
{
fprintf(out, "(");
print_expression(access->array_ref);
fprintf(out, ")[");
print_expression(access->index);
fprintf(out, "]");
}
static void print_sizeof_expression(const sizeof_expression_t *expr)
{
fprintf(out, "(sizeof<");
print_type(expr->type);
fprintf(out, ">)");
}
static void print_unary_expression(const unary_expression_t *unexpr)
{
fprintf(out, "(");
switch (unexpr->base.kind) {
case EXPR_UNARY_CAST:
fprintf(out, "cast<");
print_type(unexpr->base.type);
fprintf(out, "> ");
print_expression(unexpr->value);
break;
default:
fprintf(out, "*unexpr %d*", unexpr->base.kind);
break;
}
fprintf(out, ")");
}
static void print_binary_expression(const binary_expression_t *binexpr)
{
fprintf(out, "(");
print_expression(binexpr->left);
fprintf(out, " ");
switch (binexpr->base.kind) {
case EXPR_BINARY_ASSIGN:
fprintf(out, "<-");
break;
case EXPR_BINARY_ADD:
fprintf(out, "+");
break;
case EXPR_BINARY_SUB:
fprintf(out, "-");
break;
case EXPR_BINARY_MUL:
fprintf(out, "*");
break;
case EXPR_BINARY_DIV:
fprintf(out, "/");
break;
case EXPR_BINARY_NOTEQUAL:
fprintf(out, "/=");
break;
case EXPR_BINARY_EQUAL:
fprintf(out, "=");
break;
case EXPR_BINARY_LESS:
fprintf(out, "<");
break;
case EXPR_BINARY_LESSEQUAL:
fprintf(out, "<=");
break;
case EXPR_BINARY_GREATER:
fprintf(out, ">");
break;
case EXPR_BINARY_GREATEREQUAL:
fprintf(out, ">=");
break;
default:
/* TODO: add missing ops */
fprintf(out, "op%d", binexpr->base.kind);
break;
}
fprintf(out, " ");
print_expression(binexpr->right);
fprintf(out, ")");
}
void print_expression(const expression_t *expression)
{
if (expression == NULL) {
fprintf(out, "*null expression*");
return;
}
switch (expression->kind) {
case EXPR_ERROR:
fprintf(out, "*error expression*");
break;
case EXPR_INVALID:
fprintf(out, "*invalid expression*");
break;
case EXPR_INT_CONST:
print_int_const((const int_const_t*) expression);
break;
case EXPR_STRING_CONST:
print_string_const((const string_const_t*) expression);
break;
case EXPR_NULL_POINTER:
fprintf(out, "null");
break;
case EXPR_CALL:
print_call_expression((const call_expression_t*) expression);
break;
EXPR_BINARY_CASES
print_binary_expression((const binary_expression_t*) expression);
break;
EXPR_UNARY_CASES
print_unary_expression((const unary_expression_t*) expression);
break;
case EXPR_SELECT:
print_select_expression((const select_expression_t*) expression);
break;
case EXPR_ARRAY_ACCESS:
print_array_access_expression(
(const array_access_expression_t*) expression);
break;
case EXPR_SIZEOF:
print_sizeof_expression((const sizeof_expression_t*) expression);
break;
case EXPR_REFERENCE:
print_reference_expression((const reference_expression_t*) expression);
break;
case EXPR_FLOAT_CONST:
case EXPR_BOOL_CONST:
case EXPR_FUNC:
/* TODO */
fprintf(out, "*expr TODO*");
break;
}
}
static void print_indent(void)
{
for (int i = 0; i < indent; ++i)
fprintf(out, "\t");
}
static void print_block_statement(const block_statement_t *block)
{
statement_t *statement = block->statements;
for ( ; statement != NULL; statement = statement->base.next) {
indent++;
print_statement(statement);
indent--;
}
}
static void print_return_statement(const return_statement_t *statement)
{
fprintf(out, "return ");
if (statement->value != NULL)
print_expression(statement->value);
}
static void print_expression_statement(const expression_statement_t *statement)
{
print_expression(statement->expression);
}
static void print_goto_statement(const goto_statement_t *statement)
{
fprintf(out, "goto ");
if (statement->label != NULL) {
symbol_t *symbol = statement->label->base.symbol;
if (symbol == NULL) {
fprintf(out, "$%p$", statement->label);
} else {
fprintf(out, "%s", symbol->string);
}
} else {
fprintf(out, "?%s", statement->label_symbol->string);
}
}
static void print_label_statement(const label_statement_t *statement)
{
symbol_t *symbol = statement->label.base.symbol;
if (symbol != NULL) {
fprintf(out, ":%s", symbol->string);
} else {
const label_t *label = &statement->label;
fprintf(out, ":$%p$", label);
}
}
static void print_if_statement(const if_statement_t *statement)
{
fprintf(out, "if ");
print_expression(statement->condition);
fprintf(out, ":\n");
if (statement->true_statement != NULL)
print_statement(statement->true_statement);
if (statement->false_statement != NULL) {
print_indent();
fprintf(out, "else:\n");
print_statement(statement->false_statement);
}
}
static void print_variable(const variable_t *variable)
{
fprintf(out, "var");
if (variable->type != NULL) {
fprintf(out, "<");
print_type(variable->type);
fprintf(out, ">");
}
fprintf(out, " %s", variable->base.symbol->string);
}
static void print_declaration_statement(const declaration_statement_t *statement)
{
print_variable(&statement->entity);
}
void print_statement(const statement_t *statement)
{
print_indent();
switch (statement->kind) {
case STATEMENT_BLOCK:
print_block_statement(&statement->block);
break;
case STATEMENT_RETURN:
print_return_statement(&statement->returns);
break;
case STATEMENT_EXPRESSION:
print_expression_statement(&statement->expression);
break;
case STATEMENT_LABEL:
print_label_statement(&statement->label);
break;
case STATEMENT_GOTO:
print_goto_statement(&statement->gotos);
break;
case STATEMENT_IF:
print_if_statement(&statement->ifs);
break;
case STATEMENT_DECLARATION:
print_declaration_statement(&statement->declaration);
break;
case STATEMENT_INVALID:
default:
fprintf(out, "*invalid statement*");
break;
}
fprintf(out, "\n");
}
static void print_type_constraint(const type_constraint_t *constraint)
{
if (constraint->concept == NULL) {
fprintf(out, "?%s", constraint->concept_symbol->string);
} else {
fprintf(out, "%s", constraint->concept->base.symbol->string);
}
}
static void print_type_variable(const type_variable_t *type_variable)
{
type_constraint_t *constraint = type_variable->constraints;
while (constraint != NULL) {
print_type_constraint(constraint);
fprintf(out, " ");
constraint = constraint->next;
}
fprintf(out, "%s", type_variable->base.symbol->string);
}
static void print_type_parameters(const type_variable_t *type_parameters)
{
int first = 1;
const type_variable_t *type_parameter = type_parameters;
while (type_parameter != NULL) {
if (first) {
fprintf(out, "<");
first = 0;
} else {
fprintf(out, ", ");
}
print_type_variable(type_parameter);
type_parameter = type_parameter->next;
}
if (type_parameters != NULL)
fprintf(out, ">");
}
static void print_function_parameters(const function_parameter_t *parameters,
const function_type_t *function_type)
{
fprintf(out, "(");
int first = 1;
const function_parameter_t *parameter = parameters;
const function_parameter_type_t *parameter_type
= function_type->parameter_types;
while (parameter != NULL && parameter_type != NULL) {
if (!first) {
fprintf(out, ", ");
} else {
first = 0;
}
print_type(parameter_type->type);
fprintf(out, " %s", parameter->base.symbol->string);
parameter = parameter->next;
parameter_type = parameter_type->next;
}
assert(parameter == NULL && parameter_type == NULL);
fprintf(out, ")");
}
static void print_function(const function_entity_t *function_entity)
{
const function_t *function = &function_entity->function;
function_type_t *type = function->type;
fprintf(out, "func ");
if (function->is_extern) {
fprintf(out, "extern ");
}
fprintf(out, " %s", function_entity->base.symbol->string);
print_type_parameters(function->type_parameters);
print_function_parameters(function->parameters, type);
fprintf(out, " : ");
print_type(type->result_type);
if (function->statement != NULL) {
fprintf(out, ":\n");
print_statement(function->statement);
} else {
fprintf(out, "\n");
}
}
static void print_concept_function(const concept_function_t *function)
{
fprintf(out, "\tfunc ");
fprintf(out, "%s", function->base.symbol->string);
print_function_parameters(function->parameters, function->type);
fprintf(out, " : ");
print_type(function->type->result_type);
fprintf(out, "\n\n");
}
static void print_concept(const concept_t *concept)
{
fprintf(out, "concept %s", concept->base.symbol->string);
print_type_parameters(concept->type_parameters);
fprintf(out, ":\n");
concept_function_t *function = concept->functions;
while (function != NULL) {
print_concept_function(function);
function = function->next;
}
}
static void print_concept_function_instance(
concept_function_instance_t *function_instance)
{
fprintf(out, "\tfunc ");
const function_t *function = &function_instance->function;
if (function_instance->concept_function != NULL) {
concept_function_t *function = function_instance->concept_function;
fprintf(out, "%s", function->base.symbol->string);
} else {
fprintf(out, "?%s", function_instance->symbol->string);
}
print_function_parameters(function->parameters, function->type);
fprintf(out, " : ");
print_type(function_instance->function.type->result_type);
if (function->statement != NULL) {
fprintf(out, ":\n");
print_statement(function->statement);
} else {
fprintf(out, "\n");
}
}
static void print_concept_instance(const concept_instance_t *instance)
{
fprintf(out, "instance ");
if (instance->concept != NULL) {
fprintf(out, "%s", instance->concept->base.symbol->string);
} else {
fprintf(out, "?%s", instance->concept_symbol->string);
}
print_type_arguments(instance->type_arguments);
fprintf(out, ":\n");
concept_function_instance_t *function_instance
= instance->function_instances;
while (function_instance != NULL) {
print_concept_function_instance(function_instance);
function_instance = function_instance->next;
}
}
static void print_constant(const constant_t *constant)
{
fprintf(out, "const %s", constant->base.symbol->string);
if (constant->type != NULL) {
fprintf(out, " ");
print_type(constant->type);
}
if (constant->expression != NULL) {
fprintf(out, " <- ");
print_expression(constant->expression);
}
fprintf(out, "\n");
}
static void print_typealias(const typealias_t *alias)
{
fprintf(out, "typealias %s <- ", alias->base.symbol->string);
print_type(alias->type);
fprintf(out, "\n");
}
static void print_entity(const entity_t *entity)
{
print_indent();
switch (entity->kind) {
case ENTITY_FUNCTION:
print_function(&entity->function);
break;
case ENTITY_CONCEPT:
print_concept(&entity->concept);
break;
case ENTITY_VARIABLE:
print_variable(&entity->variable);
break;
case ENTITY_TYPEALIAS:
print_typealias(&entity->typealias);
break;
case ENTITY_CONSTANT:
print_constant(&entity->constant);
break;
case ENTITY_CONCEPT_FUNCTION:
case ENTITY_FUNCTION_PARAMETER:
case ENTITY_ERROR:
// TODO
fprintf(out, "some entity of type '%s'\n",
get_entity_kind_name(entity->kind));
break;
case ENTITY_TYPE_VARIABLE:
case ENTITY_LABEL:
break;
case ENTITY_INVALID:
fprintf(out, "invalid entity (%s)\n",
get_entity_kind_name(entity->kind));
break;
}
}
static void print_context(const context_t *context)
{
for (entity_t *entity = context->entities; entity != NULL;
entity = entity->base.next) {
print_entity(entity);
}
concept_instance_t *instance = context->concept_instances;
for ( ; instance != NULL; instance = instance->next) {
print_concept_instance(instance);
}
}
void print_ast(FILE *new_out, const context_t *context)
{
indent = 0;
out = new_out;
print_context(context);
assert(indent == 0);
out = NULL;
}
const char *get_entity_kind_name(entity_kind_t type)
{
switch (type) {
case ENTITY_ERROR: return "parse error";
case ENTITY_INVALID: return "invalid reference";
case ENTITY_VARIABLE: return "variable";
case ENTITY_CONSTANT: return "constant";
case ENTITY_FUNCTION_PARAMETER: return "function parameter";
case ENTITY_FUNCTION: return "function";
case ENTITY_CONCEPT: return "concept";
case ENTITY_TYPEALIAS: return "type alias";
case ENTITY_TYPE_VARIABLE: return "type variable";
case ENTITY_LABEL: return "label";
case ENTITY_CONCEPT_FUNCTION: return "concept function";
}
panic("invalid environment entry found");
}
void init_ast_module(void)
{
out = stderr;
obstack_init(&ast_obstack);
}
void exit_ast_module(void)
{
obstack_free(&ast_obstack, NULL);
}
void* (allocate_ast) (size_t size)
{
return _allocate_ast(size);
}
unsigned register_expression(void)
{
static unsigned nextid = EXPR_LAST;
++nextid;
return nextid;
}
unsigned register_statement(void)
{
static unsigned nextid = STATEMENT_LAST;
++nextid;
return nextid;
}
unsigned register_entity(void)
{
static unsigned nextid = ENTITY_LAST;
++nextid;
return nextid;
}
bool is_linktime_constant(const expression_t *expression)
{
switch (expression->kind) {
case EXPR_SELECT:
/* TODO */
return false;
case EXPR_ARRAY_ACCESS:
/* TODO */
return false;
case EXPR_UNARY_DEREFERENCE:
return is_constant_expression(expression->unary.value);
default:
return false;
}
}
bool is_constant_expression(const expression_t *expression)
{
switch (expression->kind) {
case EXPR_INT_CONST:
case EXPR_FLOAT_CONST:
case EXPR_BOOL_CONST:
case EXPR_NULL_POINTER:
case EXPR_SIZEOF:
return true;
case EXPR_STRING_CONST:
case EXPR_FUNC:
case EXPR_UNARY_INCREMENT:
case EXPR_UNARY_DECREMENT:
case EXPR_UNARY_DEREFERENCE:
case EXPR_BINARY_ASSIGN:
case EXPR_SELECT:
case EXPR_ARRAY_ACCESS:
return false;
case EXPR_UNARY_TAKE_ADDRESS:
return is_linktime_constant(expression->unary.value);
case EXPR_REFERENCE: {
entity_t *entity = expression->reference.entity;
if (entity->kind == ENTITY_CONSTANT)
return true;
return false;
}
case EXPR_CALL:
/* TODO: we might introduce pure/side effect free calls */
return false;
case EXPR_UNARY_CAST:
case EXPR_UNARY_NEGATE:
case EXPR_UNARY_NOT:
case EXPR_UNARY_BITWISE_NOT:
return is_constant_expression(expression->unary.value);
case EXPR_BINARY_ADD:
case EXPR_BINARY_SUB:
case EXPR_BINARY_MUL:
case EXPR_BINARY_DIV:
case EXPR_BINARY_MOD:
case EXPR_BINARY_EQUAL:
case EXPR_BINARY_NOTEQUAL:
case EXPR_BINARY_LESS:
case EXPR_BINARY_LESSEQUAL:
case EXPR_BINARY_GREATER:
case EXPR_BINARY_GREATEREQUAL:
case EXPR_BINARY_AND:
case EXPR_BINARY_OR:
case EXPR_BINARY_XOR:
case EXPR_BINARY_SHIFTLEFT:
case EXPR_BINARY_SHIFTRIGHT:
/* not that lazy and/or are not constant if their value is clear after
* evaluating the left side. This is because we can't (always) evaluate the
* left hand side until the ast2firm phase, and therefore can't determine
* constness */
case EXPR_BINARY_LAZY_AND:
case EXPR_BINARY_LAZY_OR:
return is_constant_expression(expression->binary.left)
&& is_constant_expression(expression->binary.right);
case EXPR_ERROR:
return true;
case EXPR_INVALID:
break;
}
panic("invalid expression in is_constant_expression");
}