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_uint = NULL;
42 static type_t *type_long_double = NULL;
43 static type_t *type_double = NULL;
44 static type_t *type_float = NULL;
45 static type_t *type_const_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_void_ptr = NULL;
49 static type_t *type_size_t = NULL;
50 static type_t *type_ptrdiff_t = NULL;
52 static statement_t *parse_compound_statement(void);
53 static statement_t *parse_statement(void);
55 static expression_t *parse_sub_expression(unsigned precedence);
56 static expression_t *parse_expression(void);
57 static type_t *parse_typename(void);
59 #define STORAGE_CLASSES \
66 #define TYPE_QUALIFIERS \
72 #ifdef PROVIDE_COMPLEX
73 #define COMPLEX_SPECIFIERS \
75 #define IMAGINARY_SPECIFIERS \
78 #define COMPLEX_SPECIFIERS
79 #define IMAGINARY_SPECIFIERS
82 #define TYPE_SPECIFIERS \
100 #define DECLARATION_START \
105 #define TYPENAME_START \
109 static inline void *allocate_ast_zero(size_t size)
111 void *res = allocate_ast(size);
112 memset(res, 0, size);
116 static inline void *allocate_type_zero(size_t size)
118 void *res = obstack_alloc(type_obst, size);
119 memset(res, 0, size);
123 static inline size_t get_initializer_size(initializer_type_t type)
125 static const size_t size[] = {
126 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
127 [INITIALIZER_STRING] = sizeof(initializer_string_t),
128 [INITIALIZER_LIST] = sizeof(initializer_list_t)
130 assert(type < INITIALIZER_COUNT);
131 assert(size[type] != 0);
135 static inline initializer_t *allocate_initializer(initializer_type_t type)
137 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
143 static inline void free_type(void *type)
145 obstack_free(type_obst, type);
149 * returns the top element of the environment stack
151 static inline size_t environment_top(void)
153 return ARR_LEN(environment_stack);
156 static inline size_t label_top(void)
158 return ARR_LEN(label_stack);
163 static inline void next_token(void)
165 token = lookahead_buffer[lookahead_bufpos];
166 lookahead_buffer[lookahead_bufpos] = lexer_token;
169 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
172 print_token(stderr, &token);
173 fprintf(stderr, "\n");
177 static inline const token_t *look_ahead(int num)
179 assert(num > 0 && num <= MAX_LOOKAHEAD);
180 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
181 return & lookahead_buffer[pos];
184 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
186 static void error(void)
189 #ifdef ABORT_ON_ERROR
194 static void parser_print_prefix_pos(const source_position_t source_position)
196 fputs(source_position.input_name, stderr);
198 fprintf(stderr, "%d", source_position.linenr);
202 static void parser_print_error_prefix_pos(
203 const source_position_t source_position)
205 parser_print_prefix_pos(source_position);
206 fputs("error: ", stderr);
210 static void parser_print_error_prefix(void)
212 parser_print_error_prefix_pos(token.source_position);
215 static void parse_error(const char *message)
217 parser_print_error_prefix();
218 fprintf(stderr, "parse error: %s\n", message);
221 static void parser_print_warning_prefix_pos(
222 const source_position_t source_position)
224 parser_print_prefix_pos(source_position);
225 fputs("warning: ", stderr);
228 static void parse_warning_pos(const source_position_t source_position,
229 const char *const message)
231 parser_print_prefix_pos(source_position);
232 fprintf(stderr, "warning: %s\n", message);
235 static void parse_warning(const char *message)
237 parse_warning_pos(token.source_position, message);
240 static void parse_error_expected(const char *message, ...)
245 if(message != NULL) {
246 parser_print_error_prefix();
247 fprintf(stderr, "%s\n", message);
249 parser_print_error_prefix();
250 fputs("Parse error: got ", stderr);
251 print_token(stderr, &token);
252 fputs(", expected ", stderr);
254 va_start(args, message);
255 token_type_t token_type = va_arg(args, token_type_t);
256 while(token_type != 0) {
260 fprintf(stderr, ", ");
262 print_token_type(stderr, token_type);
263 token_type = va_arg(args, token_type_t);
266 fprintf(stderr, "\n");
269 static void print_type_quoted(type_t *type)
276 static void type_error(const char *msg, const source_position_t source_position,
279 parser_print_error_prefix_pos(source_position);
280 fprintf(stderr, "%s, but found type ", msg);
281 print_type_quoted(type);
285 static void type_error_incompatible(const char *msg,
286 const source_position_t source_position, type_t *type1, type_t *type2)
288 parser_print_error_prefix_pos(source_position);
289 fprintf(stderr, "%s, incompatible types: ", msg);
290 print_type_quoted(type1);
291 fprintf(stderr, " - ");
292 print_type_quoted(type2);
293 fprintf(stderr, ")\n");
296 static void eat_block(void)
298 if(token.type == '{')
301 while(token.type != '}') {
302 if(token.type == T_EOF)
304 if(token.type == '{') {
313 static void eat_statement(void)
315 while(token.type != ';') {
316 if(token.type == T_EOF)
318 if(token.type == '}')
320 if(token.type == '{') {
329 static void eat_brace(void)
331 if(token.type == '(')
334 while(token.type != ')') {
335 if(token.type == T_EOF)
337 if(token.type == ')' || token.type == ';' || token.type == '}') {
340 if(token.type == '(') {
344 if(token.type == '{') {
353 #define expect(expected) \
354 if(UNLIKELY(token.type != (expected))) { \
355 parse_error_expected(NULL, (expected), 0); \
361 #define expect_block(expected) \
362 if(UNLIKELY(token.type != (expected))) { \
363 parse_error_expected(NULL, (expected), 0); \
369 #define expect_void(expected) \
370 if(UNLIKELY(token.type != (expected))) { \
371 parse_error_expected(NULL, (expected), 0); \
377 static void set_context(context_t *new_context)
379 context = new_context;
381 last_declaration = new_context->declarations;
382 if(last_declaration != NULL) {
383 while(last_declaration->next != NULL) {
384 last_declaration = last_declaration->next;
390 * called when we find a 2nd declarator for an identifier we already have a
393 static bool is_compatible_declaration (declaration_t *declaration,
394 declaration_t *previous)
396 /* TODO: not correct yet */
397 return declaration->type == previous->type;
400 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
402 declaration_t *declaration = symbol->declaration;
403 for( ; declaration != NULL; declaration = declaration->symbol_next) {
404 if(declaration->namespc == namespc)
411 static const char *get_namespace_prefix(namespace_t namespc)
414 case NAMESPACE_NORMAL:
416 case NAMESPACE_UNION:
418 case NAMESPACE_STRUCT:
422 case NAMESPACE_LABEL:
425 panic("invalid namespace found");
429 * pushs an environment_entry on the environment stack and links the
430 * corresponding symbol to the new entry
432 static declaration_t *stack_push(stack_entry_t **stack_ptr,
433 declaration_t *declaration,
434 context_t *parent_context)
436 symbol_t *symbol = declaration->symbol;
437 namespace_t namespc = (namespace_t)declaration->namespc;
439 /* a declaration should be only pushed once */
440 assert(declaration->parent_context == NULL);
441 declaration->parent_context = parent_context;
443 declaration_t *previous_declaration = get_declaration(symbol, namespc);
444 assert(declaration != previous_declaration);
445 if(previous_declaration != NULL
446 && previous_declaration->parent_context == context) {
447 if(!is_compatible_declaration(declaration, previous_declaration)) {
448 parser_print_error_prefix_pos(declaration->source_position);
449 fprintf(stderr, "definition of symbol %s%s with type ",
450 get_namespace_prefix(namespc), symbol->string);
451 print_type_quoted(declaration->type);
453 parser_print_error_prefix_pos(
454 previous_declaration->source_position);
455 fprintf(stderr, "is incompatible with previous declaration "
457 print_type_quoted(previous_declaration->type);
460 const storage_class_t old_storage = previous_declaration->storage_class;
461 const storage_class_t new_storage = declaration->storage_class;
462 if (current_function == NULL) {
463 if (old_storage != STORAGE_CLASS_STATIC &&
464 new_storage == STORAGE_CLASS_STATIC) {
465 parser_print_error_prefix_pos(declaration->source_position);
467 "static declaration of '%s' follows non-static declaration\n",
469 parser_print_error_prefix_pos(previous_declaration->source_position);
470 fprintf(stderr, "previous declaration of '%s' was here\n",
473 if (old_storage == STORAGE_CLASS_EXTERN) {
474 if (new_storage == STORAGE_CLASS_NONE) {
475 previous_declaration->storage_class = STORAGE_CLASS_NONE;
478 parser_print_warning_prefix_pos(declaration->source_position);
479 fprintf(stderr, "redundant declaration for '%s'\n",
481 parser_print_warning_prefix_pos(previous_declaration->source_position);
482 fprintf(stderr, "previous declaration of '%s' was here\n",
487 if (old_storage == STORAGE_CLASS_EXTERN &&
488 new_storage == STORAGE_CLASS_EXTERN) {
489 parser_print_warning_prefix_pos(declaration->source_position);
490 fprintf(stderr, "redundant extern declaration for '%s'\n",
492 parser_print_warning_prefix_pos(previous_declaration->source_position);
493 fprintf(stderr, "previous declaration of '%s' was here\n",
496 parser_print_error_prefix_pos(declaration->source_position);
497 if (old_storage == new_storage) {
498 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
500 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
502 parser_print_error_prefix_pos(previous_declaration->source_position);
503 fprintf(stderr, "previous declaration of '%s' was here\n",
508 return previous_declaration;
511 /* remember old declaration */
513 entry.symbol = symbol;
514 entry.old_declaration = symbol->declaration;
515 entry.namespc = namespc;
516 ARR_APP1(stack_entry_t, *stack_ptr, entry);
518 /* replace/add declaration into declaration list of the symbol */
519 if(symbol->declaration == NULL) {
520 symbol->declaration = declaration;
522 declaration_t *iter_last = NULL;
523 declaration_t *iter = symbol->declaration;
524 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
525 /* replace an entry? */
526 if(iter->namespc == namespc) {
527 if(iter_last == NULL) {
528 symbol->declaration = declaration;
530 iter_last->symbol_next = declaration;
532 declaration->symbol_next = iter->symbol_next;
537 assert(iter_last->symbol_next == NULL);
538 iter_last->symbol_next = declaration;
545 static declaration_t *environment_push(declaration_t *declaration)
547 assert(declaration->source_position.input_name != NULL);
548 return stack_push(&environment_stack, declaration, context);
551 static declaration_t *label_push(declaration_t *declaration)
553 return stack_push(&label_stack, declaration, ¤t_function->context);
557 * pops symbols from the environment stack until @p new_top is the top element
559 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
561 stack_entry_t *stack = *stack_ptr;
562 size_t top = ARR_LEN(stack);
565 assert(new_top <= top);
569 for(i = top; i > new_top; --i) {
570 stack_entry_t *entry = & stack[i - 1];
572 declaration_t *old_declaration = entry->old_declaration;
573 symbol_t *symbol = entry->symbol;
574 namespace_t namespc = (namespace_t)entry->namespc;
576 /* replace/remove declaration */
577 declaration_t *declaration = symbol->declaration;
578 assert(declaration != NULL);
579 if(declaration->namespc == namespc) {
580 if(old_declaration == NULL) {
581 symbol->declaration = declaration->symbol_next;
583 symbol->declaration = old_declaration;
586 declaration_t *iter_last = declaration;
587 declaration_t *iter = declaration->symbol_next;
588 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
589 /* replace an entry? */
590 if(iter->namespc == namespc) {
591 assert(iter_last != NULL);
592 iter_last->symbol_next = old_declaration;
593 old_declaration->symbol_next = iter->symbol_next;
597 assert(iter != NULL);
601 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
604 static void environment_pop_to(size_t new_top)
606 stack_pop_to(&environment_stack, new_top);
609 static void label_pop_to(size_t new_top)
611 stack_pop_to(&label_stack, new_top);
615 static int get_rank(const type_t *type)
617 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
618 * and esp. footnote 108). However we can't fold constants (yet), so we
619 * can't decide wether unsigned int is possible, while int always works.
620 * (unsigned int would be preferable when possible... for stuff like
621 * struct { enum { ... } bla : 4; } ) */
622 if(type->type == TYPE_ENUM)
623 return ATOMIC_TYPE_INT;
625 assert(type->type == TYPE_ATOMIC);
626 atomic_type_t *atomic_type = (atomic_type_t*) type;
627 atomic_type_type_t atype = atomic_type->atype;
631 static type_t *promote_integer(type_t *type)
633 if(get_rank(type) < ATOMIC_TYPE_INT)
639 static expression_t *create_cast_expression(expression_t *expression,
642 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
644 cast->expression.type = EXPR_UNARY;
645 cast->type = UNEXPR_CAST;
646 cast->value = expression;
647 cast->expression.datatype = dest_type;
649 return (expression_t*) cast;
652 static bool is_null_expression(const expression_t *const expr)
654 if (expr->type != EXPR_CONST) return false;
656 type_t *const type = skip_typeref(expr->datatype);
657 if (!is_type_integer(type)) return false;
659 const const_t *const const_expr = (const const_t*)expr;
660 return const_expr->v.int_value == 0;
663 static expression_t *create_implicit_cast(expression_t *expression,
666 type_t *source_type = expression->datatype;
668 if(source_type == NULL)
671 source_type = skip_typeref(source_type);
672 dest_type = skip_typeref(dest_type);
674 if(source_type == dest_type)
677 switch (dest_type->type) {
679 /* TODO warning for implicitly converting to enum */
681 if (source_type->type != TYPE_ATOMIC &&
682 source_type->type != TYPE_ENUM) {
683 panic("casting of non-atomic types not implemented yet");
686 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
687 type_error_incompatible("can't cast types",
688 expression->source_position,
689 source_type, dest_type);
693 return create_cast_expression(expression, dest_type);
696 switch (source_type->type) {
698 if (is_null_expression(expression)) {
699 return create_cast_expression(expression, dest_type);
704 if (pointers_compatible(source_type, dest_type)) {
705 return create_cast_expression(expression, dest_type);
710 array_type_t *array_type = (array_type_t*) source_type;
711 pointer_type_t *pointer_type
712 = (pointer_type_t*) dest_type;
713 if (types_compatible(array_type->element_type,
714 pointer_type->points_to)) {
715 return create_cast_expression(expression, dest_type);
721 panic("casting of non-atomic types not implemented yet");
724 type_error_incompatible("can't implicitly cast types",
725 expression->source_position,
726 source_type, dest_type);
730 panic("casting of non-atomic types not implemented yet");
734 static bool is_atomic_type(const type_t *type, atomic_type_type_t atype)
736 if(type->type != TYPE_ATOMIC)
738 const atomic_type_t *atomic_type = (const atomic_type_t*) type;
740 return atomic_type->atype == atype;
743 static bool is_pointer(const type_t *type)
745 return type->type == TYPE_POINTER;
748 static bool is_compound_type(const type_t *type)
750 return type->type == TYPE_COMPOUND_STRUCT
751 || type->type == TYPE_COMPOUND_UNION;
754 /** Implements the rules from § 6.5.16.1 */
755 static void semantic_assign(type_t *orig_type_left, expression_t **right,
758 type_t *orig_type_right = (*right)->datatype;
760 if(orig_type_right == NULL)
763 type_t *const type_left = skip_typeref(orig_type_left);
764 type_t *const type_right = skip_typeref(orig_type_right);
766 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
767 (is_pointer(type_left) && is_null_expression(*right)) ||
768 (is_atomic_type(type_left, ATOMIC_TYPE_BOOL)
769 && is_pointer(type_right))) {
770 *right = create_implicit_cast(*right, type_left);
774 if (is_pointer(type_left) && is_pointer(type_right)) {
775 pointer_type_t *pointer_type_left = (pointer_type_t*) type_left;
776 pointer_type_t *pointer_type_right = (pointer_type_t*) type_right;
777 type_t *points_to_left = pointer_type_left->points_to;
778 type_t *points_to_right = pointer_type_right->points_to;
780 points_to_left = skip_typeref(points_to_left);
781 points_to_right = skip_typeref(points_to_right);
783 if(!is_atomic_type(points_to_left, ATOMIC_TYPE_VOID)
784 && !is_atomic_type(points_to_right, ATOMIC_TYPE_VOID)
785 && !types_compatible(points_to_left, points_to_right)) {
786 goto incompatible_assign_types;
789 /* the left type has all qualifiers from the right type */
790 unsigned missing_qualifiers
791 = points_to_right->qualifiers & ~points_to_left->qualifiers;
792 if(missing_qualifiers != 0) {
793 parser_print_error_prefix();
794 fprintf(stderr, "destination type ");
795 print_type_quoted(type_left);
796 fprintf(stderr, " in %s from type ", context);
797 print_type_quoted(type_right);
798 fprintf(stderr, " lacks qualifiers '");
799 print_type_qualifiers(missing_qualifiers);
800 fprintf(stderr, "' in pointed-to type\n");
804 *right = create_implicit_cast(*right, type_left);
808 if (is_compound_type(type_left)
809 && types_compatible(type_left, type_right)) {
810 *right = create_implicit_cast(*right, type_left);
814 incompatible_assign_types:
815 /* TODO: improve error message */
816 parser_print_error_prefix();
817 fprintf(stderr, "incompatible types in %s\n", context);
818 parser_print_error_prefix();
819 print_type_quoted(type_left);
820 fputs(" <- ", stderr);
821 print_type_quoted(type_right);
825 static expression_t *parse_constant_expression(void)
827 /* start parsing at precedence 7 (conditional expression) */
828 return parse_sub_expression(7);
831 static expression_t *parse_assignment_expression(void)
833 /* start parsing at precedence 2 (assignment expression) */
834 return parse_sub_expression(2);
837 typedef struct declaration_specifiers_t declaration_specifiers_t;
838 struct declaration_specifiers_t {
839 storage_class_t storage_class;
844 static void parse_compound_type_entries(void);
845 static declaration_t *parse_declarator(
846 const declaration_specifiers_t *specifiers, type_t *type,
847 bool may_be_abstract);
848 static declaration_t *record_declaration(declaration_t *declaration);
850 static const char *parse_string_literals(void)
852 assert(token.type == T_STRING_LITERAL);
853 const char *result = token.v.string;
857 while(token.type == T_STRING_LITERAL) {
858 result = concat_strings(result, token.v.string);
865 static void parse_attributes(void)
869 case T___attribute__:
877 parse_error("EOF while parsing attribute");
895 if(token.type != T_STRING_LITERAL) {
896 parse_error_expected("while parsing assembler attribute",
901 parse_string_literals();
906 goto attributes_finished;
915 static designator_t *parse_designation(void)
917 if(token.type != '[' && token.type != '.')
920 designator_t *result = NULL;
921 designator_t *last = NULL;
924 designator_t *designator;
927 designator = allocate_ast_zero(sizeof(designator[0]));
929 designator->array_access = parse_constant_expression();
933 designator = allocate_ast_zero(sizeof(designator[0]));
935 if(token.type != T_IDENTIFIER) {
936 parse_error_expected("while parsing designator",
940 designator->symbol = token.v.symbol;
948 assert(designator != NULL);
950 last->next = designator;
959 static initializer_t *initializer_from_string(array_type_t *type,
962 /* TODO: check len vs. size of array type */
965 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
966 initializer->string.string = string;
971 static initializer_t *initializer_from_expression(type_t *type,
972 expression_t *expression)
975 /* TODO check that expression is a constant expression */
977 /* § 6.7.8.14/15 char array may be initialized by string literals */
978 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
979 array_type_t *array_type = (array_type_t*) type;
980 type_t *element_type = array_type->element_type;
982 if(element_type->type == TYPE_ATOMIC) {
983 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
984 atomic_type_type_t atype = atomic_type->atype;
986 /* TODO handle wide strings */
987 if(atype == ATOMIC_TYPE_CHAR
988 || atype == ATOMIC_TYPE_SCHAR
989 || atype == ATOMIC_TYPE_UCHAR) {
991 string_literal_t *literal = (string_literal_t*) expression;
992 return initializer_from_string(array_type, literal->value);
997 semantic_assign(type, &expression, "initializer");
999 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1000 result->value.value = expression;
1005 static initializer_t *parse_sub_initializer(type_t *type,
1006 expression_t *expression,
1007 type_t *expression_type);
1009 static initializer_t *parse_sub_initializer_elem(type_t *type)
1011 if(token.type == '{') {
1012 return parse_sub_initializer(type, NULL, NULL);
1015 expression_t *expression = parse_assignment_expression();
1016 type_t *expression_type = skip_typeref(expression->datatype);
1018 return parse_sub_initializer(type, expression, expression_type);
1021 static bool had_initializer_brace_warning;
1023 static initializer_t *parse_sub_initializer(type_t *type,
1024 expression_t *expression,
1025 type_t *expression_type)
1027 if(is_type_scalar(type)) {
1028 /* there might be extra {} hierarchies */
1029 if(token.type == '{') {
1031 if(!had_initializer_brace_warning) {
1032 parse_warning("braces around scalar initializer");
1033 had_initializer_brace_warning = true;
1035 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1036 if(token.type == ',') {
1038 /* TODO: warn about excessive elements */
1044 if(expression == NULL) {
1045 expression = parse_assignment_expression();
1047 return initializer_from_expression(type, expression);
1050 /* TODO: ignore qualifiers, comparing pointers is probably
1052 if(expression != NULL && expression_type == type) {
1053 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1056 semantic_assign(type, &expression, "initializer");
1058 result->value.value = expression;
1063 bool read_paren = false;
1064 if(token.type == '{') {
1069 /* descend into subtype */
1070 initializer_t *result = NULL;
1071 initializer_t **elems;
1072 if(type->type == TYPE_ARRAY) {
1073 array_type_t *array_type = (array_type_t*) type;
1074 type_t *element_type = array_type->element_type;
1075 element_type = skip_typeref(element_type);
1078 had_initializer_brace_warning = false;
1079 if(expression == NULL) {
1080 sub = parse_sub_initializer_elem(element_type);
1082 sub = parse_sub_initializer(element_type, expression,
1086 /* didn't match the subtypes -> try the parent type */
1088 assert(!read_paren);
1092 elems = NEW_ARR_F(initializer_t*, 0);
1093 ARR_APP1(initializer_t*, elems, sub);
1096 if(token.type == '}')
1099 if(token.type == '}')
1103 = parse_sub_initializer(element_type, NULL, NULL);
1105 /* TODO error, do nicer cleanup */
1106 parse_error("member initializer didn't match");
1110 ARR_APP1(initializer_t*, elems, sub);
1113 assert(type->type == TYPE_COMPOUND_STRUCT
1114 || type->type == TYPE_COMPOUND_UNION);
1115 compound_type_t *compound_type = (compound_type_t*) type;
1116 context_t *context = & compound_type->declaration->context;
1118 declaration_t *first = context->declarations;
1121 type_t *first_type = first->type;
1122 first_type = skip_typeref(first_type);
1125 had_initializer_brace_warning = false;
1126 if(expression == NULL) {
1127 sub = parse_sub_initializer_elem(first_type);
1129 sub = parse_sub_initializer(first_type, expression,expression_type);
1132 /* didn't match the subtypes -> try our parent type */
1134 assert(!read_paren);
1138 elems = NEW_ARR_F(initializer_t*, 0);
1139 ARR_APP1(initializer_t*, elems, sub);
1141 declaration_t *iter = first->next;
1142 for( ; iter != NULL; iter = iter->next) {
1143 if(iter->symbol == NULL)
1145 if(iter->namespc != NAMESPACE_NORMAL)
1148 if(token.type == '}')
1152 type_t *iter_type = iter->type;
1153 iter_type = skip_typeref(iter_type);
1155 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1157 /* TODO error, do nicer cleanup*/
1158 parse_error("member initializer didn't match");
1162 ARR_APP1(initializer_t*, elems, sub);
1166 int len = ARR_LEN(elems);
1167 size_t elems_size = sizeof(initializer_t*) * len;
1169 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1171 init->initializer.type = INITIALIZER_LIST;
1173 memcpy(init->initializers, elems, elems_size);
1176 result = (initializer_t*) init;
1179 if(token.type == ',')
1186 static initializer_t *parse_initializer(type_t *type)
1188 initializer_t *result;
1190 type = skip_typeref(type);
1192 if(token.type != '{') {
1193 expression_t *expression = parse_assignment_expression();
1194 return initializer_from_expression(type, expression);
1197 if(is_type_scalar(type)) {
1201 expression_t *expression = parse_assignment_expression();
1202 result = initializer_from_expression(type, expression);
1204 if(token.type == ',')
1210 result = parse_sub_initializer(type, NULL, NULL);
1218 static declaration_t *parse_compound_type_specifier(bool is_struct)
1226 symbol_t *symbol = NULL;
1227 declaration_t *declaration = NULL;
1229 if (token.type == T___attribute__) {
1234 if(token.type == T_IDENTIFIER) {
1235 symbol = token.v.symbol;
1239 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1241 declaration = get_declaration(symbol, NAMESPACE_UNION);
1243 } else if(token.type != '{') {
1245 parse_error_expected("while parsing struct type specifier",
1246 T_IDENTIFIER, '{', 0);
1248 parse_error_expected("while parsing union type specifier",
1249 T_IDENTIFIER, '{', 0);
1255 if(declaration == NULL) {
1256 declaration = allocate_type_zero(sizeof(declaration[0]));
1259 declaration->namespc = NAMESPACE_STRUCT;
1261 declaration->namespc = NAMESPACE_UNION;
1263 declaration->source_position = token.source_position;
1264 declaration->symbol = symbol;
1265 record_declaration(declaration);
1268 if(token.type == '{') {
1269 if(declaration->init.is_defined) {
1270 assert(symbol != NULL);
1271 parser_print_error_prefix();
1272 fprintf(stderr, "multiple definition of %s %s\n",
1273 is_struct ? "struct" : "union", symbol->string);
1274 declaration->context.declarations = NULL;
1276 declaration->init.is_defined = true;
1278 int top = environment_top();
1279 context_t *last_context = context;
1280 set_context(& declaration->context);
1282 parse_compound_type_entries();
1285 assert(context == & declaration->context);
1286 set_context(last_context);
1287 environment_pop_to(top);
1293 static void parse_enum_entries(enum_type_t *const enum_type)
1297 if(token.type == '}') {
1299 parse_error("empty enum not allowed");
1304 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1306 if(token.type != T_IDENTIFIER) {
1307 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1311 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1312 entry->type = (type_t*) enum_type;
1313 entry->symbol = token.v.symbol;
1314 entry->source_position = token.source_position;
1317 if(token.type == '=') {
1319 entry->init.enum_value = parse_constant_expression();
1324 record_declaration(entry);
1326 if(token.type != ',')
1329 } while(token.type != '}');
1334 static type_t *parse_enum_specifier(void)
1338 declaration_t *declaration;
1341 if(token.type == T_IDENTIFIER) {
1342 symbol = token.v.symbol;
1345 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1346 } else if(token.type != '{') {
1347 parse_error_expected("while parsing enum type specifier",
1348 T_IDENTIFIER, '{', 0);
1355 if(declaration == NULL) {
1356 declaration = allocate_type_zero(sizeof(declaration[0]));
1358 declaration->namespc = NAMESPACE_ENUM;
1359 declaration->source_position = token.source_position;
1360 declaration->symbol = symbol;
1363 enum_type_t *const enum_type = allocate_type_zero(sizeof(enum_type[0]));
1364 enum_type->type.type = TYPE_ENUM;
1365 enum_type->declaration = declaration;
1367 if(token.type == '{') {
1368 if(declaration->init.is_defined) {
1369 parser_print_error_prefix();
1370 fprintf(stderr, "multiple definitions of enum %s\n",
1373 record_declaration(declaration);
1374 declaration->init.is_defined = 1;
1376 parse_enum_entries(enum_type);
1380 return (type_t*) enum_type;
1384 * if a symbol is a typedef to another type, return true
1386 static bool is_typedef_symbol(symbol_t *symbol)
1388 const declaration_t *const declaration =
1389 get_declaration(symbol, NAMESPACE_NORMAL);
1391 declaration != NULL &&
1392 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1395 static type_t *parse_typeof(void)
1403 expression_t *expression = NULL;
1406 switch(token.type) {
1407 case T___extension__:
1408 /* this can be a prefix to a typename or an expression */
1409 /* we simply eat it now. */
1412 } while(token.type == T___extension__);
1416 if(is_typedef_symbol(token.v.symbol)) {
1417 type = parse_typename();
1419 expression = parse_expression();
1420 type = expression->datatype;
1425 type = parse_typename();
1429 expression = parse_expression();
1430 type = expression->datatype;
1436 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1437 typeof->type.type = TYPE_TYPEOF;
1438 typeof->expression = expression;
1439 typeof->typeof_type = type;
1441 return (type_t*) typeof;
1445 SPECIFIER_SIGNED = 1 << 0,
1446 SPECIFIER_UNSIGNED = 1 << 1,
1447 SPECIFIER_LONG = 1 << 2,
1448 SPECIFIER_INT = 1 << 3,
1449 SPECIFIER_DOUBLE = 1 << 4,
1450 SPECIFIER_CHAR = 1 << 5,
1451 SPECIFIER_SHORT = 1 << 6,
1452 SPECIFIER_LONG_LONG = 1 << 7,
1453 SPECIFIER_FLOAT = 1 << 8,
1454 SPECIFIER_BOOL = 1 << 9,
1455 SPECIFIER_VOID = 1 << 10,
1456 #ifdef PROVIDE_COMPLEX
1457 SPECIFIER_COMPLEX = 1 << 11,
1458 SPECIFIER_IMAGINARY = 1 << 12,
1462 static type_t *create_builtin_type(symbol_t *symbol)
1464 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1465 type->type.type = TYPE_BUILTIN;
1466 type->symbol = symbol;
1468 type->real_type = type_int;
1470 return (type_t*) type;
1473 static type_t *get_typedef_type(symbol_t *symbol)
1475 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1476 if(declaration == NULL
1477 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1480 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1481 typedef_type->type.type = TYPE_TYPEDEF;
1482 typedef_type->declaration = declaration;
1484 return (type_t*) typedef_type;
1487 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1489 type_t *type = NULL;
1490 unsigned type_qualifiers = 0;
1491 unsigned type_specifiers = 0;
1495 switch(token.type) {
1498 #define MATCH_STORAGE_CLASS(token, class) \
1500 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1501 parse_error("multiple storage classes in declaration " \
1504 specifiers->storage_class = class; \
1508 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1509 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1510 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1511 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1512 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1514 /* type qualifiers */
1515 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1517 type_qualifiers |= qualifier; \
1521 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1522 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1523 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1525 case T___extension__:
1530 /* type specifiers */
1531 #define MATCH_SPECIFIER(token, specifier, name) \
1534 if(type_specifiers & specifier) { \
1535 parse_error("multiple " name " type specifiers given"); \
1537 type_specifiers |= specifier; \
1541 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1542 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1543 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1544 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1545 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1546 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1547 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1548 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1549 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1550 #ifdef PROVIDE_COMPLEX
1551 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1552 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1556 specifiers->is_inline = true;
1561 if(type_specifiers & SPECIFIER_LONG_LONG) {
1562 parse_error("multiple type specifiers given");
1563 } else if(type_specifiers & SPECIFIER_LONG) {
1564 type_specifiers |= SPECIFIER_LONG_LONG;
1566 type_specifiers |= SPECIFIER_LONG;
1570 /* TODO: if type != NULL for the following rules should issue
1573 type = allocate_type_zero(sizeof(struct compound_type_t));
1575 compound_type_t *compound_type = (compound_type_t*) type;
1576 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1577 compound_type->declaration = parse_compound_type_specifier(true);
1581 type = allocate_type_zero(sizeof(compound_type_t));
1583 compound_type_t *compound_type = (compound_type_t*) type;
1584 compound_type->type.type = TYPE_COMPOUND_UNION;
1585 compound_type->declaration = parse_compound_type_specifier(false);
1589 type = parse_enum_specifier();
1592 type = parse_typeof();
1594 case T___builtin_va_list:
1595 type = create_builtin_type(token.v.symbol);
1599 case T___attribute__:
1604 case T_IDENTIFIER: {
1605 type_t *typedef_type = get_typedef_type(token.v.symbol);
1607 if(typedef_type == NULL)
1608 goto finish_specifiers;
1611 type = typedef_type;
1615 /* function specifier */
1617 goto finish_specifiers;
1624 atomic_type_type_t atomic_type;
1626 /* match valid basic types */
1627 switch(type_specifiers) {
1628 case SPECIFIER_VOID:
1629 atomic_type = ATOMIC_TYPE_VOID;
1631 case SPECIFIER_CHAR:
1632 atomic_type = ATOMIC_TYPE_CHAR;
1634 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1635 atomic_type = ATOMIC_TYPE_SCHAR;
1637 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1638 atomic_type = ATOMIC_TYPE_UCHAR;
1640 case SPECIFIER_SHORT:
1641 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1642 case SPECIFIER_SHORT | SPECIFIER_INT:
1643 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1644 atomic_type = ATOMIC_TYPE_SHORT;
1646 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1647 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1648 atomic_type = ATOMIC_TYPE_USHORT;
1651 case SPECIFIER_SIGNED:
1652 case SPECIFIER_SIGNED | SPECIFIER_INT:
1653 atomic_type = ATOMIC_TYPE_INT;
1655 case SPECIFIER_UNSIGNED:
1656 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1657 atomic_type = ATOMIC_TYPE_UINT;
1659 case SPECIFIER_LONG:
1660 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1661 case SPECIFIER_LONG | SPECIFIER_INT:
1662 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1663 atomic_type = ATOMIC_TYPE_LONG;
1665 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1666 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1667 atomic_type = ATOMIC_TYPE_ULONG;
1669 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1670 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1671 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1672 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1674 atomic_type = ATOMIC_TYPE_LONGLONG;
1676 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1677 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1679 atomic_type = ATOMIC_TYPE_ULONGLONG;
1681 case SPECIFIER_FLOAT:
1682 atomic_type = ATOMIC_TYPE_FLOAT;
1684 case SPECIFIER_DOUBLE:
1685 atomic_type = ATOMIC_TYPE_DOUBLE;
1687 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1688 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1690 case SPECIFIER_BOOL:
1691 atomic_type = ATOMIC_TYPE_BOOL;
1693 #ifdef PROVIDE_COMPLEX
1694 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1695 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1697 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1698 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1700 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1701 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1703 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1704 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1706 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1707 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1709 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1710 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1714 /* invalid specifier combination, give an error message */
1715 if(type_specifiers == 0) {
1717 parse_warning("no type specifiers in declaration (using int)");
1718 atomic_type = ATOMIC_TYPE_INT;
1721 parse_error("no type specifiers given in declaration");
1723 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1724 (type_specifiers & SPECIFIER_UNSIGNED)) {
1725 parse_error("signed and unsigned specifiers gives");
1726 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1727 parse_error("only integer types can be signed or unsigned");
1729 parse_error("multiple datatypes in declaration");
1731 atomic_type = ATOMIC_TYPE_INVALID;
1734 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1735 atype->type.type = TYPE_ATOMIC;
1736 atype->atype = atomic_type;
1739 type = (type_t*) atype;
1741 if(type_specifiers != 0) {
1742 parse_error("multiple datatypes in declaration");
1746 type->qualifiers = (type_qualifier_t)type_qualifiers;
1748 type_t *result = typehash_insert(type);
1749 if(newtype && result != (type_t*) type) {
1753 specifiers->type = result;
1756 static type_qualifiers_t parse_type_qualifiers(void)
1758 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1761 switch(token.type) {
1762 /* type qualifiers */
1763 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1764 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1765 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1768 return type_qualifiers;
1773 static void parse_identifier_list(void)
1776 if(token.type != T_IDENTIFIER) {
1777 parse_error_expected("while parsing parameter identifier list",
1782 if(token.type != ',')
1788 static declaration_t *parse_parameter(void)
1790 declaration_specifiers_t specifiers;
1791 memset(&specifiers, 0, sizeof(specifiers));
1793 parse_declaration_specifiers(&specifiers);
1795 declaration_t *declaration
1796 = parse_declarator(&specifiers, specifiers.type, true);
1798 /* TODO check declaration constraints for parameters */
1799 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1800 parse_error("typedef not allowed in parameter list");
1803 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1805 if (declaration->type->type == TYPE_ARRAY) {
1806 const array_type_t *const arr_type =
1807 (const array_type_t*)declaration->type;
1809 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1815 static declaration_t *parse_parameters(function_type_t *type)
1817 if(token.type == T_IDENTIFIER) {
1818 symbol_t *symbol = token.v.symbol;
1819 if(!is_typedef_symbol(symbol)) {
1820 /* TODO: K&R style C parameters */
1821 parse_identifier_list();
1826 if(token.type == ')') {
1827 type->unspecified_parameters = 1;
1830 if(token.type == T_void && look_ahead(1)->type == ')') {
1835 declaration_t *declarations = NULL;
1836 declaration_t *declaration;
1837 declaration_t *last_declaration = NULL;
1838 function_parameter_t *parameter;
1839 function_parameter_t *last_parameter = NULL;
1842 switch(token.type) {
1846 return declarations;
1849 case T___extension__:
1851 declaration = parse_parameter();
1853 parameter = allocate_type_zero(sizeof(parameter[0]));
1854 parameter->type = declaration->type;
1856 if(last_parameter != NULL) {
1857 last_declaration->next = declaration;
1858 last_parameter->next = parameter;
1860 type->parameters = parameter;
1861 declarations = declaration;
1863 last_parameter = parameter;
1864 last_declaration = declaration;
1868 return declarations;
1870 if(token.type != ',')
1871 return declarations;
1881 } construct_type_type_t;
1883 typedef struct construct_type_t construct_type_t;
1884 struct construct_type_t {
1885 construct_type_type_t type;
1886 construct_type_t *next;
1889 typedef struct parsed_pointer_t parsed_pointer_t;
1890 struct parsed_pointer_t {
1891 construct_type_t construct_type;
1892 type_qualifiers_t type_qualifiers;
1895 typedef struct construct_function_type_t construct_function_type_t;
1896 struct construct_function_type_t {
1897 construct_type_t construct_type;
1898 function_type_t *function_type;
1901 typedef struct parsed_array_t parsed_array_t;
1902 struct parsed_array_t {
1903 construct_type_t construct_type;
1904 type_qualifiers_t type_qualifiers;
1910 typedef struct construct_base_type_t construct_base_type_t;
1911 struct construct_base_type_t {
1912 construct_type_t construct_type;
1916 static construct_type_t *parse_pointer_declarator(void)
1920 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1921 memset(pointer, 0, sizeof(pointer[0]));
1922 pointer->construct_type.type = CONSTRUCT_POINTER;
1923 pointer->type_qualifiers = parse_type_qualifiers();
1925 return (construct_type_t*) pointer;
1928 static construct_type_t *parse_array_declarator(void)
1932 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1933 memset(array, 0, sizeof(array[0]));
1934 array->construct_type.type = CONSTRUCT_ARRAY;
1936 if(token.type == T_static) {
1937 array->is_static = true;
1941 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1942 if(type_qualifiers != 0) {
1943 if(token.type == T_static) {
1944 array->is_static = true;
1948 array->type_qualifiers = type_qualifiers;
1950 if(token.type == '*' && look_ahead(1)->type == ']') {
1951 array->is_variable = true;
1953 } else if(token.type != ']') {
1954 array->size = parse_assignment_expression();
1959 return (construct_type_t*) array;
1962 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1966 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1967 type->type.type = TYPE_FUNCTION;
1969 declaration_t *parameters = parse_parameters(type);
1970 if(declaration != NULL) {
1971 declaration->context.declarations = parameters;
1974 construct_function_type_t *construct_function_type =
1975 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1976 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1977 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1978 construct_function_type->function_type = type;
1982 return (construct_type_t*) construct_function_type;
1985 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1986 bool may_be_abstract)
1988 /* construct a single linked list of construct_type_t's which describe
1989 * how to construct the final declarator type */
1990 construct_type_t *first = NULL;
1991 construct_type_t *last = NULL;
1994 while(token.type == '*') {
1995 construct_type_t *type = parse_pointer_declarator();
2006 /* TODO: find out if this is correct */
2009 construct_type_t *inner_types = NULL;
2011 switch(token.type) {
2013 if(declaration == NULL) {
2014 parse_error("no identifier expected in typename");
2016 declaration->symbol = token.v.symbol;
2017 declaration->source_position = token.source_position;
2023 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2029 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2030 /* avoid a loop in the outermost scope, because eat_statement doesn't
2032 if(token.type == '}' && current_function == NULL) {
2040 construct_type_t *p = last;
2043 construct_type_t *type;
2044 switch(token.type) {
2046 type = parse_function_declarator(declaration);
2049 type = parse_array_declarator();
2052 goto declarator_finished;
2055 /* insert in the middle of the list (behind p) */
2057 type->next = p->next;
2068 declarator_finished:
2071 /* append inner_types at the end of the list, we don't to set last anymore
2072 * as it's not needed anymore */
2074 assert(first == NULL);
2075 first = inner_types;
2077 last->next = inner_types;
2083 static type_t *construct_declarator_type(construct_type_t *construct_list,
2086 construct_type_t *iter = construct_list;
2087 for( ; iter != NULL; iter = iter->next) {
2088 parsed_pointer_t *parsed_pointer;
2089 parsed_array_t *parsed_array;
2090 construct_function_type_t *construct_function_type;
2091 function_type_t *function_type;
2092 pointer_type_t *pointer_type;
2093 array_type_t *array_type;
2095 switch(iter->type) {
2096 case CONSTRUCT_INVALID:
2097 panic("invalid type construction found");
2098 case CONSTRUCT_FUNCTION:
2099 construct_function_type = (construct_function_type_t*) iter;
2100 function_type = construct_function_type->function_type;
2102 function_type->result_type = type;
2103 type = (type_t*) function_type;
2106 case CONSTRUCT_POINTER:
2107 parsed_pointer = (parsed_pointer_t*) iter;
2108 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2110 pointer_type->type.type = TYPE_POINTER;
2111 pointer_type->points_to = type;
2112 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2113 type = (type_t*) pointer_type;
2116 case CONSTRUCT_ARRAY:
2117 parsed_array = (parsed_array_t*) iter;
2118 array_type = allocate_type_zero(sizeof(array_type[0]));
2120 array_type->type.type = TYPE_ARRAY;
2121 array_type->element_type = type;
2122 array_type->type.qualifiers = parsed_array->type_qualifiers;
2123 array_type->is_static = parsed_array->is_static;
2124 array_type->is_variable = parsed_array->is_variable;
2125 array_type->size = parsed_array->size;
2126 type = (type_t*) array_type;
2130 type_t *hashed_type = typehash_insert((type_t*) type);
2131 if(hashed_type != type) {
2132 /* the function type was constructed earlier freeing it here will
2133 * destroy other types... */
2134 if(iter->type != CONSTRUCT_FUNCTION) {
2144 static declaration_t *parse_declarator(
2145 const declaration_specifiers_t *specifiers,
2146 type_t *type, bool may_be_abstract)
2148 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2149 declaration->storage_class = specifiers->storage_class;
2150 declaration->is_inline = specifiers->is_inline;
2152 construct_type_t *construct_type
2153 = parse_inner_declarator(declaration, may_be_abstract);
2154 declaration->type = construct_declarator_type(construct_type, type);
2156 if(construct_type != NULL) {
2157 obstack_free(&temp_obst, construct_type);
2163 static type_t *parse_abstract_declarator(type_t *base_type)
2165 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2167 type_t *result = construct_declarator_type(construct_type, base_type);
2168 if(construct_type != NULL) {
2169 obstack_free(&temp_obst, construct_type);
2175 static declaration_t *record_declaration(declaration_t *declaration)
2177 assert(context != NULL);
2179 symbol_t *symbol = declaration->symbol;
2180 if(symbol != NULL) {
2181 declaration_t *alias = environment_push(declaration);
2182 if(alias != declaration)
2185 declaration->parent_context = context;
2188 if(last_declaration != NULL) {
2189 last_declaration->next = declaration;
2191 context->declarations = declaration;
2193 last_declaration = declaration;
2198 static void parser_error_multiple_definition(declaration_t *previous,
2199 declaration_t *declaration)
2201 parser_print_error_prefix_pos(declaration->source_position);
2202 fprintf(stderr, "multiple definition of symbol '%s'\n",
2203 declaration->symbol->string);
2204 parser_print_error_prefix_pos(previous->source_position);
2205 fprintf(stderr, "this is the location of the previous definition.\n");
2208 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2211 declaration_t *ndeclaration
2212 = parse_declarator(specifiers, specifiers->type, false);
2214 declaration_t *declaration = record_declaration(ndeclaration);
2216 type_t *orig_type = declaration->type;
2217 type_t *type = skip_typeref(orig_type);
2218 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2219 parser_print_warning_prefix_pos(declaration->source_position);
2220 fprintf(stderr, "variable '%s' declared 'inline'\n",
2221 declaration->symbol->string);
2224 if(token.type == '=') {
2227 /* TODO: check that this is an allowed type (no function type) */
2229 if(declaration->init.initializer != NULL) {
2230 parser_error_multiple_definition(declaration, ndeclaration);
2233 initializer_t *initializer = parse_initializer(type);
2235 if(type->type == TYPE_ARRAY && initializer != NULL) {
2236 array_type_t *array_type = (array_type_t*) type;
2238 if(array_type->size == NULL) {
2239 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2241 cnst->expression.type = EXPR_CONST;
2242 cnst->expression.datatype = type_size_t;
2244 if(initializer->type == INITIALIZER_LIST) {
2245 initializer_list_t *list = &initializer->list;
2246 cnst->v.int_value = list->len;
2248 assert(initializer->type == INITIALIZER_STRING);
2249 initializer_string_t *string = &initializer->string;
2250 cnst->v.int_value = strlen(string->string) + 1;
2253 array_type->size = (expression_t*) cnst;
2258 ndeclaration->init.initializer = initializer;
2259 } else if(token.type == '{') {
2260 if(type->type != TYPE_FUNCTION) {
2261 parser_print_error_prefix();
2262 fprintf(stderr, "declarator '");
2263 print_type_ext(orig_type, declaration->symbol, NULL);
2264 fprintf(stderr, "' has a body but is not a function type.\n");
2269 if(declaration->init.statement != NULL) {
2270 parser_error_multiple_definition(declaration, ndeclaration);
2272 if(ndeclaration != declaration) {
2273 memcpy(&declaration->context, &ndeclaration->context,
2274 sizeof(declaration->context));
2277 int top = environment_top();
2278 context_t *last_context = context;
2279 set_context(&declaration->context);
2281 /* push function parameters */
2282 declaration_t *parameter = declaration->context.declarations;
2283 for( ; parameter != NULL; parameter = parameter->next) {
2284 environment_push(parameter);
2287 int label_stack_top = label_top();
2288 declaration_t *old_current_function = current_function;
2289 current_function = declaration;
2291 statement_t *statement = parse_compound_statement();
2293 assert(current_function == declaration);
2294 current_function = old_current_function;
2295 label_pop_to(label_stack_top);
2297 assert(context == &declaration->context);
2298 set_context(last_context);
2299 environment_pop_to(top);
2301 declaration->init.statement = statement;
2305 if(token.type != ',')
2312 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2315 if(token.type == ':') {
2317 parse_constant_expression();
2318 /* TODO (bitfields) */
2320 declaration_t *declaration
2321 = parse_declarator(specifiers, specifiers->type, true);
2323 /* TODO: check constraints for struct declarations */
2324 /* TODO: check for doubled fields */
2325 record_declaration(declaration);
2327 if(token.type == ':') {
2329 parse_constant_expression();
2330 /* TODO (bitfields) */
2334 if(token.type != ',')
2341 static void parse_compound_type_entries(void)
2345 while(token.type != '}' && token.type != T_EOF) {
2346 declaration_specifiers_t specifiers;
2347 memset(&specifiers, 0, sizeof(specifiers));
2348 parse_declaration_specifiers(&specifiers);
2350 parse_struct_declarators(&specifiers);
2352 if(token.type == T_EOF) {
2353 parse_error("unexpected error while parsing struct");
2358 static void parse_declaration(void)
2360 source_position_t source_position = token.source_position;
2362 declaration_specifiers_t specifiers;
2363 memset(&specifiers, 0, sizeof(specifiers));
2364 parse_declaration_specifiers(&specifiers);
2366 if(token.type == ';') {
2367 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2368 parse_warning_pos(source_position,
2369 "useless keyword in empty declaration");
2371 switch (specifiers.type->type) {
2372 case TYPE_COMPOUND_STRUCT:
2373 case TYPE_COMPOUND_UNION: {
2374 const compound_type_t *const comp_type =
2375 (const compound_type_t*)specifiers.type;
2376 if (comp_type->declaration->symbol == NULL) {
2377 parse_warning_pos(source_position,
2378 "unnamed struct/union that defines no instances");
2383 case TYPE_ENUM: break;
2386 parse_warning_pos(source_position, "empty declaration");
2392 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2394 declaration->type = specifiers.type;
2395 declaration->storage_class = specifiers.storage_class;
2396 declaration->source_position = source_position;
2397 record_declaration(declaration);
2400 parse_init_declarators(&specifiers);
2403 static type_t *parse_typename(void)
2405 declaration_specifiers_t specifiers;
2406 memset(&specifiers, 0, sizeof(specifiers));
2407 parse_declaration_specifiers(&specifiers);
2408 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2409 /* TODO: improve error message, user does probably not know what a
2410 * storage class is...
2412 parse_error("typename may not have a storage class");
2415 type_t *result = parse_abstract_declarator(specifiers.type);
2423 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2424 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2425 expression_t *left);
2427 typedef struct expression_parser_function_t expression_parser_function_t;
2428 struct expression_parser_function_t {
2429 unsigned precedence;
2430 parse_expression_function parser;
2431 unsigned infix_precedence;
2432 parse_expression_infix_function infix_parser;
2435 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2437 static expression_t *make_invalid_expression(void)
2439 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2440 expression->type = EXPR_INVALID;
2441 expression->source_position = token.source_position;
2445 static expression_t *expected_expression_error(void)
2447 parser_print_error_prefix();
2448 fprintf(stderr, "expected expression, got token ");
2449 print_token(stderr, & token);
2450 fprintf(stderr, "\n");
2454 return make_invalid_expression();
2457 static expression_t *parse_string_const(void)
2459 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2461 cnst->expression.type = EXPR_STRING_LITERAL;
2462 cnst->expression.datatype = type_string;
2463 cnst->value = parse_string_literals();
2465 return (expression_t*) cnst;
2468 static expression_t *parse_int_const(void)
2470 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2472 cnst->expression.type = EXPR_CONST;
2473 cnst->expression.datatype = token.datatype;
2474 cnst->v.int_value = token.v.intvalue;
2478 return (expression_t*) cnst;
2481 static expression_t *parse_float_const(void)
2483 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2485 cnst->expression.type = EXPR_CONST;
2486 cnst->expression.datatype = token.datatype;
2487 cnst->v.float_value = token.v.floatvalue;
2491 return (expression_t*) cnst;
2494 static declaration_t *create_implicit_function(symbol_t *symbol,
2495 const source_position_t source_position)
2497 function_type_t *function_type
2498 = allocate_type_zero(sizeof(function_type[0]));
2500 function_type->type.type = TYPE_FUNCTION;
2501 function_type->result_type = type_int;
2502 function_type->unspecified_parameters = true;
2504 type_t *type = typehash_insert((type_t*) function_type);
2505 if(type != (type_t*) function_type) {
2506 free_type(function_type);
2509 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2511 declaration->storage_class = STORAGE_CLASS_EXTERN;
2512 declaration->type = type;
2513 declaration->symbol = symbol;
2514 declaration->source_position = source_position;
2516 /* prepend the implicit definition to the global context
2517 * this is safe since the symbol wasn't declared as anything else yet
2519 assert(symbol->declaration == NULL);
2521 context_t *last_context = context;
2522 context = global_context;
2524 environment_push(declaration);
2525 declaration->next = context->declarations;
2526 context->declarations = declaration;
2528 context = last_context;
2533 static expression_t *parse_reference(void)
2535 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2537 ref->expression.type = EXPR_REFERENCE;
2538 ref->symbol = token.v.symbol;
2540 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2542 source_position_t source_position = token.source_position;
2545 if(declaration == NULL) {
2547 /* an implicitly defined function */
2548 if(token.type == '(') {
2549 parser_print_prefix_pos(token.source_position);
2550 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2551 ref->symbol->string);
2553 declaration = create_implicit_function(ref->symbol,
2558 parser_print_error_prefix();
2559 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2560 return (expression_t*) ref;
2564 ref->declaration = declaration;
2565 ref->expression.datatype = declaration->type;
2567 return (expression_t*) ref;
2570 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2574 /* TODO check if explicit cast is allowed and issue warnings/errors */
2577 static expression_t *parse_cast(void)
2579 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2581 cast->expression.type = EXPR_UNARY;
2582 cast->type = UNEXPR_CAST;
2583 cast->expression.source_position = token.source_position;
2585 type_t *type = parse_typename();
2588 expression_t *value = parse_sub_expression(20);
2590 check_cast_allowed(value, type);
2592 cast->expression.datatype = type;
2593 cast->value = value;
2595 return (expression_t*) cast;
2598 static expression_t *parse_statement_expression(void)
2600 statement_expression_t *expression
2601 = allocate_ast_zero(sizeof(expression[0]));
2602 expression->expression.type = EXPR_STATEMENT;
2604 statement_t *statement = parse_compound_statement();
2605 expression->statement = statement;
2606 if(statement == NULL) {
2611 assert(statement->type == STATEMENT_COMPOUND);
2612 compound_statement_t *compound_statement
2613 = (compound_statement_t*) statement;
2615 /* find last statement and use it's type */
2616 const statement_t *last_statement = NULL;
2617 const statement_t *iter = compound_statement->statements;
2618 for( ; iter != NULL; iter = iter->next) {
2619 last_statement = iter;
2622 if(last_statement->type == STATEMENT_EXPRESSION) {
2623 const expression_statement_t *expression_statement =
2624 (const expression_statement_t*) last_statement;
2625 expression->expression.datatype
2626 = expression_statement->expression->datatype;
2628 expression->expression.datatype = type_void;
2633 return (expression_t*) expression;
2636 static expression_t *parse_brace_expression(void)
2640 switch(token.type) {
2642 /* gcc extension: a stement expression */
2643 return parse_statement_expression();
2647 return parse_cast();
2649 if(is_typedef_symbol(token.v.symbol)) {
2650 return parse_cast();
2654 expression_t *result = parse_expression();
2660 static expression_t *parse_function_keyword(void)
2665 if (current_function == NULL) {
2666 parse_error("'__func__' used outside of a function");
2669 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2670 expression->expression.type = EXPR_FUNCTION;
2671 expression->expression.datatype = type_string;
2672 expression->value = "TODO: FUNCTION";
2674 return (expression_t*) expression;
2677 static expression_t *parse_pretty_function_keyword(void)
2679 eat(T___PRETTY_FUNCTION__);
2682 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2683 expression->expression.type = EXPR_PRETTY_FUNCTION;
2684 expression->expression.datatype = type_string;
2685 expression->value = "TODO: PRETTY FUNCTION";
2687 return (expression_t*) expression;
2690 static designator_t *parse_designator(void)
2692 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2694 if(token.type != T_IDENTIFIER) {
2695 parse_error_expected("while parsing member designator",
2700 result->symbol = token.v.symbol;
2703 designator_t *last_designator = result;
2705 if(token.type == '.') {
2707 if(token.type != T_IDENTIFIER) {
2708 parse_error_expected("while parsing member designator",
2713 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2714 designator->symbol = token.v.symbol;
2717 last_designator->next = designator;
2718 last_designator = designator;
2721 if(token.type == '[') {
2723 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2724 designator->array_access = parse_expression();
2725 if(designator->array_access == NULL) {
2731 last_designator->next = designator;
2732 last_designator = designator;
2741 static expression_t *parse_offsetof(void)
2743 eat(T___builtin_offsetof);
2745 offsetof_expression_t *expression
2746 = allocate_ast_zero(sizeof(expression[0]));
2747 expression->expression.type = EXPR_OFFSETOF;
2748 expression->expression.datatype = type_size_t;
2751 expression->type = parse_typename();
2753 expression->designator = parse_designator();
2756 return (expression_t*) expression;
2759 static expression_t *parse_va_arg(void)
2761 eat(T___builtin_va_arg);
2763 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2764 expression->expression.type = EXPR_VA_ARG;
2767 expression->arg = parse_assignment_expression();
2769 expression->expression.datatype = parse_typename();
2772 return (expression_t*) expression;
2775 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2777 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2778 parameter->type = argument_type;
2780 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2781 type->type.type = TYPE_FUNCTION;
2782 type->result_type = result_type;
2783 type->parameters = parameter;
2785 type_t *result = typehash_insert((type_t*) type);
2786 if(result != (type_t*) type) {
2793 static expression_t *parse_builtin_symbol(void)
2795 builtin_symbol_expression_t *expression
2796 = allocate_ast_zero(sizeof(expression[0]));
2797 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2799 expression->symbol = token.v.symbol;
2802 switch(token.type) {
2803 case T___builtin_alloca:
2804 type = make_function_1_type(type_void_ptr, type_size_t);
2810 expression->expression.datatype = type;
2811 return (expression_t*) expression;
2814 static expression_t *parse_primary_expression(void)
2816 switch(token.type) {
2818 return parse_int_const();
2819 case T_FLOATINGPOINT:
2820 return parse_float_const();
2821 case T_STRING_LITERAL:
2822 return parse_string_const();
2824 return parse_reference();
2825 case T___FUNCTION__:
2827 return parse_function_keyword();
2828 case T___PRETTY_FUNCTION__:
2829 return parse_pretty_function_keyword();
2830 case T___builtin_offsetof:
2831 return parse_offsetof();
2832 case T___builtin_va_arg:
2833 return parse_va_arg();
2834 case T___builtin_alloca:
2835 case T___builtin_expect:
2836 case T___builtin_va_start:
2837 case T___builtin_va_end:
2838 return parse_builtin_symbol();
2841 return parse_brace_expression();
2844 parser_print_error_prefix();
2845 fprintf(stderr, "unexpected token ");
2846 print_token(stderr, &token);
2847 fprintf(stderr, "\n");
2850 return make_invalid_expression();
2853 static expression_t *parse_array_expression(unsigned precedence,
2854 expression_t *array_ref)
2860 expression_t *index = parse_expression();
2862 array_access_expression_t *array_access
2863 = allocate_ast_zero(sizeof(array_access[0]));
2865 array_access->expression.type = EXPR_ARRAY_ACCESS;
2866 array_access->array_ref = array_ref;
2867 array_access->index = index;
2869 type_t *type_left = skip_typeref(array_ref->datatype);
2870 type_t *type_right = skip_typeref(index->datatype);
2872 if(type_left != NULL && type_right != NULL) {
2873 if(type_left->type == TYPE_POINTER) {
2874 pointer_type_t *pointer = (pointer_type_t*) type_left;
2875 array_access->expression.datatype = pointer->points_to;
2876 } else if(type_left->type == TYPE_ARRAY) {
2877 array_type_t *array_type = (array_type_t*) type_left;
2878 array_access->expression.datatype = array_type->element_type;
2879 } else if(type_right->type == TYPE_POINTER) {
2880 pointer_type_t *pointer = (pointer_type_t*) type_right;
2881 array_access->expression.datatype = pointer->points_to;
2882 } else if(type_right->type == TYPE_ARRAY) {
2883 array_type_t *array_type = (array_type_t*) type_right;
2884 array_access->expression.datatype = array_type->element_type;
2886 parser_print_error_prefix();
2887 fprintf(stderr, "array access on object with non-pointer types ");
2888 print_type_quoted(type_left);
2889 fprintf(stderr, ", ");
2890 print_type_quoted(type_right);
2891 fprintf(stderr, "\n");
2895 if(token.type != ']') {
2896 parse_error_expected("Problem while parsing array access", ']', 0);
2897 return (expression_t*) array_access;
2901 return (expression_t*) array_access;
2904 static bool is_declaration_specifier(const token_t *token,
2905 bool only_type_specifiers)
2907 switch(token->type) {
2911 return is_typedef_symbol(token->v.symbol);
2914 if(only_type_specifiers)
2923 static expression_t *parse_sizeof(unsigned precedence)
2927 sizeof_expression_t *sizeof_expression
2928 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2929 sizeof_expression->expression.type = EXPR_SIZEOF;
2930 sizeof_expression->expression.datatype = type_size_t;
2932 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2934 sizeof_expression->type = parse_typename();
2937 expression_t *expression = parse_sub_expression(precedence);
2938 sizeof_expression->type = expression->datatype;
2939 sizeof_expression->size_expression = expression;
2942 return (expression_t*) sizeof_expression;
2945 static expression_t *parse_select_expression(unsigned precedence,
2946 expression_t *compound)
2949 assert(token.type == '.' || token.type == T_MINUSGREATER);
2951 bool is_pointer = (token.type == T_MINUSGREATER);
2954 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2956 select->expression.type = EXPR_SELECT;
2957 select->compound = compound;
2959 if(token.type != T_IDENTIFIER) {
2960 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2961 return (expression_t*) select;
2963 symbol_t *symbol = token.v.symbol;
2964 select->symbol = symbol;
2967 type_t *orig_type = compound->datatype;
2968 if(orig_type == NULL)
2969 return make_invalid_expression();
2971 type_t *type = skip_typeref(orig_type);
2973 type_t *type_left = type;
2975 if(type->type != TYPE_POINTER) {
2976 parser_print_error_prefix();
2977 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2978 print_type_quoted(orig_type);
2979 fputc('\n', stderr);
2980 return make_invalid_expression();
2982 pointer_type_t *pointer_type = (pointer_type_t*) type;
2983 type_left = pointer_type->points_to;
2985 type_left = skip_typeref(type_left);
2987 if(type_left->type != TYPE_COMPOUND_STRUCT
2988 && type_left->type != TYPE_COMPOUND_UNION) {
2989 parser_print_error_prefix();
2990 fprintf(stderr, "request for member '%s' in something not a struct or "
2991 "union, but ", symbol->string);
2992 print_type_quoted(type_left);
2993 fputc('\n', stderr);
2994 return make_invalid_expression();
2997 compound_type_t *compound_type = (compound_type_t*) type_left;
2998 declaration_t *declaration = compound_type->declaration;
3000 if(!declaration->init.is_defined) {
3001 parser_print_error_prefix();
3002 fprintf(stderr, "request for member '%s' of incomplete type ",
3004 print_type_quoted(type_left);
3005 fputc('\n', stderr);
3006 return make_invalid_expression();
3009 declaration_t *iter = declaration->context.declarations;
3010 for( ; iter != NULL; iter = iter->next) {
3011 if(iter->symbol == symbol) {
3016 parser_print_error_prefix();
3017 print_type_quoted(type_left);
3018 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3019 return make_invalid_expression();
3022 select->compound_entry = iter;
3023 select->expression.datatype = iter->type;
3024 return (expression_t*) select;
3027 static expression_t *parse_call_expression(unsigned precedence,
3028 expression_t *expression)
3031 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3032 call->expression.type = EXPR_CALL;
3033 call->function = expression;
3035 function_type_t *function_type;
3036 type_t *orig_type = expression->datatype;
3037 type_t *type = skip_typeref(orig_type);
3039 if(type->type == TYPE_POINTER) {
3040 pointer_type_t *pointer_type = (pointer_type_t*) type;
3042 type = skip_typeref(pointer_type->points_to);
3044 if (type->type == TYPE_FUNCTION) {
3045 function_type = (function_type_t*) type;
3046 call->expression.datatype = function_type->result_type;
3048 parser_print_error_prefix();
3049 fputs("called object '", stderr);
3050 print_expression(expression);
3051 fputs("' (type ", stderr);
3052 print_type_quoted(orig_type);
3053 fputs(") is not a function\n", stderr);
3055 function_type = NULL;
3056 call->expression.datatype = NULL;
3059 /* parse arguments */
3062 if(token.type != ')') {
3063 call_argument_t *last_argument = NULL;
3066 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3068 argument->expression = parse_assignment_expression();
3069 if(last_argument == NULL) {
3070 call->arguments = argument;
3072 last_argument->next = argument;
3074 last_argument = argument;
3076 if(token.type != ',')
3083 if(function_type != NULL) {
3084 function_parameter_t *parameter = function_type->parameters;
3085 call_argument_t *argument = call->arguments;
3086 for( ; parameter != NULL && argument != NULL;
3087 parameter = parameter->next, argument = argument->next) {
3088 type_t *expected_type = parameter->type;
3089 /* TODO report context in error messages */
3090 argument->expression = create_implicit_cast(argument->expression,
3093 /* too few parameters */
3094 if(parameter != NULL) {
3095 parser_print_error_prefix();
3096 fprintf(stderr, "too few arguments to function '");
3097 print_expression(expression);
3098 fprintf(stderr, "'\n");
3099 } else if(argument != NULL) {
3100 /* too many parameters */
3101 if(!function_type->variadic
3102 && !function_type->unspecified_parameters) {
3103 parser_print_error_prefix();
3104 fprintf(stderr, "too many arguments to function '");
3105 print_expression(expression);
3106 fprintf(stderr, "'\n");
3108 /* do default promotion */
3109 for( ; argument != NULL; argument = argument->next) {
3110 type_t *type = argument->expression->datatype;
3115 if(is_type_integer(type)) {
3116 type = promote_integer(type);
3117 } else if(type == type_float) {
3120 argument->expression
3121 = create_implicit_cast(argument->expression, type);
3127 return (expression_t*) call;
3130 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3132 static expression_t *parse_conditional_expression(unsigned precedence,
3133 expression_t *expression)
3137 conditional_expression_t *conditional
3138 = allocate_ast_zero(sizeof(conditional[0]));
3139 conditional->expression.type = EXPR_CONDITIONAL;
3140 conditional->condition = expression;
3143 type_t *condition_type_orig = conditional->condition->datatype;
3144 if(condition_type_orig != NULL) {
3145 type_t *condition_type = skip_typeref(condition_type_orig);
3146 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3147 type_error("expected a scalar type", expression->source_position,
3148 condition_type_orig);
3152 expression_t *const t_expr = parse_expression();
3153 conditional->true_expression = t_expr;
3155 expression_t *const f_expr = parse_sub_expression(precedence);
3156 conditional->false_expression = f_expr;
3158 type_t *const true_type = t_expr->datatype;
3159 if(true_type == NULL)
3160 return (expression_t*) conditional;
3161 type_t *const false_type = f_expr->datatype;
3162 if(false_type == NULL)
3163 return (expression_t*) conditional;
3165 type_t *const skipped_true_type = skip_typeref(true_type);
3166 type_t *const skipped_false_type = skip_typeref(false_type);
3169 if (skipped_true_type == skipped_false_type) {
3170 conditional->expression.datatype = skipped_true_type;
3171 } else if (is_type_arithmetic(skipped_true_type) &&
3172 is_type_arithmetic(skipped_false_type)) {
3173 type_t *const result = semantic_arithmetic(skipped_true_type,
3174 skipped_false_type);
3175 conditional->true_expression = create_implicit_cast(t_expr, result);
3176 conditional->false_expression = create_implicit_cast(f_expr, result);
3177 conditional->expression.datatype = result;
3178 } else if (skipped_true_type->type == TYPE_POINTER &&
3179 skipped_false_type->type == TYPE_POINTER &&
3180 true /* TODO compatible points_to types */) {
3182 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3183 skipped_false_type->type == TYPE_POINTER)
3184 || (is_null_ptr_const(skipped_false_type) &&
3185 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3187 } else if(/* 1 is pointer to object type, other is void* */ false) {
3190 type_error_incompatible("while parsing conditional",
3191 expression->source_position, true_type,
3192 skipped_false_type);
3195 return (expression_t*) conditional;
3198 static expression_t *parse_extension(unsigned precedence)
3200 eat(T___extension__);
3202 /* TODO enable extensions */
3204 return parse_sub_expression(precedence);
3207 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3209 eat(T___builtin_classify_type);
3211 classify_type_expression_t *const classify_type_expr =
3212 allocate_ast_zero(sizeof(classify_type_expr[0]));
3213 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3214 classify_type_expr->expression.datatype = type_int;
3217 expression_t *const expression = parse_sub_expression(precedence);
3219 classify_type_expr->type_expression = expression;
3221 return (expression_t*)classify_type_expr;
3224 static void semantic_incdec(unary_expression_t *expression)
3226 type_t *orig_type = expression->value->datatype;
3227 if(orig_type == NULL)
3230 type_t *type = skip_typeref(orig_type);
3231 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3232 /* TODO: improve error message */
3233 parser_print_error_prefix();
3234 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3238 expression->expression.datatype = orig_type;
3241 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3243 type_t *orig_type = expression->value->datatype;
3244 if(orig_type == NULL)
3247 type_t *type = skip_typeref(orig_type);
3248 if(!is_type_arithmetic(type)) {
3249 /* TODO: improve error message */
3250 parser_print_error_prefix();
3251 fprintf(stderr, "operation needs an arithmetic type\n");
3255 expression->expression.datatype = orig_type;
3258 static void semantic_unexpr_scalar(unary_expression_t *expression)
3260 type_t *orig_type = expression->value->datatype;
3261 if(orig_type == NULL)
3264 type_t *type = skip_typeref(orig_type);
3265 if (!is_type_scalar(type)) {
3266 parse_error("operand of ! must be of scalar type\n");
3270 expression->expression.datatype = orig_type;
3273 static void semantic_unexpr_integer(unary_expression_t *expression)
3275 type_t *orig_type = expression->value->datatype;
3276 if(orig_type == NULL)
3279 type_t *type = skip_typeref(orig_type);
3280 if (!is_type_integer(type)) {
3281 parse_error("operand of ~ must be of integer type\n");
3285 expression->expression.datatype = orig_type;
3288 static void semantic_dereference(unary_expression_t *expression)
3290 type_t *orig_type = expression->value->datatype;
3291 if(orig_type == NULL)
3294 type_t *type = skip_typeref(orig_type);
3295 switch (type->type) {
3297 array_type_t *const array_type = (array_type_t*)type;
3298 expression->expression.datatype = array_type->element_type;
3302 case TYPE_POINTER: {
3303 pointer_type_t *pointer_type = (pointer_type_t*)type;
3304 expression->expression.datatype = pointer_type->points_to;
3309 parser_print_error_prefix();
3310 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3311 print_type_quoted(orig_type);
3312 fputs(" given.\n", stderr);
3317 static void semantic_take_addr(unary_expression_t *expression)
3319 type_t *orig_type = expression->value->datatype;
3320 if(orig_type == NULL)
3323 expression_t *value = expression->value;
3324 if(value->type == EXPR_REFERENCE) {
3325 reference_expression_t *reference = (reference_expression_t*) value;
3326 declaration_t *declaration = reference->declaration;
3327 if(declaration != NULL) {
3328 declaration->address_taken = 1;
3332 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3335 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3336 static expression_t *parse_##unexpression_type(unsigned precedence) \
3340 unary_expression_t *unary_expression \
3341 = allocate_ast_zero(sizeof(unary_expression[0])); \
3342 unary_expression->expression.type = EXPR_UNARY; \
3343 unary_expression->type = unexpression_type; \
3344 unary_expression->value = parse_sub_expression(precedence); \
3346 sfunc(unary_expression); \
3348 return (expression_t*) unary_expression; \
3351 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3352 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3353 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3354 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3355 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3356 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3357 semantic_unexpr_integer)
3358 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3360 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3363 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3365 static expression_t *parse_##unexpression_type(unsigned precedence, \
3366 expression_t *left) \
3368 (void) precedence; \
3371 unary_expression_t *unary_expression \
3372 = allocate_ast_zero(sizeof(unary_expression[0])); \
3373 unary_expression->expression.type = EXPR_UNARY; \
3374 unary_expression->type = unexpression_type; \
3375 unary_expression->value = left; \
3377 sfunc(unary_expression); \
3379 return (expression_t*) unary_expression; \
3382 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3384 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3387 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3389 /* TODO: handle complex + imaginary types */
3391 /* § 6.3.1.8 Usual arithmetic conversions */
3392 if(type_left == type_long_double || type_right == type_long_double) {
3393 return type_long_double;
3394 } else if(type_left == type_double || type_right == type_double) {
3396 } else if(type_left == type_float || type_right == type_float) {
3400 type_right = promote_integer(type_right);
3401 type_left = promote_integer(type_left);
3403 if(type_left == type_right)
3406 bool signed_left = is_type_signed(type_left);
3407 bool signed_right = is_type_signed(type_right);
3408 if(get_rank(type_left) < get_rank(type_right)) {
3409 if(signed_left == signed_right || !signed_right) {
3415 if(signed_left == signed_right || !signed_left) {
3423 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3425 expression_t *left = expression->left;
3426 expression_t *right = expression->right;
3427 type_t *orig_type_left = left->datatype;
3428 type_t *orig_type_right = right->datatype;
3430 if(orig_type_left == NULL || orig_type_right == NULL)
3433 type_t *type_left = skip_typeref(orig_type_left);
3434 type_t *type_right = skip_typeref(orig_type_right);
3436 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3437 /* TODO: improve error message */
3438 parser_print_error_prefix();
3439 fprintf(stderr, "operation needs arithmetic types\n");
3443 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3444 expression->left = create_implicit_cast(left, arithmetic_type);
3445 expression->right = create_implicit_cast(right, arithmetic_type);
3446 expression->expression.datatype = arithmetic_type;
3449 static void semantic_shift_op(binary_expression_t *expression)
3451 expression_t *left = expression->left;
3452 expression_t *right = expression->right;
3453 type_t *orig_type_left = left->datatype;
3454 type_t *orig_type_right = right->datatype;
3456 if(orig_type_left == NULL || orig_type_right == NULL)
3459 type_t *type_left = skip_typeref(orig_type_left);
3460 type_t *type_right = skip_typeref(orig_type_right);
3462 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3463 /* TODO: improve error message */
3464 parser_print_error_prefix();
3465 fprintf(stderr, "operation needs integer types\n");
3469 type_left = promote_integer(type_left);
3470 type_right = promote_integer(type_right);
3472 expression->left = create_implicit_cast(left, type_left);
3473 expression->right = create_implicit_cast(right, type_right);
3474 expression->expression.datatype = type_left;
3477 static void semantic_add(binary_expression_t *expression)
3479 expression_t *left = expression->left;
3480 expression_t *right = expression->right;
3481 type_t *orig_type_left = left->datatype;
3482 type_t *orig_type_right = right->datatype;
3484 if(orig_type_left == NULL || orig_type_right == NULL)
3487 type_t *type_left = skip_typeref(orig_type_left);
3488 type_t *type_right = skip_typeref(orig_type_right);
3491 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3492 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3493 expression->left = create_implicit_cast(left, arithmetic_type);
3494 expression->right = create_implicit_cast(right, arithmetic_type);
3495 expression->expression.datatype = arithmetic_type;
3497 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3498 expression->expression.datatype = type_left;
3499 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3500 expression->expression.datatype = type_right;
3501 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3502 const array_type_t *const arr_type = (const array_type_t*)type_left;
3503 expression->expression.datatype =
3504 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3505 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3506 const array_type_t *const arr_type = (const array_type_t*)type_right;
3507 expression->expression.datatype =
3508 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3510 parser_print_error_prefix();
3511 fprintf(stderr, "invalid operands to binary + (");
3512 print_type_quoted(orig_type_left);
3513 fprintf(stderr, ", ");
3514 print_type_quoted(orig_type_right);
3515 fprintf(stderr, ")\n");
3519 static void semantic_sub(binary_expression_t *expression)
3521 expression_t *left = expression->left;
3522 expression_t *right = expression->right;
3523 type_t *orig_type_left = left->datatype;
3524 type_t *orig_type_right = right->datatype;
3526 if(orig_type_left == NULL || orig_type_right == NULL)
3529 type_t *type_left = skip_typeref(orig_type_left);
3530 type_t *type_right = skip_typeref(orig_type_right);
3533 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3534 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3535 expression->left = create_implicit_cast(left, arithmetic_type);
3536 expression->right = create_implicit_cast(right, arithmetic_type);
3537 expression->expression.datatype = arithmetic_type;
3539 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3540 expression->expression.datatype = type_left;
3541 } else if(type_left->type == TYPE_POINTER &&
3542 type_right->type == TYPE_POINTER) {
3543 if(!pointers_compatible(type_left, type_right)) {
3544 parser_print_error_prefix();
3545 fprintf(stderr, "pointers to incompatible objects to binary - (");
3546 print_type_quoted(orig_type_left);
3547 fprintf(stderr, ", ");
3548 print_type_quoted(orig_type_right);
3549 fprintf(stderr, ")\n");
3551 expression->expression.datatype = type_ptrdiff_t;
3554 parser_print_error_prefix();
3555 fprintf(stderr, "invalid operands to binary - (");
3556 print_type_quoted(orig_type_left);
3557 fprintf(stderr, ", ");
3558 print_type_quoted(orig_type_right);
3559 fprintf(stderr, ")\n");
3563 static void semantic_comparison(binary_expression_t *expression)
3565 expression_t *left = expression->left;
3566 expression_t *right = expression->right;
3567 type_t *orig_type_left = left->datatype;
3568 type_t *orig_type_right = right->datatype;
3570 if(orig_type_left == NULL || orig_type_right == NULL)
3573 type_t *type_left = skip_typeref(orig_type_left);
3574 type_t *type_right = skip_typeref(orig_type_right);
3576 /* TODO non-arithmetic types */
3577 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3578 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3579 expression->left = create_implicit_cast(left, arithmetic_type);
3580 expression->right = create_implicit_cast(right, arithmetic_type);
3581 expression->expression.datatype = arithmetic_type;
3582 } else if (type_left->type == TYPE_POINTER &&
3583 type_right->type == TYPE_POINTER) {
3584 /* TODO check compatibility */
3585 } else if (type_left->type == TYPE_POINTER) {
3586 expression->right = create_implicit_cast(right, type_left);
3587 } else if (type_right->type == TYPE_POINTER) {
3588 expression->left = create_implicit_cast(left, type_right);
3590 type_error_incompatible("invalid operands in comparison",
3591 token.source_position, type_left, type_right);
3593 expression->expression.datatype = type_int;
3596 static void semantic_arithmetic_assign(binary_expression_t *expression)
3598 expression_t *left = expression->left;
3599 expression_t *right = expression->right;
3600 type_t *orig_type_left = left->datatype;
3601 type_t *orig_type_right = right->datatype;
3603 if(orig_type_left == NULL || orig_type_right == NULL)
3606 type_t *type_left = skip_typeref(orig_type_left);
3607 type_t *type_right = skip_typeref(orig_type_right);
3609 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3610 /* TODO: improve error message */
3611 parser_print_error_prefix();
3612 fprintf(stderr, "operation needs arithmetic types\n");
3616 /* combined instructions are tricky. We can't create an implicit cast on
3617 * the left side, because we need the uncasted form for the store.
3618 * The ast2firm pass has to know that left_type must be right_type
3619 * for the arithmeitc operation and create a cast by itself */
3620 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3621 expression->right = create_implicit_cast(right, arithmetic_type);
3622 expression->expression.datatype = type_left;
3625 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3627 expression_t *left = expression->left;
3628 expression_t *right = expression->right;
3629 type_t *orig_type_left = left->datatype;
3630 type_t *orig_type_right = right->datatype;
3632 if(orig_type_left == NULL || orig_type_right == NULL)
3635 type_t *type_left = skip_typeref(orig_type_left);
3636 type_t *type_right = skip_typeref(orig_type_right);
3638 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3639 /* combined instructions are tricky. We can't create an implicit cast on
3640 * the left side, because we need the uncasted form for the store.
3641 * The ast2firm pass has to know that left_type must be right_type
3642 * for the arithmeitc operation and create a cast by itself */
3643 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3644 expression->right = create_implicit_cast(right, arithmetic_type);
3645 expression->expression.datatype = type_left;
3646 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3647 expression->expression.datatype = type_left;
3649 parser_print_error_prefix();
3650 fputs("Incompatible types ", stderr);
3651 print_type_quoted(orig_type_left);
3652 fputs(" and ", stderr);
3653 print_type_quoted(orig_type_right);
3654 fputs(" in assignment\n", stderr);
3659 static void semantic_logical_op(binary_expression_t *expression)
3661 expression_t *left = expression->left;
3662 expression_t *right = expression->right;
3663 type_t *orig_type_left = left->datatype;
3664 type_t *orig_type_right = right->datatype;
3666 if(orig_type_left == NULL || orig_type_right == NULL)
3669 type_t *type_left = skip_typeref(orig_type_left);
3670 type_t *type_right = skip_typeref(orig_type_right);
3672 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3673 /* TODO: improve error message */
3674 parser_print_error_prefix();
3675 fprintf(stderr, "operation needs scalar types\n");
3679 expression->expression.datatype = type_int;
3682 static void semantic_binexpr_assign(binary_expression_t *expression)
3684 expression_t *left = expression->left;
3685 type_t *orig_type_left = left->datatype;
3687 if(orig_type_left == NULL)
3690 type_t *type_left = skip_typeref(orig_type_left);
3692 if (type_left->type == TYPE_ARRAY) {
3693 parse_error("Cannot assign to arrays.");
3697 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3698 parser_print_error_prefix();
3699 fprintf(stderr, "assignment to readonly location '");
3700 print_expression(left);
3701 fprintf(stderr, "' (type ");
3702 print_type_quoted(orig_type_left);
3703 fprintf(stderr, ")\n");
3706 semantic_assign(orig_type_left, &expression->right, "assignment");
3708 expression->expression.datatype = orig_type_left;
3711 static void semantic_comma(binary_expression_t *expression)
3713 expression->expression.datatype = expression->right->datatype;
3716 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3717 static expression_t *parse_##binexpression_type(unsigned precedence, \
3718 expression_t *left) \
3722 expression_t *right = parse_sub_expression(precedence + lr); \
3724 binary_expression_t *binexpr \
3725 = allocate_ast_zero(sizeof(binexpr[0])); \
3726 binexpr->expression.type = EXPR_BINARY; \
3727 binexpr->type = binexpression_type; \
3728 binexpr->left = left; \
3729 binexpr->right = right; \
3732 return (expression_t*) binexpr; \
3735 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3736 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3737 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3738 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3739 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3740 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3741 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3742 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3743 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3744 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3745 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3746 semantic_comparison, 1)
3747 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3748 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3749 semantic_comparison, 1)
3750 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3751 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3752 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3753 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3754 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3755 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3756 semantic_shift_op, 1)
3757 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3758 semantic_shift_op, 1)
3759 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3760 semantic_arithmetic_addsubb_assign, 0)
3761 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3762 semantic_arithmetic_addsubb_assign, 0)
3763 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3764 semantic_arithmetic_assign, 0)
3765 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3766 semantic_arithmetic_assign, 0)
3767 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3768 semantic_arithmetic_assign, 0)
3769 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3770 semantic_arithmetic_assign, 0)
3771 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3772 semantic_arithmetic_assign, 0)
3773 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3774 semantic_arithmetic_assign, 0)
3775 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3776 semantic_arithmetic_assign, 0)
3777 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3778 semantic_arithmetic_assign, 0)
3780 static expression_t *parse_sub_expression(unsigned precedence)
3782 if(token.type < 0) {
3783 return expected_expression_error();
3786 expression_parser_function_t *parser
3787 = &expression_parsers[token.type];
3788 source_position_t source_position = token.source_position;
3791 if(parser->parser != NULL) {
3792 left = parser->parser(parser->precedence);
3794 left = parse_primary_expression();
3796 assert(left != NULL);
3797 left->source_position = source_position;
3800 if(token.type < 0) {
3801 return expected_expression_error();
3804 parser = &expression_parsers[token.type];
3805 if(parser->infix_parser == NULL)
3807 if(parser->infix_precedence < precedence)
3810 left = parser->infix_parser(parser->infix_precedence, left);
3812 assert(left != NULL);
3813 assert(left->type != EXPR_UNKNOWN);
3814 left->source_position = source_position;
3820 static expression_t *parse_expression(void)
3822 return parse_sub_expression(1);
3827 static void register_expression_parser(parse_expression_function parser,
3828 int token_type, unsigned precedence)
3830 expression_parser_function_t *entry = &expression_parsers[token_type];
3832 if(entry->parser != NULL) {
3833 fprintf(stderr, "for token ");
3834 print_token_type(stderr, token_type);
3835 fprintf(stderr, "\n");
3836 panic("trying to register multiple expression parsers for a token");
3838 entry->parser = parser;
3839 entry->precedence = precedence;
3842 static void register_expression_infix_parser(
3843 parse_expression_infix_function parser, int token_type,
3844 unsigned precedence)
3846 expression_parser_function_t *entry = &expression_parsers[token_type];
3848 if(entry->infix_parser != NULL) {
3849 fprintf(stderr, "for token ");
3850 print_token_type(stderr, token_type);
3851 fprintf(stderr, "\n");
3852 panic("trying to register multiple infix expression parsers for a "
3855 entry->infix_parser = parser;
3856 entry->infix_precedence = precedence;
3859 static void init_expression_parsers(void)
3861 memset(&expression_parsers, 0, sizeof(expression_parsers));
3863 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3864 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3865 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3866 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3867 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3868 T_GREATERGREATER, 16);
3869 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3870 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3871 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3872 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3873 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3874 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3875 T_GREATEREQUAL, 14);
3876 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3877 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3878 T_EXCLAMATIONMARKEQUAL, 13);
3879 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3880 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3881 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3882 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3883 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3884 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3885 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3886 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3887 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3888 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3889 T_ASTERISKEQUAL, 2);
3890 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3891 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3893 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3894 T_LESSLESSEQUAL, 2);
3895 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3896 T_GREATERGREATEREQUAL, 2);
3897 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3899 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3901 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3904 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3906 register_expression_infix_parser(parse_array_expression, '[', 30);
3907 register_expression_infix_parser(parse_call_expression, '(', 30);
3908 register_expression_infix_parser(parse_select_expression, '.', 30);
3909 register_expression_infix_parser(parse_select_expression,
3910 T_MINUSGREATER, 30);
3911 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3913 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3916 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3917 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3918 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3919 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3920 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3921 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3922 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3923 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3924 register_expression_parser(parse_sizeof, T_sizeof, 25);
3925 register_expression_parser(parse_extension, T___extension__, 25);
3926 register_expression_parser(parse_builtin_classify_type,
3927 T___builtin_classify_type, 25);
3931 static statement_t *parse_case_statement(void)
3934 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3935 label->statement.type = STATEMENT_CASE_LABEL;
3936 label->statement.source_position = token.source_position;
3938 label->expression = parse_expression();
3941 label->label_statement = parse_statement();
3943 return (statement_t*) label;
3946 static statement_t *parse_default_statement(void)
3950 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3951 label->statement.type = STATEMENT_CASE_LABEL;
3952 label->statement.source_position = token.source_position;
3955 label->label_statement = parse_statement();
3957 return (statement_t*) label;
3960 static declaration_t *get_label(symbol_t *symbol)
3962 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3963 assert(current_function != NULL);
3964 /* if we found a label in the same function, then we already created the
3966 if(candidate != NULL
3967 && candidate->parent_context == ¤t_function->context) {
3971 /* otherwise we need to create a new one */
3972 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3973 declaration->namespc = NAMESPACE_LABEL;
3974 declaration->symbol = symbol;
3976 label_push(declaration);
3981 static statement_t *parse_label_statement(void)
3983 assert(token.type == T_IDENTIFIER);
3984 symbol_t *symbol = token.v.symbol;
3987 declaration_t *label = get_label(symbol);
3989 /* if source position is already set then the label is defined twice,
3990 * otherwise it was just mentioned in a goto so far */
3991 if(label->source_position.input_name != NULL) {
3992 parser_print_error_prefix();
3993 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3994 parser_print_error_prefix_pos(label->source_position);
3995 fprintf(stderr, "previous definition of '%s' was here\n",
3998 label->source_position = token.source_position;
4001 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4003 label_statement->statement.type = STATEMENT_LABEL;
4004 label_statement->statement.source_position = token.source_position;
4005 label_statement->label = label;
4009 if(token.type == '}') {
4010 parse_error("label at end of compound statement");
4011 return (statement_t*) label_statement;
4013 label_statement->label_statement = parse_statement();
4016 return (statement_t*) label_statement;
4019 static statement_t *parse_if(void)
4023 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4024 statement->statement.type = STATEMENT_IF;
4025 statement->statement.source_position = token.source_position;
4028 statement->condition = parse_expression();
4031 statement->true_statement = parse_statement();
4032 if(token.type == T_else) {
4034 statement->false_statement = parse_statement();
4037 return (statement_t*) statement;
4040 static statement_t *parse_switch(void)
4044 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4045 statement->statement.type = STATEMENT_SWITCH;
4046 statement->statement.source_position = token.source_position;
4049 statement->expression = parse_expression();
4051 statement->body = parse_statement();
4053 return (statement_t*) statement;
4056 static statement_t *parse_while(void)
4060 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4061 statement->statement.type = STATEMENT_WHILE;
4062 statement->statement.source_position = token.source_position;
4065 statement->condition = parse_expression();
4067 statement->body = parse_statement();
4069 return (statement_t*) statement;
4072 static statement_t *parse_do(void)
4076 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4077 statement->statement.type = STATEMENT_DO_WHILE;
4078 statement->statement.source_position = token.source_position;
4080 statement->body = parse_statement();
4083 statement->condition = parse_expression();
4087 return (statement_t*) statement;
4090 static statement_t *parse_for(void)
4094 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4095 statement->statement.type = STATEMENT_FOR;
4096 statement->statement.source_position = token.source_position;
4100 int top = environment_top();
4101 context_t *last_context = context;
4102 set_context(&statement->context);
4104 if(token.type != ';') {
4105 if(is_declaration_specifier(&token, false)) {
4106 parse_declaration();
4108 statement->initialisation = parse_expression();
4115 if(token.type != ';') {
4116 statement->condition = parse_expression();
4119 if(token.type != ')') {
4120 statement->step = parse_expression();
4123 statement->body = parse_statement();
4125 assert(context == &statement->context);
4126 set_context(last_context);
4127 environment_pop_to(top);
4129 return (statement_t*) statement;
4132 static statement_t *parse_goto(void)
4136 if(token.type != T_IDENTIFIER) {
4137 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4141 symbol_t *symbol = token.v.symbol;
4144 declaration_t *label = get_label(symbol);
4146 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4148 statement->statement.type = STATEMENT_GOTO;
4149 statement->statement.source_position = token.source_position;
4151 statement->label = label;
4155 return (statement_t*) statement;
4158 static statement_t *parse_continue(void)
4163 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4164 statement->type = STATEMENT_CONTINUE;
4165 statement->source_position = token.source_position;
4170 static statement_t *parse_break(void)
4175 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4176 statement->type = STATEMENT_BREAK;
4177 statement->source_position = token.source_position;
4182 static statement_t *parse_return(void)
4186 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4188 statement->statement.type = STATEMENT_RETURN;
4189 statement->statement.source_position = token.source_position;
4191 assert(current_function->type->type == TYPE_FUNCTION);
4192 function_type_t *function_type = (function_type_t*) current_function->type;
4193 type_t *return_type = function_type->result_type;
4195 expression_t *return_value;
4196 if(token.type != ';') {
4197 return_value = parse_expression();
4199 if(return_type == type_void && return_value->datatype != type_void) {
4200 parse_warning("'return' with a value, in function returning void");
4201 return_value = NULL;
4203 if(return_type != NULL) {
4204 semantic_assign(return_type, &return_value, "'return'");
4208 return_value = NULL;
4209 if(return_type != type_void) {
4210 parse_warning("'return' without value, in function returning "
4214 statement->return_value = return_value;
4218 return (statement_t*) statement;
4221 static statement_t *parse_declaration_statement(void)
4223 declaration_t *before = last_declaration;
4225 declaration_statement_t *statement
4226 = allocate_ast_zero(sizeof(statement[0]));
4227 statement->statement.type = STATEMENT_DECLARATION;
4228 statement->statement.source_position = token.source_position;
4230 declaration_specifiers_t specifiers;
4231 memset(&specifiers, 0, sizeof(specifiers));
4232 parse_declaration_specifiers(&specifiers);
4234 if(token.type == ';') {
4237 parse_init_declarators(&specifiers);
4240 if(before == NULL) {
4241 statement->declarations_begin = context->declarations;
4243 statement->declarations_begin = before->next;
4245 statement->declarations_end = last_declaration;
4247 return (statement_t*) statement;
4250 static statement_t *parse_expression_statement(void)
4252 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4253 statement->statement.type = STATEMENT_EXPRESSION;
4254 statement->statement.source_position = token.source_position;
4256 statement->expression = parse_expression();
4260 return (statement_t*) statement;
4263 static statement_t *parse_statement(void)
4265 statement_t *statement = NULL;
4267 /* declaration or statement */
4268 switch(token.type) {
4270 statement = parse_case_statement();
4274 statement = parse_default_statement();
4278 statement = parse_compound_statement();
4282 statement = parse_if();
4286 statement = parse_switch();
4290 statement = parse_while();
4294 statement = parse_do();
4298 statement = parse_for();
4302 statement = parse_goto();
4306 statement = parse_continue();
4310 statement = parse_break();
4314 statement = parse_return();
4323 if(look_ahead(1)->type == ':') {
4324 statement = parse_label_statement();
4328 if(is_typedef_symbol(token.v.symbol)) {
4329 statement = parse_declaration_statement();
4333 statement = parse_expression_statement();
4336 case T___extension__:
4337 /* this can be a prefix to a declaration or an expression statement */
4338 /* we simply eat it now and parse the rest with tail recursion */
4341 } while(token.type == T___extension__);
4342 statement = parse_statement();
4346 statement = parse_declaration_statement();
4350 statement = parse_expression_statement();
4354 assert(statement == NULL || statement->source_position.input_name != NULL);
4359 static statement_t *parse_compound_statement(void)
4361 compound_statement_t *compound_statement
4362 = allocate_ast_zero(sizeof(compound_statement[0]));
4363 compound_statement->statement.type = STATEMENT_COMPOUND;
4364 compound_statement->statement.source_position = token.source_position;
4368 int top = environment_top();
4369 context_t *last_context = context;
4370 set_context(&compound_statement->context);
4372 statement_t *last_statement = NULL;
4374 while(token.type != '}' && token.type != T_EOF) {
4375 statement_t *statement = parse_statement();
4376 if(statement == NULL)
4379 if(last_statement != NULL) {
4380 last_statement->next = statement;
4382 compound_statement->statements = statement;
4385 while(statement->next != NULL)
4386 statement = statement->next;
4388 last_statement = statement;
4391 if(token.type != '}') {
4392 parser_print_error_prefix_pos(
4393 compound_statement->statement.source_position);
4394 fprintf(stderr, "end of file while looking for closing '}'\n");
4398 assert(context == &compound_statement->context);
4399 set_context(last_context);
4400 environment_pop_to(top);
4402 return (statement_t*) compound_statement;
4405 static translation_unit_t *parse_translation_unit(void)
4407 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4409 assert(global_context == NULL);
4410 global_context = &unit->context;
4412 assert(context == NULL);
4413 set_context(&unit->context);
4415 while(token.type != T_EOF) {
4416 parse_declaration();
4419 assert(context == &unit->context);
4421 last_declaration = NULL;
4423 assert(global_context == &unit->context);
4424 global_context = NULL;
4429 translation_unit_t *parse(void)
4431 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4432 label_stack = NEW_ARR_F(stack_entry_t, 0);
4433 found_error = false;
4435 type_set_output(stderr);
4436 ast_set_output(stderr);
4438 lookahead_bufpos = 0;
4439 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4442 translation_unit_t *unit = parse_translation_unit();
4444 DEL_ARR_F(environment_stack);
4445 DEL_ARR_F(label_stack);
4453 void init_parser(void)
4455 init_expression_parsers();
4456 obstack_init(&temp_obst);
4458 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4459 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4460 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4461 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4462 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4463 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4464 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4465 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4466 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4467 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4468 type_string = make_pointer_type(type_const_char, TYPE_QUALIFIER_NONE);
4471 void exit_parser(void)
4473 obstack_free(&temp_obst, NULL);