-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdeclaration.c
1273 lines (1164 loc) · 33.1 KB
/
declaration.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if !AMALGAMATION
# define INTERNAL
# define EXTERNAL extern
#endif
#include "declaration.h"
#include "eval.h"
#include "expression.h"
#include "initializer.h"
#include "parse.h"
#include "statement.h"
#include "symtab.h"
#include "typetree.h"
#include <lacc/context.h>
#include <lacc/token.h>
#include <assert.h>
static const Type *get_typedef(String str)
{
struct symbol *tag;
tag = sym_lookup(&ns_ident, str);
if (tag && tag->symtype == SYM_TYPEDEF) {
return &tag->type;
}
return NULL;
}
static struct block *parameter_declarator(
struct definition *def,
struct block *block,
Type base,
Type *type,
String *name,
size_t *length);
/*
* Parse a function parameter list, adding symbols to scope.
*
* FOLLOW(parameter-list) = { ')' }, peek to return empty list; even
* though K&R require at least specifier: (void)
* Set parameter-type-list = parameter-list, including the , ...
*
* As a special case, ignore evaluation when in block scope. This is to
* avoid VLA code that would be generated in cases like this:
*
* int main(void) {
* int foo(int n, int arr[][n + 1]);
* return 0;
* }
*
* The evaluation of n + 1 is done in a throwaway block, and not
* included in the CFG of main.
*/
static struct block *parameter_list(
struct definition *def,
struct block *parent,
Type base,
Type *func)
{
static String dots = SHORT_STRING_INIT("...");
String name;
size_t length;
struct block *block;
struct member *param;
struct declaration_specifier_info info;
*func = type_create_function(base);
if (current_scope_depth(&ns_ident) != 1) {
block = begin_throwaway_block(def);
} else {
block = parent;
}
while (peek() != ')') {
name = str_empty();
length = 0;
base = declaration_specifiers(&info);
if (info.storage_class) {
error("Unexpected storage class in parameter list.");
} else if (info.is_inline || info.is_noreturn) {
error("Unexpected function specifier.");
}
block = parameter_declarator(def, block, base, &base, &name, &length);
if (is_void(base)) {
if (nmembers(*func)) {
error("Incomplete type in parameter list.");
exit(1);
}
type_seal(*func);
break;
} else if (is_array(base)) {
base = type_create_pointer(type_next(base));
} else if (is_function(base)) {
base = type_create_pointer(base);
}
param = type_add_member(*func, name, base);
param->offset = length;
if (!str_is_empty(name)) {
param->sym =
sym_add(&ns_ident, name, base, SYM_DEFINITION, LINK_NONE);
}
if (!try_consume(',')) {
break;
}
if (try_consume(DOTS)) {
assert(!is_vararg(*func));
type_add_member(*func, dots, basic_type__void);
assert(is_vararg(*func));
break;
}
}
if (current_scope_depth(&ns_ident) != 1) {
restore_block(def);
} else {
parent = block;
}
return parent;
}
/*
* Old-style function definitions with separate identifiers list and
* type declarations.
*
* Return a function type where all members have placeholder type.
*/
static Type identifier_list(Type base)
{
String str;
Type type;
type = type_create_function(base);
if (peek() != ')') {
while (1) {
consume(IDENTIFIER);
str = access_token(0)->d.string;
if (get_typedef(str)) {
error("Unexpected type '%t' in identifier list.");
exit(1);
}
type_add_member(type, str, get_type_placeholder());
if (peek() == ',') {
next();
} else break;
}
}
return type;
}
struct array_param {
char is_const;
char is_volatile;
char is_restrict;
char is_static;
};
static void array_param_qualifiers(struct array_param *cvrs)
{
cvrs->is_static = try_consume(STATIC);
while (1) {
switch (peek()) {
case CONST:
cvrs->is_const = 1;
next();
continue;
case VOLATILE:
cvrs->is_volatile = 1;
next();
continue;
case RESTRICT:
cvrs->is_restrict = 1;
next();
continue;
default:
break;
}
break;
}
cvrs->is_static = cvrs->is_static || try_consume(STATIC);
}
/*
* Parse array declarations of the form [s0][s1]..[sn], resulting in
* type [s0] [s1] .. [sn] (base).
*
* Only the first dimension s0 can be unspecified, yielding an
* incomplete type. Incomplete types are represented by having size of
* zero.
*
* VLA require evaluating an expression, and storing it in a separate
* stack allocated variable.
*/
static struct block *array_declarator(
struct definition *def,
struct block *block,
Type base,
Type *type,
size_t *static_length)
{
size_t length = 0;
struct var val;
struct array_param cvrs = {0};
int is_incomplete = 0;
const struct symbol *sym = NULL;
consume('[');
if (peek() == ']') {
is_incomplete = 1;
} else {
if (!def) {
val = constant_expression();
block = cfg_block_init(NULL);
} else {
assert(block);
if (static_length) {
array_param_qualifiers(&cvrs);
if (cvrs.is_static && peek() == ']') {
error("Missing array length.");
exit(1);
}
}
if (peek() != ']') {
block = assignment_expression(def, block);
val = eval(def, block, block->expr);
} else {
is_incomplete = 1;
}
}
}
if (!is_incomplete) {
if (!is_integer(val.type)) {
error("Array dimension must be of integer type.");
exit(1);
}
if (val.kind == IMMEDIATE && is_signed(val.type) && val.value.imm.i < 0) {
error("Array dimension must be a positive number.");
exit(1);
}
if (!type_equal(val.type, basic_type__unsigned_long)) {
val = eval(def, block,
eval_cast(def, block, val, basic_type__unsigned_long));
} else if (val.kind == DIRECT && !is_temporary(val.value.symbol)) {
val = eval_copy(def, block, val);
}
assert(is_unsigned(val.type));
assert(type_equal(val.type, basic_type__unsigned_long));
block->expr = as_expr(val);
if (val.kind == IMMEDIATE) {
length = val.value.imm.u;
} else {
assert(val.kind == DIRECT);
assert(val.value.symbol);
sym = val.value.symbol;
}
}
consume(']');
if (peek() == '[') {
block = array_declarator(def, block, base, &base, NULL);
}
if (!is_complete(base)) {
error("Array has incomplete element type.");
exit(1);
}
if (static_length) {
*static_length = length;
*type = type_create_pointer(base);
if (cvrs.is_const) *type = type_set_const(*type);
if (cvrs.is_volatile) *type = type_set_volatile(*type);
if (cvrs.is_restrict) *type = type_set_restrict(*type);
} else if (is_incomplete) {
*type = type_create_incomplete(base);
} else if (sym) {
*type = type_create_vla(base, sym);
} else {
*type = type_create_array(base, length);
}
return block;
}
/*
* Parse function and array declarators.
*
* Example:
*
* void (*foo)(int)
*
* Traverse (*foo) first, and prepended on the outer `(int) -> void`,
* making it `* (int) -> void`. Void is used as a sentinel, the inner
* declarator can only produce pointer, function or array.
*/
static struct block *direct_declarator(
struct definition *def,
struct block *block,
Type base,
Type *type,
String *name,
size_t *length)
{
enum token_type t;
Type head = basic_type__void;
switch (peek()) {
case IDENTIFIER:
next();
if (!name) {
error("Unexpected identifier in abstract declarator.");
exit(1);
}
*name = access_token(0)->d.string;
break;
case '(':
t = peekn(2);
if ((t == IDENTIFIER && !get_typedef(access_token(2)->d.string))
|| t == '('
|| t == '*')
{
next();
block = declarator(def, block, head, &head, name);
consume(')');
if (!is_void(head)) {
length = NULL;
}
}
break;
default:
break;
}
switch (peek()) {
case '[':
block = array_declarator(def, block, base, type, length);
break;
case '(':
next();
t = peek();
push_scope(&ns_tag);
push_scope(&ns_ident);
if (t == IDENTIFIER && !get_typedef(access_token(1)->d.string)) {
*type = identifier_list(base);
} else {
block = parameter_list(def, block, base, type);
}
pop_scope(&ns_ident);
pop_scope(&ns_tag);
consume(')');
break;
default:
*type = base;
break;
}
if (!is_void(head)) {
*type = type_patch_declarator(head, *type);
}
return block;
}
static Type pointer(Type type)
{
type = type_create_pointer(type);
while (1) {
next();
switch (peek()) {
case CONST:
type = type_set_const(type);
break;
case VOLATILE:
type = type_set_volatile(type);
break;
case RESTRICT:
type = type_set_restrict(type);
break;
default:
return type;
}
}
}
static struct block *parameter_declarator(
struct definition *def,
struct block *block,
Type base,
Type *type,
String *name,
size_t *length)
{
assert(type);
while (peek() == '*') {
base = pointer(base);
}
return direct_declarator(def, block, base, type, name, length);
}
INTERNAL struct block *declarator(
struct definition *def,
struct block *block,
Type base,
Type *type,
String *name)
{
return parameter_declarator(def, block, base, type, name, NULL);
}
static void member_declaration_list(Type type)
{
String name;
struct var expr;
Type decl_base, decl_type;
do {
decl_base = declaration_specifiers(NULL);
while (1) {
name = str_empty();
declarator(NULL, NULL, decl_base, &decl_type, &name);
if (is_struct_or_union(type) && peek() == ':') {
if (!is_integer(decl_type)) {
error("Unsupported type '%t' for bit-field.", decl_type);
exit(1);
}
consume(':');
expr = constant_expression();
if (is_signed(expr.type) && expr.value.imm.i < 0) {
error("Negative width in bit-field.");
exit(1);
}
type_add_field(type, name, decl_type, expr.value.imm.u);
} else if (str_is_empty(name)) {
if (is_struct_or_union(decl_type)) {
type_add_anonymous_member(type, decl_type);
} else {
error("Missing name in member declarator.");
exit(1);
}
} else {
type_add_member(type, name, decl_type);
}
if (!try_consume(',')) {
break;
}
}
consume(';');
} while (peek() != '}');
type_seal(type);
}
/*
* Parse and declare a new struct or union type, or retrieve type from
* existing symbol; possibly providing a complete definition that will
* be available for later declarations.
*/
static Type struct_or_union_declaration(enum token_type t)
{
struct symbol *sym = NULL;
Type type = {0};
String name;
enum type kind;
assert(t == STRUCT || t == UNION);
kind = (t == STRUCT) ? T_STRUCT : T_UNION;
if (try_consume(IDENTIFIER)) {
name = access_token(0)->d.string;
sym = sym_lookup(&ns_tag, name);
if (!sym) {
type = type_create(kind);
sym = sym_add(&ns_tag, name, type, SYM_TAG, LINK_NONE);
} else if (is_integer(sym->type)) {
error("Tag '%s' was previously declared as enum.",
str_raw(sym->name));
exit(1);
} else if (type_of(sym->type) != kind) {
error("Tag '%s' was previously declared as %s.",
str_raw(sym->name),
(is_struct(sym->type)) ? "struct" : "union");
exit(1);
}
type = sym->type;
if (peek() == '{' && size_of(type)) {
error("Redefiniton of '%s'.", str_raw(sym->name));
exit(1);
}
}
if (try_consume('{')) {
if (!sym) {
type = type_create(kind);
}
member_declaration_list(type);
assert(size_of(type));
consume('}');
} else if (!sym) {
error("Invalid declaration.");
exit(1);
}
return type;
}
static void enumerator_list(void)
{
String name;
struct var val;
struct symbol *sym;
int count = 0;
consume('{');
do {
consume(IDENTIFIER);
name = access_token(0)->d.string;
if (try_consume('=')) {
val = constant_expression();
if (!is_integer(val.type)) {
error("Implicit conversion from non-integer type in enum.");
}
count = val.value.imm.i;
}
sym = sym_add(
&ns_ident,
name,
basic_type__int,
SYM_CONSTANT,
LINK_NONE);
sym->value.constant.i = count++;
if (!try_consume(','))
break;
} while (peek() != '}');
consume('}');
}
/*
* Consume enum definition, which represents an int type.
*
* Use value.constant as a sentinel to represent definition, checked on
* lookup to detect duplicate definitions.
*/
static void enum_declaration(void)
{
String name;
struct symbol *tag;
if (try_consume(IDENTIFIER)) {
name = access_token(0)->d.string;
tag = sym_lookup(&ns_tag, name);
if (!tag || tag->depth < current_scope_depth(&ns_tag)) {
tag = sym_add(
&ns_tag,
name,
basic_type__int,
SYM_TAG,
LINK_NONE);
} else if (!is_integer(tag->type)) {
error("Tag '%s' was previously defined as aggregate type.",
str_raw(tag->name));
exit(1);
}
if (peek() == '{') {
if (tag->value.constant.i) {
error("Redefiniton of enum '%s'.", str_raw(tag->name));
exit(1);
}
enumerator_list();
tag->value.constant.i = 1;
}
} else {
enumerator_list();
}
}
/*
* Parse type, qualifiers and storage class. Do not assume int by
* default, but require at least one type specifier. Storage class is
* returned as token value, unless the provided pointer is NULL, in
* which case the input is parsed as specifier-qualifier-list.
*
* Type specifiers must be one of the following permutations:
*
* void
* char
* signed char
* unsigned char
* short, signed short, short int, or signed short int
* unsigned short, or unsigned short int
* int, signed, or signed int
* unsigned, or unsigned int
* long, signed long, long int, or signed long int
* unsigned long, or unsigned long int
* long long, signed long long, long long int, or
* signed long long int
* unsigned long long, or unsigned long long int
* float
* double
* long double
* _Bool
* struct specifier
* union specifier
* enum specifier
* typedef name
*/
INTERNAL Type declaration_specifiers(struct declaration_specifier_info *info)
{
Type type = {0};
const Type *tagged;
enum token_type t;
enum {
B_NONE,
B_VOID,
B_BOOL,
B_CHAR,
B_INT,
B_FLOAT,
B_DOUBLE,
B_ENUM,
B_AGGREGATE
} base = 0;
enum {
M_NONE,
M_SHORT,
M_LONG,
M_LONG_LONG
} modifier = 0;
enum {
S_NONE,
S_SIGNED,
S_UNSIGNED
} sign = 0;
enum {
Q_NONE,
Q_CONST = 1,
Q_VOLATILE = 2,
Q_CONST_VOLATILE = Q_CONST | Q_VOLATILE
} qual = 0;
if (info) {
memset(info, 0, sizeof(*info));
}
while (1) {
t = peek();
switch (t) {
case VOID:
if (base || modifier || sign) goto done;
next();
base = B_VOID;
break;
case BOOL:
if (base || modifier || sign) goto done;
next();
base = B_BOOL;
break;
case CHAR:
if (base || modifier) goto done;
next();
base = B_CHAR;
break;
case SHORT:
if (modifier || (base && base != B_INT)) goto done;
next();
modifier = M_SHORT;
break;
case INT:
if (base) goto done;
next();
base = B_INT;
break;
case LONG:
if (base || (modifier && modifier != M_LONG)) goto done;
next();
if (modifier == M_LONG) {
modifier = M_LONG_LONG;
} else {
modifier = M_LONG;
}
break;
case SIGNED:
next();
if (sign == S_SIGNED) {
error("Duplicate 'signed' specifier.");
} else if (sign == S_UNSIGNED) {
error("Conflicting 'signed' and 'unsigned' specifiers.");
} else {
sign = S_SIGNED;
}
break;
case UNSIGNED:
next();
if (sign == S_UNSIGNED) {
error("Duplicate 'unsigned' specifier.");
} else if (sign == S_SIGNED) {
error("Conflicting 'signed' and 'unsigned' specifiers.");
} else {
sign = S_UNSIGNED;
}
break;
case FLOAT:
if (base || modifier || sign) goto done;
next();
base = B_FLOAT;
break;
case DOUBLE:
if (base || (modifier && modifier != M_LONG) || sign) goto done;
next();
base = B_DOUBLE;
break;
case CONST:
next();
qual |= Q_CONST;
break;
case VOLATILE:
next();
qual |= Q_VOLATILE;
break;
case IDENTIFIER:
if (base || modifier || sign) goto done;
tagged = get_typedef(access_token(1)->d.string);
if (!tagged) goto done;
next();
type = *tagged;
base = B_AGGREGATE;
if (info) {
info->from_typedef = 1;
}
break;
case UNION:
case STRUCT:
if (base || modifier || sign) goto done;
next();
type = struct_or_union_declaration(t);
base = B_AGGREGATE;
break;
case ENUM:
if (base || modifier || sign) goto done;
next();
enum_declaration();
base = B_ENUM;
break;
case INLINE:
next();
if (!info) {
error("Unexpected 'inline' specifier.");
} else if (info->is_inline) {
error("Multiple 'inline' specifiers.");
} else {
info->is_inline = 1;
}
break;
case NORETURN:
next();
if (!info) {
error("Unexpected '_Noreturn' specifier.");
} else if (info->is_noreturn) {
error("Multiple '_Noreturn' specifiers.");
} else {
info->is_noreturn = 1;
}
break;
case REGISTER:
next();
if (!info) {
error("Unexpected 'register' specifier.");
} else if (info->is_register) {
error("Multiple 'register' specifiers.");
} else {
info->is_register = 1;
}
break;
case AUTO:
case STATIC:
case EXTERN:
case TYPEDEF:
next();
if (!info) {
error("Unexpected storage class in qualifier list.");
} else if (info->storage_class) {
error("Multiple storage class specifiers.");
} else {
info->storage_class = t;
}
break;
default:
goto done;
}
}
done:
switch (base) {
case B_AGGREGATE:
break;
case B_VOID:
type.type = T_VOID;
break;
case B_BOOL:
type.type = T_BOOL;
break;
case B_CHAR:
type.type = T_CHAR;
type.is_unsigned = sign == S_UNSIGNED;
break;
case B_ENUM:
case B_NONE:
case B_INT:
type.type = T_INT;
type.is_unsigned = sign == S_UNSIGNED;
switch (modifier) {
case M_SHORT:
type.type = T_SHORT;
break;
case M_LONG:
case M_LONG_LONG:
type.type = T_LONG;
default:
break;
}
break;
case B_FLOAT:
type.type = T_FLOAT;
break;
case B_DOUBLE:
type.type = T_DOUBLE;
if (modifier == M_LONG) {
type.type = T_LDOUBLE;
}
break;
}
if (qual & Q_CONST)
type = type_set_const(type);
if (qual & Q_VOLATILE)
type = type_set_volatile(type);
return type;
}
/* Define __func__ as static const char __func__[] = sym->name;
*
* Just add the symbol directly as a special string value. No
* explicit assignment reflected in the IR.
*/
static void define_builtin__func__(String name)
{
Type type;
struct symbol *sym;
size_t len;
static String func = SHORT_STRING_INIT("__func__");
assert(current_scope_depth(&ns_ident) == 1);
if (!context.pedantic || context.standard >= STD_C99) {
len = str_len(name);
type = type_create_array(basic_type__char, len + 1);
sym = sym_add(&ns_ident, func, type, SYM_LITERAL, LINK_INTERN);
sym->value.string = name;
}
}
static void ensure_main_returns_zero(
struct symbol *sym,
struct block *block)
{
static String name = SHORT_STRING_INIT("main");
assert(is_function(sym->type));
assert(!sym->n);
if (context.standard < STD_C99 || !str_eq(name, sym->name))
return;
if (!block->has_return_value) {
block->expr = as_expr(var_int(0));
block->has_return_value = 1;
}
}
/*
* Parse old-style function definition parameter declarations if present
* before opening bracket.
*
* Verify in the end that all variables have been declared, and add to
* symbol table parameters that have not been declared old-style.
* Default to int for parameters that are given without type in the
* function signature.
*/
static struct block *parameter_declaration_list(
struct definition *def,
struct block *block,
Type type)
{
int i;
struct symbol sym = {0};
struct member *param;
assert(is_function(type));
assert(current_scope_depth(&ns_ident) == 1);
assert(!def->symbol);
sym.type = type;
def->symbol = &sym;
while (peek() != '{') {
block = declaration(def, block);
}
def->symbol = NULL;
for (i = 0; i < nmembers(type); ++i) {
param = get_member(type, i);
if (str_is_empty(param->name)) {
error("Missing parameter name at position %d.", i + 1);
exit(1);
}
assert(!param->sym);
param->sym = sym_lookup(&ns_ident, param->name);
if (!param->sym || param->sym->depth != 1) {
assert(is_type_placeholder(param->type));
param->type = basic_type__int;
param->sym = sym_add(&ns_ident,
param->name,
param->type,
SYM_DEFINITION,
LINK_NONE);
}
}
type_seal(type);
assert(is_complete(type));
return block;
}
/* Add function parameters to scope. */
static struct block *make_parameters_visible(
struct definition *def,
struct block *block)
{
int i;
struct member *param;
assert(def->symbol);
assert(is_function(def->symbol->type));
assert(current_scope_depth(&ns_ident) == 1);
for (i = 0; i < nmembers(def->symbol->type); ++i) {
param = get_member(def->symbol->type, i);
if (str_is_empty(param->name)) {
error("Missing parameter at position %d.", i + 1);
exit(1);
}
assert(param->sym);
assert(param->sym->depth == 1);
assert(!is_type_placeholder(param->type));
assert(!is_array(param->type));
sym_make_visible(&ns_ident, param->sym);
array_push_back(&def->params, param->sym);
}
return block;
}
INTERNAL struct block *declare_vla(
struct definition *def,
struct block *block,
struct symbol *sym)
{
struct symbol *addr;
assert(is_vla(sym->type));
addr = sym_create_temporary(type_create_pointer(type_next(sym->type)));
array_push_back(&def->locals, addr);
sym->value.vla_address = addr;
eval_vla_alloc(def, block, sym);
return block;
}
/*
* Parse declaration, possibly with initializer. New symbols are added