11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_long_double = NULL;
42 static type_t *type_double = NULL;
43 static type_t *type_float = NULL;
44 static type_t *type_char = NULL;
45 static type_t *type_string = NULL;
46 static type_t *type_void = NULL;
47 static type_t *type_void_ptr = NULL;
48 static type_t *type_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
74 #define IMAGINARY_SPECIFIERS \
77 #define COMPLEX_SPECIFIERS
78 #define IMAGINARY_SPECIFIERS
81 #define TYPE_SPECIFIERS \
99 #define DECLARATION_START \
104 #define TYPENAME_START \
108 static void *allocate_ast_zero(size_t size)
110 void *res = allocate_ast(size);
111 memset(res, 0, size);
115 static size_t get_expression_struct_size(expression_type_t type)
117 static const size_t sizes[] = {
118 [EXPR_REFERENCE] = sizeof(reference_expression_t),
119 [EXPR_CONST] = sizeof(const_expression_t),
120 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
121 [EXPR_CALL] = sizeof(call_expression_t),
122 [EXPR_UNARY] = sizeof(unary_expression_t),
123 [EXPR_BINARY] = sizeof(binary_expression_t),
124 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
125 [EXPR_SELECT] = sizeof(select_expression_t),
126 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
127 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
128 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
129 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
130 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
131 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
132 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
133 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
134 [EXPR_STATEMENT] = sizeof(statement_expression_t)
136 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
137 assert(type <= EXPR_STATEMENT);
138 assert(sizes[type] != 0);
139 (void) get_expression_struct_size;
143 static size_t get_type_struct_size(type_type_t type)
145 static const size_t sizes[] = {
146 [TYPE_ATOMIC] = sizeof(atomic_type_t),
147 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
148 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
149 [TYPE_ENUM] = sizeof(enum_type_t),
150 [TYPE_FUNCTION] = sizeof(function_type_t),
151 [TYPE_POINTER] = sizeof(pointer_type_t),
152 [TYPE_ARRAY] = sizeof(array_type_t),
153 [TYPE_BUILTIN] = sizeof(builtin_type_t),
154 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
155 [TYPE_TYPEOF] = sizeof(typeof_type_t),
157 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
158 assert(type <= TYPE_TYPEOF);
159 assert(sizes[type] != 0);
163 static type_t *allocate_type_zero(type_type_t type)
165 size_t size = get_type_struct_size(type);
166 type_t *res = obstack_alloc(type_obst, size);
167 memset(res, 0, size);
169 res->base.type = type;
173 static size_t get_initializer_size(initializer_type_t type)
175 static const size_t sizes[] = {
176 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
177 [INITIALIZER_STRING] = sizeof(initializer_string_t),
178 [INITIALIZER_LIST] = sizeof(initializer_list_t)
180 assert(type < INITIALIZER_COUNT);
181 assert(sizes[type] != 0);
185 static initializer_t *allocate_initializer(initializer_type_t type)
187 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
193 static void free_type(void *type)
195 obstack_free(type_obst, type);
199 * returns the top element of the environment stack
201 static size_t environment_top(void)
203 return ARR_LEN(environment_stack);
206 static size_t label_top(void)
208 return ARR_LEN(label_stack);
213 static inline void next_token(void)
215 token = lookahead_buffer[lookahead_bufpos];
216 lookahead_buffer[lookahead_bufpos] = lexer_token;
219 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
222 print_token(stderr, &token);
223 fprintf(stderr, "\n");
227 static inline const token_t *look_ahead(int num)
229 assert(num > 0 && num <= MAX_LOOKAHEAD);
230 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
231 return & lookahead_buffer[pos];
234 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
236 static void error(void)
239 #ifdef ABORT_ON_ERROR
244 static void parser_print_prefix_pos(const source_position_t source_position)
246 fputs(source_position.input_name, stderr);
248 fprintf(stderr, "%u", source_position.linenr);
252 static void parser_print_error_prefix_pos(
253 const source_position_t source_position)
255 parser_print_prefix_pos(source_position);
256 fputs("error: ", stderr);
260 static void parser_print_error_prefix(void)
262 parser_print_error_prefix_pos(token.source_position);
265 static void parse_error(const char *message)
267 parser_print_error_prefix();
268 fprintf(stderr, "parse error: %s\n", message);
271 static void parser_print_warning_prefix_pos(
272 const source_position_t source_position)
274 parser_print_prefix_pos(source_position);
275 fputs("warning: ", stderr);
278 static void parse_warning_pos(const source_position_t source_position,
279 const char *const message)
281 parser_print_prefix_pos(source_position);
282 fprintf(stderr, "warning: %s\n", message);
285 static void parse_warning(const char *message)
287 parse_warning_pos(token.source_position, message);
290 static void parse_error_expected(const char *message, ...)
295 if(message != NULL) {
296 parser_print_error_prefix();
297 fprintf(stderr, "%s\n", message);
299 parser_print_error_prefix();
300 fputs("Parse error: got ", stderr);
301 print_token(stderr, &token);
302 fputs(", expected ", stderr);
304 va_start(args, message);
305 token_type_t token_type = va_arg(args, token_type_t);
306 while(token_type != 0) {
310 fprintf(stderr, ", ");
312 print_token_type(stderr, token_type);
313 token_type = va_arg(args, token_type_t);
316 fprintf(stderr, "\n");
319 static void print_type_quoted(type_t *type)
326 static void type_error(const char *msg, const source_position_t source_position,
329 parser_print_error_prefix_pos(source_position);
330 fprintf(stderr, "%s, but found type ", msg);
331 print_type_quoted(type);
335 static void type_error_incompatible(const char *msg,
336 const source_position_t source_position, type_t *type1, type_t *type2)
338 parser_print_error_prefix_pos(source_position);
339 fprintf(stderr, "%s, incompatible types: ", msg);
340 print_type_quoted(type1);
341 fprintf(stderr, " - ");
342 print_type_quoted(type2);
343 fprintf(stderr, ")\n");
346 static void eat_block(void)
348 if(token.type == '{')
351 while(token.type != '}') {
352 if(token.type == T_EOF)
354 if(token.type == '{') {
363 static void eat_statement(void)
365 while(token.type != ';') {
366 if(token.type == T_EOF)
368 if(token.type == '}')
370 if(token.type == '{') {
379 static void eat_brace(void)
381 if(token.type == '(')
384 while(token.type != ')') {
385 if(token.type == T_EOF)
387 if(token.type == ')' || token.type == ';' || token.type == '}') {
390 if(token.type == '(') {
394 if(token.type == '{') {
403 #define expect(expected) \
404 if(UNLIKELY(token.type != (expected))) { \
405 parse_error_expected(NULL, (expected), 0); \
411 #define expect_block(expected) \
412 if(UNLIKELY(token.type != (expected))) { \
413 parse_error_expected(NULL, (expected), 0); \
419 #define expect_void(expected) \
420 if(UNLIKELY(token.type != (expected))) { \
421 parse_error_expected(NULL, (expected), 0); \
427 static void set_context(context_t *new_context)
429 context = new_context;
431 last_declaration = new_context->declarations;
432 if(last_declaration != NULL) {
433 while(last_declaration->next != NULL) {
434 last_declaration = last_declaration->next;
440 * called when we find a 2nd declarator for an identifier we already have a
443 static bool is_compatible_declaration (declaration_t *declaration,
444 declaration_t *previous)
446 if (declaration->type->type == TYPE_FUNCTION &&
447 previous->type->type == TYPE_FUNCTION &&
448 previous->type->function.unspecified_parameters) {
449 function_type_t* const prev_func = &previous->type->function;
450 function_type_t* const decl_func = &declaration->type->function;
451 if (prev_func->unspecified_parameters &&
452 prev_func->result_type == decl_func->result_type) {
453 declaration->type = previous->type;
457 /* TODO: not correct yet */
458 return declaration->type == previous->type;
461 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
463 declaration_t *declaration = symbol->declaration;
464 for( ; declaration != NULL; declaration = declaration->symbol_next) {
465 if(declaration->namespc == namespc)
472 static const char *get_namespace_prefix(namespace_t namespc)
475 case NAMESPACE_NORMAL:
477 case NAMESPACE_UNION:
479 case NAMESPACE_STRUCT:
483 case NAMESPACE_LABEL:
486 panic("invalid namespace found");
490 * pushs an environment_entry on the environment stack and links the
491 * corresponding symbol to the new entry
493 static declaration_t *stack_push(stack_entry_t **stack_ptr,
494 declaration_t *declaration,
495 context_t *parent_context)
497 symbol_t *symbol = declaration->symbol;
498 namespace_t namespc = (namespace_t)declaration->namespc;
500 /* a declaration should be only pushed once */
501 assert(declaration->parent_context == NULL);
502 declaration->parent_context = parent_context;
504 declaration_t *previous_declaration = get_declaration(symbol, namespc);
505 assert(declaration != previous_declaration);
506 if(previous_declaration != NULL
507 && previous_declaration->parent_context == context) {
508 if(!is_compatible_declaration(declaration, previous_declaration)) {
509 parser_print_error_prefix_pos(declaration->source_position);
510 fprintf(stderr, "definition of symbol %s%s with type ",
511 get_namespace_prefix(namespc), symbol->string);
512 print_type_quoted(declaration->type);
514 parser_print_error_prefix_pos(
515 previous_declaration->source_position);
516 fprintf(stderr, "is incompatible with previous declaration "
518 print_type_quoted(previous_declaration->type);
521 unsigned old_storage_class = previous_declaration->storage_class;
522 unsigned new_storage_class = declaration->storage_class;
523 if (current_function == NULL) {
524 if (old_storage_class != STORAGE_CLASS_STATIC &&
525 new_storage_class == STORAGE_CLASS_STATIC) {
526 parser_print_error_prefix_pos(declaration->source_position);
528 "static declaration of '%s' follows non-static declaration\n",
530 parser_print_error_prefix_pos(previous_declaration->source_position);
531 fprintf(stderr, "previous declaration of '%s' was here\n",
534 if (old_storage_class == STORAGE_CLASS_EXTERN) {
535 if (new_storage_class == STORAGE_CLASS_NONE) {
536 previous_declaration->storage_class = STORAGE_CLASS_NONE;
539 parser_print_warning_prefix_pos(declaration->source_position);
540 fprintf(stderr, "redundant declaration for '%s'\n",
542 parser_print_warning_prefix_pos(previous_declaration->source_position);
543 fprintf(stderr, "previous declaration of '%s' was here\n",
548 if (old_storage_class == STORAGE_CLASS_EXTERN &&
549 new_storage_class == STORAGE_CLASS_EXTERN) {
550 parser_print_warning_prefix_pos(declaration->source_position);
551 fprintf(stderr, "redundant extern declaration for '%s'\n",
553 parser_print_warning_prefix_pos(previous_declaration->source_position);
554 fprintf(stderr, "previous declaration of '%s' was here\n",
557 parser_print_error_prefix_pos(declaration->source_position);
558 if (old_storage_class == new_storage_class) {
559 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
561 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
563 parser_print_error_prefix_pos(previous_declaration->source_position);
564 fprintf(stderr, "previous declaration of '%s' was here\n",
569 return previous_declaration;
572 /* remember old declaration */
574 entry.symbol = symbol;
575 entry.old_declaration = symbol->declaration;
576 entry.namespc = (unsigned short) namespc;
577 ARR_APP1(stack_entry_t, *stack_ptr, entry);
579 /* replace/add declaration into declaration list of the symbol */
580 if(symbol->declaration == NULL) {
581 symbol->declaration = declaration;
583 declaration_t *iter_last = NULL;
584 declaration_t *iter = symbol->declaration;
585 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
586 /* replace an entry? */
587 if(iter->namespc == namespc) {
588 if(iter_last == NULL) {
589 symbol->declaration = declaration;
591 iter_last->symbol_next = declaration;
593 declaration->symbol_next = iter->symbol_next;
598 assert(iter_last->symbol_next == NULL);
599 iter_last->symbol_next = declaration;
606 static declaration_t *environment_push(declaration_t *declaration)
608 assert(declaration->source_position.input_name != NULL);
609 return stack_push(&environment_stack, declaration, context);
612 static declaration_t *label_push(declaration_t *declaration)
614 return stack_push(&label_stack, declaration, ¤t_function->context);
618 * pops symbols from the environment stack until @p new_top is the top element
620 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
622 stack_entry_t *stack = *stack_ptr;
623 size_t top = ARR_LEN(stack);
626 assert(new_top <= top);
630 for(i = top; i > new_top; --i) {
631 stack_entry_t *entry = & stack[i - 1];
633 declaration_t *old_declaration = entry->old_declaration;
634 symbol_t *symbol = entry->symbol;
635 namespace_t namespc = (namespace_t)entry->namespc;
637 /* replace/remove declaration */
638 declaration_t *declaration = symbol->declaration;
639 assert(declaration != NULL);
640 if(declaration->namespc == namespc) {
641 if(old_declaration == NULL) {
642 symbol->declaration = declaration->symbol_next;
644 symbol->declaration = old_declaration;
647 declaration_t *iter_last = declaration;
648 declaration_t *iter = declaration->symbol_next;
649 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
650 /* replace an entry? */
651 if(iter->namespc == namespc) {
652 assert(iter_last != NULL);
653 iter_last->symbol_next = old_declaration;
654 old_declaration->symbol_next = iter->symbol_next;
658 assert(iter != NULL);
662 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
665 static void environment_pop_to(size_t new_top)
667 stack_pop_to(&environment_stack, new_top);
670 static void label_pop_to(size_t new_top)
672 stack_pop_to(&label_stack, new_top);
676 static int get_rank(const type_t *type)
678 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
679 * and esp. footnote 108). However we can't fold constants (yet), so we
680 * can't decide wether unsigned int is possible, while int always works.
681 * (unsigned int would be preferable when possible... for stuff like
682 * struct { enum { ... } bla : 4; } ) */
683 if(type->type == TYPE_ENUM)
684 return ATOMIC_TYPE_INT;
686 assert(type->type == TYPE_ATOMIC);
687 const atomic_type_t *atomic_type = &type->atomic;
688 atomic_type_type_t atype = atomic_type->atype;
692 static type_t *promote_integer(type_t *type)
694 if(get_rank(type) < ATOMIC_TYPE_INT)
700 static expression_t *create_cast_expression(expression_t *expression,
703 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
705 cast->expression.type = EXPR_UNARY;
706 cast->type = UNEXPR_CAST;
707 cast->value = expression;
708 cast->expression.datatype = dest_type;
710 return (expression_t*) cast;
713 static bool is_null_expression(const expression_t *const expression)
715 if (expression->type != EXPR_CONST)
718 type_t *const type = skip_typeref(expression->base.datatype);
719 if (!is_type_integer(type))
722 return expression->conste.v.int_value == 0;
725 static expression_t *create_implicit_cast(expression_t *expression,
728 type_t *source_type = expression->base.datatype;
730 if(source_type == NULL)
733 source_type = skip_typeref(source_type);
734 dest_type = skip_typeref(dest_type);
736 if(source_type == dest_type)
739 switch (dest_type->type) {
741 /* TODO warning for implicitly converting to enum */
743 if (source_type->type != TYPE_ATOMIC &&
744 source_type->type != TYPE_ENUM) {
745 panic("casting of non-atomic types not implemented yet");
748 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
749 type_error_incompatible("can't cast types",
750 expression->base.source_position, source_type,
755 return create_cast_expression(expression, dest_type);
758 switch (source_type->type) {
760 if (is_null_expression(expression)) {
761 return create_cast_expression(expression, dest_type);
766 if (pointers_compatible(source_type, dest_type)) {
767 return create_cast_expression(expression, dest_type);
772 array_type_t *array_type = &source_type->array;
773 pointer_type_t *pointer_type = &dest_type->pointer;
774 if (types_compatible(array_type->element_type,
775 pointer_type->points_to)) {
776 return create_cast_expression(expression, dest_type);
782 panic("casting of non-atomic types not implemented yet");
785 type_error_incompatible("can't implicitly cast types",
786 expression->base.source_position, source_type, dest_type);
790 panic("casting of non-atomic types not implemented yet");
794 /** Implements the rules from § 6.5.16.1 */
795 static void semantic_assign(type_t *orig_type_left, expression_t **right,
798 type_t *orig_type_right = (*right)->base.datatype;
800 if(orig_type_right == NULL)
803 type_t *const type_left = skip_typeref(orig_type_left);
804 type_t *const type_right = skip_typeref(orig_type_right);
806 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
807 (is_type_pointer(type_left) && is_null_expression(*right)) ||
808 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
809 && is_type_pointer(type_right))) {
810 *right = create_implicit_cast(*right, type_left);
814 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
815 pointer_type_t *pointer_type_left = &type_left->pointer;
816 pointer_type_t *pointer_type_right = &type_right->pointer;
817 type_t *points_to_left = pointer_type_left->points_to;
818 type_t *points_to_right = pointer_type_right->points_to;
820 points_to_left = skip_typeref(points_to_left);
821 points_to_right = skip_typeref(points_to_right);
823 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
824 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
825 && !types_compatible(points_to_left, points_to_right)) {
826 goto incompatible_assign_types;
829 /* the left type has all qualifiers from the right type */
830 unsigned missing_qualifiers
831 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
832 if(missing_qualifiers != 0) {
833 parser_print_error_prefix();
834 fprintf(stderr, "destination type ");
835 print_type_quoted(type_left);
836 fprintf(stderr, " in %s from type ", context);
837 print_type_quoted(type_right);
838 fprintf(stderr, " lacks qualifiers '");
839 print_type_qualifiers(missing_qualifiers);
840 fprintf(stderr, "' in pointed-to type\n");
844 *right = create_implicit_cast(*right, type_left);
848 if (is_type_compound(type_left)
849 && types_compatible(type_left, type_right)) {
850 *right = create_implicit_cast(*right, type_left);
854 incompatible_assign_types:
855 /* TODO: improve error message */
856 parser_print_error_prefix();
857 fprintf(stderr, "incompatible types in %s\n", context);
858 parser_print_error_prefix();
859 print_type_quoted(type_left);
860 fputs(" <- ", stderr);
861 print_type_quoted(type_right);
865 static expression_t *parse_constant_expression(void)
867 /* start parsing at precedence 7 (conditional expression) */
868 return parse_sub_expression(7);
871 static expression_t *parse_assignment_expression(void)
873 /* start parsing at precedence 2 (assignment expression) */
874 return parse_sub_expression(2);
877 typedef struct declaration_specifiers_t declaration_specifiers_t;
878 struct declaration_specifiers_t {
879 unsigned char storage_class;
884 static void parse_compound_type_entries(void);
885 static declaration_t *parse_declarator(
886 const declaration_specifiers_t *specifiers, type_t *type,
887 bool may_be_abstract);
888 static declaration_t *record_declaration(declaration_t *declaration);
890 static const char *parse_string_literals(void)
892 assert(token.type == T_STRING_LITERAL);
893 const char *result = token.v.string;
897 while(token.type == T_STRING_LITERAL) {
898 result = concat_strings(result, token.v.string);
905 static void parse_attributes(void)
909 case T___attribute__: {
917 parse_error("EOF while parsing attribute");
936 if(token.type != T_STRING_LITERAL) {
937 parse_error_expected("while parsing assembler attribute",
942 parse_string_literals();
947 goto attributes_finished;
956 static designator_t *parse_designation(void)
958 if(token.type != '[' && token.type != '.')
961 designator_t *result = NULL;
962 designator_t *last = NULL;
965 designator_t *designator;
968 designator = allocate_ast_zero(sizeof(designator[0]));
970 designator->array_access = parse_constant_expression();
974 designator = allocate_ast_zero(sizeof(designator[0]));
976 if(token.type != T_IDENTIFIER) {
977 parse_error_expected("while parsing designator",
981 designator->symbol = token.v.symbol;
989 assert(designator != NULL);
991 last->next = designator;
1000 static initializer_t *initializer_from_string(array_type_t *type,
1003 /* TODO: check len vs. size of array type */
1006 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1007 initializer->string.string = string;
1012 static initializer_t *initializer_from_expression(type_t *type,
1013 expression_t *expression)
1015 /* TODO check that expression is a constant expression */
1017 /* § 6.7.8.14/15 char array may be initialized by string literals */
1018 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1019 array_type_t *array_type = &type->array;
1020 type_t *element_type = array_type->element_type;
1022 if(element_type->type == TYPE_ATOMIC) {
1023 atomic_type_t *atomic_type = &element_type->atomic;
1024 atomic_type_type_t atype = atomic_type->atype;
1026 /* TODO handle wide strings */
1027 if(atype == ATOMIC_TYPE_CHAR
1028 || atype == ATOMIC_TYPE_SCHAR
1029 || atype == ATOMIC_TYPE_UCHAR) {
1031 string_literal_expression_t *literal
1032 = &expression->string_literal;
1033 return initializer_from_string(array_type, literal->value);
1038 semantic_assign(type, &expression, "initializer");
1040 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1041 result->value.value = expression;
1046 static initializer_t *parse_sub_initializer(type_t *type,
1047 expression_t *expression,
1048 type_t *expression_type);
1050 static initializer_t *parse_sub_initializer_elem(type_t *type)
1052 if(token.type == '{') {
1053 return parse_sub_initializer(type, NULL, NULL);
1056 expression_t *expression = parse_assignment_expression();
1057 type_t *expression_type = skip_typeref(expression->base.datatype);
1059 return parse_sub_initializer(type, expression, expression_type);
1062 static bool had_initializer_brace_warning;
1064 static initializer_t *parse_sub_initializer(type_t *type,
1065 expression_t *expression,
1066 type_t *expression_type)
1068 if(is_type_scalar(type)) {
1069 /* there might be extra {} hierarchies */
1070 if(token.type == '{') {
1072 if(!had_initializer_brace_warning) {
1073 parse_warning("braces around scalar initializer");
1074 had_initializer_brace_warning = true;
1076 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1077 if(token.type == ',') {
1079 /* TODO: warn about excessive elements */
1085 if(expression == NULL) {
1086 expression = parse_assignment_expression();
1088 return initializer_from_expression(type, expression);
1091 /* TODO: ignore qualifiers, comparing pointers is probably
1093 if(expression != NULL && expression_type == type) {
1094 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1097 semantic_assign(type, &expression, "initializer");
1099 result->value.value = expression;
1104 bool read_paren = false;
1105 if(token.type == '{') {
1110 /* descend into subtype */
1111 initializer_t *result = NULL;
1112 initializer_t **elems;
1113 if(type->type == TYPE_ARRAY) {
1114 array_type_t *array_type = &type->array;
1115 type_t *element_type = array_type->element_type;
1116 element_type = skip_typeref(element_type);
1119 had_initializer_brace_warning = false;
1120 if(expression == NULL) {
1121 sub = parse_sub_initializer_elem(element_type);
1123 sub = parse_sub_initializer(element_type, expression,
1127 /* didn't match the subtypes -> try the parent type */
1129 assert(!read_paren);
1133 elems = NEW_ARR_F(initializer_t*, 0);
1134 ARR_APP1(initializer_t*, elems, sub);
1137 if(token.type == '}')
1140 if(token.type == '}')
1143 sub = parse_sub_initializer(element_type, NULL, NULL);
1145 /* TODO error, do nicer cleanup */
1146 parse_error("member initializer didn't match");
1150 ARR_APP1(initializer_t*, elems, sub);
1153 assert(type->type == TYPE_COMPOUND_STRUCT
1154 || type->type == TYPE_COMPOUND_UNION);
1155 compound_type_t *compound_type = &type->compound;
1156 context_t *context = & compound_type->declaration->context;
1158 declaration_t *first = context->declarations;
1161 type_t *first_type = first->type;
1162 first_type = skip_typeref(first_type);
1165 had_initializer_brace_warning = false;
1166 if(expression == NULL) {
1167 sub = parse_sub_initializer_elem(first_type);
1169 sub = parse_sub_initializer(first_type, expression,expression_type);
1172 /* didn't match the subtypes -> try our parent type */
1174 assert(!read_paren);
1178 elems = NEW_ARR_F(initializer_t*, 0);
1179 ARR_APP1(initializer_t*, elems, sub);
1181 declaration_t *iter = first->next;
1182 for( ; iter != NULL; iter = iter->next) {
1183 if(iter->symbol == NULL)
1185 if(iter->namespc != NAMESPACE_NORMAL)
1188 if(token.type == '}')
1191 if(token.type == '}')
1194 type_t *iter_type = iter->type;
1195 iter_type = skip_typeref(iter_type);
1197 sub = parse_sub_initializer(iter_type, NULL, NULL);
1199 /* TODO error, do nicer cleanup*/
1200 parse_error("member initializer didn't match");
1204 ARR_APP1(initializer_t*, elems, sub);
1208 int len = ARR_LEN(elems);
1209 size_t elems_size = sizeof(initializer_t*) * len;
1211 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1213 init->initializer.type = INITIALIZER_LIST;
1215 memcpy(init->initializers, elems, elems_size);
1218 result = (initializer_t*) init;
1221 if(token.type == ',')
1228 static initializer_t *parse_initializer(type_t *type)
1230 initializer_t *result;
1232 type = skip_typeref(type);
1234 if(token.type != '{') {
1235 expression_t *expression = parse_assignment_expression();
1236 return initializer_from_expression(type, expression);
1239 if(is_type_scalar(type)) {
1243 expression_t *expression = parse_assignment_expression();
1244 result = initializer_from_expression(type, expression);
1246 if(token.type == ',')
1252 result = parse_sub_initializer(type, NULL, NULL);
1260 static declaration_t *parse_compound_type_specifier(bool is_struct)
1268 symbol_t *symbol = NULL;
1269 declaration_t *declaration = NULL;
1271 if (token.type == T___attribute__) {
1276 if(token.type == T_IDENTIFIER) {
1277 symbol = token.v.symbol;
1281 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1283 declaration = get_declaration(symbol, NAMESPACE_UNION);
1285 } else if(token.type != '{') {
1287 parse_error_expected("while parsing struct type specifier",
1288 T_IDENTIFIER, '{', 0);
1290 parse_error_expected("while parsing union type specifier",
1291 T_IDENTIFIER, '{', 0);
1297 if(declaration == NULL) {
1298 declaration = allocate_ast_zero(sizeof(declaration[0]));
1301 declaration->namespc = NAMESPACE_STRUCT;
1303 declaration->namespc = NAMESPACE_UNION;
1305 declaration->source_position = token.source_position;
1306 declaration->symbol = symbol;
1307 record_declaration(declaration);
1310 if(token.type == '{') {
1311 if(declaration->init.is_defined) {
1312 assert(symbol != NULL);
1313 parser_print_error_prefix();
1314 fprintf(stderr, "multiple definition of %s %s\n",
1315 is_struct ? "struct" : "union", symbol->string);
1316 declaration->context.declarations = NULL;
1318 declaration->init.is_defined = true;
1320 int top = environment_top();
1321 context_t *last_context = context;
1322 set_context(& declaration->context);
1324 parse_compound_type_entries();
1327 assert(context == & declaration->context);
1328 set_context(last_context);
1329 environment_pop_to(top);
1335 static void parse_enum_entries(enum_type_t *const enum_type)
1339 if(token.type == '}') {
1341 parse_error("empty enum not allowed");
1346 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1348 if(token.type != T_IDENTIFIER) {
1349 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1353 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1354 entry->type = (type_t*) enum_type;
1355 entry->symbol = token.v.symbol;
1356 entry->source_position = token.source_position;
1359 if(token.type == '=') {
1361 entry->init.enum_value = parse_constant_expression();
1366 record_declaration(entry);
1368 if(token.type != ',')
1371 } while(token.type != '}');
1376 static type_t *parse_enum_specifier(void)
1380 declaration_t *declaration;
1383 if(token.type == T_IDENTIFIER) {
1384 symbol = token.v.symbol;
1387 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1388 } else if(token.type != '{') {
1389 parse_error_expected("while parsing enum type specifier",
1390 T_IDENTIFIER, '{', 0);
1397 if(declaration == NULL) {
1398 declaration = allocate_ast_zero(sizeof(declaration[0]));
1400 declaration->namespc = NAMESPACE_ENUM;
1401 declaration->source_position = token.source_position;
1402 declaration->symbol = symbol;
1405 type_t *const type = allocate_type_zero(TYPE_ENUM);
1406 type->enumt.declaration = declaration;
1408 if(token.type == '{') {
1409 if(declaration->init.is_defined) {
1410 parser_print_error_prefix();
1411 fprintf(stderr, "multiple definitions of enum %s\n",
1414 record_declaration(declaration);
1415 declaration->init.is_defined = 1;
1417 parse_enum_entries(&type->enumt);
1425 * if a symbol is a typedef to another type, return true
1427 static bool is_typedef_symbol(symbol_t *symbol)
1429 const declaration_t *const declaration =
1430 get_declaration(symbol, NAMESPACE_NORMAL);
1432 declaration != NULL &&
1433 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1436 static type_t *parse_typeof(void)
1444 expression_t *expression = NULL;
1447 switch(token.type) {
1448 case T___extension__:
1449 /* this can be a prefix to a typename or an expression */
1450 /* we simply eat it now. */
1453 } while(token.type == T___extension__);
1457 if(is_typedef_symbol(token.v.symbol)) {
1458 type = parse_typename();
1460 expression = parse_expression();
1461 type = expression->base.datatype;
1466 type = parse_typename();
1470 expression = parse_expression();
1471 type = expression->base.datatype;
1477 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1478 typeof_type->typeoft.expression = expression;
1479 typeof_type->typeoft.typeof_type = type;
1485 SPECIFIER_SIGNED = 1 << 0,
1486 SPECIFIER_UNSIGNED = 1 << 1,
1487 SPECIFIER_LONG = 1 << 2,
1488 SPECIFIER_INT = 1 << 3,
1489 SPECIFIER_DOUBLE = 1 << 4,
1490 SPECIFIER_CHAR = 1 << 5,
1491 SPECIFIER_SHORT = 1 << 6,
1492 SPECIFIER_LONG_LONG = 1 << 7,
1493 SPECIFIER_FLOAT = 1 << 8,
1494 SPECIFIER_BOOL = 1 << 9,
1495 SPECIFIER_VOID = 1 << 10,
1496 #ifdef PROVIDE_COMPLEX
1497 SPECIFIER_COMPLEX = 1 << 11,
1498 SPECIFIER_IMAGINARY = 1 << 12,
1502 static type_t *create_builtin_type(symbol_t *symbol)
1504 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1505 type->builtin.symbol = symbol;
1507 type->builtin.real_type = type_int;
1512 static type_t *get_typedef_type(symbol_t *symbol)
1514 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1515 if(declaration == NULL
1516 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1519 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1520 type->typedeft.declaration = declaration;
1525 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1527 type_t *type = NULL;
1528 unsigned type_qualifiers = 0;
1529 unsigned type_specifiers = 0;
1533 switch(token.type) {
1536 #define MATCH_STORAGE_CLASS(token, class) \
1538 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1539 parse_error("multiple storage classes in declaration " \
1542 specifiers->storage_class = class; \
1546 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1547 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1548 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1549 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1550 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1552 /* type qualifiers */
1553 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1555 type_qualifiers |= qualifier; \
1559 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1560 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1561 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1563 case T___extension__:
1568 /* type specifiers */
1569 #define MATCH_SPECIFIER(token, specifier, name) \
1572 if(type_specifiers & specifier) { \
1573 parse_error("multiple " name " type specifiers given"); \
1575 type_specifiers |= specifier; \
1579 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1580 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1581 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1582 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1583 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1584 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1585 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1586 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1587 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1588 #ifdef PROVIDE_COMPLEX
1589 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1590 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1594 specifiers->is_inline = true;
1599 if(type_specifiers & SPECIFIER_LONG_LONG) {
1600 parse_error("multiple type specifiers given");
1601 } else if(type_specifiers & SPECIFIER_LONG) {
1602 type_specifiers |= SPECIFIER_LONG_LONG;
1604 type_specifiers |= SPECIFIER_LONG;
1608 /* TODO: if type != NULL for the following rules should issue
1611 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1613 type->compound.declaration = parse_compound_type_specifier(true);
1617 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1619 type->compound.declaration = parse_compound_type_specifier(false);
1623 type = parse_enum_specifier();
1626 type = parse_typeof();
1628 case T___builtin_va_list:
1629 type = create_builtin_type(token.v.symbol);
1633 case T___attribute__:
1638 case T_IDENTIFIER: {
1639 type_t *typedef_type = get_typedef_type(token.v.symbol);
1641 if(typedef_type == NULL)
1642 goto finish_specifiers;
1645 type = typedef_type;
1649 /* function specifier */
1651 goto finish_specifiers;
1658 atomic_type_type_t atomic_type;
1660 /* match valid basic types */
1661 switch(type_specifiers) {
1662 case SPECIFIER_VOID:
1663 atomic_type = ATOMIC_TYPE_VOID;
1665 case SPECIFIER_CHAR:
1666 atomic_type = ATOMIC_TYPE_CHAR;
1668 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1669 atomic_type = ATOMIC_TYPE_SCHAR;
1671 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1672 atomic_type = ATOMIC_TYPE_UCHAR;
1674 case SPECIFIER_SHORT:
1675 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1676 case SPECIFIER_SHORT | SPECIFIER_INT:
1677 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1678 atomic_type = ATOMIC_TYPE_SHORT;
1680 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1681 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1682 atomic_type = ATOMIC_TYPE_USHORT;
1685 case SPECIFIER_SIGNED:
1686 case SPECIFIER_SIGNED | SPECIFIER_INT:
1687 atomic_type = ATOMIC_TYPE_INT;
1689 case SPECIFIER_UNSIGNED:
1690 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1691 atomic_type = ATOMIC_TYPE_UINT;
1693 case SPECIFIER_LONG:
1694 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1695 case SPECIFIER_LONG | SPECIFIER_INT:
1696 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1697 atomic_type = ATOMIC_TYPE_LONG;
1699 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1700 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1701 atomic_type = ATOMIC_TYPE_ULONG;
1703 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1704 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1705 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1706 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1708 atomic_type = ATOMIC_TYPE_LONGLONG;
1710 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1713 atomic_type = ATOMIC_TYPE_ULONGLONG;
1715 case SPECIFIER_FLOAT:
1716 atomic_type = ATOMIC_TYPE_FLOAT;
1718 case SPECIFIER_DOUBLE:
1719 atomic_type = ATOMIC_TYPE_DOUBLE;
1721 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1722 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1724 case SPECIFIER_BOOL:
1725 atomic_type = ATOMIC_TYPE_BOOL;
1727 #ifdef PROVIDE_COMPLEX
1728 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1729 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1731 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1732 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1734 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1735 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1737 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1738 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1740 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1741 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1743 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1744 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1748 /* invalid specifier combination, give an error message */
1749 if(type_specifiers == 0) {
1751 parse_warning("no type specifiers in declaration (using int)");
1752 atomic_type = ATOMIC_TYPE_INT;
1755 parse_error("no type specifiers given in declaration");
1757 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1758 (type_specifiers & SPECIFIER_UNSIGNED)) {
1759 parse_error("signed and unsigned specifiers gives");
1760 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1761 parse_error("only integer types can be signed or unsigned");
1763 parse_error("multiple datatypes in declaration");
1765 atomic_type = ATOMIC_TYPE_INVALID;
1768 type = allocate_type_zero(TYPE_ATOMIC);
1769 type->atomic.atype = atomic_type;
1772 if(type_specifiers != 0) {
1773 parse_error("multiple datatypes in declaration");
1777 type->base.qualifiers = type_qualifiers;
1779 type_t *result = typehash_insert(type);
1780 if(newtype && result != type) {
1784 specifiers->type = result;
1787 static type_qualifiers_t parse_type_qualifiers(void)
1789 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1792 switch(token.type) {
1793 /* type qualifiers */
1794 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1795 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1796 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1799 return type_qualifiers;
1804 static void parse_identifier_list(void)
1807 if(token.type != T_IDENTIFIER) {
1808 parse_error_expected("while parsing parameter identifier list",
1813 if(token.type != ',')
1819 static declaration_t *parse_parameter(void)
1821 declaration_specifiers_t specifiers;
1822 memset(&specifiers, 0, sizeof(specifiers));
1824 parse_declaration_specifiers(&specifiers);
1826 declaration_t *declaration
1827 = parse_declarator(&specifiers, specifiers.type, true);
1829 /* TODO check declaration constraints for parameters */
1830 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1831 parse_error("typedef not allowed in parameter list");
1834 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1836 if (declaration->type->type == TYPE_ARRAY) {
1837 const array_type_t *const arr_type = &declaration->type->array;
1838 type_t *element_type = arr_type->element_type;
1839 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1845 static declaration_t *parse_parameters(function_type_t *type)
1847 if(token.type == T_IDENTIFIER) {
1848 symbol_t *symbol = token.v.symbol;
1849 if(!is_typedef_symbol(symbol)) {
1850 /* TODO: K&R style C parameters */
1851 parse_identifier_list();
1856 if(token.type == ')') {
1857 type->unspecified_parameters = 1;
1860 if(token.type == T_void && look_ahead(1)->type == ')') {
1865 declaration_t *declarations = NULL;
1866 declaration_t *declaration;
1867 declaration_t *last_declaration = NULL;
1868 function_parameter_t *parameter;
1869 function_parameter_t *last_parameter = NULL;
1872 switch(token.type) {
1876 return declarations;
1879 case T___extension__:
1881 declaration = parse_parameter();
1883 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1884 memset(parameter, 0, sizeof(parameter[0]));
1885 parameter->type = declaration->type;
1887 if(last_parameter != NULL) {
1888 last_declaration->next = declaration;
1889 last_parameter->next = parameter;
1891 type->parameters = parameter;
1892 declarations = declaration;
1894 last_parameter = parameter;
1895 last_declaration = declaration;
1899 return declarations;
1901 if(token.type != ',')
1902 return declarations;
1912 } construct_type_type_t;
1914 typedef struct construct_type_t construct_type_t;
1915 struct construct_type_t {
1916 construct_type_type_t type;
1917 construct_type_t *next;
1920 typedef struct parsed_pointer_t parsed_pointer_t;
1921 struct parsed_pointer_t {
1922 construct_type_t construct_type;
1923 type_qualifiers_t type_qualifiers;
1926 typedef struct construct_function_type_t construct_function_type_t;
1927 struct construct_function_type_t {
1928 construct_type_t construct_type;
1929 type_t *function_type;
1932 typedef struct parsed_array_t parsed_array_t;
1933 struct parsed_array_t {
1934 construct_type_t construct_type;
1935 type_qualifiers_t type_qualifiers;
1941 typedef struct construct_base_type_t construct_base_type_t;
1942 struct construct_base_type_t {
1943 construct_type_t construct_type;
1947 static construct_type_t *parse_pointer_declarator(void)
1951 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1952 memset(pointer, 0, sizeof(pointer[0]));
1953 pointer->construct_type.type = CONSTRUCT_POINTER;
1954 pointer->type_qualifiers = parse_type_qualifiers();
1956 return (construct_type_t*) pointer;
1959 static construct_type_t *parse_array_declarator(void)
1963 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1964 memset(array, 0, sizeof(array[0]));
1965 array->construct_type.type = CONSTRUCT_ARRAY;
1967 if(token.type == T_static) {
1968 array->is_static = true;
1972 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1973 if(type_qualifiers != 0) {
1974 if(token.type == T_static) {
1975 array->is_static = true;
1979 array->type_qualifiers = type_qualifiers;
1981 if(token.type == '*' && look_ahead(1)->type == ']') {
1982 array->is_variable = true;
1984 } else if(token.type != ']') {
1985 array->size = parse_assignment_expression();
1990 return (construct_type_t*) array;
1993 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1997 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1999 declaration_t *parameters = parse_parameters(&type->function);
2000 if(declaration != NULL) {
2001 declaration->context.declarations = parameters;
2004 construct_function_type_t *construct_function_type =
2005 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2006 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2007 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2008 construct_function_type->function_type = type;
2012 return (construct_type_t*) construct_function_type;
2015 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2016 bool may_be_abstract)
2018 /* construct a single linked list of construct_type_t's which describe
2019 * how to construct the final declarator type */
2020 construct_type_t *first = NULL;
2021 construct_type_t *last = NULL;
2024 while(token.type == '*') {
2025 construct_type_t *type = parse_pointer_declarator();
2036 /* TODO: find out if this is correct */
2039 construct_type_t *inner_types = NULL;
2041 switch(token.type) {
2043 if(declaration == NULL) {
2044 parse_error("no identifier expected in typename");
2046 declaration->symbol = token.v.symbol;
2047 declaration->source_position = token.source_position;
2053 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2059 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2060 /* avoid a loop in the outermost scope, because eat_statement doesn't
2062 if(token.type == '}' && current_function == NULL) {
2070 construct_type_t *p = last;
2073 construct_type_t *type;
2074 switch(token.type) {
2076 type = parse_function_declarator(declaration);
2079 type = parse_array_declarator();
2082 goto declarator_finished;
2085 /* insert in the middle of the list (behind p) */
2087 type->next = p->next;
2098 declarator_finished:
2101 /* append inner_types at the end of the list, we don't to set last anymore
2102 * as it's not needed anymore */
2104 assert(first == NULL);
2105 first = inner_types;
2107 last->next = inner_types;
2113 static type_t *construct_declarator_type(construct_type_t *construct_list,
2116 construct_type_t *iter = construct_list;
2117 for( ; iter != NULL; iter = iter->next) {
2118 switch(iter->type) {
2119 case CONSTRUCT_INVALID:
2120 panic("invalid type construction found");
2121 case CONSTRUCT_FUNCTION: {
2122 construct_function_type_t *construct_function_type
2123 = (construct_function_type_t*) iter;
2125 type_t *function_type = construct_function_type->function_type;
2127 function_type->function.result_type = type;
2129 type = function_type;
2133 case CONSTRUCT_POINTER: {
2134 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2135 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2136 pointer_type->pointer.points_to = type;
2137 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2139 type = pointer_type;
2143 case CONSTRUCT_ARRAY: {
2144 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2145 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2147 array_type->base.qualifiers = parsed_array->type_qualifiers;
2148 array_type->array.element_type = type;
2149 array_type->array.is_static = parsed_array->is_static;
2150 array_type->array.is_variable = parsed_array->is_variable;
2151 array_type->array.size = parsed_array->size;
2158 type_t *hashed_type = typehash_insert(type);
2159 if(hashed_type != type) {
2160 /* the function type was constructed earlier freeing it here will
2161 * destroy other types... */
2162 if(iter->type != CONSTRUCT_FUNCTION) {
2172 static declaration_t *parse_declarator(
2173 const declaration_specifiers_t *specifiers,
2174 type_t *type, bool may_be_abstract)
2176 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2177 declaration->storage_class = specifiers->storage_class;
2178 declaration->is_inline = specifiers->is_inline;
2180 construct_type_t *construct_type
2181 = parse_inner_declarator(declaration, may_be_abstract);
2182 declaration->type = construct_declarator_type(construct_type, type);
2184 if(construct_type != NULL) {
2185 obstack_free(&temp_obst, construct_type);
2191 static type_t *parse_abstract_declarator(type_t *base_type)
2193 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2195 type_t *result = construct_declarator_type(construct_type, base_type);
2196 if(construct_type != NULL) {
2197 obstack_free(&temp_obst, construct_type);
2203 static declaration_t *record_declaration(declaration_t *declaration)
2205 assert(context != NULL);
2207 symbol_t *symbol = declaration->symbol;
2208 if(symbol != NULL) {
2209 declaration_t *alias = environment_push(declaration);
2210 if(alias != declaration)
2213 declaration->parent_context = context;
2216 if(last_declaration != NULL) {
2217 last_declaration->next = declaration;
2219 context->declarations = declaration;
2221 last_declaration = declaration;
2226 static void parser_error_multiple_definition(declaration_t *previous,
2227 declaration_t *declaration)
2229 parser_print_error_prefix_pos(declaration->source_position);
2230 fprintf(stderr, "multiple definition of symbol '%s'\n",
2231 declaration->symbol->string);
2232 parser_print_error_prefix_pos(previous->source_position);
2233 fprintf(stderr, "this is the location of the previous definition.\n");
2236 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2239 declaration_t *ndeclaration
2240 = parse_declarator(specifiers, specifiers->type, false);
2242 declaration_t *declaration = record_declaration(ndeclaration);
2244 type_t *orig_type = declaration->type;
2245 type_t *type = skip_typeref(orig_type);
2246 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2247 parser_print_warning_prefix_pos(declaration->source_position);
2248 fprintf(stderr, "variable '%s' declared 'inline'\n",
2249 declaration->symbol->string);
2252 if(token.type == '=') {
2255 /* TODO: check that this is an allowed type (no function type) */
2257 if(declaration->init.initializer != NULL) {
2258 parser_error_multiple_definition(declaration, ndeclaration);
2261 initializer_t *initializer = parse_initializer(type);
2263 if(type->type == TYPE_ARRAY && initializer != NULL) {
2264 array_type_t *array_type = &type->array;
2266 if(array_type->size == NULL) {
2268 *cnst = allocate_ast_zero(sizeof(cnst[0]));
2270 cnst->expression.type = EXPR_CONST;
2271 cnst->expression.datatype = type_size_t;
2273 if(initializer->type == INITIALIZER_LIST) {
2274 initializer_list_t *list = &initializer->list;
2275 cnst->v.int_value = list->len;
2277 assert(initializer->type == INITIALIZER_STRING);
2278 initializer_string_t *string = &initializer->string;
2279 cnst->v.int_value = strlen(string->string) + 1;
2282 array_type->size = (expression_t*) cnst;
2287 ndeclaration->init.initializer = initializer;
2288 } else if(token.type == '{') {
2289 if(type->type != TYPE_FUNCTION) {
2290 parser_print_error_prefix();
2291 fprintf(stderr, "declarator '");
2292 print_type_ext(orig_type, declaration->symbol, NULL);
2293 fprintf(stderr, "' has a body but is not a function type.\n");
2298 if(declaration->init.statement != NULL) {
2299 parser_error_multiple_definition(declaration, ndeclaration);
2301 if(ndeclaration != declaration) {
2302 memcpy(&declaration->context, &ndeclaration->context,
2303 sizeof(declaration->context));
2306 int top = environment_top();
2307 context_t *last_context = context;
2308 set_context(&declaration->context);
2310 /* push function parameters */
2311 declaration_t *parameter = declaration->context.declarations;
2312 for( ; parameter != NULL; parameter = parameter->next) {
2313 environment_push(parameter);
2316 int label_stack_top = label_top();
2317 declaration_t *old_current_function = current_function;
2318 current_function = declaration;
2320 statement_t *statement = parse_compound_statement();
2322 assert(current_function == declaration);
2323 current_function = old_current_function;
2324 label_pop_to(label_stack_top);
2326 assert(context == &declaration->context);
2327 set_context(last_context);
2328 environment_pop_to(top);
2330 declaration->init.statement = statement;
2334 if(token.type != ',')
2341 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2344 if(token.type == ':') {
2346 parse_constant_expression();
2347 /* TODO (bitfields) */
2349 declaration_t *declaration
2350 = parse_declarator(specifiers, specifiers->type, true);
2352 /* TODO: check constraints for struct declarations */
2353 /* TODO: check for doubled fields */
2354 record_declaration(declaration);
2356 if(token.type == ':') {
2358 parse_constant_expression();
2359 /* TODO (bitfields) */
2363 if(token.type != ',')
2370 static void parse_compound_type_entries(void)
2374 while(token.type != '}' && token.type != T_EOF) {
2375 declaration_specifiers_t specifiers;
2376 memset(&specifiers, 0, sizeof(specifiers));
2377 parse_declaration_specifiers(&specifiers);
2379 parse_struct_declarators(&specifiers);
2381 if(token.type == T_EOF) {
2382 parse_error("unexpected error while parsing struct");
2387 static void parse_declaration(void)
2389 source_position_t source_position = token.source_position;
2391 declaration_specifiers_t specifiers;
2392 memset(&specifiers, 0, sizeof(specifiers));
2393 parse_declaration_specifiers(&specifiers);
2395 if(token.type == ';') {
2396 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2397 parse_warning_pos(source_position,
2398 "useless keyword in empty declaration");
2400 switch (specifiers.type->type) {
2401 case TYPE_COMPOUND_STRUCT:
2402 case TYPE_COMPOUND_UNION: {
2403 const compound_type_t *const comp_type
2404 = &specifiers.type->compound;
2405 if (comp_type->declaration->symbol == NULL) {
2406 parse_warning_pos(source_position,
2407 "unnamed struct/union that defines no instances");
2412 case TYPE_ENUM: break;
2415 parse_warning_pos(source_position, "empty declaration");
2421 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2423 declaration->type = specifiers.type;
2424 declaration->storage_class = specifiers.storage_class;
2425 declaration->source_position = source_position;
2426 record_declaration(declaration);
2429 parse_init_declarators(&specifiers);
2432 static type_t *parse_typename(void)
2434 declaration_specifiers_t specifiers;
2435 memset(&specifiers, 0, sizeof(specifiers));
2436 parse_declaration_specifiers(&specifiers);
2437 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2438 /* TODO: improve error message, user does probably not know what a
2439 * storage class is...
2441 parse_error("typename may not have a storage class");
2444 type_t *result = parse_abstract_declarator(specifiers.type);
2452 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2453 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2454 expression_t *left);
2456 typedef struct expression_parser_function_t expression_parser_function_t;
2457 struct expression_parser_function_t {
2458 unsigned precedence;
2459 parse_expression_function parser;
2460 unsigned infix_precedence;
2461 parse_expression_infix_function infix_parser;
2464 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2466 static expression_t *make_invalid_expression(void)
2468 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2469 expression->type = EXPR_INVALID;
2470 expression->base.source_position = token.source_position;
2474 static expression_t *expected_expression_error(void)
2476 parser_print_error_prefix();
2477 fprintf(stderr, "expected expression, got token ");
2478 print_token(stderr, & token);
2479 fprintf(stderr, "\n");
2483 return make_invalid_expression();
2486 static expression_t *parse_string_const(void)
2488 string_literal_expression_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2490 cnst->expression.type = EXPR_STRING_LITERAL;
2491 cnst->expression.datatype = type_string;
2492 cnst->value = parse_string_literals();
2494 return (expression_t*) cnst;
2497 static expression_t *parse_int_const(void)
2499 const_expression_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2501 cnst->expression.type = EXPR_CONST;
2502 cnst->expression.datatype = token.datatype;
2503 cnst->v.int_value = token.v.intvalue;
2507 return (expression_t*) cnst;
2510 static expression_t *parse_float_const(void)
2512 const_expression_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2514 cnst->expression.type = EXPR_CONST;
2515 cnst->expression.datatype = token.datatype;
2516 cnst->v.float_value = token.v.floatvalue;
2520 return (expression_t*) cnst;
2523 static declaration_t *create_implicit_function(symbol_t *symbol,
2524 const source_position_t source_position)
2526 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2527 ntype->function.result_type = type_int;
2528 ntype->function.unspecified_parameters = true;
2530 type_t *type = typehash_insert(ntype);
2535 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2537 declaration->storage_class = STORAGE_CLASS_EXTERN;
2538 declaration->type = type;
2539 declaration->symbol = symbol;
2540 declaration->source_position = source_position;
2542 /* prepend the implicit definition to the global context
2543 * this is safe since the symbol wasn't declared as anything else yet
2545 assert(symbol->declaration == NULL);
2547 context_t *last_context = context;
2548 context = global_context;
2550 environment_push(declaration);
2551 declaration->next = context->declarations;
2552 context->declarations = declaration;
2554 context = last_context;
2559 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2561 function_parameter_t *parameter
2562 = obstack_alloc(type_obst, sizeof(parameter[0]));
2563 memset(parameter, 0, sizeof(parameter[0]));
2564 parameter->type = argument_type;
2566 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2567 type->function.result_type = result_type;
2568 type->function.parameters = parameter;
2570 type_t *result = typehash_insert(type);
2571 if(result != type) {
2578 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2580 switch(symbol->ID) {
2581 case T___builtin_alloca:
2582 return make_function_1_type(type_void_ptr, type_size_t);
2584 panic("not implemented builtin symbol found");
2589 * performs automatic type cast as described in § 6.3.2.1
2591 static type_t *automatic_type_conversion(type_t *type)
2596 if(type->type == TYPE_ARRAY) {
2597 array_type_t *array_type = &type->array;
2598 type_t *element_type = array_type->element_type;
2599 unsigned qualifiers = array_type->type.qualifiers;
2601 return make_pointer_type(element_type, qualifiers);
2604 if(type->type == TYPE_FUNCTION) {
2605 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2612 * reverts the automatic casts of array to pointer types and function
2613 * to function-pointer types as defined § 6.3.2.1
2615 type_t *revert_automatic_type_conversion(const expression_t *expression)
2617 if(expression->base.datatype == NULL)
2620 switch(expression->type) {
2621 case EXPR_REFERENCE: {
2622 const reference_expression_t *ref
2623 = (const reference_expression_t*) expression;
2624 return ref->declaration->type;
2627 const select_expression_t *select
2628 = (const select_expression_t*) expression;
2629 return select->compound_entry->type;
2632 const unary_expression_t *unary
2633 = (const unary_expression_t*) expression;
2634 if(unary->type == UNEXPR_DEREFERENCE) {
2635 expression_t *value = unary->value;
2636 type_t *type = skip_typeref(value->base.datatype);
2637 pointer_type_t *pointer_type = &type->pointer;
2639 return pointer_type->points_to;
2643 case EXPR_BUILTIN_SYMBOL: {
2644 const builtin_symbol_expression_t *builtin
2645 = (const builtin_symbol_expression_t*) expression;
2646 return get_builtin_symbol_type(builtin->symbol);
2648 case EXPR_ARRAY_ACCESS: {
2649 const array_access_expression_t *array_access
2650 = &expression->array_access;
2651 const expression_t *array_ref = array_access->array_ref;
2652 type_t *type_left = skip_typeref(array_ref->base.datatype);
2653 assert(is_type_pointer(type_left));
2654 pointer_type_t *pointer_type = &type_left->pointer;
2655 return pointer_type->points_to;
2662 return expression->base.datatype;
2665 static expression_t *parse_reference(void)
2667 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2669 ref->expression.type = EXPR_REFERENCE;
2670 ref->symbol = token.v.symbol;
2672 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2674 source_position_t source_position = token.source_position;
2677 if(declaration == NULL) {
2679 /* an implicitly defined function */
2680 if(token.type == '(') {
2681 parser_print_prefix_pos(token.source_position);
2682 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2683 ref->symbol->string);
2685 declaration = create_implicit_function(ref->symbol,
2690 parser_print_error_prefix();
2691 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2692 return (expression_t*) ref;
2696 type_t *type = declaration->type;
2697 /* we always do the auto-type conversions; the & and sizeof parser contains
2698 * code to revert this! */
2699 type = automatic_type_conversion(type);
2701 ref->declaration = declaration;
2702 ref->expression.datatype = type;
2704 return (expression_t*) ref;
2707 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2711 /* TODO check if explicit cast is allowed and issue warnings/errors */
2714 static expression_t *parse_cast(void)
2716 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2718 cast->expression.type = EXPR_UNARY;
2719 cast->type = UNEXPR_CAST;
2720 cast->expression.source_position = token.source_position;
2722 type_t *type = parse_typename();
2725 expression_t *value = parse_sub_expression(20);
2727 check_cast_allowed(value, type);
2729 cast->expression.datatype = type;
2730 cast->value = value;
2732 return (expression_t*) cast;
2735 static expression_t *parse_statement_expression(void)
2737 statement_expression_t *expression
2738 = allocate_ast_zero(sizeof(expression[0]));
2739 expression->expression.type = EXPR_STATEMENT;
2741 statement_t *statement = parse_compound_statement();
2742 expression->statement = statement;
2743 if(statement == NULL) {
2748 assert(statement->type == STATEMENT_COMPOUND);
2749 compound_statement_t *compound_statement
2750 = (compound_statement_t*) statement;
2752 /* find last statement and use it's type */
2753 const statement_t *last_statement = NULL;
2754 const statement_t *iter = compound_statement->statements;
2755 for( ; iter != NULL; iter = iter->base.next) {
2756 last_statement = iter;
2759 if(last_statement->type == STATEMENT_EXPRESSION) {
2760 const expression_statement_t *expression_statement =
2761 (const expression_statement_t*) last_statement;
2762 expression->expression.datatype
2763 = expression_statement->expression->base.datatype;
2765 expression->expression.datatype = type_void;
2770 return (expression_t*) expression;
2773 static expression_t *parse_brace_expression(void)
2777 switch(token.type) {
2779 /* gcc extension: a stement expression */
2780 return parse_statement_expression();
2784 return parse_cast();
2786 if(is_typedef_symbol(token.v.symbol)) {
2787 return parse_cast();
2791 expression_t *result = parse_expression();
2797 static expression_t *parse_function_keyword(void)
2802 if (current_function == NULL) {
2803 parse_error("'__func__' used outside of a function");
2806 string_literal_expression_t *expression
2807 = allocate_ast_zero(sizeof(expression[0]));
2809 expression->expression.type = EXPR_FUNCTION;
2810 expression->expression.datatype = type_string;
2811 expression->value = "TODO: FUNCTION";
2813 return (expression_t*) expression;
2816 static expression_t *parse_pretty_function_keyword(void)
2818 eat(T___PRETTY_FUNCTION__);
2821 string_literal_expression_t *expression
2822 = allocate_ast_zero(sizeof(expression[0]));
2824 expression->expression.type = EXPR_PRETTY_FUNCTION;
2825 expression->expression.datatype = type_string;
2826 expression->value = "TODO: PRETTY FUNCTION";
2828 return (expression_t*) expression;
2831 static designator_t *parse_designator(void)
2833 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2835 if(token.type != T_IDENTIFIER) {
2836 parse_error_expected("while parsing member designator",
2841 result->symbol = token.v.symbol;
2844 designator_t *last_designator = result;
2846 if(token.type == '.') {
2848 if(token.type != T_IDENTIFIER) {
2849 parse_error_expected("while parsing member designator",
2854 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2855 designator->symbol = token.v.symbol;
2858 last_designator->next = designator;
2859 last_designator = designator;
2862 if(token.type == '[') {
2864 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2865 designator->array_access = parse_expression();
2866 if(designator->array_access == NULL) {
2872 last_designator->next = designator;
2873 last_designator = designator;
2882 static expression_t *parse_offsetof(void)
2884 eat(T___builtin_offsetof);
2886 offsetof_expression_t *expression
2887 = allocate_ast_zero(sizeof(expression[0]));
2888 expression->expression.type = EXPR_OFFSETOF;
2889 expression->expression.datatype = type_size_t;
2892 expression->type = parse_typename();
2894 expression->designator = parse_designator();
2897 return (expression_t*) expression;
2900 static expression_t *parse_va_arg(void)
2902 eat(T___builtin_va_arg);
2904 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2905 expression->expression.type = EXPR_VA_ARG;
2908 expression->arg = parse_assignment_expression();
2910 expression->expression.datatype = parse_typename();
2913 return (expression_t*) expression;
2916 static expression_t *parse_builtin_symbol(void)
2918 builtin_symbol_expression_t *expression
2919 = allocate_ast_zero(sizeof(expression[0]));
2920 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2922 expression->symbol = token.v.symbol;
2925 type_t *type = get_builtin_symbol_type(expression->symbol);
2926 type = automatic_type_conversion(type);
2928 expression->expression.datatype = type;
2929 return (expression_t*) expression;
2932 static expression_t *parse_primary_expression(void)
2934 switch(token.type) {
2936 return parse_int_const();
2937 case T_FLOATINGPOINT:
2938 return parse_float_const();
2939 case T_STRING_LITERAL:
2940 return parse_string_const();
2942 return parse_reference();
2943 case T___FUNCTION__:
2945 return parse_function_keyword();
2946 case T___PRETTY_FUNCTION__:
2947 return parse_pretty_function_keyword();
2948 case T___builtin_offsetof:
2949 return parse_offsetof();
2950 case T___builtin_va_arg:
2951 return parse_va_arg();
2952 case T___builtin_alloca:
2953 case T___builtin_expect:
2954 case T___builtin_va_start:
2955 case T___builtin_va_end:
2956 return parse_builtin_symbol();
2959 return parse_brace_expression();
2962 parser_print_error_prefix();
2963 fprintf(stderr, "unexpected token ");
2964 print_token(stderr, &token);
2965 fprintf(stderr, "\n");
2968 return make_invalid_expression();
2971 static expression_t *parse_array_expression(unsigned precedence,
2978 expression_t *inside = parse_expression();
2980 array_access_expression_t *array_access
2981 = allocate_ast_zero(sizeof(array_access[0]));
2983 array_access->expression.type = EXPR_ARRAY_ACCESS;
2985 type_t *type_left = left->base.datatype;
2986 type_t *type_inside = inside->base.datatype;
2987 type_t *result_type = NULL;
2989 if(type_left != NULL && type_inside != NULL) {
2990 type_left = skip_typeref(type_left);
2991 type_inside = skip_typeref(type_inside);
2993 if(is_type_pointer(type_left)) {
2994 pointer_type_t *pointer = &type_left->pointer;
2995 result_type = pointer->points_to;
2996 array_access->array_ref = left;
2997 array_access->index = inside;
2998 } else if(is_type_pointer(type_inside)) {
2999 pointer_type_t *pointer = &type_inside->pointer;
3000 result_type = pointer->points_to;
3001 array_access->array_ref = inside;
3002 array_access->index = left;
3003 array_access->flipped = true;
3005 parser_print_error_prefix();
3006 fprintf(stderr, "array access on object with non-pointer types ");
3007 print_type_quoted(type_left);
3008 fprintf(stderr, ", ");
3009 print_type_quoted(type_inside);
3010 fprintf(stderr, "\n");
3013 array_access->array_ref = left;
3014 array_access->index = inside;
3017 if(token.type != ']') {
3018 parse_error_expected("Problem while parsing array access", ']', 0);
3019 return (expression_t*) array_access;
3023 result_type = automatic_type_conversion(result_type);
3024 array_access->expression.datatype = result_type;
3026 return (expression_t*) array_access;
3029 static bool is_declaration_specifier(const token_t *token,
3030 bool only_type_specifiers)
3032 switch(token->type) {
3036 return is_typedef_symbol(token->v.symbol);
3039 if(only_type_specifiers)
3048 static expression_t *parse_sizeof(unsigned precedence)
3052 sizeof_expression_t *sizeof_expression
3053 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3054 sizeof_expression->expression.type = EXPR_SIZEOF;
3055 sizeof_expression->expression.datatype = type_size_t;
3057 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3059 sizeof_expression->type = parse_typename();
3062 expression_t *expression = parse_sub_expression(precedence);
3063 expression->base.datatype = revert_automatic_type_conversion(expression);
3065 sizeof_expression->type = expression->base.datatype;
3066 sizeof_expression->size_expression = expression;
3069 return (expression_t*) sizeof_expression;
3072 static expression_t *parse_select_expression(unsigned precedence,
3073 expression_t *compound)
3076 assert(token.type == '.' || token.type == T_MINUSGREATER);
3078 bool is_pointer = (token.type == T_MINUSGREATER);
3081 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
3083 select->expression.type = EXPR_SELECT;
3084 select->compound = compound;
3086 if(token.type != T_IDENTIFIER) {
3087 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3088 return (expression_t*) select;
3090 symbol_t *symbol = token.v.symbol;
3091 select->symbol = symbol;
3094 type_t *orig_type = compound->base.datatype;
3095 if(orig_type == NULL)
3096 return make_invalid_expression();
3098 type_t *type = skip_typeref(orig_type);
3100 type_t *type_left = type;
3102 if(type->type != TYPE_POINTER) {
3103 parser_print_error_prefix();
3104 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3105 print_type_quoted(orig_type);
3106 fputc('\n', stderr);
3107 return make_invalid_expression();
3109 pointer_type_t *pointer_type = &type->pointer;
3110 type_left = pointer_type->points_to;
3112 type_left = skip_typeref(type_left);
3114 if(type_left->type != TYPE_COMPOUND_STRUCT
3115 && type_left->type != TYPE_COMPOUND_UNION) {
3116 parser_print_error_prefix();
3117 fprintf(stderr, "request for member '%s' in something not a struct or "
3118 "union, but ", symbol->string);
3119 print_type_quoted(type_left);
3120 fputc('\n', stderr);
3121 return make_invalid_expression();
3124 compound_type_t *compound_type = &type_left->compound;
3125 declaration_t *declaration = compound_type->declaration;
3127 if(!declaration->init.is_defined) {
3128 parser_print_error_prefix();
3129 fprintf(stderr, "request for member '%s' of incomplete type ",
3131 print_type_quoted(type_left);
3132 fputc('\n', stderr);
3133 return make_invalid_expression();
3136 declaration_t *iter = declaration->context.declarations;
3137 for( ; iter != NULL; iter = iter->next) {
3138 if(iter->symbol == symbol) {
3143 parser_print_error_prefix();
3144 print_type_quoted(type_left);
3145 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3146 return make_invalid_expression();
3149 /* we always do the auto-type conversions; the & and sizeof parser contains
3150 * code to revert this! */
3151 type_t *expression_type = automatic_type_conversion(iter->type);
3153 select->compound_entry = iter;
3154 select->expression.datatype = expression_type;
3155 return (expression_t*) select;
3158 static expression_t *parse_call_expression(unsigned precedence,
3159 expression_t *expression)
3162 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3163 call->expression.type = EXPR_CALL;
3164 call->function = expression;
3166 function_type_t *function_type = NULL;
3167 type_t *orig_type = expression->base.datatype;
3168 if(orig_type != NULL) {
3169 type_t *type = skip_typeref(orig_type);
3171 if(is_type_pointer(type)) {
3172 pointer_type_t *pointer_type = &type->pointer;
3174 type = skip_typeref(pointer_type->points_to);
3176 if (type->type == TYPE_FUNCTION) {
3177 function_type = &type->function;
3178 call->expression.datatype = function_type->result_type;
3181 if(function_type == NULL) {
3182 parser_print_error_prefix();
3183 fputs("called object '", stderr);
3184 print_expression(expression);
3185 fputs("' (type ", stderr);
3186 print_type_quoted(orig_type);
3187 fputs(") is not a pointer to a function\n", stderr);
3189 function_type = NULL;
3190 call->expression.datatype = NULL;
3194 /* parse arguments */
3197 if(token.type != ')') {
3198 call_argument_t *last_argument = NULL;
3201 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3203 argument->expression = parse_assignment_expression();
3204 if(last_argument == NULL) {
3205 call->arguments = argument;
3207 last_argument->next = argument;
3209 last_argument = argument;
3211 if(token.type != ',')
3218 if(function_type != NULL) {
3219 function_parameter_t *parameter = function_type->parameters;
3220 call_argument_t *argument = call->arguments;
3221 for( ; parameter != NULL && argument != NULL;
3222 parameter = parameter->next, argument = argument->next) {
3223 type_t *expected_type = parameter->type;
3224 /* TODO report context in error messages */
3225 argument->expression = create_implicit_cast(argument->expression,
3228 /* too few parameters */
3229 if(parameter != NULL) {
3230 parser_print_error_prefix();
3231 fprintf(stderr, "too few arguments to function '");
3232 print_expression(expression);
3233 fprintf(stderr, "'\n");
3234 } else if(argument != NULL) {
3235 /* too many parameters */
3236 if(!function_type->variadic
3237 && !function_type->unspecified_parameters) {
3238 parser_print_error_prefix();
3239 fprintf(stderr, "too many arguments to function '");
3240 print_expression(expression);
3241 fprintf(stderr, "'\n");
3243 /* do default promotion */
3244 for( ; argument != NULL; argument = argument->next) {
3245 type_t *type = argument->expression->base.datatype;
3246 type = skip_typeref(type);
3251 if(is_type_integer(type)) {
3252 type = promote_integer(type);
3253 } else if(type == type_float) {
3257 argument->expression
3258 = create_implicit_cast(argument->expression, type);
3264 return (expression_t*) call;
3267 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3269 static expression_t *parse_conditional_expression(unsigned precedence,
3270 expression_t *expression)
3274 conditional_expression_t *conditional
3275 = allocate_ast_zero(sizeof(conditional[0]));
3276 conditional->expression.type = EXPR_CONDITIONAL;
3277 conditional->condition = expression;
3280 type_t *condition_type_orig = conditional->condition->base.datatype;
3281 if(condition_type_orig != NULL) {
3282 type_t *condition_type = skip_typeref(condition_type_orig);
3283 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3284 type_error("expected a scalar type",
3285 expression->base.source_position, condition_type_orig);
3289 expression_t *const t_expr = parse_expression();
3290 conditional->true_expression = t_expr;
3292 expression_t *const f_expr = parse_sub_expression(precedence);
3293 conditional->false_expression = f_expr;
3295 type_t *const true_type = t_expr->base.datatype;
3296 if(true_type == NULL)
3297 return (expression_t*) conditional;
3298 type_t *const false_type = f_expr->base.datatype;
3299 if(false_type == NULL)
3300 return (expression_t*) conditional;
3302 type_t *const skipped_true_type = skip_typeref(true_type);
3303 type_t *const skipped_false_type = skip_typeref(false_type);
3306 if (skipped_true_type == skipped_false_type) {
3307 conditional->expression.datatype = skipped_true_type;
3308 } else if (is_type_arithmetic(skipped_true_type) &&
3309 is_type_arithmetic(skipped_false_type)) {
3310 type_t *const result = semantic_arithmetic(skipped_true_type,
3311 skipped_false_type);
3312 conditional->true_expression = create_implicit_cast(t_expr, result);
3313 conditional->false_expression = create_implicit_cast(f_expr, result);
3314 conditional->expression.datatype = result;
3315 } else if (skipped_true_type->type == TYPE_POINTER &&
3316 skipped_false_type->type == TYPE_POINTER &&
3317 true /* TODO compatible points_to types */) {
3319 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3320 skipped_false_type->type == TYPE_POINTER)
3321 || (is_null_ptr_const(skipped_false_type) &&
3322 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3324 } else if(/* 1 is pointer to object type, other is void* */ false) {
3327 type_error_incompatible("while parsing conditional",
3328 expression->base.source_position, true_type,
3329 skipped_false_type);
3332 return (expression_t*) conditional;
3335 static expression_t *parse_extension(unsigned precedence)
3337 eat(T___extension__);
3339 /* TODO enable extensions */
3341 return parse_sub_expression(precedence);
3344 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3346 eat(T___builtin_classify_type);
3348 classify_type_expression_t *const classify_type_expr =
3349 allocate_ast_zero(sizeof(classify_type_expr[0]));
3350 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3351 classify_type_expr->expression.datatype = type_int;
3354 expression_t *const expression = parse_sub_expression(precedence);
3356 classify_type_expr->type_expression = expression;
3358 return (expression_t*)classify_type_expr;
3361 static void semantic_incdec(unary_expression_t *expression)
3363 type_t *orig_type = expression->value->base.datatype;
3364 if(orig_type == NULL)
3367 type_t *type = skip_typeref(orig_type);
3368 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3369 /* TODO: improve error message */
3370 parser_print_error_prefix();
3371 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3375 expression->expression.datatype = orig_type;
3378 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3380 type_t *orig_type = expression->value->base.datatype;
3381 if(orig_type == NULL)
3384 type_t *type = skip_typeref(orig_type);
3385 if(!is_type_arithmetic(type)) {
3386 /* TODO: improve error message */
3387 parser_print_error_prefix();
3388 fprintf(stderr, "operation needs an arithmetic type\n");
3392 expression->expression.datatype = orig_type;
3395 static void semantic_unexpr_scalar(unary_expression_t *expression)
3397 type_t *orig_type = expression->value->base.datatype;
3398 if(orig_type == NULL)
3401 type_t *type = skip_typeref(orig_type);
3402 if (!is_type_scalar(type)) {
3403 parse_error("operand of ! must be of scalar type\n");
3407 expression->expression.datatype = orig_type;
3410 static void semantic_unexpr_integer(unary_expression_t *expression)
3412 type_t *orig_type = expression->value->base.datatype;
3413 if(orig_type == NULL)
3416 type_t *type = skip_typeref(orig_type);
3417 if (!is_type_integer(type)) {
3418 parse_error("operand of ~ must be of integer type\n");
3422 expression->expression.datatype = orig_type;
3425 static void semantic_dereference(unary_expression_t *expression)
3427 type_t *orig_type = expression->value->base.datatype;
3428 if(orig_type == NULL)
3431 type_t *type = skip_typeref(orig_type);
3432 if(!is_type_pointer(type)) {
3433 parser_print_error_prefix();
3434 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3435 print_type_quoted(orig_type);
3436 fputs(" given.\n", stderr);
3440 pointer_type_t *pointer_type = &type->pointer;
3441 type_t *result_type = pointer_type->points_to;
3443 result_type = automatic_type_conversion(result_type);
3444 expression->expression.datatype = result_type;
3447 static void semantic_take_addr(unary_expression_t *expression)
3449 expression_t *value = expression->value;
3450 value->base.datatype = revert_automatic_type_conversion(value);
3452 type_t *orig_type = value->base.datatype;
3453 if(orig_type == NULL)
3456 if(value->type == EXPR_REFERENCE) {
3457 reference_expression_t *reference = (reference_expression_t*) value;
3458 declaration_t *declaration = reference->declaration;
3459 if(declaration != NULL) {
3460 declaration->address_taken = 1;
3464 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3467 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3468 static expression_t *parse_##unexpression_type(unsigned precedence) \
3472 unary_expression_t *unary_expression \
3473 = allocate_ast_zero(sizeof(unary_expression[0])); \
3474 unary_expression->expression.type = EXPR_UNARY; \
3475 unary_expression->type = unexpression_type; \
3476 unary_expression->value = parse_sub_expression(precedence); \
3478 sfunc(unary_expression); \
3480 return (expression_t*) unary_expression; \
3483 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3484 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3485 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3486 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3487 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3488 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3489 semantic_unexpr_integer)
3490 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3492 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3495 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3497 static expression_t *parse_##unexpression_type(unsigned precedence, \
3498 expression_t *left) \
3500 (void) precedence; \
3503 unary_expression_t *unary_expression \
3504 = allocate_ast_zero(sizeof(unary_expression[0])); \
3505 unary_expression->expression.type = EXPR_UNARY; \
3506 unary_expression->type = unexpression_type; \
3507 unary_expression->value = left; \
3509 sfunc(unary_expression); \
3511 return (expression_t*) unary_expression; \
3514 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3516 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3519 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3521 /* TODO: handle complex + imaginary types */
3523 /* § 6.3.1.8 Usual arithmetic conversions */
3524 if(type_left == type_long_double || type_right == type_long_double) {
3525 return type_long_double;
3526 } else if(type_left == type_double || type_right == type_double) {
3528 } else if(type_left == type_float || type_right == type_float) {
3532 type_right = promote_integer(type_right);
3533 type_left = promote_integer(type_left);
3535 if(type_left == type_right)
3538 bool signed_left = is_type_signed(type_left);
3539 bool signed_right = is_type_signed(type_right);
3540 int rank_left = get_rank(type_left);
3541 int rank_right = get_rank(type_right);
3542 if(rank_left < rank_right) {
3543 if(signed_left == signed_right || !signed_right) {
3549 if(signed_left == signed_right || !signed_left) {
3557 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3559 expression_t *left = expression->left;
3560 expression_t *right = expression->right;
3561 type_t *orig_type_left = left->base.datatype;
3562 type_t *orig_type_right = right->base.datatype;
3564 if(orig_type_left == NULL || orig_type_right == NULL)
3567 type_t *type_left = skip_typeref(orig_type_left);
3568 type_t *type_right = skip_typeref(orig_type_right);
3570 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3571 /* TODO: improve error message */
3572 parser_print_error_prefix();
3573 fprintf(stderr, "operation needs arithmetic types\n");
3577 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3578 expression->left = create_implicit_cast(left, arithmetic_type);
3579 expression->right = create_implicit_cast(right, arithmetic_type);
3580 expression->expression.datatype = arithmetic_type;
3583 static void semantic_shift_op(binary_expression_t *expression)
3585 expression_t *left = expression->left;
3586 expression_t *right = expression->right;
3587 type_t *orig_type_left = left->base.datatype;
3588 type_t *orig_type_right = right->base.datatype;
3590 if(orig_type_left == NULL || orig_type_right == NULL)
3593 type_t *type_left = skip_typeref(orig_type_left);
3594 type_t *type_right = skip_typeref(orig_type_right);
3596 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3597 /* TODO: improve error message */
3598 parser_print_error_prefix();
3599 fprintf(stderr, "operation needs integer types\n");
3603 type_left = promote_integer(type_left);
3604 type_right = promote_integer(type_right);
3606 expression->left = create_implicit_cast(left, type_left);
3607 expression->right = create_implicit_cast(right, type_right);
3608 expression->expression.datatype = type_left;
3611 static void semantic_add(binary_expression_t *expression)
3613 expression_t *left = expression->left;
3614 expression_t *right = expression->right;
3615 type_t *orig_type_left = left->base.datatype;
3616 type_t *orig_type_right = right->base.datatype;
3618 if(orig_type_left == NULL || orig_type_right == NULL)
3621 type_t *type_left = skip_typeref(orig_type_left);
3622 type_t *type_right = skip_typeref(orig_type_right);
3625 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3626 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3627 expression->left = create_implicit_cast(left, arithmetic_type);
3628 expression->right = create_implicit_cast(right, arithmetic_type);
3629 expression->expression.datatype = arithmetic_type;
3631 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3632 expression->expression.datatype = type_left;
3633 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3634 expression->expression.datatype = type_right;
3636 parser_print_error_prefix();
3637 fprintf(stderr, "invalid operands to binary + (");
3638 print_type_quoted(orig_type_left);
3639 fprintf(stderr, ", ");
3640 print_type_quoted(orig_type_right);
3641 fprintf(stderr, ")\n");
3645 static void semantic_sub(binary_expression_t *expression)
3647 expression_t *left = expression->left;
3648 expression_t *right = expression->right;
3649 type_t *orig_type_left = left->base.datatype;
3650 type_t *orig_type_right = right->base.datatype;
3652 if(orig_type_left == NULL || orig_type_right == NULL)
3655 type_t *type_left = skip_typeref(orig_type_left);
3656 type_t *type_right = skip_typeref(orig_type_right);
3659 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3660 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3661 expression->left = create_implicit_cast(left, arithmetic_type);
3662 expression->right = create_implicit_cast(right, arithmetic_type);
3663 expression->expression.datatype = arithmetic_type;
3665 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3666 expression->expression.datatype = type_left;
3667 } else if(type_left->type == TYPE_POINTER &&
3668 type_right->type == TYPE_POINTER) {
3669 if(!pointers_compatible(type_left, type_right)) {
3670 parser_print_error_prefix();
3671 fprintf(stderr, "pointers to incompatible objects to binary - (");
3672 print_type_quoted(orig_type_left);
3673 fprintf(stderr, ", ");
3674 print_type_quoted(orig_type_right);
3675 fprintf(stderr, ")\n");
3677 expression->expression.datatype = type_ptrdiff_t;
3680 parser_print_error_prefix();
3681 fprintf(stderr, "invalid operands to binary - (");
3682 print_type_quoted(orig_type_left);
3683 fprintf(stderr, ", ");
3684 print_type_quoted(orig_type_right);
3685 fprintf(stderr, ")\n");
3689 static void semantic_comparison(binary_expression_t *expression)
3691 expression_t *left = expression->left;
3692 expression_t *right = expression->right;
3693 type_t *orig_type_left = left->base.datatype;
3694 type_t *orig_type_right = right->base.datatype;
3696 if(orig_type_left == NULL || orig_type_right == NULL)
3699 type_t *type_left = skip_typeref(orig_type_left);
3700 type_t *type_right = skip_typeref(orig_type_right);
3702 /* TODO non-arithmetic types */
3703 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3704 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3705 expression->left = create_implicit_cast(left, arithmetic_type);
3706 expression->right = create_implicit_cast(right, arithmetic_type);
3707 expression->expression.datatype = arithmetic_type;
3708 } else if (type_left->type == TYPE_POINTER &&
3709 type_right->type == TYPE_POINTER) {
3710 /* TODO check compatibility */
3711 } else if (type_left->type == TYPE_POINTER) {
3712 expression->right = create_implicit_cast(right, type_left);
3713 } else if (type_right->type == TYPE_POINTER) {
3714 expression->left = create_implicit_cast(left, type_right);
3716 type_error_incompatible("invalid operands in comparison",
3717 token.source_position, type_left, type_right);
3719 expression->expression.datatype = type_int;
3722 static void semantic_arithmetic_assign(binary_expression_t *expression)
3724 expression_t *left = expression->left;
3725 expression_t *right = expression->right;
3726 type_t *orig_type_left = left->base.datatype;
3727 type_t *orig_type_right = right->base.datatype;
3729 if(orig_type_left == NULL || orig_type_right == NULL)
3732 type_t *type_left = skip_typeref(orig_type_left);
3733 type_t *type_right = skip_typeref(orig_type_right);
3735 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3736 /* TODO: improve error message */
3737 parser_print_error_prefix();
3738 fprintf(stderr, "operation needs arithmetic types\n");
3742 /* combined instructions are tricky. We can't create an implicit cast on
3743 * the left side, because we need the uncasted form for the store.
3744 * The ast2firm pass has to know that left_type must be right_type
3745 * for the arithmeitc operation and create a cast by itself */
3746 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3747 expression->right = create_implicit_cast(right, arithmetic_type);
3748 expression->expression.datatype = type_left;
3751 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3753 expression_t *left = expression->left;
3754 expression_t *right = expression->right;
3755 type_t *orig_type_left = left->base.datatype;
3756 type_t *orig_type_right = right->base.datatype;
3758 if(orig_type_left == NULL || orig_type_right == NULL)
3761 type_t *type_left = skip_typeref(orig_type_left);
3762 type_t *type_right = skip_typeref(orig_type_right);
3764 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3765 /* combined instructions are tricky. We can't create an implicit cast on
3766 * the left side, because we need the uncasted form for the store.
3767 * The ast2firm pass has to know that left_type must be right_type
3768 * for the arithmeitc operation and create a cast by itself */
3769 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3770 expression->right = create_implicit_cast(right, arithmetic_type);
3771 expression->expression.datatype = type_left;
3772 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3773 expression->expression.datatype = type_left;
3775 parser_print_error_prefix();
3776 fputs("Incompatible types ", stderr);
3777 print_type_quoted(orig_type_left);
3778 fputs(" and ", stderr);
3779 print_type_quoted(orig_type_right);
3780 fputs(" in assignment\n", stderr);
3785 static void semantic_logical_op(binary_expression_t *expression)
3787 expression_t *left = expression->left;
3788 expression_t *right = expression->right;
3789 type_t *orig_type_left = left->base.datatype;
3790 type_t *orig_type_right = right->base.datatype;
3792 if(orig_type_left == NULL || orig_type_right == NULL)
3795 type_t *type_left = skip_typeref(orig_type_left);
3796 type_t *type_right = skip_typeref(orig_type_right);
3798 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3799 /* TODO: improve error message */
3800 parser_print_error_prefix();
3801 fprintf(stderr, "operation needs scalar types\n");
3805 expression->expression.datatype = type_int;
3808 static bool has_const_fields(type_t *type)
3815 static void semantic_binexpr_assign(binary_expression_t *expression)
3817 expression_t *left = expression->left;
3818 type_t *orig_type_left = left->base.datatype;
3820 if(orig_type_left == NULL)
3823 type_t *type_left = revert_automatic_type_conversion(left);
3824 type_left = skip_typeref(orig_type_left);
3826 /* must be a modifiable lvalue */
3827 if (type_left->type == TYPE_ARRAY) {
3828 parser_print_error_prefix();
3829 fprintf(stderr, "Cannot assign to arrays ('");
3830 print_expression(left);
3831 fprintf(stderr, "')\n");
3834 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3835 parser_print_error_prefix();
3836 fprintf(stderr, "assignment to readonly location '");
3837 print_expression(left);
3838 fprintf(stderr, "' (type ");
3839 print_type_quoted(orig_type_left);
3840 fprintf(stderr, ")\n");
3843 if(is_type_incomplete(type_left)) {
3844 parser_print_error_prefix();
3845 fprintf(stderr, "left-hand side of assignment '");
3846 print_expression(left);
3847 fprintf(stderr, "' has incomplete type ");
3848 print_type_quoted(orig_type_left);
3849 fprintf(stderr, "\n");
3852 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3853 parser_print_error_prefix();
3854 fprintf(stderr, "can't assign to '");
3855 print_expression(left);
3856 fprintf(stderr, "' because compound type ");
3857 print_type_quoted(orig_type_left);
3858 fprintf(stderr, " has readonly fields\n");
3862 semantic_assign(orig_type_left, &expression->right, "assignment");
3864 expression->expression.datatype = orig_type_left;
3867 static void semantic_comma(binary_expression_t *expression)
3869 expression->expression.datatype = expression->right->base.datatype;
3872 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3873 static expression_t *parse_##binexpression_type(unsigned precedence, \
3874 expression_t *left) \
3878 expression_t *right = parse_sub_expression(precedence + lr); \
3880 binary_expression_t *binexpr \
3881 = allocate_ast_zero(sizeof(binexpr[0])); \
3882 binexpr->expression.type = EXPR_BINARY; \
3883 binexpr->type = binexpression_type; \
3884 binexpr->left = left; \
3885 binexpr->right = right; \
3888 return (expression_t*) binexpr; \
3891 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3892 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3893 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3894 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3895 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3896 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3897 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3898 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3899 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3900 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3901 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3902 semantic_comparison, 1)
3903 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3904 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3905 semantic_comparison, 1)
3906 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3907 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3908 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3909 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3910 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3911 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3912 semantic_shift_op, 1)
3913 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3914 semantic_shift_op, 1)
3915 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3916 semantic_arithmetic_addsubb_assign, 0)
3917 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3918 semantic_arithmetic_addsubb_assign, 0)
3919 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3920 semantic_arithmetic_assign, 0)
3921 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3922 semantic_arithmetic_assign, 0)
3923 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3924 semantic_arithmetic_assign, 0)
3925 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3926 semantic_arithmetic_assign, 0)
3927 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3928 semantic_arithmetic_assign, 0)
3929 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3930 semantic_arithmetic_assign, 0)
3931 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3932 semantic_arithmetic_assign, 0)
3933 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3934 semantic_arithmetic_assign, 0)
3936 static expression_t *parse_sub_expression(unsigned precedence)
3938 if(token.type < 0) {
3939 return expected_expression_error();
3942 expression_parser_function_t *parser
3943 = &expression_parsers[token.type];
3944 source_position_t source_position = token.source_position;
3947 if(parser->parser != NULL) {
3948 left = parser->parser(parser->precedence);
3950 left = parse_primary_expression();
3952 assert(left != NULL);
3953 left->base.source_position = source_position;
3956 if(token.type < 0) {
3957 return expected_expression_error();
3960 parser = &expression_parsers[token.type];
3961 if(parser->infix_parser == NULL)
3963 if(parser->infix_precedence < precedence)
3966 left = parser->infix_parser(parser->infix_precedence, left);
3968 assert(left != NULL);
3969 assert(left->type != EXPR_UNKNOWN);
3970 left->base.source_position = source_position;
3976 static expression_t *parse_expression(void)
3978 return parse_sub_expression(1);
3983 static void register_expression_parser(parse_expression_function parser,
3984 int token_type, unsigned precedence)
3986 expression_parser_function_t *entry = &expression_parsers[token_type];
3988 if(entry->parser != NULL) {
3989 fprintf(stderr, "for token ");
3990 print_token_type(stderr, (token_type_t) token_type);
3991 fprintf(stderr, "\n");
3992 panic("trying to register multiple expression parsers for a token");
3994 entry->parser = parser;
3995 entry->precedence = precedence;
3998 static void register_expression_infix_parser(
3999 parse_expression_infix_function parser, int token_type,
4000 unsigned precedence)
4002 expression_parser_function_t *entry = &expression_parsers[token_type];
4004 if(entry->infix_parser != NULL) {
4005 fprintf(stderr, "for token ");
4006 print_token_type(stderr, (token_type_t) token_type);
4007 fprintf(stderr, "\n");
4008 panic("trying to register multiple infix expression parsers for a "
4011 entry->infix_parser = parser;
4012 entry->infix_precedence = precedence;
4015 static void init_expression_parsers(void)
4017 memset(&expression_parsers, 0, sizeof(expression_parsers));
4019 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4020 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4021 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4022 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4023 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4024 T_GREATERGREATER, 16);
4025 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4026 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4027 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4028 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4029 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4030 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4031 T_GREATEREQUAL, 14);
4032 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4033 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4034 T_EXCLAMATIONMARKEQUAL, 13);
4035 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4036 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4037 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4038 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4039 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4040 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4041 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4042 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4043 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4044 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4045 T_ASTERISKEQUAL, 2);
4046 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4047 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4049 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4050 T_LESSLESSEQUAL, 2);
4051 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4052 T_GREATERGREATEREQUAL, 2);
4053 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4055 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4057 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4060 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4062 register_expression_infix_parser(parse_array_expression, '[', 30);
4063 register_expression_infix_parser(parse_call_expression, '(', 30);
4064 register_expression_infix_parser(parse_select_expression, '.', 30);
4065 register_expression_infix_parser(parse_select_expression,
4066 T_MINUSGREATER, 30);
4067 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4069 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4072 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4073 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4074 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4075 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4076 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4077 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4078 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4079 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4080 register_expression_parser(parse_sizeof, T_sizeof, 25);
4081 register_expression_parser(parse_extension, T___extension__, 25);
4082 register_expression_parser(parse_builtin_classify_type,
4083 T___builtin_classify_type, 25);
4087 static statement_t *parse_case_statement(void)
4090 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4091 label->statement.type = STATEMENT_CASE_LABEL;
4092 label->statement.source_position = token.source_position;
4094 label->expression = parse_expression();
4097 label->label_statement = parse_statement();
4099 return (statement_t*) label;
4102 static statement_t *parse_default_statement(void)
4106 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4107 label->statement.type = STATEMENT_CASE_LABEL;
4108 label->statement.source_position = token.source_position;
4111 label->label_statement = parse_statement();
4113 return (statement_t*) label;
4116 static declaration_t *get_label(symbol_t *symbol)
4118 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4119 assert(current_function != NULL);
4120 /* if we found a label in the same function, then we already created the
4122 if(candidate != NULL
4123 && candidate->parent_context == ¤t_function->context) {
4127 /* otherwise we need to create a new one */
4128 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4129 declaration->namespc = NAMESPACE_LABEL;
4130 declaration->symbol = symbol;
4132 label_push(declaration);
4137 static statement_t *parse_label_statement(void)
4139 assert(token.type == T_IDENTIFIER);
4140 symbol_t *symbol = token.v.symbol;
4143 declaration_t *label = get_label(symbol);
4145 /* if source position is already set then the label is defined twice,
4146 * otherwise it was just mentioned in a goto so far */
4147 if(label->source_position.input_name != NULL) {
4148 parser_print_error_prefix();
4149 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4150 parser_print_error_prefix_pos(label->source_position);
4151 fprintf(stderr, "previous definition of '%s' was here\n",
4154 label->source_position = token.source_position;
4157 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4159 label_statement->statement.type = STATEMENT_LABEL;
4160 label_statement->statement.source_position = token.source_position;
4161 label_statement->label = label;
4165 if(token.type == '}') {
4166 parse_error("label at end of compound statement");
4167 return (statement_t*) label_statement;
4169 label_statement->label_statement = parse_statement();
4172 return (statement_t*) label_statement;
4175 static statement_t *parse_if(void)
4179 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4180 statement->statement.type = STATEMENT_IF;
4181 statement->statement.source_position = token.source_position;
4184 statement->condition = parse_expression();
4187 statement->true_statement = parse_statement();
4188 if(token.type == T_else) {
4190 statement->false_statement = parse_statement();
4193 return (statement_t*) statement;
4196 static statement_t *parse_switch(void)
4200 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4201 statement->statement.type = STATEMENT_SWITCH;
4202 statement->statement.source_position = token.source_position;
4205 statement->expression = parse_expression();
4207 statement->body = parse_statement();
4209 return (statement_t*) statement;
4212 static statement_t *parse_while(void)
4216 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4217 statement->statement.type = STATEMENT_WHILE;
4218 statement->statement.source_position = token.source_position;
4221 statement->condition = parse_expression();
4223 statement->body = parse_statement();
4225 return (statement_t*) statement;
4228 static statement_t *parse_do(void)
4232 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4233 statement->statement.type = STATEMENT_DO_WHILE;
4234 statement->statement.source_position = token.source_position;
4236 statement->body = parse_statement();
4239 statement->condition = parse_expression();
4243 return (statement_t*) statement;
4246 static statement_t *parse_for(void)
4250 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4251 statement->statement.type = STATEMENT_FOR;
4252 statement->statement.source_position = token.source_position;
4256 int top = environment_top();
4257 context_t *last_context = context;
4258 set_context(&statement->context);
4260 if(token.type != ';') {
4261 if(is_declaration_specifier(&token, false)) {
4262 parse_declaration();
4264 statement->initialisation = parse_expression();
4271 if(token.type != ';') {
4272 statement->condition = parse_expression();
4275 if(token.type != ')') {
4276 statement->step = parse_expression();
4279 statement->body = parse_statement();
4281 assert(context == &statement->context);
4282 set_context(last_context);
4283 environment_pop_to(top);
4285 return (statement_t*) statement;
4288 static statement_t *parse_goto(void)
4292 if(token.type != T_IDENTIFIER) {
4293 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4297 symbol_t *symbol = token.v.symbol;
4300 declaration_t *label = get_label(symbol);
4302 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4304 statement->statement.type = STATEMENT_GOTO;
4305 statement->statement.source_position = token.source_position;
4307 statement->label = label;
4311 return (statement_t*) statement;
4314 static statement_t *parse_continue(void)
4319 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4320 statement->type = STATEMENT_CONTINUE;
4321 statement->base.source_position = token.source_position;
4326 static statement_t *parse_break(void)
4331 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4332 statement->type = STATEMENT_BREAK;
4333 statement->base.source_position = token.source_position;
4338 static statement_t *parse_return(void)
4342 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4344 statement->statement.type = STATEMENT_RETURN;
4345 statement->statement.source_position = token.source_position;
4347 assert(current_function->type->type == TYPE_FUNCTION);
4348 function_type_t *function_type = ¤t_function->type->function;
4349 type_t *return_type = function_type->result_type;
4351 expression_t *return_value = NULL;
4352 if(token.type != ';') {
4353 return_value = parse_expression();
4357 if(return_type == NULL)
4358 return (statement_t*) statement;
4360 return_type = skip_typeref(return_type);
4362 if(return_value != NULL) {
4363 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4365 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4366 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4367 parse_warning("'return' with a value, in function returning void");
4368 return_value = NULL;
4370 if(return_type != NULL) {
4371 semantic_assign(return_type, &return_value, "'return'");
4375 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4376 parse_warning("'return' without value, in function returning "
4380 statement->return_value = return_value;
4382 return (statement_t*) statement;
4385 static statement_t *parse_declaration_statement(void)
4387 declaration_t *before = last_declaration;
4389 declaration_statement_t *statement
4390 = allocate_ast_zero(sizeof(statement[0]));
4391 statement->statement.type = STATEMENT_DECLARATION;
4392 statement->statement.source_position = token.source_position;
4394 declaration_specifiers_t specifiers;
4395 memset(&specifiers, 0, sizeof(specifiers));
4396 parse_declaration_specifiers(&specifiers);
4398 if(token.type == ';') {
4401 parse_init_declarators(&specifiers);
4404 if(before == NULL) {
4405 statement->declarations_begin = context->declarations;
4407 statement->declarations_begin = before->next;
4409 statement->declarations_end = last_declaration;
4411 return (statement_t*) statement;
4414 static statement_t *parse_expression_statement(void)
4416 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4417 statement->statement.type = STATEMENT_EXPRESSION;
4418 statement->statement.source_position = token.source_position;
4420 statement->expression = parse_expression();
4424 return (statement_t*) statement;
4427 static statement_t *parse_statement(void)
4429 statement_t *statement = NULL;
4431 /* declaration or statement */
4432 switch(token.type) {
4434 statement = parse_case_statement();
4438 statement = parse_default_statement();
4442 statement = parse_compound_statement();
4446 statement = parse_if();
4450 statement = parse_switch();
4454 statement = parse_while();
4458 statement = parse_do();
4462 statement = parse_for();
4466 statement = parse_goto();
4470 statement = parse_continue();
4474 statement = parse_break();
4478 statement = parse_return();
4487 if(look_ahead(1)->type == ':') {
4488 statement = parse_label_statement();
4492 if(is_typedef_symbol(token.v.symbol)) {
4493 statement = parse_declaration_statement();
4497 statement = parse_expression_statement();
4500 case T___extension__:
4501 /* this can be a prefix to a declaration or an expression statement */
4502 /* we simply eat it now and parse the rest with tail recursion */
4505 } while(token.type == T___extension__);
4506 statement = parse_statement();
4510 statement = parse_declaration_statement();
4514 statement = parse_expression_statement();
4518 assert(statement == NULL
4519 || statement->base.source_position.input_name != NULL);
4524 static statement_t *parse_compound_statement(void)
4526 compound_statement_t *compound_statement
4527 = allocate_ast_zero(sizeof(compound_statement[0]));
4528 compound_statement->statement.type = STATEMENT_COMPOUND;
4529 compound_statement->statement.source_position = token.source_position;
4533 int top = environment_top();
4534 context_t *last_context = context;
4535 set_context(&compound_statement->context);
4537 statement_t *last_statement = NULL;
4539 while(token.type != '}' && token.type != T_EOF) {
4540 statement_t *statement = parse_statement();
4541 if(statement == NULL)
4544 if(last_statement != NULL) {
4545 last_statement->base.next = statement;
4547 compound_statement->statements = statement;
4550 while(statement->base.next != NULL)
4551 statement = statement->base.next;
4553 last_statement = statement;
4556 if(token.type != '}') {
4557 parser_print_error_prefix_pos(
4558 compound_statement->statement.source_position);
4559 fprintf(stderr, "end of file while looking for closing '}'\n");
4563 assert(context == &compound_statement->context);
4564 set_context(last_context);
4565 environment_pop_to(top);
4567 return (statement_t*) compound_statement;
4570 static translation_unit_t *parse_translation_unit(void)
4572 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4574 assert(global_context == NULL);
4575 global_context = &unit->context;
4577 assert(context == NULL);
4578 set_context(&unit->context);
4580 while(token.type != T_EOF) {
4581 parse_declaration();
4584 assert(context == &unit->context);
4586 last_declaration = NULL;
4588 assert(global_context == &unit->context);
4589 global_context = NULL;
4594 translation_unit_t *parse(void)
4596 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4597 label_stack = NEW_ARR_F(stack_entry_t, 0);
4598 found_error = false;
4600 type_set_output(stderr);
4601 ast_set_output(stderr);
4603 lookahead_bufpos = 0;
4604 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4607 translation_unit_t *unit = parse_translation_unit();
4609 DEL_ARR_F(environment_stack);
4610 DEL_ARR_F(label_stack);
4618 void init_parser(void)
4620 init_expression_parsers();
4621 obstack_init(&temp_obst);
4623 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4624 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4625 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4626 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4627 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4628 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4629 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4630 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4631 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4632 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4635 void exit_parser(void)
4637 obstack_free(&temp_obst, NULL);