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_statement_struct_size(statement_type_t type)
117 static const size_t sizes[] = {
118 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
119 [STATEMENT_RETURN] = sizeof(return_statement_t),
120 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
121 [STATEMENT_IF] = sizeof(if_statement_t),
122 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
123 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
124 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
125 [STATEMENT_BREAK] = sizeof(statement_base_t),
126 [STATEMENT_GOTO] = sizeof(goto_statement_t),
127 [STATEMENT_LABEL] = sizeof(label_statement_t),
128 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
129 [STATEMENT_WHILE] = sizeof(while_statement_t),
130 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
131 [STATEMENT_FOR] = sizeof(for_statement_t),
132 [STATEMENT_ASM] = sizeof(asm_statement_t)
134 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
135 assert(type <= STATEMENT_ASM);
136 assert(sizes[type] != 0);
140 static statement_t *allocate_statement_zero(statement_type_t type)
142 size_t size = get_statement_struct_size(type);
143 statement_t *res = allocate_ast_zero(size);
145 res->base.type = type;
150 static size_t get_expression_struct_size(expression_type_t type)
152 static const size_t sizes[] = {
153 [EXPR_INVALID] = sizeof(expression_base_t),
154 [EXPR_REFERENCE] = sizeof(reference_expression_t),
155 [EXPR_CONST] = sizeof(const_expression_t),
156 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
157 [EXPR_CALL] = sizeof(call_expression_t),
158 [EXPR_UNARY] = sizeof(unary_expression_t),
159 [EXPR_BINARY] = sizeof(binary_expression_t),
160 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
161 [EXPR_SELECT] = sizeof(select_expression_t),
162 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
163 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
164 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
165 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
166 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
167 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
168 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
169 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
170 [EXPR_STATEMENT] = sizeof(statement_expression_t)
172 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
173 assert(type <= EXPR_STATEMENT);
174 assert(sizes[type] != 0);
178 static expression_t *allocate_expression_zero(expression_type_t type)
180 size_t size = get_expression_struct_size(type);
181 expression_t *res = allocate_ast_zero(size);
183 res->base.type = type;
187 static size_t get_type_struct_size(type_type_t type)
189 static const size_t sizes[] = {
190 [TYPE_ATOMIC] = sizeof(atomic_type_t),
191 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
192 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
193 [TYPE_ENUM] = sizeof(enum_type_t),
194 [TYPE_FUNCTION] = sizeof(function_type_t),
195 [TYPE_POINTER] = sizeof(pointer_type_t),
196 [TYPE_ARRAY] = sizeof(array_type_t),
197 [TYPE_BUILTIN] = sizeof(builtin_type_t),
198 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
199 [TYPE_TYPEOF] = sizeof(typeof_type_t),
201 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
202 assert(type <= TYPE_TYPEOF);
203 assert(sizes[type] != 0);
207 static type_t *allocate_type_zero(type_type_t type)
209 size_t size = get_type_struct_size(type);
210 type_t *res = obstack_alloc(type_obst, size);
211 memset(res, 0, size);
213 res->base.type = type;
217 static size_t get_initializer_size(initializer_type_t type)
219 static const size_t sizes[] = {
220 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
221 [INITIALIZER_STRING] = sizeof(initializer_string_t),
222 [INITIALIZER_LIST] = sizeof(initializer_list_t)
224 assert(type < INITIALIZER_COUNT);
225 assert(sizes[type] != 0);
229 static initializer_t *allocate_initializer(initializer_type_t type)
231 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
237 static void free_type(void *type)
239 obstack_free(type_obst, type);
243 * returns the top element of the environment stack
245 static size_t environment_top(void)
247 return ARR_LEN(environment_stack);
250 static size_t label_top(void)
252 return ARR_LEN(label_stack);
257 static inline void next_token(void)
259 token = lookahead_buffer[lookahead_bufpos];
260 lookahead_buffer[lookahead_bufpos] = lexer_token;
263 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
266 print_token(stderr, &token);
267 fprintf(stderr, "\n");
271 static inline const token_t *look_ahead(int num)
273 assert(num > 0 && num <= MAX_LOOKAHEAD);
274 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
275 return & lookahead_buffer[pos];
278 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
280 static void error(void)
283 #ifdef ABORT_ON_ERROR
288 static void parser_print_prefix_pos(const source_position_t source_position)
290 fputs(source_position.input_name, stderr);
292 fprintf(stderr, "%u", source_position.linenr);
296 static void parser_print_error_prefix_pos(
297 const source_position_t source_position)
299 parser_print_prefix_pos(source_position);
300 fputs("error: ", stderr);
304 static void parser_print_error_prefix(void)
306 parser_print_error_prefix_pos(token.source_position);
309 static void parse_error(const char *message)
311 parser_print_error_prefix();
312 fprintf(stderr, "parse error: %s\n", message);
315 static void parser_print_warning_prefix_pos(
316 const source_position_t source_position)
318 parser_print_prefix_pos(source_position);
319 fputs("warning: ", stderr);
322 static void parse_warning_pos(const source_position_t source_position,
323 const char *const message)
325 parser_print_prefix_pos(source_position);
326 fprintf(stderr, "warning: %s\n", message);
329 static void parse_warning(const char *message)
331 parse_warning_pos(token.source_position, message);
334 static void parse_error_expected(const char *message, ...)
339 if(message != NULL) {
340 parser_print_error_prefix();
341 fprintf(stderr, "%s\n", message);
343 parser_print_error_prefix();
344 fputs("Parse error: got ", stderr);
345 print_token(stderr, &token);
346 fputs(", expected ", stderr);
348 va_start(args, message);
349 token_type_t token_type = va_arg(args, token_type_t);
350 while(token_type != 0) {
354 fprintf(stderr, ", ");
356 print_token_type(stderr, token_type);
357 token_type = va_arg(args, token_type_t);
360 fprintf(stderr, "\n");
363 static void print_type_quoted(type_t *type)
370 static void type_error(const char *msg, const source_position_t source_position,
373 parser_print_error_prefix_pos(source_position);
374 fprintf(stderr, "%s, but found type ", msg);
375 print_type_quoted(type);
379 static void type_error_incompatible(const char *msg,
380 const source_position_t source_position, type_t *type1, type_t *type2)
382 parser_print_error_prefix_pos(source_position);
383 fprintf(stderr, "%s, incompatible types: ", msg);
384 print_type_quoted(type1);
385 fprintf(stderr, " - ");
386 print_type_quoted(type2);
387 fprintf(stderr, ")\n");
390 static void eat_block(void)
392 if(token.type == '{')
395 while(token.type != '}') {
396 if(token.type == T_EOF)
398 if(token.type == '{') {
407 static void eat_statement(void)
409 while(token.type != ';') {
410 if(token.type == T_EOF)
412 if(token.type == '}')
414 if(token.type == '{') {
423 static void eat_brace(void)
425 if(token.type == '(')
428 while(token.type != ')') {
429 if(token.type == T_EOF)
431 if(token.type == ')' || token.type == ';' || token.type == '}') {
434 if(token.type == '(') {
438 if(token.type == '{') {
447 #define expect(expected) \
448 if(UNLIKELY(token.type != (expected))) { \
449 parse_error_expected(NULL, (expected), 0); \
455 #define expect_block(expected) \
456 if(UNLIKELY(token.type != (expected))) { \
457 parse_error_expected(NULL, (expected), 0); \
463 #define expect_void(expected) \
464 if(UNLIKELY(token.type != (expected))) { \
465 parse_error_expected(NULL, (expected), 0); \
471 static void set_context(context_t *new_context)
473 context = new_context;
475 last_declaration = new_context->declarations;
476 if(last_declaration != NULL) {
477 while(last_declaration->next != NULL) {
478 last_declaration = last_declaration->next;
484 * called when we find a 2nd declarator for an identifier we already have a
487 static bool is_compatible_declaration (declaration_t *declaration,
488 declaration_t *previous)
490 if (declaration->type->type == TYPE_FUNCTION &&
491 previous->type->type == TYPE_FUNCTION &&
492 previous->type->function.unspecified_parameters) {
493 function_type_t* const prev_func = &previous->type->function;
494 function_type_t* const decl_func = &declaration->type->function;
495 if (prev_func->unspecified_parameters &&
496 prev_func->result_type == decl_func->result_type) {
497 declaration->type = previous->type;
501 /* TODO: not correct yet */
502 return declaration->type == previous->type;
505 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
507 declaration_t *declaration = symbol->declaration;
508 for( ; declaration != NULL; declaration = declaration->symbol_next) {
509 if(declaration->namespc == namespc)
516 static const char *get_namespace_prefix(namespace_t namespc)
519 case NAMESPACE_NORMAL:
521 case NAMESPACE_UNION:
523 case NAMESPACE_STRUCT:
527 case NAMESPACE_LABEL:
530 panic("invalid namespace found");
534 * pushs an environment_entry on the environment stack and links the
535 * corresponding symbol to the new entry
537 static declaration_t *stack_push(stack_entry_t **stack_ptr,
538 declaration_t *declaration,
539 context_t *parent_context)
541 symbol_t *symbol = declaration->symbol;
542 namespace_t namespc = (namespace_t)declaration->namespc;
544 /* a declaration should be only pushed once */
545 assert(declaration->parent_context == NULL);
546 declaration->parent_context = parent_context;
548 declaration_t *previous_declaration = get_declaration(symbol, namespc);
549 assert(declaration != previous_declaration);
550 if(previous_declaration != NULL
551 && previous_declaration->parent_context == context) {
552 if(!is_compatible_declaration(declaration, previous_declaration)) {
553 parser_print_error_prefix_pos(declaration->source_position);
554 fprintf(stderr, "definition of symbol %s%s with type ",
555 get_namespace_prefix(namespc), symbol->string);
556 print_type_quoted(declaration->type);
558 parser_print_error_prefix_pos(
559 previous_declaration->source_position);
560 fprintf(stderr, "is incompatible with previous declaration "
562 print_type_quoted(previous_declaration->type);
565 unsigned old_storage_class = previous_declaration->storage_class;
566 unsigned new_storage_class = declaration->storage_class;
567 if (current_function == NULL) {
568 if (old_storage_class != STORAGE_CLASS_STATIC &&
569 new_storage_class == STORAGE_CLASS_STATIC) {
570 parser_print_error_prefix_pos(declaration->source_position);
572 "static declaration of '%s' follows non-static declaration\n",
574 parser_print_error_prefix_pos(previous_declaration->source_position);
575 fprintf(stderr, "previous declaration of '%s' was here\n",
578 if (old_storage_class == STORAGE_CLASS_EXTERN) {
579 if (new_storage_class == STORAGE_CLASS_NONE) {
580 previous_declaration->storage_class = STORAGE_CLASS_NONE;
583 parser_print_warning_prefix_pos(declaration->source_position);
584 fprintf(stderr, "redundant declaration for '%s'\n",
586 parser_print_warning_prefix_pos(previous_declaration->source_position);
587 fprintf(stderr, "previous declaration of '%s' was here\n",
592 if (old_storage_class == STORAGE_CLASS_EXTERN &&
593 new_storage_class == STORAGE_CLASS_EXTERN) {
594 parser_print_warning_prefix_pos(declaration->source_position);
595 fprintf(stderr, "redundant extern declaration for '%s'\n",
597 parser_print_warning_prefix_pos(previous_declaration->source_position);
598 fprintf(stderr, "previous declaration of '%s' was here\n",
601 parser_print_error_prefix_pos(declaration->source_position);
602 if (old_storage_class == new_storage_class) {
603 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
605 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
607 parser_print_error_prefix_pos(previous_declaration->source_position);
608 fprintf(stderr, "previous declaration of '%s' was here\n",
613 return previous_declaration;
616 /* remember old declaration */
618 entry.symbol = symbol;
619 entry.old_declaration = symbol->declaration;
620 entry.namespc = (unsigned short) namespc;
621 ARR_APP1(stack_entry_t, *stack_ptr, entry);
623 /* replace/add declaration into declaration list of the symbol */
624 if(symbol->declaration == NULL) {
625 symbol->declaration = declaration;
627 declaration_t *iter_last = NULL;
628 declaration_t *iter = symbol->declaration;
629 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
630 /* replace an entry? */
631 if(iter->namespc == namespc) {
632 if(iter_last == NULL) {
633 symbol->declaration = declaration;
635 iter_last->symbol_next = declaration;
637 declaration->symbol_next = iter->symbol_next;
642 assert(iter_last->symbol_next == NULL);
643 iter_last->symbol_next = declaration;
650 static declaration_t *environment_push(declaration_t *declaration)
652 assert(declaration->source_position.input_name != NULL);
653 return stack_push(&environment_stack, declaration, context);
656 static declaration_t *label_push(declaration_t *declaration)
658 return stack_push(&label_stack, declaration, ¤t_function->context);
662 * pops symbols from the environment stack until @p new_top is the top element
664 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
666 stack_entry_t *stack = *stack_ptr;
667 size_t top = ARR_LEN(stack);
670 assert(new_top <= top);
674 for(i = top; i > new_top; --i) {
675 stack_entry_t *entry = & stack[i - 1];
677 declaration_t *old_declaration = entry->old_declaration;
678 symbol_t *symbol = entry->symbol;
679 namespace_t namespc = (namespace_t)entry->namespc;
681 /* replace/remove declaration */
682 declaration_t *declaration = symbol->declaration;
683 assert(declaration != NULL);
684 if(declaration->namespc == namespc) {
685 if(old_declaration == NULL) {
686 symbol->declaration = declaration->symbol_next;
688 symbol->declaration = old_declaration;
691 declaration_t *iter_last = declaration;
692 declaration_t *iter = declaration->symbol_next;
693 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
694 /* replace an entry? */
695 if(iter->namespc == namespc) {
696 assert(iter_last != NULL);
697 iter_last->symbol_next = old_declaration;
698 old_declaration->symbol_next = iter->symbol_next;
702 assert(iter != NULL);
706 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
709 static void environment_pop_to(size_t new_top)
711 stack_pop_to(&environment_stack, new_top);
714 static void label_pop_to(size_t new_top)
716 stack_pop_to(&label_stack, new_top);
720 static int get_rank(const type_t *type)
722 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
723 * and esp. footnote 108). However we can't fold constants (yet), so we
724 * can't decide wether unsigned int is possible, while int always works.
725 * (unsigned int would be preferable when possible... for stuff like
726 * struct { enum { ... } bla : 4; } ) */
727 if(type->type == TYPE_ENUM)
728 return ATOMIC_TYPE_INT;
730 assert(type->type == TYPE_ATOMIC);
731 const atomic_type_t *atomic_type = &type->atomic;
732 atomic_type_type_t atype = atomic_type->atype;
736 static type_t *promote_integer(type_t *type)
738 if(get_rank(type) < ATOMIC_TYPE_INT)
744 static expression_t *create_cast_expression(expression_t *expression,
747 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
749 cast->unary.type = UNEXPR_CAST_IMPLICIT;
750 cast->unary.value = expression;
751 cast->base.datatype = dest_type;
756 static bool is_null_expression(const expression_t *const expression)
758 if (expression->type != EXPR_CONST)
761 type_t *const type = skip_typeref(expression->base.datatype);
762 if (!is_type_integer(type))
765 return expression->conste.v.int_value == 0;
768 static expression_t *create_implicit_cast(expression_t *expression,
771 type_t *source_type = expression->base.datatype;
773 if(source_type == NULL)
776 source_type = skip_typeref(source_type);
777 dest_type = skip_typeref(dest_type);
779 if(source_type == dest_type)
782 switch (dest_type->type) {
784 /* TODO warning for implicitly converting to enum */
786 if (source_type->type != TYPE_ATOMIC &&
787 source_type->type != TYPE_ENUM) {
788 panic("casting of non-atomic types not implemented yet");
791 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
792 type_error_incompatible("can't cast types",
793 expression->base.source_position, source_type,
798 return create_cast_expression(expression, dest_type);
801 switch (source_type->type) {
803 if (is_null_expression(expression)) {
804 return create_cast_expression(expression, dest_type);
809 if (pointers_compatible(source_type, dest_type)) {
810 return create_cast_expression(expression, dest_type);
815 array_type_t *array_type = &source_type->array;
816 pointer_type_t *pointer_type = &dest_type->pointer;
817 if (types_compatible(array_type->element_type,
818 pointer_type->points_to)) {
819 return create_cast_expression(expression, dest_type);
825 panic("casting of non-atomic types not implemented yet");
828 type_error_incompatible("can't implicitly cast types",
829 expression->base.source_position, source_type, dest_type);
833 panic("casting of non-atomic types not implemented yet");
837 /** Implements the rules from § 6.5.16.1 */
838 static void semantic_assign(type_t *orig_type_left, expression_t **right,
841 type_t *orig_type_right = (*right)->base.datatype;
843 if(orig_type_right == NULL)
846 type_t *const type_left = skip_typeref(orig_type_left);
847 type_t *const type_right = skip_typeref(orig_type_right);
849 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
850 (is_type_pointer(type_left) && is_null_expression(*right)) ||
851 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
852 && is_type_pointer(type_right))) {
853 *right = create_implicit_cast(*right, type_left);
857 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
858 pointer_type_t *pointer_type_left = &type_left->pointer;
859 pointer_type_t *pointer_type_right = &type_right->pointer;
860 type_t *points_to_left = pointer_type_left->points_to;
861 type_t *points_to_right = pointer_type_right->points_to;
863 points_to_left = skip_typeref(points_to_left);
864 points_to_right = skip_typeref(points_to_right);
866 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
867 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
868 && !types_compatible(points_to_left, points_to_right)) {
869 goto incompatible_assign_types;
872 /* the left type has all qualifiers from the right type */
873 unsigned missing_qualifiers
874 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
875 if(missing_qualifiers != 0) {
876 parser_print_error_prefix();
877 fprintf(stderr, "destination type ");
878 print_type_quoted(type_left);
879 fprintf(stderr, " in %s from type ", context);
880 print_type_quoted(type_right);
881 fprintf(stderr, " lacks qualifiers '");
882 print_type_qualifiers(missing_qualifiers);
883 fprintf(stderr, "' in pointed-to type\n");
887 *right = create_implicit_cast(*right, type_left);
891 if (is_type_compound(type_left)
892 && types_compatible(type_left, type_right)) {
893 *right = create_implicit_cast(*right, type_left);
897 incompatible_assign_types:
898 /* TODO: improve error message */
899 parser_print_error_prefix();
900 fprintf(stderr, "incompatible types in %s\n", context);
901 parser_print_error_prefix();
902 print_type_quoted(type_left);
903 fputs(" <- ", stderr);
904 print_type_quoted(type_right);
908 static expression_t *parse_constant_expression(void)
910 /* start parsing at precedence 7 (conditional expression) */
911 return parse_sub_expression(7);
914 static expression_t *parse_assignment_expression(void)
916 /* start parsing at precedence 2 (assignment expression) */
917 return parse_sub_expression(2);
920 typedef struct declaration_specifiers_t declaration_specifiers_t;
921 struct declaration_specifiers_t {
922 unsigned char storage_class;
927 static void parse_compound_type_entries(void);
928 static declaration_t *parse_declarator(
929 const declaration_specifiers_t *specifiers, type_t *type,
930 bool may_be_abstract);
931 static declaration_t *record_declaration(declaration_t *declaration);
933 static const char *parse_string_literals(void)
935 assert(token.type == T_STRING_LITERAL);
936 const char *result = token.v.string;
940 while(token.type == T_STRING_LITERAL) {
941 result = concat_strings(result, token.v.string);
948 static void parse_attributes(void)
952 case T___attribute__: {
960 parse_error("EOF while parsing attribute");
979 if(token.type != T_STRING_LITERAL) {
980 parse_error_expected("while parsing assembler attribute",
985 parse_string_literals();
990 goto attributes_finished;
999 static designator_t *parse_designation(void)
1001 if(token.type != '[' && token.type != '.')
1004 designator_t *result = NULL;
1005 designator_t *last = NULL;
1008 designator_t *designator;
1009 switch(token.type) {
1011 designator = allocate_ast_zero(sizeof(designator[0]));
1013 designator->array_access = parse_constant_expression();
1017 designator = allocate_ast_zero(sizeof(designator[0]));
1019 if(token.type != T_IDENTIFIER) {
1020 parse_error_expected("while parsing designator",
1024 designator->symbol = token.v.symbol;
1032 assert(designator != NULL);
1034 last->next = designator;
1036 result = designator;
1043 static initializer_t *initializer_from_string(array_type_t *type,
1046 /* TODO: check len vs. size of array type */
1049 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1050 initializer->string.string = string;
1055 static initializer_t *initializer_from_expression(type_t *type,
1056 expression_t *expression)
1058 /* TODO check that expression is a constant expression */
1060 /* § 6.7.8.14/15 char array may be initialized by string literals */
1061 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1062 array_type_t *array_type = &type->array;
1063 type_t *element_type = array_type->element_type;
1065 if(element_type->type == TYPE_ATOMIC) {
1066 atomic_type_t *atomic_type = &element_type->atomic;
1067 atomic_type_type_t atype = atomic_type->atype;
1069 /* TODO handle wide strings */
1070 if(atype == ATOMIC_TYPE_CHAR
1071 || atype == ATOMIC_TYPE_SCHAR
1072 || atype == ATOMIC_TYPE_UCHAR) {
1074 string_literal_expression_t *literal = &expression->string;
1075 return initializer_from_string(array_type, literal->value);
1080 semantic_assign(type, &expression, "initializer");
1082 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1083 result->value.value = expression;
1088 static initializer_t *parse_sub_initializer(type_t *type,
1089 expression_t *expression,
1090 type_t *expression_type);
1092 static initializer_t *parse_sub_initializer_elem(type_t *type)
1094 if(token.type == '{') {
1095 return parse_sub_initializer(type, NULL, NULL);
1098 expression_t *expression = parse_assignment_expression();
1099 type_t *expression_type = skip_typeref(expression->base.datatype);
1101 return parse_sub_initializer(type, expression, expression_type);
1104 static bool had_initializer_brace_warning;
1106 static initializer_t *parse_sub_initializer(type_t *type,
1107 expression_t *expression,
1108 type_t *expression_type)
1110 if(is_type_scalar(type)) {
1111 /* there might be extra {} hierarchies */
1112 if(token.type == '{') {
1114 if(!had_initializer_brace_warning) {
1115 parse_warning("braces around scalar initializer");
1116 had_initializer_brace_warning = true;
1118 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1119 if(token.type == ',') {
1121 /* TODO: warn about excessive elements */
1127 if(expression == NULL) {
1128 expression = parse_assignment_expression();
1130 return initializer_from_expression(type, expression);
1133 /* TODO: ignore qualifiers, comparing pointers is probably
1135 if(expression != NULL && expression_type == type) {
1136 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1139 semantic_assign(type, &expression, "initializer");
1141 result->value.value = expression;
1146 bool read_paren = false;
1147 if(token.type == '{') {
1152 /* descend into subtype */
1153 initializer_t *result = NULL;
1154 initializer_t **elems;
1155 if(type->type == TYPE_ARRAY) {
1156 array_type_t *array_type = &type->array;
1157 type_t *element_type = array_type->element_type;
1158 element_type = skip_typeref(element_type);
1161 had_initializer_brace_warning = false;
1162 if(expression == NULL) {
1163 sub = parse_sub_initializer_elem(element_type);
1165 sub = parse_sub_initializer(element_type, expression,
1169 /* didn't match the subtypes -> try the parent type */
1171 assert(!read_paren);
1175 elems = NEW_ARR_F(initializer_t*, 0);
1176 ARR_APP1(initializer_t*, elems, sub);
1179 if(token.type == '}')
1182 if(token.type == '}')
1185 sub = parse_sub_initializer(element_type, NULL, NULL);
1187 /* TODO error, do nicer cleanup */
1188 parse_error("member initializer didn't match");
1192 ARR_APP1(initializer_t*, elems, sub);
1195 assert(type->type == TYPE_COMPOUND_STRUCT
1196 || type->type == TYPE_COMPOUND_UNION);
1197 compound_type_t *compound_type = &type->compound;
1198 context_t *context = & compound_type->declaration->context;
1200 declaration_t *first = context->declarations;
1203 type_t *first_type = first->type;
1204 first_type = skip_typeref(first_type);
1207 had_initializer_brace_warning = false;
1208 if(expression == NULL) {
1209 sub = parse_sub_initializer_elem(first_type);
1211 sub = parse_sub_initializer(first_type, expression,expression_type);
1214 /* didn't match the subtypes -> try our parent type */
1216 assert(!read_paren);
1220 elems = NEW_ARR_F(initializer_t*, 0);
1221 ARR_APP1(initializer_t*, elems, sub);
1223 declaration_t *iter = first->next;
1224 for( ; iter != NULL; iter = iter->next) {
1225 if(iter->symbol == NULL)
1227 if(iter->namespc != NAMESPACE_NORMAL)
1230 if(token.type == '}')
1233 if(token.type == '}')
1236 type_t *iter_type = iter->type;
1237 iter_type = skip_typeref(iter_type);
1239 sub = parse_sub_initializer(iter_type, NULL, NULL);
1241 /* TODO error, do nicer cleanup*/
1242 parse_error("member initializer didn't match");
1246 ARR_APP1(initializer_t*, elems, sub);
1250 int len = ARR_LEN(elems);
1251 size_t elems_size = sizeof(initializer_t*) * len;
1253 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1255 init->initializer.type = INITIALIZER_LIST;
1257 memcpy(init->initializers, elems, elems_size);
1260 result = (initializer_t*) init;
1263 if(token.type == ',')
1270 static initializer_t *parse_initializer(type_t *type)
1272 initializer_t *result;
1274 type = skip_typeref(type);
1276 if(token.type != '{') {
1277 expression_t *expression = parse_assignment_expression();
1278 return initializer_from_expression(type, expression);
1281 if(is_type_scalar(type)) {
1285 expression_t *expression = parse_assignment_expression();
1286 result = initializer_from_expression(type, expression);
1288 if(token.type == ',')
1294 result = parse_sub_initializer(type, NULL, NULL);
1302 static declaration_t *parse_compound_type_specifier(bool is_struct)
1310 symbol_t *symbol = NULL;
1311 declaration_t *declaration = NULL;
1313 if (token.type == T___attribute__) {
1318 if(token.type == T_IDENTIFIER) {
1319 symbol = token.v.symbol;
1323 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1325 declaration = get_declaration(symbol, NAMESPACE_UNION);
1327 } else if(token.type != '{') {
1329 parse_error_expected("while parsing struct type specifier",
1330 T_IDENTIFIER, '{', 0);
1332 parse_error_expected("while parsing union type specifier",
1333 T_IDENTIFIER, '{', 0);
1339 if(declaration == NULL) {
1340 declaration = allocate_ast_zero(sizeof(declaration[0]));
1343 declaration->namespc = NAMESPACE_STRUCT;
1345 declaration->namespc = NAMESPACE_UNION;
1347 declaration->source_position = token.source_position;
1348 declaration->symbol = symbol;
1349 record_declaration(declaration);
1352 if(token.type == '{') {
1353 if(declaration->init.is_defined) {
1354 assert(symbol != NULL);
1355 parser_print_error_prefix();
1356 fprintf(stderr, "multiple definition of %s %s\n",
1357 is_struct ? "struct" : "union", symbol->string);
1358 declaration->context.declarations = NULL;
1360 declaration->init.is_defined = true;
1362 int top = environment_top();
1363 context_t *last_context = context;
1364 set_context(& declaration->context);
1366 parse_compound_type_entries();
1369 assert(context == & declaration->context);
1370 set_context(last_context);
1371 environment_pop_to(top);
1377 static void parse_enum_entries(enum_type_t *const enum_type)
1381 if(token.type == '}') {
1383 parse_error("empty enum not allowed");
1388 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1390 if(token.type != T_IDENTIFIER) {
1391 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1395 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1396 entry->type = (type_t*) enum_type;
1397 entry->symbol = token.v.symbol;
1398 entry->source_position = token.source_position;
1401 if(token.type == '=') {
1403 entry->init.enum_value = parse_constant_expression();
1408 record_declaration(entry);
1410 if(token.type != ',')
1413 } while(token.type != '}');
1418 static type_t *parse_enum_specifier(void)
1422 declaration_t *declaration;
1425 if(token.type == T_IDENTIFIER) {
1426 symbol = token.v.symbol;
1429 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1430 } else if(token.type != '{') {
1431 parse_error_expected("while parsing enum type specifier",
1432 T_IDENTIFIER, '{', 0);
1439 if(declaration == NULL) {
1440 declaration = allocate_ast_zero(sizeof(declaration[0]));
1442 declaration->namespc = NAMESPACE_ENUM;
1443 declaration->source_position = token.source_position;
1444 declaration->symbol = symbol;
1447 type_t *const type = allocate_type_zero(TYPE_ENUM);
1448 type->enumt.declaration = declaration;
1450 if(token.type == '{') {
1451 if(declaration->init.is_defined) {
1452 parser_print_error_prefix();
1453 fprintf(stderr, "multiple definitions of enum %s\n",
1456 record_declaration(declaration);
1457 declaration->init.is_defined = 1;
1459 parse_enum_entries(&type->enumt);
1467 * if a symbol is a typedef to another type, return true
1469 static bool is_typedef_symbol(symbol_t *symbol)
1471 const declaration_t *const declaration =
1472 get_declaration(symbol, NAMESPACE_NORMAL);
1474 declaration != NULL &&
1475 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1478 static type_t *parse_typeof(void)
1486 expression_t *expression = NULL;
1489 switch(token.type) {
1490 case T___extension__:
1491 /* this can be a prefix to a typename or an expression */
1492 /* we simply eat it now. */
1495 } while(token.type == T___extension__);
1499 if(is_typedef_symbol(token.v.symbol)) {
1500 type = parse_typename();
1502 expression = parse_expression();
1503 type = expression->base.datatype;
1508 type = parse_typename();
1512 expression = parse_expression();
1513 type = expression->base.datatype;
1519 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1520 typeof_type->typeoft.expression = expression;
1521 typeof_type->typeoft.typeof_type = type;
1527 SPECIFIER_SIGNED = 1 << 0,
1528 SPECIFIER_UNSIGNED = 1 << 1,
1529 SPECIFIER_LONG = 1 << 2,
1530 SPECIFIER_INT = 1 << 3,
1531 SPECIFIER_DOUBLE = 1 << 4,
1532 SPECIFIER_CHAR = 1 << 5,
1533 SPECIFIER_SHORT = 1 << 6,
1534 SPECIFIER_LONG_LONG = 1 << 7,
1535 SPECIFIER_FLOAT = 1 << 8,
1536 SPECIFIER_BOOL = 1 << 9,
1537 SPECIFIER_VOID = 1 << 10,
1538 #ifdef PROVIDE_COMPLEX
1539 SPECIFIER_COMPLEX = 1 << 11,
1540 SPECIFIER_IMAGINARY = 1 << 12,
1544 static type_t *create_builtin_type(symbol_t *symbol)
1546 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1547 type->builtin.symbol = symbol;
1549 type->builtin.real_type = type_int;
1554 static type_t *get_typedef_type(symbol_t *symbol)
1556 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1557 if(declaration == NULL
1558 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1561 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1562 type->typedeft.declaration = declaration;
1567 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1569 type_t *type = NULL;
1570 unsigned type_qualifiers = 0;
1571 unsigned type_specifiers = 0;
1575 switch(token.type) {
1578 #define MATCH_STORAGE_CLASS(token, class) \
1580 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1581 parse_error("multiple storage classes in declaration " \
1584 specifiers->storage_class = class; \
1588 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1589 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1590 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1591 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1592 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1595 switch (specifiers->storage_class) {
1596 case STORAGE_CLASS_NONE:
1597 specifiers->storage_class = STORAGE_CLASS_THREAD;
1600 case STORAGE_CLASS_EXTERN:
1601 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1604 case STORAGE_CLASS_STATIC:
1605 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1609 parse_error("multiple storage classes in declaration specifiers");
1615 /* type qualifiers */
1616 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1618 type_qualifiers |= qualifier; \
1622 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1623 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1624 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1626 case T___extension__:
1631 /* type specifiers */
1632 #define MATCH_SPECIFIER(token, specifier, name) \
1635 if(type_specifiers & specifier) { \
1636 parse_error("multiple " name " type specifiers given"); \
1638 type_specifiers |= specifier; \
1642 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1643 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1644 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1645 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1646 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1647 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1648 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1649 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1650 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1651 #ifdef PROVIDE_COMPLEX
1652 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1653 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1657 specifiers->is_inline = true;
1662 if(type_specifiers & SPECIFIER_LONG_LONG) {
1663 parse_error("multiple type specifiers given");
1664 } else if(type_specifiers & SPECIFIER_LONG) {
1665 type_specifiers |= SPECIFIER_LONG_LONG;
1667 type_specifiers |= SPECIFIER_LONG;
1671 /* TODO: if type != NULL for the following rules should issue
1674 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1676 type->compound.declaration = parse_compound_type_specifier(true);
1680 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1682 type->compound.declaration = parse_compound_type_specifier(false);
1686 type = parse_enum_specifier();
1689 type = parse_typeof();
1691 case T___builtin_va_list:
1692 type = create_builtin_type(token.v.symbol);
1696 case T___attribute__:
1701 case T_IDENTIFIER: {
1702 type_t *typedef_type = get_typedef_type(token.v.symbol);
1704 if(typedef_type == NULL)
1705 goto finish_specifiers;
1708 type = typedef_type;
1712 /* function specifier */
1714 goto finish_specifiers;
1721 atomic_type_type_t atomic_type;
1723 /* match valid basic types */
1724 switch(type_specifiers) {
1725 case SPECIFIER_VOID:
1726 atomic_type = ATOMIC_TYPE_VOID;
1728 case SPECIFIER_CHAR:
1729 atomic_type = ATOMIC_TYPE_CHAR;
1731 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1732 atomic_type = ATOMIC_TYPE_SCHAR;
1734 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1735 atomic_type = ATOMIC_TYPE_UCHAR;
1737 case SPECIFIER_SHORT:
1738 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1739 case SPECIFIER_SHORT | SPECIFIER_INT:
1740 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1741 atomic_type = ATOMIC_TYPE_SHORT;
1743 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1744 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1745 atomic_type = ATOMIC_TYPE_USHORT;
1748 case SPECIFIER_SIGNED:
1749 case SPECIFIER_SIGNED | SPECIFIER_INT:
1750 atomic_type = ATOMIC_TYPE_INT;
1752 case SPECIFIER_UNSIGNED:
1753 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1754 atomic_type = ATOMIC_TYPE_UINT;
1756 case SPECIFIER_LONG:
1757 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1758 case SPECIFIER_LONG | SPECIFIER_INT:
1759 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1760 atomic_type = ATOMIC_TYPE_LONG;
1762 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1763 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1764 atomic_type = ATOMIC_TYPE_ULONG;
1766 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1767 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1768 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1769 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1771 atomic_type = ATOMIC_TYPE_LONGLONG;
1773 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1774 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1776 atomic_type = ATOMIC_TYPE_ULONGLONG;
1778 case SPECIFIER_FLOAT:
1779 atomic_type = ATOMIC_TYPE_FLOAT;
1781 case SPECIFIER_DOUBLE:
1782 atomic_type = ATOMIC_TYPE_DOUBLE;
1784 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1785 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1787 case SPECIFIER_BOOL:
1788 atomic_type = ATOMIC_TYPE_BOOL;
1790 #ifdef PROVIDE_COMPLEX
1791 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1792 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1794 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1795 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1797 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1798 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1800 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1801 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1803 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1804 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1806 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1807 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1811 /* invalid specifier combination, give an error message */
1812 if(type_specifiers == 0) {
1814 parse_warning("no type specifiers in declaration (using int)");
1815 atomic_type = ATOMIC_TYPE_INT;
1818 parse_error("no type specifiers given in declaration");
1820 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1821 (type_specifiers & SPECIFIER_UNSIGNED)) {
1822 parse_error("signed and unsigned specifiers gives");
1823 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1824 parse_error("only integer types can be signed or unsigned");
1826 parse_error("multiple datatypes in declaration");
1828 atomic_type = ATOMIC_TYPE_INVALID;
1831 type = allocate_type_zero(TYPE_ATOMIC);
1832 type->atomic.atype = atomic_type;
1835 if(type_specifiers != 0) {
1836 parse_error("multiple datatypes in declaration");
1840 type->base.qualifiers = type_qualifiers;
1842 type_t *result = typehash_insert(type);
1843 if(newtype && result != type) {
1847 specifiers->type = result;
1850 static type_qualifiers_t parse_type_qualifiers(void)
1852 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1855 switch(token.type) {
1856 /* type qualifiers */
1857 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1858 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1859 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1862 return type_qualifiers;
1867 static void parse_identifier_list(void)
1870 if(token.type != T_IDENTIFIER) {
1871 parse_error_expected("while parsing parameter identifier list",
1875 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1876 declaration->symbol = token.v.symbol;
1880 if(token.type != ',')
1886 static declaration_t *parse_parameter(void)
1888 declaration_specifiers_t specifiers;
1889 memset(&specifiers, 0, sizeof(specifiers));
1891 parse_declaration_specifiers(&specifiers);
1893 declaration_t *declaration
1894 = parse_declarator(&specifiers, specifiers.type, true);
1896 /* TODO check declaration constraints for parameters */
1897 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1898 parse_error("typedef not allowed in parameter list");
1901 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1903 if (declaration->type->type == TYPE_ARRAY) {
1904 const array_type_t *const arr_type = &declaration->type->array;
1905 type_t *element_type = arr_type->element_type;
1906 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1912 static declaration_t *parse_parameters(function_type_t *type)
1914 if(token.type == T_IDENTIFIER) {
1915 symbol_t *symbol = token.v.symbol;
1916 if(!is_typedef_symbol(symbol)) {
1917 /* TODO: K&R style C parameters */
1918 parse_identifier_list();
1923 if(token.type == ')') {
1924 type->unspecified_parameters = 1;
1927 if(token.type == T_void && look_ahead(1)->type == ')') {
1932 declaration_t *declarations = NULL;
1933 declaration_t *declaration;
1934 declaration_t *last_declaration = NULL;
1935 function_parameter_t *parameter;
1936 function_parameter_t *last_parameter = NULL;
1939 switch(token.type) {
1943 return declarations;
1946 case T___extension__:
1948 declaration = parse_parameter();
1950 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1951 memset(parameter, 0, sizeof(parameter[0]));
1952 parameter->type = declaration->type;
1954 if(last_parameter != NULL) {
1955 last_declaration->next = declaration;
1956 last_parameter->next = parameter;
1958 type->parameters = parameter;
1959 declarations = declaration;
1961 last_parameter = parameter;
1962 last_declaration = declaration;
1966 return declarations;
1968 if(token.type != ',')
1969 return declarations;
1979 } construct_type_type_t;
1981 typedef struct construct_type_t construct_type_t;
1982 struct construct_type_t {
1983 construct_type_type_t type;
1984 construct_type_t *next;
1987 typedef struct parsed_pointer_t parsed_pointer_t;
1988 struct parsed_pointer_t {
1989 construct_type_t construct_type;
1990 type_qualifiers_t type_qualifiers;
1993 typedef struct construct_function_type_t construct_function_type_t;
1994 struct construct_function_type_t {
1995 construct_type_t construct_type;
1996 type_t *function_type;
1999 typedef struct parsed_array_t parsed_array_t;
2000 struct parsed_array_t {
2001 construct_type_t construct_type;
2002 type_qualifiers_t type_qualifiers;
2008 typedef struct construct_base_type_t construct_base_type_t;
2009 struct construct_base_type_t {
2010 construct_type_t construct_type;
2014 static construct_type_t *parse_pointer_declarator(void)
2018 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2019 memset(pointer, 0, sizeof(pointer[0]));
2020 pointer->construct_type.type = CONSTRUCT_POINTER;
2021 pointer->type_qualifiers = parse_type_qualifiers();
2023 return (construct_type_t*) pointer;
2026 static construct_type_t *parse_array_declarator(void)
2030 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2031 memset(array, 0, sizeof(array[0]));
2032 array->construct_type.type = CONSTRUCT_ARRAY;
2034 if(token.type == T_static) {
2035 array->is_static = true;
2039 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2040 if(type_qualifiers != 0) {
2041 if(token.type == T_static) {
2042 array->is_static = true;
2046 array->type_qualifiers = type_qualifiers;
2048 if(token.type == '*' && look_ahead(1)->type == ']') {
2049 array->is_variable = true;
2051 } else if(token.type != ']') {
2052 array->size = parse_assignment_expression();
2057 return (construct_type_t*) array;
2060 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2064 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2066 declaration_t *parameters = parse_parameters(&type->function);
2067 if(declaration != NULL) {
2068 declaration->context.declarations = parameters;
2071 construct_function_type_t *construct_function_type =
2072 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2073 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2074 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2075 construct_function_type->function_type = type;
2079 return (construct_type_t*) construct_function_type;
2082 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2083 bool may_be_abstract)
2085 /* construct a single linked list of construct_type_t's which describe
2086 * how to construct the final declarator type */
2087 construct_type_t *first = NULL;
2088 construct_type_t *last = NULL;
2091 while(token.type == '*') {
2092 construct_type_t *type = parse_pointer_declarator();
2103 /* TODO: find out if this is correct */
2106 construct_type_t *inner_types = NULL;
2108 switch(token.type) {
2110 if(declaration == NULL) {
2111 parse_error("no identifier expected in typename");
2113 declaration->symbol = token.v.symbol;
2114 declaration->source_position = token.source_position;
2120 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2126 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2127 /* avoid a loop in the outermost scope, because eat_statement doesn't
2129 if(token.type == '}' && current_function == NULL) {
2137 construct_type_t *p = last;
2140 construct_type_t *type;
2141 switch(token.type) {
2143 type = parse_function_declarator(declaration);
2146 type = parse_array_declarator();
2149 goto declarator_finished;
2152 /* insert in the middle of the list (behind p) */
2154 type->next = p->next;
2165 declarator_finished:
2168 /* append inner_types at the end of the list, we don't to set last anymore
2169 * as it's not needed anymore */
2171 assert(first == NULL);
2172 first = inner_types;
2174 last->next = inner_types;
2180 static type_t *construct_declarator_type(construct_type_t *construct_list,
2183 construct_type_t *iter = construct_list;
2184 for( ; iter != NULL; iter = iter->next) {
2185 switch(iter->type) {
2186 case CONSTRUCT_INVALID:
2187 panic("invalid type construction found");
2188 case CONSTRUCT_FUNCTION: {
2189 construct_function_type_t *construct_function_type
2190 = (construct_function_type_t*) iter;
2192 type_t *function_type = construct_function_type->function_type;
2194 function_type->function.result_type = type;
2196 type = function_type;
2200 case CONSTRUCT_POINTER: {
2201 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2202 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2203 pointer_type->pointer.points_to = type;
2204 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2206 type = pointer_type;
2210 case CONSTRUCT_ARRAY: {
2211 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2212 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2214 array_type->base.qualifiers = parsed_array->type_qualifiers;
2215 array_type->array.element_type = type;
2216 array_type->array.is_static = parsed_array->is_static;
2217 array_type->array.is_variable = parsed_array->is_variable;
2218 array_type->array.size = parsed_array->size;
2225 type_t *hashed_type = typehash_insert(type);
2226 if(hashed_type != type) {
2227 /* the function type was constructed earlier freeing it here will
2228 * destroy other types... */
2229 if(iter->type != CONSTRUCT_FUNCTION) {
2239 static declaration_t *parse_declarator(
2240 const declaration_specifiers_t *specifiers,
2241 type_t *type, bool may_be_abstract)
2243 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2244 declaration->storage_class = specifiers->storage_class;
2245 declaration->is_inline = specifiers->is_inline;
2247 construct_type_t *construct_type
2248 = parse_inner_declarator(declaration, may_be_abstract);
2249 declaration->type = construct_declarator_type(construct_type, type);
2251 if(construct_type != NULL) {
2252 obstack_free(&temp_obst, construct_type);
2258 static type_t *parse_abstract_declarator(type_t *base_type)
2260 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2262 type_t *result = construct_declarator_type(construct_type, base_type);
2263 if(construct_type != NULL) {
2264 obstack_free(&temp_obst, construct_type);
2270 static declaration_t *record_declaration(declaration_t *declaration)
2272 assert(context != NULL);
2274 symbol_t *symbol = declaration->symbol;
2275 if(symbol != NULL) {
2276 declaration_t *alias = environment_push(declaration);
2277 if(alias != declaration)
2280 declaration->parent_context = context;
2283 if(last_declaration != NULL) {
2284 last_declaration->next = declaration;
2286 context->declarations = declaration;
2288 last_declaration = declaration;
2293 static void parser_error_multiple_definition(declaration_t *previous,
2294 declaration_t *declaration)
2296 parser_print_error_prefix_pos(declaration->source_position);
2297 fprintf(stderr, "multiple definition of symbol '%s'\n",
2298 declaration->symbol->string);
2299 parser_print_error_prefix_pos(previous->source_position);
2300 fprintf(stderr, "this is the location of the previous definition.\n");
2303 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2306 declaration_t *ndeclaration
2307 = parse_declarator(specifiers, specifiers->type, false);
2309 declaration_t *declaration = record_declaration(ndeclaration);
2311 type_t *orig_type = declaration->type;
2312 type_t *type = skip_typeref(orig_type);
2313 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2314 parser_print_warning_prefix_pos(declaration->source_position);
2315 fprintf(stderr, "variable '%s' declared 'inline'\n",
2316 declaration->symbol->string);
2319 if(token.type == '=') {
2322 /* TODO: check that this is an allowed type (no function type) */
2324 if(declaration->init.initializer != NULL) {
2325 parser_error_multiple_definition(declaration, ndeclaration);
2328 initializer_t *initializer = parse_initializer(type);
2330 if(type->type == TYPE_ARRAY && initializer != NULL) {
2331 array_type_t *array_type = &type->array;
2333 if(array_type->size == NULL) {
2334 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2336 cnst->base.datatype = type_size_t;
2338 if(initializer->type == INITIALIZER_LIST) {
2339 initializer_list_t *list = &initializer->list;
2340 cnst->conste.v.int_value = list->len;
2342 assert(initializer->type == INITIALIZER_STRING);
2343 initializer_string_t *string = &initializer->string;
2344 cnst->conste.v.int_value = strlen(string->string) + 1;
2347 array_type->size = cnst;
2352 ndeclaration->init.initializer = initializer;
2353 } else if(token.type == '{') {
2354 if(type->type != TYPE_FUNCTION) {
2355 parser_print_error_prefix();
2356 fprintf(stderr, "declarator '");
2357 print_type_ext(orig_type, declaration->symbol, NULL);
2358 fprintf(stderr, "' has a body but is not a function type.\n");
2362 function_type_t *function_type = &type->function;
2363 /* § 6.7.5.3 (14) a function definition with () means no
2365 if(function_type->unspecified_parameters) {
2366 type_t *duplicate = duplicate_type(type);
2367 duplicate->function.unspecified_parameters = false;
2369 type = typehash_insert(duplicate);
2370 if(type != duplicate) {
2371 //obstack_free(type_obst, duplicate);
2373 function_type = &type->function;
2376 if(declaration->init.statement != NULL) {
2377 parser_error_multiple_definition(declaration, ndeclaration);
2379 if(ndeclaration != declaration) {
2380 memcpy(&declaration->context, &ndeclaration->context,
2381 sizeof(declaration->context));
2384 int top = environment_top();
2385 context_t *last_context = context;
2386 set_context(&declaration->context);
2388 /* push function parameters */
2389 declaration_t *parameter = declaration->context.declarations;
2390 for( ; parameter != NULL; parameter = parameter->next) {
2391 environment_push(parameter);
2394 int label_stack_top = label_top();
2395 declaration_t *old_current_function = current_function;
2396 current_function = declaration;
2398 statement_t *statement = parse_compound_statement();
2400 assert(current_function == declaration);
2401 current_function = old_current_function;
2402 label_pop_to(label_stack_top);
2404 assert(context == &declaration->context);
2405 set_context(last_context);
2406 environment_pop_to(top);
2408 declaration->init.statement = statement;
2412 if(token.type != ',')
2419 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2422 if(token.type == ':') {
2424 parse_constant_expression();
2425 /* TODO (bitfields) */
2427 declaration_t *declaration
2428 = parse_declarator(specifiers, specifiers->type, true);
2430 /* TODO: check constraints for struct declarations */
2431 /* TODO: check for doubled fields */
2432 record_declaration(declaration);
2434 if(token.type == ':') {
2436 parse_constant_expression();
2437 /* TODO (bitfields) */
2441 if(token.type != ',')
2448 static void parse_compound_type_entries(void)
2452 while(token.type != '}' && token.type != T_EOF) {
2453 declaration_specifiers_t specifiers;
2454 memset(&specifiers, 0, sizeof(specifiers));
2455 parse_declaration_specifiers(&specifiers);
2457 parse_struct_declarators(&specifiers);
2459 if(token.type == T_EOF) {
2460 parse_error("unexpected error while parsing struct");
2465 static void parse_declaration(void)
2467 source_position_t source_position = token.source_position;
2469 declaration_specifiers_t specifiers;
2470 memset(&specifiers, 0, sizeof(specifiers));
2471 parse_declaration_specifiers(&specifiers);
2473 if(token.type == ';') {
2474 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2475 parse_warning_pos(source_position,
2476 "useless keyword in empty declaration");
2478 switch (specifiers.type->type) {
2479 case TYPE_COMPOUND_STRUCT:
2480 case TYPE_COMPOUND_UNION: {
2481 const compound_type_t *const comp_type
2482 = &specifiers.type->compound;
2483 if (comp_type->declaration->symbol == NULL) {
2484 parse_warning_pos(source_position,
2485 "unnamed struct/union that defines no instances");
2490 case TYPE_ENUM: break;
2493 parse_warning_pos(source_position, "empty declaration");
2499 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2501 declaration->type = specifiers.type;
2502 declaration->storage_class = specifiers.storage_class;
2503 declaration->source_position = source_position;
2504 record_declaration(declaration);
2507 parse_init_declarators(&specifiers);
2510 static type_t *parse_typename(void)
2512 declaration_specifiers_t specifiers;
2513 memset(&specifiers, 0, sizeof(specifiers));
2514 parse_declaration_specifiers(&specifiers);
2515 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2516 /* TODO: improve error message, user does probably not know what a
2517 * storage class is...
2519 parse_error("typename may not have a storage class");
2522 type_t *result = parse_abstract_declarator(specifiers.type);
2530 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2531 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2532 expression_t *left);
2534 typedef struct expression_parser_function_t expression_parser_function_t;
2535 struct expression_parser_function_t {
2536 unsigned precedence;
2537 parse_expression_function parser;
2538 unsigned infix_precedence;
2539 parse_expression_infix_function infix_parser;
2542 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2544 static expression_t *make_invalid_expression(void)
2546 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2547 expression->base.source_position = token.source_position;
2551 static expression_t *expected_expression_error(void)
2553 parser_print_error_prefix();
2554 fprintf(stderr, "expected expression, got token ");
2555 print_token(stderr, & token);
2556 fprintf(stderr, "\n");
2560 return make_invalid_expression();
2563 static expression_t *parse_string_const(void)
2565 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2566 cnst->base.datatype = type_string;
2567 cnst->string.value = parse_string_literals();
2572 static expression_t *parse_int_const(void)
2574 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2575 cnst->base.datatype = token.datatype;
2576 cnst->conste.v.int_value = token.v.intvalue;
2583 static expression_t *parse_float_const(void)
2585 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2586 cnst->base.datatype = token.datatype;
2587 cnst->conste.v.float_value = token.v.floatvalue;
2594 static declaration_t *create_implicit_function(symbol_t *symbol,
2595 const source_position_t source_position)
2597 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2598 ntype->function.result_type = type_int;
2599 ntype->function.unspecified_parameters = true;
2601 type_t *type = typehash_insert(ntype);
2606 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2608 declaration->storage_class = STORAGE_CLASS_EXTERN;
2609 declaration->type = type;
2610 declaration->symbol = symbol;
2611 declaration->source_position = source_position;
2613 /* prepend the implicit definition to the global context
2614 * this is safe since the symbol wasn't declared as anything else yet
2616 assert(symbol->declaration == NULL);
2618 context_t *last_context = context;
2619 context = global_context;
2621 environment_push(declaration);
2622 declaration->next = context->declarations;
2623 context->declarations = declaration;
2625 context = last_context;
2630 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2632 function_parameter_t *parameter
2633 = obstack_alloc(type_obst, sizeof(parameter[0]));
2634 memset(parameter, 0, sizeof(parameter[0]));
2635 parameter->type = argument_type;
2637 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2638 type->function.result_type = result_type;
2639 type->function.parameters = parameter;
2641 type_t *result = typehash_insert(type);
2642 if(result != type) {
2649 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2651 switch(symbol->ID) {
2652 case T___builtin_alloca:
2653 return make_function_1_type(type_void_ptr, type_size_t);
2654 case T___builtin_nanf:
2655 return make_function_1_type(type_double, type_string);
2657 panic("not implemented builtin symbol found");
2662 * performs automatic type cast as described in § 6.3.2.1
2664 static type_t *automatic_type_conversion(type_t *type)
2669 if(type->type == TYPE_ARRAY) {
2670 array_type_t *array_type = &type->array;
2671 type_t *element_type = array_type->element_type;
2672 unsigned qualifiers = array_type->type.qualifiers;
2674 return make_pointer_type(element_type, qualifiers);
2677 if(type->type == TYPE_FUNCTION) {
2678 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2685 * reverts the automatic casts of array to pointer types and function
2686 * to function-pointer types as defined § 6.3.2.1
2688 type_t *revert_automatic_type_conversion(const expression_t *expression)
2690 if(expression->base.datatype == NULL)
2693 switch(expression->type) {
2694 case EXPR_REFERENCE: {
2695 const reference_expression_t *ref = &expression->reference;
2696 return ref->declaration->type;
2699 const select_expression_t *select = &expression->select;
2700 return select->compound_entry->type;
2703 const unary_expression_t *unary = &expression->unary;
2704 if(unary->type == UNEXPR_DEREFERENCE) {
2705 expression_t *value = unary->value;
2706 type_t *type = skip_typeref(value->base.datatype);
2707 pointer_type_t *pointer_type = &type->pointer;
2709 return pointer_type->points_to;
2713 case EXPR_BUILTIN_SYMBOL: {
2714 const builtin_symbol_expression_t *builtin
2715 = &expression->builtin_symbol;
2716 return get_builtin_symbol_type(builtin->symbol);
2718 case EXPR_ARRAY_ACCESS: {
2719 const array_access_expression_t *array_access
2720 = &expression->array_access;
2721 const expression_t *array_ref = array_access->array_ref;
2722 type_t *type_left = skip_typeref(array_ref->base.datatype);
2723 assert(is_type_pointer(type_left));
2724 pointer_type_t *pointer_type = &type_left->pointer;
2725 return pointer_type->points_to;
2732 return expression->base.datatype;
2735 static expression_t *parse_reference(void)
2737 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2739 reference_expression_t *ref = &expression->reference;
2740 ref->symbol = token.v.symbol;
2742 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2744 source_position_t source_position = token.source_position;
2747 if(declaration == NULL) {
2749 /* an implicitly defined function */
2750 if(token.type == '(') {
2751 parser_print_prefix_pos(token.source_position);
2752 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2753 ref->symbol->string);
2755 declaration = create_implicit_function(ref->symbol,
2760 parser_print_error_prefix();
2761 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2766 type_t *type = declaration->type;
2767 /* we always do the auto-type conversions; the & and sizeof parser contains
2768 * code to revert this! */
2769 type = automatic_type_conversion(type);
2771 ref->declaration = declaration;
2772 ref->expression.datatype = type;
2777 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2781 /* TODO check if explicit cast is allowed and issue warnings/errors */
2784 static expression_t *parse_cast(void)
2786 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
2788 cast->unary.type = UNEXPR_CAST;
2789 cast->base.source_position = token.source_position;
2791 type_t *type = parse_typename();
2794 expression_t *value = parse_sub_expression(20);
2796 check_cast_allowed(value, type);
2798 cast->base.datatype = type;
2799 cast->unary.value = value;
2804 static expression_t *parse_statement_expression(void)
2806 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2808 statement_t *statement = parse_compound_statement();
2809 expression->statement.statement = statement;
2810 if(statement == NULL) {
2815 assert(statement->type == STATEMENT_COMPOUND);
2816 compound_statement_t *compound_statement = &statement->compound;
2818 /* find last statement and use it's type */
2819 const statement_t *last_statement = NULL;
2820 const statement_t *iter = compound_statement->statements;
2821 for( ; iter != NULL; iter = iter->base.next) {
2822 last_statement = iter;
2825 if(last_statement->type == STATEMENT_EXPRESSION) {
2826 const expression_statement_t *expression_statement
2827 = &last_statement->expression;
2828 expression->base.datatype
2829 = expression_statement->expression->base.datatype;
2831 expression->base.datatype = type_void;
2839 static expression_t *parse_brace_expression(void)
2843 switch(token.type) {
2845 /* gcc extension: a stement expression */
2846 return parse_statement_expression();
2850 return parse_cast();
2852 if(is_typedef_symbol(token.v.symbol)) {
2853 return parse_cast();
2857 expression_t *result = parse_expression();
2863 static expression_t *parse_function_keyword(void)
2868 if (current_function == NULL) {
2869 parse_error("'__func__' used outside of a function");
2872 string_literal_expression_t *expression
2873 = allocate_ast_zero(sizeof(expression[0]));
2875 expression->expression.type = EXPR_FUNCTION;
2876 expression->expression.datatype = type_string;
2877 expression->value = "TODO: FUNCTION";
2879 return (expression_t*) expression;
2882 static expression_t *parse_pretty_function_keyword(void)
2884 eat(T___PRETTY_FUNCTION__);
2887 string_literal_expression_t *expression
2888 = allocate_ast_zero(sizeof(expression[0]));
2890 expression->expression.type = EXPR_PRETTY_FUNCTION;
2891 expression->expression.datatype = type_string;
2892 expression->value = "TODO: PRETTY FUNCTION";
2894 return (expression_t*) expression;
2897 static designator_t *parse_designator(void)
2899 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2901 if(token.type != T_IDENTIFIER) {
2902 parse_error_expected("while parsing member designator",
2907 result->symbol = token.v.symbol;
2910 designator_t *last_designator = result;
2912 if(token.type == '.') {
2914 if(token.type != T_IDENTIFIER) {
2915 parse_error_expected("while parsing member designator",
2920 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2921 designator->symbol = token.v.symbol;
2924 last_designator->next = designator;
2925 last_designator = designator;
2928 if(token.type == '[') {
2930 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2931 designator->array_access = parse_expression();
2932 if(designator->array_access == NULL) {
2938 last_designator->next = designator;
2939 last_designator = designator;
2948 static expression_t *parse_offsetof(void)
2950 eat(T___builtin_offsetof);
2952 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
2953 expression->base.datatype = type_size_t;
2956 expression->offsetofe.type = parse_typename();
2958 expression->offsetofe.designator = parse_designator();
2964 static expression_t *parse_va_arg(void)
2966 eat(T___builtin_va_arg);
2968 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
2971 expression->va_arge.arg = parse_assignment_expression();
2973 expression->base.datatype = parse_typename();
2979 static expression_t *parse_builtin_symbol(void)
2981 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
2983 symbol_t *symbol = token.v.symbol;
2985 expression->builtin_symbol.symbol = symbol;
2988 type_t *type = get_builtin_symbol_type(symbol);
2989 type = automatic_type_conversion(type);
2991 expression->base.datatype = type;
2995 static expression_t *parse_primary_expression(void)
2997 switch(token.type) {
2999 return parse_int_const();
3000 case T_FLOATINGPOINT:
3001 return parse_float_const();
3002 case T_STRING_LITERAL:
3003 return parse_string_const();
3005 return parse_reference();
3006 case T___FUNCTION__:
3008 return parse_function_keyword();
3009 case T___PRETTY_FUNCTION__:
3010 return parse_pretty_function_keyword();
3011 case T___builtin_offsetof:
3012 return parse_offsetof();
3013 case T___builtin_va_arg:
3014 return parse_va_arg();
3015 case T___builtin_nanf:
3016 case T___builtin_alloca:
3017 case T___builtin_expect:
3018 case T___builtin_va_start:
3019 case T___builtin_va_end:
3020 return parse_builtin_symbol();
3023 return parse_brace_expression();
3026 parser_print_error_prefix();
3027 fprintf(stderr, "unexpected token ");
3028 print_token(stderr, &token);
3029 fprintf(stderr, "\n");
3032 return make_invalid_expression();
3035 static expression_t *parse_array_expression(unsigned precedence,
3042 expression_t *inside = parse_expression();
3044 array_access_expression_t *array_access
3045 = allocate_ast_zero(sizeof(array_access[0]));
3047 array_access->expression.type = EXPR_ARRAY_ACCESS;
3049 type_t *type_left = left->base.datatype;
3050 type_t *type_inside = inside->base.datatype;
3051 type_t *result_type = NULL;
3053 if(type_left != NULL && type_inside != NULL) {
3054 type_left = skip_typeref(type_left);
3055 type_inside = skip_typeref(type_inside);
3057 if(is_type_pointer(type_left)) {
3058 pointer_type_t *pointer = &type_left->pointer;
3059 result_type = pointer->points_to;
3060 array_access->array_ref = left;
3061 array_access->index = inside;
3062 } else if(is_type_pointer(type_inside)) {
3063 pointer_type_t *pointer = &type_inside->pointer;
3064 result_type = pointer->points_to;
3065 array_access->array_ref = inside;
3066 array_access->index = left;
3067 array_access->flipped = true;
3069 parser_print_error_prefix();
3070 fprintf(stderr, "array access on object with non-pointer types ");
3071 print_type_quoted(type_left);
3072 fprintf(stderr, ", ");
3073 print_type_quoted(type_inside);
3074 fprintf(stderr, "\n");
3077 array_access->array_ref = left;
3078 array_access->index = inside;
3081 if(token.type != ']') {
3082 parse_error_expected("Problem while parsing array access", ']', 0);
3083 return (expression_t*) array_access;
3087 result_type = automatic_type_conversion(result_type);
3088 array_access->expression.datatype = result_type;
3090 return (expression_t*) array_access;
3093 static bool is_declaration_specifier(const token_t *token,
3094 bool only_type_specifiers)
3096 switch(token->type) {
3100 return is_typedef_symbol(token->v.symbol);
3103 if(only_type_specifiers)
3112 static expression_t *parse_sizeof(unsigned precedence)
3116 sizeof_expression_t *sizeof_expression
3117 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3118 sizeof_expression->expression.type = EXPR_SIZEOF;
3119 sizeof_expression->expression.datatype = type_size_t;
3121 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3123 sizeof_expression->type = parse_typename();
3126 expression_t *expression = parse_sub_expression(precedence);
3127 expression->base.datatype = revert_automatic_type_conversion(expression);
3129 sizeof_expression->type = expression->base.datatype;
3130 sizeof_expression->size_expression = expression;
3133 return (expression_t*) sizeof_expression;
3136 static expression_t *parse_select_expression(unsigned precedence,
3137 expression_t *compound)
3140 assert(token.type == '.' || token.type == T_MINUSGREATER);
3142 bool is_pointer = (token.type == T_MINUSGREATER);
3145 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3146 select->select.compound = compound;
3148 if(token.type != T_IDENTIFIER) {
3149 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3152 symbol_t *symbol = token.v.symbol;
3153 select->select.symbol = symbol;
3156 type_t *orig_type = compound->base.datatype;
3157 if(orig_type == NULL)
3158 return make_invalid_expression();
3160 type_t *type = skip_typeref(orig_type);
3162 type_t *type_left = type;
3164 if(type->type != TYPE_POINTER) {
3165 parser_print_error_prefix();
3166 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3167 print_type_quoted(orig_type);
3168 fputc('\n', stderr);
3169 return make_invalid_expression();
3171 pointer_type_t *pointer_type = &type->pointer;
3172 type_left = pointer_type->points_to;
3174 type_left = skip_typeref(type_left);
3176 if(type_left->type != TYPE_COMPOUND_STRUCT
3177 && type_left->type != TYPE_COMPOUND_UNION) {
3178 parser_print_error_prefix();
3179 fprintf(stderr, "request for member '%s' in something not a struct or "
3180 "union, but ", symbol->string);
3181 print_type_quoted(type_left);
3182 fputc('\n', stderr);
3183 return make_invalid_expression();
3186 compound_type_t *compound_type = &type_left->compound;
3187 declaration_t *declaration = compound_type->declaration;
3189 if(!declaration->init.is_defined) {
3190 parser_print_error_prefix();
3191 fprintf(stderr, "request for member '%s' of incomplete type ",
3193 print_type_quoted(type_left);
3194 fputc('\n', stderr);
3195 return make_invalid_expression();
3198 declaration_t *iter = declaration->context.declarations;
3199 for( ; iter != NULL; iter = iter->next) {
3200 if(iter->symbol == symbol) {
3205 parser_print_error_prefix();
3206 print_type_quoted(type_left);
3207 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3208 return make_invalid_expression();
3211 /* we always do the auto-type conversions; the & and sizeof parser contains
3212 * code to revert this! */
3213 type_t *expression_type = automatic_type_conversion(iter->type);
3215 select->select.compound_entry = iter;
3216 select->base.datatype = expression_type;
3220 static expression_t *parse_call_expression(unsigned precedence,
3221 expression_t *expression)
3224 expression_t *result = allocate_expression_zero(EXPR_CALL);
3226 call_expression_t *call = &result->call;
3227 call->function = expression;
3229 function_type_t *function_type = NULL;
3230 type_t *orig_type = expression->base.datatype;
3231 if(orig_type != NULL) {
3232 type_t *type = skip_typeref(orig_type);
3234 if(is_type_pointer(type)) {
3235 pointer_type_t *pointer_type = &type->pointer;
3237 type = skip_typeref(pointer_type->points_to);
3239 if (type->type == TYPE_FUNCTION) {
3240 function_type = &type->function;
3241 call->expression.datatype = function_type->result_type;
3244 if(function_type == NULL) {
3245 parser_print_error_prefix();
3246 fputs("called object '", stderr);
3247 print_expression(expression);
3248 fputs("' (type ", stderr);
3249 print_type_quoted(orig_type);
3250 fputs(") is not a pointer to a function\n", stderr);
3252 function_type = NULL;
3253 call->expression.datatype = NULL;
3257 /* parse arguments */
3260 if(token.type != ')') {
3261 call_argument_t *last_argument = NULL;
3264 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3266 argument->expression = parse_assignment_expression();
3267 if(last_argument == NULL) {
3268 call->arguments = argument;
3270 last_argument->next = argument;
3272 last_argument = argument;
3274 if(token.type != ',')
3281 if(function_type != NULL) {
3282 function_parameter_t *parameter = function_type->parameters;
3283 call_argument_t *argument = call->arguments;
3284 for( ; parameter != NULL && argument != NULL;
3285 parameter = parameter->next, argument = argument->next) {
3286 type_t *expected_type = parameter->type;
3287 /* TODO report context in error messages */
3288 argument->expression = create_implicit_cast(argument->expression,
3291 /* too few parameters */
3292 if(parameter != NULL) {
3293 parser_print_error_prefix();
3294 fprintf(stderr, "too few arguments to function '");
3295 print_expression(expression);
3296 fprintf(stderr, "'\n");
3297 } else if(argument != NULL) {
3298 /* too many parameters */
3299 if(!function_type->variadic
3300 && !function_type->unspecified_parameters) {
3301 parser_print_error_prefix();
3302 fprintf(stderr, "too many arguments to function '");
3303 print_expression(expression);
3304 fprintf(stderr, "'\n");
3306 /* do default promotion */
3307 for( ; argument != NULL; argument = argument->next) {
3308 type_t *type = argument->expression->base.datatype;
3309 type = skip_typeref(type);
3314 if(is_type_integer(type)) {
3315 type = promote_integer(type);
3316 } else if(type == type_float) {
3320 argument->expression
3321 = create_implicit_cast(argument->expression, type);
3330 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3332 static expression_t *parse_conditional_expression(unsigned precedence,
3333 expression_t *expression)
3337 conditional_expression_t *conditional
3338 = allocate_ast_zero(sizeof(conditional[0]));
3339 conditional->expression.type = EXPR_CONDITIONAL;
3340 conditional->condition = expression;
3343 type_t *condition_type_orig = conditional->condition->base.datatype;
3344 if(condition_type_orig != NULL) {
3345 type_t *condition_type = skip_typeref(condition_type_orig);
3346 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3347 type_error("expected a scalar type",
3348 expression->base.source_position, condition_type_orig);
3352 expression_t *const t_expr = parse_expression();
3353 conditional->true_expression = t_expr;
3355 expression_t *const f_expr = parse_sub_expression(precedence);
3356 conditional->false_expression = f_expr;
3358 type_t *const true_type = t_expr->base.datatype;
3359 if(true_type == NULL)
3360 return (expression_t*) conditional;
3361 type_t *const false_type = f_expr->base.datatype;
3362 if(false_type == NULL)
3363 return (expression_t*) conditional;
3365 type_t *const skipped_true_type = skip_typeref(true_type);
3366 type_t *const skipped_false_type = skip_typeref(false_type);
3369 if (skipped_true_type == skipped_false_type) {
3370 conditional->expression.datatype = skipped_true_type;
3371 } else if (is_type_arithmetic(skipped_true_type) &&
3372 is_type_arithmetic(skipped_false_type)) {
3373 type_t *const result = semantic_arithmetic(skipped_true_type,
3374 skipped_false_type);
3375 conditional->true_expression = create_implicit_cast(t_expr, result);
3376 conditional->false_expression = create_implicit_cast(f_expr, result);
3377 conditional->expression.datatype = result;
3378 } else if (skipped_true_type->type == TYPE_POINTER &&
3379 skipped_false_type->type == TYPE_POINTER &&
3380 true /* TODO compatible points_to types */) {
3382 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3383 skipped_false_type->type == TYPE_POINTER)
3384 || (is_null_ptr_const(skipped_false_type) &&
3385 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3387 } else if(/* 1 is pointer to object type, other is void* */ false) {
3390 type_error_incompatible("while parsing conditional",
3391 expression->base.source_position, true_type,
3392 skipped_false_type);
3395 return (expression_t*) conditional;
3398 static expression_t *parse_extension(unsigned precedence)
3400 eat(T___extension__);
3402 /* TODO enable extensions */
3404 return parse_sub_expression(precedence);
3407 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3409 eat(T___builtin_classify_type);
3411 classify_type_expression_t *const classify_type_expr =
3412 allocate_ast_zero(sizeof(classify_type_expr[0]));
3413 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3414 classify_type_expr->expression.datatype = type_int;
3417 expression_t *const expression = parse_sub_expression(precedence);
3419 classify_type_expr->type_expression = expression;
3421 return (expression_t*)classify_type_expr;
3424 static void semantic_incdec(unary_expression_t *expression)
3426 type_t *orig_type = expression->value->base.datatype;
3427 if(orig_type == NULL)
3430 type_t *type = skip_typeref(orig_type);
3431 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3432 /* TODO: improve error message */
3433 parser_print_error_prefix();
3434 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3438 expression->expression.datatype = orig_type;
3441 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3443 type_t *orig_type = expression->value->base.datatype;
3444 if(orig_type == NULL)
3447 type_t *type = skip_typeref(orig_type);
3448 if(!is_type_arithmetic(type)) {
3449 /* TODO: improve error message */
3450 parser_print_error_prefix();
3451 fprintf(stderr, "operation needs an arithmetic type\n");
3455 expression->expression.datatype = orig_type;
3458 static void semantic_unexpr_scalar(unary_expression_t *expression)
3460 type_t *orig_type = expression->value->base.datatype;
3461 if(orig_type == NULL)
3464 type_t *type = skip_typeref(orig_type);
3465 if (!is_type_scalar(type)) {
3466 parse_error("operand of ! must be of scalar type\n");
3470 expression->expression.datatype = orig_type;
3473 static void semantic_unexpr_integer(unary_expression_t *expression)
3475 type_t *orig_type = expression->value->base.datatype;
3476 if(orig_type == NULL)
3479 type_t *type = skip_typeref(orig_type);
3480 if (!is_type_integer(type)) {
3481 parse_error("operand of ~ must be of integer type\n");
3485 expression->expression.datatype = orig_type;
3488 static void semantic_dereference(unary_expression_t *expression)
3490 type_t *orig_type = expression->value->base.datatype;
3491 if(orig_type == NULL)
3494 type_t *type = skip_typeref(orig_type);
3495 if(!is_type_pointer(type)) {
3496 parser_print_error_prefix();
3497 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3498 print_type_quoted(orig_type);
3499 fputs(" given.\n", stderr);
3503 pointer_type_t *pointer_type = &type->pointer;
3504 type_t *result_type = pointer_type->points_to;
3506 result_type = automatic_type_conversion(result_type);
3507 expression->expression.datatype = result_type;
3510 static void semantic_take_addr(unary_expression_t *expression)
3512 expression_t *value = expression->value;
3513 value->base.datatype = revert_automatic_type_conversion(value);
3515 type_t *orig_type = value->base.datatype;
3516 if(orig_type == NULL)
3519 if(value->type == EXPR_REFERENCE) {
3520 reference_expression_t *reference = (reference_expression_t*) value;
3521 declaration_t *declaration = reference->declaration;
3522 if(declaration != NULL) {
3523 declaration->address_taken = 1;
3527 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3530 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3531 static expression_t *parse_##unexpression_type(unsigned precedence) \
3535 unary_expression_t *unary_expression \
3536 = allocate_ast_zero(sizeof(unary_expression[0])); \
3537 unary_expression->expression.type = EXPR_UNARY; \
3538 unary_expression->type = unexpression_type; \
3539 unary_expression->value = parse_sub_expression(precedence); \
3541 sfunc(unary_expression); \
3543 return (expression_t*) unary_expression; \
3546 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3547 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3548 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3549 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3550 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3551 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3552 semantic_unexpr_integer)
3553 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3555 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3558 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3560 static expression_t *parse_##unexpression_type(unsigned precedence, \
3561 expression_t *left) \
3563 (void) precedence; \
3566 unary_expression_t *unary_expression \
3567 = allocate_ast_zero(sizeof(unary_expression[0])); \
3568 unary_expression->expression.type = EXPR_UNARY; \
3569 unary_expression->type = unexpression_type; \
3570 unary_expression->value = left; \
3572 sfunc(unary_expression); \
3574 return (expression_t*) unary_expression; \
3577 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3579 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3582 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3584 /* TODO: handle complex + imaginary types */
3586 /* § 6.3.1.8 Usual arithmetic conversions */
3587 if(type_left == type_long_double || type_right == type_long_double) {
3588 return type_long_double;
3589 } else if(type_left == type_double || type_right == type_double) {
3591 } else if(type_left == type_float || type_right == type_float) {
3595 type_right = promote_integer(type_right);
3596 type_left = promote_integer(type_left);
3598 if(type_left == type_right)
3601 bool signed_left = is_type_signed(type_left);
3602 bool signed_right = is_type_signed(type_right);
3603 int rank_left = get_rank(type_left);
3604 int rank_right = get_rank(type_right);
3605 if(rank_left < rank_right) {
3606 if(signed_left == signed_right || !signed_right) {
3612 if(signed_left == signed_right || !signed_left) {
3620 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3622 expression_t *left = expression->left;
3623 expression_t *right = expression->right;
3624 type_t *orig_type_left = left->base.datatype;
3625 type_t *orig_type_right = right->base.datatype;
3627 if(orig_type_left == NULL || orig_type_right == NULL)
3630 type_t *type_left = skip_typeref(orig_type_left);
3631 type_t *type_right = skip_typeref(orig_type_right);
3633 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3634 /* TODO: improve error message */
3635 parser_print_error_prefix();
3636 fprintf(stderr, "operation needs arithmetic types\n");
3640 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3641 expression->left = create_implicit_cast(left, arithmetic_type);
3642 expression->right = create_implicit_cast(right, arithmetic_type);
3643 expression->expression.datatype = arithmetic_type;
3646 static void semantic_shift_op(binary_expression_t *expression)
3648 expression_t *left = expression->left;
3649 expression_t *right = expression->right;
3650 type_t *orig_type_left = left->base.datatype;
3651 type_t *orig_type_right = right->base.datatype;
3653 if(orig_type_left == NULL || orig_type_right == NULL)
3656 type_t *type_left = skip_typeref(orig_type_left);
3657 type_t *type_right = skip_typeref(orig_type_right);
3659 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3660 /* TODO: improve error message */
3661 parser_print_error_prefix();
3662 fprintf(stderr, "operation needs integer types\n");
3666 type_left = promote_integer(type_left);
3667 type_right = promote_integer(type_right);
3669 expression->left = create_implicit_cast(left, type_left);
3670 expression->right = create_implicit_cast(right, type_right);
3671 expression->expression.datatype = type_left;
3674 static void semantic_add(binary_expression_t *expression)
3676 expression_t *left = expression->left;
3677 expression_t *right = expression->right;
3678 type_t *orig_type_left = left->base.datatype;
3679 type_t *orig_type_right = right->base.datatype;
3681 if(orig_type_left == NULL || orig_type_right == NULL)
3684 type_t *type_left = skip_typeref(orig_type_left);
3685 type_t *type_right = skip_typeref(orig_type_right);
3688 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3689 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3690 expression->left = create_implicit_cast(left, arithmetic_type);
3691 expression->right = create_implicit_cast(right, arithmetic_type);
3692 expression->expression.datatype = arithmetic_type;
3694 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3695 expression->expression.datatype = type_left;
3696 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3697 expression->expression.datatype = type_right;
3699 parser_print_error_prefix();
3700 fprintf(stderr, "invalid operands to binary + (");
3701 print_type_quoted(orig_type_left);
3702 fprintf(stderr, ", ");
3703 print_type_quoted(orig_type_right);
3704 fprintf(stderr, ")\n");
3708 static void semantic_sub(binary_expression_t *expression)
3710 expression_t *left = expression->left;
3711 expression_t *right = expression->right;
3712 type_t *orig_type_left = left->base.datatype;
3713 type_t *orig_type_right = right->base.datatype;
3715 if(orig_type_left == NULL || orig_type_right == NULL)
3718 type_t *type_left = skip_typeref(orig_type_left);
3719 type_t *type_right = skip_typeref(orig_type_right);
3722 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3723 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3724 expression->left = create_implicit_cast(left, arithmetic_type);
3725 expression->right = create_implicit_cast(right, arithmetic_type);
3726 expression->expression.datatype = arithmetic_type;
3728 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3729 expression->expression.datatype = type_left;
3730 } else if(type_left->type == TYPE_POINTER &&
3731 type_right->type == TYPE_POINTER) {
3732 if(!pointers_compatible(type_left, type_right)) {
3733 parser_print_error_prefix();
3734 fprintf(stderr, "pointers to incompatible objects to binary - (");
3735 print_type_quoted(orig_type_left);
3736 fprintf(stderr, ", ");
3737 print_type_quoted(orig_type_right);
3738 fprintf(stderr, ")\n");
3740 expression->expression.datatype = type_ptrdiff_t;
3743 parser_print_error_prefix();
3744 fprintf(stderr, "invalid operands to binary - (");
3745 print_type_quoted(orig_type_left);
3746 fprintf(stderr, ", ");
3747 print_type_quoted(orig_type_right);
3748 fprintf(stderr, ")\n");
3752 static void semantic_comparison(binary_expression_t *expression)
3754 expression_t *left = expression->left;
3755 expression_t *right = expression->right;
3756 type_t *orig_type_left = left->base.datatype;
3757 type_t *orig_type_right = right->base.datatype;
3759 if(orig_type_left == NULL || orig_type_right == NULL)
3762 type_t *type_left = skip_typeref(orig_type_left);
3763 type_t *type_right = skip_typeref(orig_type_right);
3765 /* TODO non-arithmetic types */
3766 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3767 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3768 expression->left = create_implicit_cast(left, arithmetic_type);
3769 expression->right = create_implicit_cast(right, arithmetic_type);
3770 expression->expression.datatype = arithmetic_type;
3771 } else if (type_left->type == TYPE_POINTER &&
3772 type_right->type == TYPE_POINTER) {
3773 /* TODO check compatibility */
3774 } else if (type_left->type == TYPE_POINTER) {
3775 expression->right = create_implicit_cast(right, type_left);
3776 } else if (type_right->type == TYPE_POINTER) {
3777 expression->left = create_implicit_cast(left, type_right);
3779 type_error_incompatible("invalid operands in comparison",
3780 token.source_position, type_left, type_right);
3782 expression->expression.datatype = type_int;
3785 static void semantic_arithmetic_assign(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_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3799 /* TODO: improve error message */
3800 parser_print_error_prefix();
3801 fprintf(stderr, "operation needs arithmetic types\n");
3805 /* combined instructions are tricky. We can't create an implicit cast on
3806 * the left side, because we need the uncasted form for the store.
3807 * The ast2firm pass has to know that left_type must be right_type
3808 * for the arithmeitc operation and create a cast by itself */
3809 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3810 expression->right = create_implicit_cast(right, arithmetic_type);
3811 expression->expression.datatype = type_left;
3814 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3816 expression_t *left = expression->left;
3817 expression_t *right = expression->right;
3818 type_t *orig_type_left = left->base.datatype;
3819 type_t *orig_type_right = right->base.datatype;
3821 if(orig_type_left == NULL || orig_type_right == NULL)
3824 type_t *type_left = skip_typeref(orig_type_left);
3825 type_t *type_right = skip_typeref(orig_type_right);
3827 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3828 /* combined instructions are tricky. We can't create an implicit cast on
3829 * the left side, because we need the uncasted form for the store.
3830 * The ast2firm pass has to know that left_type must be right_type
3831 * for the arithmeitc operation and create a cast by itself */
3832 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3833 expression->right = create_implicit_cast(right, arithmetic_type);
3834 expression->expression.datatype = type_left;
3835 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3836 expression->expression.datatype = type_left;
3838 parser_print_error_prefix();
3839 fputs("Incompatible types ", stderr);
3840 print_type_quoted(orig_type_left);
3841 fputs(" and ", stderr);
3842 print_type_quoted(orig_type_right);
3843 fputs(" in assignment\n", stderr);
3848 static void semantic_logical_op(binary_expression_t *expression)
3850 expression_t *left = expression->left;
3851 expression_t *right = expression->right;
3852 type_t *orig_type_left = left->base.datatype;
3853 type_t *orig_type_right = right->base.datatype;
3855 if(orig_type_left == NULL || orig_type_right == NULL)
3858 type_t *type_left = skip_typeref(orig_type_left);
3859 type_t *type_right = skip_typeref(orig_type_right);
3861 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3862 /* TODO: improve error message */
3863 parser_print_error_prefix();
3864 fprintf(stderr, "operation needs scalar types\n");
3868 expression->expression.datatype = type_int;
3871 static bool has_const_fields(type_t *type)
3878 static void semantic_binexpr_assign(binary_expression_t *expression)
3880 expression_t *left = expression->left;
3881 type_t *orig_type_left = left->base.datatype;
3883 if(orig_type_left == NULL)
3886 type_t *type_left = revert_automatic_type_conversion(left);
3887 type_left = skip_typeref(orig_type_left);
3889 /* must be a modifiable lvalue */
3890 if (type_left->type == TYPE_ARRAY) {
3891 parser_print_error_prefix();
3892 fprintf(stderr, "Cannot assign to arrays ('");
3893 print_expression(left);
3894 fprintf(stderr, "')\n");
3897 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3898 parser_print_error_prefix();
3899 fprintf(stderr, "assignment to readonly location '");
3900 print_expression(left);
3901 fprintf(stderr, "' (type ");
3902 print_type_quoted(orig_type_left);
3903 fprintf(stderr, ")\n");
3906 if(is_type_incomplete(type_left)) {
3907 parser_print_error_prefix();
3908 fprintf(stderr, "left-hand side of assignment '");
3909 print_expression(left);
3910 fprintf(stderr, "' has incomplete type ");
3911 print_type_quoted(orig_type_left);
3912 fprintf(stderr, "\n");
3915 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3916 parser_print_error_prefix();
3917 fprintf(stderr, "can't assign to '");
3918 print_expression(left);
3919 fprintf(stderr, "' because compound type ");
3920 print_type_quoted(orig_type_left);
3921 fprintf(stderr, " has readonly fields\n");
3925 semantic_assign(orig_type_left, &expression->right, "assignment");
3927 expression->expression.datatype = orig_type_left;
3930 static void semantic_comma(binary_expression_t *expression)
3932 expression->expression.datatype = expression->right->base.datatype;
3935 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3936 static expression_t *parse_##binexpression_type(unsigned precedence, \
3937 expression_t *left) \
3941 expression_t *right = parse_sub_expression(precedence + lr); \
3943 binary_expression_t *binexpr \
3944 = allocate_ast_zero(sizeof(binexpr[0])); \
3945 binexpr->expression.type = EXPR_BINARY; \
3946 binexpr->type = binexpression_type; \
3947 binexpr->left = left; \
3948 binexpr->right = right; \
3951 return (expression_t*) binexpr; \
3954 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3955 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3956 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3957 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3958 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3959 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3960 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3961 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3962 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3963 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3964 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3965 semantic_comparison, 1)
3966 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3967 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3968 semantic_comparison, 1)
3969 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3970 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3971 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3972 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3973 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3974 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3975 semantic_shift_op, 1)
3976 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3977 semantic_shift_op, 1)
3978 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3979 semantic_arithmetic_addsubb_assign, 0)
3980 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3981 semantic_arithmetic_addsubb_assign, 0)
3982 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3983 semantic_arithmetic_assign, 0)
3984 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3985 semantic_arithmetic_assign, 0)
3986 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3987 semantic_arithmetic_assign, 0)
3988 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3989 semantic_arithmetic_assign, 0)
3990 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3991 semantic_arithmetic_assign, 0)
3992 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3993 semantic_arithmetic_assign, 0)
3994 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3995 semantic_arithmetic_assign, 0)
3996 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3997 semantic_arithmetic_assign, 0)
3999 static expression_t *parse_sub_expression(unsigned precedence)
4001 if(token.type < 0) {
4002 return expected_expression_error();
4005 expression_parser_function_t *parser
4006 = &expression_parsers[token.type];
4007 source_position_t source_position = token.source_position;
4010 if(parser->parser != NULL) {
4011 left = parser->parser(parser->precedence);
4013 left = parse_primary_expression();
4015 assert(left != NULL);
4016 left->base.source_position = source_position;
4019 if(token.type < 0) {
4020 return expected_expression_error();
4023 parser = &expression_parsers[token.type];
4024 if(parser->infix_parser == NULL)
4026 if(parser->infix_precedence < precedence)
4029 left = parser->infix_parser(parser->infix_precedence, left);
4031 assert(left != NULL);
4032 assert(left->type != EXPR_UNKNOWN);
4033 left->base.source_position = source_position;
4039 static expression_t *parse_expression(void)
4041 return parse_sub_expression(1);
4046 static void register_expression_parser(parse_expression_function parser,
4047 int token_type, unsigned precedence)
4049 expression_parser_function_t *entry = &expression_parsers[token_type];
4051 if(entry->parser != NULL) {
4052 fprintf(stderr, "for token ");
4053 print_token_type(stderr, (token_type_t) token_type);
4054 fprintf(stderr, "\n");
4055 panic("trying to register multiple expression parsers for a token");
4057 entry->parser = parser;
4058 entry->precedence = precedence;
4061 static void register_expression_infix_parser(
4062 parse_expression_infix_function parser, int token_type,
4063 unsigned precedence)
4065 expression_parser_function_t *entry = &expression_parsers[token_type];
4067 if(entry->infix_parser != NULL) {
4068 fprintf(stderr, "for token ");
4069 print_token_type(stderr, (token_type_t) token_type);
4070 fprintf(stderr, "\n");
4071 panic("trying to register multiple infix expression parsers for a "
4074 entry->infix_parser = parser;
4075 entry->infix_precedence = precedence;
4078 static void init_expression_parsers(void)
4080 memset(&expression_parsers, 0, sizeof(expression_parsers));
4082 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4083 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4084 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4085 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4086 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4087 T_GREATERGREATER, 16);
4088 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4089 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4090 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4091 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4092 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4093 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4094 T_GREATEREQUAL, 14);
4095 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4096 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4097 T_EXCLAMATIONMARKEQUAL, 13);
4098 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4099 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4100 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4101 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4102 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4103 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4104 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4105 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4106 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4107 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4108 T_ASTERISKEQUAL, 2);
4109 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4110 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4112 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4113 T_LESSLESSEQUAL, 2);
4114 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4115 T_GREATERGREATEREQUAL, 2);
4116 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4118 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4120 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4123 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4125 register_expression_infix_parser(parse_array_expression, '[', 30);
4126 register_expression_infix_parser(parse_call_expression, '(', 30);
4127 register_expression_infix_parser(parse_select_expression, '.', 30);
4128 register_expression_infix_parser(parse_select_expression,
4129 T_MINUSGREATER, 30);
4130 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4132 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4135 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4136 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4137 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4138 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4139 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4140 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4141 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4142 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4143 register_expression_parser(parse_sizeof, T_sizeof, 25);
4144 register_expression_parser(parse_extension, T___extension__, 25);
4145 register_expression_parser(parse_builtin_classify_type,
4146 T___builtin_classify_type, 25);
4149 static asm_constraint_t *parse_asm_constraints(void)
4151 asm_constraint_t *result = NULL;
4152 asm_constraint_t *last = NULL;
4154 while(token.type == T_STRING_LITERAL || token.type == '[') {
4155 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4156 memset(constraint, 0, sizeof(constraint[0]));
4158 if(token.type == '[') {
4160 if(token.type != T_IDENTIFIER) {
4161 parse_error_expected("while parsing asm constraint",
4165 constraint->symbol = token.v.symbol;
4170 constraint->constraints = parse_string_literals();
4172 constraint->expression = parse_expression();
4176 last->next = constraint;
4178 result = constraint;
4182 if(token.type != ',')
4190 static asm_clobber_t *parse_asm_clobbers(void)
4192 asm_clobber_t *result = NULL;
4193 asm_clobber_t *last = NULL;
4195 while(token.type == T_STRING_LITERAL) {
4196 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4197 clobber->clobber = parse_string_literals();
4200 last->next = clobber;
4206 if(token.type != ',')
4214 static statement_t *parse_asm_statement(void)
4218 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4219 statement->base.source_position = token.source_position;
4221 asm_statement_t *asm_statement = &statement->asms;
4223 if(token.type == T_volatile) {
4225 asm_statement->is_volatile = true;
4229 asm_statement->asm_text = parse_string_literals();
4231 if(token.type != ':')
4235 asm_statement->inputs = parse_asm_constraints();
4236 if(token.type != ':')
4240 asm_statement->outputs = parse_asm_constraints();
4241 if(token.type != ':')
4245 asm_statement->clobbers = parse_asm_clobbers();
4253 static statement_t *parse_case_statement(void)
4256 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4257 label->statement.type = STATEMENT_CASE_LABEL;
4258 label->statement.source_position = token.source_position;
4260 label->expression = parse_expression();
4263 label->label_statement = parse_statement();
4265 return (statement_t*) label;
4268 static statement_t *parse_default_statement(void)
4272 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4273 label->statement.type = STATEMENT_CASE_LABEL;
4274 label->statement.source_position = token.source_position;
4277 label->label_statement = parse_statement();
4279 return (statement_t*) label;
4282 static declaration_t *get_label(symbol_t *symbol)
4284 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4285 assert(current_function != NULL);
4286 /* if we found a label in the same function, then we already created the
4288 if(candidate != NULL
4289 && candidate->parent_context == ¤t_function->context) {
4293 /* otherwise we need to create a new one */
4294 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4295 declaration->namespc = NAMESPACE_LABEL;
4296 declaration->symbol = symbol;
4298 label_push(declaration);
4303 static statement_t *parse_label_statement(void)
4305 assert(token.type == T_IDENTIFIER);
4306 symbol_t *symbol = token.v.symbol;
4309 declaration_t *label = get_label(symbol);
4311 /* if source position is already set then the label is defined twice,
4312 * otherwise it was just mentioned in a goto so far */
4313 if(label->source_position.input_name != NULL) {
4314 parser_print_error_prefix();
4315 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4316 parser_print_error_prefix_pos(label->source_position);
4317 fprintf(stderr, "previous definition of '%s' was here\n",
4320 label->source_position = token.source_position;
4323 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4325 label_statement->statement.type = STATEMENT_LABEL;
4326 label_statement->statement.source_position = token.source_position;
4327 label_statement->label = label;
4331 if(token.type == '}') {
4332 parse_error("label at end of compound statement");
4333 return (statement_t*) label_statement;
4335 label_statement->label_statement = parse_statement();
4338 return (statement_t*) label_statement;
4341 static statement_t *parse_if(void)
4345 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4346 statement->statement.type = STATEMENT_IF;
4347 statement->statement.source_position = token.source_position;
4350 statement->condition = parse_expression();
4353 statement->true_statement = parse_statement();
4354 if(token.type == T_else) {
4356 statement->false_statement = parse_statement();
4359 return (statement_t*) statement;
4362 static statement_t *parse_switch(void)
4366 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4367 statement->statement.type = STATEMENT_SWITCH;
4368 statement->statement.source_position = token.source_position;
4371 statement->expression = parse_expression();
4373 statement->body = parse_statement();
4375 return (statement_t*) statement;
4378 static statement_t *parse_while(void)
4382 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4383 statement->statement.type = STATEMENT_WHILE;
4384 statement->statement.source_position = token.source_position;
4387 statement->condition = parse_expression();
4389 statement->body = parse_statement();
4391 return (statement_t*) statement;
4394 static statement_t *parse_do(void)
4398 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4399 statement->statement.type = STATEMENT_DO_WHILE;
4400 statement->statement.source_position = token.source_position;
4402 statement->body = parse_statement();
4405 statement->condition = parse_expression();
4409 return (statement_t*) statement;
4412 static statement_t *parse_for(void)
4416 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4417 statement->statement.type = STATEMENT_FOR;
4418 statement->statement.source_position = token.source_position;
4422 int top = environment_top();
4423 context_t *last_context = context;
4424 set_context(&statement->context);
4426 if(token.type != ';') {
4427 if(is_declaration_specifier(&token, false)) {
4428 parse_declaration();
4430 statement->initialisation = parse_expression();
4437 if(token.type != ';') {
4438 statement->condition = parse_expression();
4441 if(token.type != ')') {
4442 statement->step = parse_expression();
4445 statement->body = parse_statement();
4447 assert(context == &statement->context);
4448 set_context(last_context);
4449 environment_pop_to(top);
4451 return (statement_t*) statement;
4454 static statement_t *parse_goto(void)
4458 if(token.type != T_IDENTIFIER) {
4459 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4463 symbol_t *symbol = token.v.symbol;
4466 declaration_t *label = get_label(symbol);
4468 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4470 statement->statement.type = STATEMENT_GOTO;
4471 statement->statement.source_position = token.source_position;
4473 statement->label = label;
4477 return (statement_t*) statement;
4480 static statement_t *parse_continue(void)
4485 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4486 statement->type = STATEMENT_CONTINUE;
4487 statement->base.source_position = token.source_position;
4492 static statement_t *parse_break(void)
4497 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4498 statement->type = STATEMENT_BREAK;
4499 statement->base.source_position = token.source_position;
4504 static statement_t *parse_return(void)
4508 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4510 statement->statement.type = STATEMENT_RETURN;
4511 statement->statement.source_position = token.source_position;
4513 assert(current_function->type->type == TYPE_FUNCTION);
4514 function_type_t *function_type = ¤t_function->type->function;
4515 type_t *return_type = function_type->result_type;
4517 expression_t *return_value = NULL;
4518 if(token.type != ';') {
4519 return_value = parse_expression();
4523 if(return_type == NULL)
4524 return (statement_t*) statement;
4526 return_type = skip_typeref(return_type);
4528 if(return_value != NULL) {
4529 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4531 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4532 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4533 parse_warning("'return' with a value, in function returning void");
4534 return_value = NULL;
4536 if(return_type != NULL) {
4537 semantic_assign(return_type, &return_value, "'return'");
4541 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4542 parse_warning("'return' without value, in function returning "
4546 statement->return_value = return_value;
4548 return (statement_t*) statement;
4551 static statement_t *parse_declaration_statement(void)
4553 declaration_t *before = last_declaration;
4555 declaration_statement_t *statement
4556 = allocate_ast_zero(sizeof(statement[0]));
4557 statement->statement.type = STATEMENT_DECLARATION;
4558 statement->statement.source_position = token.source_position;
4560 declaration_specifiers_t specifiers;
4561 memset(&specifiers, 0, sizeof(specifiers));
4562 parse_declaration_specifiers(&specifiers);
4564 if(token.type == ';') {
4567 parse_init_declarators(&specifiers);
4570 if(before == NULL) {
4571 statement->declarations_begin = context->declarations;
4573 statement->declarations_begin = before->next;
4575 statement->declarations_end = last_declaration;
4577 return (statement_t*) statement;
4580 static statement_t *parse_expression_statement(void)
4582 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4583 statement->statement.type = STATEMENT_EXPRESSION;
4584 statement->statement.source_position = token.source_position;
4586 statement->expression = parse_expression();
4590 return (statement_t*) statement;
4593 static statement_t *parse_statement(void)
4595 statement_t *statement = NULL;
4597 /* declaration or statement */
4598 switch(token.type) {
4600 statement = parse_asm_statement();
4604 statement = parse_case_statement();
4608 statement = parse_default_statement();
4612 statement = parse_compound_statement();
4616 statement = parse_if();
4620 statement = parse_switch();
4624 statement = parse_while();
4628 statement = parse_do();
4632 statement = parse_for();
4636 statement = parse_goto();
4640 statement = parse_continue();
4644 statement = parse_break();
4648 statement = parse_return();
4657 if(look_ahead(1)->type == ':') {
4658 statement = parse_label_statement();
4662 if(is_typedef_symbol(token.v.symbol)) {
4663 statement = parse_declaration_statement();
4667 statement = parse_expression_statement();
4670 case T___extension__:
4671 /* this can be a prefix to a declaration or an expression statement */
4672 /* we simply eat it now and parse the rest with tail recursion */
4675 } while(token.type == T___extension__);
4676 statement = parse_statement();
4680 statement = parse_declaration_statement();
4684 statement = parse_expression_statement();
4688 assert(statement == NULL
4689 || statement->base.source_position.input_name != NULL);
4694 static statement_t *parse_compound_statement(void)
4696 compound_statement_t *compound_statement
4697 = allocate_ast_zero(sizeof(compound_statement[0]));
4698 compound_statement->statement.type = STATEMENT_COMPOUND;
4699 compound_statement->statement.source_position = token.source_position;
4703 int top = environment_top();
4704 context_t *last_context = context;
4705 set_context(&compound_statement->context);
4707 statement_t *last_statement = NULL;
4709 while(token.type != '}' && token.type != T_EOF) {
4710 statement_t *statement = parse_statement();
4711 if(statement == NULL)
4714 if(last_statement != NULL) {
4715 last_statement->base.next = statement;
4717 compound_statement->statements = statement;
4720 while(statement->base.next != NULL)
4721 statement = statement->base.next;
4723 last_statement = statement;
4726 if(token.type != '}') {
4727 parser_print_error_prefix_pos(
4728 compound_statement->statement.source_position);
4729 fprintf(stderr, "end of file while looking for closing '}'\n");
4733 assert(context == &compound_statement->context);
4734 set_context(last_context);
4735 environment_pop_to(top);
4737 return (statement_t*) compound_statement;
4740 static translation_unit_t *parse_translation_unit(void)
4742 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4744 assert(global_context == NULL);
4745 global_context = &unit->context;
4747 assert(context == NULL);
4748 set_context(&unit->context);
4750 while(token.type != T_EOF) {
4751 parse_declaration();
4754 assert(context == &unit->context);
4756 last_declaration = NULL;
4758 assert(global_context == &unit->context);
4759 global_context = NULL;
4764 translation_unit_t *parse(void)
4766 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4767 label_stack = NEW_ARR_F(stack_entry_t, 0);
4768 found_error = false;
4770 type_set_output(stderr);
4771 ast_set_output(stderr);
4773 lookahead_bufpos = 0;
4774 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4777 translation_unit_t *unit = parse_translation_unit();
4779 DEL_ARR_F(environment_stack);
4780 DEL_ARR_F(label_stack);
4788 void init_parser(void)
4790 init_expression_parsers();
4791 obstack_init(&temp_obst);
4793 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4794 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4795 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4796 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4797 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4798 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4799 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4800 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4801 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4802 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4805 void exit_parser(void)
4807 obstack_free(&temp_obst, NULL);