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 void free_type(void *type)
125 obstack_free(type_obst, type);
129 * returns the top element of the environment stack
131 static inline size_t environment_top(void)
133 return ARR_LEN(environment_stack);
136 static inline size_t label_top(void)
138 return ARR_LEN(label_stack);
143 static inline void next_token(void)
145 token = lookahead_buffer[lookahead_bufpos];
146 lookahead_buffer[lookahead_bufpos] = lexer_token;
149 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
152 print_token(stderr, &token);
153 fprintf(stderr, "\n");
157 static inline const token_t *look_ahead(int num)
159 assert(num > 0 && num <= MAX_LOOKAHEAD);
160 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
161 return & lookahead_buffer[pos];
164 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
166 static void error(void)
169 #ifdef ABORT_ON_ERROR
174 static void parser_print_prefix_pos(const source_position_t source_position)
176 fputs(source_position.input_name, stderr);
178 fprintf(stderr, "%d", source_position.linenr);
182 static void parser_print_error_prefix_pos(
183 const source_position_t source_position)
185 parser_print_prefix_pos(source_position);
186 fputs("error: ", stderr);
190 static void parser_print_error_prefix(void)
192 parser_print_error_prefix_pos(token.source_position);
195 static void parse_error(const char *message)
197 parser_print_error_prefix();
198 fprintf(stderr, "parse error: %s\n", message);
201 static void parser_print_warning_prefix_pos(
202 const source_position_t source_position)
204 parser_print_prefix_pos(source_position);
205 fputs("warning: ", stderr);
208 static void parse_warning_pos(const source_position_t source_position,
209 const char *const message)
211 parser_print_prefix_pos(source_position);
212 fprintf(stderr, "warning: %s\n", message);
215 static void parse_warning(const char *message)
217 parse_warning_pos(token.source_position, message);
220 static void parse_error_expected(const char *message, ...)
225 if(message != NULL) {
226 parser_print_error_prefix();
227 fprintf(stderr, "%s\n", message);
229 parser_print_error_prefix();
230 fputs("Parse error: got ", stderr);
231 print_token(stderr, &token);
232 fputs(", expected ", stderr);
234 va_start(args, message);
235 token_type_t token_type = va_arg(args, token_type_t);
236 while(token_type != 0) {
240 fprintf(stderr, ", ");
242 print_token_type(stderr, token_type);
243 token_type = va_arg(args, token_type_t);
246 fprintf(stderr, "\n");
249 static void print_type_quoted(type_t *type)
256 static void type_error(const char *msg, const source_position_t source_position,
259 parser_print_error_prefix_pos(source_position);
260 fprintf(stderr, "%s, but found type ", msg);
261 print_type_quoted(type);
265 static void type_error_incompatible(const char *msg,
266 const source_position_t source_position, type_t *type1, type_t *type2)
268 parser_print_error_prefix_pos(source_position);
269 fprintf(stderr, "%s, incompatible types: ", msg);
270 print_type_quoted(type1);
271 fprintf(stderr, " - ");
272 print_type_quoted(type2);
273 fprintf(stderr, ")\n");
276 static void eat_block(void)
278 if(token.type == '{')
281 while(token.type != '}') {
282 if(token.type == T_EOF)
284 if(token.type == '{') {
293 static void eat_statement(void)
295 while(token.type != ';') {
296 if(token.type == T_EOF)
298 if(token.type == '}')
300 if(token.type == '{') {
309 static void eat_brace(void)
311 if(token.type == '(')
314 while(token.type != ')') {
315 if(token.type == T_EOF)
317 if(token.type == ')' || token.type == ';' || token.type == '}') {
320 if(token.type == '(') {
324 if(token.type == '{') {
333 #define expect(expected) \
334 if(UNLIKELY(token.type != (expected))) { \
335 parse_error_expected(NULL, (expected), 0); \
341 #define expect_block(expected) \
342 if(UNLIKELY(token.type != (expected))) { \
343 parse_error_expected(NULL, (expected), 0); \
349 #define expect_void(expected) \
350 if(UNLIKELY(token.type != (expected))) { \
351 parse_error_expected(NULL, (expected), 0); \
357 static void set_context(context_t *new_context)
359 context = new_context;
361 last_declaration = new_context->declarations;
362 if(last_declaration != NULL) {
363 while(last_declaration->next != NULL) {
364 last_declaration = last_declaration->next;
370 * called when we find a 2nd declarator for an identifier we already have a
373 static bool is_compatible_declaration (declaration_t *declaration,
374 declaration_t *previous)
376 /* TODO: not correct yet */
377 return declaration->type == previous->type;
380 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
382 declaration_t *declaration = symbol->declaration;
383 for( ; declaration != NULL; declaration = declaration->symbol_next) {
384 if(declaration->namespc == namespc)
391 static const char *get_namespace_prefix(namespace_t namespc)
394 case NAMESPACE_NORMAL:
396 case NAMESPACE_UNION:
398 case NAMESPACE_STRUCT:
402 case NAMESPACE_LABEL:
405 panic("invalid namespace found");
409 * pushs an environment_entry on the environment stack and links the
410 * corresponding symbol to the new entry
412 static declaration_t *stack_push(stack_entry_t **stack_ptr,
413 declaration_t *declaration,
414 context_t *parent_context)
416 symbol_t *symbol = declaration->symbol;
417 namespace_t namespc = (namespace_t)declaration->namespc;
419 /* a declaration should be only pushed once */
420 assert(declaration->parent_context == NULL);
421 declaration->parent_context = parent_context;
423 declaration_t *previous_declaration = get_declaration(symbol, namespc);
424 assert(declaration != previous_declaration);
425 if(previous_declaration != NULL
426 && previous_declaration->parent_context == context) {
427 if(!is_compatible_declaration(declaration, previous_declaration)) {
428 parser_print_error_prefix_pos(declaration->source_position);
429 fprintf(stderr, "definition of symbol %s%s with type ",
430 get_namespace_prefix(namespc), symbol->string);
431 print_type_quoted(declaration->type);
433 parser_print_error_prefix_pos(
434 previous_declaration->source_position);
435 fprintf(stderr, "is incompatible with previous declaration "
437 print_type_quoted(previous_declaration->type);
440 const storage_class_t old_storage = previous_declaration->storage_class;
441 const storage_class_t new_storage = declaration->storage_class;
442 if (current_function == NULL) {
443 if (old_storage != STORAGE_CLASS_STATIC &&
444 new_storage == STORAGE_CLASS_STATIC) {
445 parser_print_error_prefix_pos(declaration->source_position);
447 "static declaration of '%s' follows non-static declaration\n",
449 parser_print_error_prefix_pos(previous_declaration->source_position);
450 fprintf(stderr, "previous declaration of '%s' was here\n",
453 if (old_storage == STORAGE_CLASS_EXTERN) {
454 if (new_storage == STORAGE_CLASS_NONE) {
455 previous_declaration->storage_class = STORAGE_CLASS_NONE;
458 parser_print_warning_prefix_pos(declaration->source_position);
459 fprintf(stderr, "redundant declaration for '%s'\n",
461 parser_print_warning_prefix_pos(previous_declaration->source_position);
462 fprintf(stderr, "previous declaration of '%s' was here\n",
467 if (old_storage == STORAGE_CLASS_EXTERN &&
468 new_storage == STORAGE_CLASS_EXTERN) {
469 parser_print_warning_prefix_pos(declaration->source_position);
470 fprintf(stderr, "redundant extern declaration for '%s'\n",
472 parser_print_warning_prefix_pos(previous_declaration->source_position);
473 fprintf(stderr, "previous declaration of '%s' was here\n",
476 parser_print_error_prefix_pos(declaration->source_position);
477 if (old_storage == new_storage) {
478 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
480 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
482 parser_print_error_prefix_pos(previous_declaration->source_position);
483 fprintf(stderr, "previous declaration of '%s' was here\n",
488 return previous_declaration;
491 /* remember old declaration */
493 entry.symbol = symbol;
494 entry.old_declaration = symbol->declaration;
495 entry.namespc = namespc;
496 ARR_APP1(stack_entry_t, *stack_ptr, entry);
498 /* replace/add declaration into declaration list of the symbol */
499 if(symbol->declaration == NULL) {
500 symbol->declaration = declaration;
502 declaration_t *iter_last = NULL;
503 declaration_t *iter = symbol->declaration;
504 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
505 /* replace an entry? */
506 if(iter->namespc == namespc) {
507 if(iter_last == NULL) {
508 symbol->declaration = declaration;
510 iter_last->symbol_next = declaration;
512 declaration->symbol_next = iter->symbol_next;
517 assert(iter_last->symbol_next == NULL);
518 iter_last->symbol_next = declaration;
525 static declaration_t *environment_push(declaration_t *declaration)
527 assert(declaration->source_position.input_name != NULL);
528 return stack_push(&environment_stack, declaration, context);
531 static declaration_t *label_push(declaration_t *declaration)
533 return stack_push(&label_stack, declaration, ¤t_function->context);
537 * pops symbols from the environment stack until @p new_top is the top element
539 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
541 stack_entry_t *stack = *stack_ptr;
542 size_t top = ARR_LEN(stack);
545 assert(new_top <= top);
549 for(i = top; i > new_top; --i) {
550 stack_entry_t *entry = & stack[i - 1];
552 declaration_t *old_declaration = entry->old_declaration;
553 symbol_t *symbol = entry->symbol;
554 namespace_t namespc = (namespace_t)entry->namespc;
556 /* replace/remove declaration */
557 declaration_t *declaration = symbol->declaration;
558 assert(declaration != NULL);
559 if(declaration->namespc == namespc) {
560 if(old_declaration == NULL) {
561 symbol->declaration = declaration->symbol_next;
563 symbol->declaration = old_declaration;
566 declaration_t *iter_last = declaration;
567 declaration_t *iter = declaration->symbol_next;
568 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
569 /* replace an entry? */
570 if(iter->namespc == namespc) {
571 assert(iter_last != NULL);
572 iter_last->symbol_next = old_declaration;
573 old_declaration->symbol_next = iter->symbol_next;
577 assert(iter != NULL);
581 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
584 static void environment_pop_to(size_t new_top)
586 stack_pop_to(&environment_stack, new_top);
589 static void label_pop_to(size_t new_top)
591 stack_pop_to(&label_stack, new_top);
595 static int get_rank(const type_t *type)
597 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
598 * and esp. footnote 108). However we can't fold constants (yet), so we
599 * can't decide wether unsigned int is possible, while int always works.
600 * (unsigned int would be preferable when possible... for stuff like
601 * struct { enum { ... } bla : 4; } ) */
602 if(type->type == TYPE_ENUM)
603 return ATOMIC_TYPE_INT;
605 assert(type->type == TYPE_ATOMIC);
606 atomic_type_t *atomic_type = (atomic_type_t*) type;
607 atomic_type_type_t atype = atomic_type->atype;
611 static type_t *promote_integer(type_t *type)
613 if(get_rank(type) < ATOMIC_TYPE_INT)
619 static expression_t *create_cast_expression(expression_t *expression,
622 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
624 cast->expression.type = EXPR_UNARY;
625 cast->type = UNEXPR_CAST;
626 cast->value = expression;
627 cast->expression.datatype = dest_type;
629 return (expression_t*) cast;
632 static bool is_null_expression(const expression_t *const expr)
634 if (expr->type != EXPR_CONST) return false;
636 type_t *const type = skip_typeref(expr->datatype);
637 if (!is_type_integer(type)) return false;
639 const const_t *const const_expr = (const const_t*)expr;
640 return const_expr->v.int_value == 0;
643 static expression_t *create_implicit_cast(expression_t *expression,
646 type_t *source_type = expression->datatype;
648 if(source_type == NULL)
651 source_type = skip_typeref(source_type);
652 dest_type = skip_typeref(dest_type);
654 if(source_type == dest_type)
657 if(dest_type->type == TYPE_ATOMIC) {
658 if(source_type->type != TYPE_ATOMIC)
659 panic("casting of non-atomic types not implemented yet");
661 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
662 type_error_incompatible("can't cast types",
663 expression->source_position,
664 source_type, dest_type);
668 return create_cast_expression(expression, dest_type);
670 if(dest_type->type == TYPE_POINTER) {
671 pointer_type_t *pointer_type
672 = (pointer_type_t*) dest_type;
673 switch (source_type->type) {
675 if (is_null_expression(expression)) {
676 return create_cast_expression(expression, dest_type);
681 if (pointers_compatible(source_type, dest_type)) {
682 return create_cast_expression(expression, dest_type);
687 array_type_t *const array_type = (array_type_t*) source_type;
688 if (types_compatible(array_type->element_type,
689 pointer_type->points_to)) {
690 return create_cast_expression(expression, dest_type);
696 panic("casting of non-atomic types not implemented yet");
699 type_error_incompatible("can't implicitly cast types",
700 expression->source_position,
701 source_type, dest_type);
705 panic("casting of non-atomic types not implemented yet");
708 static bool is_atomic_type(const type_t *type, atomic_type_type_t atype)
710 if(type->type != TYPE_ATOMIC)
712 const atomic_type_t *atomic_type = (const atomic_type_t*) type;
714 return atomic_type->atype == atype;
717 static bool is_pointer(const type_t *type)
719 return type->type == TYPE_POINTER;
722 static bool is_compound_type(const type_t *type)
724 return type->type == TYPE_COMPOUND_STRUCT
725 || type->type == TYPE_COMPOUND_UNION;
728 /** Implements the rules from § 6.5.16.1 */
729 static void semantic_assign(type_t *orig_type_left, expression_t **right,
732 type_t *orig_type_right = (*right)->datatype;
734 if(orig_type_right == NULL)
737 type_t *const type_left = skip_typeref(orig_type_left);
738 type_t *const type_right = skip_typeref(orig_type_right);
740 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
741 (is_pointer(type_left) && is_null_expression(*right)) ||
742 (is_atomic_type(type_left, ATOMIC_TYPE_BOOL)
743 && is_pointer(type_right))) {
744 *right = create_implicit_cast(*right, type_left);
748 if (is_pointer(type_left) && is_pointer(type_right)) {
749 pointer_type_t *pointer_type_left = (pointer_type_t*) type_left;
750 pointer_type_t *pointer_type_right = (pointer_type_t*) type_right;
751 type_t *points_to_left = pointer_type_left->points_to;
752 type_t *points_to_right = pointer_type_right->points_to;
754 if(!is_atomic_type(points_to_left, ATOMIC_TYPE_VOID)
755 && !is_atomic_type(points_to_right, ATOMIC_TYPE_VOID)
756 && !types_compatible(points_to_left, points_to_right)) {
757 goto incompatible_assign_types;
760 /* the left type has all qualifiers from the right type */
761 unsigned missing_qualifiers
762 = points_to_right->qualifiers & ~points_to_left->qualifiers;
763 if(missing_qualifiers != 0) {
764 parser_print_error_prefix();
765 fprintf(stderr, "destination type ");
766 print_type_quoted(type_left);
767 fprintf(stderr, " in %s from type ", context);
768 print_type_quoted(type_right);
769 fprintf(stderr, " lacks qualifiers '");
770 print_type_qualifiers(missing_qualifiers);
771 fprintf(stderr, "' in pointed-to type\n");
775 *right = create_implicit_cast(*right, type_left);
779 if (is_compound_type(type_left)
780 && types_compatible(type_left, type_right)) {
781 *right = create_implicit_cast(*right, type_left);
785 incompatible_assign_types:
786 /* TODO: improve error message */
787 parser_print_error_prefix();
788 fprintf(stderr, "incompatible types in %s\n", context);
789 parser_print_error_prefix();
790 print_type_quoted(type_left);
791 fputs(" <- ", stderr);
792 print_type_quoted(type_right);
796 static expression_t *parse_constant_expression(void)
798 /* start parsing at precedence 7 (conditional expression) */
799 return parse_sub_expression(7);
802 static expression_t *parse_assignment_expression(void)
804 /* start parsing at precedence 2 (assignment expression) */
805 return parse_sub_expression(2);
808 typedef struct declaration_specifiers_t declaration_specifiers_t;
809 struct declaration_specifiers_t {
810 storage_class_t storage_class;
815 static void parse_compound_type_entries(void);
816 static declaration_t *parse_declarator(
817 const declaration_specifiers_t *specifiers, type_t *type,
818 bool may_be_abstract);
819 static declaration_t *record_declaration(declaration_t *declaration);
821 static const char *parse_string_literals(void)
823 assert(token.type == T_STRING_LITERAL);
824 const char *result = token.v.string;
828 while(token.type == T_STRING_LITERAL) {
829 result = concat_strings(result, token.v.string);
836 static void parse_attributes(void)
840 case T___attribute__:
848 parse_error("EOF while parsing attribute");
866 if(token.type != T_STRING_LITERAL) {
867 parse_error_expected("while parsing assembler attribute",
872 parse_string_literals();
877 goto attributes_finished;
886 static designator_t *parse_designation(void)
888 if(token.type != '[' && token.type != '.')
891 designator_t *result = NULL;
892 designator_t *last = NULL;
895 designator_t *designator;
898 designator = allocate_ast_zero(sizeof(designator[0]));
900 designator->array_access = parse_constant_expression();
904 designator = allocate_ast_zero(sizeof(designator[0]));
906 if(token.type != T_IDENTIFIER) {
907 parse_error_expected("while parsing designator",
911 designator->symbol = token.v.symbol;
919 assert(designator != NULL);
921 last->next = designator;
930 static initializer_t *initializer_from_string(array_type_t *type,
933 /* TODO: check len vs. size of array type */
936 initializer_string_t *initializer
937 = allocate_ast_zero(sizeof(initializer[0]));
939 initializer->initializer.type = INITIALIZER_STRING;
940 initializer->string = string;
942 return (initializer_t*) initializer;
945 static initializer_t *initializer_from_expression(type_t *type,
946 expression_t *expression)
949 /* TODO check that expression is a constant expression */
951 /* § 6.7.8.14/15 char array may be initialized by string literals */
952 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
953 array_type_t *array_type = (array_type_t*) type;
954 type_t *element_type = array_type->element_type;
956 if(element_type->type == TYPE_ATOMIC) {
957 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
958 atomic_type_type_t atype = atomic_type->atype;
960 /* TODO handle wide strings */
961 if(atype == ATOMIC_TYPE_CHAR
962 || atype == ATOMIC_TYPE_SCHAR
963 || atype == ATOMIC_TYPE_UCHAR) {
965 string_literal_t *literal = (string_literal_t*) expression;
966 return initializer_from_string(array_type, literal->value);
971 semantic_assign(type, &expression, "initializer");
973 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
974 result->initializer.type = INITIALIZER_VALUE;
975 result->value = expression;
977 return (initializer_t*) result;
980 static initializer_t *parse_sub_initializer(type_t *type,
981 expression_t *expression,
982 type_t *expression_type);
984 static initializer_t *parse_sub_initializer_elem(type_t *type)
986 if(token.type == '{') {
987 return parse_sub_initializer(type, NULL, NULL);
990 expression_t *expression = parse_assignment_expression();
991 type_t *expression_type = skip_typeref(expression->datatype);
993 return parse_sub_initializer(type, expression, expression_type);
996 static bool had_initializer_brace_warning;
998 static initializer_t *parse_sub_initializer(type_t *type,
999 expression_t *expression,
1000 type_t *expression_type)
1002 if(is_type_scalar(type)) {
1003 /* there might be extra {} hierarchies */
1004 if(token.type == '{') {
1006 if(!had_initializer_brace_warning) {
1007 parse_warning("braces around scalar initializer");
1008 had_initializer_brace_warning = true;
1010 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1011 if(token.type == ',') {
1013 /* TODO: warn about excessive elements */
1019 if(expression == NULL) {
1020 expression = parse_assignment_expression();
1022 return initializer_from_expression(type, expression);
1025 /* TODO: ignore qualifiers, comparing pointers is probably
1027 if(expression != NULL && expression_type == type) {
1028 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
1029 result->initializer.type = INITIALIZER_VALUE;
1032 semantic_assign(type, &expression, "initializer");
1034 result->value = expression;
1036 return (initializer_t*) result;
1039 bool read_paren = false;
1040 if(token.type == '{') {
1045 /* descend into subtype */
1046 initializer_t *result = NULL;
1047 initializer_t **elems;
1048 if(type->type == TYPE_ARRAY) {
1049 array_type_t *array_type = (array_type_t*) type;
1050 type_t *element_type = array_type->element_type;
1051 element_type = skip_typeref(element_type);
1054 had_initializer_brace_warning = false;
1055 if(expression == NULL) {
1056 sub = parse_sub_initializer_elem(element_type);
1058 sub = parse_sub_initializer(element_type, expression,
1062 /* didn't match the subtypes -> try the parent type */
1064 assert(!read_paren);
1068 elems = NEW_ARR_F(initializer_t*, 0);
1069 ARR_APP1(initializer_t*, elems, sub);
1072 if(token.type == '}')
1075 if(token.type == '}')
1079 = parse_sub_initializer(element_type, NULL, NULL);
1081 /* TODO error, do nicer cleanup */
1082 parse_error("member initializer didn't match");
1086 ARR_APP1(initializer_t*, elems, sub);
1089 assert(type->type == TYPE_COMPOUND_STRUCT
1090 || type->type == TYPE_COMPOUND_UNION);
1091 compound_type_t *compound_type = (compound_type_t*) type;
1092 context_t *context = & compound_type->declaration->context;
1094 declaration_t *first = context->declarations;
1097 type_t *first_type = first->type;
1098 first_type = skip_typeref(first_type);
1101 had_initializer_brace_warning = false;
1102 if(expression == NULL) {
1103 sub = parse_sub_initializer_elem(first_type);
1105 sub = parse_sub_initializer(first_type, expression,expression_type);
1108 /* didn't match the subtypes -> try our parent type */
1110 assert(!read_paren);
1114 elems = NEW_ARR_F(initializer_t*, 0);
1115 ARR_APP1(initializer_t*, elems, sub);
1117 declaration_t *iter = first->next;
1118 for( ; iter != NULL; iter = iter->next) {
1119 if(iter->symbol == NULL)
1121 if(iter->namespc != NAMESPACE_NORMAL)
1124 if(token.type == '}')
1128 type_t *iter_type = iter->type;
1129 iter_type = skip_typeref(iter_type);
1131 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1133 /* TODO error, do nicer cleanup*/
1134 parse_error("member initializer didn't match");
1138 ARR_APP1(initializer_t*, elems, sub);
1142 int len = ARR_LEN(elems);
1143 size_t elems_size = sizeof(initializer_t*) * len;
1145 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1147 init->initializer.type = INITIALIZER_LIST;
1149 memcpy(init->initializers, elems, elems_size);
1152 result = (initializer_t*) init;
1155 if(token.type == ',')
1162 static initializer_t *parse_initializer(type_t *type)
1164 initializer_t *result;
1166 type = skip_typeref(type);
1168 if(token.type != '{') {
1169 expression_t *expression = parse_assignment_expression();
1170 return initializer_from_expression(type, expression);
1173 if(is_type_scalar(type)) {
1177 expression_t *expression = parse_assignment_expression();
1178 result = initializer_from_expression(type, expression);
1180 if(token.type == ',')
1186 result = parse_sub_initializer(type, NULL, NULL);
1194 static declaration_t *parse_compound_type_specifier(bool is_struct)
1202 symbol_t *symbol = NULL;
1203 declaration_t *declaration = NULL;
1205 if (token.type == T___attribute__) {
1210 if(token.type == T_IDENTIFIER) {
1211 symbol = token.v.symbol;
1215 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1217 declaration = get_declaration(symbol, NAMESPACE_UNION);
1219 } else if(token.type != '{') {
1221 parse_error_expected("while parsing struct type specifier",
1222 T_IDENTIFIER, '{', 0);
1224 parse_error_expected("while parsing union type specifier",
1225 T_IDENTIFIER, '{', 0);
1231 if(declaration == NULL) {
1232 declaration = allocate_type_zero(sizeof(declaration[0]));
1235 declaration->namespc = NAMESPACE_STRUCT;
1237 declaration->namespc = NAMESPACE_UNION;
1239 declaration->source_position = token.source_position;
1240 declaration->symbol = symbol;
1241 record_declaration(declaration);
1244 if(token.type == '{') {
1245 if(declaration->init.is_defined) {
1246 assert(symbol != NULL);
1247 parser_print_error_prefix();
1248 fprintf(stderr, "multiple definition of %s %s\n",
1249 is_struct ? "struct" : "union", symbol->string);
1250 declaration->context.declarations = NULL;
1252 declaration->init.is_defined = true;
1254 int top = environment_top();
1255 context_t *last_context = context;
1256 set_context(& declaration->context);
1258 parse_compound_type_entries();
1261 assert(context == & declaration->context);
1262 set_context(last_context);
1263 environment_pop_to(top);
1269 static void parse_enum_entries(type_t *enum_type)
1273 if(token.type == '}') {
1275 parse_error("empty enum not allowed");
1280 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1282 if(token.type != T_IDENTIFIER) {
1283 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1287 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1288 entry->type = enum_type;
1289 entry->symbol = token.v.symbol;
1290 entry->source_position = token.source_position;
1293 if(token.type == '=') {
1295 entry->init.enum_value = parse_constant_expression();
1300 record_declaration(entry);
1302 if(token.type != ',')
1305 } while(token.type != '}');
1310 static declaration_t *parse_enum_specifier(void)
1314 declaration_t *declaration;
1317 if(token.type == T_IDENTIFIER) {
1318 symbol = token.v.symbol;
1321 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1322 } else if(token.type != '{') {
1323 parse_error_expected("while parsing enum type specifier",
1324 T_IDENTIFIER, '{', 0);
1331 if(declaration == NULL) {
1332 declaration = allocate_type_zero(sizeof(declaration[0]));
1334 declaration->namespc = NAMESPACE_ENUM;
1335 declaration->source_position = token.source_position;
1336 declaration->symbol = symbol;
1339 if(token.type == '{') {
1340 if(declaration->init.is_defined) {
1341 parser_print_error_prefix();
1342 fprintf(stderr, "multiple definitions of enum %s\n",
1345 record_declaration(declaration);
1346 declaration->init.is_defined = 1;
1348 parse_enum_entries(NULL);
1356 * if a symbol is a typedef to another type, return true
1358 static bool is_typedef_symbol(symbol_t *symbol)
1360 const declaration_t *const declaration =
1361 get_declaration(symbol, NAMESPACE_NORMAL);
1363 declaration != NULL &&
1364 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1367 static type_t *parse_typeof(void)
1375 expression_t *expression = NULL;
1378 switch(token.type) {
1379 case T___extension__:
1380 /* this can be a prefix to a typename or an expression */
1381 /* we simply eat it now. */
1384 } while(token.type == T___extension__);
1388 if(is_typedef_symbol(token.v.symbol)) {
1389 type = parse_typename();
1391 expression = parse_expression();
1392 type = expression->datatype;
1397 type = parse_typename();
1401 expression = parse_expression();
1402 type = expression->datatype;
1408 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1409 typeof->type.type = TYPE_TYPEOF;
1410 typeof->expression = expression;
1411 typeof->typeof_type = type;
1413 return (type_t*) typeof;
1417 SPECIFIER_SIGNED = 1 << 0,
1418 SPECIFIER_UNSIGNED = 1 << 1,
1419 SPECIFIER_LONG = 1 << 2,
1420 SPECIFIER_INT = 1 << 3,
1421 SPECIFIER_DOUBLE = 1 << 4,
1422 SPECIFIER_CHAR = 1 << 5,
1423 SPECIFIER_SHORT = 1 << 6,
1424 SPECIFIER_LONG_LONG = 1 << 7,
1425 SPECIFIER_FLOAT = 1 << 8,
1426 SPECIFIER_BOOL = 1 << 9,
1427 SPECIFIER_VOID = 1 << 10,
1428 #ifdef PROVIDE_COMPLEX
1429 SPECIFIER_COMPLEX = 1 << 11,
1430 SPECIFIER_IMAGINARY = 1 << 12,
1434 static type_t *create_builtin_type(symbol_t *symbol)
1436 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1437 type->type.type = TYPE_BUILTIN;
1438 type->symbol = symbol;
1440 type->real_type = type_int;
1442 return (type_t*) type;
1445 static type_t *get_typedef_type(symbol_t *symbol)
1447 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1448 if(declaration == NULL
1449 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1452 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1453 typedef_type->type.type = TYPE_TYPEDEF;
1454 typedef_type->declaration = declaration;
1456 return (type_t*) typedef_type;
1459 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1461 type_t *type = NULL;
1462 unsigned type_qualifiers = 0;
1463 unsigned type_specifiers = 0;
1467 switch(token.type) {
1470 #define MATCH_STORAGE_CLASS(token, class) \
1472 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1473 parse_error("multiple storage classes in declaration " \
1476 specifiers->storage_class = class; \
1480 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1481 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1482 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1483 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1484 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1486 /* type qualifiers */
1487 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1489 type_qualifiers |= qualifier; \
1493 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1494 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1495 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1497 case T___extension__:
1502 /* type specifiers */
1503 #define MATCH_SPECIFIER(token, specifier, name) \
1506 if(type_specifiers & specifier) { \
1507 parse_error("multiple " name " type specifiers given"); \
1509 type_specifiers |= specifier; \
1513 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1514 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1515 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1516 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1517 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1518 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1519 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1520 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1521 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1522 #ifdef PROVIDE_COMPLEX
1523 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1524 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1528 specifiers->is_inline = true;
1533 if(type_specifiers & SPECIFIER_LONG_LONG) {
1534 parse_error("multiple type specifiers given");
1535 } else if(type_specifiers & SPECIFIER_LONG) {
1536 type_specifiers |= SPECIFIER_LONG_LONG;
1538 type_specifiers |= SPECIFIER_LONG;
1542 /* TODO: if type != NULL for the following rules should issue
1545 compound_type_t *compound_type
1546 = allocate_type_zero(sizeof(compound_type[0]));
1547 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1548 compound_type->declaration = parse_compound_type_specifier(true);
1550 type = (type_t*) compound_type;
1554 compound_type_t *compound_type
1555 = allocate_type_zero(sizeof(compound_type[0]));
1556 compound_type->type.type = TYPE_COMPOUND_UNION;
1557 compound_type->declaration = parse_compound_type_specifier(false);
1559 type = (type_t*) compound_type;
1563 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1564 enum_type->type.type = TYPE_ENUM;
1565 enum_type->declaration = parse_enum_specifier();
1567 type = (type_t*) enum_type;
1571 type = parse_typeof();
1573 case T___builtin_va_list:
1574 type = create_builtin_type(token.v.symbol);
1578 case T___attribute__:
1583 case T_IDENTIFIER: {
1584 type_t *typedef_type = get_typedef_type(token.v.symbol);
1586 if(typedef_type == NULL)
1587 goto finish_specifiers;
1590 type = typedef_type;
1594 /* function specifier */
1596 goto finish_specifiers;
1603 atomic_type_type_t atomic_type;
1605 /* match valid basic types */
1606 switch(type_specifiers) {
1607 case SPECIFIER_VOID:
1608 atomic_type = ATOMIC_TYPE_VOID;
1610 case SPECIFIER_CHAR:
1611 atomic_type = ATOMIC_TYPE_CHAR;
1613 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1614 atomic_type = ATOMIC_TYPE_SCHAR;
1616 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1617 atomic_type = ATOMIC_TYPE_UCHAR;
1619 case SPECIFIER_SHORT:
1620 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1621 case SPECIFIER_SHORT | SPECIFIER_INT:
1622 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1623 atomic_type = ATOMIC_TYPE_SHORT;
1625 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1626 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1627 atomic_type = ATOMIC_TYPE_USHORT;
1630 case SPECIFIER_SIGNED:
1631 case SPECIFIER_SIGNED | SPECIFIER_INT:
1632 atomic_type = ATOMIC_TYPE_INT;
1634 case SPECIFIER_UNSIGNED:
1635 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1636 atomic_type = ATOMIC_TYPE_UINT;
1638 case SPECIFIER_LONG:
1639 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1640 case SPECIFIER_LONG | SPECIFIER_INT:
1641 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1642 atomic_type = ATOMIC_TYPE_LONG;
1644 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1645 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1646 atomic_type = ATOMIC_TYPE_ULONG;
1648 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1649 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1650 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1651 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1653 atomic_type = ATOMIC_TYPE_LONGLONG;
1655 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1656 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1658 atomic_type = ATOMIC_TYPE_ULONGLONG;
1660 case SPECIFIER_FLOAT:
1661 atomic_type = ATOMIC_TYPE_FLOAT;
1663 case SPECIFIER_DOUBLE:
1664 atomic_type = ATOMIC_TYPE_DOUBLE;
1666 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1667 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1669 case SPECIFIER_BOOL:
1670 atomic_type = ATOMIC_TYPE_BOOL;
1672 #ifdef PROVIDE_COMPLEX
1673 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1674 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1676 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1677 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1679 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1680 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1682 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1683 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1685 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1686 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1688 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1689 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1693 /* invalid specifier combination, give an error message */
1694 if(type_specifiers == 0) {
1696 parse_warning("no type specifiers in declaration (using int)");
1697 atomic_type = ATOMIC_TYPE_INT;
1700 parse_error("no type specifiers given in declaration");
1702 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1703 (type_specifiers & SPECIFIER_UNSIGNED)) {
1704 parse_error("signed and unsigned specifiers gives");
1705 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1706 parse_error("only integer types can be signed or unsigned");
1708 parse_error("multiple datatypes in declaration");
1710 atomic_type = ATOMIC_TYPE_INVALID;
1713 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1714 atype->type.type = TYPE_ATOMIC;
1715 atype->atype = atomic_type;
1718 type = (type_t*) atype;
1720 if(type_specifiers != 0) {
1721 parse_error("multiple datatypes in declaration");
1725 type->qualifiers = (type_qualifier_t)type_qualifiers;
1727 type_t *result = typehash_insert(type);
1728 if(newtype && result != (type_t*) type) {
1732 specifiers->type = result;
1735 static unsigned parse_type_qualifiers(void)
1737 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1740 switch(token.type) {
1741 /* type qualifiers */
1742 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1743 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1744 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1747 return type_qualifiers;
1752 static void parse_identifier_list(void)
1755 if(token.type != T_IDENTIFIER) {
1756 parse_error_expected("while parsing parameter identifier list",
1761 if(token.type != ',')
1767 static declaration_t *parse_parameter(void)
1769 declaration_specifiers_t specifiers;
1770 memset(&specifiers, 0, sizeof(specifiers));
1772 parse_declaration_specifiers(&specifiers);
1774 declaration_t *declaration
1775 = parse_declarator(&specifiers, specifiers.type, true);
1777 /* TODO check declaration constraints for parameters */
1778 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1779 parse_error("typedef not allowed in parameter list");
1782 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1784 if (declaration->type->type == TYPE_ARRAY) {
1785 const array_type_t *const arr_type =
1786 (const array_type_t*)declaration->type;
1788 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1794 static declaration_t *parse_parameters(function_type_t *type)
1796 if(token.type == T_IDENTIFIER) {
1797 symbol_t *symbol = token.v.symbol;
1798 if(!is_typedef_symbol(symbol)) {
1799 /* TODO: K&R style C parameters */
1800 parse_identifier_list();
1805 if(token.type == ')') {
1806 type->unspecified_parameters = 1;
1809 if(token.type == T_void && look_ahead(1)->type == ')') {
1814 declaration_t *declarations = NULL;
1815 declaration_t *declaration;
1816 declaration_t *last_declaration = NULL;
1817 function_parameter_t *parameter;
1818 function_parameter_t *last_parameter = NULL;
1821 switch(token.type) {
1825 return declarations;
1828 case T___extension__:
1830 declaration = parse_parameter();
1832 parameter = allocate_type_zero(sizeof(parameter[0]));
1833 parameter->type = declaration->type;
1835 if(last_parameter != NULL) {
1836 last_declaration->next = declaration;
1837 last_parameter->next = parameter;
1839 type->parameters = parameter;
1840 declarations = declaration;
1842 last_parameter = parameter;
1843 last_declaration = declaration;
1847 return declarations;
1849 if(token.type != ',')
1850 return declarations;
1860 } construct_type_type_t;
1862 typedef struct construct_type_t construct_type_t;
1863 struct construct_type_t {
1864 construct_type_type_t type;
1865 construct_type_t *next;
1868 typedef struct parsed_pointer_t parsed_pointer_t;
1869 struct parsed_pointer_t {
1870 construct_type_t construct_type;
1871 type_qualifier_t type_qualifiers;
1874 typedef struct construct_function_type_t construct_function_type_t;
1875 struct construct_function_type_t {
1876 construct_type_t construct_type;
1877 function_type_t *function_type;
1880 typedef struct parsed_array_t parsed_array_t;
1881 struct parsed_array_t {
1882 construct_type_t construct_type;
1883 type_qualifier_t type_qualifiers;
1889 typedef struct construct_base_type_t construct_base_type_t;
1890 struct construct_base_type_t {
1891 construct_type_t construct_type;
1895 static construct_type_t *parse_pointer_declarator(void)
1899 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1900 memset(pointer, 0, sizeof(pointer[0]));
1901 pointer->construct_type.type = CONSTRUCT_POINTER;
1902 pointer->type_qualifiers = parse_type_qualifiers();
1904 return (construct_type_t*) pointer;
1907 static construct_type_t *parse_array_declarator(void)
1911 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1912 memset(array, 0, sizeof(array[0]));
1913 array->construct_type.type = CONSTRUCT_ARRAY;
1915 if(token.type == T_static) {
1916 array->is_static = true;
1920 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1921 if(type_qualifiers != 0) {
1922 if(token.type == T_static) {
1923 array->is_static = true;
1927 array->type_qualifiers = type_qualifiers;
1929 if(token.type == '*' && look_ahead(1)->type == ']') {
1930 array->is_variable = true;
1932 } else if(token.type != ']') {
1933 array->size = parse_assignment_expression();
1938 return (construct_type_t*) array;
1941 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1945 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1946 type->type.type = TYPE_FUNCTION;
1948 declaration_t *parameters = parse_parameters(type);
1949 if(declaration != NULL) {
1950 declaration->context.declarations = parameters;
1953 construct_function_type_t *construct_function_type =
1954 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1955 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1956 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1957 construct_function_type->function_type = type;
1961 return (construct_type_t*) construct_function_type;
1964 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1965 bool may_be_abstract)
1967 /* construct a single linked list of construct_type_t's which describe
1968 * how to construct the final declarator type */
1969 construct_type_t *first = NULL;
1970 construct_type_t *last = NULL;
1973 while(token.type == '*') {
1974 construct_type_t *type = parse_pointer_declarator();
1985 /* TODO: find out if this is correct */
1988 construct_type_t *inner_types = NULL;
1990 switch(token.type) {
1992 if(declaration == NULL) {
1993 parse_error("no identifier expected in typename");
1995 declaration->symbol = token.v.symbol;
1996 declaration->source_position = token.source_position;
2002 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2008 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2009 /* avoid a loop in the outermost scope, because eat_statement doesn't
2011 if(token.type == '}' && current_function == NULL) {
2019 construct_type_t *p = last;
2022 construct_type_t *type;
2023 switch(token.type) {
2025 type = parse_function_declarator(declaration);
2028 type = parse_array_declarator();
2031 goto declarator_finished;
2034 /* insert in the middle of the list (behind p) */
2036 type->next = p->next;
2047 declarator_finished:
2050 /* append inner_types at the end of the list, we don't to set last anymore
2051 * as it's not needed anymore */
2053 assert(first == NULL);
2054 first = inner_types;
2056 last->next = inner_types;
2062 static type_t *construct_declarator_type(construct_type_t *construct_list,
2065 construct_type_t *iter = construct_list;
2066 for( ; iter != NULL; iter = iter->next) {
2067 parsed_pointer_t *parsed_pointer;
2068 parsed_array_t *parsed_array;
2069 construct_function_type_t *construct_function_type;
2070 function_type_t *function_type;
2071 pointer_type_t *pointer_type;
2072 array_type_t *array_type;
2074 switch(iter->type) {
2075 case CONSTRUCT_INVALID:
2076 panic("invalid type construction found");
2077 case CONSTRUCT_FUNCTION:
2078 construct_function_type = (construct_function_type_t*) iter;
2079 function_type = construct_function_type->function_type;
2081 function_type->result_type = type;
2082 type = (type_t*) function_type;
2085 case CONSTRUCT_POINTER:
2086 parsed_pointer = (parsed_pointer_t*) iter;
2087 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2089 pointer_type->type.type = TYPE_POINTER;
2090 pointer_type->points_to = type;
2091 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2092 type = (type_t*) pointer_type;
2095 case CONSTRUCT_ARRAY:
2096 parsed_array = (parsed_array_t*) iter;
2097 array_type = allocate_type_zero(sizeof(array_type[0]));
2099 array_type->type.type = TYPE_ARRAY;
2100 array_type->element_type = type;
2101 array_type->type.qualifiers = parsed_array->type_qualifiers;
2102 array_type->is_static = parsed_array->is_static;
2103 array_type->is_variable = parsed_array->is_variable;
2104 array_type->size = parsed_array->size;
2105 type = (type_t*) array_type;
2109 type_t *hashed_type = typehash_insert((type_t*) type);
2110 if(hashed_type != type) {
2111 /* the function type was constructed earlier freeing it here will
2112 * destroy other types... */
2113 if(iter->type != CONSTRUCT_FUNCTION) {
2123 static declaration_t *parse_declarator(
2124 const declaration_specifiers_t *specifiers,
2125 type_t *type, bool may_be_abstract)
2127 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2128 declaration->storage_class = specifiers->storage_class;
2129 declaration->is_inline = specifiers->is_inline;
2131 construct_type_t *construct_type
2132 = parse_inner_declarator(declaration, may_be_abstract);
2133 declaration->type = construct_declarator_type(construct_type, type);
2135 if(construct_type != NULL) {
2136 obstack_free(&temp_obst, construct_type);
2142 static type_t *parse_abstract_declarator(type_t *base_type)
2144 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2146 type_t *result = construct_declarator_type(construct_type, base_type);
2147 if(construct_type != NULL) {
2148 obstack_free(&temp_obst, construct_type);
2154 static declaration_t *record_declaration(declaration_t *declaration)
2156 assert(context != NULL);
2158 symbol_t *symbol = declaration->symbol;
2159 if(symbol != NULL) {
2160 declaration_t *alias = environment_push(declaration);
2161 if(alias != declaration)
2164 declaration->parent_context = context;
2167 if(last_declaration != NULL) {
2168 last_declaration->next = declaration;
2170 context->declarations = declaration;
2172 last_declaration = declaration;
2177 static void parser_error_multiple_definition(declaration_t *previous,
2178 declaration_t *declaration)
2180 parser_print_error_prefix_pos(declaration->source_position);
2181 fprintf(stderr, "multiple definition of symbol '%s'\n",
2182 declaration->symbol->string);
2183 parser_print_error_prefix_pos(previous->source_position);
2184 fprintf(stderr, "this is the location of the previous definition.\n");
2187 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2190 declaration_t *ndeclaration
2191 = parse_declarator(specifiers, specifiers->type, false);
2193 declaration_t *declaration = record_declaration(ndeclaration);
2195 type_t *orig_type = declaration->type;
2196 type_t *type = skip_typeref(orig_type);
2197 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2198 parser_print_warning_prefix_pos(declaration->source_position);
2199 fprintf(stderr, "variable '%s' declared 'inline'\n",
2200 declaration->symbol->string);
2203 if(token.type == '=') {
2206 /* TODO: check that this is an allowed type (no function type) */
2208 if(declaration->init.initializer != NULL) {
2209 parser_error_multiple_definition(declaration, ndeclaration);
2212 initializer_t *initializer = parse_initializer(type);
2214 if(type->type == TYPE_ARRAY && initializer != NULL) {
2215 array_type_t *array_type = (array_type_t*) type;
2217 if(array_type->size == NULL) {
2218 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2220 cnst->expression.type = EXPR_CONST;
2221 cnst->expression.datatype = type_size_t;
2223 if(initializer->type == INITIALIZER_LIST) {
2224 initializer_list_t *list
2225 = (initializer_list_t*) initializer;
2226 cnst->v.int_value = list->len;
2228 assert(initializer->type == INITIALIZER_STRING);
2229 initializer_string_t *string
2230 = (initializer_string_t*) initializer;
2231 cnst->v.int_value = strlen(string->string) + 1;
2234 array_type->size = (expression_t*) cnst;
2239 ndeclaration->init.initializer = initializer;
2240 } else if(token.type == '{') {
2241 if(type->type != TYPE_FUNCTION) {
2242 parser_print_error_prefix();
2243 fprintf(stderr, "declarator '");
2244 print_type_ext(orig_type, declaration->symbol, NULL);
2245 fprintf(stderr, "' has a body but is not a function type.\n");
2250 if(declaration->init.statement != NULL) {
2251 parser_error_multiple_definition(declaration, ndeclaration);
2253 if(ndeclaration != declaration) {
2254 memcpy(&declaration->context, &ndeclaration->context,
2255 sizeof(declaration->context));
2258 int top = environment_top();
2259 context_t *last_context = context;
2260 set_context(&declaration->context);
2262 /* push function parameters */
2263 declaration_t *parameter = declaration->context.declarations;
2264 for( ; parameter != NULL; parameter = parameter->next) {
2265 environment_push(parameter);
2268 int label_stack_top = label_top();
2269 declaration_t *old_current_function = current_function;
2270 current_function = declaration;
2272 statement_t *statement = parse_compound_statement();
2274 assert(current_function == declaration);
2275 current_function = old_current_function;
2276 label_pop_to(label_stack_top);
2278 assert(context == &declaration->context);
2279 set_context(last_context);
2280 environment_pop_to(top);
2282 declaration->init.statement = statement;
2286 if(token.type != ',')
2293 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2296 if(token.type == ':') {
2298 parse_constant_expression();
2299 /* TODO (bitfields) */
2301 declaration_t *declaration
2302 = parse_declarator(specifiers, specifiers->type, true);
2304 /* TODO: check constraints for struct declarations */
2305 /* TODO: check for doubled fields */
2306 record_declaration(declaration);
2308 if(token.type == ':') {
2310 parse_constant_expression();
2311 /* TODO (bitfields) */
2315 if(token.type != ',')
2322 static void parse_compound_type_entries(void)
2326 while(token.type != '}' && token.type != T_EOF) {
2327 declaration_specifiers_t specifiers;
2328 memset(&specifiers, 0, sizeof(specifiers));
2329 parse_declaration_specifiers(&specifiers);
2331 parse_struct_declarators(&specifiers);
2333 if(token.type == T_EOF) {
2334 parse_error("unexpected error while parsing struct");
2339 static void parse_declaration(void)
2341 source_position_t source_position = token.source_position;
2343 declaration_specifiers_t specifiers;
2344 memset(&specifiers, 0, sizeof(specifiers));
2345 parse_declaration_specifiers(&specifiers);
2347 if(token.type == ';') {
2348 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2349 parse_warning_pos(source_position,
2350 "useless keyword in empty declaration");
2352 switch (specifiers.type->type) {
2353 case TYPE_COMPOUND_STRUCT:
2354 case TYPE_COMPOUND_UNION: {
2355 const compound_type_t *const comp_type =
2356 (const compound_type_t*)specifiers.type;
2357 if (comp_type->declaration->symbol == NULL) {
2358 parse_warning_pos(source_position,
2359 "unnamed struct/union that defines no instances");
2364 case TYPE_ENUM: break;
2367 parse_warning_pos(source_position, "empty declaration");
2373 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2375 declaration->type = specifiers.type;
2376 declaration->storage_class = specifiers.storage_class;
2377 declaration->source_position = source_position;
2378 record_declaration(declaration);
2381 parse_init_declarators(&specifiers);
2384 static type_t *parse_typename(void)
2386 declaration_specifiers_t specifiers;
2387 memset(&specifiers, 0, sizeof(specifiers));
2388 parse_declaration_specifiers(&specifiers);
2389 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2390 /* TODO: improve error message, user does probably not know what a
2391 * storage class is...
2393 parse_error("typename may not have a storage class");
2396 type_t *result = parse_abstract_declarator(specifiers.type);
2404 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2405 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2406 expression_t *left);
2408 typedef struct expression_parser_function_t expression_parser_function_t;
2409 struct expression_parser_function_t {
2410 unsigned precedence;
2411 parse_expression_function parser;
2412 unsigned infix_precedence;
2413 parse_expression_infix_function infix_parser;
2416 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2418 static expression_t *make_invalid_expression(void)
2420 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2421 expression->type = EXPR_INVALID;
2422 expression->source_position = token.source_position;
2426 static expression_t *expected_expression_error(void)
2428 parser_print_error_prefix();
2429 fprintf(stderr, "expected expression, got token ");
2430 print_token(stderr, & token);
2431 fprintf(stderr, "\n");
2435 return make_invalid_expression();
2438 static expression_t *parse_string_const(void)
2440 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2442 cnst->expression.type = EXPR_STRING_LITERAL;
2443 cnst->expression.datatype = type_string;
2444 cnst->value = parse_string_literals();
2446 return (expression_t*) cnst;
2449 static expression_t *parse_int_const(void)
2451 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2453 cnst->expression.type = EXPR_CONST;
2454 cnst->expression.datatype = token.datatype;
2455 cnst->v.int_value = token.v.intvalue;
2459 return (expression_t*) cnst;
2462 static expression_t *parse_float_const(void)
2464 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2466 cnst->expression.type = EXPR_CONST;
2467 cnst->expression.datatype = token.datatype;
2468 cnst->v.float_value = token.v.floatvalue;
2472 return (expression_t*) cnst;
2475 static declaration_t *create_implicit_function(symbol_t *symbol,
2476 const source_position_t source_position)
2478 function_type_t *function_type
2479 = allocate_type_zero(sizeof(function_type[0]));
2481 function_type->type.type = TYPE_FUNCTION;
2482 function_type->result_type = type_int;
2483 function_type->unspecified_parameters = true;
2485 type_t *type = typehash_insert((type_t*) function_type);
2486 if(type != (type_t*) function_type) {
2487 free_type(function_type);
2490 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2492 declaration->storage_class = STORAGE_CLASS_EXTERN;
2493 declaration->type = type;
2494 declaration->symbol = symbol;
2495 declaration->source_position = source_position;
2497 /* prepend the implicit definition to the global context
2498 * this is safe since the symbol wasn't declared as anything else yet
2500 assert(symbol->declaration == NULL);
2502 context_t *last_context = context;
2503 context = global_context;
2505 environment_push(declaration);
2506 declaration->next = context->declarations;
2507 context->declarations = declaration;
2509 context = last_context;
2514 static expression_t *parse_reference(void)
2516 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2518 ref->expression.type = EXPR_REFERENCE;
2519 ref->symbol = token.v.symbol;
2521 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2523 source_position_t source_position = token.source_position;
2526 if(declaration == NULL) {
2528 /* an implicitly defined function */
2529 if(token.type == '(') {
2530 parser_print_prefix_pos(token.source_position);
2531 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2532 ref->symbol->string);
2534 declaration = create_implicit_function(ref->symbol,
2539 parser_print_error_prefix();
2540 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2541 return (expression_t*) ref;
2545 ref->declaration = declaration;
2546 ref->expression.datatype = declaration->type;
2548 return (expression_t*) ref;
2551 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2555 /* TODO check if explicit cast is allowed and issue warnings/errors */
2558 static expression_t *parse_cast(void)
2560 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2562 cast->expression.type = EXPR_UNARY;
2563 cast->type = UNEXPR_CAST;
2564 cast->expression.source_position = token.source_position;
2566 type_t *type = parse_typename();
2569 expression_t *value = parse_sub_expression(20);
2571 check_cast_allowed(value, type);
2573 cast->expression.datatype = type;
2574 cast->value = value;
2576 return (expression_t*) cast;
2579 static expression_t *parse_statement_expression(void)
2581 statement_expression_t *expression
2582 = allocate_ast_zero(sizeof(expression[0]));
2583 expression->expression.type = EXPR_STATEMENT;
2585 statement_t *statement = parse_compound_statement();
2586 expression->statement = statement;
2587 if(statement == NULL) {
2592 assert(statement->type == STATEMENT_COMPOUND);
2593 compound_statement_t *compound_statement
2594 = (compound_statement_t*) statement;
2596 /* find last statement and use it's type */
2597 const statement_t *last_statement = NULL;
2598 const statement_t *iter = compound_statement->statements;
2599 for( ; iter != NULL; iter = iter->next) {
2600 last_statement = iter;
2603 if(last_statement->type == STATEMENT_EXPRESSION) {
2604 const expression_statement_t *expression_statement =
2605 (const expression_statement_t*) last_statement;
2606 expression->expression.datatype
2607 = expression_statement->expression->datatype;
2609 expression->expression.datatype = type_void;
2614 return (expression_t*) expression;
2617 static expression_t *parse_brace_expression(void)
2621 switch(token.type) {
2623 /* gcc extension: a stement expression */
2624 return parse_statement_expression();
2628 return parse_cast();
2630 if(is_typedef_symbol(token.v.symbol)) {
2631 return parse_cast();
2635 expression_t *result = parse_expression();
2641 static expression_t *parse_function_keyword(void)
2646 if (current_function == NULL) {
2647 parse_error("'__func__' used outside of a function");
2650 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2651 expression->expression.type = EXPR_FUNCTION;
2652 expression->expression.datatype = type_string;
2653 expression->value = "TODO: FUNCTION";
2655 return (expression_t*) expression;
2658 static expression_t *parse_pretty_function_keyword(void)
2660 eat(T___PRETTY_FUNCTION__);
2663 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2664 expression->expression.type = EXPR_PRETTY_FUNCTION;
2665 expression->expression.datatype = type_string;
2666 expression->value = "TODO: PRETTY FUNCTION";
2668 return (expression_t*) expression;
2671 static designator_t *parse_designator(void)
2673 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2675 if(token.type != T_IDENTIFIER) {
2676 parse_error_expected("while parsing member designator",
2681 result->symbol = token.v.symbol;
2684 designator_t *last_designator = result;
2686 if(token.type == '.') {
2688 if(token.type != T_IDENTIFIER) {
2689 parse_error_expected("while parsing member designator",
2694 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2695 designator->symbol = token.v.symbol;
2698 last_designator->next = designator;
2699 last_designator = designator;
2702 if(token.type == '[') {
2704 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2705 designator->array_access = parse_expression();
2706 if(designator->array_access == NULL) {
2712 last_designator->next = designator;
2713 last_designator = designator;
2722 static expression_t *parse_offsetof(void)
2724 eat(T___builtin_offsetof);
2726 offsetof_expression_t *expression
2727 = allocate_ast_zero(sizeof(expression[0]));
2728 expression->expression.type = EXPR_OFFSETOF;
2729 expression->expression.datatype = type_size_t;
2732 expression->type = parse_typename();
2734 expression->designator = parse_designator();
2737 return (expression_t*) expression;
2740 static expression_t *parse_va_arg(void)
2742 eat(T___builtin_va_arg);
2744 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2745 expression->expression.type = EXPR_VA_ARG;
2748 expression->arg = parse_assignment_expression();
2750 expression->expression.datatype = parse_typename();
2753 return (expression_t*) expression;
2756 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2758 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2759 parameter->type = argument_type;
2761 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2762 type->type.type = TYPE_FUNCTION;
2763 type->result_type = result_type;
2764 type->parameters = parameter;
2766 type_t *result = typehash_insert((type_t*) type);
2767 if(result != (type_t*) type) {
2774 static expression_t *parse_builtin_symbol(void)
2776 builtin_symbol_expression_t *expression
2777 = allocate_ast_zero(sizeof(expression[0]));
2778 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2780 expression->symbol = token.v.symbol;
2783 switch(token.type) {
2784 case T___builtin_alloca:
2785 type = make_function_1_type(type_void_ptr, type_size_t);
2791 expression->expression.datatype = type;
2792 return (expression_t*) expression;
2795 static expression_t *parse_primary_expression(void)
2797 switch(token.type) {
2799 return parse_int_const();
2800 case T_FLOATINGPOINT:
2801 return parse_float_const();
2802 case T_STRING_LITERAL:
2803 return parse_string_const();
2805 return parse_reference();
2806 case T___FUNCTION__:
2808 return parse_function_keyword();
2809 case T___PRETTY_FUNCTION__:
2810 return parse_pretty_function_keyword();
2811 case T___builtin_offsetof:
2812 return parse_offsetof();
2813 case T___builtin_va_arg:
2814 return parse_va_arg();
2815 case T___builtin_alloca:
2816 case T___builtin_expect:
2817 case T___builtin_va_start:
2818 case T___builtin_va_end:
2819 return parse_builtin_symbol();
2822 return parse_brace_expression();
2825 parser_print_error_prefix();
2826 fprintf(stderr, "unexpected token ");
2827 print_token(stderr, &token);
2828 fprintf(stderr, "\n");
2831 return make_invalid_expression();
2834 static expression_t *parse_array_expression(unsigned precedence,
2835 expression_t *array_ref)
2841 expression_t *index = parse_expression();
2843 array_access_expression_t *array_access
2844 = allocate_ast_zero(sizeof(array_access[0]));
2846 array_access->expression.type = EXPR_ARRAY_ACCESS;
2847 array_access->array_ref = array_ref;
2848 array_access->index = index;
2850 type_t *type_left = skip_typeref(array_ref->datatype);
2851 type_t *type_right = skip_typeref(index->datatype);
2853 if(type_left != NULL && type_right != NULL) {
2854 if(type_left->type == TYPE_POINTER) {
2855 pointer_type_t *pointer = (pointer_type_t*) type_left;
2856 array_access->expression.datatype = pointer->points_to;
2857 } else if(type_left->type == TYPE_ARRAY) {
2858 array_type_t *array_type = (array_type_t*) type_left;
2859 array_access->expression.datatype = array_type->element_type;
2860 } else if(type_right->type == TYPE_POINTER) {
2861 pointer_type_t *pointer = (pointer_type_t*) type_right;
2862 array_access->expression.datatype = pointer->points_to;
2863 } else if(type_right->type == TYPE_ARRAY) {
2864 array_type_t *array_type = (array_type_t*) type_right;
2865 array_access->expression.datatype = array_type->element_type;
2867 parser_print_error_prefix();
2868 fprintf(stderr, "array access on object with non-pointer types ");
2869 print_type_quoted(type_left);
2870 fprintf(stderr, ", ");
2871 print_type_quoted(type_right);
2872 fprintf(stderr, "\n");
2876 if(token.type != ']') {
2877 parse_error_expected("Problem while parsing array access", ']', 0);
2878 return (expression_t*) array_access;
2882 return (expression_t*) array_access;
2885 static bool is_declaration_specifier(const token_t *token,
2886 bool only_type_specifiers)
2888 switch(token->type) {
2892 return is_typedef_symbol(token->v.symbol);
2895 if(only_type_specifiers)
2904 static expression_t *parse_sizeof(unsigned precedence)
2908 sizeof_expression_t *sizeof_expression
2909 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2910 sizeof_expression->expression.type = EXPR_SIZEOF;
2911 sizeof_expression->expression.datatype = type_size_t;
2913 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2915 sizeof_expression->type = parse_typename();
2918 expression_t *expression = parse_sub_expression(precedence);
2919 sizeof_expression->type = expression->datatype;
2920 sizeof_expression->size_expression = expression;
2923 return (expression_t*) sizeof_expression;
2926 static expression_t *parse_select_expression(unsigned precedence,
2927 expression_t *compound)
2930 assert(token.type == '.' || token.type == T_MINUSGREATER);
2932 bool is_pointer = (token.type == T_MINUSGREATER);
2935 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2937 select->expression.type = EXPR_SELECT;
2938 select->compound = compound;
2940 if(token.type != T_IDENTIFIER) {
2941 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2942 return (expression_t*) select;
2944 symbol_t *symbol = token.v.symbol;
2945 select->symbol = symbol;
2948 type_t *orig_type = compound->datatype;
2949 if(orig_type == NULL)
2950 return make_invalid_expression();
2952 type_t *type = skip_typeref(orig_type);
2954 type_t *type_left = type;
2956 if(type->type != TYPE_POINTER) {
2957 parser_print_error_prefix();
2958 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2959 print_type_quoted(orig_type);
2960 fputc('\n', stderr);
2961 return make_invalid_expression();
2963 pointer_type_t *pointer_type = (pointer_type_t*) type;
2964 type_left = pointer_type->points_to;
2966 type_left = skip_typeref(type_left);
2968 if(type_left->type != TYPE_COMPOUND_STRUCT
2969 && type_left->type != TYPE_COMPOUND_UNION) {
2970 parser_print_error_prefix();
2971 fprintf(stderr, "request for member '%s' in something not a struct or "
2972 "union, but ", symbol->string);
2973 print_type_quoted(type_left);
2974 fputc('\n', stderr);
2975 return make_invalid_expression();
2978 compound_type_t *compound_type = (compound_type_t*) type_left;
2979 declaration_t *declaration = compound_type->declaration;
2981 if(!declaration->init.is_defined) {
2982 parser_print_error_prefix();
2983 fprintf(stderr, "request for member '%s' of incomplete type ",
2985 print_type_quoted(type_left);
2986 fputc('\n', stderr);
2987 return make_invalid_expression();
2990 declaration_t *iter = declaration->context.declarations;
2991 for( ; iter != NULL; iter = iter->next) {
2992 if(iter->symbol == symbol) {
2997 parser_print_error_prefix();
2998 print_type_quoted(type_left);
2999 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3000 return make_invalid_expression();
3003 select->compound_entry = iter;
3004 select->expression.datatype = iter->type;
3005 return (expression_t*) select;
3008 static expression_t *parse_call_expression(unsigned precedence,
3009 expression_t *expression)
3012 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3013 call->expression.type = EXPR_CALL;
3014 call->function = expression;
3016 function_type_t *function_type;
3017 type_t *orig_type = expression->datatype;
3018 type_t *type = skip_typeref(orig_type);
3020 if(type->type == TYPE_POINTER) {
3021 pointer_type_t *pointer_type = (pointer_type_t*) type;
3023 type = skip_typeref(pointer_type->points_to);
3025 if (type->type == TYPE_FUNCTION) {
3026 function_type = (function_type_t*) type;
3027 call->expression.datatype = function_type->result_type;
3029 parser_print_error_prefix();
3030 fputs("called object '", stderr);
3031 print_expression(expression);
3032 fputs("' (type ", stderr);
3033 print_type_quoted(orig_type);
3034 fputs(") is not a function\n", stderr);
3036 function_type = NULL;
3037 call->expression.datatype = NULL;
3040 /* parse arguments */
3043 if(token.type != ')') {
3044 call_argument_t *last_argument = NULL;
3047 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3049 argument->expression = parse_assignment_expression();
3050 if(last_argument == NULL) {
3051 call->arguments = argument;
3053 last_argument->next = argument;
3055 last_argument = argument;
3057 if(token.type != ',')
3064 if(function_type != NULL) {
3065 function_parameter_t *parameter = function_type->parameters;
3066 call_argument_t *argument = call->arguments;
3067 for( ; parameter != NULL && argument != NULL;
3068 parameter = parameter->next, argument = argument->next) {
3069 type_t *expected_type = parameter->type;
3070 /* TODO report context in error messages */
3071 argument->expression = create_implicit_cast(argument->expression,
3074 /* too few parameters */
3075 if(parameter != NULL) {
3076 parser_print_error_prefix();
3077 fprintf(stderr, "too few arguments to function '");
3078 print_expression(expression);
3079 fprintf(stderr, "'\n");
3080 } else if(argument != NULL) {
3081 /* too many parameters */
3082 if(!function_type->variadic
3083 && !function_type->unspecified_parameters) {
3084 parser_print_error_prefix();
3085 fprintf(stderr, "too many arguments to function '");
3086 print_expression(expression);
3087 fprintf(stderr, "'\n");
3089 /* do default promotion */
3090 for( ; argument != NULL; argument = argument->next) {
3091 type_t *type = argument->expression->datatype;
3096 if(is_type_integer(type)) {
3097 type = promote_integer(type);
3098 } else if(type == type_float) {
3101 argument->expression
3102 = create_implicit_cast(argument->expression, type);
3108 return (expression_t*) call;
3111 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3113 static expression_t *parse_conditional_expression(unsigned precedence,
3114 expression_t *expression)
3118 conditional_expression_t *conditional
3119 = allocate_ast_zero(sizeof(conditional[0]));
3120 conditional->expression.type = EXPR_CONDITIONAL;
3121 conditional->condition = expression;
3124 type_t *condition_type_orig = conditional->condition->datatype;
3125 if(condition_type_orig != NULL) {
3126 type_t *condition_type = skip_typeref(condition_type_orig);
3127 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3128 type_error("expected a scalar type", expression->source_position,
3129 condition_type_orig);
3133 expression_t *const t_expr = parse_expression();
3134 conditional->true_expression = t_expr;
3136 expression_t *const f_expr = parse_sub_expression(precedence);
3137 conditional->false_expression = f_expr;
3139 type_t *const true_type = t_expr->datatype;
3140 if(true_type == NULL)
3141 return (expression_t*) conditional;
3142 type_t *const false_type = f_expr->datatype;
3143 if(false_type == NULL)
3144 return (expression_t*) conditional;
3146 type_t *const skipped_true_type = skip_typeref(true_type);
3147 type_t *const skipped_false_type = skip_typeref(false_type);
3150 if (skipped_true_type == skipped_false_type) {
3151 conditional->expression.datatype = skipped_true_type;
3152 } else if (is_type_arithmetic(skipped_true_type) &&
3153 is_type_arithmetic(skipped_false_type)) {
3154 type_t *const result = semantic_arithmetic(skipped_true_type,
3155 skipped_false_type);
3156 conditional->true_expression = create_implicit_cast(t_expr, result);
3157 conditional->false_expression = create_implicit_cast(f_expr, result);
3158 conditional->expression.datatype = result;
3159 } else if (skipped_true_type->type == TYPE_POINTER &&
3160 skipped_false_type->type == TYPE_POINTER &&
3161 true /* TODO compatible points_to types */) {
3163 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3164 skipped_false_type->type == TYPE_POINTER)
3165 || (is_null_ptr_const(skipped_false_type) &&
3166 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3168 } else if(/* 1 is pointer to object type, other is void* */ false) {
3171 type_error_incompatible("while parsing conditional",
3172 expression->source_position, true_type,
3173 skipped_false_type);
3176 return (expression_t*) conditional;
3179 static expression_t *parse_extension(unsigned precedence)
3181 eat(T___extension__);
3183 /* TODO enable extensions */
3185 return parse_sub_expression(precedence);
3188 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3190 eat(T___builtin_classify_type);
3192 classify_type_expression_t *const classify_type_expr =
3193 allocate_ast_zero(sizeof(classify_type_expr[0]));
3194 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3195 classify_type_expr->expression.datatype = type_int;
3198 expression_t *const expression = parse_sub_expression(precedence);
3200 classify_type_expr->type_expression = expression;
3202 return (expression_t*)classify_type_expr;
3205 static void semantic_incdec(unary_expression_t *expression)
3207 type_t *orig_type = expression->value->datatype;
3208 if(orig_type == NULL)
3211 type_t *type = skip_typeref(orig_type);
3212 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3213 /* TODO: improve error message */
3214 parser_print_error_prefix();
3215 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3219 expression->expression.datatype = orig_type;
3222 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3224 type_t *orig_type = expression->value->datatype;
3225 if(orig_type == NULL)
3228 type_t *type = skip_typeref(orig_type);
3229 if(!is_type_arithmetic(type)) {
3230 /* TODO: improve error message */
3231 parser_print_error_prefix();
3232 fprintf(stderr, "operation needs an arithmetic type\n");
3236 expression->expression.datatype = orig_type;
3239 static void semantic_unexpr_scalar(unary_expression_t *expression)
3241 type_t *orig_type = expression->value->datatype;
3242 if(orig_type == NULL)
3245 type_t *type = skip_typeref(orig_type);
3246 if (!is_type_scalar(type)) {
3247 parse_error("operand of ! must be of scalar type\n");
3251 expression->expression.datatype = orig_type;
3254 static void semantic_unexpr_integer(unary_expression_t *expression)
3256 type_t *orig_type = expression->value->datatype;
3257 if(orig_type == NULL)
3260 type_t *type = skip_typeref(orig_type);
3261 if (!is_type_integer(type)) {
3262 parse_error("operand of ~ must be of integer type\n");
3266 expression->expression.datatype = orig_type;
3269 static void semantic_dereference(unary_expression_t *expression)
3271 type_t *orig_type = expression->value->datatype;
3272 if(orig_type == NULL)
3275 type_t *type = skip_typeref(orig_type);
3276 switch (type->type) {
3278 array_type_t *const array_type = (array_type_t*)type;
3279 expression->expression.datatype = array_type->element_type;
3283 case TYPE_POINTER: {
3284 pointer_type_t *pointer_type = (pointer_type_t*)type;
3285 expression->expression.datatype = pointer_type->points_to;
3290 parser_print_error_prefix();
3291 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3292 print_type_quoted(orig_type);
3293 fputs(" given.\n", stderr);
3298 static void semantic_take_addr(unary_expression_t *expression)
3300 type_t *orig_type = expression->value->datatype;
3301 if(orig_type == NULL)
3304 expression_t *value = expression->value;
3305 if(value->type == EXPR_REFERENCE) {
3306 reference_expression_t *reference = (reference_expression_t*) value;
3307 declaration_t *declaration = reference->declaration;
3308 if(declaration != NULL) {
3309 declaration->address_taken = 1;
3313 expression->expression.datatype = make_pointer_type(orig_type, 0);
3316 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3317 static expression_t *parse_##unexpression_type(unsigned precedence) \
3321 unary_expression_t *unary_expression \
3322 = allocate_ast_zero(sizeof(unary_expression[0])); \
3323 unary_expression->expression.type = EXPR_UNARY; \
3324 unary_expression->type = unexpression_type; \
3325 unary_expression->value = parse_sub_expression(precedence); \
3327 sfunc(unary_expression); \
3329 return (expression_t*) unary_expression; \
3332 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3333 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3334 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3335 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3336 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3337 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3338 semantic_unexpr_integer)
3339 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3341 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3344 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3346 static expression_t *parse_##unexpression_type(unsigned precedence, \
3347 expression_t *left) \
3349 (void) precedence; \
3352 unary_expression_t *unary_expression \
3353 = allocate_ast_zero(sizeof(unary_expression[0])); \
3354 unary_expression->expression.type = EXPR_UNARY; \
3355 unary_expression->type = unexpression_type; \
3356 unary_expression->value = left; \
3358 sfunc(unary_expression); \
3360 return (expression_t*) unary_expression; \
3363 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3365 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3368 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3370 /* TODO: handle complex + imaginary types */
3372 /* § 6.3.1.8 Usual arithmetic conversions */
3373 if(type_left == type_long_double || type_right == type_long_double) {
3374 return type_long_double;
3375 } else if(type_left == type_double || type_right == type_double) {
3377 } else if(type_left == type_float || type_right == type_float) {
3381 type_right = promote_integer(type_right);
3382 type_left = promote_integer(type_left);
3384 if(type_left == type_right)
3387 bool signed_left = is_type_signed(type_left);
3388 bool signed_right = is_type_signed(type_right);
3389 if(get_rank(type_left) < get_rank(type_right)) {
3390 if(signed_left == signed_right || !signed_right) {
3396 if(signed_left == signed_right || !signed_left) {
3404 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3406 expression_t *left = expression->left;
3407 expression_t *right = expression->right;
3408 type_t *orig_type_left = left->datatype;
3409 type_t *orig_type_right = right->datatype;
3411 if(orig_type_left == NULL || orig_type_right == NULL)
3414 type_t *type_left = skip_typeref(orig_type_left);
3415 type_t *type_right = skip_typeref(orig_type_right);
3417 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3418 /* TODO: improve error message */
3419 parser_print_error_prefix();
3420 fprintf(stderr, "operation needs arithmetic types\n");
3424 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3425 expression->left = create_implicit_cast(left, arithmetic_type);
3426 expression->right = create_implicit_cast(right, arithmetic_type);
3427 expression->expression.datatype = arithmetic_type;
3430 static void semantic_shift_op(binary_expression_t *expression)
3432 expression_t *left = expression->left;
3433 expression_t *right = expression->right;
3434 type_t *orig_type_left = left->datatype;
3435 type_t *orig_type_right = right->datatype;
3437 if(orig_type_left == NULL || orig_type_right == NULL)
3440 type_t *type_left = skip_typeref(orig_type_left);
3441 type_t *type_right = skip_typeref(orig_type_right);
3443 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3444 /* TODO: improve error message */
3445 parser_print_error_prefix();
3446 fprintf(stderr, "operation needs integer types\n");
3450 type_left = promote_integer(type_left);
3451 type_right = promote_integer(type_right);
3453 expression->left = create_implicit_cast(left, type_left);
3454 expression->right = create_implicit_cast(right, type_right);
3455 expression->expression.datatype = type_left;
3458 static void semantic_add(binary_expression_t *expression)
3460 expression_t *left = expression->left;
3461 expression_t *right = expression->right;
3462 type_t *orig_type_left = left->datatype;
3463 type_t *orig_type_right = right->datatype;
3465 if(orig_type_left == NULL || orig_type_right == NULL)
3468 type_t *type_left = skip_typeref(orig_type_left);
3469 type_t *type_right = skip_typeref(orig_type_right);
3472 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3473 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3474 expression->left = create_implicit_cast(left, arithmetic_type);
3475 expression->right = create_implicit_cast(right, arithmetic_type);
3476 expression->expression.datatype = arithmetic_type;
3478 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3479 expression->expression.datatype = type_left;
3480 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3481 expression->expression.datatype = type_right;
3482 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3483 const array_type_t *const arr_type = (const array_type_t*)type_left;
3484 expression->expression.datatype =
3485 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3486 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3487 const array_type_t *const arr_type = (const array_type_t*)type_right;
3488 expression->expression.datatype =
3489 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3491 parser_print_error_prefix();
3492 fprintf(stderr, "invalid operands to binary + (");
3493 print_type_quoted(orig_type_left);
3494 fprintf(stderr, ", ");
3495 print_type_quoted(orig_type_right);
3496 fprintf(stderr, ")\n");
3500 static void semantic_sub(binary_expression_t *expression)
3502 expression_t *left = expression->left;
3503 expression_t *right = expression->right;
3504 type_t *orig_type_left = left->datatype;
3505 type_t *orig_type_right = right->datatype;
3507 if(orig_type_left == NULL || orig_type_right == NULL)
3510 type_t *type_left = skip_typeref(orig_type_left);
3511 type_t *type_right = skip_typeref(orig_type_right);
3514 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3515 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3516 expression->left = create_implicit_cast(left, arithmetic_type);
3517 expression->right = create_implicit_cast(right, arithmetic_type);
3518 expression->expression.datatype = arithmetic_type;
3520 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3521 expression->expression.datatype = type_left;
3522 } else if(type_left->type == TYPE_POINTER &&
3523 type_right->type == TYPE_POINTER) {
3524 if(!pointers_compatible(type_left, type_right)) {
3525 parser_print_error_prefix();
3526 fprintf(stderr, "pointers to incompatible objects to binary - (");
3527 print_type_quoted(orig_type_left);
3528 fprintf(stderr, ", ");
3529 print_type_quoted(orig_type_right);
3530 fprintf(stderr, ")\n");
3532 expression->expression.datatype = type_ptrdiff_t;
3535 parser_print_error_prefix();
3536 fprintf(stderr, "invalid operands to binary - (");
3537 print_type_quoted(orig_type_left);
3538 fprintf(stderr, ", ");
3539 print_type_quoted(orig_type_right);
3540 fprintf(stderr, ")\n");
3544 static void semantic_comparison(binary_expression_t *expression)
3546 expression_t *left = expression->left;
3547 expression_t *right = expression->right;
3548 type_t *orig_type_left = left->datatype;
3549 type_t *orig_type_right = right->datatype;
3551 if(orig_type_left == NULL || orig_type_right == NULL)
3554 type_t *type_left = skip_typeref(orig_type_left);
3555 type_t *type_right = skip_typeref(orig_type_right);
3557 /* TODO non-arithmetic types */
3558 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3559 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3560 expression->left = create_implicit_cast(left, arithmetic_type);
3561 expression->right = create_implicit_cast(right, arithmetic_type);
3562 expression->expression.datatype = arithmetic_type;
3563 } else if (type_left->type == TYPE_POINTER &&
3564 type_right->type == TYPE_POINTER) {
3565 /* TODO check compatibility */
3566 } else if (type_left->type == TYPE_POINTER) {
3567 expression->right = create_implicit_cast(right, type_left);
3568 } else if (type_right->type == TYPE_POINTER) {
3569 expression->left = create_implicit_cast(left, type_right);
3571 type_error_incompatible("invalid operands in comparison",
3572 token.source_position, type_left, type_right);
3574 expression->expression.datatype = type_int;
3577 static void semantic_arithmetic_assign(binary_expression_t *expression)
3579 expression_t *left = expression->left;
3580 expression_t *right = expression->right;
3581 type_t *orig_type_left = left->datatype;
3582 type_t *orig_type_right = right->datatype;
3584 if(orig_type_left == NULL || orig_type_right == NULL)
3587 type_t *type_left = skip_typeref(orig_type_left);
3588 type_t *type_right = skip_typeref(orig_type_right);
3590 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3591 /* TODO: improve error message */
3592 parser_print_error_prefix();
3593 fprintf(stderr, "operation needs arithmetic types\n");
3597 /* combined instructions are tricky. We can't create an implicit cast on
3598 * the left side, because we need the uncasted form for the store.
3599 * The ast2firm pass has to know that left_type must be right_type
3600 * for the arithmeitc operation and create a cast by itself */
3601 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3602 expression->right = create_implicit_cast(right, arithmetic_type);
3603 expression->expression.datatype = type_left;
3606 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3608 expression_t *left = expression->left;
3609 expression_t *right = expression->right;
3610 type_t *orig_type_left = left->datatype;
3611 type_t *orig_type_right = right->datatype;
3613 if(orig_type_left == NULL || orig_type_right == NULL)
3616 type_t *type_left = skip_typeref(orig_type_left);
3617 type_t *type_right = skip_typeref(orig_type_right);
3619 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3620 /* combined instructions are tricky. We can't create an implicit cast on
3621 * the left side, because we need the uncasted form for the store.
3622 * The ast2firm pass has to know that left_type must be right_type
3623 * for the arithmeitc operation and create a cast by itself */
3624 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3625 expression->right = create_implicit_cast(right, arithmetic_type);
3626 expression->expression.datatype = type_left;
3627 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3628 expression->expression.datatype = type_left;
3630 parser_print_error_prefix();
3631 fputs("Incompatible types ", stderr);
3632 print_type_quoted(orig_type_left);
3633 fputs(" and ", stderr);
3634 print_type_quoted(orig_type_right);
3635 fputs(" in assignment\n", stderr);
3640 static void semantic_logical_op(binary_expression_t *expression)
3642 expression_t *left = expression->left;
3643 expression_t *right = expression->right;
3644 type_t *orig_type_left = left->datatype;
3645 type_t *orig_type_right = right->datatype;
3647 if(orig_type_left == NULL || orig_type_right == NULL)
3650 type_t *type_left = skip_typeref(orig_type_left);
3651 type_t *type_right = skip_typeref(orig_type_right);
3653 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3654 /* TODO: improve error message */
3655 parser_print_error_prefix();
3656 fprintf(stderr, "operation needs scalar types\n");
3660 expression->expression.datatype = type_int;
3663 static void semantic_binexpr_assign(binary_expression_t *expression)
3665 expression_t *left = expression->left;
3666 type_t *orig_type_left = left->datatype;
3668 if(orig_type_left == NULL)
3671 type_t *type_left = skip_typeref(orig_type_left);
3673 if (type_left->type == TYPE_ARRAY) {
3674 parse_error("Cannot assign to arrays.");
3678 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3679 parser_print_error_prefix();
3680 fprintf(stderr, "assignment to readonly location '");
3681 print_expression(left);
3682 fprintf(stderr, "' (type ");
3683 print_type_quoted(orig_type_left);
3684 fprintf(stderr, ")\n");
3687 semantic_assign(orig_type_left, &expression->right, "assignment");
3689 expression->expression.datatype = orig_type_left;
3692 static void semantic_comma(binary_expression_t *expression)
3694 expression->expression.datatype = expression->right->datatype;
3697 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3698 static expression_t *parse_##binexpression_type(unsigned precedence, \
3699 expression_t *left) \
3703 expression_t *right = parse_sub_expression(precedence + lr); \
3705 binary_expression_t *binexpr \
3706 = allocate_ast_zero(sizeof(binexpr[0])); \
3707 binexpr->expression.type = EXPR_BINARY; \
3708 binexpr->type = binexpression_type; \
3709 binexpr->left = left; \
3710 binexpr->right = right; \
3713 return (expression_t*) binexpr; \
3716 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3717 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3718 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3719 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3720 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3721 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3722 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3723 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3724 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3725 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3726 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3727 semantic_comparison, 1)
3728 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3729 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3730 semantic_comparison, 1)
3731 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3732 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3733 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3734 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3735 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3736 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3737 semantic_shift_op, 1)
3738 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3739 semantic_shift_op, 1)
3740 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3741 semantic_arithmetic_addsubb_assign, 0)
3742 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3743 semantic_arithmetic_addsubb_assign, 0)
3744 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3745 semantic_arithmetic_assign, 0)
3746 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3747 semantic_arithmetic_assign, 0)
3748 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3749 semantic_arithmetic_assign, 0)
3750 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3751 semantic_arithmetic_assign, 0)
3752 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3753 semantic_arithmetic_assign, 0)
3754 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3755 semantic_arithmetic_assign, 0)
3756 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3757 semantic_arithmetic_assign, 0)
3758 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3759 semantic_arithmetic_assign, 0)
3761 static expression_t *parse_sub_expression(unsigned precedence)
3763 if(token.type < 0) {
3764 return expected_expression_error();
3767 expression_parser_function_t *parser
3768 = &expression_parsers[token.type];
3769 source_position_t source_position = token.source_position;
3772 if(parser->parser != NULL) {
3773 left = parser->parser(parser->precedence);
3775 left = parse_primary_expression();
3777 assert(left != NULL);
3778 left->source_position = source_position;
3781 if(token.type < 0) {
3782 return expected_expression_error();
3785 parser = &expression_parsers[token.type];
3786 if(parser->infix_parser == NULL)
3788 if(parser->infix_precedence < precedence)
3791 left = parser->infix_parser(parser->infix_precedence, left);
3793 assert(left != NULL);
3794 assert(left->type != EXPR_UNKNOWN);
3795 left->source_position = source_position;
3801 static expression_t *parse_expression(void)
3803 return parse_sub_expression(1);
3808 static void register_expression_parser(parse_expression_function parser,
3809 int token_type, unsigned precedence)
3811 expression_parser_function_t *entry = &expression_parsers[token_type];
3813 if(entry->parser != NULL) {
3814 fprintf(stderr, "for token ");
3815 print_token_type(stderr, token_type);
3816 fprintf(stderr, "\n");
3817 panic("trying to register multiple expression parsers for a token");
3819 entry->parser = parser;
3820 entry->precedence = precedence;
3823 static void register_expression_infix_parser(
3824 parse_expression_infix_function parser, int token_type,
3825 unsigned precedence)
3827 expression_parser_function_t *entry = &expression_parsers[token_type];
3829 if(entry->infix_parser != NULL) {
3830 fprintf(stderr, "for token ");
3831 print_token_type(stderr, token_type);
3832 fprintf(stderr, "\n");
3833 panic("trying to register multiple infix expression parsers for a "
3836 entry->infix_parser = parser;
3837 entry->infix_precedence = precedence;
3840 static void init_expression_parsers(void)
3842 memset(&expression_parsers, 0, sizeof(expression_parsers));
3844 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3845 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3846 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3847 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3848 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3849 T_GREATERGREATER, 16);
3850 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3851 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3852 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3853 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3854 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3855 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3856 T_GREATEREQUAL, 14);
3857 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3858 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3859 T_EXCLAMATIONMARKEQUAL, 13);
3860 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3861 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3862 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3863 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3864 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3865 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3866 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3867 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3868 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3869 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3870 T_ASTERISKEQUAL, 2);
3871 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3872 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3874 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3875 T_LESSLESSEQUAL, 2);
3876 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3877 T_GREATERGREATEREQUAL, 2);
3878 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3880 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3882 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3885 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3887 register_expression_infix_parser(parse_array_expression, '[', 30);
3888 register_expression_infix_parser(parse_call_expression, '(', 30);
3889 register_expression_infix_parser(parse_select_expression, '.', 30);
3890 register_expression_infix_parser(parse_select_expression,
3891 T_MINUSGREATER, 30);
3892 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3894 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3897 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3898 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3899 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3900 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3901 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3902 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3903 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3904 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3905 register_expression_parser(parse_sizeof, T_sizeof, 25);
3906 register_expression_parser(parse_extension, T___extension__, 25);
3907 register_expression_parser(parse_builtin_classify_type,
3908 T___builtin_classify_type, 25);
3912 static statement_t *parse_case_statement(void)
3915 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3916 label->statement.type = STATEMENT_CASE_LABEL;
3917 label->statement.source_position = token.source_position;
3919 label->expression = parse_expression();
3922 label->label_statement = parse_statement();
3924 return (statement_t*) label;
3927 static statement_t *parse_default_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;
3936 label->label_statement = parse_statement();
3938 return (statement_t*) label;
3941 static declaration_t *get_label(symbol_t *symbol)
3943 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3944 assert(current_function != NULL);
3945 /* if we found a label in the same function, then we already created the
3947 if(candidate != NULL
3948 && candidate->parent_context == ¤t_function->context) {
3952 /* otherwise we need to create a new one */
3953 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3954 declaration->namespc = NAMESPACE_LABEL;
3955 declaration->symbol = symbol;
3957 label_push(declaration);
3962 static statement_t *parse_label_statement(void)
3964 assert(token.type == T_IDENTIFIER);
3965 symbol_t *symbol = token.v.symbol;
3968 declaration_t *label = get_label(symbol);
3970 /* if source position is already set then the label is defined twice,
3971 * otherwise it was just mentioned in a goto so far */
3972 if(label->source_position.input_name != NULL) {
3973 parser_print_error_prefix();
3974 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3975 parser_print_error_prefix_pos(label->source_position);
3976 fprintf(stderr, "previous definition of '%s' was here\n",
3979 label->source_position = token.source_position;
3982 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3984 label_statement->statement.type = STATEMENT_LABEL;
3985 label_statement->statement.source_position = token.source_position;
3986 label_statement->label = label;
3990 if(token.type == '}') {
3991 parse_error("label at end of compound statement");
3992 return (statement_t*) label_statement;
3994 label_statement->label_statement = parse_statement();
3997 return (statement_t*) label_statement;
4000 static statement_t *parse_if(void)
4004 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4005 statement->statement.type = STATEMENT_IF;
4006 statement->statement.source_position = token.source_position;
4009 statement->condition = parse_expression();
4012 statement->true_statement = parse_statement();
4013 if(token.type == T_else) {
4015 statement->false_statement = parse_statement();
4018 return (statement_t*) statement;
4021 static statement_t *parse_switch(void)
4025 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4026 statement->statement.type = STATEMENT_SWITCH;
4027 statement->statement.source_position = token.source_position;
4030 statement->expression = parse_expression();
4032 statement->body = parse_statement();
4034 return (statement_t*) statement;
4037 static statement_t *parse_while(void)
4041 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4042 statement->statement.type = STATEMENT_WHILE;
4043 statement->statement.source_position = token.source_position;
4046 statement->condition = parse_expression();
4048 statement->body = parse_statement();
4050 return (statement_t*) statement;
4053 static statement_t *parse_do(void)
4057 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4058 statement->statement.type = STATEMENT_DO_WHILE;
4059 statement->statement.source_position = token.source_position;
4061 statement->body = parse_statement();
4064 statement->condition = parse_expression();
4068 return (statement_t*) statement;
4071 static statement_t *parse_for(void)
4075 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4076 statement->statement.type = STATEMENT_FOR;
4077 statement->statement.source_position = token.source_position;
4081 int top = environment_top();
4082 context_t *last_context = context;
4083 set_context(&statement->context);
4085 if(token.type != ';') {
4086 if(is_declaration_specifier(&token, false)) {
4087 parse_declaration();
4089 statement->initialisation = parse_expression();
4096 if(token.type != ';') {
4097 statement->condition = parse_expression();
4100 if(token.type != ')') {
4101 statement->step = parse_expression();
4104 statement->body = parse_statement();
4106 assert(context == &statement->context);
4107 set_context(last_context);
4108 environment_pop_to(top);
4110 return (statement_t*) statement;
4113 static statement_t *parse_goto(void)
4117 if(token.type != T_IDENTIFIER) {
4118 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4122 symbol_t *symbol = token.v.symbol;
4125 declaration_t *label = get_label(symbol);
4127 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4129 statement->statement.type = STATEMENT_GOTO;
4130 statement->statement.source_position = token.source_position;
4132 statement->label = label;
4136 return (statement_t*) statement;
4139 static statement_t *parse_continue(void)
4144 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4145 statement->type = STATEMENT_CONTINUE;
4146 statement->source_position = token.source_position;
4151 static statement_t *parse_break(void)
4156 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4157 statement->type = STATEMENT_BREAK;
4158 statement->source_position = token.source_position;
4163 static statement_t *parse_return(void)
4167 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4169 statement->statement.type = STATEMENT_RETURN;
4170 statement->statement.source_position = token.source_position;
4172 assert(current_function->type->type == TYPE_FUNCTION);
4173 function_type_t *function_type = (function_type_t*) current_function->type;
4174 type_t *return_type = function_type->result_type;
4176 expression_t *return_value;
4177 if(token.type != ';') {
4178 return_value = parse_expression();
4180 if(return_type == type_void && return_value->datatype != type_void) {
4181 parse_warning("'return' with a value, in function returning void");
4182 return_value = NULL;
4184 if(return_type != NULL) {
4185 semantic_assign(return_type, &return_value, "'return'");
4189 return_value = NULL;
4190 if(return_type != type_void) {
4191 parse_warning("'return' without value, in function returning "
4195 statement->return_value = return_value;
4199 return (statement_t*) statement;
4202 static statement_t *parse_declaration_statement(void)
4204 declaration_t *before = last_declaration;
4206 declaration_statement_t *statement
4207 = allocate_ast_zero(sizeof(statement[0]));
4208 statement->statement.type = STATEMENT_DECLARATION;
4209 statement->statement.source_position = token.source_position;
4211 declaration_specifiers_t specifiers;
4212 memset(&specifiers, 0, sizeof(specifiers));
4213 parse_declaration_specifiers(&specifiers);
4215 if(token.type == ';') {
4218 parse_init_declarators(&specifiers);
4221 if(before == NULL) {
4222 statement->declarations_begin = context->declarations;
4224 statement->declarations_begin = before->next;
4226 statement->declarations_end = last_declaration;
4228 return (statement_t*) statement;
4231 static statement_t *parse_expression_statement(void)
4233 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4234 statement->statement.type = STATEMENT_EXPRESSION;
4235 statement->statement.source_position = token.source_position;
4237 statement->expression = parse_expression();
4241 return (statement_t*) statement;
4244 static statement_t *parse_statement(void)
4246 statement_t *statement = NULL;
4248 /* declaration or statement */
4249 switch(token.type) {
4251 statement = parse_case_statement();
4255 statement = parse_default_statement();
4259 statement = parse_compound_statement();
4263 statement = parse_if();
4267 statement = parse_switch();
4271 statement = parse_while();
4275 statement = parse_do();
4279 statement = parse_for();
4283 statement = parse_goto();
4287 statement = parse_continue();
4291 statement = parse_break();
4295 statement = parse_return();
4304 if(look_ahead(1)->type == ':') {
4305 statement = parse_label_statement();
4309 if(is_typedef_symbol(token.v.symbol)) {
4310 statement = parse_declaration_statement();
4314 statement = parse_expression_statement();
4317 case T___extension__:
4318 /* this can be a prefix to a declaration or an expression statement */
4319 /* we simply eat it now and parse the rest with tail recursion */
4322 } while(token.type == T___extension__);
4323 statement = parse_statement();
4327 statement = parse_declaration_statement();
4331 statement = parse_expression_statement();
4335 assert(statement == NULL || statement->source_position.input_name != NULL);
4340 static statement_t *parse_compound_statement(void)
4342 compound_statement_t *compound_statement
4343 = allocate_ast_zero(sizeof(compound_statement[0]));
4344 compound_statement->statement.type = STATEMENT_COMPOUND;
4345 compound_statement->statement.source_position = token.source_position;
4349 int top = environment_top();
4350 context_t *last_context = context;
4351 set_context(&compound_statement->context);
4353 statement_t *last_statement = NULL;
4355 while(token.type != '}' && token.type != T_EOF) {
4356 statement_t *statement = parse_statement();
4357 if(statement == NULL)
4360 if(last_statement != NULL) {
4361 last_statement->next = statement;
4363 compound_statement->statements = statement;
4366 while(statement->next != NULL)
4367 statement = statement->next;
4369 last_statement = statement;
4372 if(token.type != '}') {
4373 parser_print_error_prefix_pos(
4374 compound_statement->statement.source_position);
4375 fprintf(stderr, "end of file while looking for closing '}'\n");
4379 assert(context == &compound_statement->context);
4380 set_context(last_context);
4381 environment_pop_to(top);
4383 return (statement_t*) compound_statement;
4386 static translation_unit_t *parse_translation_unit(void)
4388 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4390 assert(global_context == NULL);
4391 global_context = &unit->context;
4393 assert(context == NULL);
4394 set_context(&unit->context);
4396 while(token.type != T_EOF) {
4397 parse_declaration();
4400 assert(context == &unit->context);
4402 last_declaration = NULL;
4404 assert(global_context == &unit->context);
4405 global_context = NULL;
4410 translation_unit_t *parse(void)
4412 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4413 label_stack = NEW_ARR_F(stack_entry_t, 0);
4414 found_error = false;
4416 type_set_output(stderr);
4417 ast_set_output(stderr);
4419 lookahead_bufpos = 0;
4420 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4423 translation_unit_t *unit = parse_translation_unit();
4425 DEL_ARR_F(environment_stack);
4426 DEL_ARR_F(label_stack);
4434 void init_parser(void)
4436 init_expression_parsers();
4437 obstack_init(&temp_obst);
4439 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4440 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4441 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4442 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4443 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4444 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4445 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4446 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4447 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4448 type_void_ptr = make_pointer_type(type_void, 0);
4449 type_string = make_pointer_type(type_const_char, 0);
4452 void exit_parser(void)
4454 obstack_free(&temp_obst, NULL);