13 static ast_t *parse_expression(void);
14 static ast_t *parse_expression_postfix(void);
15 static ast_t *parse_expression_multiplicative(void);
16 static ast_t *parse_expression_conditional(void);
17 static ast_t *parse_expression_cast(void);
18 static data_type_t *parse_expression_cast_type(void);
19 static ast_t *parse_expression_unary(void);
20 static ast_t *parse_expression_comma(void);
22 static ast_t *parse_statement_compound(void);
23 static void parse_statement_declaration(list_t *);
24 static ast_t *parse_statement(void);
27 static data_type_t *parse_declaration_specification(storage_t *);
28 static data_type_t *parse_declarator(char **, data_type_t *, list_t *, cdecl_t);
29 static void parse_declaration(list_t *, ast_t *(*)(data_type_t *, char *));
31 static data_type_t *parse_function_parameter(char **, bool);
32 static data_type_t *parse_function_parameters(list_t *, data_type_t *);
35 table_t *parse_typedefs = &SENTINEL_TABLE;
37 static bool parse_type_check(lexer_token_t *token);
39 static void parse_semantic_lvalue(ast_t *ast) {
41 case AST_TYPE_VAR_LOCAL:
42 case AST_TYPE_VAR_GLOBAL:
43 case AST_TYPE_DEREFERENCE:
47 compile_error("expected lvalue, `%s' isn't a valid lvalue", ast_string(ast));
50 static void parse_semantic_notvoid(data_type_t *type) {
51 if (type->type == TYPE_VOID)
52 compile_error("void not allowed in expression");
55 void parse_expect(char punct) {
56 lexer_token_t *token = lexer_next();
57 if (!lexer_ispunct(token, punct))
58 compile_error("expected `%c`, got %s instead", punct, lexer_token_string(token));
61 void parse_semantic_assignable(data_type_t *to, data_type_t *from) {
62 from = ast_array_convert(from);
63 if ((conv_capable(to) || to->type == TYPE_POINTER) &&
64 (conv_capable(from) || from->type == TYPE_POINTER)) {
69 if (ast_struct_compare(to, from))
72 compile_error("incompatible types '%s' and '%s' in assignment", ast_type_string(to), ast_type_string(from));
75 static bool parse_identifer_check(lexer_token_t *token, const char *identifier) {
76 return token->type == LEXER_TOKEN_IDENTIFIER && !strcmp(token->string, identifier);
79 long parse_evaluate(ast_t *ast);
80 int parse_evaluate_offsetof(ast_t *ast, int offset) {
81 if (ast->type == AST_TYPE_STRUCT)
82 return parse_evaluate_offsetof(ast->structure, ast->ctype->offset + offset);
83 return parse_evaluate(ast) + offset;
86 long parse_evaluate(ast_t *ast) {
90 case AST_TYPE_LITERAL:
91 if (ast_type_isinteger(ast->ctype))
93 compile_error("not a valid integer constant expression `%s'", ast_string(ast));
96 case '+': return parse_evaluate(ast->left) + parse_evaluate(ast->right);
97 case '-': return parse_evaluate(ast->left) - parse_evaluate(ast->right);
98 case '*': return parse_evaluate(ast->left) * parse_evaluate(ast->right);
99 case '/': return parse_evaluate(ast->left) / parse_evaluate(ast->right);
100 case '<': return parse_evaluate(ast->left) < parse_evaluate(ast->right);
101 case '>': return parse_evaluate(ast->left) > parse_evaluate(ast->right);
102 case '^': return parse_evaluate(ast->left) ^ parse_evaluate(ast->right);
103 case '%': return parse_evaluate(ast->left) % parse_evaluate(ast->right);
104 case '|': return parse_evaluate(ast->left) | parse_evaluate(ast->right);
106 case AST_TYPE_AND: return parse_evaluate(ast->left) && parse_evaluate(ast->right);
107 case AST_TYPE_OR: return parse_evaluate(ast->left) || parse_evaluate(ast->right);
108 case AST_TYPE_EQUAL: return parse_evaluate(ast->left) == parse_evaluate(ast->right);
109 case AST_TYPE_LEQUAL: return parse_evaluate(ast->left) <= parse_evaluate(ast->right);
110 case AST_TYPE_GEQUAL: return parse_evaluate(ast->left) >= parse_evaluate(ast->right);
111 case AST_TYPE_NEQUAL: return parse_evaluate(ast->left) != parse_evaluate(ast->right);
112 case AST_TYPE_LSHIFT: return parse_evaluate(ast->left) << parse_evaluate(ast->right);
113 case AST_TYPE_RSHIFT: return parse_evaluate(ast->left) >> parse_evaluate(ast->right);
116 /* Deal with unary operations differently */
117 case '!': return !parse_evaluate(ast->unary.operand);
118 case '~': return ~parse_evaluate(ast->unary.operand);
119 case AST_TYPE_NEGATE: return -parse_evaluate(ast->unary.operand);
120 case AST_TYPE_EXPRESSION_CAST: return parse_evaluate(ast->unary.operand);
123 case AST_TYPE_EXPRESSION_TERNARY:
124 cond = parse_evaluate(ast->ifstmt.cond);
126 return ast->ifstmt.then ? parse_evaluate(ast->ifstmt.cond) : cond;
127 return parse_evaluate(ast->ifstmt.last);
129 /* Deal with logical right shift specifically */
130 case AST_TYPE_LRSHIFT:
131 return ((unsigned long)parse_evaluate(ast->left)) >> parse_evaluate(ast->right);
133 case AST_TYPE_CONVERT:
134 return parse_evaluate(ast->unary.operand);
137 * Allow constant evaluation for things like offsetof, although
138 * we could just implement __builtin_offsetof, this works easier
139 * and also allows the existing idiom to work.
141 case AST_TYPE_ADDRESS:
142 if (ast->unary.operand->type == AST_TYPE_STRUCT)
143 return parse_evaluate_offsetof(ast->unary.operand, 0);
146 case AST_TYPE_DEREFERENCE:
147 if (ast->unary.operand->ctype->type == TYPE_POINTER)
148 return parse_evaluate(ast->unary.operand);
153 compile_error("not a valid integer constant expression `%s'", ast_string(ast));
158 bool parse_next(int ch) {
159 lexer_token_t *token = lexer_next();
160 if (lexer_ispunct(token, ch))
166 static list_t *parse_function_args(list_t *parameters) {
167 list_t *list = list_create();
168 list_iterator_t *it = list_iterator(parameters);
172 ast_t *arg = parse_expression_assignment();
173 data_type_t *type = list_iterator_next(it);
176 type = ast_type_isfloating(arg->ctype)
177 ? ast_data_table[AST_DATA_DOUBLE]
178 : ast_type_isinteger(arg->ctype)
179 ? ast_data_table[AST_DATA_INT]
183 // todo semantic check
184 parse_semantic_assignable(ast_array_convert(type), ast_array_convert(arg->ctype));
186 if (type->type != arg->ctype->type)
187 arg = ast_type_convert(type, arg);
189 list_push(list, arg);
191 lexer_token_t *token = lexer_next();
192 if (lexer_ispunct(token, ')'))
194 if (!lexer_ispunct(token, ','))
195 compile_error("unexpected token `%s'", lexer_token_string(token));
200 static ast_t *parse_va_start(void) {
201 ast_t *ap = parse_expression_assignment();
203 return ast_va_start(ap);
206 static ast_t *parse_va_arg(void) {
207 ast_t *ap = parse_expression_assignment();
209 data_type_t *type = parse_expression_cast_type();
211 return ast_va_arg(type, ap);
214 static ast_t *parse_function_call(char *name, ast_t *function) {
215 if (!strcmp(name, "__builtin_va_start"))
216 return parse_va_start();
217 if (!strcmp(name, "__builtin_va_arg"))
218 return parse_va_arg();
221 data_type_t *declaration = function->ctype;
222 if (declaration->type != TYPE_FUNCTION)
223 compile_error("expected a function name, `%s' isn't a function", name);
224 list_t *list = parse_function_args(declaration->parameters);
225 return ast_call(declaration, name, list);
227 compile_warn("implicit declaration of function ‘%s’", name);
228 list_t *list = parse_function_args(&SENTINEL_LIST);
229 return ast_call(ast_prototype(ast_data_table[AST_DATA_INT], list_create(), true), name, list);
232 static ast_t *parse_function_pointer_call(ast_t *function) {
233 list_t *list = parse_function_args(function->ctype->pointer->parameters);
234 return ast_pointercall(function, list);
237 static ast_t *parse_generic(char *name) {
238 ast_t *ast = table_find(ast_localenv ? ast_localenv : ast_globalenv, name);
240 if (!ast || ast->ctype->type == TYPE_FUNCTION)
241 return ast_designator(name, ast);
246 static ast_t *parse_number_integer(char *string) {
250 if (!strncasecmp(string, "0x", 2)) {
254 } else if (!strncasecmp(string, "0b", 2)){
255 if (!opt_extension_test(EXTENSION_BINARYCONSTANTS))
256 compile_error("binary constants only supported in -std=lice or -std=gnuc");
260 } else if (string[0] == '0' && string[1] != '\0') {
266 while (isxdigit(*p)) {
267 if (base == 10 && isalpha(*p))
268 compile_error("invalid character in decimal literal");
269 if (base == 8 && !('0' <= *p && *p <= '7'))
270 compile_error("invalid character in octal literal");
271 if (base == 2 && (*p != '0' && *p != '1'))
272 compile_error("invalid character in binary literal");
276 if (!strcasecmp(p, "l"))
277 return ast_new_integer(ast_data_table[AST_DATA_LONG], strtol(string, NULL, base));
278 if (!strcasecmp(p, "ul") || !strcasecmp(p, "lu") || !strcasecmp(p, "u"))
279 return ast_new_integer(ast_data_table[AST_DATA_ULONG], strtoul(string, NULL, base));
280 if (!strcasecmp(p, "ll"))
281 return ast_new_integer(ast_data_table[AST_DATA_LLONG], strtoll(string, NULL, base));
282 if (!strcasecmp(p, "ull") || !strcasecmp(p, "llu"))
283 return ast_new_integer(ast_data_table[AST_DATA_ULLONG], strtoull(string, NULL, base));
285 compile_error("invalid suffix for literal");
287 long value = strtol(digits, NULL, base);
288 return (value & ~(long)UINT_MAX)
289 ? ast_new_integer(ast_data_table[AST_DATA_LONG], value)
290 : ast_new_integer(ast_data_table[AST_DATA_INT], value);
293 static ast_t *parse_number_floating(char *string) {
301 if (*p == 'l' || *p == 'L')
302 ast = ast_new_floating(ast_data_table[AST_DATA_LDOUBLE], strtold(string, &end));
303 else if (*p == 'f' || *p == 'F')
304 ast = ast_new_floating(ast_data_table[AST_DATA_FLOAT], strtof(string, &end));
306 ast = ast_new_floating(ast_data_table[AST_DATA_DOUBLE], strtod(string, &end));
311 compile_error("malformatted float literal");
316 static ast_t *parse_number(char *string) {
317 return strpbrk(string, ".pP") ||
319 * 0xe.. is integer type, sadly it's hard to check for that
320 * without additonal logic.
322 (strncasecmp(string, "0x", 2) && strpbrk(string, "eE"))
323 ? parse_number_floating(string)
324 : parse_number_integer(string);
327 static int parse_operation_reclassify(int punct) {
329 case LEXER_TOKEN_LSHIFT: return AST_TYPE_LSHIFT;
330 case LEXER_TOKEN_RSHIFT: return AST_TYPE_RSHIFT;
331 case LEXER_TOKEN_EQUAL: return AST_TYPE_EQUAL;
332 case LEXER_TOKEN_GEQUAL: return AST_TYPE_GEQUAL;
333 case LEXER_TOKEN_LEQUAL: return AST_TYPE_LEQUAL;
334 case LEXER_TOKEN_NEQUAL: return AST_TYPE_NEQUAL;
335 case LEXER_TOKEN_AND: return AST_TYPE_AND;
336 case LEXER_TOKEN_OR: return AST_TYPE_OR;
343 static int parse_operation_compound_operator(lexer_token_t *token) {
344 if (token->type != LEXER_TOKEN_PUNCT)
347 switch (token->punct) {
348 case LEXER_TOKEN_COMPOUND_RSHIFT: return LEXER_TOKEN_RSHIFT;
349 case LEXER_TOKEN_COMPOUND_LSHIFT: return LEXER_TOKEN_LSHIFT;
350 case LEXER_TOKEN_COMPOUND_ADD: return '+';
351 case LEXER_TOKEN_COMPOUND_AND: return '&';
352 case LEXER_TOKEN_COMPOUND_DIV: return '/';
353 case LEXER_TOKEN_COMPOUND_MOD: return '%';
354 case LEXER_TOKEN_COMPOUND_MUL: return '*';
355 case LEXER_TOKEN_COMPOUND_OR: return '|';
356 case LEXER_TOKEN_COMPOUND_SUB: return '-';
357 case LEXER_TOKEN_COMPOUND_XOR: return '^';
364 static ast_t *parse_expression_statement(void) {
365 ast_t *ast = parse_statement_compound();
367 data_type_t *type = ast_data_table[AST_DATA_VOID];
368 if (list_length(ast->compound) > 0) {
369 ast_t *last = list_tail(ast->compound);
377 static ast_t *parse_expression_primary(void) {
378 lexer_token_t *token;
380 if (!(token = lexer_next()))
383 if (lexer_ispunct(token, '(')) {
385 if (parse_next('{')) {
386 if (!opt_extension_test(EXTENSION_STATEMENTEXPRS))
387 compile_error("statement expressions only supported in -std=gnuc or -std=licec");
388 return parse_expression_statement();
391 ast_t *ast = parse_expression();
396 switch (token->type) {
397 case LEXER_TOKEN_IDENTIFIER:
398 return parse_generic(token->string);
399 case LEXER_TOKEN_NUMBER:
400 return parse_number(token->string);
401 case LEXER_TOKEN_CHAR:
402 return ast_new_integer(ast_data_table[AST_DATA_INT], token->character);
403 case LEXER_TOKEN_STRING:
404 return ast_new_string(token->string);
405 case LEXER_TOKEN_PUNCT:
412 compile_ice("parse_expression_primary");
416 static ast_t *parse_expression_subscript(ast_t *ast) {
417 ast_t *subscript = parse_expression();
419 compile_error("expected subscript operand");
421 ast_t *node = conv_usual('+', ast, subscript);
422 return ast_new_unary(AST_TYPE_DEREFERENCE, node->ctype->pointer, node);
425 static data_type_t *parse_sizeof_operand(void) {
426 lexer_token_t *token = lexer_next();
428 if (lexer_ispunct(token, '(') && parse_type_check(lexer_peek())) {
429 data_type_t *next = parse_function_parameter(NULL, true);
435 ast_t *expression = parse_expression_unary();
437 if (opt_std_test(STANDARD_GNUC) || opt_std_test(STANDARD_LICEC)) {
438 if (expression->type == AST_TYPE_DESIGNATOR)
439 return expression->function.call.functionpointer->ctype;
440 return expression->ctype;
442 if (expression->ctype->size == 0)
443 compile_error("invalid operand to sizeof operator");
444 return expression->ctype;
450 static ast_t *parse_sizeof(void) {
451 data_type_t *type = parse_sizeof_operand();
452 if (type->type == TYPE_VOID || type->type == TYPE_FUNCTION)
453 return ast_new_integer(ast_data_table[AST_DATA_LONG], 1);
454 return ast_new_integer(ast_data_table[AST_DATA_LONG], type->size);
457 static int parse_alignment(data_type_t *type) {
458 int size = type->type == TYPE_ARRAY ? type->pointer->size : type->size;
459 return MIN(size, ARCH_ALIGNMENT);
462 static ast_t *parse_alignof(void) {
464 data_type_t *type = parse_function_parameter(NULL, true);
466 return ast_new_integer(ast_data_table[AST_DATA_LONG], parse_alignment(type));
469 static ast_t *parse_structure_field(ast_t *structure) {
470 if (structure->ctype->type != TYPE_STRUCTURE)
471 compile_error("expected structure type, `%s' isn't structure type", ast_string(structure));
472 lexer_token_t *name = lexer_next();
473 if (name->type != LEXER_TOKEN_IDENTIFIER)
474 compile_error("expected field name, got `%s' instead", lexer_token_string(name));
476 data_type_t *field = table_find(structure->ctype->fields, name->string);
478 compile_error("structure has no such field `%s'", lexer_token_string(name));
479 return ast_structure_reference(field, structure, name->string);
482 static void parse_static_assert(void) {
484 int eval = parse_expression_evaluate();
486 lexer_token_t *token = lexer_next();
487 if (token->type != LEXER_TOKEN_STRING)
488 compile_error("expected string");
492 compile_error("static assertion failed: %s", token->string);
495 static ast_t *parse_label_address(void) {
496 lexer_token_t *token = lexer_next();
497 if (token->type != LEXER_TOKEN_IDENTIFIER)
498 compile_error("expected identifier");
499 ast_t *address = ast_label_address(token->string);
500 list_push(ast_gotos, address);
504 static ast_t *parse_expression_postfix_tail(ast_t *ast) {
508 if (parse_next('(')) {
509 data_type_t *type = ast->ctype;
510 if (type->type == TYPE_POINTER && type->pointer->type == TYPE_FUNCTION)
511 return parse_function_pointer_call(ast);
512 if (ast->type != AST_TYPE_DESIGNATOR)
513 compile_error("expected function name");
514 ast = parse_function_call(ast->function.name, ast->function.call.functionpointer);
517 if (ast->type == AST_TYPE_DESIGNATOR && !ast->function.call.functionpointer)
518 compile_error("undefined variable: %s", ast->function.name);
520 if (parse_next('[')) {
521 ast = parse_expression_subscript(ast);
525 if (parse_next('.')) {
526 ast = parse_structure_field(ast);
530 if (parse_next(LEXER_TOKEN_ARROW)) {
531 if (ast->ctype->type != TYPE_POINTER)
532 compile_error("expected pointer type");
533 ast = ast_new_unary(AST_TYPE_DEREFERENCE, ast->ctype->pointer, ast);
534 ast = parse_structure_field(ast);
538 lexer_token_t *peek = lexer_peek();
539 if (parse_next(LEXER_TOKEN_INCREMENT) || parse_next(LEXER_TOKEN_DECREMENT)) {
540 parse_semantic_lvalue(ast);
541 int operation = lexer_ispunct(peek, LEXER_TOKEN_INCREMENT)
542 ? AST_TYPE_POST_INCREMENT
543 : AST_TYPE_POST_DECREMENT;
545 return ast_new_unary(operation, ast->ctype, ast);
554 static ast_t *parse_expression_postfix(void) {
555 return parse_expression_postfix_tail(parse_expression_primary());
558 static ast_t *parse_expression_unary_address(void) {
559 ast_t *operand = parse_expression_cast();
560 if (operand->type == AST_TYPE_DESIGNATOR)
561 return ast_designator_convert(operand);
562 parse_semantic_lvalue(operand);
563 return ast_new_unary(AST_TYPE_ADDRESS, ast_pointer(operand->ctype), operand);
566 static ast_t *parse_expression_unary_deref(void) {
567 ast_t *operand = parse_expression_cast();
568 operand = ast_designator_convert(operand);
569 data_type_t *type = ast_array_convert(operand->ctype);
570 if (type->type != TYPE_POINTER)
571 compile_error("invalid type argument of unary ‘*’ (have ‘%s’)", ast_type_string(type));
572 if (type->pointer->type == TYPE_FUNCTION)
575 return ast_new_unary(AST_TYPE_DEREFERENCE, operand->ctype->pointer, operand);
578 static ast_t *parse_expression_unary_plus(void) {
579 return parse_expression_cast();
582 static ast_t *parse_expression_unary_minus(void) {
583 ast_t *expression = parse_expression_cast();
585 return ast_new_unary(AST_TYPE_NEGATE, expression->ctype, expression);
588 static ast_t *parse_expression_unary_bitcomp(void) {
589 ast_t *expression = parse_expression_cast();
590 expression = ast_designator_convert(expression);
591 if (!ast_type_isinteger(expression->ctype))
592 compile_error("invalid argument type ‘%s‘ to bit-complement", ast_type_string(expression->ctype));
594 return ast_new_unary('~', expression->ctype, expression);
597 static ast_t *parse_expression_unary_not(void) {
598 ast_t *operand = parse_expression_cast();
599 operand = ast_designator_convert(operand);
600 return ast_new_unary('!', ast_data_table[AST_DATA_INT], operand);
603 static ast_t *parse_expression_unary(void) {
604 lexer_token_t *token = lexer_peek();
605 if (parse_identifer_check(token, "sizeof")) {
607 return parse_sizeof();
611 if (parse_identifer_check(token, "__alignof__"))
614 if (parse_identifer_check(token, "_Alignof")) {
615 if (!opt_std_test(STANDARD_C11) && !opt_std_test(STANDARD_LICEC))
616 compile_error("_Alignof not supported in this version of the standard, try -std=c11 or -std=licec");
620 return parse_alignof();
623 if (parse_next(LEXER_TOKEN_INCREMENT) || parse_next(LEXER_TOKEN_DECREMENT)) {
624 ast_t *operand = parse_expression_unary();
625 operand = ast_designator_convert(operand);
626 parse_semantic_lvalue(operand);
627 int operation = lexer_ispunct(token, LEXER_TOKEN_INCREMENT)
628 ? AST_TYPE_PRE_INCREMENT
629 : AST_TYPE_PRE_DECREMENT;
631 return ast_new_unary(operation, operand->ctype, operand);
634 /* &&label for computed goto */
635 if (parse_next(LEXER_TOKEN_AND))
636 return parse_label_address();
638 /* a more managable method for unary parsers */
639 static ast_t *(*const parsers['~'-'!'+1])(void) = {
640 [0] = &parse_expression_unary_not,
641 ['&'-'!'] = &parse_expression_unary_address,
642 ['*'-'!'] = &parse_expression_unary_deref,
643 ['+'-'!'] = &parse_expression_unary_plus,
644 ['-'-'!'] = &parse_expression_unary_minus,
645 ['~'-'!'] = &parse_expression_unary_bitcomp
648 for (const char *test = "!&*+-~"; *test; test++)
649 if (parse_next(*test))
650 return parsers[*test-'!']();
652 return parse_expression_postfix();
655 ast_t *parse_expression_compound_literal(data_type_t *type) {
656 char *name = ast_label();
657 list_t *init = init_entry(type);
658 ast_t *ast = ast_variable_local(type, name);
659 ast->variable.init = init;
663 static data_type_t *parse_expression_cast_type(void) {
664 data_type_t *basetype = parse_declaration_specification(NULL);
665 return parse_declarator(NULL, basetype, NULL, CDECL_CAST);
668 static ast_t *parse_expression_cast(void) {
669 lexer_token_t *token = lexer_next();
670 if (lexer_ispunct(token, '(') && parse_type_check(lexer_peek())) {
671 data_type_t *type = parse_expression_cast_type();
673 if (lexer_ispunct(lexer_peek(), '{')) {
674 ast_t *ast = parse_expression_compound_literal(type);
675 return parse_expression_postfix_tail(ast);
677 return ast_new_unary(AST_TYPE_EXPRESSION_CAST, type, parse_expression_cast());
680 return parse_expression_unary();
683 static ast_t *parse_expression_multiplicative(void) {
684 ast_t *ast = ast_designator_convert(parse_expression_cast());
686 if (parse_next('*')) ast = conv_usual('*', ast, ast_designator_convert(parse_expression_cast()));
687 else if (parse_next('/')) ast = conv_usual('/', ast, ast_designator_convert(parse_expression_cast()));
688 else if (parse_next('%')) ast = conv_usual('%', ast, ast_designator_convert(parse_expression_cast()));
694 static ast_t *parse_expression_additive(void) {
695 ast_t *ast = parse_expression_multiplicative();
697 if (parse_next('+')) ast = conv_usual('+', ast, parse_expression_multiplicative());
698 else if (parse_next('-')) ast = conv_usual('-', ast, parse_expression_multiplicative());
704 static ast_t *parse_expression_shift(void) {
705 ast_t *ast = parse_expression_additive();
707 lexer_token_t *token = lexer_next();
709 if (lexer_ispunct(token, LEXER_TOKEN_LSHIFT))
710 operation = AST_TYPE_LSHIFT;
711 else if (lexer_ispunct(token, LEXER_TOKEN_RSHIFT))
712 operation = ast->ctype->sign
719 ast_t *right = parse_expression_additive();
720 // todo ensure integer
721 data_type_t *result = conv_senority(ast->ctype, right->ctype);
722 ast = ast_new_binary(result, operation, ast, right);
727 static ast_t *parse_expression_relational(void) {
728 ast_t *ast = parse_expression_shift();
730 if (parse_next('<')) ast = conv_usual('<', ast, parse_expression_shift());
731 else if (parse_next('>')) ast = conv_usual('>', ast, parse_expression_shift());
732 else if (parse_next(LEXER_TOKEN_LEQUAL)) ast = conv_usual(AST_TYPE_LEQUAL, ast, parse_expression_shift());
733 else if (parse_next(LEXER_TOKEN_GEQUAL)) ast = conv_usual(AST_TYPE_GEQUAL, ast, parse_expression_shift());
739 static ast_t *parse_expression_equality(void) {
740 ast_t *ast = parse_expression_relational();
741 if (parse_next(LEXER_TOKEN_EQUAL))
742 return conv_usual(AST_TYPE_EQUAL, ast, parse_expression_equality());
743 if (parse_next(LEXER_TOKEN_NEQUAL))
744 return conv_usual(AST_TYPE_NEQUAL, ast, parse_expression_equality());
748 static ast_t *parse_expression_bitand(void) {
749 ast_t *ast = parse_expression_equality();
750 while (parse_next('&'))
751 ast = conv_usual('&', ast, parse_expression_equality());
755 static ast_t *parse_expression_bitxor(void) {
756 ast_t *ast = parse_expression_bitand();
757 while (parse_next('^'))
758 ast = conv_usual('^', ast, parse_expression_bitand());
762 static ast_t *parse_expression_bitor(void) {
763 ast_t *ast = parse_expression_bitxor();
764 while (parse_next('|'))
765 ast = conv_usual('|', ast, parse_expression_bitxor());
769 static ast_t *parse_expression_logand(void) {
770 ast_t *ast = parse_expression_bitor();
771 while (parse_next(LEXER_TOKEN_AND))
772 ast = ast_new_binary(ast_data_table[AST_DATA_INT], AST_TYPE_AND, ast, parse_expression_bitor());
776 static ast_t *parse_expression_logor(void) {
777 ast_t *ast = parse_expression_logand();
778 while (parse_next(LEXER_TOKEN_OR))
779 ast = ast_new_binary(ast_data_table[AST_DATA_INT], AST_TYPE_OR, ast, parse_expression_logand());
783 ast_t *parse_expression_assignment(void) {
784 ast_t *ast = parse_expression_logor();
785 lexer_token_t *token = lexer_next();
790 if (lexer_ispunct(token, '?')) {
791 ast_t *then = parse_expression_comma();
793 ast_t *last = parse_expression_conditional();
794 ast_t *operand = (opt_extension_test(EXTENSION_OMITOPCOND)) ? last : then;
795 return ast_ternary(operand->ctype, ast, then, last);
798 int compound = parse_operation_compound_operator(token);
799 int reclassify = parse_operation_reclassify(compound);
800 if (lexer_ispunct(token, '=') || compound) {
801 ast_t *value = parse_expression_assignment();
802 if (lexer_ispunct(token, '=') || compound)
803 parse_semantic_lvalue(ast);
805 ast_t *right = compound ? conv_usual(reclassify, ast, value) : value;
806 if (conv_capable(right->ctype) && ast->ctype->type != right->ctype->type)
807 right = ast_type_convert(ast->ctype, right);
808 return ast_new_binary(ast->ctype, '=', ast, right);
815 static ast_t *parse_expression_comma(void) {
816 ast_t *ast = parse_expression_assignment();
817 while (parse_next(',')) {
818 ast_t *expression = parse_expression_assignment();
819 ast = ast_new_binary(expression->ctype, ',', ast, expression);
824 static ast_t *parse_expression(void) {
825 ast_t *ast = parse_expression_comma();
827 compile_error("expression expected");
831 static ast_t *parse_expression_optional(void) {
832 return parse_expression_comma();
835 static ast_t *parse_expression_condition(void) {
836 ast_t *cond = parse_expression();
837 if (ast_type_isfloating(cond->ctype))
838 return ast_type_convert(ast_data_table[TYPE_BOOL], cond);
842 static ast_t *parse_expression_conditional(void) {
843 ast_t *ast = parse_expression_logor();
844 if (!parse_next('?'))
846 ast_t *then = parse_expression_comma();
848 ast_t *last = parse_expression_conditional();
849 return ast_ternary(last->ctype, ast, then, last);
852 int parse_expression_evaluate(void) {
853 return parse_evaluate(parse_expression_conditional());
856 static bool parse_type_check(lexer_token_t *token) {
857 if (token->type != LEXER_TOKEN_IDENTIFIER)
860 static const char *keywords[] = {
861 "char", "short", "int", "long", "float", "double",
862 "struct", "union", "signed", "unsigned", "enum", "void",
863 "typedef", "extern", "static", "auto", "register", "const",
864 "volatile", "inline", "restrict", "typeof", "_Bool",
867 * Ironically there is also another way to specific some keywords
868 * in C due to compilers being sneaky. Thus some code may use
869 * things like, __static__, or __typeof__, which are compilers
870 * reserved implementations of those keywords before the standard
877 for (size_t i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++)
878 if (!strcmp(keywords[i], token->string))
881 if (table_find(parse_typedefs, token->string))
888 static char *parse_memory_tag(void) {
889 lexer_token_t *token = lexer_next();
890 if (token->type == LEXER_TOKEN_IDENTIFIER)
891 return token->string;
896 static int parse_memory_fields_padding(int offset, data_type_t *type) {
897 int size = type->type == TYPE_ARRAY ? type->pointer->size : type->size;
898 size = MIN(size, ARCH_ALIGNMENT);
899 return (offset % size == 0) ? 0 : size - offset % size;
902 static void parse_memory_fields_flexiblize(list_t *fields) {
903 list_iterator_t *it = list_iterator(fields);
904 while (!list_iterator_end(it)) {
905 pair_t *pair = list_iterator_next(it);
906 char *name = pair->first;
907 data_type_t *type = pair->second;
909 if (type->type != TYPE_ARRAY)
912 if (!opt_std_test(STANDARD_GNUC) && !opt_std_test(STANDARD_LICEC)) {
913 if (type->length == 0)
917 if (type->length == -1) {
918 if (!list_iterator_end(it))
919 compile_error("flexible field %s can only appear as the last field in a structure", name);
920 if (list_length(fields) == 1)
921 compile_error("flexible field %s as only field in structure is illegal", name);
927 static void parse_memory_fields_squash(table_t *table, data_type_t *unnamed, int offset) {
928 for (list_iterator_t *it = list_iterator(table_keys(unnamed->fields)); !list_iterator_end(it); ) {
929 char *name = list_iterator_next(it);
930 data_type_t *type = ast_type_copy(table_find(unnamed->fields, name));
931 type->offset += offset;
932 table_insert(table, name, type);
936 static int parse_bitfield_maybe(char *name, data_type_t *through) {
937 if (!parse_next(':'))
939 if (!ast_type_isinteger(through))
940 compile_error("invalid type for bitfield size %s", ast_type_string(through));
941 int evaluate = parse_expression_evaluate();
942 if (evaluate < 0 || through->size * 8 < evaluate)
943 compile_error("invalid bitfield size %s: %d", ast_type_string(through), evaluate);
944 if (evaluate == 0 && name)
945 compile_error("zero sized bitfield cannot be named");
949 static list_t *parse_memory_fields_intermediate(void) {
950 list_t *list = list_create();
953 lexer_token_t *next = lexer_next();
954 if (parse_identifer_check(next, "_Static_assert")) {
955 if (!opt_std_test(STANDARD_C11) && !opt_std_test(STANDARD_LICEC))
956 compile_error("_Static_assert not supported in this version of the standard, try -std=c11 or -std=licec");
958 parse_static_assert();
964 if (!parse_type_check(lexer_peek()))
967 data_type_t *basetype = parse_declaration_specification(NULL);
969 if (basetype->type == TYPE_STRUCTURE && parse_next(';')) {
970 list_push(list, pair_create(NULL, basetype));
976 data_type_t *fieldtype = parse_declarator(&name, basetype, NULL, CDECL_TYPEONLY);
977 parse_semantic_notvoid(fieldtype);
979 /* copy though for bitfield */
980 fieldtype = ast_type_copy(fieldtype);
981 fieldtype->bitfield.size = parse_bitfield_maybe(name, fieldtype);
983 list_push(list, pair_create(name, fieldtype));
987 if (lexer_ispunct(lexer_peek(), '}'))
988 compile_warn("no semicolon at end of struct or union");
998 static void parse_bitfield_update(int *offset, int *bitfield) {
999 *offset += (*bitfield + 8) / 8;
1003 static table_t *parse_struct_offset(list_t *fields, int *size) {
1006 list_iterator_t *it = list_iterator(fields);
1007 table_t *table = table_create(NULL);
1009 while (!list_iterator_end(it)) {
1010 pair_t *pair = list_iterator_next(it);
1011 char *name = pair->first;
1012 data_type_t *type = pair->second;
1014 if (!name && type->type == TYPE_STRUCTURE) {
1015 parse_bitfield_update(&offset, &bitfield);
1016 parse_memory_fields_squash(table, type, offset);
1017 offset += type->size;
1020 if (type->bitfield.size == 0) {
1021 parse_bitfield_update(&offset, &bitfield);
1022 offset += parse_memory_fields_padding(offset, type);
1026 if (type->bitfield.size >= 0) {
1027 /* bitfield space */
1028 int space = type->size * 8 - bitfield;
1029 if (0 <= bitfield && type->bitfield.size <= space) {
1030 type->bitfield.offset = bitfield;
1031 type->offset = offset;
1033 parse_bitfield_update(&offset, &bitfield);
1034 offset += parse_memory_fields_padding(offset, type);
1035 type->offset = offset;
1036 type->bitfield.offset = 0;
1038 bitfield = type->bitfield.size;
1040 parse_bitfield_update(&offset, &bitfield);
1041 offset += parse_memory_fields_padding(offset, type);
1042 type->offset = offset;
1043 offset += type->size;
1046 table_insert(table, name, type); /* no copy needed */
1048 parse_bitfield_update(&offset, &bitfield); /* stage it */
1053 static table_t *parse_union_offset(list_t *fields, int *size) {
1055 list_iterator_t *it = list_iterator(fields);
1056 table_t *table = table_create(NULL);
1058 while (!list_iterator_end(it)) {
1059 pair_t *pair = list_iterator_next(it);
1060 char *name = pair->first;
1061 data_type_t *type = pair->second;
1063 maxsize = MAX(maxsize, type->size);
1064 if (!name && type->type == TYPE_STRUCTURE) {
1065 parse_memory_fields_squash(table, type, 0);
1069 if (type->bitfield.size >= 0)
1070 type->bitfield.offset = 0;
1072 table_insert(table, name, type);
1078 static table_t *parse_memory_fields(int *size, bool isstruct) {
1079 if (!parse_next('{'))
1081 list_t *fields = parse_memory_fields_intermediate();
1083 /* make flexible if it can be made flexible */
1084 parse_memory_fields_flexiblize(fields);
1087 ? parse_struct_offset(fields, size)
1088 : parse_union_offset(fields, size);
1091 static data_type_t *parse_tag_definition(table_t *table, bool isstruct) {
1092 char *tag = parse_memory_tag();
1096 if (!(r = table_find(table, tag))) {
1097 r = ast_structure_new(NULL, 0, isstruct);
1098 table_insert(table, tag, r);
1101 r = ast_structure_new(NULL, 0, isstruct);
1105 table_t *fields = parse_memory_fields(&size, isstruct);
1120 data_type_t *parse_enumeration(void) {
1121 lexer_token_t *token = lexer_next();
1122 if (token->type == LEXER_TOKEN_IDENTIFIER)
1123 token = lexer_next();
1124 if (!lexer_ispunct(token, '{')) {
1126 return ast_data_table[AST_DATA_INT];
1130 token = lexer_next();
1131 if (lexer_ispunct(token, '}'))
1134 if (token->type != LEXER_TOKEN_IDENTIFIER)
1135 compile_error("expected identifier before ‘%s’ token", lexer_token_string(token));
1137 char *name = token->string;
1138 token = lexer_next();
1139 if (lexer_ispunct(token, '='))
1140 accumulate = parse_expression_evaluate();
1144 ast_t *constval = ast_new_integer(ast_data_table[AST_DATA_INT], accumulate++);
1145 table_insert(ast_localenv ? ast_localenv : ast_globalenv, name, constval);
1146 token = lexer_next();
1147 if (lexer_ispunct(token, ','))
1149 if (lexer_ispunct(token, '}'))
1152 compile_ice("parse_enumeration");
1154 return ast_data_table[AST_DATA_INT];
1157 data_type_t *parse_typeof(void) {
1159 data_type_t *type = parse_type_check(lexer_peek())
1160 ? parse_expression_cast_type()
1161 : parse_expression_comma()->ctype;
1166 data_type_t *parse_structure(void) {
1167 return parse_tag_definition(ast_structures, true);
1170 data_type_t *parse_union(void) {
1171 return parse_tag_definition(ast_unions, false);
1174 data_type_t *parse_typedef_find(const char *key) {
1175 return table_find(parse_typedefs, key);
1179 static data_type_t *parse_declaration_specification(storage_t *rstorage) {
1180 lexer_token_t *token = lexer_peek();
1181 if (!token || token->type != LEXER_TOKEN_IDENTIFIER)
1182 compile_ice("declaration specification");
1184 data_type_t *decl_spec(storage_t *);
1185 return decl_spec(rstorage);
1188 static data_type_t *parse_function_parameter(char **name, bool next) {
1189 data_type_t *basetype;
1192 basetype = parse_declaration_specification(&storage);
1193 return parse_declarator(name, basetype, NULL, next ? CDECL_TYPEONLY : CDECL_PARAMETER);
1196 static ast_t *parse_statement_if(void) {
1197 lexer_token_t *token;
1203 cond = parse_expression_condition();
1207 then = parse_statement();
1208 token = lexer_next();
1210 if (!token || token->type != LEXER_TOKEN_IDENTIFIER || strcmp(token->string, "else")) {
1212 return ast_if(cond, then, NULL);
1215 last = parse_statement();
1216 return ast_if(cond, then, last);
1219 static ast_t *parse_statement_declaration_semicolon(void) {
1220 lexer_token_t *token = lexer_next();
1221 if (lexer_ispunct(token, ';'))
1224 list_t *list = list_create();
1225 parse_statement_declaration(list);
1226 return list_shift(list);
1229 static ast_t *parse_statement_for(void) {
1231 ast_localenv = table_create(ast_localenv);
1232 ast_t *init = parse_statement_declaration_semicolon();
1233 ast_t *cond = parse_expression_optional();
1234 if (cond && ast_type_isfloating(cond->ctype))
1235 cond = ast_type_convert(ast_data_table[AST_DATA_BOOL], cond);
1237 ast_t *step = parse_expression_optional();
1239 ast_t *body = parse_statement();
1240 ast_localenv = table_parent(ast_localenv);
1241 return ast_for(init, cond, step, body);
1244 static ast_t *parse_statement_while(void) {
1246 ast_t *cond = parse_expression_condition();
1248 ast_t *body = parse_statement();
1249 return ast_while(cond, body);
1252 static ast_t *parse_statement_do(void) {
1253 ast_t *body = parse_statement();
1254 lexer_token_t *token = lexer_next();
1256 if (!parse_identifer_check(token, "while"))
1257 compile_error("expected ‘while’ before ‘%s’ token", lexer_token_string(token));
1260 ast_t *cond = parse_expression_condition();
1264 return ast_do(cond, body);
1267 static ast_t *parse_statement_break(void) {
1269 return ast_make(AST_TYPE_STATEMENT_BREAK);
1272 static ast_t *parse_statement_continue(void) {
1274 return ast_make(AST_TYPE_STATEMENT_CONTINUE);
1277 static ast_t *parse_statement_switch(void) {
1279 ast_t *expression = parse_expression();
1281 if (!ast_type_isinteger(expression->ctype)) {
1283 "switch statement requires expression of integer type (‘%s‘ invalid)",
1284 ast_type_string(expression->ctype)
1289 ast_t *body = parse_statement();
1290 return ast_switch(expression, body);
1293 static ast_t *parse_statement_case(void) {
1294 int begin = parse_expression_evaluate();
1296 lexer_token_t *token = lexer_next();
1297 if (parse_identifer_check(token, "..."))
1298 end = parse_expression_evaluate();
1305 compile_warn("empty case range specified");
1306 return ast_case(begin, end);
1309 static ast_t *parse_statement_default(void) {
1311 return ast_make(AST_TYPE_STATEMENT_DEFAULT);
1314 static ast_t *parse_statement_return(void) {
1315 ast_t *val = parse_expression_optional();
1318 return ast_return(ast_type_convert(ast_data_table[AST_DATA_FUNCTION]->returntype, val));
1319 return ast_return(NULL);
1322 static ast_t *parse_statement_goto(void) {
1323 /* deal with computed goto */
1324 if (parse_next('*')) {
1325 ast_t *expression = parse_expression_cast();
1326 if (expression->ctype->type != TYPE_POINTER)
1327 compile_error("expected pointer type for computed goto");
1328 return ast_goto_computed(expression);
1331 /* otherwise the traditional route */
1333 lexer_token_t *token = lexer_next();
1334 if (!token || token->type != LEXER_TOKEN_IDENTIFIER)
1335 compile_error("expected label in goto statement");
1338 ast_t *node = ast_goto(token->string);
1339 list_push(ast_gotos, node);
1344 static void parse_label_backfill(void) {
1345 for (list_iterator_t *it = list_iterator(ast_gotos); !list_iterator_end(it); ) {
1346 ast_t *source = list_iterator_next(it);
1347 char *label = source->gotostmt.label;
1348 ast_t *destination = table_find(ast_labels, label);
1351 compile_error("undefined label ‘%s‘", label);
1352 if (destination->gotostmt.where)
1353 source->gotostmt.where = destination->gotostmt.where;
1355 source->gotostmt.where = destination->gotostmt.where = ast_label();
1359 static ast_t *parse_label(lexer_token_t *token) {
1361 char *label = token->string;
1362 ast_t *node = ast_new_label(label);
1364 if (table_find(ast_labels, label))
1365 compile_error("duplicate label ‘%s‘", label);
1366 table_insert(ast_labels, label, node);
1371 static ast_t *parse_statement(void) {
1372 lexer_token_t *token = lexer_next();
1375 if (lexer_ispunct (token, '{')) return parse_statement_compound();
1376 if (parse_identifer_check(token, "if")) return parse_statement_if();
1377 if (parse_identifer_check(token, "for")) return parse_statement_for();
1378 if (parse_identifer_check(token, "while")) return parse_statement_while();
1379 if (parse_identifer_check(token, "do")) return parse_statement_do();
1380 if (parse_identifer_check(token, "return")) return parse_statement_return();
1381 if (parse_identifer_check(token, "switch")) return parse_statement_switch();
1382 if (parse_identifer_check(token, "case")) return parse_statement_case();
1383 if (parse_identifer_check(token, "default")) return parse_statement_default();
1384 if (parse_identifer_check(token, "break")) return parse_statement_break();
1385 if (parse_identifer_check(token, "continue")) return parse_statement_continue();
1386 if (parse_identifer_check(token, "goto")) return parse_statement_goto();
1388 if (token->type == LEXER_TOKEN_IDENTIFIER && lexer_ispunct(lexer_peek(), ':'))
1389 return parse_label(token);
1393 ast = parse_expression_optional();
1399 static void parse_statement_declaration(list_t *list){
1400 lexer_token_t *token = lexer_peek();
1402 compile_error("statement declaration with unexpected ending");
1403 if (parse_type_check(token)) {
1404 parse_declaration(list, ast_variable_local);
1406 lexer_token_t *next = lexer_next();
1407 if (parse_identifer_check(next, "_Static_assert"))
1408 return parse_static_assert();
1411 ast_t *statement = parse_statement();
1413 list_push(list, statement);
1417 static ast_t *parse_statement_compound(void) {
1418 ast_localenv = table_create(ast_localenv);
1419 list_t *statements = list_create();
1421 lexer_token_t *token = lexer_next();
1422 if (lexer_ispunct(token, '}'))
1426 parse_statement_declaration(statements);
1428 ast_localenv = table_parent(ast_localenv);
1429 return ast_compound(statements);
1432 static data_type_t *parse_function_parameters(list_t *paramvars, data_type_t *returntype) {
1433 bool typeonly = !paramvars;
1434 list_t *paramtypes = list_create();
1435 lexer_token_t *token = lexer_next();
1436 lexer_token_t *next = lexer_next();
1438 if (parse_identifer_check(token, "void") && lexer_ispunct(next, ')'))
1439 return ast_prototype(returntype, paramtypes, false);
1441 if (lexer_ispunct(token, ')'))
1442 return ast_prototype(returntype, paramtypes, true);
1446 token = lexer_next();
1447 if (parse_identifer_check(token, "...")) {
1448 if (list_length(paramtypes) == 0)
1449 compile_ice("parse_function_parameters");
1451 return ast_prototype(returntype, paramtypes, true);
1457 data_type_t *ptype = parse_function_parameter(&name, typeonly);
1458 parse_semantic_notvoid(ptype);
1460 if (ptype->type == TYPE_ARRAY)
1461 ptype = ast_pointer(ptype->pointer);
1462 list_push(paramtypes, ptype);
1465 list_push(paramvars, ast_variable_local(ptype, name));
1467 lexer_token_t *token = lexer_next();
1468 if (lexer_ispunct(token, ')'))
1469 return ast_prototype(returntype, paramtypes, false);
1471 if (!lexer_ispunct(token, ','))
1472 compile_ice("parse_function_parameters");
1476 static ast_t *parse_function_definition(data_type_t *functype, char *name, list_t *parameters) {
1477 ast_localenv = table_create(ast_localenv);
1478 ast_locals = list_create();
1479 ast_data_table[AST_DATA_FUNCTION] = functype;
1481 table_insert(ast_localenv, "__func__", ast_new_string(name));
1483 ast_t *body = parse_statement_compound();
1484 ast_t *r = ast_function(functype, name, parameters, body, ast_locals);
1486 table_insert(ast_globalenv, name, r);
1488 ast_data_table[AST_DATA_FUNCTION] = NULL;
1489 ast_localenv = NULL;
1495 static bool parse_function_definition_check(void) {
1496 list_t *buffer = list_create();
1503 lexer_token_t *token = lexer_next();
1504 list_push(buffer, token);
1507 compile_error("function definition with unexpected ending");
1509 if (nests == 0 && paren && lexer_ispunct(token, '{'))
1512 if (nests == 0 && (lexer_ispunct(token, ';')
1513 ||lexer_ispunct(token, ',')
1514 ||lexer_ispunct(token, '=')))
1520 if (lexer_ispunct(token, '('))
1523 if (lexer_ispunct(token, ')')) {
1525 compile_error("unmatched parenthesis");
1531 while (list_length(buffer) > 0)
1532 lexer_unget(list_pop(buffer));
1537 static ast_t *parse_function_definition_intermediate(void) {
1540 list_t *parameters = list_create();
1541 data_type_t *basetype = ast_data_table[AST_DATA_INT];
1543 if (parse_type_check(lexer_peek()))
1544 basetype = parse_declaration_specification(&storage);
1546 compile_warn("return type defaults to ’int’");
1548 ast_localenv = table_create(ast_globalenv);
1549 ast_labels = table_create(NULL);
1550 ast_gotos = list_create();
1552 data_type_t *functype = parse_declarator(&name, basetype, parameters, CDECL_BODY);
1553 functype->isstatic = !!(storage == STORAGE_STATIC);
1554 ast_variable_global(functype, name);
1556 ast_t *value = parse_function_definition(functype, name, parameters);
1558 parse_label_backfill();
1560 ast_localenv = NULL;
1564 static data_type_t *parse_declarator_direct_restage(data_type_t *basetype, list_t *parameters) {
1565 lexer_token_t *token = lexer_next();
1566 if (lexer_ispunct(token, '[')) {
1568 token = lexer_next();
1569 if (lexer_ispunct(token, ']'))
1573 length = parse_expression_evaluate();
1577 data_type_t *type = parse_declarator_direct_restage(basetype, parameters);
1578 if (type->type == TYPE_FUNCTION)
1579 compile_error("array of functions");
1580 return ast_array(type, length);
1582 if (lexer_ispunct(token, '(')) {
1583 if (basetype->type == TYPE_FUNCTION)
1584 compile_error("function returning function");
1585 if (basetype->type == TYPE_ARRAY)
1586 compile_error("function returning array");
1587 return parse_function_parameters(parameters, basetype);
1593 static void parse_qualifiers_skip(void) {
1595 lexer_token_t *token = lexer_next();
1596 if (parse_identifer_check(token, "const")
1597 || parse_identifer_check(token, "volatile")
1598 || parse_identifer_check(token, "restrict")) {
1606 static data_type_t *parse_declarator_direct(char **rname, data_type_t *basetype, list_t *parameters, cdecl_t context) {
1607 lexer_token_t *token = lexer_next();
1608 lexer_token_t *next = lexer_peek();
1610 if (lexer_ispunct(token, '(') && !parse_type_check(next) && !lexer_ispunct(next, ')')) {
1611 data_type_t *stub = ast_type_stub();
1612 data_type_t *type = parse_declarator_direct(rname, stub, parameters, context);
1614 *stub = *parse_declarator_direct_restage(basetype, parameters);
1618 if (lexer_ispunct(token, '*')) {
1619 parse_qualifiers_skip();
1620 data_type_t *stub = ast_type_stub();
1621 data_type_t *type = parse_declarator_direct(rname, stub, parameters, context);
1622 *stub = *ast_pointer(basetype);
1626 if (token->type == LEXER_TOKEN_IDENTIFIER) {
1627 if (context == CDECL_CAST)
1628 compile_error("wasn't expecting identifier `%s'", lexer_token_string(token));
1629 *rname = token->string;
1630 return parse_declarator_direct_restage(basetype, parameters);
1633 if (context == CDECL_BODY || context == CDECL_PARAMETER)
1634 compile_error("expected identifier, `(` or `*` for declarator");
1638 return parse_declarator_direct_restage(basetype, parameters);
1641 static void parse_array_fix(data_type_t *type) {
1642 if (type->type == TYPE_ARRAY) {
1643 parse_array_fix(type->pointer);
1644 type->size = type->length * type->pointer->size;
1645 } else if (type->type == TYPE_POINTER) {
1646 parse_array_fix(type->pointer);
1647 } else if (type->type == TYPE_FUNCTION) {
1648 parse_array_fix(type->returntype);
1652 static data_type_t *parse_declarator(char **rname, data_type_t *basetype, list_t *parameters, cdecl_t context) {
1653 data_type_t *type = parse_declarator_direct(rname, basetype, parameters, context);
1654 parse_array_fix(type);
1658 static void parse_declaration(list_t *list, ast_t *(*make)(data_type_t *, char *)) {
1660 data_type_t *basetype = parse_declaration_specification(&storage);
1661 lexer_token_t *token = lexer_next();
1663 if (lexer_ispunct(token, ';'))
1670 data_type_t *type = parse_declarator(&name, ast_type_copy_incomplete(basetype), NULL, CDECL_BODY);
1672 if (storage == STORAGE_STATIC)
1673 type->isstatic = true;
1675 token = lexer_next();
1676 if (lexer_ispunct(token, '=')) {
1677 if (storage == STORAGE_TYPEDEF)
1678 compile_error("invalid use of typedef");
1679 parse_semantic_notvoid(type);
1680 ast_t *var = make(type, name);
1681 list_push(list, ast_declaration(var, init_entry(var->ctype)));
1682 token = lexer_next();
1683 } else if (storage == STORAGE_TYPEDEF) {
1684 table_insert(parse_typedefs, name, type);
1685 } else if (type->type == TYPE_FUNCTION) {
1688 ast_t *var = make(type, name);
1689 if (storage != STORAGE_EXTERN)
1690 list_push(list, ast_declaration(var, NULL));
1692 if (lexer_ispunct(token, ';'))
1694 if (!lexer_ispunct(token, ','))
1695 compile_ice("confused %X", token);
1699 list_t *parse_run(void) {
1700 list_t *list = list_create();
1704 if (parse_function_definition_check())
1705 list_push(list, parse_function_definition_intermediate());
1707 parse_declaration(list, &ast_variable_global);
1712 void parse_init(void) {
1713 data_type_t *voidp = ast_pointer(ast_data_table[AST_DATA_VOID]);
1714 data_type_t *type = ast_prototype(voidp, list_create(), true);
1715 data_type_t *addr = ast_prototype(voidp, list_default(ast_data_table[AST_DATA_ULONG]), true);
1717 table_insert(ast_globalenv, "__builtin_va_start", ast_variable_global(type, "__builtin_va_start"));
1718 table_insert(ast_globalenv, "__builtin_va_arg", ast_variable_global(type, "__builtin_va_arg"));
1719 table_insert(ast_globalenv, "__builtin_return_address", ast_variable_global(addr, "__buitlin_return_address"));