-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_type.c
542 lines (473 loc) · 15.5 KB
/
parse_type.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
#include "header.h"
#include "std.h"
#include "std_io.h"
/***************************************
* pure parsers with respect to types. *
***************************************/
static void parse_declarator(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str,
struct Vector /*<struct TypeNode>*/ *ptr_vec);
static void skip_consts_or_noreturns(const struct Token **ptr_tokvec)
{
const struct Token *tokvec = *ptr_tokvec;
while (tokvec[0].kind == RES_CONST || tokvec[0].kind == RES_NORETURN) {
++tokvec;
}
*ptr_tokvec = tokvec;
return;
}
#define TypeNode Type
/* struct TypeNode denotes that the link list is incomplete and should not be
brought outside */
static struct Type from_type3_to_type(const void **type3)
{
struct Type type;
const struct TypeNode *ptr_elem = type3[0];
struct TypeNode elem = *ptr_elem;
type = elem;
switch (elem.type_category) {
case INT_:
case CHAR_:
case STRUCT_NOT_UNION:
case UNION:
case VOID_:
case ENUM_:
return type;
case PTR_:
case ARRAY:
case FN: {
struct Type *ptr_to_current_type = calloc(1, sizeof(struct Type));
*ptr_to_current_type = from_type3_to_type(type3 + 1);
type.derived_from = ptr_to_current_type;
if (type.type_category == ARRAY &&
type.derived_from->type_category == FN) {
fprintf(stderr, "It is illegal to declare an array of functions\n");
exit(EXIT_FAILURE);
} else if (type.type_category == FN &&
type.derived_from->type_category == FN) {
fprintf(stderr,
"It is illegal for a function to return a function type\n");
exit(EXIT_FAILURE);
} else if (type.type_category == FN &&
type.derived_from->type_category == ARRAY) {
fprintf(stderr,
"It is illegal for a function to return an array type\n");
exit(EXIT_FAILURE);
}
return type;
}
}
fprintf(stderr,
"****************************\n"
"* INTERNAL COMPILER ERROR @ %s\n"
"* Unexpected value of TypeCategory: `elem.type_category` is `%d`\n"
"****************************\n",
__func__, elem.type_category);
exit(EXIT_FAILURE);
}
/*
parameter-declaration:
declaration-specifiers declarator
declaration-specifiers abstract-declarator_opt
*/
struct TypeAndIdent *
parse_parameter_declaration(const struct Token **ptr_tokvec)
{
const char *ident_str = 0; /* null when abstract */
struct TypeAndIdent *ptr_param_info =
calloc(1, sizeof(struct TypeAndIdent));
struct Type type;
{
struct Type *ptr_base_type = parse_type_specifier(ptr_tokvec);
struct Vector /*<struct TypeNode>*/ *ptr_vec = init_vector_();
if ((*ptr_tokvec)[0].kind == OP_COMMA ||
(*ptr_tokvec)[0].kind == RIGHT_PAREN) {
/* comma or right paren signifies the immediate end of
* parameter_declaration */
} else {
parse_declarator(ptr_tokvec, &ident_str, ptr_vec);
if ((0)) {
unsupported("abstract-declarator inside parameter declaration");
}
}
push_vector(ptr_vec, ptr_base_type);
type = from_type3_to_type(ptr_vec->vector);
}
if (type.type_category == FN) {
/* shall be adjusted to `pointer to func`, according to the spec */
ptr_param_info->type = ptr_to_type(&type);
} else {
/* convert to pointer */
if (type.type_category == ARRAY) {
type.type_category = PTR_;
}
ptr_param_info->type = type;
}
ptr_param_info->ident_str = ident_str;
return ptr_param_info;
}
struct Type *
try_parse_type_specifier_and_semicolon(const struct Token **ptr_tokvec)
{
const struct Token *tokvec3 = *ptr_tokvec;
struct Type *ptr_type = parse_type_specifier(&tokvec3);
if (tokvec3[0].kind != SEMICOLON) {
return 0;
}
++tokvec3;
*ptr_tokvec = tokvec3;
return ptr_type;
}
struct Type *parse_type_specifier(const struct Token **ptr_tokvec)
{
const struct Token *tokvec = *ptr_tokvec;
skip_consts_or_noreturns(&tokvec);
enum TokenKind tok = tokvec[0].kind;
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
if (tok == RES_CHAR) {
ptr->type_category = CHAR_;
++tokvec;
} else if (tok == RES_INT) {
ptr->type_category = INT_;
++tokvec;
} else if (tok == RES_VOID) {
ptr->type_category = VOID_;
++tokvec;
} else if (tok == RES_STRUCT || tok == RES_UNION) {
++tokvec;
expect_and_consume(&tokvec, IDENT_OR_RESERVED,
"identifier after `struct` or `union`");
const char *ident = tokvec[-1].ident_str;
ptr->type_category = tok == RES_STRUCT ? STRUCT_NOT_UNION : UNION;
ptr->s.struct_or_union_tag = ident;
if (tokvec[0].kind != LEFT_BRACE) {
ptr->s.struct_or_union_info.ptr_types_and_idents =
0; /* crucial; no info */
skip_consts_or_noreturns(&tokvec);
*ptr_tokvec = tokvec;
return ptr;
}
++tokvec;
ptr->s.struct_or_union_info.ptr_types_and_idents = init_vector_();
while (1) {
if (tokvec[0].kind == RIGHT_BRACE) {
++tokvec;
break;
}
const char *ident_str;
struct Type t = parse_struct_declaration(&tokvec, &ident_str);
expect_and_consume(
&tokvec, SEMICOLON,
"semicolon after the declarator inside struct or union");
struct TypeAndIdent *ptr_t_and_i =
calloc(1, sizeof(struct TypeAndIdent));
ptr_t_and_i->type = t;
ptr_t_and_i->ident_str = ident_str;
push_vector(ptr->s.struct_or_union_info.ptr_types_and_idents,
ptr_t_and_i);
}
} else if (tok == RES_ENUM) {
++tokvec;
expect_and_consume(&tokvec, IDENT_OR_RESERVED,
"identifier after `enum`");
const char *ident = tokvec[-1].ident_str;
ptr->type_category = ENUM_;
ptr->e.enum_tag = ident;
if (tokvec[0].kind != LEFT_BRACE) {
ptr->e.enum_info.ptr_enumerators = 0; /* crucial; no info */
skip_consts_or_noreturns(&tokvec);
*ptr_tokvec = tokvec;
return ptr;
}
++tokvec;
ptr->e.enum_info.ptr_enumerators = init_vector_();
do { /* at least one enumerator is needed */
expect_and_consume(&tokvec, IDENT_OR_RESERVED,
"identifier as a declaration of an enumerator");
const char *ident_str = tokvec[-1].ident_str;
push_vector(ptr->e.enum_info.ptr_enumerators, ident_str);
/* ending without comma */
if (tokvec[0].kind == RIGHT_BRACE) {
++tokvec;
break;
} else if (tokvec[0].kind == OP_COMMA) {
++tokvec;
if (tokvec[0].kind == RIGHT_BRACE) {
++tokvec;
break;
}
}
} while (1);
} else {
error_unexpected_token(
tokvec, "type name `int`, `char`, `void`, `struct` or `enum`");
}
skip_consts_or_noreturns(&tokvec);
*ptr_tokvec = tokvec;
return ptr;
}
static void
parse_parameter_type_list(const struct Token **ptr_tokvec,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
assert((*ptr_tokvec)[-1].kind == LEFT_PAREN);
const struct Token *tokvec = *ptr_tokvec;
struct Vector vec = *ptr_vec;
{
if (tokvec[0].kind == RIGHT_PAREN) { /* NO INFO */
struct TypeNode f;
f.type_category = FN;
f.param_infos_validity = INVALID;
f.param_infos = init_vector();
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
*ptr = f;
push_vector(&vec, ptr);
} else if (tokvec[0].kind == RES_VOID &&
tokvec[1].kind == RIGHT_PAREN) { /* EXPLICITLY EMPTY */
tokvec += 1;
struct TypeNode f;
f.type_category = FN;
f.param_infos_validity = VALID;
f.param_infos = init_vector();
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
*ptr = f;
push_vector(&vec, ptr);
} else if (can_start_a_type(tokvec)) {
struct TypeNode f;
f.type_category = FN;
f.param_infos_validity = VALID;
f.param_infos = init_vector();
push_vector(&f.param_infos, parse_parameter_declaration(&tokvec));
while (1) {
enum TokenKind kind = tokvec[0].kind;
if (kind != OP_COMMA) {
break;
}
++tokvec;
if (tokvec[0].kind == TRIPLE_DOT) {
f.param_infos_validity = VA_ARGS;
++tokvec;
break;
} else {
push_vector(&f.param_infos,
parse_parameter_declaration(&tokvec));
}
}
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
*ptr = f;
push_vector(&vec, ptr);
} else {
error_unexpected_token(tokvec,
"while parsing function parameter list");
}
}
*ptr_tokvec = tokvec;
*ptr_vec = vec;
}
static void parse_dcl_postfixes(const struct Token **ptr_tokvec,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
const struct Token *tokvec = *ptr_tokvec;
struct Vector vec = *ptr_vec;
// The parser must handle cases like `int test()[3];` (which tries to define
// an array of functions) or `int test()()` (which tries to define a
// function returning a function). While these declarations are semantically
// forbidden, they are syntactically legal.
while (1) {
if (tokvec[0].kind == LEFT_BRACKET) {
++tokvec;
expect_and_consume(&tokvec, LIT_DEC_INTEGER,
"an integer while parsing a declaration");
int length = tokvec[-1].int_value;
expect_and_consume(&tokvec, RIGHT_BRACKET,
"closing ] while parsing a declaration");
struct TypeNode a;
a.type_category = ARRAY;
a.array_length = length;
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
*ptr = a;
push_vector(&vec, ptr);
continue;
} else if (tokvec[0].kind == LEFT_PAREN &&
(can_start_a_type(&tokvec[1]) ||
tokvec[1].kind == RIGHT_PAREN)) {
++tokvec;
parse_parameter_type_list(&tokvec, &vec);
expect_and_consume(&tokvec, RIGHT_PAREN,
"closing ) while parsing functional type");
continue;
} else {
break;
}
}
*ptr_tokvec = tokvec;
*ptr_vec = vec;
}
static void
parse_direct_declarator(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
const struct Token *tokvec = *ptr_tokvec;
struct Vector vec = *ptr_vec;
if (tokvec[0].kind == LEFT_PAREN) {
++tokvec;
parse_declarator(&tokvec, ptr_to_ident_str, &vec);
expect_and_consume(&tokvec, RIGHT_PAREN,
"closing parenthesis inside direct declarator");
*ptr_tokvec = tokvec;
} else if (tokvec[0].kind == IDENT_OR_RESERVED) {
*ptr_to_ident_str = tokvec[0].ident_str;
++tokvec;
} else {
error_unexpected_token(tokvec, "an identifier in the declarator");
}
parse_dcl_postfixes(&tokvec, &vec);
*ptr_tokvec = tokvec;
*ptr_vec = vec;
}
static void parse_declarator(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
int asterisk_num = 0;
for (; (*ptr_tokvec)[0].kind == OP_ASTERISK;
++*ptr_tokvec, skip_consts_or_noreturns(ptr_tokvec)) {
asterisk_num++;
}
parse_direct_declarator(ptr_tokvec, ptr_to_ident_str, ptr_vec);
while (asterisk_num-- > 0) {
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
ptr->type_category = PTR_;
push_vector(ptr_vec, ptr);
}
}
struct UntypedExpr *
parse_init_declarator(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
struct UntypedExpr *ptr_expr = 0;
parse_declarator(ptr_tokvec, ptr_to_ident_str, ptr_vec);
if ((*ptr_tokvec)[0].kind == OP_EQ) {
++*ptr_tokvec;
ptr_expr = calloc(1, sizeof(struct UntypedExpr));
*ptr_expr = parse_assignment_expression(ptr_tokvec);
}
return ptr_expr;
}
int can_start_a_type(const struct Token *tokvec)
{
return tokvec[0].kind == RES_INT || tokvec[0].kind == RES_CHAR ||
tokvec[0].kind == RES_STRUCT || tokvec[0].kind == RES_VOID ||
tokvec[0].kind == RES_ENUM || tokvec[0].kind == RES_CONST ||
tokvec[0].kind == RES_NORETURN || tokvec[0].kind == RES_UNION;
}
/* `int a`, `int *a` */
struct Type parse_struct_declaration(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str)
{
struct Type *ptr_base_type = parse_type_specifier(ptr_tokvec);
struct Vector /*<struct TypeNode>*/ *ptr_vec = init_vector_();
parse_declarator(ptr_tokvec, ptr_to_ident_str, ptr_vec);
push_vector(ptr_vec, ptr_base_type);
return from_type3_to_type(ptr_vec->vector);
}
/* `int a;`, `int *a;`, `int a = 5;` */
struct Type parse_declaration(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str,
struct UntypedExpr **ptr_ptr_uexpr)
{
struct Type *ptr_base_type = parse_type_specifier(ptr_tokvec);
struct Vector /*<struct TypeNode>*/ *ptr_vec = init_vector_();
struct UntypedExpr *ptr_uexpr = parse_init_declarator(
ptr_tokvec, ptr_to_ident_str, ptr_vec); /* nullable */
*ptr_ptr_uexpr = ptr_uexpr;
push_vector(ptr_vec, ptr_base_type);
expect_and_consume(
ptr_tokvec, SEMICOLON,
"semicolon at the end of variable definition/declaration");
return from_type3_to_type(ptr_vec->vector);
}
struct Type parse_type_specifier_and_declarator(const struct Token **ptr_tokvec,
const char **ptr_to_ident_str)
{
struct Type *ptr_base_type = parse_type_specifier(ptr_tokvec);
struct Vector /*<struct TypeNode>*/ *ptr_vec = init_vector_();
parse_declarator(ptr_tokvec, ptr_to_ident_str, ptr_vec);
push_vector(ptr_vec, ptr_base_type);
return from_type3_to_type(ptr_vec->vector);
}
/*
type-name:
type-specifier abstract-declarator_opt
abstract-declarator:
pointer
pointer_opt direct-abstract-declarator
direct-abstract-declarator:
( abstract-declarator )
direct-abstract-declarator_opt [ constant-expression_opt ]
direct-abstract-declarator_opt ( parameter-type-list_opt )
*/
static void
parse_abstract_declarator(const struct Token **ptr_tokvec,
struct Vector /*<struct TypeNode>*/ *ptr_vec);
static void
parse_direct_abstract_declarator(const struct Token **ptr_tokvec,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
const struct Token *tokvec = *ptr_tokvec;
struct Vector vec = *ptr_vec;
if (tokvec[0].kind == LEFT_PAREN && !can_start_a_type(&tokvec[1]) &&
tokvec[1].kind !=
RIGHT_PAREN) { /* an empty parenthesis must be of a function */
++tokvec;
parse_abstract_declarator(&tokvec, &vec);
expect_and_consume(&tokvec, RIGHT_PAREN,
"closing parenthesis inside direct declarator");
}
parse_dcl_postfixes(&tokvec, &vec);
*ptr_tokvec = tokvec;
*ptr_vec = vec;
}
static void
parse_abstract_declarator(const struct Token **ptr_tokvec,
struct Vector /*<struct TypeNode>*/ *ptr_vec)
{
const struct Token *tokvec = *ptr_tokvec;
if (tokvec[0].kind != OP_ASTERISK) {
parse_direct_abstract_declarator(&tokvec, ptr_vec);
*ptr_tokvec = tokvec;
return;
}
int asterisk_num = 0;
for (; tokvec[0].kind == OP_ASTERISK;
++tokvec, skip_consts_or_noreturns(&tokvec)) {
asterisk_num++;
}
/* optional */
if (tokvec[0].kind == LEFT_PAREN || tokvec[0].kind == LEFT_BRACKET) {
parse_direct_abstract_declarator(&tokvec, ptr_vec);
}
while (asterisk_num-- > 0) {
struct TypeNode *ptr = calloc(1, sizeof(struct TypeNode));
ptr->type_category = PTR_;
push_vector(ptr_vec, ptr);
}
*ptr_tokvec = tokvec;
}
/* `int `, `int *` */
struct Type parse_type_name(const struct Token **ptr_tokvec)
{
struct Type *ptr_base_type = parse_type_specifier(ptr_tokvec);
struct Vector /*<struct TypeNode>*/ *ptr_vec = init_vector_();
const struct Token *tokvec = *ptr_tokvec;
/* optional */
if (tokvec[0].kind == OP_ASTERISK || tokvec[0].kind == LEFT_PAREN ||
tokvec[0].kind == LEFT_BRACKET) {
parse_abstract_declarator(&tokvec, ptr_vec);
}
push_vector(ptr_vec, ptr_base_type);
*ptr_tokvec = tokvec;
return from_type3_to_type(ptr_vec->vector);
}