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 = (storage_class_t)previous_declaration->storage_class;
441 const storage_class_t new_storage = (storage_class_t)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 whether 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_type_t atype = type->v.atomic_type.atype;
610 static type_t *promote_integer(type_t *type)
612 if(get_rank(type) < ATOMIC_TYPE_INT)
618 static expression_t *create_cast_expression(expression_t *expression,
621 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
623 cast->expression.type = EXPR_UNARY;
624 cast->type = UNEXPR_CAST;
625 cast->value = expression;
626 cast->expression.datatype = dest_type;
628 return (expression_t*) cast;
631 static bool is_null_expression(const expression_t *const expr)
633 if (expr->type != EXPR_CONST) return false;
635 type_t *const type = skip_typeref(expr->datatype);
636 if (!is_type_integer(type)) return false;
638 const const_t *const const_expr = (const const_t*)expr;
639 return const_expr->v.int_value == 0;
642 static expression_t *create_implicit_cast(expression_t *expression,
645 type_t *source_type = expression->datatype;
647 if(source_type == NULL)
650 source_type = skip_typeref(source_type);
651 dest_type = skip_typeref(dest_type);
653 if(source_type == dest_type)
656 if(dest_type->type == TYPE_ATOMIC) {
657 if(source_type->type != TYPE_ATOMIC)
658 panic("casting of non-atomic types not implemented yet");
660 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
661 type_error_incompatible("can't cast types",
662 expression->source_position,
663 source_type, dest_type);
667 return create_cast_expression(expression, dest_type);
669 if(dest_type->type == TYPE_POINTER) {
670 type_t *pointer_type = dest_type;
671 switch (source_type->type) {
673 if (is_null_expression(expression)) {
674 return create_cast_expression(expression, dest_type);
679 if (pointers_compatible(source_type, dest_type)) {
680 return create_cast_expression(expression, dest_type);
685 type_t *const array_type = source_type;
686 if (types_compatible(array_type->v.array_type.element_type,
687 pointer_type->v.pointer_type.points_to)) {
688 return create_cast_expression(expression, dest_type);
694 panic("casting of non-atomic types not implemented yet");
697 type_error_incompatible("can't implicitly cast types",
698 expression->source_position,
699 source_type, dest_type);
703 panic("casting of non-atomic types not implemented yet");
706 static bool is_atomic_type(const type_t *type, atomic_type_type_t atype)
708 if(type->type != TYPE_ATOMIC)
710 return type->v.atomic_type.atype == atype;
713 static bool is_pointer(const type_t *type)
715 return type->type == TYPE_POINTER;
718 static bool is_compound_type(const type_t *type)
720 return type->type == TYPE_COMPOUND_STRUCT
721 || type->type == TYPE_COMPOUND_UNION;
724 /** Implements the rules from § 6.5.16.1 */
725 static void semantic_assign(type_t *orig_type_left, expression_t **right,
728 type_t *orig_type_right = (*right)->datatype;
730 if(orig_type_right == NULL)
733 type_t *const type_left = skip_typeref(orig_type_left);
734 type_t *const type_right = skip_typeref(orig_type_right);
736 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
737 (is_pointer(type_left) && is_null_expression(*right)) ||
738 (is_atomic_type(type_left, ATOMIC_TYPE_BOOL)
739 && is_pointer(type_right))) {
740 *right = create_implicit_cast(*right, type_left);
744 if (is_pointer(type_left) && is_pointer(type_right)) {
745 type_t *pointer_type_left = type_left;
746 type_t *pointer_type_right = type_right;
747 type_t *points_to_left = pointer_type_left->v.pointer_type.points_to;
748 type_t *points_to_right = pointer_type_right->v.pointer_type.points_to;
750 if(!is_atomic_type(points_to_left, ATOMIC_TYPE_VOID)
751 && !is_atomic_type(points_to_right, ATOMIC_TYPE_VOID)
752 && !types_compatible(points_to_left, points_to_right)) {
753 goto incompatible_assign_types;
756 /* the left type has all qualifiers from the right type */
757 unsigned missing_qualifiers
758 = points_to_right->qualifiers & ~points_to_left->qualifiers;
759 if(missing_qualifiers != 0) {
760 parser_print_error_prefix();
761 fprintf(stderr, "destination type ");
762 print_type_quoted(type_left);
763 fprintf(stderr, " in %s from type ", context);
764 print_type_quoted(type_right);
765 fprintf(stderr, " lacks qualifiers '");
766 print_type_qualifiers(missing_qualifiers);
767 fprintf(stderr, "' in pointed-to type\n");
771 *right = create_implicit_cast(*right, type_left);
775 if (is_compound_type(type_left)
776 && types_compatible(type_left, type_right)) {
777 *right = create_implicit_cast(*right, type_left);
781 incompatible_assign_types:
782 /* TODO: improve error message */
783 parser_print_error_prefix();
784 fprintf(stderr, "incompatible types in %s\n", context);
785 parser_print_error_prefix();
786 print_type_quoted(type_left);
787 fputs(" <- ", stderr);
788 print_type_quoted(type_right);
792 static expression_t *parse_constant_expression(void)
794 /* start parsing at precedence 7 (conditional expression) */
795 return parse_sub_expression(7);
798 static expression_t *parse_assignment_expression(void)
800 /* start parsing at precedence 2 (assignment expression) */
801 return parse_sub_expression(2);
804 typedef struct declaration_specifiers_t declaration_specifiers_t;
805 struct declaration_specifiers_t {
806 storage_class_t storage_class;
811 static void parse_compound_type_entries(void);
812 static declaration_t *parse_declarator(
813 const declaration_specifiers_t *specifiers, type_t *type,
814 bool may_be_abstract);
815 static declaration_t *record_declaration(declaration_t *declaration);
817 static const char *parse_string_literals(void)
819 assert(token.type == T_STRING_LITERAL);
820 const char *result = token.v.string;
824 while(token.type == T_STRING_LITERAL) {
825 result = concat_strings(result, token.v.string);
832 static void parse_attributes(void)
836 case T___attribute__:
840 for (int depth = 1; depth > 0;) {
843 parse_error("EOF while parsing attribute");
861 if(token.type != T_STRING_LITERAL) {
862 parse_error_expected("while parsing assembler attribute",
867 parse_string_literals();
872 goto attributes_finished;
881 static designator_t *parse_designation(void)
883 if(token.type != '[' && token.type != '.')
886 designator_t *result = NULL;
887 designator_t *last = NULL;
890 designator_t *designator;
893 designator = allocate_ast_zero(sizeof(designator[0]));
895 designator->array_access = parse_constant_expression();
899 designator = allocate_ast_zero(sizeof(designator[0]));
901 if(token.type != T_IDENTIFIER) {
902 parse_error_expected("while parsing designator",
906 designator->symbol = token.v.symbol;
914 assert(designator != NULL);
916 last->next = designator;
925 static initializer_t *initializer_from_string(type_t *type, const char *string)
927 /* TODO: check len vs. size of array type */
930 initializer_t *initializer
931 = allocate_ast_zero(sizeof(initializer[0]));
933 initializer->type = INITIALIZER_STRING;
934 initializer->v.string = string;
939 static initializer_t *initializer_from_expression(type_t *type,
940 expression_t *expression)
943 /* TODO check that expression is a constant expression */
945 /* § 6.7.8.14/15 char array may be initialized by string literals */
946 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
947 type_t *element_type = type->v.array_type.element_type;
949 if(element_type->type == TYPE_ATOMIC) {
950 atomic_type_type_t atype = element_type->v.atomic_type.atype;
952 /* TODO handle wide strings */
953 if(atype == ATOMIC_TYPE_CHAR
954 || atype == ATOMIC_TYPE_SCHAR
955 || atype == ATOMIC_TYPE_UCHAR) {
957 string_literal_t *literal = (string_literal_t*) expression;
958 return initializer_from_string(type, literal->value);
963 semantic_assign(type, &expression, "initializer");
965 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
966 result->type = INITIALIZER_VALUE;
967 result->v.value = expression;
972 static initializer_t *parse_sub_initializer(type_t *type,
973 expression_t *expression,
974 type_t *expression_type);
976 static initializer_t *parse_sub_initializer_elem(type_t *type)
978 if(token.type == '{') {
979 return parse_sub_initializer(type, NULL, NULL);
982 expression_t *expression = parse_assignment_expression();
983 type_t *expression_type = skip_typeref(expression->datatype);
985 return parse_sub_initializer(type, expression, expression_type);
988 static bool had_initializer_brace_warning;
990 static initializer_t *parse_sub_initializer(type_t *type,
991 expression_t *expression,
992 type_t *expression_type)
994 if(is_type_scalar(type)) {
995 /* there might be extra {} hierarchies */
996 if(token.type == '{') {
998 if(!had_initializer_brace_warning) {
999 parse_warning("braces around scalar initializer");
1000 had_initializer_brace_warning = true;
1002 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1003 if(token.type == ',') {
1005 /* TODO: warn about excessive elements */
1011 if(expression == NULL) {
1012 expression = parse_assignment_expression();
1014 return initializer_from_expression(type, expression);
1017 /* TODO: ignore qualifiers, comparing pointers is probably
1019 if(expression != NULL && expression_type == type) {
1020 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
1021 result->type = INITIALIZER_VALUE;
1024 semantic_assign(type, &expression, "initializer");
1026 result->v.value = expression;
1031 bool read_paren = false;
1032 if(token.type == '{') {
1037 /* descend into subtype */
1038 initializer_t *result = NULL;
1039 initializer_t **elems;
1040 if(type->type == TYPE_ARRAY) {
1041 type_t *element_type = type->v.array_type.element_type;
1042 element_type = skip_typeref(element_type);
1045 had_initializer_brace_warning = false;
1046 if(expression == NULL) {
1047 sub = parse_sub_initializer_elem(element_type);
1049 sub = parse_sub_initializer(element_type, expression,
1053 /* didn't match the subtypes -> try the parent type */
1055 assert(!read_paren);
1059 elems = NEW_ARR_F(initializer_t*, 0);
1060 ARR_APP1(initializer_t*, elems, sub);
1063 if(token.type == '}')
1066 if(token.type == '}')
1070 = parse_sub_initializer(element_type, NULL, NULL);
1072 /* TODO error, do nicer cleanup */
1073 parse_error("member initializer didn't match");
1077 ARR_APP1(initializer_t*, elems, sub);
1080 assert(type->type == TYPE_COMPOUND_STRUCT
1081 || type->type == TYPE_COMPOUND_UNION);
1082 context_t *context = &type->v.compound_type.declaration->context;
1084 declaration_t *first = context->declarations;
1087 type_t *first_type = first->type;
1088 first_type = skip_typeref(first_type);
1091 had_initializer_brace_warning = false;
1092 if(expression == NULL) {
1093 sub = parse_sub_initializer_elem(first_type);
1095 sub = parse_sub_initializer(first_type, expression,expression_type);
1098 /* didn't match the subtypes -> try our parent type */
1100 assert(!read_paren);
1104 elems = NEW_ARR_F(initializer_t*, 0);
1105 ARR_APP1(initializer_t*, elems, sub);
1107 declaration_t *iter = first->next;
1108 for( ; iter != NULL; iter = iter->next) {
1109 if(iter->symbol == NULL)
1111 if(iter->namespc != NAMESPACE_NORMAL)
1114 if(token.type == '}')
1118 type_t *iter_type = iter->type;
1119 iter_type = skip_typeref(iter_type);
1121 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1123 /* TODO error, do nicer cleanup*/
1124 parse_error("member initializer didn't match");
1128 ARR_APP1(initializer_t*, elems, sub);
1132 int len = ARR_LEN(elems);
1133 size_t elems_size = sizeof(initializer_t*) * len;
1135 initializer_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1137 init->type = INITIALIZER_LIST;
1138 init->v.list.len = len;
1139 memcpy(init->v.list.initializers, elems, elems_size);
1145 if(token.type == ',')
1152 static initializer_t *parse_initializer(type_t *type)
1154 initializer_t *result;
1156 type = skip_typeref(type);
1158 if(token.type != '{') {
1159 expression_t *expression = parse_assignment_expression();
1160 return initializer_from_expression(type, expression);
1163 if(is_type_scalar(type)) {
1167 expression_t *expression = parse_assignment_expression();
1168 result = initializer_from_expression(type, expression);
1170 if(token.type == ',')
1176 result = parse_sub_initializer(type, NULL, NULL);
1184 static declaration_t *parse_compound_type_specifier(bool is_struct)
1192 symbol_t *symbol = NULL;
1193 declaration_t *declaration = NULL;
1195 if (token.type == T___attribute__) {
1200 if(token.type == T_IDENTIFIER) {
1201 symbol = token.v.symbol;
1205 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1207 declaration = get_declaration(symbol, NAMESPACE_UNION);
1209 } else if(token.type != '{') {
1211 parse_error_expected("while parsing struct type specifier",
1212 T_IDENTIFIER, '{', 0);
1214 parse_error_expected("while parsing union type specifier",
1215 T_IDENTIFIER, '{', 0);
1221 if(declaration == NULL) {
1222 declaration = allocate_type_zero(sizeof(declaration[0]));
1225 declaration->namespc = NAMESPACE_STRUCT;
1227 declaration->namespc = NAMESPACE_UNION;
1229 declaration->source_position = token.source_position;
1230 declaration->symbol = symbol;
1231 record_declaration(declaration);
1234 if(token.type == '{') {
1235 if(declaration->init.is_defined) {
1236 assert(symbol != NULL);
1237 parser_print_error_prefix();
1238 fprintf(stderr, "multiple definition of %s %s\n",
1239 is_struct ? "struct" : "union", symbol->string);
1240 declaration->context.declarations = NULL;
1242 declaration->init.is_defined = true;
1244 int top = environment_top();
1245 context_t *last_context = context;
1246 set_context(& declaration->context);
1248 parse_compound_type_entries();
1251 assert(context == & declaration->context);
1252 set_context(last_context);
1253 environment_pop_to(top);
1259 static void parse_enum_entries(type_t *enum_type)
1263 if(token.type == '}') {
1265 parse_error("empty enum not allowed");
1270 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1272 if(token.type != T_IDENTIFIER) {
1273 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1277 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1278 entry->type = enum_type;
1279 entry->symbol = token.v.symbol;
1280 entry->source_position = token.source_position;
1283 if(token.type == '=') {
1285 entry->init.enum_value = parse_constant_expression();
1290 record_declaration(entry);
1292 if(token.type != ',')
1295 } while(token.type != '}');
1300 static declaration_t *parse_enum_specifier(void)
1304 declaration_t *declaration;
1307 if(token.type == T_IDENTIFIER) {
1308 symbol = token.v.symbol;
1311 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1312 } else if(token.type != '{') {
1313 parse_error_expected("while parsing enum type specifier",
1314 T_IDENTIFIER, '{', 0);
1321 if(declaration == NULL) {
1322 declaration = allocate_type_zero(sizeof(declaration[0]));
1324 declaration->namespc = NAMESPACE_ENUM;
1325 declaration->source_position = token.source_position;
1326 declaration->symbol = symbol;
1329 if(token.type == '{') {
1330 if(declaration->init.is_defined) {
1331 parser_print_error_prefix();
1332 fprintf(stderr, "multiple definitions of enum %s\n",
1335 record_declaration(declaration);
1336 declaration->init.is_defined = 1;
1338 parse_enum_entries(NULL);
1346 * if a symbol is a typedef to another type, return true
1348 static bool is_typedef_symbol(symbol_t *symbol)
1350 const declaration_t *const declaration =
1351 get_declaration(symbol, NAMESPACE_NORMAL);
1353 declaration != NULL &&
1354 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1357 static type_t *parse_typeof(void)
1365 expression_t *expression = NULL;
1368 switch(token.type) {
1369 case T___extension__:
1370 /* this can be a prefix to a typename or an expression */
1371 /* we simply eat it now. */
1374 } while(token.type == T___extension__);
1378 if(is_typedef_symbol(token.v.symbol)) {
1379 type = parse_typename();
1381 expression = parse_expression();
1382 type = expression->datatype;
1387 type = parse_typename();
1391 expression = parse_expression();
1392 type = expression->datatype;
1398 type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1399 typeof->type = TYPE_TYPEOF;
1400 typeof->v.typeof_type.expression = expression;
1401 typeof->v.typeof_type.typeof_type = type;
1407 SPECIFIER_SIGNED = 1 << 0,
1408 SPECIFIER_UNSIGNED = 1 << 1,
1409 SPECIFIER_LONG = 1 << 2,
1410 SPECIFIER_INT = 1 << 3,
1411 SPECIFIER_DOUBLE = 1 << 4,
1412 SPECIFIER_CHAR = 1 << 5,
1413 SPECIFIER_SHORT = 1 << 6,
1414 SPECIFIER_LONG_LONG = 1 << 7,
1415 SPECIFIER_FLOAT = 1 << 8,
1416 SPECIFIER_BOOL = 1 << 9,
1417 SPECIFIER_VOID = 1 << 10,
1418 #ifdef PROVIDE_COMPLEX
1419 SPECIFIER_COMPLEX = 1 << 11,
1420 SPECIFIER_IMAGINARY = 1 << 12,
1424 static type_t *create_builtin_type(symbol_t *symbol)
1426 type_t *type = allocate_type_zero(sizeof(type[0]));
1427 type->type = TYPE_BUILTIN;
1428 type->v.builtin_type.symbol = symbol;
1430 type->v.builtin_type.real_type = type_int;
1435 static type_t *get_typedef_type(symbol_t *symbol)
1437 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1438 if(declaration == NULL
1439 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1442 type_t *type = allocate_type_zero(sizeof(type[0]));
1443 type->type = TYPE_TYPEDEF;
1444 type->v.typedef_type.declaration = declaration;
1449 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1451 type_t *type = NULL;
1452 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1453 unsigned type_specifiers = 0;
1457 switch(token.type) {
1460 #define MATCH_STORAGE_CLASS(token, class) \
1462 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1463 parse_error("multiple storage classes in declaration " \
1466 specifiers->storage_class = class; \
1470 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1471 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1472 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1473 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1474 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1476 /* type qualifiers */
1477 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1479 type_qualifiers |= qualifier; \
1483 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1484 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1485 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1487 case T___extension__:
1492 /* type specifiers */
1493 #define MATCH_SPECIFIER(token, specifier, name) \
1496 if(type_specifiers & specifier) { \
1497 parse_error("multiple " name " type specifiers given"); \
1499 type_specifiers |= specifier; \
1503 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1504 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1505 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1506 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1507 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1508 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1509 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1510 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1511 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1512 #ifdef PROVIDE_COMPLEX
1513 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1514 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1518 specifiers->is_inline = true;
1523 if(type_specifiers & SPECIFIER_LONG_LONG) {
1524 parse_error("multiple type specifiers given");
1525 } else if(type_specifiers & SPECIFIER_LONG) {
1526 type_specifiers |= SPECIFIER_LONG_LONG;
1528 type_specifiers |= SPECIFIER_LONG;
1532 /* TODO: if type != NULL for the following rules should issue
1535 type = allocate_type_zero(sizeof(type[0]));
1536 type->type = TYPE_COMPOUND_STRUCT;
1537 type->v.compound_type.declaration = parse_compound_type_specifier(true);
1541 type = allocate_type_zero(sizeof(type[0]));
1542 type->type = TYPE_COMPOUND_UNION;
1543 type->v.compound_type.declaration = parse_compound_type_specifier(false);
1547 type = allocate_type_zero(sizeof(type[0]));
1548 type->type = TYPE_ENUM;
1549 type->v.enum_type.declaration = parse_enum_specifier();
1553 type = parse_typeof();
1555 case T___builtin_va_list:
1556 type = create_builtin_type(token.v.symbol);
1560 case T___attribute__:
1565 case T_IDENTIFIER: {
1566 type_t *typedef_type = get_typedef_type(token.v.symbol);
1568 if(typedef_type == NULL)
1569 goto finish_specifiers;
1572 type = typedef_type;
1576 /* function specifier */
1578 goto finish_specifiers;
1585 atomic_type_type_t atomic_type;
1587 /* match valid basic types */
1588 switch(type_specifiers) {
1589 case SPECIFIER_VOID:
1590 atomic_type = ATOMIC_TYPE_VOID;
1592 case SPECIFIER_CHAR:
1593 atomic_type = ATOMIC_TYPE_CHAR;
1595 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1596 atomic_type = ATOMIC_TYPE_SCHAR;
1598 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1599 atomic_type = ATOMIC_TYPE_UCHAR;
1601 case SPECIFIER_SHORT:
1602 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1603 case SPECIFIER_SHORT | SPECIFIER_INT:
1604 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1605 atomic_type = ATOMIC_TYPE_SHORT;
1607 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1608 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1609 atomic_type = ATOMIC_TYPE_USHORT;
1612 case SPECIFIER_SIGNED:
1613 case SPECIFIER_SIGNED | SPECIFIER_INT:
1614 atomic_type = ATOMIC_TYPE_INT;
1616 case SPECIFIER_UNSIGNED:
1617 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1618 atomic_type = ATOMIC_TYPE_UINT;
1620 case SPECIFIER_LONG:
1621 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1622 case SPECIFIER_LONG | SPECIFIER_INT:
1623 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1624 atomic_type = ATOMIC_TYPE_LONG;
1626 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1627 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1628 atomic_type = ATOMIC_TYPE_ULONG;
1630 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1631 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1632 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1633 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1635 atomic_type = ATOMIC_TYPE_LONGLONG;
1637 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1638 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1640 atomic_type = ATOMIC_TYPE_ULONGLONG;
1642 case SPECIFIER_FLOAT:
1643 atomic_type = ATOMIC_TYPE_FLOAT;
1645 case SPECIFIER_DOUBLE:
1646 atomic_type = ATOMIC_TYPE_DOUBLE;
1648 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1649 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1651 case SPECIFIER_BOOL:
1652 atomic_type = ATOMIC_TYPE_BOOL;
1654 #ifdef PROVIDE_COMPLEX
1655 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1656 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1658 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1659 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1661 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1662 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1664 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1665 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1667 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1668 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1670 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1671 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1675 /* invalid specifier combination, give an error message */
1676 if(type_specifiers == 0) {
1678 parse_warning("no type specifiers in declaration (using int)");
1679 atomic_type = ATOMIC_TYPE_INT;
1682 parse_error("no type specifiers given in declaration");
1684 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1685 (type_specifiers & SPECIFIER_UNSIGNED)) {
1686 parse_error("signed and unsigned specifiers gives");
1687 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1688 parse_error("only integer types can be signed or unsigned");
1690 parse_error("multiple datatypes in declaration");
1692 atomic_type = ATOMIC_TYPE_INVALID;
1695 type = allocate_type_zero(sizeof(type[0]));
1696 type->type = TYPE_ATOMIC;
1697 type->v.atomic_type.atype = atomic_type;
1700 if(type_specifiers != 0) {
1701 parse_error("multiple datatypes in declaration");
1705 type->qualifiers = type_qualifiers;
1707 type_t *result = typehash_insert(type);
1708 if(newtype && result != type) {
1712 specifiers->type = result;
1715 static type_qualifiers_t parse_type_qualifiers(void)
1717 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1720 switch(token.type) {
1721 /* type qualifiers */
1722 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1723 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1724 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1727 return type_qualifiers;
1732 static void parse_identifier_list(void)
1735 if(token.type != T_IDENTIFIER) {
1736 parse_error_expected("while parsing parameter identifier list",
1741 if(token.type != ',')
1747 static declaration_t *parse_parameter(void)
1749 declaration_specifiers_t specifiers;
1750 memset(&specifiers, 0, sizeof(specifiers));
1752 parse_declaration_specifiers(&specifiers);
1754 declaration_t *declaration
1755 = parse_declarator(&specifiers, specifiers.type, true);
1757 /* TODO check declaration constraints for parameters */
1758 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1759 parse_error("typedef not allowed in parameter list");
1762 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1764 if (declaration->type->type == TYPE_ARRAY) {
1765 const type_t *const arr_type = declaration->type;
1767 make_pointer_type(arr_type->v.array_type.element_type, TYPE_QUALIFIER_NONE);
1773 static declaration_t *parse_parameters(type_t *type)
1775 if(token.type == T_IDENTIFIER) {
1776 symbol_t *symbol = token.v.symbol;
1777 if(!is_typedef_symbol(symbol)) {
1778 /* TODO: K&R style C parameters */
1779 parse_identifier_list();
1784 if(token.type == ')') {
1785 type->v.function_type.unspecified_parameters = 1;
1788 if(token.type == T_void && look_ahead(1)->type == ')') {
1793 declaration_t *declarations = NULL;
1794 declaration_t *declaration;
1795 declaration_t *last_declaration = NULL;
1796 function_parameter_t *parameter;
1797 function_parameter_t *last_parameter = NULL;
1800 switch(token.type) {
1803 type->v.function_type.variadic = 1;
1804 return declarations;
1807 case T___extension__:
1809 declaration = parse_parameter();
1811 parameter = allocate_type_zero(sizeof(parameter[0]));
1812 parameter->type = declaration->type;
1814 if(last_parameter != NULL) {
1815 last_declaration->next = declaration;
1816 last_parameter->next = parameter;
1818 type->v.function_type.parameters = parameter;
1819 declarations = declaration;
1821 last_parameter = parameter;
1822 last_declaration = declaration;
1826 return declarations;
1828 if(token.type != ',')
1829 return declarations;
1839 } construct_type_type_t;
1841 typedef struct construct_type_t construct_type_t;
1842 struct construct_type_t {
1843 construct_type_type_t type;
1844 construct_type_t *next;
1847 typedef struct parsed_pointer_t parsed_pointer_t;
1848 struct parsed_pointer_t {
1849 construct_type_t construct_type;
1850 type_qualifiers_t type_qualifiers;
1853 typedef struct construct_function_type_t construct_function_type_t;
1854 struct construct_function_type_t {
1855 construct_type_t construct_type;
1856 type_t *function_type;
1859 typedef struct parsed_array_t parsed_array_t;
1860 struct parsed_array_t {
1861 construct_type_t construct_type;
1862 type_qualifiers_t type_qualifiers;
1868 typedef struct construct_base_type_t construct_base_type_t;
1869 struct construct_base_type_t {
1870 construct_type_t construct_type;
1874 static construct_type_t *parse_pointer_declarator(void)
1878 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1879 memset(pointer, 0, sizeof(pointer[0]));
1880 pointer->construct_type.type = CONSTRUCT_POINTER;
1881 pointer->type_qualifiers = parse_type_qualifiers();
1883 return (construct_type_t*) pointer;
1886 static construct_type_t *parse_array_declarator(void)
1890 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1891 memset(array, 0, sizeof(array[0]));
1892 array->construct_type.type = CONSTRUCT_ARRAY;
1894 if(token.type == T_static) {
1895 array->is_static = true;
1899 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1900 if(type_qualifiers != 0) {
1901 if(token.type == T_static) {
1902 array->is_static = true;
1906 array->type_qualifiers = type_qualifiers;
1908 if(token.type == '*' && look_ahead(1)->type == ']') {
1909 array->is_variable = true;
1911 } else if(token.type != ']') {
1912 array->size = parse_assignment_expression();
1917 return (construct_type_t*) array;
1920 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1924 type_t *type = allocate_type_zero(sizeof(type[0]));
1925 type->type = TYPE_FUNCTION;
1927 declaration_t *parameters = parse_parameters(type);
1928 if(declaration != NULL) {
1929 declaration->context.declarations = parameters;
1932 construct_function_type_t *construct_function_type =
1933 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1934 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1935 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1936 construct_function_type->function_type = type;
1940 return (construct_type_t*) construct_function_type;
1943 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1944 bool may_be_abstract)
1946 /* construct a single linked list of construct_type_t's which describe
1947 * how to construct the final declarator type */
1948 construct_type_t *first = NULL;
1949 construct_type_t *last = NULL;
1952 while(token.type == '*') {
1953 construct_type_t *type = parse_pointer_declarator();
1964 /* TODO: find out if this is correct */
1967 construct_type_t *inner_types = NULL;
1969 switch(token.type) {
1971 if(declaration == NULL) {
1972 parse_error("no identifier expected in typename");
1974 declaration->symbol = token.v.symbol;
1975 declaration->source_position = token.source_position;
1981 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1987 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1988 /* avoid a loop in the outermost scope, because eat_statement doesn't
1990 if(token.type == '}' && current_function == NULL) {
1998 construct_type_t *p = last;
2001 construct_type_t *type;
2002 switch(token.type) {
2004 type = parse_function_declarator(declaration);
2007 type = parse_array_declarator();
2010 goto declarator_finished;
2013 /* insert in the middle of the list (behind p) */
2015 type->next = p->next;
2026 declarator_finished:
2029 /* append inner_types at the end of the list, we don't to set last anymore
2030 * as it's not needed anymore */
2032 assert(first == NULL);
2033 first = inner_types;
2035 last->next = inner_types;
2041 static type_t *construct_declarator_type(construct_type_t *construct_list,
2044 construct_type_t *iter = construct_list;
2045 for( ; iter != NULL; iter = iter->next) {
2046 parsed_pointer_t *parsed_pointer;
2047 parsed_array_t *parsed_array;
2048 construct_function_type_t *construct_function_type;
2053 switch(iter->type) {
2054 case CONSTRUCT_INVALID:
2055 panic("invalid type construction found");
2056 case CONSTRUCT_FUNCTION:
2057 construct_function_type = (construct_function_type_t*) iter;
2058 ftype = construct_function_type->function_type;
2060 ftype->v.function_type.result_type = type;
2064 case CONSTRUCT_POINTER:
2065 parsed_pointer = (parsed_pointer_t*) iter;
2066 ptype = allocate_type_zero(sizeof(ptype[0]));
2068 ptype->type = TYPE_POINTER;
2069 ptype->v.pointer_type.points_to = type;
2070 ptype->qualifiers = parsed_pointer->type_qualifiers;
2074 case CONSTRUCT_ARRAY:
2075 parsed_array = (parsed_array_t*) iter;
2076 atype = allocate_type_zero(sizeof(atype[0]));
2078 atype->type = TYPE_ARRAY;
2079 atype->v.array_type.element_type = type;
2080 atype->qualifiers = parsed_array->type_qualifiers;
2081 atype->v.array_type.is_static = parsed_array->is_static;
2082 atype->v.array_type.is_variable = parsed_array->is_variable;
2083 atype->v.array_type.size = parsed_array->size;
2088 type_t *hashed_type = typehash_insert(type);
2089 if(hashed_type != type) {
2090 /* the function type was constructed earlier freeing it here will
2091 * destroy other types... */
2092 if(iter->type != CONSTRUCT_FUNCTION) {
2102 static declaration_t *parse_declarator(
2103 const declaration_specifiers_t *specifiers,
2104 type_t *type, bool may_be_abstract)
2106 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2107 declaration->storage_class = specifiers->storage_class;
2108 declaration->is_inline = specifiers->is_inline;
2110 construct_type_t *construct_type
2111 = parse_inner_declarator(declaration, may_be_abstract);
2112 declaration->type = construct_declarator_type(construct_type, type);
2114 if(construct_type != NULL) {
2115 obstack_free(&temp_obst, construct_type);
2121 static type_t *parse_abstract_declarator(type_t *base_type)
2123 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2125 type_t *result = construct_declarator_type(construct_type, base_type);
2126 if(construct_type != NULL) {
2127 obstack_free(&temp_obst, construct_type);
2133 static declaration_t *record_declaration(declaration_t *declaration)
2135 assert(context != NULL);
2137 symbol_t *symbol = declaration->symbol;
2138 if(symbol != NULL) {
2139 declaration_t *alias = environment_push(declaration);
2140 if(alias != declaration)
2143 declaration->parent_context = context;
2146 if(last_declaration != NULL) {
2147 last_declaration->next = declaration;
2149 context->declarations = declaration;
2151 last_declaration = declaration;
2156 static void parser_error_multiple_definition(declaration_t *previous,
2157 declaration_t *declaration)
2159 parser_print_error_prefix_pos(declaration->source_position);
2160 fprintf(stderr, "multiple definition of symbol '%s'\n",
2161 declaration->symbol->string);
2162 parser_print_error_prefix_pos(previous->source_position);
2163 fprintf(stderr, "this is the location of the previous definition.\n");
2166 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2169 declaration_t *ndeclaration
2170 = parse_declarator(specifiers, specifiers->type, false);
2172 declaration_t *declaration = record_declaration(ndeclaration);
2174 type_t *orig_type = declaration->type;
2175 type_t *type = skip_typeref(orig_type);
2176 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2177 parser_print_warning_prefix_pos(declaration->source_position);
2178 fprintf(stderr, "variable '%s' declared 'inline'\n",
2179 declaration->symbol->string);
2182 if(token.type == '=') {
2185 /* TODO: check that this is an allowed type (no function type) */
2187 if(declaration->init.initializer != NULL) {
2188 parser_error_multiple_definition(declaration, ndeclaration);
2191 initializer_t *initializer = parse_initializer(type);
2193 if(type->type == TYPE_ARRAY && initializer != NULL) {
2194 if(type->v.array_type.size == NULL) {
2195 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2197 cnst->expression.type = EXPR_CONST;
2198 cnst->expression.datatype = type_size_t;
2200 if(initializer->type == INITIALIZER_LIST) {
2201 cnst->v.int_value = initializer->v.list.len;
2203 assert(initializer->type == INITIALIZER_STRING);
2204 cnst->v.int_value = strlen(initializer->v.string) + 1;
2207 type->v.array_type.size = (expression_t*) cnst;
2211 ndeclaration->init.initializer = initializer;
2212 } else if(token.type == '{') {
2213 if(type->type != TYPE_FUNCTION) {
2214 parser_print_error_prefix();
2215 fprintf(stderr, "declarator '");
2216 print_type_ext(orig_type, declaration->symbol, NULL);
2217 fprintf(stderr, "' has a body but is not a function type.\n");
2222 if(declaration->init.statement != NULL) {
2223 parser_error_multiple_definition(declaration, ndeclaration);
2225 if(ndeclaration != declaration) {
2226 memcpy(&declaration->context, &ndeclaration->context,
2227 sizeof(declaration->context));
2230 int top = environment_top();
2231 context_t *last_context = context;
2232 set_context(&declaration->context);
2234 /* push function parameters */
2235 declaration_t *parameter = declaration->context.declarations;
2236 for( ; parameter != NULL; parameter = parameter->next) {
2237 environment_push(parameter);
2240 int label_stack_top = label_top();
2241 declaration_t *old_current_function = current_function;
2242 current_function = declaration;
2244 statement_t *statement = parse_compound_statement();
2246 assert(current_function == declaration);
2247 current_function = old_current_function;
2248 label_pop_to(label_stack_top);
2250 assert(context == &declaration->context);
2251 set_context(last_context);
2252 environment_pop_to(top);
2254 declaration->init.statement = statement;
2258 if(token.type != ',')
2265 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2268 if(token.type == ':') {
2270 parse_constant_expression();
2271 /* TODO (bitfields) */
2273 declaration_t *declaration
2274 = parse_declarator(specifiers, specifiers->type, true);
2276 /* TODO: check constraints for struct declarations */
2277 /* TODO: check for doubled fields */
2278 record_declaration(declaration);
2280 if(token.type == ':') {
2282 parse_constant_expression();
2283 /* TODO (bitfields) */
2287 if(token.type != ',')
2294 static void parse_compound_type_entries(void)
2298 while(token.type != '}' && token.type != T_EOF) {
2299 declaration_specifiers_t specifiers;
2300 memset(&specifiers, 0, sizeof(specifiers));
2301 parse_declaration_specifiers(&specifiers);
2303 parse_struct_declarators(&specifiers);
2305 if(token.type == T_EOF) {
2306 parse_error("unexpected error while parsing struct");
2311 static void parse_declaration(void)
2313 source_position_t source_position = token.source_position;
2315 declaration_specifiers_t specifiers;
2316 memset(&specifiers, 0, sizeof(specifiers));
2317 parse_declaration_specifiers(&specifiers);
2319 if(token.type == ';') {
2320 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2321 parse_warning_pos(source_position,
2322 "useless keyword in empty declaration");
2324 switch (specifiers.type->type) {
2325 case TYPE_COMPOUND_STRUCT:
2326 case TYPE_COMPOUND_UNION: {
2327 const type_t *const comp_type = specifiers.type;
2328 if (comp_type->v.compound_type.declaration->symbol == NULL) {
2329 parse_warning_pos(source_position,
2330 "unnamed struct/union that defines no instances");
2335 case TYPE_ENUM: break;
2338 parse_warning_pos(source_position, "empty declaration");
2344 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2346 declaration->type = specifiers.type;
2347 declaration->storage_class = specifiers.storage_class;
2348 declaration->source_position = source_position;
2349 record_declaration(declaration);
2352 parse_init_declarators(&specifiers);
2355 static type_t *parse_typename(void)
2357 declaration_specifiers_t specifiers;
2358 memset(&specifiers, 0, sizeof(specifiers));
2359 parse_declaration_specifiers(&specifiers);
2360 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2361 /* TODO: improve error message, user does probably not know what a
2362 * storage class is...
2364 parse_error("typename may not have a storage class");
2367 type_t *result = parse_abstract_declarator(specifiers.type);
2375 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2376 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2377 expression_t *left);
2379 typedef struct expression_parser_function_t expression_parser_function_t;
2380 struct expression_parser_function_t {
2381 unsigned precedence;
2382 parse_expression_function parser;
2383 unsigned infix_precedence;
2384 parse_expression_infix_function infix_parser;
2387 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2389 static expression_t *make_invalid_expression(void)
2391 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2392 expression->type = EXPR_INVALID;
2393 expression->source_position = token.source_position;
2397 static expression_t *expected_expression_error(void)
2399 parser_print_error_prefix();
2400 fprintf(stderr, "expected expression, got token ");
2401 print_token(stderr, & token);
2402 fprintf(stderr, "\n");
2406 return make_invalid_expression();
2409 static expression_t *parse_string_const(void)
2411 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2413 cnst->expression.type = EXPR_STRING_LITERAL;
2414 cnst->expression.datatype = type_string;
2415 cnst->value = parse_string_literals();
2417 return (expression_t*) cnst;
2420 static expression_t *parse_int_const(void)
2422 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2424 cnst->expression.type = EXPR_CONST;
2425 cnst->expression.datatype = token.datatype;
2426 cnst->v.int_value = token.v.intvalue;
2430 return (expression_t*) cnst;
2433 static expression_t *parse_float_const(void)
2435 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2437 cnst->expression.type = EXPR_CONST;
2438 cnst->expression.datatype = token.datatype;
2439 cnst->v.float_value = token.v.floatvalue;
2443 return (expression_t*) cnst;
2446 static declaration_t *create_implicit_function(symbol_t *symbol,
2447 const source_position_t source_position)
2449 type_t *ftype = allocate_type_zero(sizeof(ftype[0]));
2451 ftype->type = TYPE_FUNCTION;
2452 ftype->v.function_type.result_type = type_int;
2453 ftype->v.function_type.unspecified_parameters = true;
2455 type_t *type = typehash_insert(ftype);
2460 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2462 declaration->storage_class = STORAGE_CLASS_EXTERN;
2463 declaration->type = type;
2464 declaration->symbol = symbol;
2465 declaration->source_position = source_position;
2467 /* prepend the implicit definition to the global context
2468 * this is safe since the symbol wasn't declared as anything else yet
2470 assert(symbol->declaration == NULL);
2472 context_t *last_context = context;
2473 context = global_context;
2475 environment_push(declaration);
2476 declaration->next = context->declarations;
2477 context->declarations = declaration;
2479 context = last_context;
2484 static expression_t *parse_reference(void)
2486 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2488 ref->expression.type = EXPR_REFERENCE;
2489 ref->symbol = token.v.symbol;
2491 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2493 source_position_t source_position = token.source_position;
2496 if(declaration == NULL) {
2498 /* an implicitly defined function */
2499 if(token.type == '(') {
2500 parser_print_prefix_pos(token.source_position);
2501 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2502 ref->symbol->string);
2504 declaration = create_implicit_function(ref->symbol,
2509 parser_print_error_prefix();
2510 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2511 return (expression_t*) ref;
2515 ref->declaration = declaration;
2516 ref->expression.datatype = declaration->type;
2518 return (expression_t*) ref;
2521 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2525 /* TODO check if explicit cast is allowed and issue warnings/errors */
2528 static expression_t *parse_cast(void)
2530 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2532 cast->expression.type = EXPR_UNARY;
2533 cast->type = UNEXPR_CAST;
2534 cast->expression.source_position = token.source_position;
2536 type_t *type = parse_typename();
2539 expression_t *value = parse_sub_expression(20);
2541 check_cast_allowed(value, type);
2543 cast->expression.datatype = type;
2544 cast->value = value;
2546 return (expression_t*) cast;
2549 static expression_t *parse_statement_expression(void)
2551 statement_expression_t *expression
2552 = allocate_ast_zero(sizeof(expression[0]));
2553 expression->expression.type = EXPR_STATEMENT;
2555 statement_t *statement = parse_compound_statement();
2556 expression->statement = statement;
2557 if(statement == NULL) {
2562 assert(statement->type == STATEMENT_COMPOUND);
2564 /* find last statement and use it's type */
2565 const statement_t *last_statement = NULL;
2566 const statement_t *iter = statement->v.compound_stmt.statements;
2567 for( ; iter != NULL; iter = iter->next) {
2568 last_statement = iter;
2571 if(last_statement->type == STATEMENT_EXPRESSION) {
2572 expression->expression.datatype
2573 = last_statement->v.expression->datatype;
2575 expression->expression.datatype = type_void;
2580 return (expression_t*) expression;
2583 static expression_t *parse_brace_expression(void)
2587 switch(token.type) {
2589 /* gcc extension: a stement expression */
2590 return parse_statement_expression();
2594 return parse_cast();
2596 if(is_typedef_symbol(token.v.symbol)) {
2597 return parse_cast();
2601 expression_t *result = parse_expression();
2607 static expression_t *parse_function_keyword(void)
2612 if (current_function == NULL) {
2613 parse_error("'__func__' used outside of a function");
2616 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2617 expression->expression.type = EXPR_FUNCTION;
2618 expression->expression.datatype = type_string;
2619 expression->value = "TODO: FUNCTION";
2621 return (expression_t*) expression;
2624 static expression_t *parse_pretty_function_keyword(void)
2626 eat(T___PRETTY_FUNCTION__);
2629 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2630 expression->expression.type = EXPR_PRETTY_FUNCTION;
2631 expression->expression.datatype = type_string;
2632 expression->value = "TODO: PRETTY FUNCTION";
2634 return (expression_t*) expression;
2637 static designator_t *parse_designator(void)
2639 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2641 if(token.type != T_IDENTIFIER) {
2642 parse_error_expected("while parsing member designator",
2647 result->symbol = token.v.symbol;
2650 designator_t *last_designator = result;
2652 if(token.type == '.') {
2654 if(token.type != T_IDENTIFIER) {
2655 parse_error_expected("while parsing member designator",
2660 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2661 designator->symbol = token.v.symbol;
2664 last_designator->next = designator;
2665 last_designator = designator;
2668 if(token.type == '[') {
2670 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2671 designator->array_access = parse_expression();
2672 if(designator->array_access == NULL) {
2678 last_designator->next = designator;
2679 last_designator = designator;
2688 static expression_t *parse_offsetof(void)
2690 eat(T___builtin_offsetof);
2692 offsetof_expression_t *expression
2693 = allocate_ast_zero(sizeof(expression[0]));
2694 expression->expression.type = EXPR_OFFSETOF;
2695 expression->expression.datatype = type_size_t;
2698 expression->type = parse_typename();
2700 expression->designator = parse_designator();
2703 return (expression_t*) expression;
2706 static expression_t *parse_va_arg(void)
2708 eat(T___builtin_va_arg);
2710 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2711 expression->expression.type = EXPR_VA_ARG;
2714 expression->arg = parse_assignment_expression();
2716 expression->expression.datatype = parse_typename();
2719 return (expression_t*) expression;
2722 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2724 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2725 parameter->type = argument_type;
2727 type_t *type = allocate_type_zero(sizeof(type[0]));
2728 type->type = TYPE_FUNCTION;
2729 type->v.function_type.result_type = result_type;
2730 type->v.function_type.parameters = parameter;
2732 type_t *result = typehash_insert(type);
2733 if(result != type) {
2740 static expression_t *parse_builtin_symbol(void)
2742 builtin_symbol_expression_t *expression
2743 = allocate_ast_zero(sizeof(expression[0]));
2744 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2746 expression->symbol = token.v.symbol;
2749 switch(token.type) {
2750 case T___builtin_alloca:
2751 type = make_function_1_type(type_void_ptr, type_size_t);
2757 expression->expression.datatype = type;
2758 return (expression_t*) expression;
2761 static expression_t *parse_primary_expression(void)
2763 switch(token.type) {
2765 return parse_int_const();
2766 case T_FLOATINGPOINT:
2767 return parse_float_const();
2768 case T_STRING_LITERAL:
2769 return parse_string_const();
2771 return parse_reference();
2772 case T___FUNCTION__:
2774 return parse_function_keyword();
2775 case T___PRETTY_FUNCTION__:
2776 return parse_pretty_function_keyword();
2777 case T___builtin_offsetof:
2778 return parse_offsetof();
2779 case T___builtin_va_arg:
2780 return parse_va_arg();
2781 case T___builtin_alloca:
2782 case T___builtin_expect:
2783 case T___builtin_va_start:
2784 case T___builtin_va_end:
2785 return parse_builtin_symbol();
2788 return parse_brace_expression();
2791 parser_print_error_prefix();
2792 fprintf(stderr, "unexpected token ");
2793 print_token(stderr, &token);
2794 fprintf(stderr, "\n");
2797 return make_invalid_expression();
2800 static expression_t *parse_array_expression(unsigned precedence,
2801 expression_t *array_ref)
2807 expression_t *index = parse_expression();
2809 array_access_expression_t *array_access
2810 = allocate_ast_zero(sizeof(array_access[0]));
2812 array_access->expression.type = EXPR_ARRAY_ACCESS;
2813 array_access->array_ref = array_ref;
2814 array_access->index = index;
2816 type_t *type_left = skip_typeref(array_ref->datatype);
2817 type_t *type_right = skip_typeref(index->datatype);
2819 if(type_left != NULL && type_right != NULL) {
2820 if(type_left->type == TYPE_POINTER) {
2821 type_t *pointer = type_left;
2822 array_access->expression.datatype = pointer->v.pointer_type.points_to;
2823 } else if(type_left->type == TYPE_ARRAY) {
2824 type_t *array_type = type_left;
2825 array_access->expression.datatype = array_type->v.array_type.element_type;
2826 } else if(type_right->type == TYPE_POINTER) {
2827 type_t *pointer = type_right;
2828 array_access->expression.datatype = pointer->v.pointer_type.points_to;
2829 } else if(type_right->type == TYPE_ARRAY) {
2830 type_t *array_type = type_right;
2831 array_access->expression.datatype = array_type->v.array_type.element_type;
2833 parser_print_error_prefix();
2834 fprintf(stderr, "array access on object with non-pointer types ");
2835 print_type_quoted(type_left);
2836 fprintf(stderr, ", ");
2837 print_type_quoted(type_right);
2838 fprintf(stderr, "\n");
2842 if(token.type != ']') {
2843 parse_error_expected("Problem while parsing array access", ']', 0);
2844 return (expression_t*) array_access;
2848 return (expression_t*) array_access;
2851 static bool is_declaration_specifier(const token_t *token,
2852 bool only_type_specifiers)
2854 switch(token->type) {
2858 return is_typedef_symbol(token->v.symbol);
2861 if(only_type_specifiers)
2870 static expression_t *parse_sizeof(unsigned precedence)
2874 sizeof_expression_t *sizeof_expression
2875 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2876 sizeof_expression->expression.type = EXPR_SIZEOF;
2877 sizeof_expression->expression.datatype = type_size_t;
2879 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2881 sizeof_expression->type = parse_typename();
2884 expression_t *expression = parse_sub_expression(precedence);
2885 sizeof_expression->type = expression->datatype;
2886 sizeof_expression->size_expression = expression;
2889 return (expression_t*) sizeof_expression;
2892 static expression_t *parse_select_expression(unsigned precedence,
2893 expression_t *compound)
2896 assert(token.type == '.' || token.type == T_MINUSGREATER);
2898 bool is_pointer = (token.type == T_MINUSGREATER);
2901 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2903 select->expression.type = EXPR_SELECT;
2904 select->compound = compound;
2906 if(token.type != T_IDENTIFIER) {
2907 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2908 return (expression_t*) select;
2910 symbol_t *symbol = token.v.symbol;
2911 select->symbol = symbol;
2914 type_t *orig_type = compound->datatype;
2915 if(orig_type == NULL)
2916 return make_invalid_expression();
2918 type_t *type = skip_typeref(orig_type);
2920 type_t *type_left = type;
2922 if(type->type != TYPE_POINTER) {
2923 parser_print_error_prefix();
2924 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2925 print_type_quoted(orig_type);
2926 fputc('\n', stderr);
2927 return make_invalid_expression();
2929 type_left = type->v.pointer_type.points_to;
2931 type_left = skip_typeref(type_left);
2933 if(type_left->type != TYPE_COMPOUND_STRUCT
2934 && type_left->type != TYPE_COMPOUND_UNION) {
2935 parser_print_error_prefix();
2936 fprintf(stderr, "request for member '%s' in something not a struct or "
2937 "union, but ", symbol->string);
2938 print_type_quoted(type_left);
2939 fputc('\n', stderr);
2940 return make_invalid_expression();
2943 type_t *compound_type = type_left;
2944 declaration_t *declaration = compound_type->v.compound_type.declaration;
2946 if(!declaration->init.is_defined) {
2947 parser_print_error_prefix();
2948 fprintf(stderr, "request for member '%s' of incomplete type ",
2950 print_type_quoted(type_left);
2951 fputc('\n', stderr);
2952 return make_invalid_expression();
2955 declaration_t *iter = declaration->context.declarations;
2956 for( ; iter != NULL; iter = iter->next) {
2957 if(iter->symbol == symbol) {
2962 parser_print_error_prefix();
2963 print_type_quoted(type_left);
2964 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2965 return make_invalid_expression();
2968 select->compound_entry = iter;
2969 select->expression.datatype = iter->type;
2970 return (expression_t*) select;
2973 static expression_t *parse_call_expression(unsigned precedence,
2974 expression_t *expression)
2977 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2978 call->expression.type = EXPR_CALL;
2979 call->function = expression;
2981 type_t *function_type;
2982 type_t *orig_type = expression->datatype;
2983 type_t *type = skip_typeref(orig_type);
2985 if(type->type == TYPE_POINTER) {
2986 type = skip_typeref(type->v.pointer_type.points_to);
2988 if (type->type == TYPE_FUNCTION) {
2989 function_type = type;
2990 call->expression.datatype = type->v.function_type.result_type;
2992 parser_print_error_prefix();
2993 fputs("called object '", stderr);
2994 print_expression(expression);
2995 fputs("' (type ", stderr);
2996 print_type_quoted(orig_type);
2997 fputs(") is not a function\n", stderr);
2999 function_type = NULL;
3000 call->expression.datatype = NULL;
3003 /* parse arguments */
3006 if(token.type != ')') {
3007 call_argument_t *last_argument = NULL;
3010 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3012 argument->expression = parse_assignment_expression();
3013 if(last_argument == NULL) {
3014 call->arguments = argument;
3016 last_argument->next = argument;
3018 last_argument = argument;
3020 if(token.type != ',')
3027 if(function_type != NULL) {
3028 function_parameter_t *parameter = function_type->v.function_type.parameters;
3029 call_argument_t *argument = call->arguments;
3030 for( ; parameter != NULL && argument != NULL;
3031 parameter = parameter->next, argument = argument->next) {
3032 type_t *expected_type = parameter->type;
3033 /* TODO report context in error messages */
3034 argument->expression = create_implicit_cast(argument->expression,
3037 /* too few parameters */
3038 if(parameter != NULL) {
3039 parser_print_error_prefix();
3040 fprintf(stderr, "too few arguments to function '");
3041 print_expression(expression);
3042 fprintf(stderr, "'\n");
3043 } else if(argument != NULL) {
3044 /* too many parameters */
3045 if(!function_type->v.function_type.variadic
3046 && !function_type->v.function_type.unspecified_parameters) {
3047 parser_print_error_prefix();
3048 fprintf(stderr, "too many arguments to function '");
3049 print_expression(expression);
3050 fprintf(stderr, "'\n");
3052 /* do default promotion */
3053 for( ; argument != NULL; argument = argument->next) {
3054 type_t *type = argument->expression->datatype;
3059 if(is_type_integer(type)) {
3060 type = promote_integer(type);
3061 } else if(type == type_float) {
3064 argument->expression
3065 = create_implicit_cast(argument->expression, type);
3071 return (expression_t*) call;
3074 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3076 static expression_t *parse_conditional_expression(unsigned precedence,
3077 expression_t *expression)
3081 conditional_expression_t *conditional
3082 = allocate_ast_zero(sizeof(conditional[0]));
3083 conditional->expression.type = EXPR_CONDITIONAL;
3084 conditional->condition = expression;
3087 type_t *condition_type_orig = conditional->condition->datatype;
3088 if(condition_type_orig != NULL) {
3089 type_t *condition_type = skip_typeref(condition_type_orig);
3090 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3091 type_error("expected a scalar type", expression->source_position,
3092 condition_type_orig);
3096 expression_t *const t_expr = parse_expression();
3097 conditional->true_expression = t_expr;
3099 expression_t *const f_expr = parse_sub_expression(precedence);
3100 conditional->false_expression = f_expr;
3102 type_t *const true_type = t_expr->datatype;
3103 if(true_type == NULL)
3104 return (expression_t*) conditional;
3105 type_t *const false_type = f_expr->datatype;
3106 if(false_type == NULL)
3107 return (expression_t*) conditional;
3109 type_t *const skipped_true_type = skip_typeref(true_type);
3110 type_t *const skipped_false_type = skip_typeref(false_type);
3113 if (skipped_true_type == skipped_false_type) {
3114 conditional->expression.datatype = skipped_true_type;
3115 } else if (is_type_arithmetic(skipped_true_type) &&
3116 is_type_arithmetic(skipped_false_type)) {
3117 type_t *const result = semantic_arithmetic(skipped_true_type,
3118 skipped_false_type);
3119 conditional->true_expression = create_implicit_cast(t_expr, result);
3120 conditional->false_expression = create_implicit_cast(f_expr, result);
3121 conditional->expression.datatype = result;
3122 } else if (skipped_true_type->type == TYPE_POINTER &&
3123 skipped_false_type->type == TYPE_POINTER &&
3124 true /* TODO compatible points_to types */) {
3126 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3127 skipped_false_type->type == TYPE_POINTER)
3128 || (is_null_ptr_const(skipped_false_type) &&
3129 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3131 } else if(/* 1 is pointer to object type, other is void* */ false) {
3134 type_error_incompatible("while parsing conditional",
3135 expression->source_position, true_type,
3136 skipped_false_type);
3139 return (expression_t*) conditional;
3142 static expression_t *parse_extension(unsigned precedence)
3144 eat(T___extension__);
3146 /* TODO enable extensions */
3148 return parse_sub_expression(precedence);
3151 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3153 eat(T___builtin_classify_type);
3155 classify_type_expression_t *const classify_type_expr =
3156 allocate_ast_zero(sizeof(classify_type_expr[0]));
3157 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3158 classify_type_expr->expression.datatype = type_int;
3161 expression_t *const expression = parse_sub_expression(precedence);
3163 classify_type_expr->type_expression = expression;
3165 return (expression_t*)classify_type_expr;
3168 static void semantic_incdec(unary_expression_t *expression)
3170 type_t *orig_type = expression->value->datatype;
3171 if(orig_type == NULL)
3174 type_t *type = skip_typeref(orig_type);
3175 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3176 /* TODO: improve error message */
3177 parser_print_error_prefix();
3178 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3182 expression->expression.datatype = orig_type;
3185 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3187 type_t *orig_type = expression->value->datatype;
3188 if(orig_type == NULL)
3191 type_t *type = skip_typeref(orig_type);
3192 if(!is_type_arithmetic(type)) {
3193 /* TODO: improve error message */
3194 parser_print_error_prefix();
3195 fprintf(stderr, "operation needs an arithmetic type\n");
3199 expression->expression.datatype = orig_type;
3202 static void semantic_unexpr_scalar(unary_expression_t *expression)
3204 type_t *orig_type = expression->value->datatype;
3205 if(orig_type == NULL)
3208 type_t *type = skip_typeref(orig_type);
3209 if (!is_type_scalar(type)) {
3210 parse_error("operand of ! must be of scalar type\n");
3214 expression->expression.datatype = orig_type;
3217 static void semantic_unexpr_integer(unary_expression_t *expression)
3219 type_t *orig_type = expression->value->datatype;
3220 if(orig_type == NULL)
3223 type_t *type = skip_typeref(orig_type);
3224 if (!is_type_integer(type)) {
3225 parse_error("operand of ~ must be of integer type\n");
3229 expression->expression.datatype = orig_type;
3232 static void semantic_dereference(unary_expression_t *expression)
3234 type_t *orig_type = expression->value->datatype;
3235 if(orig_type == NULL)
3238 type_t *type = skip_typeref(orig_type);
3239 switch (type->type) {
3241 expression->expression.datatype = type->v.array_type.element_type;
3245 expression->expression.datatype = type->v.pointer_type.points_to;
3249 parser_print_error_prefix();
3250 fputs("'Unary *' needs pointer or array type, but type ", stderr);
3251 print_type_quoted(orig_type);
3252 fputs(" given.\n", stderr);
3257 static void semantic_take_addr(unary_expression_t *expression)
3259 type_t *orig_type = expression->value->datatype;
3260 if(orig_type == NULL)
3263 expression_t *value = expression->value;
3264 if(value->type == EXPR_REFERENCE) {
3265 reference_expression_t *reference = (reference_expression_t*) value;
3266 declaration_t *declaration = reference->declaration;
3267 if(declaration != NULL) {
3268 declaration->address_taken = 1;
3272 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3275 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3276 static expression_t *parse_##unexpression_type(unsigned precedence) \
3280 unary_expression_t *unary_expression \
3281 = allocate_ast_zero(sizeof(unary_expression[0])); \
3282 unary_expression->expression.type = EXPR_UNARY; \
3283 unary_expression->type = unexpression_type; \
3284 unary_expression->value = parse_sub_expression(precedence); \
3286 sfunc(unary_expression); \
3288 return (expression_t*) unary_expression; \
3291 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3292 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3293 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3294 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3295 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3296 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3297 semantic_unexpr_integer)
3298 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3300 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3303 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3305 static expression_t *parse_##unexpression_type(unsigned precedence, \
3306 expression_t *left) \
3308 (void) precedence; \
3311 unary_expression_t *unary_expression \
3312 = allocate_ast_zero(sizeof(unary_expression[0])); \
3313 unary_expression->expression.type = EXPR_UNARY; \
3314 unary_expression->type = unexpression_type; \
3315 unary_expression->value = left; \
3317 sfunc(unary_expression); \
3319 return (expression_t*) unary_expression; \
3322 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3324 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3327 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3329 /* TODO: handle complex + imaginary types */
3331 /* § 6.3.1.8 Usual arithmetic conversions */
3332 if(type_left == type_long_double || type_right == type_long_double) {
3333 return type_long_double;
3334 } else if(type_left == type_double || type_right == type_double) {
3336 } else if(type_left == type_float || type_right == type_float) {
3340 type_right = promote_integer(type_right);
3341 type_left = promote_integer(type_left);
3343 if(type_left == type_right)
3346 bool signed_left = is_type_signed(type_left);
3347 bool signed_right = is_type_signed(type_right);
3348 if(get_rank(type_left) < get_rank(type_right)) {
3349 if(signed_left == signed_right || !signed_right) {
3355 if(signed_left == signed_right || !signed_left) {
3363 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3365 expression_t *left = expression->left;
3366 expression_t *right = expression->right;
3367 type_t *orig_type_left = left->datatype;
3368 type_t *orig_type_right = right->datatype;
3370 if(orig_type_left == NULL || orig_type_right == NULL)
3373 type_t *type_left = skip_typeref(orig_type_left);
3374 type_t *type_right = skip_typeref(orig_type_right);
3376 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3377 /* TODO: improve error message */
3378 parser_print_error_prefix();
3379 fprintf(stderr, "operation needs arithmetic types\n");
3383 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3384 expression->left = create_implicit_cast(left, arithmetic_type);
3385 expression->right = create_implicit_cast(right, arithmetic_type);
3386 expression->expression.datatype = arithmetic_type;
3389 static void semantic_shift_op(binary_expression_t *expression)
3391 expression_t *left = expression->left;
3392 expression_t *right = expression->right;
3393 type_t *orig_type_left = left->datatype;
3394 type_t *orig_type_right = right->datatype;
3396 if(orig_type_left == NULL || orig_type_right == NULL)
3399 type_t *type_left = skip_typeref(orig_type_left);
3400 type_t *type_right = skip_typeref(orig_type_right);
3402 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3403 /* TODO: improve error message */
3404 parser_print_error_prefix();
3405 fprintf(stderr, "operation needs integer types\n");
3409 type_left = promote_integer(type_left);
3410 type_right = promote_integer(type_right);
3412 expression->left = create_implicit_cast(left, type_left);
3413 expression->right = create_implicit_cast(right, type_right);
3414 expression->expression.datatype = type_left;
3417 static void semantic_add(binary_expression_t *expression)
3419 expression_t *left = expression->left;
3420 expression_t *right = expression->right;
3421 type_t *orig_type_left = left->datatype;
3422 type_t *orig_type_right = right->datatype;
3424 if(orig_type_left == NULL || orig_type_right == NULL)
3427 type_t *type_left = skip_typeref(orig_type_left);
3428 type_t *type_right = skip_typeref(orig_type_right);
3431 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3432 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3433 expression->left = create_implicit_cast(left, arithmetic_type);
3434 expression->right = create_implicit_cast(right, arithmetic_type);
3435 expression->expression.datatype = arithmetic_type;
3437 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3438 expression->expression.datatype = type_left;
3439 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3440 expression->expression.datatype = type_right;
3441 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3442 const type_t *const arr_type = type_left;
3443 expression->expression.datatype =
3444 make_pointer_type(arr_type->v.array_type.element_type, TYPE_QUALIFIER_NONE);
3445 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3446 const type_t *const arr_type = type_right;
3447 expression->expression.datatype =
3448 make_pointer_type(arr_type->v.array_type.element_type, TYPE_QUALIFIER_NONE);
3450 parser_print_error_prefix();
3451 fprintf(stderr, "invalid operands to binary + (");
3452 print_type_quoted(orig_type_left);
3453 fprintf(stderr, ", ");
3454 print_type_quoted(orig_type_right);
3455 fprintf(stderr, ")\n");
3459 static void semantic_sub(binary_expression_t *expression)
3461 expression_t *left = expression->left;
3462 expression_t *right = expression->right;
3463 type_t *orig_type_left = left->datatype;
3464 type_t *orig_type_right = right->datatype;
3466 if(orig_type_left == NULL || orig_type_right == NULL)
3469 type_t *type_left = skip_typeref(orig_type_left);
3470 type_t *type_right = skip_typeref(orig_type_right);
3473 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3474 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3475 expression->left = create_implicit_cast(left, arithmetic_type);
3476 expression->right = create_implicit_cast(right, arithmetic_type);
3477 expression->expression.datatype = arithmetic_type;
3479 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3480 expression->expression.datatype = type_left;
3481 } else if(type_left->type == TYPE_POINTER &&
3482 type_right->type == TYPE_POINTER) {
3483 if(!pointers_compatible(type_left, type_right)) {
3484 parser_print_error_prefix();
3485 fprintf(stderr, "pointers to incompatible objects to binary - (");
3486 print_type_quoted(orig_type_left);
3487 fprintf(stderr, ", ");
3488 print_type_quoted(orig_type_right);
3489 fprintf(stderr, ")\n");
3491 expression->expression.datatype = type_ptrdiff_t;
3494 parser_print_error_prefix();
3495 fprintf(stderr, "invalid operands to binary - (");
3496 print_type_quoted(orig_type_left);
3497 fprintf(stderr, ", ");
3498 print_type_quoted(orig_type_right);
3499 fprintf(stderr, ")\n");
3503 static void semantic_comparison(binary_expression_t *expression)
3505 expression_t *left = expression->left;
3506 expression_t *right = expression->right;
3507 type_t *orig_type_left = left->datatype;
3508 type_t *orig_type_right = right->datatype;
3510 if(orig_type_left == NULL || orig_type_right == NULL)
3513 type_t *type_left = skip_typeref(orig_type_left);
3514 type_t *type_right = skip_typeref(orig_type_right);
3516 /* TODO non-arithmetic types */
3517 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3518 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3519 expression->left = create_implicit_cast(left, arithmetic_type);
3520 expression->right = create_implicit_cast(right, arithmetic_type);
3521 expression->expression.datatype = arithmetic_type;
3522 } else if (type_left->type == TYPE_POINTER &&
3523 type_right->type == TYPE_POINTER) {
3524 /* TODO check compatibility */
3525 } else if (type_left->type == TYPE_POINTER) {
3526 expression->right = create_implicit_cast(right, type_left);
3527 } else if (type_right->type == TYPE_POINTER) {
3528 expression->left = create_implicit_cast(left, type_right);
3530 type_error_incompatible("invalid operands in comparison",
3531 token.source_position, type_left, type_right);
3533 expression->expression.datatype = type_int;
3536 static void semantic_arithmetic_assign(binary_expression_t *expression)
3538 expression_t *left = expression->left;
3539 expression_t *right = expression->right;
3540 type_t *orig_type_left = left->datatype;
3541 type_t *orig_type_right = right->datatype;
3543 if(orig_type_left == NULL || orig_type_right == NULL)
3546 type_t *type_left = skip_typeref(orig_type_left);
3547 type_t *type_right = skip_typeref(orig_type_right);
3549 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3550 /* TODO: improve error message */
3551 parser_print_error_prefix();
3552 fprintf(stderr, "operation needs arithmetic types\n");
3556 /* combined instructions are tricky. We can't create an implicit cast on
3557 * the left side, because we need the uncasted form for the store.
3558 * The ast2firm pass has to know that left_type must be right_type
3559 * for the arithmeitc operation and create a cast by itself */
3560 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3561 expression->right = create_implicit_cast(right, arithmetic_type);
3562 expression->expression.datatype = type_left;
3565 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3567 expression_t *left = expression->left;
3568 expression_t *right = expression->right;
3569 type_t *orig_type_left = left->datatype;
3570 type_t *orig_type_right = right->datatype;
3572 if(orig_type_left == NULL || orig_type_right == NULL)
3575 type_t *type_left = skip_typeref(orig_type_left);
3576 type_t *type_right = skip_typeref(orig_type_right);
3578 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3579 /* combined instructions are tricky. We can't create an implicit cast on
3580 * the left side, because we need the uncasted form for the store.
3581 * The ast2firm pass has to know that left_type must be right_type
3582 * for the arithmeitc operation and create a cast by itself */
3583 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3584 expression->right = create_implicit_cast(right, arithmetic_type);
3585 expression->expression.datatype = type_left;
3586 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3587 expression->expression.datatype = type_left;
3589 parser_print_error_prefix();
3590 fputs("Incompatible types ", stderr);
3591 print_type_quoted(orig_type_left);
3592 fputs(" and ", stderr);
3593 print_type_quoted(orig_type_right);
3594 fputs(" in assignment\n", stderr);
3599 static void semantic_logical_op(binary_expression_t *expression)
3601 expression_t *left = expression->left;
3602 expression_t *right = expression->right;
3603 type_t *orig_type_left = left->datatype;
3604 type_t *orig_type_right = right->datatype;
3606 if(orig_type_left == NULL || orig_type_right == NULL)
3609 type_t *type_left = skip_typeref(orig_type_left);
3610 type_t *type_right = skip_typeref(orig_type_right);
3612 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3613 /* TODO: improve error message */
3614 parser_print_error_prefix();
3615 fprintf(stderr, "operation needs scalar types\n");
3619 expression->expression.datatype = type_int;
3622 static void semantic_binexpr_assign(binary_expression_t *expression)
3624 expression_t *left = expression->left;
3625 type_t *orig_type_left = left->datatype;
3627 if(orig_type_left == NULL)
3630 type_t *type_left = skip_typeref(orig_type_left);
3632 if (type_left->type == TYPE_ARRAY) {
3633 parse_error("Cannot assign to arrays.");
3637 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3638 parser_print_error_prefix();
3639 fprintf(stderr, "assignment to readonly location '");
3640 print_expression(left);
3641 fprintf(stderr, "' (type ");
3642 print_type_quoted(orig_type_left);
3643 fprintf(stderr, ")\n");
3646 semantic_assign(orig_type_left, &expression->right, "assignment");
3648 expression->expression.datatype = orig_type_left;
3651 static void semantic_comma(binary_expression_t *expression)
3653 expression->expression.datatype = expression->right->datatype;
3656 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3657 static expression_t *parse_##binexpression_type(unsigned precedence, \
3658 expression_t *left) \
3662 expression_t *right = parse_sub_expression(precedence + lr); \
3664 binary_expression_t *binexpr \
3665 = allocate_ast_zero(sizeof(binexpr[0])); \
3666 binexpr->expression.type = EXPR_BINARY; \
3667 binexpr->type = binexpression_type; \
3668 binexpr->left = left; \
3669 binexpr->right = right; \
3672 return (expression_t*) binexpr; \
3675 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3676 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3677 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3678 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3679 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3680 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3681 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3682 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3683 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3684 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3685 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3686 semantic_comparison, 1)
3687 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3688 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3689 semantic_comparison, 1)
3690 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3691 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3692 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3693 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3694 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3695 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3696 semantic_shift_op, 1)
3697 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3698 semantic_shift_op, 1)
3699 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3700 semantic_arithmetic_addsubb_assign, 0)
3701 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3702 semantic_arithmetic_addsubb_assign, 0)
3703 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3704 semantic_arithmetic_assign, 0)
3705 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3706 semantic_arithmetic_assign, 0)
3707 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3708 semantic_arithmetic_assign, 0)
3709 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3710 semantic_arithmetic_assign, 0)
3711 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3712 semantic_arithmetic_assign, 0)
3713 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3714 semantic_arithmetic_assign, 0)
3715 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3716 semantic_arithmetic_assign, 0)
3717 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3718 semantic_arithmetic_assign, 0)
3720 static expression_t *parse_sub_expression(unsigned precedence)
3722 if(token.type < 0) {
3723 return expected_expression_error();
3726 expression_parser_function_t *parser
3727 = &expression_parsers[token.type];
3728 source_position_t source_position = token.source_position;
3731 if(parser->parser != NULL) {
3732 left = parser->parser(parser->precedence);
3734 left = parse_primary_expression();
3736 assert(left != NULL);
3737 left->source_position = source_position;
3740 if(token.type < 0) {
3741 return expected_expression_error();
3744 parser = &expression_parsers[token.type];
3745 if(parser->infix_parser == NULL)
3747 if(parser->infix_precedence < precedence)
3750 left = parser->infix_parser(parser->infix_precedence, left);
3752 assert(left != NULL);
3753 assert(left->type != EXPR_UNKNOWN);
3754 left->source_position = source_position;
3760 static expression_t *parse_expression(void)
3762 return parse_sub_expression(1);
3767 static void register_expression_parser(parse_expression_function parser,
3768 int token_type, unsigned precedence)
3770 expression_parser_function_t *entry = &expression_parsers[token_type];
3772 if(entry->parser != NULL) {
3773 fprintf(stderr, "for token ");
3774 print_token_type(stderr, token_type);
3775 fprintf(stderr, "\n");
3776 panic("trying to register multiple expression parsers for a token");
3778 entry->parser = parser;
3779 entry->precedence = precedence;
3782 static void register_expression_infix_parser(
3783 parse_expression_infix_function parser, int token_type,
3784 unsigned precedence)
3786 expression_parser_function_t *entry = &expression_parsers[token_type];
3788 if(entry->infix_parser != NULL) {
3789 fprintf(stderr, "for token ");
3790 print_token_type(stderr, token_type);
3791 fprintf(stderr, "\n");
3792 panic("trying to register multiple infix expression parsers for a "
3795 entry->infix_parser = parser;
3796 entry->infix_precedence = precedence;
3799 static void init_expression_parsers(void)
3801 memset(&expression_parsers, 0, sizeof(expression_parsers));
3803 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3804 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3805 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3806 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3807 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3808 T_GREATERGREATER, 16);
3809 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3810 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3811 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3812 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3813 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3814 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3815 T_GREATEREQUAL, 14);
3816 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3817 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3818 T_EXCLAMATIONMARKEQUAL, 13);
3819 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3820 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3821 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3822 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3823 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3824 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3825 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3826 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3827 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3828 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3829 T_ASTERISKEQUAL, 2);
3830 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3831 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3833 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3834 T_LESSLESSEQUAL, 2);
3835 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3836 T_GREATERGREATEREQUAL, 2);
3837 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3839 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3841 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3844 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3846 register_expression_infix_parser(parse_array_expression, '[', 30);
3847 register_expression_infix_parser(parse_call_expression, '(', 30);
3848 register_expression_infix_parser(parse_select_expression, '.', 30);
3849 register_expression_infix_parser(parse_select_expression,
3850 T_MINUSGREATER, 30);
3851 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3853 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3856 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3857 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3858 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3859 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3860 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3861 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3862 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3863 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3864 register_expression_parser(parse_sizeof, T_sizeof, 25);
3865 register_expression_parser(parse_extension, T___extension__, 25);
3866 register_expression_parser(parse_builtin_classify_type,
3867 T___builtin_classify_type, 25);
3871 static statement_t *parse_case_statement(void)
3874 statement_t *label = allocate_ast_zero(sizeof(label[0]));
3875 label->type = STATEMENT_CASE_LABEL;
3876 label->source_position = token.source_position;
3878 label->v.case_label_stmt.expression = parse_expression();
3881 label->v.case_label_stmt.label_statement = parse_statement();
3886 static statement_t *parse_default_statement(void)
3890 statement_t *label = allocate_ast_zero(sizeof(label[0]));
3891 label->type = STATEMENT_CASE_LABEL;
3892 label->source_position = token.source_position;
3895 label->v.case_label_stmt.label_statement = parse_statement();
3897 return (statement_t*) label;
3900 static declaration_t *get_label(symbol_t *symbol)
3902 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3903 assert(current_function != NULL);
3904 /* if we found a label in the same function, then we already created the
3906 if(candidate != NULL
3907 && candidate->parent_context == ¤t_function->context) {
3911 /* otherwise we need to create a new one */
3912 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3913 declaration->namespc = NAMESPACE_LABEL;
3914 declaration->symbol = symbol;
3916 label_push(declaration);
3921 static statement_t *parse_label_statement(void)
3923 assert(token.type == T_IDENTIFIER);
3924 symbol_t *symbol = token.v.symbol;
3927 declaration_t *label = get_label(symbol);
3929 /* if source position is already set then the label is defined twice,
3930 * otherwise it was just mentioned in a goto so far */
3931 if(label->source_position.input_name != NULL) {
3932 parser_print_error_prefix();
3933 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3934 parser_print_error_prefix_pos(label->source_position);
3935 fprintf(stderr, "previous definition of '%s' was here\n",
3938 label->source_position = token.source_position;
3941 statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3943 label_statement->type = STATEMENT_LABEL;
3944 label_statement->source_position = token.source_position;
3945 label_statement->v.label_stmt.label = label;
3949 if(token.type == '}') {
3950 parse_error("label at end of compound statement");
3951 return (statement_t*) label_statement;
3953 label_statement->v.label_stmt.label_statement = parse_statement();
3956 return label_statement;
3959 static statement_t *parse_if(void)
3963 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3964 statement->type = STATEMENT_IF;
3965 statement->source_position = token.source_position;
3968 statement->v.if_stmt.condition = parse_expression();
3971 statement->v.if_stmt.true_statement = parse_statement();
3972 if(token.type == T_else) {
3974 statement->v.if_stmt.false_statement = parse_statement();
3980 static statement_t *parse_switch(void)
3984 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3985 statement->type = STATEMENT_SWITCH;
3986 statement->source_position = token.source_position;
3989 statement->v.switch_stmt.expression = parse_expression();
3991 statement->v.switch_stmt.body = parse_statement();
3996 static statement_t *parse_while(void)
4000 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4001 statement->type = STATEMENT_WHILE;
4002 statement->source_position = token.source_position;
4005 statement->v.while_stmt.condition = parse_expression();
4007 statement->v.while_stmt.body = parse_statement();
4012 static statement_t *parse_do(void)
4016 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4017 statement->type = STATEMENT_DO_WHILE;
4018 statement->source_position = token.source_position;
4020 statement->v.while_stmt.body = parse_statement();
4023 statement->v.while_stmt.condition = parse_expression();
4030 static statement_t *parse_for(void)
4034 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4035 statement->type = STATEMENT_FOR;
4036 statement->source_position = token.source_position;
4040 int top = environment_top();
4041 context_t *last_context = context;
4042 set_context(&statement->v.for_stmt.context);
4044 if(token.type != ';') {
4045 if(is_declaration_specifier(&token, false)) {
4046 parse_declaration();
4048 statement->v.for_stmt.initialisation = parse_expression();
4055 if(token.type != ';') {
4056 statement->v.for_stmt.condition = parse_expression();
4059 if(token.type != ')') {
4060 statement->v.for_stmt.step = parse_expression();
4063 statement->v.for_stmt.body = parse_statement();
4065 assert(context == &statement->v.for_stmt.context);
4066 set_context(last_context);
4067 environment_pop_to(top);
4072 static statement_t *parse_goto(void)
4076 if(token.type != T_IDENTIFIER) {
4077 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4081 symbol_t *symbol = token.v.symbol;
4084 declaration_t *label = get_label(symbol);
4086 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4088 statement->type = STATEMENT_GOTO;
4089 statement->source_position = token.source_position;
4091 statement->v.goto_label = label;
4098 static statement_t *parse_continue(void)
4103 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4104 statement->type = STATEMENT_CONTINUE;
4105 statement->source_position = token.source_position;
4110 static statement_t *parse_break(void)
4115 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4116 statement->type = STATEMENT_BREAK;
4117 statement->source_position = token.source_position;
4122 static statement_t *parse_return(void)
4126 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4128 statement->type = STATEMENT_RETURN;
4129 statement->source_position = token.source_position;
4131 assert(current_function->type->type == TYPE_FUNCTION);
4132 type_t *function_type = current_function->type;
4133 type_t *return_type = function_type->v.function_type.result_type;
4135 expression_t *return_value;
4136 if(token.type != ';') {
4137 return_value = parse_expression();
4139 if(return_type == type_void && return_value->datatype != type_void) {
4140 parse_warning("'return' with a value, in function returning void");
4141 return_value = NULL;
4143 if(return_type != NULL) {
4144 semantic_assign(return_type, &return_value, "'return'");
4148 return_value = NULL;
4149 if(return_type != type_void) {
4150 parse_warning("'return' without value, in function returning "
4154 statement->v.return_value = return_value;
4161 static statement_t *parse_declaration_statement(void)
4163 declaration_t *before = last_declaration;
4165 statement_t *statement
4166 = allocate_ast_zero(sizeof(statement[0]));
4167 statement->type = STATEMENT_DECLARATION;
4168 statement->source_position = token.source_position;
4170 declaration_specifiers_t specifiers;
4171 memset(&specifiers, 0, sizeof(specifiers));
4172 parse_declaration_specifiers(&specifiers);
4174 if(token.type == ';') {
4177 parse_init_declarators(&specifiers);
4180 if(before == NULL) {
4181 statement->v.declaration_stmt.begin = context->declarations;
4183 statement->v.declaration_stmt.begin = before->next;
4185 statement->v.declaration_stmt.end = last_declaration;
4190 static statement_t *parse_expression_statement(void)
4192 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4193 statement->type = STATEMENT_EXPRESSION;
4194 statement->source_position = token.source_position;
4196 statement->v.expression = parse_expression();
4203 static statement_t *parse_statement(void)
4205 statement_t *statement = NULL;
4207 /* declaration or statement */
4208 switch(token.type) {
4210 statement = parse_case_statement();
4214 statement = parse_default_statement();
4218 statement = parse_compound_statement();
4222 statement = parse_if();
4226 statement = parse_switch();
4230 statement = parse_while();
4234 statement = parse_do();
4238 statement = parse_for();
4242 statement = parse_goto();
4246 statement = parse_continue();
4250 statement = parse_break();
4254 statement = parse_return();
4263 if(look_ahead(1)->type == ':') {
4264 statement = parse_label_statement();
4268 if(is_typedef_symbol(token.v.symbol)) {
4269 statement = parse_declaration_statement();
4273 statement = parse_expression_statement();
4276 case T___extension__:
4277 /* this can be a prefix to a declaration or an expression statement */
4278 /* we simply eat it now and parse the rest with tail recursion */
4281 } while(token.type == T___extension__);
4282 statement = parse_statement();
4286 statement = parse_declaration_statement();
4290 statement = parse_expression_statement();
4294 assert(statement == NULL || statement->source_position.input_name != NULL);
4299 static statement_t *parse_compound_statement(void)
4301 statement_t *compound_statement
4302 = allocate_ast_zero(sizeof(compound_statement[0]));
4303 compound_statement->type = STATEMENT_COMPOUND;
4304 compound_statement->source_position = token.source_position;
4308 int top = environment_top();
4309 context_t *last_context = context;
4310 set_context(&compound_statement->v.compound_stmt.context);
4312 statement_t *last_statement = NULL;
4314 while(token.type != '}' && token.type != T_EOF) {
4315 statement_t *statement = parse_statement();
4316 if(statement == NULL)
4319 if(last_statement != NULL) {
4320 last_statement->next = statement;
4322 compound_statement->v.compound_stmt.statements = statement;
4325 while(statement->next != NULL)
4326 statement = statement->next;
4328 last_statement = statement;
4331 if(token.type != '}') {
4332 parser_print_error_prefix_pos(
4333 compound_statement->source_position);
4334 fprintf(stderr, "end of file while looking for closing '}'\n");
4338 assert(context == &compound_statement->v.compound_stmt.context);
4339 set_context(last_context);
4340 environment_pop_to(top);
4342 return compound_statement;
4345 static translation_unit_t *parse_translation_unit(void)
4347 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4349 assert(global_context == NULL);
4350 global_context = &unit->context;
4352 assert(context == NULL);
4353 set_context(&unit->context);
4355 while(token.type != T_EOF) {
4356 parse_declaration();
4359 assert(context == &unit->context);
4361 last_declaration = NULL;
4363 assert(global_context == &unit->context);
4364 global_context = NULL;
4369 translation_unit_t *parse(void)
4371 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4372 label_stack = NEW_ARR_F(stack_entry_t, 0);
4373 found_error = false;
4375 type_set_output(stderr);
4376 ast_set_output(stderr);
4378 lookahead_bufpos = 0;
4379 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4382 translation_unit_t *unit = parse_translation_unit();
4384 DEL_ARR_F(environment_stack);
4385 DEL_ARR_F(label_stack);
4393 void init_parser(void)
4395 init_expression_parsers();
4396 obstack_init(&temp_obst);
4398 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4399 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4400 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4401 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4402 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4403 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4404 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4405 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4406 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4407 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4408 type_string = make_pointer_type(type_const_char, TYPE_QUALIFIER_NONE);
4411 void exit_parser(void)
4413 obstack_free(&temp_obst, NULL);