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 if(!is_atomic_type(points_to_left, ATOMIC_TYPE_VOID)
781 && !is_atomic_type(points_to_right, ATOMIC_TYPE_VOID)
782 && !types_compatible(points_to_left, points_to_right)) {
783 goto incompatible_assign_types;
786 /* the left type has all qualifiers from the right type */
787 unsigned missing_qualifiers
788 = points_to_right->qualifiers & ~points_to_left->qualifiers;
789 if(missing_qualifiers != 0) {
790 parser_print_error_prefix();
791 fprintf(stderr, "destination type ");
792 print_type_quoted(type_left);
793 fprintf(stderr, " in %s from type ", context);
794 print_type_quoted(type_right);
795 fprintf(stderr, " lacks qualifiers '");
796 print_type_qualifiers(missing_qualifiers);
797 fprintf(stderr, "' in pointed-to type\n");
801 *right = create_implicit_cast(*right, type_left);
805 if (is_compound_type(type_left)
806 && types_compatible(type_left, type_right)) {
807 *right = create_implicit_cast(*right, type_left);
811 incompatible_assign_types:
812 /* TODO: improve error message */
813 parser_print_error_prefix();
814 fprintf(stderr, "incompatible types in %s\n", context);
815 parser_print_error_prefix();
816 print_type_quoted(type_left);
817 fputs(" <- ", stderr);
818 print_type_quoted(type_right);
822 static expression_t *parse_constant_expression(void)
824 /* start parsing at precedence 7 (conditional expression) */
825 return parse_sub_expression(7);
828 static expression_t *parse_assignment_expression(void)
830 /* start parsing at precedence 2 (assignment expression) */
831 return parse_sub_expression(2);
834 typedef struct declaration_specifiers_t declaration_specifiers_t;
835 struct declaration_specifiers_t {
836 storage_class_t storage_class;
841 static void parse_compound_type_entries(void);
842 static declaration_t *parse_declarator(
843 const declaration_specifiers_t *specifiers, type_t *type,
844 bool may_be_abstract);
845 static declaration_t *record_declaration(declaration_t *declaration);
847 static const char *parse_string_literals(void)
849 assert(token.type == T_STRING_LITERAL);
850 const char *result = token.v.string;
854 while(token.type == T_STRING_LITERAL) {
855 result = concat_strings(result, token.v.string);
862 static void parse_attributes(void)
866 case T___attribute__:
874 parse_error("EOF while parsing attribute");
892 if(token.type != T_STRING_LITERAL) {
893 parse_error_expected("while parsing assembler attribute",
898 parse_string_literals();
903 goto attributes_finished;
912 static designator_t *parse_designation(void)
914 if(token.type != '[' && token.type != '.')
917 designator_t *result = NULL;
918 designator_t *last = NULL;
921 designator_t *designator;
924 designator = allocate_ast_zero(sizeof(designator[0]));
926 designator->array_access = parse_constant_expression();
930 designator = allocate_ast_zero(sizeof(designator[0]));
932 if(token.type != T_IDENTIFIER) {
933 parse_error_expected("while parsing designator",
937 designator->symbol = token.v.symbol;
945 assert(designator != NULL);
947 last->next = designator;
956 static initializer_t *initializer_from_string(array_type_t *type,
959 /* TODO: check len vs. size of array type */
962 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
963 initializer->string.string = string;
968 static initializer_t *initializer_from_expression(type_t *type,
969 expression_t *expression)
972 /* TODO check that expression is a constant expression */
974 /* § 6.7.8.14/15 char array may be initialized by string literals */
975 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
976 array_type_t *array_type = (array_type_t*) type;
977 type_t *element_type = array_type->element_type;
979 if(element_type->type == TYPE_ATOMIC) {
980 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
981 atomic_type_type_t atype = atomic_type->atype;
983 /* TODO handle wide strings */
984 if(atype == ATOMIC_TYPE_CHAR
985 || atype == ATOMIC_TYPE_SCHAR
986 || atype == ATOMIC_TYPE_UCHAR) {
988 string_literal_t *literal = (string_literal_t*) expression;
989 return initializer_from_string(array_type, literal->value);
994 semantic_assign(type, &expression, "initializer");
996 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
997 result->value.value = expression;
1002 static initializer_t *parse_sub_initializer(type_t *type,
1003 expression_t *expression,
1004 type_t *expression_type);
1006 static initializer_t *parse_sub_initializer_elem(type_t *type)
1008 if(token.type == '{') {
1009 return parse_sub_initializer(type, NULL, NULL);
1012 expression_t *expression = parse_assignment_expression();
1013 type_t *expression_type = skip_typeref(expression->datatype);
1015 return parse_sub_initializer(type, expression, expression_type);
1018 static bool had_initializer_brace_warning;
1020 static initializer_t *parse_sub_initializer(type_t *type,
1021 expression_t *expression,
1022 type_t *expression_type)
1024 if(is_type_scalar(type)) {
1025 /* there might be extra {} hierarchies */
1026 if(token.type == '{') {
1028 if(!had_initializer_brace_warning) {
1029 parse_warning("braces around scalar initializer");
1030 had_initializer_brace_warning = true;
1032 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1033 if(token.type == ',') {
1035 /* TODO: warn about excessive elements */
1041 if(expression == NULL) {
1042 expression = parse_assignment_expression();
1044 return initializer_from_expression(type, expression);
1047 /* TODO: ignore qualifiers, comparing pointers is probably
1049 if(expression != NULL && expression_type == type) {
1050 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1053 semantic_assign(type, &expression, "initializer");
1055 result->value.value = expression;
1060 bool read_paren = false;
1061 if(token.type == '{') {
1066 /* descend into subtype */
1067 initializer_t *result = NULL;
1068 initializer_t **elems;
1069 if(type->type == TYPE_ARRAY) {
1070 array_type_t *array_type = (array_type_t*) type;
1071 type_t *element_type = array_type->element_type;
1072 element_type = skip_typeref(element_type);
1075 had_initializer_brace_warning = false;
1076 if(expression == NULL) {
1077 sub = parse_sub_initializer_elem(element_type);
1079 sub = parse_sub_initializer(element_type, expression,
1083 /* didn't match the subtypes -> try the parent type */
1085 assert(!read_paren);
1089 elems = NEW_ARR_F(initializer_t*, 0);
1090 ARR_APP1(initializer_t*, elems, sub);
1093 if(token.type == '}')
1096 if(token.type == '}')
1100 = parse_sub_initializer(element_type, NULL, NULL);
1102 /* TODO error, do nicer cleanup */
1103 parse_error("member initializer didn't match");
1107 ARR_APP1(initializer_t*, elems, sub);
1110 assert(type->type == TYPE_COMPOUND_STRUCT
1111 || type->type == TYPE_COMPOUND_UNION);
1112 compound_type_t *compound_type = (compound_type_t*) type;
1113 context_t *context = & compound_type->declaration->context;
1115 declaration_t *first = context->declarations;
1118 type_t *first_type = first->type;
1119 first_type = skip_typeref(first_type);
1122 had_initializer_brace_warning = false;
1123 if(expression == NULL) {
1124 sub = parse_sub_initializer_elem(first_type);
1126 sub = parse_sub_initializer(first_type, expression,expression_type);
1129 /* didn't match the subtypes -> try our parent type */
1131 assert(!read_paren);
1135 elems = NEW_ARR_F(initializer_t*, 0);
1136 ARR_APP1(initializer_t*, elems, sub);
1138 declaration_t *iter = first->next;
1139 for( ; iter != NULL; iter = iter->next) {
1140 if(iter->symbol == NULL)
1142 if(iter->namespc != NAMESPACE_NORMAL)
1145 if(token.type == '}')
1149 type_t *iter_type = iter->type;
1150 iter_type = skip_typeref(iter_type);
1152 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1154 /* TODO error, do nicer cleanup*/
1155 parse_error("member initializer didn't match");
1159 ARR_APP1(initializer_t*, elems, sub);
1163 int len = ARR_LEN(elems);
1164 size_t elems_size = sizeof(initializer_t*) * len;
1166 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1168 init->initializer.type = INITIALIZER_LIST;
1170 memcpy(init->initializers, elems, elems_size);
1173 result = (initializer_t*) init;
1176 if(token.type == ',')
1183 static initializer_t *parse_initializer(type_t *type)
1185 initializer_t *result;
1187 type = skip_typeref(type);
1189 if(token.type != '{') {
1190 expression_t *expression = parse_assignment_expression();
1191 return initializer_from_expression(type, expression);
1194 if(is_type_scalar(type)) {
1198 expression_t *expression = parse_assignment_expression();
1199 result = initializer_from_expression(type, expression);
1201 if(token.type == ',')
1207 result = parse_sub_initializer(type, NULL, NULL);
1215 static declaration_t *parse_compound_type_specifier(bool is_struct)
1223 symbol_t *symbol = NULL;
1224 declaration_t *declaration = NULL;
1226 if (token.type == T___attribute__) {
1231 if(token.type == T_IDENTIFIER) {
1232 symbol = token.v.symbol;
1236 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1238 declaration = get_declaration(symbol, NAMESPACE_UNION);
1240 } else if(token.type != '{') {
1242 parse_error_expected("while parsing struct type specifier",
1243 T_IDENTIFIER, '{', 0);
1245 parse_error_expected("while parsing union type specifier",
1246 T_IDENTIFIER, '{', 0);
1252 if(declaration == NULL) {
1253 declaration = allocate_type_zero(sizeof(declaration[0]));
1256 declaration->namespc = NAMESPACE_STRUCT;
1258 declaration->namespc = NAMESPACE_UNION;
1260 declaration->source_position = token.source_position;
1261 declaration->symbol = symbol;
1262 record_declaration(declaration);
1265 if(token.type == '{') {
1266 if(declaration->init.is_defined) {
1267 assert(symbol != NULL);
1268 parser_print_error_prefix();
1269 fprintf(stderr, "multiple definition of %s %s\n",
1270 is_struct ? "struct" : "union", symbol->string);
1271 declaration->context.declarations = NULL;
1273 declaration->init.is_defined = true;
1275 int top = environment_top();
1276 context_t *last_context = context;
1277 set_context(& declaration->context);
1279 parse_compound_type_entries();
1282 assert(context == & declaration->context);
1283 set_context(last_context);
1284 environment_pop_to(top);
1290 static void parse_enum_entries(enum_type_t *const enum_type)
1294 if(token.type == '}') {
1296 parse_error("empty enum not allowed");
1301 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1303 if(token.type != T_IDENTIFIER) {
1304 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1308 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1309 entry->type = (type_t*) enum_type;
1310 entry->symbol = token.v.symbol;
1311 entry->source_position = token.source_position;
1314 if(token.type == '=') {
1316 entry->init.enum_value = parse_constant_expression();
1321 record_declaration(entry);
1323 if(token.type != ',')
1326 } while(token.type != '}');
1331 static type_t *parse_enum_specifier(void)
1335 declaration_t *declaration;
1338 if(token.type == T_IDENTIFIER) {
1339 symbol = token.v.symbol;
1342 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1343 } else if(token.type != '{') {
1344 parse_error_expected("while parsing enum type specifier",
1345 T_IDENTIFIER, '{', 0);
1352 if(declaration == NULL) {
1353 declaration = allocate_type_zero(sizeof(declaration[0]));
1355 declaration->namespc = NAMESPACE_ENUM;
1356 declaration->source_position = token.source_position;
1357 declaration->symbol = symbol;
1360 enum_type_t *const enum_type = allocate_type_zero(sizeof(enum_type[0]));
1361 enum_type->type.type = TYPE_ENUM;
1362 enum_type->declaration = declaration;
1364 if(token.type == '{') {
1365 if(declaration->init.is_defined) {
1366 parser_print_error_prefix();
1367 fprintf(stderr, "multiple definitions of enum %s\n",
1370 record_declaration(declaration);
1371 declaration->init.is_defined = 1;
1373 parse_enum_entries(enum_type);
1377 return (type_t*) enum_type;
1381 * if a symbol is a typedef to another type, return true
1383 static bool is_typedef_symbol(symbol_t *symbol)
1385 const declaration_t *const declaration =
1386 get_declaration(symbol, NAMESPACE_NORMAL);
1388 declaration != NULL &&
1389 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1392 static type_t *parse_typeof(void)
1400 expression_t *expression = NULL;
1403 switch(token.type) {
1404 case T___extension__:
1405 /* this can be a prefix to a typename or an expression */
1406 /* we simply eat it now. */
1409 } while(token.type == T___extension__);
1413 if(is_typedef_symbol(token.v.symbol)) {
1414 type = parse_typename();
1416 expression = parse_expression();
1417 type = expression->datatype;
1422 type = parse_typename();
1426 expression = parse_expression();
1427 type = expression->datatype;
1433 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1434 typeof->type.type = TYPE_TYPEOF;
1435 typeof->expression = expression;
1436 typeof->typeof_type = type;
1438 return (type_t*) typeof;
1442 SPECIFIER_SIGNED = 1 << 0,
1443 SPECIFIER_UNSIGNED = 1 << 1,
1444 SPECIFIER_LONG = 1 << 2,
1445 SPECIFIER_INT = 1 << 3,
1446 SPECIFIER_DOUBLE = 1 << 4,
1447 SPECIFIER_CHAR = 1 << 5,
1448 SPECIFIER_SHORT = 1 << 6,
1449 SPECIFIER_LONG_LONG = 1 << 7,
1450 SPECIFIER_FLOAT = 1 << 8,
1451 SPECIFIER_BOOL = 1 << 9,
1452 SPECIFIER_VOID = 1 << 10,
1453 #ifdef PROVIDE_COMPLEX
1454 SPECIFIER_COMPLEX = 1 << 11,
1455 SPECIFIER_IMAGINARY = 1 << 12,
1459 static type_t *create_builtin_type(symbol_t *symbol)
1461 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1462 type->type.type = TYPE_BUILTIN;
1463 type->symbol = symbol;
1465 type->real_type = type_int;
1467 return (type_t*) type;
1470 static type_t *get_typedef_type(symbol_t *symbol)
1472 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1473 if(declaration == NULL
1474 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1477 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1478 typedef_type->type.type = TYPE_TYPEDEF;
1479 typedef_type->declaration = declaration;
1481 return (type_t*) typedef_type;
1484 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1486 type_t *type = NULL;
1487 unsigned type_qualifiers = 0;
1488 unsigned type_specifiers = 0;
1492 switch(token.type) {
1495 #define MATCH_STORAGE_CLASS(token, class) \
1497 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1498 parse_error("multiple storage classes in declaration " \
1501 specifiers->storage_class = class; \
1505 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1506 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1507 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1508 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1509 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1511 /* type qualifiers */
1512 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1514 type_qualifiers |= qualifier; \
1518 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1519 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1520 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1522 case T___extension__:
1527 /* type specifiers */
1528 #define MATCH_SPECIFIER(token, specifier, name) \
1531 if(type_specifiers & specifier) { \
1532 parse_error("multiple " name " type specifiers given"); \
1534 type_specifiers |= specifier; \
1538 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1539 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1540 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1541 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1542 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1543 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1544 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1545 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1546 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1547 #ifdef PROVIDE_COMPLEX
1548 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1549 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1553 specifiers->is_inline = true;
1558 if(type_specifiers & SPECIFIER_LONG_LONG) {
1559 parse_error("multiple type specifiers given");
1560 } else if(type_specifiers & SPECIFIER_LONG) {
1561 type_specifiers |= SPECIFIER_LONG_LONG;
1563 type_specifiers |= SPECIFIER_LONG;
1567 /* TODO: if type != NULL for the following rules should issue
1570 type = allocate_type_zero(sizeof(struct compound_type_t));
1572 compound_type_t *compound_type = (compound_type_t*) type;
1573 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1574 compound_type->declaration = parse_compound_type_specifier(true);
1578 type = allocate_type_zero(sizeof(compound_type_t));
1580 compound_type_t *compound_type = (compound_type_t*) type;
1581 compound_type->type.type = TYPE_COMPOUND_UNION;
1582 compound_type->declaration = parse_compound_type_specifier(false);
1586 type = parse_enum_specifier();
1589 type = parse_typeof();
1591 case T___builtin_va_list:
1592 type = create_builtin_type(token.v.symbol);
1596 case T___attribute__:
1601 case T_IDENTIFIER: {
1602 type_t *typedef_type = get_typedef_type(token.v.symbol);
1604 if(typedef_type == NULL)
1605 goto finish_specifiers;
1608 type = typedef_type;
1612 /* function specifier */
1614 goto finish_specifiers;
1621 atomic_type_type_t atomic_type;
1623 /* match valid basic types */
1624 switch(type_specifiers) {
1625 case SPECIFIER_VOID:
1626 atomic_type = ATOMIC_TYPE_VOID;
1628 case SPECIFIER_CHAR:
1629 atomic_type = ATOMIC_TYPE_CHAR;
1631 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1632 atomic_type = ATOMIC_TYPE_SCHAR;
1634 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1635 atomic_type = ATOMIC_TYPE_UCHAR;
1637 case SPECIFIER_SHORT:
1638 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1639 case SPECIFIER_SHORT | SPECIFIER_INT:
1640 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1641 atomic_type = ATOMIC_TYPE_SHORT;
1643 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1644 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1645 atomic_type = ATOMIC_TYPE_USHORT;
1648 case SPECIFIER_SIGNED:
1649 case SPECIFIER_SIGNED | SPECIFIER_INT:
1650 atomic_type = ATOMIC_TYPE_INT;
1652 case SPECIFIER_UNSIGNED:
1653 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1654 atomic_type = ATOMIC_TYPE_UINT;
1656 case SPECIFIER_LONG:
1657 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1658 case SPECIFIER_LONG | SPECIFIER_INT:
1659 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1660 atomic_type = ATOMIC_TYPE_LONG;
1662 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1663 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1664 atomic_type = ATOMIC_TYPE_ULONG;
1666 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1667 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1668 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1669 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1671 atomic_type = ATOMIC_TYPE_LONGLONG;
1673 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1674 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1676 atomic_type = ATOMIC_TYPE_ULONGLONG;
1678 case SPECIFIER_FLOAT:
1679 atomic_type = ATOMIC_TYPE_FLOAT;
1681 case SPECIFIER_DOUBLE:
1682 atomic_type = ATOMIC_TYPE_DOUBLE;
1684 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1685 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1687 case SPECIFIER_BOOL:
1688 atomic_type = ATOMIC_TYPE_BOOL;
1690 #ifdef PROVIDE_COMPLEX
1691 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1692 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1694 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1695 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1697 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1698 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1700 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1701 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1703 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1704 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1706 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1707 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1711 /* invalid specifier combination, give an error message */
1712 if(type_specifiers == 0) {
1714 parse_warning("no type specifiers in declaration (using int)");
1715 atomic_type = ATOMIC_TYPE_INT;
1718 parse_error("no type specifiers given in declaration");
1720 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1721 (type_specifiers & SPECIFIER_UNSIGNED)) {
1722 parse_error("signed and unsigned specifiers gives");
1723 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1724 parse_error("only integer types can be signed or unsigned");
1726 parse_error("multiple datatypes in declaration");
1728 atomic_type = ATOMIC_TYPE_INVALID;
1731 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1732 atype->type.type = TYPE_ATOMIC;
1733 atype->atype = atomic_type;
1736 type = (type_t*) atype;
1738 if(type_specifiers != 0) {
1739 parse_error("multiple datatypes in declaration");
1743 type->qualifiers = (type_qualifier_t)type_qualifiers;
1745 type_t *result = typehash_insert(type);
1746 if(newtype && result != (type_t*) type) {
1750 specifiers->type = result;
1753 static type_qualifiers_t parse_type_qualifiers(void)
1755 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1758 switch(token.type) {
1759 /* type qualifiers */
1760 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1761 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1762 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1765 return type_qualifiers;
1770 static void parse_identifier_list(void)
1773 if(token.type != T_IDENTIFIER) {
1774 parse_error_expected("while parsing parameter identifier list",
1779 if(token.type != ',')
1785 static declaration_t *parse_parameter(void)
1787 declaration_specifiers_t specifiers;
1788 memset(&specifiers, 0, sizeof(specifiers));
1790 parse_declaration_specifiers(&specifiers);
1792 declaration_t *declaration
1793 = parse_declarator(&specifiers, specifiers.type, true);
1795 /* TODO check declaration constraints for parameters */
1796 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1797 parse_error("typedef not allowed in parameter list");
1800 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1802 if (declaration->type->type == TYPE_ARRAY) {
1803 const array_type_t *const arr_type =
1804 (const array_type_t*)declaration->type;
1806 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1812 static declaration_t *parse_parameters(function_type_t *type)
1814 if(token.type == T_IDENTIFIER) {
1815 symbol_t *symbol = token.v.symbol;
1816 if(!is_typedef_symbol(symbol)) {
1817 /* TODO: K&R style C parameters */
1818 parse_identifier_list();
1823 if(token.type == ')') {
1824 type->unspecified_parameters = 1;
1827 if(token.type == T_void && look_ahead(1)->type == ')') {
1832 declaration_t *declarations = NULL;
1833 declaration_t *declaration;
1834 declaration_t *last_declaration = NULL;
1835 function_parameter_t *parameter;
1836 function_parameter_t *last_parameter = NULL;
1839 switch(token.type) {
1843 return declarations;
1846 case T___extension__:
1848 declaration = parse_parameter();
1850 parameter = allocate_type_zero(sizeof(parameter[0]));
1851 parameter->type = declaration->type;
1853 if(last_parameter != NULL) {
1854 last_declaration->next = declaration;
1855 last_parameter->next = parameter;
1857 type->parameters = parameter;
1858 declarations = declaration;
1860 last_parameter = parameter;
1861 last_declaration = declaration;
1865 return declarations;
1867 if(token.type != ',')
1868 return declarations;
1878 } construct_type_type_t;
1880 typedef struct construct_type_t construct_type_t;
1881 struct construct_type_t {
1882 construct_type_type_t type;
1883 construct_type_t *next;
1886 typedef struct parsed_pointer_t parsed_pointer_t;
1887 struct parsed_pointer_t {
1888 construct_type_t construct_type;
1889 type_qualifiers_t type_qualifiers;
1892 typedef struct construct_function_type_t construct_function_type_t;
1893 struct construct_function_type_t {
1894 construct_type_t construct_type;
1895 function_type_t *function_type;
1898 typedef struct parsed_array_t parsed_array_t;
1899 struct parsed_array_t {
1900 construct_type_t construct_type;
1901 type_qualifiers_t type_qualifiers;
1907 typedef struct construct_base_type_t construct_base_type_t;
1908 struct construct_base_type_t {
1909 construct_type_t construct_type;
1913 static construct_type_t *parse_pointer_declarator(void)
1917 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1918 memset(pointer, 0, sizeof(pointer[0]));
1919 pointer->construct_type.type = CONSTRUCT_POINTER;
1920 pointer->type_qualifiers = parse_type_qualifiers();
1922 return (construct_type_t*) pointer;
1925 static construct_type_t *parse_array_declarator(void)
1929 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1930 memset(array, 0, sizeof(array[0]));
1931 array->construct_type.type = CONSTRUCT_ARRAY;
1933 if(token.type == T_static) {
1934 array->is_static = true;
1938 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1939 if(type_qualifiers != 0) {
1940 if(token.type == T_static) {
1941 array->is_static = true;
1945 array->type_qualifiers = type_qualifiers;
1947 if(token.type == '*' && look_ahead(1)->type == ']') {
1948 array->is_variable = true;
1950 } else if(token.type != ']') {
1951 array->size = parse_assignment_expression();
1956 return (construct_type_t*) array;
1959 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1963 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1964 type->type.type = TYPE_FUNCTION;
1966 declaration_t *parameters = parse_parameters(type);
1967 if(declaration != NULL) {
1968 declaration->context.declarations = parameters;
1971 construct_function_type_t *construct_function_type =
1972 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1973 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1974 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1975 construct_function_type->function_type = type;
1979 return (construct_type_t*) construct_function_type;
1982 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1983 bool may_be_abstract)
1985 /* construct a single linked list of construct_type_t's which describe
1986 * how to construct the final declarator type */
1987 construct_type_t *first = NULL;
1988 construct_type_t *last = NULL;
1991 while(token.type == '*') {
1992 construct_type_t *type = parse_pointer_declarator();
2003 /* TODO: find out if this is correct */
2006 construct_type_t *inner_types = NULL;
2008 switch(token.type) {
2010 if(declaration == NULL) {
2011 parse_error("no identifier expected in typename");
2013 declaration->symbol = token.v.symbol;
2014 declaration->source_position = token.source_position;
2020 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2026 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2027 /* avoid a loop in the outermost scope, because eat_statement doesn't
2029 if(token.type == '}' && current_function == NULL) {
2037 construct_type_t *p = last;
2040 construct_type_t *type;
2041 switch(token.type) {
2043 type = parse_function_declarator(declaration);
2046 type = parse_array_declarator();
2049 goto declarator_finished;
2052 /* insert in the middle of the list (behind p) */
2054 type->next = p->next;
2065 declarator_finished:
2068 /* append inner_types at the end of the list, we don't to set last anymore
2069 * as it's not needed anymore */
2071 assert(first == NULL);
2072 first = inner_types;
2074 last->next = inner_types;
2080 static type_t *construct_declarator_type(construct_type_t *construct_list,
2083 construct_type_t *iter = construct_list;
2084 for( ; iter != NULL; iter = iter->next) {
2085 parsed_pointer_t *parsed_pointer;
2086 parsed_array_t *parsed_array;
2087 construct_function_type_t *construct_function_type;
2088 function_type_t *function_type;
2089 pointer_type_t *pointer_type;
2090 array_type_t *array_type;
2092 switch(iter->type) {
2093 case CONSTRUCT_INVALID:
2094 panic("invalid type construction found");
2095 case CONSTRUCT_FUNCTION:
2096 construct_function_type = (construct_function_type_t*) iter;
2097 function_type = construct_function_type->function_type;
2099 function_type->result_type = type;
2100 type = (type_t*) function_type;
2103 case CONSTRUCT_POINTER:
2104 parsed_pointer = (parsed_pointer_t*) iter;
2105 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2107 pointer_type->type.type = TYPE_POINTER;
2108 pointer_type->points_to = type;
2109 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2110 type = (type_t*) pointer_type;
2113 case CONSTRUCT_ARRAY:
2114 parsed_array = (parsed_array_t*) iter;
2115 array_type = allocate_type_zero(sizeof(array_type[0]));
2117 array_type->type.type = TYPE_ARRAY;
2118 array_type->element_type = type;
2119 array_type->type.qualifiers = parsed_array->type_qualifiers;
2120 array_type->is_static = parsed_array->is_static;
2121 array_type->is_variable = parsed_array->is_variable;
2122 array_type->size = parsed_array->size;
2123 type = (type_t*) array_type;
2127 type_t *hashed_type = typehash_insert((type_t*) type);
2128 if(hashed_type != type) {
2129 /* the function type was constructed earlier freeing it here will
2130 * destroy other types... */
2131 if(iter->type != CONSTRUCT_FUNCTION) {
2141 static declaration_t *parse_declarator(
2142 const declaration_specifiers_t *specifiers,
2143 type_t *type, bool may_be_abstract)
2145 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2146 declaration->storage_class = specifiers->storage_class;
2147 declaration->is_inline = specifiers->is_inline;
2149 construct_type_t *construct_type
2150 = parse_inner_declarator(declaration, may_be_abstract);
2151 declaration->type = construct_declarator_type(construct_type, type);
2153 if(construct_type != NULL) {
2154 obstack_free(&temp_obst, construct_type);
2160 static type_t *parse_abstract_declarator(type_t *base_type)
2162 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2164 type_t *result = construct_declarator_type(construct_type, base_type);
2165 if(construct_type != NULL) {
2166 obstack_free(&temp_obst, construct_type);
2172 static declaration_t *record_declaration(declaration_t *declaration)
2174 assert(context != NULL);
2176 symbol_t *symbol = declaration->symbol;
2177 if(symbol != NULL) {
2178 declaration_t *alias = environment_push(declaration);
2179 if(alias != declaration)
2182 declaration->parent_context = context;
2185 if(last_declaration != NULL) {
2186 last_declaration->next = declaration;
2188 context->declarations = declaration;
2190 last_declaration = declaration;
2195 static void parser_error_multiple_definition(declaration_t *previous,
2196 declaration_t *declaration)
2198 parser_print_error_prefix_pos(declaration->source_position);
2199 fprintf(stderr, "multiple definition of symbol '%s'\n",
2200 declaration->symbol->string);
2201 parser_print_error_prefix_pos(previous->source_position);
2202 fprintf(stderr, "this is the location of the previous definition.\n");
2205 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2208 declaration_t *ndeclaration
2209 = parse_declarator(specifiers, specifiers->type, false);
2211 declaration_t *declaration = record_declaration(ndeclaration);
2213 type_t *orig_type = declaration->type;
2214 type_t *type = skip_typeref(orig_type);
2215 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2216 parser_print_warning_prefix_pos(declaration->source_position);
2217 fprintf(stderr, "variable '%s' declared 'inline'\n",
2218 declaration->symbol->string);
2221 if(token.type == '=') {
2224 /* TODO: check that this is an allowed type (no function type) */
2226 if(declaration->init.initializer != NULL) {
2227 parser_error_multiple_definition(declaration, ndeclaration);
2230 initializer_t *initializer = parse_initializer(type);
2232 if(type->type == TYPE_ARRAY && initializer != NULL) {
2233 array_type_t *array_type = (array_type_t*) type;
2235 if(array_type->size == NULL) {
2236 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2238 cnst->expression.type = EXPR_CONST;
2239 cnst->expression.datatype = type_size_t;
2241 if(initializer->type == INITIALIZER_LIST) {
2242 initializer_list_t *list = &initializer->list;
2243 cnst->v.int_value = list->len;
2245 assert(initializer->type == INITIALIZER_STRING);
2246 initializer_string_t *string = &initializer->string;
2247 cnst->v.int_value = strlen(string->string) + 1;
2250 array_type->size = (expression_t*) cnst;
2255 ndeclaration->init.initializer = initializer;
2256 } else if(token.type == '{') {
2257 if(type->type != TYPE_FUNCTION) {
2258 parser_print_error_prefix();
2259 fprintf(stderr, "declarator '");
2260 print_type_ext(orig_type, declaration->symbol, NULL);
2261 fprintf(stderr, "' has a body but is not a function type.\n");
2266 if(declaration->init.statement != NULL) {
2267 parser_error_multiple_definition(declaration, ndeclaration);
2269 if(ndeclaration != declaration) {
2270 memcpy(&declaration->context, &ndeclaration->context,
2271 sizeof(declaration->context));
2274 int top = environment_top();
2275 context_t *last_context = context;
2276 set_context(&declaration->context);
2278 /* push function parameters */
2279 declaration_t *parameter = declaration->context.declarations;
2280 for( ; parameter != NULL; parameter = parameter->next) {
2281 environment_push(parameter);
2284 int label_stack_top = label_top();
2285 declaration_t *old_current_function = current_function;
2286 current_function = declaration;
2288 statement_t *statement = parse_compound_statement();
2290 assert(current_function == declaration);
2291 current_function = old_current_function;
2292 label_pop_to(label_stack_top);
2294 assert(context == &declaration->context);
2295 set_context(last_context);
2296 environment_pop_to(top);
2298 declaration->init.statement = statement;
2302 if(token.type != ',')
2309 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2312 if(token.type == ':') {
2314 parse_constant_expression();
2315 /* TODO (bitfields) */
2317 declaration_t *declaration
2318 = parse_declarator(specifiers, specifiers->type, true);
2320 /* TODO: check constraints for struct declarations */
2321 /* TODO: check for doubled fields */
2322 record_declaration(declaration);
2324 if(token.type == ':') {
2326 parse_constant_expression();
2327 /* TODO (bitfields) */
2331 if(token.type != ',')
2338 static void parse_compound_type_entries(void)
2342 while(token.type != '}' && token.type != T_EOF) {
2343 declaration_specifiers_t specifiers;
2344 memset(&specifiers, 0, sizeof(specifiers));
2345 parse_declaration_specifiers(&specifiers);
2347 parse_struct_declarators(&specifiers);
2349 if(token.type == T_EOF) {
2350 parse_error("unexpected error while parsing struct");
2355 static void parse_declaration(void)
2357 source_position_t source_position = token.source_position;
2359 declaration_specifiers_t specifiers;
2360 memset(&specifiers, 0, sizeof(specifiers));
2361 parse_declaration_specifiers(&specifiers);
2363 if(token.type == ';') {
2364 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2365 parse_warning_pos(source_position,
2366 "useless keyword in empty declaration");
2368 switch (specifiers.type->type) {
2369 case TYPE_COMPOUND_STRUCT:
2370 case TYPE_COMPOUND_UNION: {
2371 const compound_type_t *const comp_type =
2372 (const compound_type_t*)specifiers.type;
2373 if (comp_type->declaration->symbol == NULL) {
2374 parse_warning_pos(source_position,
2375 "unnamed struct/union that defines no instances");
2380 case TYPE_ENUM: break;
2383 parse_warning_pos(source_position, "empty declaration");
2389 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2391 declaration->type = specifiers.type;
2392 declaration->storage_class = specifiers.storage_class;
2393 declaration->source_position = source_position;
2394 record_declaration(declaration);
2397 parse_init_declarators(&specifiers);
2400 static type_t *parse_typename(void)
2402 declaration_specifiers_t specifiers;
2403 memset(&specifiers, 0, sizeof(specifiers));
2404 parse_declaration_specifiers(&specifiers);
2405 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2406 /* TODO: improve error message, user does probably not know what a
2407 * storage class is...
2409 parse_error("typename may not have a storage class");
2412 type_t *result = parse_abstract_declarator(specifiers.type);
2420 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2421 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2422 expression_t *left);
2424 typedef struct expression_parser_function_t expression_parser_function_t;
2425 struct expression_parser_function_t {
2426 unsigned precedence;
2427 parse_expression_function parser;
2428 unsigned infix_precedence;
2429 parse_expression_infix_function infix_parser;
2432 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2434 static expression_t *make_invalid_expression(void)
2436 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2437 expression->type = EXPR_INVALID;
2438 expression->source_position = token.source_position;
2442 static expression_t *expected_expression_error(void)
2444 parser_print_error_prefix();
2445 fprintf(stderr, "expected expression, got token ");
2446 print_token(stderr, & token);
2447 fprintf(stderr, "\n");
2451 return make_invalid_expression();
2454 static expression_t *parse_string_const(void)
2456 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2458 cnst->expression.type = EXPR_STRING_LITERAL;
2459 cnst->expression.datatype = type_string;
2460 cnst->value = parse_string_literals();
2462 return (expression_t*) cnst;
2465 static expression_t *parse_int_const(void)
2467 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2469 cnst->expression.type = EXPR_CONST;
2470 cnst->expression.datatype = token.datatype;
2471 cnst->v.int_value = token.v.intvalue;
2475 return (expression_t*) cnst;
2478 static expression_t *parse_float_const(void)
2480 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2482 cnst->expression.type = EXPR_CONST;
2483 cnst->expression.datatype = token.datatype;
2484 cnst->v.float_value = token.v.floatvalue;
2488 return (expression_t*) cnst;
2491 static declaration_t *create_implicit_function(symbol_t *symbol,
2492 const source_position_t source_position)
2494 function_type_t *function_type
2495 = allocate_type_zero(sizeof(function_type[0]));
2497 function_type->type.type = TYPE_FUNCTION;
2498 function_type->result_type = type_int;
2499 function_type->unspecified_parameters = true;
2501 type_t *type = typehash_insert((type_t*) function_type);
2502 if(type != (type_t*) function_type) {
2503 free_type(function_type);
2506 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2508 declaration->storage_class = STORAGE_CLASS_EXTERN;
2509 declaration->type = type;
2510 declaration->symbol = symbol;
2511 declaration->source_position = source_position;
2513 /* prepend the implicit definition to the global context
2514 * this is safe since the symbol wasn't declared as anything else yet
2516 assert(symbol->declaration == NULL);
2518 context_t *last_context = context;
2519 context = global_context;
2521 environment_push(declaration);
2522 declaration->next = context->declarations;
2523 context->declarations = declaration;
2525 context = last_context;
2530 static expression_t *parse_reference(void)
2532 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2534 ref->expression.type = EXPR_REFERENCE;
2535 ref->symbol = token.v.symbol;
2537 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2539 source_position_t source_position = token.source_position;
2542 if(declaration == NULL) {
2544 /* an implicitly defined function */
2545 if(token.type == '(') {
2546 parser_print_prefix_pos(token.source_position);
2547 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2548 ref->symbol->string);
2550 declaration = create_implicit_function(ref->symbol,
2555 parser_print_error_prefix();
2556 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2557 return (expression_t*) ref;
2561 ref->declaration = declaration;
2562 ref->expression.datatype = declaration->type;
2564 return (expression_t*) ref;
2567 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2571 /* TODO check if explicit cast is allowed and issue warnings/errors */
2574 static expression_t *parse_cast(void)
2576 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2578 cast->expression.type = EXPR_UNARY;
2579 cast->type = UNEXPR_CAST;
2580 cast->expression.source_position = token.source_position;
2582 type_t *type = parse_typename();
2585 expression_t *value = parse_sub_expression(20);
2587 check_cast_allowed(value, type);
2589 cast->expression.datatype = type;
2590 cast->value = value;
2592 return (expression_t*) cast;
2595 static expression_t *parse_statement_expression(void)
2597 statement_expression_t *expression
2598 = allocate_ast_zero(sizeof(expression[0]));
2599 expression->expression.type = EXPR_STATEMENT;
2601 statement_t *statement = parse_compound_statement();
2602 expression->statement = statement;
2603 if(statement == NULL) {
2608 assert(statement->type == STATEMENT_COMPOUND);
2609 compound_statement_t *compound_statement
2610 = (compound_statement_t*) statement;
2612 /* find last statement and use it's type */
2613 const statement_t *last_statement = NULL;
2614 const statement_t *iter = compound_statement->statements;
2615 for( ; iter != NULL; iter = iter->next) {
2616 last_statement = iter;
2619 if(last_statement->type == STATEMENT_EXPRESSION) {
2620 const expression_statement_t *expression_statement =
2621 (const expression_statement_t*) last_statement;
2622 expression->expression.datatype
2623 = expression_statement->expression->datatype;
2625 expression->expression.datatype = type_void;
2630 return (expression_t*) expression;
2633 static expression_t *parse_brace_expression(void)
2637 switch(token.type) {
2639 /* gcc extension: a stement expression */
2640 return parse_statement_expression();
2644 return parse_cast();
2646 if(is_typedef_symbol(token.v.symbol)) {
2647 return parse_cast();
2651 expression_t *result = parse_expression();
2657 static expression_t *parse_function_keyword(void)
2662 if (current_function == NULL) {
2663 parse_error("'__func__' used outside of a function");
2666 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2667 expression->expression.type = EXPR_FUNCTION;
2668 expression->expression.datatype = type_string;
2669 expression->value = "TODO: FUNCTION";
2671 return (expression_t*) expression;
2674 static expression_t *parse_pretty_function_keyword(void)
2676 eat(T___PRETTY_FUNCTION__);
2679 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2680 expression->expression.type = EXPR_PRETTY_FUNCTION;
2681 expression->expression.datatype = type_string;
2682 expression->value = "TODO: PRETTY FUNCTION";
2684 return (expression_t*) expression;
2687 static designator_t *parse_designator(void)
2689 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2691 if(token.type != T_IDENTIFIER) {
2692 parse_error_expected("while parsing member designator",
2697 result->symbol = token.v.symbol;
2700 designator_t *last_designator = result;
2702 if(token.type == '.') {
2704 if(token.type != T_IDENTIFIER) {
2705 parse_error_expected("while parsing member designator",
2710 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2711 designator->symbol = token.v.symbol;
2714 last_designator->next = designator;
2715 last_designator = designator;
2718 if(token.type == '[') {
2720 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2721 designator->array_access = parse_expression();
2722 if(designator->array_access == NULL) {
2728 last_designator->next = designator;
2729 last_designator = designator;
2738 static expression_t *parse_offsetof(void)
2740 eat(T___builtin_offsetof);
2742 offsetof_expression_t *expression
2743 = allocate_ast_zero(sizeof(expression[0]));
2744 expression->expression.type = EXPR_OFFSETOF;
2745 expression->expression.datatype = type_size_t;
2748 expression->type = parse_typename();
2750 expression->designator = parse_designator();
2753 return (expression_t*) expression;
2756 static expression_t *parse_va_arg(void)
2758 eat(T___builtin_va_arg);
2760 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2761 expression->expression.type = EXPR_VA_ARG;
2764 expression->arg = parse_assignment_expression();
2766 expression->expression.datatype = parse_typename();
2769 return (expression_t*) expression;
2772 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2774 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2775 parameter->type = argument_type;
2777 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2778 type->type.type = TYPE_FUNCTION;
2779 type->result_type = result_type;
2780 type->parameters = parameter;
2782 type_t *result = typehash_insert((type_t*) type);
2783 if(result != (type_t*) type) {
2790 static expression_t *parse_builtin_symbol(void)
2792 builtin_symbol_expression_t *expression
2793 = allocate_ast_zero(sizeof(expression[0]));
2794 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2796 expression->symbol = token.v.symbol;
2799 switch(token.type) {
2800 case T___builtin_alloca:
2801 type = make_function_1_type(type_void_ptr, type_size_t);
2807 expression->expression.datatype = type;
2808 return (expression_t*) expression;
2811 static expression_t *parse_primary_expression(void)
2813 switch(token.type) {
2815 return parse_int_const();
2816 case T_FLOATINGPOINT:
2817 return parse_float_const();
2818 case T_STRING_LITERAL:
2819 return parse_string_const();
2821 return parse_reference();
2822 case T___FUNCTION__:
2824 return parse_function_keyword();
2825 case T___PRETTY_FUNCTION__:
2826 return parse_pretty_function_keyword();
2827 case T___builtin_offsetof:
2828 return parse_offsetof();
2829 case T___builtin_va_arg:
2830 return parse_va_arg();
2831 case T___builtin_alloca:
2832 case T___builtin_expect:
2833 case T___builtin_va_start:
2834 case T___builtin_va_end:
2835 return parse_builtin_symbol();
2838 return parse_brace_expression();
2841 parser_print_error_prefix();
2842 fprintf(stderr, "unexpected token ");
2843 print_token(stderr, &token);
2844 fprintf(stderr, "\n");
2847 return make_invalid_expression();
2850 static expression_t *parse_array_expression(unsigned precedence,
2851 expression_t *array_ref)
2857 expression_t *index = parse_expression();
2859 array_access_expression_t *array_access
2860 = allocate_ast_zero(sizeof(array_access[0]));
2862 array_access->expression.type = EXPR_ARRAY_ACCESS;
2863 array_access->array_ref = array_ref;
2864 array_access->index = index;
2866 type_t *type_left = skip_typeref(array_ref->datatype);
2867 type_t *type_right = skip_typeref(index->datatype);
2869 if(type_left != NULL && type_right != NULL) {
2870 if(type_left->type == TYPE_POINTER) {
2871 pointer_type_t *pointer = (pointer_type_t*) type_left;
2872 array_access->expression.datatype = pointer->points_to;
2873 } else if(type_left->type == TYPE_ARRAY) {
2874 array_type_t *array_type = (array_type_t*) type_left;
2875 array_access->expression.datatype = array_type->element_type;
2876 } else if(type_right->type == TYPE_POINTER) {
2877 pointer_type_t *pointer = (pointer_type_t*) type_right;
2878 array_access->expression.datatype = pointer->points_to;
2879 } else if(type_right->type == TYPE_ARRAY) {
2880 array_type_t *array_type = (array_type_t*) type_right;
2881 array_access->expression.datatype = array_type->element_type;
2883 parser_print_error_prefix();
2884 fprintf(stderr, "array access on object with non-pointer types ");
2885 print_type_quoted(type_left);
2886 fprintf(stderr, ", ");
2887 print_type_quoted(type_right);
2888 fprintf(stderr, "\n");
2892 if(token.type != ']') {
2893 parse_error_expected("Problem while parsing array access", ']', 0);
2894 return (expression_t*) array_access;
2898 return (expression_t*) array_access;
2901 static bool is_declaration_specifier(const token_t *token,
2902 bool only_type_specifiers)
2904 switch(token->type) {
2908 return is_typedef_symbol(token->v.symbol);
2911 if(only_type_specifiers)
2920 static expression_t *parse_sizeof(unsigned precedence)
2924 sizeof_expression_t *sizeof_expression
2925 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2926 sizeof_expression->expression.type = EXPR_SIZEOF;
2927 sizeof_expression->expression.datatype = type_size_t;
2929 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2931 sizeof_expression->type = parse_typename();
2934 expression_t *expression = parse_sub_expression(precedence);
2935 sizeof_expression->type = expression->datatype;
2936 sizeof_expression->size_expression = expression;
2939 return (expression_t*) sizeof_expression;
2942 static expression_t *parse_select_expression(unsigned precedence,
2943 expression_t *compound)
2946 assert(token.type == '.' || token.type == T_MINUSGREATER);
2948 bool is_pointer = (token.type == T_MINUSGREATER);
2951 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2953 select->expression.type = EXPR_SELECT;
2954 select->compound = compound;
2956 if(token.type != T_IDENTIFIER) {
2957 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2958 return (expression_t*) select;
2960 symbol_t *symbol = token.v.symbol;
2961 select->symbol = symbol;
2964 type_t *orig_type = compound->datatype;
2965 if(orig_type == NULL)
2966 return make_invalid_expression();
2968 type_t *type = skip_typeref(orig_type);
2970 type_t *type_left = type;
2972 if(type->type != TYPE_POINTER) {
2973 parser_print_error_prefix();
2974 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2975 print_type_quoted(orig_type);
2976 fputc('\n', stderr);
2977 return make_invalid_expression();
2979 pointer_type_t *pointer_type = (pointer_type_t*) type;
2980 type_left = pointer_type->points_to;
2982 type_left = skip_typeref(type_left);
2984 if(type_left->type != TYPE_COMPOUND_STRUCT
2985 && type_left->type != TYPE_COMPOUND_UNION) {
2986 parser_print_error_prefix();
2987 fprintf(stderr, "request for member '%s' in something not a struct or "
2988 "union, but ", symbol->string);
2989 print_type_quoted(type_left);
2990 fputc('\n', stderr);
2991 return make_invalid_expression();
2994 compound_type_t *compound_type = (compound_type_t*) type_left;
2995 declaration_t *declaration = compound_type->declaration;
2997 if(!declaration->init.is_defined) {
2998 parser_print_error_prefix();
2999 fprintf(stderr, "request for member '%s' of incomplete type ",
3001 print_type_quoted(type_left);
3002 fputc('\n', stderr);
3003 return make_invalid_expression();
3006 declaration_t *iter = declaration->context.declarations;
3007 for( ; iter != NULL; iter = iter->next) {
3008 if(iter->symbol == symbol) {
3013 parser_print_error_prefix();
3014 print_type_quoted(type_left);
3015 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3016 return make_invalid_expression();
3019 select->compound_entry = iter;
3020 select->expression.datatype = iter->type;
3021 return (expression_t*) select;
3024 static expression_t *parse_call_expression(unsigned precedence,
3025 expression_t *expression)
3028 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3029 call->expression.type = EXPR_CALL;
3030 call->function = expression;
3032 function_type_t *function_type;
3033 type_t *orig_type = expression->datatype;
3034 type_t *type = skip_typeref(orig_type);
3036 if(type->type == TYPE_POINTER) {
3037 pointer_type_t *pointer_type = (pointer_type_t*) type;
3039 type = skip_typeref(pointer_type->points_to);
3041 if (type->type == TYPE_FUNCTION) {
3042 function_type = (function_type_t*) type;
3043 call->expression.datatype = function_type->result_type;
3045 parser_print_error_prefix();
3046 fputs("called object '", stderr);
3047 print_expression(expression);
3048 fputs("' (type ", stderr);
3049 print_type_quoted(orig_type);
3050 fputs(") is not a function\n", stderr);
3052 function_type = NULL;
3053 call->expression.datatype = NULL;
3056 /* parse arguments */
3059 if(token.type != ')') {
3060 call_argument_t *last_argument = NULL;
3063 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3065 argument->expression = parse_assignment_expression();
3066 if(last_argument == NULL) {
3067 call->arguments = argument;
3069 last_argument->next = argument;
3071 last_argument = argument;
3073 if(token.type != ',')
3080 if(function_type != NULL) {
3081 function_parameter_t *parameter = function_type->parameters;
3082 call_argument_t *argument = call->arguments;
3083 for( ; parameter != NULL && argument != NULL;
3084 parameter = parameter->next, argument = argument->next) {
3085 type_t *expected_type = parameter->type;
3086 /* TODO report context in error messages */
3087 argument->expression = create_implicit_cast(argument->expression,
3090 /* too few parameters */
3091 if(parameter != NULL) {
3092 parser_print_error_prefix();
3093 fprintf(stderr, "too few arguments to function '");
3094 print_expression(expression);
3095 fprintf(stderr, "'\n");
3096 } else if(argument != NULL) {
3097 /* too many parameters */
3098 if(!function_type->variadic
3099 && !function_type->unspecified_parameters) {
3100 parser_print_error_prefix();
3101 fprintf(stderr, "too many arguments to function '");
3102 print_expression(expression);
3103 fprintf(stderr, "'\n");
3105 /* do default promotion */
3106 for( ; argument != NULL; argument = argument->next) {
3107 type_t *type = argument->expression->datatype;
3112 if(is_type_integer(type)) {
3113 type = promote_integer(type);
3114 } else if(type == type_float) {
3117 argument->expression
3118 = create_implicit_cast(argument->expression, type);
3124 return (expression_t*) call;
3127 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3129 static expression_t *parse_conditional_expression(unsigned precedence,
3130 expression_t *expression)
3134 conditional_expression_t *conditional
3135 = allocate_ast_zero(sizeof(conditional[0]));
3136 conditional->expression.type = EXPR_CONDITIONAL;
3137 conditional->condition = expression;
3140 type_t *condition_type_orig = conditional->condition->datatype;
3141 if(condition_type_orig != NULL) {
3142 type_t *condition_type = skip_typeref(condition_type_orig);
3143 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3144 type_error("expected a scalar type", expression->source_position,
3145 condition_type_orig);
3149 expression_t *const t_expr = parse_expression();
3150 conditional->true_expression = t_expr;
3152 expression_t *const f_expr = parse_sub_expression(precedence);
3153 conditional->false_expression = f_expr;
3155 type_t *const true_type = t_expr->datatype;
3156 if(true_type == NULL)
3157 return (expression_t*) conditional;
3158 type_t *const false_type = f_expr->datatype;
3159 if(false_type == NULL)
3160 return (expression_t*) conditional;
3162 type_t *const skipped_true_type = skip_typeref(true_type);
3163 type_t *const skipped_false_type = skip_typeref(false_type);
3166 if (skipped_true_type == skipped_false_type) {
3167 conditional->expression.datatype = skipped_true_type;
3168 } else if (is_type_arithmetic(skipped_true_type) &&
3169 is_type_arithmetic(skipped_false_type)) {
3170 type_t *const result = semantic_arithmetic(skipped_true_type,
3171 skipped_false_type);
3172 conditional->true_expression = create_implicit_cast(t_expr, result);
3173 conditional->false_expression = create_implicit_cast(f_expr, result);
3174 conditional->expression.datatype = result;
3175 } else if (skipped_true_type->type == TYPE_POINTER &&
3176 skipped_false_type->type == TYPE_POINTER &&
3177 true /* TODO compatible points_to types */) {
3179 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3180 skipped_false_type->type == TYPE_POINTER)
3181 || (is_null_ptr_const(skipped_false_type) &&
3182 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3184 } else if(/* 1 is pointer to object type, other is void* */ false) {
3187 type_error_incompatible("while parsing conditional",
3188 expression->source_position, true_type,
3189 skipped_false_type);
3192 return (expression_t*) conditional;
3195 static expression_t *parse_extension(unsigned precedence)
3197 eat(T___extension__);
3199 /* TODO enable extensions */
3201 return parse_sub_expression(precedence);
3204 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3206 eat(T___builtin_classify_type);
3208 classify_type_expression_t *const classify_type_expr =
3209 allocate_ast_zero(sizeof(classify_type_expr[0]));
3210 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3211 classify_type_expr->expression.datatype = type_int;
3214 expression_t *const expression = parse_sub_expression(precedence);
3216 classify_type_expr->type_expression = expression;
3218 return (expression_t*)classify_type_expr;
3221 static void semantic_incdec(unary_expression_t *expression)
3223 type_t *orig_type = expression->value->datatype;
3224 if(orig_type == NULL)
3227 type_t *type = skip_typeref(orig_type);
3228 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3229 /* TODO: improve error message */
3230 parser_print_error_prefix();
3231 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3235 expression->expression.datatype = orig_type;
3238 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3240 type_t *orig_type = expression->value->datatype;
3241 if(orig_type == NULL)
3244 type_t *type = skip_typeref(orig_type);
3245 if(!is_type_arithmetic(type)) {
3246 /* TODO: improve error message */
3247 parser_print_error_prefix();
3248 fprintf(stderr, "operation needs an arithmetic type\n");
3252 expression->expression.datatype = orig_type;
3255 static void semantic_unexpr_scalar(unary_expression_t *expression)
3257 type_t *orig_type = expression->value->datatype;
3258 if(orig_type == NULL)
3261 type_t *type = skip_typeref(orig_type);
3262 if (!is_type_scalar(type)) {
3263 parse_error("operand of ! must be of scalar type\n");
3267 expression->expression.datatype = orig_type;
3270 static void semantic_unexpr_integer(unary_expression_t *expression)
3272 type_t *orig_type = expression->value->datatype;
3273 if(orig_type == NULL)
3276 type_t *type = skip_typeref(orig_type);
3277 if (!is_type_integer(type)) {
3278 parse_error("operand of ~ must be of integer type\n");
3282 expression->expression.datatype = orig_type;
3285 static void semantic_dereference(unary_expression_t *expression)
3287 type_t *orig_type = expression->value->datatype;
3288 if(orig_type == NULL)
3291 type_t *type = skip_typeref(orig_type);
3292 switch (type->type) {
3294 array_type_t *const array_type = (array_type_t*)type;
3295 expression->expression.datatype = array_type->element_type;
3299 case TYPE_POINTER: {
3300 pointer_type_t *pointer_type = (pointer_type_t*)type;
3301 expression->expression.datatype = pointer_type->points_to;
3306 parser_print_error_prefix();
3307 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3308 print_type_quoted(orig_type);
3309 fputs(" given.\n", stderr);
3314 static void semantic_take_addr(unary_expression_t *expression)
3316 type_t *orig_type = expression->value->datatype;
3317 if(orig_type == NULL)
3320 expression_t *value = expression->value;
3321 if(value->type == EXPR_REFERENCE) {
3322 reference_expression_t *reference = (reference_expression_t*) value;
3323 declaration_t *declaration = reference->declaration;
3324 if(declaration != NULL) {
3325 declaration->address_taken = 1;
3329 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3332 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3333 static expression_t *parse_##unexpression_type(unsigned precedence) \
3337 unary_expression_t *unary_expression \
3338 = allocate_ast_zero(sizeof(unary_expression[0])); \
3339 unary_expression->expression.type = EXPR_UNARY; \
3340 unary_expression->type = unexpression_type; \
3341 unary_expression->value = parse_sub_expression(precedence); \
3343 sfunc(unary_expression); \
3345 return (expression_t*) unary_expression; \
3348 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3349 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3350 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3351 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3352 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3353 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3354 semantic_unexpr_integer)
3355 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3357 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3360 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3362 static expression_t *parse_##unexpression_type(unsigned precedence, \
3363 expression_t *left) \
3365 (void) precedence; \
3368 unary_expression_t *unary_expression \
3369 = allocate_ast_zero(sizeof(unary_expression[0])); \
3370 unary_expression->expression.type = EXPR_UNARY; \
3371 unary_expression->type = unexpression_type; \
3372 unary_expression->value = left; \
3374 sfunc(unary_expression); \
3376 return (expression_t*) unary_expression; \
3379 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3381 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3384 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3386 /* TODO: handle complex + imaginary types */
3388 /* § 6.3.1.8 Usual arithmetic conversions */
3389 if(type_left == type_long_double || type_right == type_long_double) {
3390 return type_long_double;
3391 } else if(type_left == type_double || type_right == type_double) {
3393 } else if(type_left == type_float || type_right == type_float) {
3397 type_right = promote_integer(type_right);
3398 type_left = promote_integer(type_left);
3400 if(type_left == type_right)
3403 bool signed_left = is_type_signed(type_left);
3404 bool signed_right = is_type_signed(type_right);
3405 if(get_rank(type_left) < get_rank(type_right)) {
3406 if(signed_left == signed_right || !signed_right) {
3412 if(signed_left == signed_right || !signed_left) {
3420 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3422 expression_t *left = expression->left;
3423 expression_t *right = expression->right;
3424 type_t *orig_type_left = left->datatype;
3425 type_t *orig_type_right = right->datatype;
3427 if(orig_type_left == NULL || orig_type_right == NULL)
3430 type_t *type_left = skip_typeref(orig_type_left);
3431 type_t *type_right = skip_typeref(orig_type_right);
3433 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3434 /* TODO: improve error message */
3435 parser_print_error_prefix();
3436 fprintf(stderr, "operation needs arithmetic types\n");
3440 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3441 expression->left = create_implicit_cast(left, arithmetic_type);
3442 expression->right = create_implicit_cast(right, arithmetic_type);
3443 expression->expression.datatype = arithmetic_type;
3446 static void semantic_shift_op(binary_expression_t *expression)
3448 expression_t *left = expression->left;
3449 expression_t *right = expression->right;
3450 type_t *orig_type_left = left->datatype;
3451 type_t *orig_type_right = right->datatype;
3453 if(orig_type_left == NULL || orig_type_right == NULL)
3456 type_t *type_left = skip_typeref(orig_type_left);
3457 type_t *type_right = skip_typeref(orig_type_right);
3459 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3460 /* TODO: improve error message */
3461 parser_print_error_prefix();
3462 fprintf(stderr, "operation needs integer types\n");
3466 type_left = promote_integer(type_left);
3467 type_right = promote_integer(type_right);
3469 expression->left = create_implicit_cast(left, type_left);
3470 expression->right = create_implicit_cast(right, type_right);
3471 expression->expression.datatype = type_left;
3474 static void semantic_add(binary_expression_t *expression)
3476 expression_t *left = expression->left;
3477 expression_t *right = expression->right;
3478 type_t *orig_type_left = left->datatype;
3479 type_t *orig_type_right = right->datatype;
3481 if(orig_type_left == NULL || orig_type_right == NULL)
3484 type_t *type_left = skip_typeref(orig_type_left);
3485 type_t *type_right = skip_typeref(orig_type_right);
3488 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3489 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3490 expression->left = create_implicit_cast(left, arithmetic_type);
3491 expression->right = create_implicit_cast(right, arithmetic_type);
3492 expression->expression.datatype = arithmetic_type;
3494 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3495 expression->expression.datatype = type_left;
3496 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3497 expression->expression.datatype = type_right;
3498 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3499 const array_type_t *const arr_type = (const array_type_t*)type_left;
3500 expression->expression.datatype =
3501 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3502 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3503 const array_type_t *const arr_type = (const array_type_t*)type_right;
3504 expression->expression.datatype =
3505 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3507 parser_print_error_prefix();
3508 fprintf(stderr, "invalid operands to binary + (");
3509 print_type_quoted(orig_type_left);
3510 fprintf(stderr, ", ");
3511 print_type_quoted(orig_type_right);
3512 fprintf(stderr, ")\n");
3516 static void semantic_sub(binary_expression_t *expression)
3518 expression_t *left = expression->left;
3519 expression_t *right = expression->right;
3520 type_t *orig_type_left = left->datatype;
3521 type_t *orig_type_right = right->datatype;
3523 if(orig_type_left == NULL || orig_type_right == NULL)
3526 type_t *type_left = skip_typeref(orig_type_left);
3527 type_t *type_right = skip_typeref(orig_type_right);
3530 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3531 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3532 expression->left = create_implicit_cast(left, arithmetic_type);
3533 expression->right = create_implicit_cast(right, arithmetic_type);
3534 expression->expression.datatype = arithmetic_type;
3536 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3537 expression->expression.datatype = type_left;
3538 } else if(type_left->type == TYPE_POINTER &&
3539 type_right->type == TYPE_POINTER) {
3540 if(!pointers_compatible(type_left, type_right)) {
3541 parser_print_error_prefix();
3542 fprintf(stderr, "pointers to incompatible objects to binary - (");
3543 print_type_quoted(orig_type_left);
3544 fprintf(stderr, ", ");
3545 print_type_quoted(orig_type_right);
3546 fprintf(stderr, ")\n");
3548 expression->expression.datatype = type_ptrdiff_t;
3551 parser_print_error_prefix();
3552 fprintf(stderr, "invalid operands to binary - (");
3553 print_type_quoted(orig_type_left);
3554 fprintf(stderr, ", ");
3555 print_type_quoted(orig_type_right);
3556 fprintf(stderr, ")\n");
3560 static void semantic_comparison(binary_expression_t *expression)
3562 expression_t *left = expression->left;
3563 expression_t *right = expression->right;
3564 type_t *orig_type_left = left->datatype;
3565 type_t *orig_type_right = right->datatype;
3567 if(orig_type_left == NULL || orig_type_right == NULL)
3570 type_t *type_left = skip_typeref(orig_type_left);
3571 type_t *type_right = skip_typeref(orig_type_right);
3573 /* TODO non-arithmetic types */
3574 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3575 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3576 expression->left = create_implicit_cast(left, arithmetic_type);
3577 expression->right = create_implicit_cast(right, arithmetic_type);
3578 expression->expression.datatype = arithmetic_type;
3579 } else if (type_left->type == TYPE_POINTER &&
3580 type_right->type == TYPE_POINTER) {
3581 /* TODO check compatibility */
3582 } else if (type_left->type == TYPE_POINTER) {
3583 expression->right = create_implicit_cast(right, type_left);
3584 } else if (type_right->type == TYPE_POINTER) {
3585 expression->left = create_implicit_cast(left, type_right);
3587 type_error_incompatible("invalid operands in comparison",
3588 token.source_position, type_left, type_right);
3590 expression->expression.datatype = type_int;
3593 static void semantic_arithmetic_assign(binary_expression_t *expression)
3595 expression_t *left = expression->left;
3596 expression_t *right = expression->right;
3597 type_t *orig_type_left = left->datatype;
3598 type_t *orig_type_right = right->datatype;
3600 if(orig_type_left == NULL || orig_type_right == NULL)
3603 type_t *type_left = skip_typeref(orig_type_left);
3604 type_t *type_right = skip_typeref(orig_type_right);
3606 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3607 /* TODO: improve error message */
3608 parser_print_error_prefix();
3609 fprintf(stderr, "operation needs arithmetic types\n");
3613 /* combined instructions are tricky. We can't create an implicit cast on
3614 * the left side, because we need the uncasted form for the store.
3615 * The ast2firm pass has to know that left_type must be right_type
3616 * for the arithmeitc operation and create a cast by itself */
3617 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3618 expression->right = create_implicit_cast(right, arithmetic_type);
3619 expression->expression.datatype = type_left;
3622 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3624 expression_t *left = expression->left;
3625 expression_t *right = expression->right;
3626 type_t *orig_type_left = left->datatype;
3627 type_t *orig_type_right = right->datatype;
3629 if(orig_type_left == NULL || orig_type_right == NULL)
3632 type_t *type_left = skip_typeref(orig_type_left);
3633 type_t *type_right = skip_typeref(orig_type_right);
3635 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3636 /* combined instructions are tricky. We can't create an implicit cast on
3637 * the left side, because we need the uncasted form for the store.
3638 * The ast2firm pass has to know that left_type must be right_type
3639 * for the arithmeitc operation and create a cast by itself */
3640 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3641 expression->right = create_implicit_cast(right, arithmetic_type);
3642 expression->expression.datatype = type_left;
3643 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3644 expression->expression.datatype = type_left;
3646 parser_print_error_prefix();
3647 fputs("Incompatible types ", stderr);
3648 print_type_quoted(orig_type_left);
3649 fputs(" and ", stderr);
3650 print_type_quoted(orig_type_right);
3651 fputs(" in assignment\n", stderr);
3656 static void semantic_logical_op(binary_expression_t *expression)
3658 expression_t *left = expression->left;
3659 expression_t *right = expression->right;
3660 type_t *orig_type_left = left->datatype;
3661 type_t *orig_type_right = right->datatype;
3663 if(orig_type_left == NULL || orig_type_right == NULL)
3666 type_t *type_left = skip_typeref(orig_type_left);
3667 type_t *type_right = skip_typeref(orig_type_right);
3669 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3670 /* TODO: improve error message */
3671 parser_print_error_prefix();
3672 fprintf(stderr, "operation needs scalar types\n");
3676 expression->expression.datatype = type_int;
3679 static void semantic_binexpr_assign(binary_expression_t *expression)
3681 expression_t *left = expression->left;
3682 type_t *orig_type_left = left->datatype;
3684 if(orig_type_left == NULL)
3687 type_t *type_left = skip_typeref(orig_type_left);
3689 if (type_left->type == TYPE_ARRAY) {
3690 parse_error("Cannot assign to arrays.");
3694 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3695 parser_print_error_prefix();
3696 fprintf(stderr, "assignment to readonly location '");
3697 print_expression(left);
3698 fprintf(stderr, "' (type ");
3699 print_type_quoted(orig_type_left);
3700 fprintf(stderr, ")\n");
3703 semantic_assign(orig_type_left, &expression->right, "assignment");
3705 expression->expression.datatype = orig_type_left;
3708 static void semantic_comma(binary_expression_t *expression)
3710 expression->expression.datatype = expression->right->datatype;
3713 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3714 static expression_t *parse_##binexpression_type(unsigned precedence, \
3715 expression_t *left) \
3719 expression_t *right = parse_sub_expression(precedence + lr); \
3721 binary_expression_t *binexpr \
3722 = allocate_ast_zero(sizeof(binexpr[0])); \
3723 binexpr->expression.type = EXPR_BINARY; \
3724 binexpr->type = binexpression_type; \
3725 binexpr->left = left; \
3726 binexpr->right = right; \
3729 return (expression_t*) binexpr; \
3732 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3733 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3734 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3735 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3736 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3737 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3738 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3739 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3740 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3741 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3742 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3743 semantic_comparison, 1)
3744 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3745 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3746 semantic_comparison, 1)
3747 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3748 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3749 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3750 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3751 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3752 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3753 semantic_shift_op, 1)
3754 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3755 semantic_shift_op, 1)
3756 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3757 semantic_arithmetic_addsubb_assign, 0)
3758 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3759 semantic_arithmetic_addsubb_assign, 0)
3760 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3761 semantic_arithmetic_assign, 0)
3762 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3763 semantic_arithmetic_assign, 0)
3764 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3765 semantic_arithmetic_assign, 0)
3766 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3767 semantic_arithmetic_assign, 0)
3768 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3769 semantic_arithmetic_assign, 0)
3770 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3771 semantic_arithmetic_assign, 0)
3772 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3773 semantic_arithmetic_assign, 0)
3774 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3775 semantic_arithmetic_assign, 0)
3777 static expression_t *parse_sub_expression(unsigned precedence)
3779 if(token.type < 0) {
3780 return expected_expression_error();
3783 expression_parser_function_t *parser
3784 = &expression_parsers[token.type];
3785 source_position_t source_position = token.source_position;
3788 if(parser->parser != NULL) {
3789 left = parser->parser(parser->precedence);
3791 left = parse_primary_expression();
3793 assert(left != NULL);
3794 left->source_position = source_position;
3797 if(token.type < 0) {
3798 return expected_expression_error();
3801 parser = &expression_parsers[token.type];
3802 if(parser->infix_parser == NULL)
3804 if(parser->infix_precedence < precedence)
3807 left = parser->infix_parser(parser->infix_precedence, left);
3809 assert(left != NULL);
3810 assert(left->type != EXPR_UNKNOWN);
3811 left->source_position = source_position;
3817 static expression_t *parse_expression(void)
3819 return parse_sub_expression(1);
3824 static void register_expression_parser(parse_expression_function parser,
3825 int token_type, unsigned precedence)
3827 expression_parser_function_t *entry = &expression_parsers[token_type];
3829 if(entry->parser != NULL) {
3830 fprintf(stderr, "for token ");
3831 print_token_type(stderr, token_type);
3832 fprintf(stderr, "\n");
3833 panic("trying to register multiple expression parsers for a token");
3835 entry->parser = parser;
3836 entry->precedence = precedence;
3839 static void register_expression_infix_parser(
3840 parse_expression_infix_function parser, int token_type,
3841 unsigned precedence)
3843 expression_parser_function_t *entry = &expression_parsers[token_type];
3845 if(entry->infix_parser != NULL) {
3846 fprintf(stderr, "for token ");
3847 print_token_type(stderr, token_type);
3848 fprintf(stderr, "\n");
3849 panic("trying to register multiple infix expression parsers for a "
3852 entry->infix_parser = parser;
3853 entry->infix_precedence = precedence;
3856 static void init_expression_parsers(void)
3858 memset(&expression_parsers, 0, sizeof(expression_parsers));
3860 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3861 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3862 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3863 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3864 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3865 T_GREATERGREATER, 16);
3866 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3867 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3868 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3869 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3870 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3871 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3872 T_GREATEREQUAL, 14);
3873 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3874 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3875 T_EXCLAMATIONMARKEQUAL, 13);
3876 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3877 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3878 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3879 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3880 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3881 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3882 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3883 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3884 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3885 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3886 T_ASTERISKEQUAL, 2);
3887 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3888 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3890 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3891 T_LESSLESSEQUAL, 2);
3892 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3893 T_GREATERGREATEREQUAL, 2);
3894 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3896 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3898 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3901 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3903 register_expression_infix_parser(parse_array_expression, '[', 30);
3904 register_expression_infix_parser(parse_call_expression, '(', 30);
3905 register_expression_infix_parser(parse_select_expression, '.', 30);
3906 register_expression_infix_parser(parse_select_expression,
3907 T_MINUSGREATER, 30);
3908 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3910 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3913 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3914 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3915 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3916 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3917 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3918 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3919 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3920 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3921 register_expression_parser(parse_sizeof, T_sizeof, 25);
3922 register_expression_parser(parse_extension, T___extension__, 25);
3923 register_expression_parser(parse_builtin_classify_type,
3924 T___builtin_classify_type, 25);
3928 static statement_t *parse_case_statement(void)
3931 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3932 label->statement.type = STATEMENT_CASE_LABEL;
3933 label->statement.source_position = token.source_position;
3935 label->expression = parse_expression();
3938 label->label_statement = parse_statement();
3940 return (statement_t*) label;
3943 static statement_t *parse_default_statement(void)
3947 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3948 label->statement.type = STATEMENT_CASE_LABEL;
3949 label->statement.source_position = token.source_position;
3952 label->label_statement = parse_statement();
3954 return (statement_t*) label;
3957 static declaration_t *get_label(symbol_t *symbol)
3959 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3960 assert(current_function != NULL);
3961 /* if we found a label in the same function, then we already created the
3963 if(candidate != NULL
3964 && candidate->parent_context == ¤t_function->context) {
3968 /* otherwise we need to create a new one */
3969 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3970 declaration->namespc = NAMESPACE_LABEL;
3971 declaration->symbol = symbol;
3973 label_push(declaration);
3978 static statement_t *parse_label_statement(void)
3980 assert(token.type == T_IDENTIFIER);
3981 symbol_t *symbol = token.v.symbol;
3984 declaration_t *label = get_label(symbol);
3986 /* if source position is already set then the label is defined twice,
3987 * otherwise it was just mentioned in a goto so far */
3988 if(label->source_position.input_name != NULL) {
3989 parser_print_error_prefix();
3990 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3991 parser_print_error_prefix_pos(label->source_position);
3992 fprintf(stderr, "previous definition of '%s' was here\n",
3995 label->source_position = token.source_position;
3998 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4000 label_statement->statement.type = STATEMENT_LABEL;
4001 label_statement->statement.source_position = token.source_position;
4002 label_statement->label = label;
4006 if(token.type == '}') {
4007 parse_error("label at end of compound statement");
4008 return (statement_t*) label_statement;
4010 label_statement->label_statement = parse_statement();
4013 return (statement_t*) label_statement;
4016 static statement_t *parse_if(void)
4020 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4021 statement->statement.type = STATEMENT_IF;
4022 statement->statement.source_position = token.source_position;
4025 statement->condition = parse_expression();
4028 statement->true_statement = parse_statement();
4029 if(token.type == T_else) {
4031 statement->false_statement = parse_statement();
4034 return (statement_t*) statement;
4037 static statement_t *parse_switch(void)
4041 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4042 statement->statement.type = STATEMENT_SWITCH;
4043 statement->statement.source_position = token.source_position;
4046 statement->expression = parse_expression();
4048 statement->body = parse_statement();
4050 return (statement_t*) statement;
4053 static statement_t *parse_while(void)
4057 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4058 statement->statement.type = STATEMENT_WHILE;
4059 statement->statement.source_position = token.source_position;
4062 statement->condition = parse_expression();
4064 statement->body = parse_statement();
4066 return (statement_t*) statement;
4069 static statement_t *parse_do(void)
4073 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4074 statement->statement.type = STATEMENT_DO_WHILE;
4075 statement->statement.source_position = token.source_position;
4077 statement->body = parse_statement();
4080 statement->condition = parse_expression();
4084 return (statement_t*) statement;
4087 static statement_t *parse_for(void)
4091 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4092 statement->statement.type = STATEMENT_FOR;
4093 statement->statement.source_position = token.source_position;
4097 int top = environment_top();
4098 context_t *last_context = context;
4099 set_context(&statement->context);
4101 if(token.type != ';') {
4102 if(is_declaration_specifier(&token, false)) {
4103 parse_declaration();
4105 statement->initialisation = parse_expression();
4112 if(token.type != ';') {
4113 statement->condition = parse_expression();
4116 if(token.type != ')') {
4117 statement->step = parse_expression();
4120 statement->body = parse_statement();
4122 assert(context == &statement->context);
4123 set_context(last_context);
4124 environment_pop_to(top);
4126 return (statement_t*) statement;
4129 static statement_t *parse_goto(void)
4133 if(token.type != T_IDENTIFIER) {
4134 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4138 symbol_t *symbol = token.v.symbol;
4141 declaration_t *label = get_label(symbol);
4143 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4145 statement->statement.type = STATEMENT_GOTO;
4146 statement->statement.source_position = token.source_position;
4148 statement->label = label;
4152 return (statement_t*) statement;
4155 static statement_t *parse_continue(void)
4160 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4161 statement->type = STATEMENT_CONTINUE;
4162 statement->source_position = token.source_position;
4167 static statement_t *parse_break(void)
4172 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4173 statement->type = STATEMENT_BREAK;
4174 statement->source_position = token.source_position;
4179 static statement_t *parse_return(void)
4183 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4185 statement->statement.type = STATEMENT_RETURN;
4186 statement->statement.source_position = token.source_position;
4188 assert(current_function->type->type == TYPE_FUNCTION);
4189 function_type_t *function_type = (function_type_t*) current_function->type;
4190 type_t *return_type = function_type->result_type;
4192 expression_t *return_value;
4193 if(token.type != ';') {
4194 return_value = parse_expression();
4196 if(return_type == type_void && return_value->datatype != type_void) {
4197 parse_warning("'return' with a value, in function returning void");
4198 return_value = NULL;
4200 if(return_type != NULL) {
4201 semantic_assign(return_type, &return_value, "'return'");
4205 return_value = NULL;
4206 if(return_type != type_void) {
4207 parse_warning("'return' without value, in function returning "
4211 statement->return_value = return_value;
4215 return (statement_t*) statement;
4218 static statement_t *parse_declaration_statement(void)
4220 declaration_t *before = last_declaration;
4222 declaration_statement_t *statement
4223 = allocate_ast_zero(sizeof(statement[0]));
4224 statement->statement.type = STATEMENT_DECLARATION;
4225 statement->statement.source_position = token.source_position;
4227 declaration_specifiers_t specifiers;
4228 memset(&specifiers, 0, sizeof(specifiers));
4229 parse_declaration_specifiers(&specifiers);
4231 if(token.type == ';') {
4234 parse_init_declarators(&specifiers);
4237 if(before == NULL) {
4238 statement->declarations_begin = context->declarations;
4240 statement->declarations_begin = before->next;
4242 statement->declarations_end = last_declaration;
4244 return (statement_t*) statement;
4247 static statement_t *parse_expression_statement(void)
4249 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4250 statement->statement.type = STATEMENT_EXPRESSION;
4251 statement->statement.source_position = token.source_position;
4253 statement->expression = parse_expression();
4257 return (statement_t*) statement;
4260 static statement_t *parse_statement(void)
4262 statement_t *statement = NULL;
4264 /* declaration or statement */
4265 switch(token.type) {
4267 statement = parse_case_statement();
4271 statement = parse_default_statement();
4275 statement = parse_compound_statement();
4279 statement = parse_if();
4283 statement = parse_switch();
4287 statement = parse_while();
4291 statement = parse_do();
4295 statement = parse_for();
4299 statement = parse_goto();
4303 statement = parse_continue();
4307 statement = parse_break();
4311 statement = parse_return();
4320 if(look_ahead(1)->type == ':') {
4321 statement = parse_label_statement();
4325 if(is_typedef_symbol(token.v.symbol)) {
4326 statement = parse_declaration_statement();
4330 statement = parse_expression_statement();
4333 case T___extension__:
4334 /* this can be a prefix to a declaration or an expression statement */
4335 /* we simply eat it now and parse the rest with tail recursion */
4338 } while(token.type == T___extension__);
4339 statement = parse_statement();
4343 statement = parse_declaration_statement();
4347 statement = parse_expression_statement();
4351 assert(statement == NULL || statement->source_position.input_name != NULL);
4356 static statement_t *parse_compound_statement(void)
4358 compound_statement_t *compound_statement
4359 = allocate_ast_zero(sizeof(compound_statement[0]));
4360 compound_statement->statement.type = STATEMENT_COMPOUND;
4361 compound_statement->statement.source_position = token.source_position;
4365 int top = environment_top();
4366 context_t *last_context = context;
4367 set_context(&compound_statement->context);
4369 statement_t *last_statement = NULL;
4371 while(token.type != '}' && token.type != T_EOF) {
4372 statement_t *statement = parse_statement();
4373 if(statement == NULL)
4376 if(last_statement != NULL) {
4377 last_statement->next = statement;
4379 compound_statement->statements = statement;
4382 while(statement->next != NULL)
4383 statement = statement->next;
4385 last_statement = statement;
4388 if(token.type != '}') {
4389 parser_print_error_prefix_pos(
4390 compound_statement->statement.source_position);
4391 fprintf(stderr, "end of file while looking for closing '}'\n");
4395 assert(context == &compound_statement->context);
4396 set_context(last_context);
4397 environment_pop_to(top);
4399 return (statement_t*) compound_statement;
4402 static translation_unit_t *parse_translation_unit(void)
4404 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4406 assert(global_context == NULL);
4407 global_context = &unit->context;
4409 assert(context == NULL);
4410 set_context(&unit->context);
4412 while(token.type != T_EOF) {
4413 parse_declaration();
4416 assert(context == &unit->context);
4418 last_declaration = NULL;
4420 assert(global_context == &unit->context);
4421 global_context = NULL;
4426 translation_unit_t *parse(void)
4428 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4429 label_stack = NEW_ARR_F(stack_entry_t, 0);
4430 found_error = false;
4432 type_set_output(stderr);
4433 ast_set_output(stderr);
4435 lookahead_bufpos = 0;
4436 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4439 translation_unit_t *unit = parse_translation_unit();
4441 DEL_ARR_F(environment_stack);
4442 DEL_ARR_F(label_stack);
4450 void init_parser(void)
4452 init_expression_parsers();
4453 obstack_init(&temp_obst);
4455 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4456 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4457 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4458 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4459 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4460 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4461 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4462 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4463 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4464 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4465 type_string = make_pointer_type(type_const_char, TYPE_QUALIFIER_NONE);
4468 void exit_parser(void)
4470 obstack_free(&temp_obst, NULL);