11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_uint = NULL;
42 static type_t *type_long_double = NULL;
43 static type_t *type_double = NULL;
44 static type_t *type_float = NULL;
45 static type_t *type_const_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_void_ptr = NULL;
49 static type_t *type_size_t = NULL;
50 static type_t *type_ptrdiff_t = NULL;
52 static statement_t *parse_compound_statement(void);
53 static statement_t *parse_statement(void);
55 static expression_t *parse_sub_expression(unsigned precedence);
56 static expression_t *parse_expression(void);
57 static type_t *parse_typename(void);
59 #define STORAGE_CLASSES \
66 #define TYPE_QUALIFIERS \
72 #ifdef PROVIDE_COMPLEX
73 #define COMPLEX_SPECIFIERS \
75 #define IMAGINARY_SPECIFIERS \
78 #define COMPLEX_SPECIFIERS
79 #define IMAGINARY_SPECIFIERS
82 #define TYPE_SPECIFIERS \
100 #define DECLARATION_START \
105 #define TYPENAME_START \
109 static inline void *allocate_ast_zero(size_t size)
111 void *res = allocate_ast(size);
112 memset(res, 0, size);
116 static inline void *allocate_type_zero(size_t size)
118 void *res = obstack_alloc(type_obst, size);
119 memset(res, 0, size);
123 static inline void free_type(void *type)
125 obstack_free(type_obst, type);
129 * returns the top element of the environment stack
131 static inline size_t environment_top(void)
133 return ARR_LEN(environment_stack);
136 static inline size_t label_top(void)
138 return ARR_LEN(label_stack);
143 static inline void next_token(void)
145 token = lookahead_buffer[lookahead_bufpos];
146 lookahead_buffer[lookahead_bufpos] = lexer_token;
149 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
152 print_token(stderr, &token);
153 fprintf(stderr, "\n");
157 static inline const token_t *look_ahead(int num)
159 assert(num > 0 && num <= MAX_LOOKAHEAD);
160 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
161 return & lookahead_buffer[pos];
164 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
166 static void error(void)
169 #ifdef ABORT_ON_ERROR
174 static void parser_print_prefix_pos(const source_position_t source_position)
176 fputs(source_position.input_name, stderr);
178 fprintf(stderr, "%d", source_position.linenr);
182 static void parser_print_error_prefix_pos(
183 const source_position_t source_position)
185 parser_print_prefix_pos(source_position);
186 fputs("error: ", stderr);
190 static void parser_print_error_prefix(void)
192 parser_print_error_prefix_pos(token.source_position);
195 static void parse_error(const char *message)
197 parser_print_error_prefix();
198 fprintf(stderr, "parse error: %s\n", message);
201 static void parser_print_warning_prefix_pos(
202 const source_position_t source_position)
204 parser_print_prefix_pos(source_position);
205 fputs("warning: ", stderr);
208 static void parse_warning_pos(const source_position_t source_position,
209 const char *const message)
211 parser_print_prefix_pos(source_position);
212 fprintf(stderr, "warning: %s\n", message);
215 static void parse_warning(const char *message)
217 parse_warning_pos(token.source_position, message);
220 static void parse_error_expected(const char *message, ...)
225 if(message != NULL) {
226 parser_print_error_prefix();
227 fprintf(stderr, "%s\n", message);
229 parser_print_error_prefix();
230 fputs("Parse error: got ", stderr);
231 print_token(stderr, &token);
232 fputs(", expected ", stderr);
234 va_start(args, message);
235 token_type_t token_type = va_arg(args, token_type_t);
236 while(token_type != 0) {
240 fprintf(stderr, ", ");
242 print_token_type(stderr, token_type);
243 token_type = va_arg(args, token_type_t);
246 fprintf(stderr, "\n");
249 static void print_type_quoted(type_t *type)
256 static void type_error(const char *msg, const source_position_t source_position,
259 parser_print_error_prefix_pos(source_position);
260 fprintf(stderr, "%s, but found type ", msg);
261 print_type_quoted(type);
265 static void type_error_incompatible(const char *msg,
266 const source_position_t source_position, type_t *type1, type_t *type2)
268 parser_print_error_prefix_pos(source_position);
269 fprintf(stderr, "%s, incompatible types: ", msg);
270 print_type_quoted(type1);
271 fprintf(stderr, " - ");
272 print_type_quoted(type2);
273 fprintf(stderr, ")\n");
276 static void eat_block(void)
278 if(token.type == '{')
281 while(token.type != '}') {
282 if(token.type == T_EOF)
284 if(token.type == '{') {
293 static void eat_statement(void)
295 while(token.type != ';') {
296 if(token.type == T_EOF)
298 if(token.type == '}')
300 if(token.type == '{') {
309 static void eat_brace(void)
311 if(token.type == '(')
314 while(token.type != ')') {
315 if(token.type == T_EOF)
317 if(token.type == ')' || token.type == ';' || token.type == '}') {
320 if(token.type == '(') {
324 if(token.type == '{') {
333 #define expect(expected) \
334 if(UNLIKELY(token.type != (expected))) { \
335 parse_error_expected(NULL, (expected), 0); \
341 #define expect_block(expected) \
342 if(UNLIKELY(token.type != (expected))) { \
343 parse_error_expected(NULL, (expected), 0); \
349 #define expect_void(expected) \
350 if(UNLIKELY(token.type != (expected))) { \
351 parse_error_expected(NULL, (expected), 0); \
357 static void set_context(context_t *new_context)
359 context = new_context;
361 last_declaration = new_context->declarations;
362 if(last_declaration != NULL) {
363 while(last_declaration->next != NULL) {
364 last_declaration = last_declaration->next;
370 * called when we find a 2nd declarator for an identifier we already have a
373 static bool is_compatible_declaration (declaration_t *declaration,
374 declaration_t *previous)
376 /* TODO: not correct yet */
377 return declaration->type == previous->type;
380 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
382 declaration_t *declaration = symbol->declaration;
383 for( ; declaration != NULL; declaration = declaration->symbol_next) {
384 if(declaration->namespc == namespc)
391 static const char *get_namespace_prefix(namespace_t namespc)
394 case NAMESPACE_NORMAL:
396 case NAMESPACE_UNION:
398 case NAMESPACE_STRUCT:
402 case NAMESPACE_LABEL:
405 panic("invalid namespace found");
409 * pushs an environment_entry on the environment stack and links the
410 * corresponding symbol to the new entry
412 static declaration_t *stack_push(stack_entry_t **stack_ptr,
413 declaration_t *declaration,
414 context_t *parent_context)
416 symbol_t *symbol = declaration->symbol;
417 namespace_t namespc = (namespace_t)declaration->namespc;
419 /* a declaration should be only pushed once */
420 assert(declaration->parent_context == NULL);
421 declaration->parent_context = parent_context;
423 declaration_t *previous_declaration = get_declaration(symbol, namespc);
424 assert(declaration != previous_declaration);
425 if(previous_declaration != NULL
426 && previous_declaration->parent_context == context) {
427 if(!is_compatible_declaration(declaration, previous_declaration)) {
428 parser_print_error_prefix_pos(declaration->source_position);
429 fprintf(stderr, "definition of symbol %s%s with type ",
430 get_namespace_prefix(namespc), symbol->string);
431 print_type_quoted(declaration->type);
433 parser_print_error_prefix_pos(
434 previous_declaration->source_position);
435 fprintf(stderr, "is incompatible with previous declaration "
437 print_type_quoted(previous_declaration->type);
440 const storage_class_t old_storage = previous_declaration->storage_class;
441 const storage_class_t new_storage = declaration->storage_class;
442 if (current_function == NULL) {
443 if (old_storage != STORAGE_CLASS_STATIC &&
444 new_storage == STORAGE_CLASS_STATIC) {
445 parser_print_error_prefix_pos(declaration->source_position);
447 "static declaration of '%s' follows non-static declaration\n",
449 parser_print_error_prefix_pos(previous_declaration->source_position);
450 fprintf(stderr, "previous declaration of '%s' was here\n",
453 if (old_storage == STORAGE_CLASS_EXTERN) {
454 if (new_storage == STORAGE_CLASS_NONE) {
455 previous_declaration->storage_class = STORAGE_CLASS_NONE;
458 parser_print_warning_prefix_pos(declaration->source_position);
459 fprintf(stderr, "redundant declaration for '%s'\n",
461 parser_print_warning_prefix_pos(previous_declaration->source_position);
462 fprintf(stderr, "previous declaration of '%s' was here\n",
467 if (old_storage == STORAGE_CLASS_EXTERN &&
468 new_storage == STORAGE_CLASS_EXTERN) {
469 parser_print_warning_prefix_pos(declaration->source_position);
470 fprintf(stderr, "redundant extern declaration for '%s'\n",
472 parser_print_warning_prefix_pos(previous_declaration->source_position);
473 fprintf(stderr, "previous declaration of '%s' was here\n",
476 parser_print_error_prefix_pos(declaration->source_position);
477 if (old_storage == new_storage) {
478 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
480 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
482 parser_print_error_prefix_pos(previous_declaration->source_position);
483 fprintf(stderr, "previous declaration of '%s' was here\n",
488 return previous_declaration;
491 /* remember old declaration */
493 entry.symbol = symbol;
494 entry.old_declaration = symbol->declaration;
495 entry.namespc = namespc;
496 ARR_APP1(stack_entry_t, *stack_ptr, entry);
498 /* replace/add declaration into declaration list of the symbol */
499 if(symbol->declaration == NULL) {
500 symbol->declaration = declaration;
502 declaration_t *iter_last = NULL;
503 declaration_t *iter = symbol->declaration;
504 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
505 /* replace an entry? */
506 if(iter->namespc == namespc) {
507 if(iter_last == NULL) {
508 symbol->declaration = declaration;
510 iter_last->symbol_next = declaration;
512 declaration->symbol_next = iter->symbol_next;
517 assert(iter_last->symbol_next == NULL);
518 iter_last->symbol_next = declaration;
525 static declaration_t *environment_push(declaration_t *declaration)
527 assert(declaration->source_position.input_name != NULL);
528 return stack_push(&environment_stack, declaration, context);
531 static declaration_t *label_push(declaration_t *declaration)
533 return stack_push(&label_stack, declaration, ¤t_function->context);
537 * pops symbols from the environment stack until @p new_top is the top element
539 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
541 stack_entry_t *stack = *stack_ptr;
542 size_t top = ARR_LEN(stack);
545 assert(new_top <= top);
549 for(i = top; i > new_top; --i) {
550 stack_entry_t *entry = & stack[i - 1];
552 declaration_t *old_declaration = entry->old_declaration;
553 symbol_t *symbol = entry->symbol;
554 namespace_t namespc = (namespace_t)entry->namespc;
556 /* replace/remove declaration */
557 declaration_t *declaration = symbol->declaration;
558 assert(declaration != NULL);
559 if(declaration->namespc == namespc) {
560 if(old_declaration == NULL) {
561 symbol->declaration = declaration->symbol_next;
563 symbol->declaration = old_declaration;
566 declaration_t *iter_last = declaration;
567 declaration_t *iter = declaration->symbol_next;
568 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
569 /* replace an entry? */
570 if(iter->namespc == namespc) {
571 assert(iter_last != NULL);
572 iter_last->symbol_next = old_declaration;
573 old_declaration->symbol_next = iter->symbol_next;
577 assert(iter != NULL);
581 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
584 static void environment_pop_to(size_t new_top)
586 stack_pop_to(&environment_stack, new_top);
589 static void label_pop_to(size_t new_top)
591 stack_pop_to(&label_stack, new_top);
595 static int get_rank(const type_t *type)
597 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
598 * and esp. footnote 108). However we can't fold constants (yet), so we
599 * can't decide wether unsigned int is possible, while int always works.
600 * (unsigned int would be preferable when possible... for stuff like
601 * struct { enum { ... } bla : 4; } ) */
602 if(type->type == TYPE_ENUM)
603 return ATOMIC_TYPE_INT;
605 assert(type->type == TYPE_ATOMIC);
606 atomic_type_t *atomic_type = (atomic_type_t*) type;
607 atomic_type_type_t atype = atomic_type->atype;
611 static type_t *promote_integer(type_t *type)
613 if(get_rank(type) < ATOMIC_TYPE_INT)
619 static expression_t *create_cast_expression(expression_t *expression,
622 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
624 cast->expression.type = EXPR_UNARY;
625 cast->type = UNEXPR_CAST;
626 cast->value = expression;
627 cast->expression.datatype = dest_type;
629 return (expression_t*) cast;
632 static bool is_null_expression(const expression_t *const expr)
634 if (expr->type != EXPR_CONST) return false;
636 type_t *const type = skip_typeref(expr->datatype);
637 if (!is_type_integer(type)) return false;
639 const const_t *const const_expr = (const const_t*)expr;
640 return const_expr->v.int_value == 0;
643 static expression_t *create_implicit_cast(expression_t *expression,
646 type_t *source_type = expression->datatype;
648 if(source_type == NULL)
651 source_type = skip_typeref(source_type);
652 dest_type = skip_typeref(dest_type);
654 if(source_type == dest_type)
657 if(dest_type->type == TYPE_ATOMIC) {
658 if(source_type->type != TYPE_ATOMIC)
659 panic("casting of non-atomic types not implemented yet");
661 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
662 type_error_incompatible("can't cast types",
663 expression->source_position,
664 source_type, dest_type);
668 return create_cast_expression(expression, dest_type);
670 if(dest_type->type == TYPE_POINTER) {
671 pointer_type_t *pointer_type
672 = (pointer_type_t*) dest_type;
673 switch (source_type->type) {
675 if (is_null_expression(expression)) {
676 return create_cast_expression(expression, dest_type);
681 if (pointers_compatible(source_type, dest_type)) {
682 return create_cast_expression(expression, dest_type);
687 array_type_t *const array_type = (array_type_t*) source_type;
688 if (types_compatible(array_type->element_type,
689 pointer_type->points_to)) {
690 return create_cast_expression(expression, dest_type);
696 panic("casting of non-atomic types not implemented yet");
699 type_error_incompatible("can't implicitly cast types",
700 expression->source_position,
701 source_type, dest_type);
705 panic("casting of non-atomic types not implemented yet");
708 static void semantic_assign(type_t *orig_type_left, expression_t **right,
711 type_t *orig_type_right = (*right)->datatype;
713 if(orig_type_right == NULL)
716 type_t *const type_left = skip_typeref(orig_type_left);
717 type_t *const type_right = skip_typeref(orig_type_right);
719 if (types_compatible(type_left, type_right)) {
723 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
724 (type_left->type == TYPE_POINTER && is_null_expression(*right)) ||
725 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
726 *right = create_implicit_cast(*right, type_left);
730 if (type_left->type == TYPE_POINTER) {
731 switch (type_right->type) {
732 case TYPE_FUNCTION: {
733 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
734 if (ptr_type->points_to == type_right) {
741 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
742 array_type_t *const arr_type = (array_type_t*)type_right;
743 if (ptr_type->points_to == arr_type->element_type) {
753 /* TODO: improve error message */
754 parser_print_error_prefix();
755 fprintf(stderr, "incompatible types in %s\n", context);
756 parser_print_error_prefix();
757 print_type_quoted(type_left);
758 fputs(" <- ", stderr);
759 print_type_quoted(type_right);
763 static expression_t *parse_constant_expression(void)
765 /* start parsing at precedence 7 (conditional expression) */
766 return parse_sub_expression(7);
769 static expression_t *parse_assignment_expression(void)
771 /* start parsing at precedence 2 (assignment expression) */
772 return parse_sub_expression(2);
775 typedef struct declaration_specifiers_t declaration_specifiers_t;
776 struct declaration_specifiers_t {
777 storage_class_t storage_class;
782 static void parse_compound_type_entries(void);
783 static declaration_t *parse_declarator(
784 const declaration_specifiers_t *specifiers, type_t *type,
785 bool may_be_abstract);
786 static declaration_t *record_declaration(declaration_t *declaration);
788 static const char *parse_string_literals(void)
790 assert(token.type == T_STRING_LITERAL);
791 const char *result = token.v.string;
795 while(token.type == T_STRING_LITERAL) {
796 result = concat_strings(result, token.v.string);
803 static void parse_attributes(void)
807 case T___attribute__:
815 parse_error("EOF while parsing attribute");
833 if(token.type != T_STRING_LITERAL) {
834 parse_error_expected("while parsing assembler attribute",
839 parse_string_literals();
844 goto attributes_finished;
853 static designator_t *parse_designation(void)
855 if(token.type != '[' && token.type != '.')
858 designator_t *result = NULL;
859 designator_t *last = NULL;
862 designator_t *designator;
865 designator = allocate_ast_zero(sizeof(designator[0]));
867 designator->array_access = parse_constant_expression();
871 designator = allocate_ast_zero(sizeof(designator[0]));
873 if(token.type != T_IDENTIFIER) {
874 parse_error_expected("while parsing designator",
878 designator->symbol = token.v.symbol;
886 assert(designator != NULL);
888 last->next = designator;
897 static initializer_t *initializer_from_string(array_type_t *type,
900 /* TODO: check len vs. size of array type */
903 initializer_string_t *initializer
904 = allocate_ast_zero(sizeof(initializer[0]));
906 initializer->initializer.type = INITIALIZER_STRING;
907 initializer->string = string;
909 return (initializer_t*) initializer;
912 static initializer_t *initializer_from_expression(type_t *type,
913 expression_t *expression)
916 /* TODO check that expression is a constant expression */
918 /* § 6.7.8.14/15 char array may be initialized by string literals */
919 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
920 array_type_t *array_type = (array_type_t*) type;
921 type_t *element_type = array_type->element_type;
923 if(element_type->type == TYPE_ATOMIC) {
924 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
925 atomic_type_type_t atype = atomic_type->atype;
927 /* TODO handle wide strings */
928 if(atype == ATOMIC_TYPE_CHAR
929 || atype == ATOMIC_TYPE_SCHAR
930 || atype == ATOMIC_TYPE_UCHAR) {
932 string_literal_t *literal = (string_literal_t*) expression;
933 return initializer_from_string(array_type, literal->value);
938 semantic_assign(type, &expression, "initializer");
940 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
941 result->initializer.type = INITIALIZER_VALUE;
942 result->value = expression;
944 return (initializer_t*) result;
947 static initializer_t *parse_sub_initializer(type_t *type,
948 expression_t *expression,
949 type_t *expression_type);
951 static initializer_t *parse_sub_initializer_elem(type_t *type)
953 if(token.type == '{') {
954 return parse_sub_initializer(type, NULL, NULL);
957 expression_t *expression = parse_assignment_expression();
958 type_t *expression_type = skip_typeref(expression->datatype);
960 return parse_sub_initializer(type, expression, expression_type);
963 static bool had_initializer_brace_warning;
965 static initializer_t *parse_sub_initializer(type_t *type,
966 expression_t *expression,
967 type_t *expression_type)
969 if(is_type_scalar(type)) {
970 /* there might be extra {} hierarchies */
971 if(token.type == '{') {
973 if(!had_initializer_brace_warning) {
974 parse_warning("braces around scalar initializer");
975 had_initializer_brace_warning = true;
977 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
978 if(token.type == ',') {
980 /* TODO: warn about excessive elements */
986 if(expression == NULL) {
987 expression = parse_assignment_expression();
989 return initializer_from_expression(type, expression);
992 /* TODO: ignore qualifiers, comparing pointers is probably
994 if(expression != NULL && expression_type == type) {
995 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
996 result->initializer.type = INITIALIZER_VALUE;
999 semantic_assign(type, &expression, "initializer");
1001 result->value = expression;
1003 return (initializer_t*) result;
1006 bool read_paren = false;
1007 if(token.type == '{') {
1012 /* descend into subtype */
1013 initializer_t *result = NULL;
1014 initializer_t **elems;
1015 if(type->type == TYPE_ARRAY) {
1016 array_type_t *array_type = (array_type_t*) type;
1017 type_t *element_type = array_type->element_type;
1018 element_type = skip_typeref(element_type);
1021 had_initializer_brace_warning = false;
1022 if(expression == NULL) {
1023 sub = parse_sub_initializer_elem(element_type);
1025 sub = parse_sub_initializer(element_type, expression,
1029 /* didn't match the subtypes -> try the parent type */
1031 assert(!read_paren);
1035 elems = NEW_ARR_F(initializer_t*, 0);
1036 ARR_APP1(initializer_t*, elems, sub);
1039 if(token.type == '}')
1042 if(token.type == '}')
1046 = parse_sub_initializer(element_type, NULL, NULL);
1048 /* TODO error, do nicer cleanup */
1049 parse_error("member initializer didn't match");
1053 ARR_APP1(initializer_t*, elems, sub);
1056 assert(type->type == TYPE_COMPOUND_STRUCT
1057 || type->type == TYPE_COMPOUND_UNION);
1058 compound_type_t *compound_type = (compound_type_t*) type;
1059 context_t *context = & compound_type->declaration->context;
1061 declaration_t *first = context->declarations;
1064 type_t *first_type = first->type;
1065 first_type = skip_typeref(first_type);
1068 had_initializer_brace_warning = false;
1069 if(expression == NULL) {
1070 sub = parse_sub_initializer_elem(first_type);
1072 sub = parse_sub_initializer(first_type, expression,expression_type);
1075 /* didn't match the subtypes -> try our parent type */
1077 assert(!read_paren);
1081 elems = NEW_ARR_F(initializer_t*, 0);
1082 ARR_APP1(initializer_t*, elems, sub);
1084 declaration_t *iter = first->next;
1085 for( ; iter != NULL; iter = iter->next) {
1086 if(iter->symbol == NULL)
1088 if(iter->namespc != NAMESPACE_NORMAL)
1091 if(token.type == '}')
1095 type_t *iter_type = iter->type;
1096 iter_type = skip_typeref(iter_type);
1098 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1100 /* TODO error, do nicer cleanup*/
1101 parse_error("member initializer didn't match");
1105 ARR_APP1(initializer_t*, elems, sub);
1109 int len = ARR_LEN(elems);
1110 size_t elems_size = sizeof(initializer_t*) * len;
1112 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1114 init->initializer.type = INITIALIZER_LIST;
1116 memcpy(init->initializers, elems, elems_size);
1119 result = (initializer_t*) init;
1122 if(token.type == ',')
1129 static initializer_t *parse_initializer(type_t *type)
1131 initializer_t *result;
1133 type = skip_typeref(type);
1135 if(token.type != '{') {
1136 expression_t *expression = parse_assignment_expression();
1137 return initializer_from_expression(type, expression);
1140 if(is_type_scalar(type)) {
1144 expression_t *expression = parse_assignment_expression();
1145 result = initializer_from_expression(type, expression);
1147 if(token.type == ',')
1153 result = parse_sub_initializer(type, NULL, NULL);
1161 static declaration_t *parse_compound_type_specifier(bool is_struct)
1169 symbol_t *symbol = NULL;
1170 declaration_t *declaration = NULL;
1172 if (token.type == T___attribute__) {
1177 if(token.type == T_IDENTIFIER) {
1178 symbol = token.v.symbol;
1182 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1184 declaration = get_declaration(symbol, NAMESPACE_UNION);
1186 } else if(token.type != '{') {
1188 parse_error_expected("while parsing struct type specifier",
1189 T_IDENTIFIER, '{', 0);
1191 parse_error_expected("while parsing union type specifier",
1192 T_IDENTIFIER, '{', 0);
1198 if(declaration == NULL) {
1199 declaration = allocate_type_zero(sizeof(declaration[0]));
1202 declaration->namespc = NAMESPACE_STRUCT;
1204 declaration->namespc = NAMESPACE_UNION;
1206 declaration->source_position = token.source_position;
1207 declaration->symbol = symbol;
1208 record_declaration(declaration);
1211 if(token.type == '{') {
1212 if(declaration->init.is_defined) {
1213 assert(symbol != NULL);
1214 parser_print_error_prefix();
1215 fprintf(stderr, "multiple definition of %s %s\n",
1216 is_struct ? "struct" : "union", symbol->string);
1217 declaration->context.declarations = NULL;
1219 declaration->init.is_defined = true;
1221 int top = environment_top();
1222 context_t *last_context = context;
1223 set_context(& declaration->context);
1225 parse_compound_type_entries();
1228 assert(context == & declaration->context);
1229 set_context(last_context);
1230 environment_pop_to(top);
1236 static void parse_enum_entries(type_t *enum_type)
1240 if(token.type == '}') {
1242 parse_error("empty enum not allowed");
1247 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1249 if(token.type != T_IDENTIFIER) {
1250 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1254 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1255 entry->type = enum_type;
1256 entry->symbol = token.v.symbol;
1257 entry->source_position = token.source_position;
1260 if(token.type == '=') {
1262 entry->init.enum_value = parse_constant_expression();
1267 record_declaration(entry);
1269 if(token.type != ',')
1272 } while(token.type != '}');
1277 static declaration_t *parse_enum_specifier(void)
1281 declaration_t *declaration;
1284 if(token.type == T_IDENTIFIER) {
1285 symbol = token.v.symbol;
1288 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1289 } else if(token.type != '{') {
1290 parse_error_expected("while parsing enum type specifier",
1291 T_IDENTIFIER, '{', 0);
1298 if(declaration == NULL) {
1299 declaration = allocate_type_zero(sizeof(declaration[0]));
1301 declaration->namespc = NAMESPACE_ENUM;
1302 declaration->source_position = token.source_position;
1303 declaration->symbol = symbol;
1306 if(token.type == '{') {
1307 if(declaration->init.is_defined) {
1308 parser_print_error_prefix();
1309 fprintf(stderr, "multiple definitions of enum %s\n",
1312 record_declaration(declaration);
1313 declaration->init.is_defined = 1;
1315 parse_enum_entries(NULL);
1323 * if a symbol is a typedef to another type, return true
1325 static bool is_typedef_symbol(symbol_t *symbol)
1327 const declaration_t *const declaration =
1328 get_declaration(symbol, NAMESPACE_NORMAL);
1330 declaration != NULL &&
1331 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1334 static type_t *parse_typeof(void)
1342 expression_t *expression = NULL;
1345 switch(token.type) {
1346 case T___extension__:
1347 /* this can be a prefix to a typename or an expression */
1348 /* we simply eat it now. */
1351 } while(token.type == T___extension__);
1355 if(is_typedef_symbol(token.v.symbol)) {
1356 type = parse_typename();
1358 expression = parse_expression();
1359 type = expression->datatype;
1364 type = parse_typename();
1368 expression = parse_expression();
1369 type = expression->datatype;
1375 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1376 typeof->type.type = TYPE_TYPEOF;
1377 typeof->expression = expression;
1378 typeof->typeof_type = type;
1380 return (type_t*) typeof;
1384 SPECIFIER_SIGNED = 1 << 0,
1385 SPECIFIER_UNSIGNED = 1 << 1,
1386 SPECIFIER_LONG = 1 << 2,
1387 SPECIFIER_INT = 1 << 3,
1388 SPECIFIER_DOUBLE = 1 << 4,
1389 SPECIFIER_CHAR = 1 << 5,
1390 SPECIFIER_SHORT = 1 << 6,
1391 SPECIFIER_LONG_LONG = 1 << 7,
1392 SPECIFIER_FLOAT = 1 << 8,
1393 SPECIFIER_BOOL = 1 << 9,
1394 SPECIFIER_VOID = 1 << 10,
1395 #ifdef PROVIDE_COMPLEX
1396 SPECIFIER_COMPLEX = 1 << 11,
1397 SPECIFIER_IMAGINARY = 1 << 12,
1401 static type_t *create_builtin_type(symbol_t *symbol)
1403 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1404 type->type.type = TYPE_BUILTIN;
1405 type->symbol = symbol;
1407 type->real_type = type_int;
1409 return (type_t*) type;
1412 static type_t *get_typedef_type(symbol_t *symbol)
1414 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1415 if(declaration == NULL
1416 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1419 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1420 typedef_type->type.type = TYPE_TYPEDEF;
1421 typedef_type->declaration = declaration;
1423 return (type_t*) typedef_type;
1426 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1428 type_t *type = NULL;
1429 unsigned type_qualifiers = 0;
1430 unsigned type_specifiers = 0;
1434 switch(token.type) {
1437 #define MATCH_STORAGE_CLASS(token, class) \
1439 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1440 parse_error("multiple storage classes in declaration " \
1443 specifiers->storage_class = class; \
1447 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1448 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1449 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1450 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1451 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1453 /* type qualifiers */
1454 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1456 type_qualifiers |= qualifier; \
1460 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1461 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1462 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1464 case T___extension__:
1469 /* type specifiers */
1470 #define MATCH_SPECIFIER(token, specifier, name) \
1473 if(type_specifiers & specifier) { \
1474 parse_error("multiple " name " type specifiers given"); \
1476 type_specifiers |= specifier; \
1480 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1481 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1482 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1483 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1484 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1485 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1486 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1487 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1488 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1489 #ifdef PROVIDE_COMPLEX
1490 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1491 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1495 specifiers->is_inline = true;
1500 if(type_specifiers & SPECIFIER_LONG_LONG) {
1501 parse_error("multiple type specifiers given");
1502 } else if(type_specifiers & SPECIFIER_LONG) {
1503 type_specifiers |= SPECIFIER_LONG_LONG;
1505 type_specifiers |= SPECIFIER_LONG;
1509 /* TODO: if type != NULL for the following rules should issue
1512 compound_type_t *compound_type
1513 = allocate_type_zero(sizeof(compound_type[0]));
1514 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1515 compound_type->declaration = parse_compound_type_specifier(true);
1517 type = (type_t*) compound_type;
1521 compound_type_t *compound_type
1522 = allocate_type_zero(sizeof(compound_type[0]));
1523 compound_type->type.type = TYPE_COMPOUND_UNION;
1524 compound_type->declaration = parse_compound_type_specifier(false);
1526 type = (type_t*) compound_type;
1530 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1531 enum_type->type.type = TYPE_ENUM;
1532 enum_type->declaration = parse_enum_specifier();
1534 type = (type_t*) enum_type;
1538 type = parse_typeof();
1540 case T___builtin_va_list:
1541 type = create_builtin_type(token.v.symbol);
1545 case T___attribute__:
1550 case T_IDENTIFIER: {
1551 type_t *typedef_type = get_typedef_type(token.v.symbol);
1553 if(typedef_type == NULL)
1554 goto finish_specifiers;
1557 type = typedef_type;
1561 /* function specifier */
1563 goto finish_specifiers;
1570 atomic_type_type_t atomic_type;
1572 /* match valid basic types */
1573 switch(type_specifiers) {
1574 case SPECIFIER_VOID:
1575 atomic_type = ATOMIC_TYPE_VOID;
1577 case SPECIFIER_CHAR:
1578 atomic_type = ATOMIC_TYPE_CHAR;
1580 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1581 atomic_type = ATOMIC_TYPE_SCHAR;
1583 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1584 atomic_type = ATOMIC_TYPE_UCHAR;
1586 case SPECIFIER_SHORT:
1587 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1588 case SPECIFIER_SHORT | SPECIFIER_INT:
1589 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1590 atomic_type = ATOMIC_TYPE_SHORT;
1592 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1593 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1594 atomic_type = ATOMIC_TYPE_USHORT;
1597 case SPECIFIER_SIGNED:
1598 case SPECIFIER_SIGNED | SPECIFIER_INT:
1599 atomic_type = ATOMIC_TYPE_INT;
1601 case SPECIFIER_UNSIGNED:
1602 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1603 atomic_type = ATOMIC_TYPE_UINT;
1605 case SPECIFIER_LONG:
1606 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1607 case SPECIFIER_LONG | SPECIFIER_INT:
1608 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1609 atomic_type = ATOMIC_TYPE_LONG;
1611 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1612 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1613 atomic_type = ATOMIC_TYPE_ULONG;
1615 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1616 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1617 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1618 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1620 atomic_type = ATOMIC_TYPE_LONGLONG;
1622 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1623 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1625 atomic_type = ATOMIC_TYPE_ULONGLONG;
1627 case SPECIFIER_FLOAT:
1628 atomic_type = ATOMIC_TYPE_FLOAT;
1630 case SPECIFIER_DOUBLE:
1631 atomic_type = ATOMIC_TYPE_DOUBLE;
1633 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1634 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1636 case SPECIFIER_BOOL:
1637 atomic_type = ATOMIC_TYPE_BOOL;
1639 #ifdef PROVIDE_COMPLEX
1640 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1641 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1643 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1644 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1646 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1647 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1649 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1650 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1652 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1653 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1655 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1656 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1660 /* invalid specifier combination, give an error message */
1661 if(type_specifiers == 0) {
1663 parse_warning("no type specifiers in declaration (using int)");
1664 atomic_type = ATOMIC_TYPE_INT;
1667 parse_error("no type specifiers given in declaration");
1669 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1670 (type_specifiers & SPECIFIER_UNSIGNED)) {
1671 parse_error("signed and unsigned specifiers gives");
1672 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1673 parse_error("only integer types can be signed or unsigned");
1675 parse_error("multiple datatypes in declaration");
1677 atomic_type = ATOMIC_TYPE_INVALID;
1680 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1681 atype->type.type = TYPE_ATOMIC;
1682 atype->atype = atomic_type;
1685 type = (type_t*) atype;
1687 if(type_specifiers != 0) {
1688 parse_error("multiple datatypes in declaration");
1692 type->qualifiers = (type_qualifier_t)type_qualifiers;
1694 type_t *result = typehash_insert(type);
1695 if(newtype && result != (type_t*) type) {
1699 specifiers->type = result;
1702 static unsigned parse_type_qualifiers(void)
1704 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1707 switch(token.type) {
1708 /* type qualifiers */
1709 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1710 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1711 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1714 return type_qualifiers;
1719 static void parse_identifier_list(void)
1722 if(token.type != T_IDENTIFIER) {
1723 parse_error_expected("while parsing parameter identifier list",
1728 if(token.type != ',')
1734 static declaration_t *parse_parameter(void)
1736 declaration_specifiers_t specifiers;
1737 memset(&specifiers, 0, sizeof(specifiers));
1739 parse_declaration_specifiers(&specifiers);
1741 declaration_t *declaration
1742 = parse_declarator(&specifiers, specifiers.type, true);
1744 /* TODO check declaration constraints for parameters */
1745 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1746 parse_error("typedef not allowed in parameter list");
1749 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1751 if (declaration->type->type == TYPE_ARRAY) {
1752 const array_type_t *const arr_type =
1753 (const array_type_t*)declaration->type;
1755 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1761 static declaration_t *parse_parameters(function_type_t *type)
1763 if(token.type == T_IDENTIFIER) {
1764 symbol_t *symbol = token.v.symbol;
1765 if(!is_typedef_symbol(symbol)) {
1766 /* TODO: K&R style C parameters */
1767 parse_identifier_list();
1772 if(token.type == ')') {
1773 type->unspecified_parameters = 1;
1776 if(token.type == T_void && look_ahead(1)->type == ')') {
1781 declaration_t *declarations = NULL;
1782 declaration_t *declaration;
1783 declaration_t *last_declaration = NULL;
1784 function_parameter_t *parameter;
1785 function_parameter_t *last_parameter = NULL;
1788 switch(token.type) {
1792 return declarations;
1795 case T___extension__:
1797 declaration = parse_parameter();
1799 parameter = allocate_type_zero(sizeof(parameter[0]));
1800 parameter->type = declaration->type;
1802 if(last_parameter != NULL) {
1803 last_declaration->next = declaration;
1804 last_parameter->next = parameter;
1806 type->parameters = parameter;
1807 declarations = declaration;
1809 last_parameter = parameter;
1810 last_declaration = declaration;
1814 return declarations;
1816 if(token.type != ',')
1817 return declarations;
1827 } construct_type_type_t;
1829 typedef struct construct_type_t construct_type_t;
1830 struct construct_type_t {
1831 construct_type_type_t type;
1832 construct_type_t *next;
1835 typedef struct parsed_pointer_t parsed_pointer_t;
1836 struct parsed_pointer_t {
1837 construct_type_t construct_type;
1838 type_qualifier_t type_qualifiers;
1841 typedef struct construct_function_type_t construct_function_type_t;
1842 struct construct_function_type_t {
1843 construct_type_t construct_type;
1844 function_type_t *function_type;
1847 typedef struct parsed_array_t parsed_array_t;
1848 struct parsed_array_t {
1849 construct_type_t construct_type;
1850 type_qualifier_t type_qualifiers;
1856 typedef struct construct_base_type_t construct_base_type_t;
1857 struct construct_base_type_t {
1858 construct_type_t construct_type;
1862 static construct_type_t *parse_pointer_declarator(void)
1866 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1867 memset(pointer, 0, sizeof(pointer[0]));
1868 pointer->construct_type.type = CONSTRUCT_POINTER;
1869 pointer->type_qualifiers = parse_type_qualifiers();
1871 return (construct_type_t*) pointer;
1874 static construct_type_t *parse_array_declarator(void)
1878 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1879 memset(array, 0, sizeof(array[0]));
1880 array->construct_type.type = CONSTRUCT_ARRAY;
1882 if(token.type == T_static) {
1883 array->is_static = true;
1887 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1888 if(type_qualifiers != 0) {
1889 if(token.type == T_static) {
1890 array->is_static = true;
1894 array->type_qualifiers = type_qualifiers;
1896 if(token.type == '*' && look_ahead(1)->type == ']') {
1897 array->is_variable = true;
1899 } else if(token.type != ']') {
1900 array->size = parse_assignment_expression();
1905 return (construct_type_t*) array;
1908 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1912 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1913 type->type.type = TYPE_FUNCTION;
1915 declaration_t *parameters = parse_parameters(type);
1916 if(declaration != NULL) {
1917 declaration->context.declarations = parameters;
1920 construct_function_type_t *construct_function_type =
1921 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1922 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1923 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1924 construct_function_type->function_type = type;
1928 return (construct_type_t*) construct_function_type;
1931 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1932 bool may_be_abstract)
1934 /* construct a single linked list of construct_type_t's which describe
1935 * how to construct the final declarator type */
1936 construct_type_t *first = NULL;
1937 construct_type_t *last = NULL;
1940 while(token.type == '*') {
1941 construct_type_t *type = parse_pointer_declarator();
1952 /* TODO: find out if this is correct */
1955 construct_type_t *inner_types = NULL;
1957 switch(token.type) {
1959 if(declaration == NULL) {
1960 parse_error("no identifier expected in typename");
1962 declaration->symbol = token.v.symbol;
1963 declaration->source_position = token.source_position;
1969 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1975 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1976 /* avoid a loop in the outermost scope, because eat_statement doesn't
1978 if(token.type == '}' && current_function == NULL) {
1986 construct_type_t *p = last;
1989 construct_type_t *type;
1990 switch(token.type) {
1992 type = parse_function_declarator(declaration);
1995 type = parse_array_declarator();
1998 goto declarator_finished;
2001 /* insert in the middle of the list (behind p) */
2003 type->next = p->next;
2014 declarator_finished:
2017 /* append inner_types at the end of the list, we don't to set last anymore
2018 * as it's not needed anymore */
2020 assert(first == NULL);
2021 first = inner_types;
2023 last->next = inner_types;
2029 static type_t *construct_declarator_type(construct_type_t *construct_list,
2032 construct_type_t *iter = construct_list;
2033 for( ; iter != NULL; iter = iter->next) {
2034 parsed_pointer_t *parsed_pointer;
2035 parsed_array_t *parsed_array;
2036 construct_function_type_t *construct_function_type;
2037 function_type_t *function_type;
2038 pointer_type_t *pointer_type;
2039 array_type_t *array_type;
2041 switch(iter->type) {
2042 case CONSTRUCT_INVALID:
2043 panic("invalid type construction found");
2044 case CONSTRUCT_FUNCTION:
2045 construct_function_type = (construct_function_type_t*) iter;
2046 function_type = construct_function_type->function_type;
2048 function_type->result_type = type;
2049 type = (type_t*) function_type;
2052 case CONSTRUCT_POINTER:
2053 parsed_pointer = (parsed_pointer_t*) iter;
2054 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2056 pointer_type->type.type = TYPE_POINTER;
2057 pointer_type->points_to = type;
2058 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2059 type = (type_t*) pointer_type;
2062 case CONSTRUCT_ARRAY:
2063 parsed_array = (parsed_array_t*) iter;
2064 array_type = allocate_type_zero(sizeof(array_type[0]));
2066 array_type->type.type = TYPE_ARRAY;
2067 array_type->element_type = type;
2068 array_type->type.qualifiers = parsed_array->type_qualifiers;
2069 array_type->is_static = parsed_array->is_static;
2070 array_type->is_variable = parsed_array->is_variable;
2071 array_type->size = parsed_array->size;
2072 type = (type_t*) array_type;
2076 type_t *hashed_type = typehash_insert((type_t*) type);
2077 if(hashed_type != type) {
2078 /* the function type was constructed earlier freeing it here will
2079 * destroy other types... */
2080 if(iter->type != CONSTRUCT_FUNCTION) {
2090 static declaration_t *parse_declarator(
2091 const declaration_specifiers_t *specifiers,
2092 type_t *type, bool may_be_abstract)
2094 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2095 declaration->storage_class = specifiers->storage_class;
2096 declaration->is_inline = specifiers->is_inline;
2098 construct_type_t *construct_type
2099 = parse_inner_declarator(declaration, may_be_abstract);
2100 declaration->type = construct_declarator_type(construct_type, type);
2102 if(construct_type != NULL) {
2103 obstack_free(&temp_obst, construct_type);
2109 static type_t *parse_abstract_declarator(type_t *base_type)
2111 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2113 type_t *result = construct_declarator_type(construct_type, base_type);
2114 if(construct_type != NULL) {
2115 obstack_free(&temp_obst, construct_type);
2121 static declaration_t *record_declaration(declaration_t *declaration)
2123 assert(context != NULL);
2125 symbol_t *symbol = declaration->symbol;
2126 if(symbol != NULL) {
2127 declaration_t *alias = environment_push(declaration);
2128 if(alias != declaration)
2131 declaration->parent_context = context;
2134 if(last_declaration != NULL) {
2135 last_declaration->next = declaration;
2137 context->declarations = declaration;
2139 last_declaration = declaration;
2144 static void parser_error_multiple_definition(declaration_t *previous,
2145 declaration_t *declaration)
2147 parser_print_error_prefix_pos(declaration->source_position);
2148 fprintf(stderr, "multiple definition of symbol '%s'\n",
2149 declaration->symbol->string);
2150 parser_print_error_prefix_pos(previous->source_position);
2151 fprintf(stderr, "this is the location of the previous definition.\n");
2154 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2157 declaration_t *ndeclaration
2158 = parse_declarator(specifiers, specifiers->type, false);
2160 declaration_t *declaration = record_declaration(ndeclaration);
2162 type_t *orig_type = declaration->type;
2163 type_t *type = skip_typeref(orig_type);
2164 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2165 parser_print_warning_prefix_pos(declaration->source_position);
2166 fprintf(stderr, "variable '%s' declared 'inline'\n",
2167 declaration->symbol->string);
2170 if(token.type == '=') {
2173 /* TODO: check that this is an allowed type (no function type) */
2175 if(declaration->init.initializer != NULL) {
2176 parser_error_multiple_definition(declaration, ndeclaration);
2179 initializer_t *initializer = parse_initializer(type);
2181 if(type->type == TYPE_ARRAY && initializer != NULL) {
2182 array_type_t *array_type = (array_type_t*) type;
2184 if(array_type->size == NULL) {
2185 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2187 cnst->expression.type = EXPR_CONST;
2188 cnst->expression.datatype = type_size_t;
2190 if(initializer->type == INITIALIZER_LIST) {
2191 initializer_list_t *list
2192 = (initializer_list_t*) initializer;
2193 cnst->v.int_value = list->len;
2195 assert(initializer->type == INITIALIZER_STRING);
2196 initializer_string_t *string
2197 = (initializer_string_t*) initializer;
2198 cnst->v.int_value = strlen(string->string) + 1;
2201 array_type->size = (expression_t*) cnst;
2206 ndeclaration->init.initializer = initializer;
2207 } else if(token.type == '{') {
2208 if(type->type != TYPE_FUNCTION) {
2209 parser_print_error_prefix();
2210 fprintf(stderr, "declarator '");
2211 print_type_ext(orig_type, declaration->symbol, NULL);
2212 fprintf(stderr, "' has a body but is not a function type.\n");
2217 if(declaration->init.statement != NULL) {
2218 parser_error_multiple_definition(declaration, ndeclaration);
2220 if(ndeclaration != declaration) {
2221 memcpy(&declaration->context, &ndeclaration->context,
2222 sizeof(declaration->context));
2225 int top = environment_top();
2226 context_t *last_context = context;
2227 set_context(&declaration->context);
2229 /* push function parameters */
2230 declaration_t *parameter = declaration->context.declarations;
2231 for( ; parameter != NULL; parameter = parameter->next) {
2232 environment_push(parameter);
2235 int label_stack_top = label_top();
2236 declaration_t *old_current_function = current_function;
2237 current_function = declaration;
2239 statement_t *statement = parse_compound_statement();
2241 assert(current_function == declaration);
2242 current_function = old_current_function;
2243 label_pop_to(label_stack_top);
2245 assert(context == &declaration->context);
2246 set_context(last_context);
2247 environment_pop_to(top);
2249 declaration->init.statement = statement;
2253 if(token.type != ',')
2260 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2263 if(token.type == ':') {
2265 parse_constant_expression();
2266 /* TODO (bitfields) */
2268 declaration_t *declaration
2269 = parse_declarator(specifiers, specifiers->type, true);
2271 /* TODO: check constraints for struct declarations */
2272 /* TODO: check for doubled fields */
2273 record_declaration(declaration);
2275 if(token.type == ':') {
2277 parse_constant_expression();
2278 /* TODO (bitfields) */
2282 if(token.type != ',')
2289 static void parse_compound_type_entries(void)
2293 while(token.type != '}' && token.type != T_EOF) {
2294 declaration_specifiers_t specifiers;
2295 memset(&specifiers, 0, sizeof(specifiers));
2296 parse_declaration_specifiers(&specifiers);
2298 parse_struct_declarators(&specifiers);
2300 if(token.type == T_EOF) {
2301 parse_error("unexpected error while parsing struct");
2306 static void parse_declaration(void)
2308 source_position_t source_position = token.source_position;
2310 declaration_specifiers_t specifiers;
2311 memset(&specifiers, 0, sizeof(specifiers));
2312 parse_declaration_specifiers(&specifiers);
2314 if(token.type == ';') {
2315 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2316 parse_warning_pos(source_position,
2317 "useless keyword in empty declaration");
2319 switch (specifiers.type->type) {
2320 case TYPE_COMPOUND_STRUCT:
2321 case TYPE_COMPOUND_UNION: {
2322 const compound_type_t *const comp_type =
2323 (const compound_type_t*)specifiers.type;
2324 if (comp_type->declaration->symbol == NULL) {
2325 parse_warning_pos(source_position,
2326 "unnamed struct/union that defines no instances");
2331 case TYPE_ENUM: break;
2334 parse_warning_pos(source_position, "empty declaration");
2340 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2342 declaration->type = specifiers.type;
2343 declaration->storage_class = specifiers.storage_class;
2344 declaration->source_position = source_position;
2345 record_declaration(declaration);
2348 parse_init_declarators(&specifiers);
2351 static type_t *parse_typename(void)
2353 declaration_specifiers_t specifiers;
2354 memset(&specifiers, 0, sizeof(specifiers));
2355 parse_declaration_specifiers(&specifiers);
2356 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2357 /* TODO: improve error message, user does probably not know what a
2358 * storage class is...
2360 parse_error("typename may not have a storage class");
2363 type_t *result = parse_abstract_declarator(specifiers.type);
2371 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2372 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2373 expression_t *left);
2375 typedef struct expression_parser_function_t expression_parser_function_t;
2376 struct expression_parser_function_t {
2377 unsigned precedence;
2378 parse_expression_function parser;
2379 unsigned infix_precedence;
2380 parse_expression_infix_function infix_parser;
2383 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2385 static expression_t *make_invalid_expression(void)
2387 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2388 expression->type = EXPR_INVALID;
2389 expression->source_position = token.source_position;
2393 static expression_t *expected_expression_error(void)
2395 parser_print_error_prefix();
2396 fprintf(stderr, "expected expression, got token ");
2397 print_token(stderr, & token);
2398 fprintf(stderr, "\n");
2402 return make_invalid_expression();
2405 static expression_t *parse_string_const(void)
2407 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2409 cnst->expression.type = EXPR_STRING_LITERAL;
2410 cnst->expression.datatype = type_string;
2411 cnst->value = parse_string_literals();
2413 return (expression_t*) cnst;
2416 static expression_t *parse_int_const(void)
2418 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2420 cnst->expression.type = EXPR_CONST;
2421 cnst->expression.datatype = token.datatype;
2422 cnst->v.int_value = token.v.intvalue;
2426 return (expression_t*) cnst;
2429 static expression_t *parse_float_const(void)
2431 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2433 cnst->expression.type = EXPR_CONST;
2434 cnst->expression.datatype = token.datatype;
2435 cnst->v.float_value = token.v.floatvalue;
2439 return (expression_t*) cnst;
2442 static declaration_t *create_implicit_function(symbol_t *symbol,
2443 const source_position_t source_position)
2445 function_type_t *function_type
2446 = allocate_type_zero(sizeof(function_type[0]));
2448 function_type->type.type = TYPE_FUNCTION;
2449 function_type->result_type = type_int;
2450 function_type->unspecified_parameters = true;
2452 type_t *type = typehash_insert((type_t*) function_type);
2453 if(type != (type_t*) function_type) {
2454 free_type(function_type);
2457 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2459 declaration->storage_class = STORAGE_CLASS_EXTERN;
2460 declaration->type = type;
2461 declaration->symbol = symbol;
2462 declaration->source_position = source_position;
2464 /* prepend the implicit definition to the global context
2465 * this is safe since the symbol wasn't declared as anything else yet
2467 assert(symbol->declaration == NULL);
2469 context_t *last_context = context;
2470 context = global_context;
2472 environment_push(declaration);
2473 declaration->next = context->declarations;
2474 context->declarations = declaration;
2476 context = last_context;
2481 static expression_t *parse_reference(void)
2483 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2485 ref->expression.type = EXPR_REFERENCE;
2486 ref->symbol = token.v.symbol;
2488 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2490 source_position_t source_position = token.source_position;
2493 if(declaration == NULL) {
2495 /* an implicitly defined function */
2496 if(token.type == '(') {
2497 parser_print_prefix_pos(token.source_position);
2498 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2499 ref->symbol->string);
2501 declaration = create_implicit_function(ref->symbol,
2506 parser_print_error_prefix();
2507 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2508 return (expression_t*) ref;
2512 ref->declaration = declaration;
2513 ref->expression.datatype = declaration->type;
2515 return (expression_t*) ref;
2518 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2522 /* TODO check if explicit cast is allowed and issue warnings/errors */
2525 static expression_t *parse_cast(void)
2527 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2529 cast->expression.type = EXPR_UNARY;
2530 cast->type = UNEXPR_CAST;
2531 cast->expression.source_position = token.source_position;
2533 type_t *type = parse_typename();
2536 expression_t *value = parse_sub_expression(20);
2538 check_cast_allowed(value, type);
2540 cast->expression.datatype = type;
2541 cast->value = value;
2543 return (expression_t*) cast;
2546 static expression_t *parse_statement_expression(void)
2548 statement_expression_t *expression
2549 = allocate_ast_zero(sizeof(expression[0]));
2550 expression->expression.type = EXPR_STATEMENT;
2552 statement_t *statement = parse_compound_statement();
2553 expression->statement = statement;
2554 if(statement == NULL) {
2559 assert(statement->type == STATEMENT_COMPOUND);
2560 compound_statement_t *compound_statement
2561 = (compound_statement_t*) statement;
2563 /* find last statement and use it's type */
2564 const statement_t *last_statement = NULL;
2565 const statement_t *iter = compound_statement->statements;
2566 for( ; iter != NULL; iter = iter->next) {
2567 last_statement = iter;
2570 if(last_statement->type == STATEMENT_EXPRESSION) {
2571 const expression_statement_t *expression_statement =
2572 (const expression_statement_t*) last_statement;
2573 expression->expression.datatype
2574 = expression_statement->expression->datatype;
2576 expression->expression.datatype = type_void;
2581 return (expression_t*) expression;
2584 static expression_t *parse_brace_expression(void)
2588 switch(token.type) {
2590 /* gcc extension: a stement expression */
2591 return parse_statement_expression();
2595 return parse_cast();
2597 if(is_typedef_symbol(token.v.symbol)) {
2598 return parse_cast();
2602 expression_t *result = parse_expression();
2608 static expression_t *parse_function_keyword(void)
2613 if (current_function == NULL) {
2614 parse_error("'__func__' used outside of a function");
2617 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2618 expression->expression.type = EXPR_FUNCTION;
2619 expression->expression.datatype = type_string;
2620 expression->value = "TODO: FUNCTION";
2622 return (expression_t*) expression;
2625 static expression_t *parse_pretty_function_keyword(void)
2627 eat(T___PRETTY_FUNCTION__);
2630 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2631 expression->expression.type = EXPR_PRETTY_FUNCTION;
2632 expression->expression.datatype = type_string;
2633 expression->value = "TODO: PRETTY FUNCTION";
2635 return (expression_t*) expression;
2638 static designator_t *parse_designator(void)
2640 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2642 if(token.type != T_IDENTIFIER) {
2643 parse_error_expected("while parsing member designator",
2648 result->symbol = token.v.symbol;
2651 designator_t *last_designator = result;
2653 if(token.type == '.') {
2655 if(token.type != T_IDENTIFIER) {
2656 parse_error_expected("while parsing member designator",
2661 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2662 designator->symbol = token.v.symbol;
2665 last_designator->next = designator;
2666 last_designator = designator;
2669 if(token.type == '[') {
2671 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2672 designator->array_access = parse_expression();
2673 if(designator->array_access == NULL) {
2679 last_designator->next = designator;
2680 last_designator = designator;
2689 static expression_t *parse_offsetof(void)
2691 eat(T___builtin_offsetof);
2693 offsetof_expression_t *expression
2694 = allocate_ast_zero(sizeof(expression[0]));
2695 expression->expression.type = EXPR_OFFSETOF;
2696 expression->expression.datatype = type_size_t;
2699 expression->type = parse_typename();
2701 expression->designator = parse_designator();
2704 return (expression_t*) expression;
2707 static expression_t *parse_va_arg(void)
2709 eat(T___builtin_va_arg);
2711 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2712 expression->expression.type = EXPR_VA_ARG;
2715 expression->arg = parse_assignment_expression();
2717 expression->expression.datatype = parse_typename();
2720 return (expression_t*) expression;
2723 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2725 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2726 parameter->type = argument_type;
2728 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2729 type->type.type = TYPE_FUNCTION;
2730 type->result_type = result_type;
2731 type->parameters = parameter;
2733 type_t *result = typehash_insert((type_t*) type);
2734 if(result != (type_t*) type) {
2741 static expression_t *parse_builtin_symbol(void)
2743 builtin_symbol_expression_t *expression
2744 = allocate_ast_zero(sizeof(expression[0]));
2745 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2747 expression->symbol = token.v.symbol;
2750 switch(token.type) {
2751 case T___builtin_alloca:
2752 type = make_function_1_type(type_void_ptr, type_size_t);
2758 expression->expression.datatype = type;
2759 return (expression_t*) expression;
2762 static expression_t *parse_primary_expression(void)
2764 switch(token.type) {
2766 return parse_int_const();
2767 case T_FLOATINGPOINT:
2768 return parse_float_const();
2769 case T_STRING_LITERAL:
2770 return parse_string_const();
2772 return parse_reference();
2773 case T___FUNCTION__:
2775 return parse_function_keyword();
2776 case T___PRETTY_FUNCTION__:
2777 return parse_pretty_function_keyword();
2778 case T___builtin_offsetof:
2779 return parse_offsetof();
2780 case T___builtin_va_arg:
2781 return parse_va_arg();
2782 case T___builtin_alloca:
2783 case T___builtin_expect:
2784 case T___builtin_va_start:
2785 case T___builtin_va_end:
2786 return parse_builtin_symbol();
2789 return parse_brace_expression();
2792 parser_print_error_prefix();
2793 fprintf(stderr, "unexpected token ");
2794 print_token(stderr, &token);
2795 fprintf(stderr, "\n");
2798 return make_invalid_expression();
2801 static expression_t *parse_array_expression(unsigned precedence,
2802 expression_t *array_ref)
2808 expression_t *index = parse_expression();
2810 array_access_expression_t *array_access
2811 = allocate_ast_zero(sizeof(array_access[0]));
2813 array_access->expression.type = EXPR_ARRAY_ACCESS;
2814 array_access->array_ref = array_ref;
2815 array_access->index = index;
2817 type_t *type_left = skip_typeref(array_ref->datatype);
2818 type_t *type_right = skip_typeref(index->datatype);
2820 if(type_left != NULL && type_right != NULL) {
2821 if(type_left->type == TYPE_POINTER) {
2822 pointer_type_t *pointer = (pointer_type_t*) type_left;
2823 array_access->expression.datatype = pointer->points_to;
2824 } else if(type_left->type == TYPE_ARRAY) {
2825 array_type_t *array_type = (array_type_t*) type_left;
2826 array_access->expression.datatype = array_type->element_type;
2827 } else if(type_right->type == TYPE_POINTER) {
2828 pointer_type_t *pointer = (pointer_type_t*) type_right;
2829 array_access->expression.datatype = pointer->points_to;
2830 } else if(type_right->type == TYPE_ARRAY) {
2831 array_type_t *array_type = (array_type_t*) type_right;
2832 array_access->expression.datatype = array_type->element_type;
2834 parser_print_error_prefix();
2835 fprintf(stderr, "array access on object with non-pointer types ");
2836 print_type_quoted(type_left);
2837 fprintf(stderr, ", ");
2838 print_type_quoted(type_right);
2839 fprintf(stderr, "\n");
2843 if(token.type != ']') {
2844 parse_error_expected("Problem while parsing array access", ']', 0);
2845 return (expression_t*) array_access;
2849 return (expression_t*) array_access;
2852 static bool is_declaration_specifier(const token_t *token,
2853 bool only_type_specifiers)
2855 switch(token->type) {
2859 return is_typedef_symbol(token->v.symbol);
2862 if(only_type_specifiers)
2871 static expression_t *parse_sizeof(unsigned precedence)
2875 sizeof_expression_t *sizeof_expression
2876 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2877 sizeof_expression->expression.type = EXPR_SIZEOF;
2878 sizeof_expression->expression.datatype = type_size_t;
2880 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2882 sizeof_expression->type = parse_typename();
2885 expression_t *expression = parse_sub_expression(precedence);
2886 sizeof_expression->type = expression->datatype;
2887 sizeof_expression->size_expression = expression;
2890 return (expression_t*) sizeof_expression;
2893 static expression_t *parse_select_expression(unsigned precedence,
2894 expression_t *compound)
2897 assert(token.type == '.' || token.type == T_MINUSGREATER);
2899 bool is_pointer = (token.type == T_MINUSGREATER);
2902 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2904 select->expression.type = EXPR_SELECT;
2905 select->compound = compound;
2907 if(token.type != T_IDENTIFIER) {
2908 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2909 return (expression_t*) select;
2911 symbol_t *symbol = token.v.symbol;
2912 select->symbol = symbol;
2915 type_t *orig_type = compound->datatype;
2916 if(orig_type == NULL)
2917 return make_invalid_expression();
2919 type_t *type = skip_typeref(orig_type);
2921 type_t *type_left = type;
2923 if(type->type != TYPE_POINTER) {
2924 parser_print_error_prefix();
2925 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2926 print_type_quoted(orig_type);
2927 fputc('\n', stderr);
2928 return make_invalid_expression();
2930 pointer_type_t *pointer_type = (pointer_type_t*) type;
2931 type_left = pointer_type->points_to;
2933 type_left = skip_typeref(type_left);
2935 if(type_left->type != TYPE_COMPOUND_STRUCT
2936 && type_left->type != TYPE_COMPOUND_UNION) {
2937 parser_print_error_prefix();
2938 fprintf(stderr, "request for member '%s' in something not a struct or "
2939 "union, but ", symbol->string);
2940 print_type_quoted(type_left);
2941 fputc('\n', stderr);
2942 return make_invalid_expression();
2945 compound_type_t *compound_type = (compound_type_t*) type_left;
2946 declaration_t *declaration = compound_type->declaration;
2948 if(!declaration->init.is_defined) {
2949 parser_print_error_prefix();
2950 fprintf(stderr, "request for member '%s' of incomplete type ",
2952 print_type_quoted(type_left);
2953 fputc('\n', stderr);
2954 return make_invalid_expression();
2957 declaration_t *iter = declaration->context.declarations;
2958 for( ; iter != NULL; iter = iter->next) {
2959 if(iter->symbol == symbol) {
2964 parser_print_error_prefix();
2965 print_type_quoted(type_left);
2966 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2967 return make_invalid_expression();
2970 select->compound_entry = iter;
2971 select->expression.datatype = iter->type;
2972 return (expression_t*) select;
2975 static expression_t *parse_call_expression(unsigned precedence,
2976 expression_t *expression)
2979 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2980 call->expression.type = EXPR_CALL;
2981 call->function = expression;
2983 function_type_t *function_type;
2984 type_t *orig_type = expression->datatype;
2985 type_t *type = skip_typeref(orig_type);
2987 if(type->type == TYPE_POINTER) {
2988 pointer_type_t *pointer_type = (pointer_type_t*) type;
2990 type = skip_typeref(pointer_type->points_to);
2992 if (type->type == TYPE_FUNCTION) {
2993 function_type = (function_type_t*) type;
2994 call->expression.datatype = function_type->result_type;
2996 parser_print_error_prefix();
2997 fputs("called object '", stderr);
2998 print_expression(expression);
2999 fputs("' (type ", stderr);
3000 print_type_quoted(orig_type);
3001 fputs(") is not a function\n", stderr);
3003 function_type = NULL;
3004 call->expression.datatype = NULL;
3007 /* parse arguments */
3010 if(token.type != ')') {
3011 call_argument_t *last_argument = NULL;
3014 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3016 argument->expression = parse_assignment_expression();
3017 if(last_argument == NULL) {
3018 call->arguments = argument;
3020 last_argument->next = argument;
3022 last_argument = argument;
3024 if(token.type != ',')
3031 if(function_type != NULL) {
3032 function_parameter_t *parameter = function_type->parameters;
3033 call_argument_t *argument = call->arguments;
3034 for( ; parameter != NULL && argument != NULL;
3035 parameter = parameter->next, argument = argument->next) {
3036 type_t *expected_type = parameter->type;
3037 /* TODO report context in error messages */
3038 argument->expression = create_implicit_cast(argument->expression,
3041 /* too few parameters */
3042 if(parameter != NULL) {
3043 parser_print_error_prefix();
3044 fprintf(stderr, "too few arguments to function '");
3045 print_expression(expression);
3046 fprintf(stderr, "'\n");
3047 } else if(argument != NULL) {
3048 /* too many parameters */
3049 if(!function_type->variadic
3050 && !function_type->unspecified_parameters) {
3051 parser_print_error_prefix();
3052 fprintf(stderr, "too many arguments to function '");
3053 print_expression(expression);
3054 fprintf(stderr, "'\n");
3056 /* do default promotion */
3057 for( ; argument != NULL; argument = argument->next) {
3058 type_t *type = argument->expression->datatype;
3063 if(is_type_integer(type)) {
3064 type = promote_integer(type);
3065 } else if(type == type_float) {
3068 argument->expression
3069 = create_implicit_cast(argument->expression, type);
3075 return (expression_t*) call;
3078 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3080 static expression_t *parse_conditional_expression(unsigned precedence,
3081 expression_t *expression)
3085 conditional_expression_t *conditional
3086 = allocate_ast_zero(sizeof(conditional[0]));
3087 conditional->expression.type = EXPR_CONDITIONAL;
3088 conditional->condition = expression;
3091 type_t *condition_type_orig = conditional->condition->datatype;
3092 if(condition_type_orig != NULL) {
3093 type_t *condition_type = skip_typeref(condition_type_orig);
3094 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3095 type_error("expected a scalar type", expression->source_position,
3096 condition_type_orig);
3100 expression_t *const t_expr = parse_expression();
3101 conditional->true_expression = t_expr;
3103 expression_t *const f_expr = parse_sub_expression(precedence);
3104 conditional->false_expression = f_expr;
3106 type_t *const true_type = t_expr->datatype;
3107 if(true_type == NULL)
3108 return (expression_t*) conditional;
3109 type_t *const false_type = f_expr->datatype;
3110 if(false_type == NULL)
3111 return (expression_t*) conditional;
3113 type_t *const skipped_true_type = skip_typeref(true_type);
3114 type_t *const skipped_false_type = skip_typeref(false_type);
3117 if (skipped_true_type == skipped_false_type) {
3118 conditional->expression.datatype = skipped_true_type;
3119 } else if (is_type_arithmetic(skipped_true_type) &&
3120 is_type_arithmetic(skipped_false_type)) {
3121 type_t *const result = semantic_arithmetic(skipped_true_type,
3122 skipped_false_type);
3123 conditional->true_expression = create_implicit_cast(t_expr, result);
3124 conditional->false_expression = create_implicit_cast(f_expr, result);
3125 conditional->expression.datatype = result;
3126 } else if (skipped_true_type->type == TYPE_POINTER &&
3127 skipped_false_type->type == TYPE_POINTER &&
3128 true /* TODO compatible points_to types */) {
3130 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3131 skipped_false_type->type == TYPE_POINTER)
3132 || (is_null_ptr_const(skipped_false_type) &&
3133 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3135 } else if(/* 1 is pointer to object type, other is void* */ false) {
3138 type_error_incompatible("while parsing conditional",
3139 expression->source_position, true_type,
3140 skipped_false_type);
3143 return (expression_t*) conditional;
3146 static expression_t *parse_extension(unsigned precedence)
3148 eat(T___extension__);
3150 /* TODO enable extensions */
3152 return parse_sub_expression(precedence);
3155 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3157 eat(T___builtin_classify_type);
3159 classify_type_expression_t *const classify_type_expr =
3160 allocate_ast_zero(sizeof(classify_type_expr[0]));
3161 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3162 classify_type_expr->expression.datatype = type_int;
3165 expression_t *const expression = parse_sub_expression(precedence);
3167 classify_type_expr->type_expression = expression;
3169 return (expression_t*)classify_type_expr;
3172 static void semantic_incdec(unary_expression_t *expression)
3174 type_t *orig_type = expression->value->datatype;
3175 if(orig_type == NULL)
3178 type_t *type = skip_typeref(orig_type);
3179 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3180 /* TODO: improve error message */
3181 parser_print_error_prefix();
3182 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3186 expression->expression.datatype = orig_type;
3189 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3191 type_t *orig_type = expression->value->datatype;
3192 if(orig_type == NULL)
3195 type_t *type = skip_typeref(orig_type);
3196 if(!is_type_arithmetic(type)) {
3197 /* TODO: improve error message */
3198 parser_print_error_prefix();
3199 fprintf(stderr, "operation needs an arithmetic type\n");
3203 expression->expression.datatype = orig_type;
3206 static void semantic_unexpr_scalar(unary_expression_t *expression)
3208 type_t *orig_type = expression->value->datatype;
3209 if(orig_type == NULL)
3212 type_t *type = skip_typeref(orig_type);
3213 if (!is_type_scalar(type)) {
3214 parse_error("operand of ! must be of scalar type\n");
3218 expression->expression.datatype = orig_type;
3221 static void semantic_unexpr_integer(unary_expression_t *expression)
3223 type_t *orig_type = expression->value->datatype;
3224 if(orig_type == NULL)
3227 type_t *type = skip_typeref(orig_type);
3228 if (!is_type_integer(type)) {
3229 parse_error("operand of ~ must be of integer type\n");
3233 expression->expression.datatype = orig_type;
3236 static void semantic_dereference(unary_expression_t *expression)
3238 type_t *orig_type = expression->value->datatype;
3239 if(orig_type == NULL)
3242 type_t *type = skip_typeref(orig_type);
3243 switch (type->type) {
3245 array_type_t *const array_type = (array_type_t*)type;
3246 expression->expression.datatype = array_type->element_type;
3250 case TYPE_POINTER: {
3251 pointer_type_t *pointer_type = (pointer_type_t*)type;
3252 expression->expression.datatype = pointer_type->points_to;
3257 parser_print_error_prefix();
3258 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3259 print_type_quoted(orig_type);
3260 fputs(" given.\n", stderr);
3265 static void semantic_take_addr(unary_expression_t *expression)
3267 type_t *orig_type = expression->value->datatype;
3268 if(orig_type == NULL)
3271 expression_t *value = expression->value;
3272 if(value->type == EXPR_REFERENCE) {
3273 reference_expression_t *reference = (reference_expression_t*) value;
3274 declaration_t *declaration = reference->declaration;
3275 if(declaration != NULL) {
3276 declaration->address_taken = 1;
3280 expression->expression.datatype = make_pointer_type(orig_type, 0);
3283 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3284 static expression_t *parse_##unexpression_type(unsigned precedence) \
3288 unary_expression_t *unary_expression \
3289 = allocate_ast_zero(sizeof(unary_expression[0])); \
3290 unary_expression->expression.type = EXPR_UNARY; \
3291 unary_expression->type = unexpression_type; \
3292 unary_expression->value = parse_sub_expression(precedence); \
3294 sfunc(unary_expression); \
3296 return (expression_t*) unary_expression; \
3299 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3300 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3301 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3302 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3303 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3304 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3305 semantic_unexpr_integer)
3306 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3308 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3311 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3313 static expression_t *parse_##unexpression_type(unsigned precedence, \
3314 expression_t *left) \
3316 (void) precedence; \
3319 unary_expression_t *unary_expression \
3320 = allocate_ast_zero(sizeof(unary_expression[0])); \
3321 unary_expression->expression.type = EXPR_UNARY; \
3322 unary_expression->type = unexpression_type; \
3323 unary_expression->value = left; \
3325 sfunc(unary_expression); \
3327 return (expression_t*) unary_expression; \
3330 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3332 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3335 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3337 /* TODO: handle complex + imaginary types */
3339 /* § 6.3.1.8 Usual arithmetic conversions */
3340 if(type_left == type_long_double || type_right == type_long_double) {
3341 return type_long_double;
3342 } else if(type_left == type_double || type_right == type_double) {
3344 } else if(type_left == type_float || type_right == type_float) {
3348 type_right = promote_integer(type_right);
3349 type_left = promote_integer(type_left);
3351 if(type_left == type_right)
3354 bool signed_left = is_type_signed(type_left);
3355 bool signed_right = is_type_signed(type_right);
3356 if(get_rank(type_left) < get_rank(type_right)) {
3357 if(signed_left == signed_right || !signed_right) {
3363 if(signed_left == signed_right || !signed_left) {
3371 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3373 expression_t *left = expression->left;
3374 expression_t *right = expression->right;
3375 type_t *orig_type_left = left->datatype;
3376 type_t *orig_type_right = right->datatype;
3378 if(orig_type_left == NULL || orig_type_right == NULL)
3381 type_t *type_left = skip_typeref(orig_type_left);
3382 type_t *type_right = skip_typeref(orig_type_right);
3384 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3385 /* TODO: improve error message */
3386 parser_print_error_prefix();
3387 fprintf(stderr, "operation needs arithmetic types\n");
3391 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3392 expression->left = create_implicit_cast(left, arithmetic_type);
3393 expression->right = create_implicit_cast(right, arithmetic_type);
3394 expression->expression.datatype = arithmetic_type;
3397 static void semantic_shift_op(binary_expression_t *expression)
3399 expression_t *left = expression->left;
3400 expression_t *right = expression->right;
3401 type_t *orig_type_left = left->datatype;
3402 type_t *orig_type_right = right->datatype;
3404 if(orig_type_left == NULL || orig_type_right == NULL)
3407 type_t *type_left = skip_typeref(orig_type_left);
3408 type_t *type_right = skip_typeref(orig_type_right);
3410 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3411 /* TODO: improve error message */
3412 parser_print_error_prefix();
3413 fprintf(stderr, "operation needs integer types\n");
3417 type_left = promote_integer(type_left);
3418 type_right = promote_integer(type_right);
3420 expression->left = create_implicit_cast(left, type_left);
3421 expression->right = create_implicit_cast(right, type_right);
3422 expression->expression.datatype = type_left;
3425 static void semantic_add(binary_expression_t *expression)
3427 expression_t *left = expression->left;
3428 expression_t *right = expression->right;
3429 type_t *orig_type_left = left->datatype;
3430 type_t *orig_type_right = right->datatype;
3432 if(orig_type_left == NULL || orig_type_right == NULL)
3435 type_t *type_left = skip_typeref(orig_type_left);
3436 type_t *type_right = skip_typeref(orig_type_right);
3439 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3440 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3441 expression->left = create_implicit_cast(left, arithmetic_type);
3442 expression->right = create_implicit_cast(right, arithmetic_type);
3443 expression->expression.datatype = arithmetic_type;
3445 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3446 expression->expression.datatype = type_left;
3447 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3448 expression->expression.datatype = type_right;
3449 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3450 const array_type_t *const arr_type = (const array_type_t*)type_left;
3451 expression->expression.datatype =
3452 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3453 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3454 const array_type_t *const arr_type = (const array_type_t*)type_right;
3455 expression->expression.datatype =
3456 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3458 parser_print_error_prefix();
3459 fprintf(stderr, "invalid operands to binary + (");
3460 print_type_quoted(orig_type_left);
3461 fprintf(stderr, ", ");
3462 print_type_quoted(orig_type_right);
3463 fprintf(stderr, ")\n");
3467 static void semantic_sub(binary_expression_t *expression)
3469 expression_t *left = expression->left;
3470 expression_t *right = expression->right;
3471 type_t *orig_type_left = left->datatype;
3472 type_t *orig_type_right = right->datatype;
3474 if(orig_type_left == NULL || orig_type_right == NULL)
3477 type_t *type_left = skip_typeref(orig_type_left);
3478 type_t *type_right = skip_typeref(orig_type_right);
3481 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3482 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3483 expression->left = create_implicit_cast(left, arithmetic_type);
3484 expression->right = create_implicit_cast(right, arithmetic_type);
3485 expression->expression.datatype = arithmetic_type;
3487 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3488 expression->expression.datatype = type_left;
3489 } else if(type_left->type == TYPE_POINTER &&
3490 type_right->type == TYPE_POINTER) {
3491 if(!pointers_compatible(type_left, type_right)) {
3492 parser_print_error_prefix();
3493 fprintf(stderr, "pointers to incompatible objects to binary - (");
3494 print_type_quoted(orig_type_left);
3495 fprintf(stderr, ", ");
3496 print_type_quoted(orig_type_right);
3497 fprintf(stderr, ")\n");
3499 expression->expression.datatype = type_ptrdiff_t;
3502 parser_print_error_prefix();
3503 fprintf(stderr, "invalid operands to binary - (");
3504 print_type_quoted(orig_type_left);
3505 fprintf(stderr, ", ");
3506 print_type_quoted(orig_type_right);
3507 fprintf(stderr, ")\n");
3511 static void semantic_comparison(binary_expression_t *expression)
3513 expression_t *left = expression->left;
3514 expression_t *right = expression->right;
3515 type_t *orig_type_left = left->datatype;
3516 type_t *orig_type_right = right->datatype;
3518 if(orig_type_left == NULL || orig_type_right == NULL)
3521 type_t *type_left = skip_typeref(orig_type_left);
3522 type_t *type_right = skip_typeref(orig_type_right);
3524 /* TODO non-arithmetic types */
3525 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3526 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3527 expression->left = create_implicit_cast(left, arithmetic_type);
3528 expression->right = create_implicit_cast(right, arithmetic_type);
3529 expression->expression.datatype = arithmetic_type;
3530 } else if (type_left->type == TYPE_POINTER &&
3531 type_right->type == TYPE_POINTER) {
3532 /* TODO check compatibility */
3533 } else if (type_left->type == TYPE_POINTER) {
3534 expression->right = create_implicit_cast(right, type_left);
3535 } else if (type_right->type == TYPE_POINTER) {
3536 expression->left = create_implicit_cast(left, type_right);
3538 type_error_incompatible("invalid operands in comparison",
3539 token.source_position, type_left, type_right);
3541 expression->expression.datatype = type_int;
3544 static void semantic_arithmetic_assign(binary_expression_t *expression)
3546 expression_t *left = expression->left;
3547 expression_t *right = expression->right;
3548 type_t *orig_type_left = left->datatype;
3549 type_t *orig_type_right = right->datatype;
3551 if(orig_type_left == NULL || orig_type_right == NULL)
3554 type_t *type_left = skip_typeref(orig_type_left);
3555 type_t *type_right = skip_typeref(orig_type_right);
3557 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3558 /* TODO: improve error message */
3559 parser_print_error_prefix();
3560 fprintf(stderr, "operation needs arithmetic types\n");
3564 /* combined instructions are tricky. We can't create an implicit cast on
3565 * the left side, because we need the uncasted form for the store.
3566 * The ast2firm pass has to know that left_type must be right_type
3567 * for the arithmeitc operation and create a cast by itself */
3568 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3569 expression->right = create_implicit_cast(right, arithmetic_type);
3570 expression->expression.datatype = type_left;
3573 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3575 expression_t *left = expression->left;
3576 expression_t *right = expression->right;
3577 type_t *orig_type_left = left->datatype;
3578 type_t *orig_type_right = right->datatype;
3580 if(orig_type_left == NULL || orig_type_right == NULL)
3583 type_t *type_left = skip_typeref(orig_type_left);
3584 type_t *type_right = skip_typeref(orig_type_right);
3586 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3587 /* combined instructions are tricky. We can't create an implicit cast on
3588 * the left side, because we need the uncasted form for the store.
3589 * The ast2firm pass has to know that left_type must be right_type
3590 * for the arithmeitc operation and create a cast by itself */
3591 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3592 expression->right = create_implicit_cast(right, arithmetic_type);
3593 expression->expression.datatype = type_left;
3594 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3595 expression->expression.datatype = type_left;
3597 parser_print_error_prefix();
3598 fputs("Incompatible types ", stderr);
3599 print_type_quoted(orig_type_left);
3600 fputs(" and ", stderr);
3601 print_type_quoted(orig_type_right);
3602 fputs(" in assignment\n", stderr);
3607 static void semantic_logical_op(binary_expression_t *expression)
3609 expression_t *left = expression->left;
3610 expression_t *right = expression->right;
3611 type_t *orig_type_left = left->datatype;
3612 type_t *orig_type_right = right->datatype;
3614 if(orig_type_left == NULL || orig_type_right == NULL)
3617 type_t *type_left = skip_typeref(orig_type_left);
3618 type_t *type_right = skip_typeref(orig_type_right);
3620 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3621 /* TODO: improve error message */
3622 parser_print_error_prefix();
3623 fprintf(stderr, "operation needs scalar types\n");
3627 expression->expression.datatype = type_int;
3630 static void semantic_binexpr_assign(binary_expression_t *expression)
3632 expression_t *left = expression->left;
3633 type_t *orig_type_left = left->datatype;
3635 if(orig_type_left == NULL)
3638 type_t *type_left = skip_typeref(orig_type_left);
3640 if (type_left->type == TYPE_ARRAY) {
3641 parse_error("Cannot assign to arrays.");
3645 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3646 parser_print_error_prefix();
3647 fprintf(stderr, "assignment to readonly location '");
3648 print_expression(left);
3649 fprintf(stderr, "' (type ");
3650 print_type_quoted(orig_type_left);
3651 fprintf(stderr, ")\n");
3654 semantic_assign(orig_type_left, &expression->right, "assignment");
3656 expression->expression.datatype = orig_type_left;
3659 static void semantic_comma(binary_expression_t *expression)
3661 expression->expression.datatype = expression->right->datatype;
3664 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3665 static expression_t *parse_##binexpression_type(unsigned precedence, \
3666 expression_t *left) \
3670 expression_t *right = parse_sub_expression(precedence + lr); \
3672 binary_expression_t *binexpr \
3673 = allocate_ast_zero(sizeof(binexpr[0])); \
3674 binexpr->expression.type = EXPR_BINARY; \
3675 binexpr->type = binexpression_type; \
3676 binexpr->left = left; \
3677 binexpr->right = right; \
3680 return (expression_t*) binexpr; \
3683 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3684 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3685 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3686 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3687 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3688 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3689 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3690 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3691 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3692 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3693 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3694 semantic_comparison, 1)
3695 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3696 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3697 semantic_comparison, 1)
3698 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3699 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3700 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3701 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3702 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3703 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3704 semantic_shift_op, 1)
3705 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3706 semantic_shift_op, 1)
3707 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3708 semantic_arithmetic_addsubb_assign, 0)
3709 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3710 semantic_arithmetic_addsubb_assign, 0)
3711 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3712 semantic_arithmetic_assign, 0)
3713 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3714 semantic_arithmetic_assign, 0)
3715 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3716 semantic_arithmetic_assign, 0)
3717 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3718 semantic_arithmetic_assign, 0)
3719 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3720 semantic_arithmetic_assign, 0)
3721 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3722 semantic_arithmetic_assign, 0)
3723 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3724 semantic_arithmetic_assign, 0)
3725 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3726 semantic_arithmetic_assign, 0)
3728 static expression_t *parse_sub_expression(unsigned precedence)
3730 if(token.type < 0) {
3731 return expected_expression_error();
3734 expression_parser_function_t *parser
3735 = &expression_parsers[token.type];
3736 source_position_t source_position = token.source_position;
3739 if(parser->parser != NULL) {
3740 left = parser->parser(parser->precedence);
3742 left = parse_primary_expression();
3744 assert(left != NULL);
3745 left->source_position = source_position;
3748 if(token.type < 0) {
3749 return expected_expression_error();
3752 parser = &expression_parsers[token.type];
3753 if(parser->infix_parser == NULL)
3755 if(parser->infix_precedence < precedence)
3758 left = parser->infix_parser(parser->infix_precedence, left);
3760 assert(left != NULL);
3761 assert(left->type != EXPR_UNKNOWN);
3762 left->source_position = source_position;
3768 static expression_t *parse_expression(void)
3770 return parse_sub_expression(1);
3775 static void register_expression_parser(parse_expression_function parser,
3776 int token_type, unsigned precedence)
3778 expression_parser_function_t *entry = &expression_parsers[token_type];
3780 if(entry->parser != NULL) {
3781 fprintf(stderr, "for token ");
3782 print_token_type(stderr, token_type);
3783 fprintf(stderr, "\n");
3784 panic("trying to register multiple expression parsers for a token");
3786 entry->parser = parser;
3787 entry->precedence = precedence;
3790 static void register_expression_infix_parser(
3791 parse_expression_infix_function parser, int token_type,
3792 unsigned precedence)
3794 expression_parser_function_t *entry = &expression_parsers[token_type];
3796 if(entry->infix_parser != NULL) {
3797 fprintf(stderr, "for token ");
3798 print_token_type(stderr, token_type);
3799 fprintf(stderr, "\n");
3800 panic("trying to register multiple infix expression parsers for a "
3803 entry->infix_parser = parser;
3804 entry->infix_precedence = precedence;
3807 static void init_expression_parsers(void)
3809 memset(&expression_parsers, 0, sizeof(expression_parsers));
3811 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3812 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3813 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3814 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3815 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3816 T_GREATERGREATER, 16);
3817 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3818 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3819 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3820 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3821 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3822 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3823 T_GREATEREQUAL, 14);
3824 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3825 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3826 T_EXCLAMATIONMARKEQUAL, 13);
3827 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3828 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3829 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3830 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3831 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3832 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3833 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3834 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3835 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3836 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3837 T_ASTERISKEQUAL, 2);
3838 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3839 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3841 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3842 T_LESSLESSEQUAL, 2);
3843 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3844 T_GREATERGREATEREQUAL, 2);
3845 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3847 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3849 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3852 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3854 register_expression_infix_parser(parse_array_expression, '[', 30);
3855 register_expression_infix_parser(parse_call_expression, '(', 30);
3856 register_expression_infix_parser(parse_select_expression, '.', 30);
3857 register_expression_infix_parser(parse_select_expression,
3858 T_MINUSGREATER, 30);
3859 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3861 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3864 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3865 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3866 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3867 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3868 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3869 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3870 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3871 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3872 register_expression_parser(parse_sizeof, T_sizeof, 25);
3873 register_expression_parser(parse_extension, T___extension__, 25);
3874 register_expression_parser(parse_builtin_classify_type,
3875 T___builtin_classify_type, 25);
3879 static statement_t *parse_case_statement(void)
3882 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3883 label->statement.type = STATEMENT_CASE_LABEL;
3884 label->statement.source_position = token.source_position;
3886 label->expression = parse_expression();
3889 label->label_statement = parse_statement();
3891 return (statement_t*) label;
3894 static statement_t *parse_default_statement(void)
3898 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3899 label->statement.type = STATEMENT_CASE_LABEL;
3900 label->statement.source_position = token.source_position;
3903 label->label_statement = parse_statement();
3905 return (statement_t*) label;
3908 static declaration_t *get_label(symbol_t *symbol)
3910 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3911 assert(current_function != NULL);
3912 /* if we found a label in the same function, then we already created the
3914 if(candidate != NULL
3915 && candidate->parent_context == ¤t_function->context) {
3919 /* otherwise we need to create a new one */
3920 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3921 declaration->namespc = NAMESPACE_LABEL;
3922 declaration->symbol = symbol;
3924 label_push(declaration);
3929 static statement_t *parse_label_statement(void)
3931 assert(token.type == T_IDENTIFIER);
3932 symbol_t *symbol = token.v.symbol;
3935 declaration_t *label = get_label(symbol);
3937 /* if source position is already set then the label is defined twice,
3938 * otherwise it was just mentioned in a goto so far */
3939 if(label->source_position.input_name != NULL) {
3940 parser_print_error_prefix();
3941 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3942 parser_print_error_prefix_pos(label->source_position);
3943 fprintf(stderr, "previous definition of '%s' was here\n",
3946 label->source_position = token.source_position;
3949 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3951 label_statement->statement.type = STATEMENT_LABEL;
3952 label_statement->statement.source_position = token.source_position;
3953 label_statement->label = label;
3957 if(token.type == '}') {
3958 parse_error("label at end of compound statement");
3959 return (statement_t*) label_statement;
3961 label_statement->label_statement = parse_statement();
3964 return (statement_t*) label_statement;
3967 static statement_t *parse_if(void)
3971 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3972 statement->statement.type = STATEMENT_IF;
3973 statement->statement.source_position = token.source_position;
3976 statement->condition = parse_expression();
3979 statement->true_statement = parse_statement();
3980 if(token.type == T_else) {
3982 statement->false_statement = parse_statement();
3985 return (statement_t*) statement;
3988 static statement_t *parse_switch(void)
3992 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3993 statement->statement.type = STATEMENT_SWITCH;
3994 statement->statement.source_position = token.source_position;
3997 statement->expression = parse_expression();
3999 statement->body = parse_statement();
4001 return (statement_t*) statement;
4004 static statement_t *parse_while(void)
4008 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4009 statement->statement.type = STATEMENT_WHILE;
4010 statement->statement.source_position = token.source_position;
4013 statement->condition = parse_expression();
4015 statement->body = parse_statement();
4017 return (statement_t*) statement;
4020 static statement_t *parse_do(void)
4024 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4025 statement->statement.type = STATEMENT_DO_WHILE;
4026 statement->statement.source_position = token.source_position;
4028 statement->body = parse_statement();
4031 statement->condition = parse_expression();
4035 return (statement_t*) statement;
4038 static statement_t *parse_for(void)
4042 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4043 statement->statement.type = STATEMENT_FOR;
4044 statement->statement.source_position = token.source_position;
4048 int top = environment_top();
4049 context_t *last_context = context;
4050 set_context(&statement->context);
4052 if(token.type != ';') {
4053 if(is_declaration_specifier(&token, false)) {
4054 parse_declaration();
4056 statement->initialisation = parse_expression();
4063 if(token.type != ';') {
4064 statement->condition = parse_expression();
4067 if(token.type != ')') {
4068 statement->step = parse_expression();
4071 statement->body = parse_statement();
4073 assert(context == &statement->context);
4074 set_context(last_context);
4075 environment_pop_to(top);
4077 return (statement_t*) statement;
4080 static statement_t *parse_goto(void)
4084 if(token.type != T_IDENTIFIER) {
4085 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4089 symbol_t *symbol = token.v.symbol;
4092 declaration_t *label = get_label(symbol);
4094 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4096 statement->statement.type = STATEMENT_GOTO;
4097 statement->statement.source_position = token.source_position;
4099 statement->label = label;
4103 return (statement_t*) statement;
4106 static statement_t *parse_continue(void)
4111 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4112 statement->type = STATEMENT_CONTINUE;
4113 statement->source_position = token.source_position;
4118 static statement_t *parse_break(void)
4123 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4124 statement->type = STATEMENT_BREAK;
4125 statement->source_position = token.source_position;
4130 static statement_t *parse_return(void)
4134 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4136 statement->statement.type = STATEMENT_RETURN;
4137 statement->statement.source_position = token.source_position;
4139 assert(current_function->type->type == TYPE_FUNCTION);
4140 function_type_t *function_type = (function_type_t*) current_function->type;
4141 type_t *return_type = function_type->result_type;
4143 expression_t *return_value;
4144 if(token.type != ';') {
4145 return_value = parse_expression();
4147 if(return_type == type_void && return_value->datatype != type_void) {
4148 parse_warning("'return' with a value, in function returning void");
4149 return_value = NULL;
4151 if(return_type != NULL) {
4152 semantic_assign(return_type, &return_value, "'return'");
4156 return_value = NULL;
4157 if(return_type != type_void) {
4158 parse_warning("'return' without value, in function returning "
4162 statement->return_value = return_value;
4166 return (statement_t*) statement;
4169 static statement_t *parse_declaration_statement(void)
4171 declaration_t *before = last_declaration;
4173 declaration_statement_t *statement
4174 = allocate_ast_zero(sizeof(statement[0]));
4175 statement->statement.type = STATEMENT_DECLARATION;
4176 statement->statement.source_position = token.source_position;
4178 declaration_specifiers_t specifiers;
4179 memset(&specifiers, 0, sizeof(specifiers));
4180 parse_declaration_specifiers(&specifiers);
4182 if(token.type == ';') {
4185 parse_init_declarators(&specifiers);
4188 if(before == NULL) {
4189 statement->declarations_begin = context->declarations;
4191 statement->declarations_begin = before->next;
4193 statement->declarations_end = last_declaration;
4195 return (statement_t*) statement;
4198 static statement_t *parse_expression_statement(void)
4200 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4201 statement->statement.type = STATEMENT_EXPRESSION;
4202 statement->statement.source_position = token.source_position;
4204 statement->expression = parse_expression();
4208 return (statement_t*) statement;
4211 static statement_t *parse_statement(void)
4213 statement_t *statement = NULL;
4215 /* declaration or statement */
4216 switch(token.type) {
4218 statement = parse_case_statement();
4222 statement = parse_default_statement();
4226 statement = parse_compound_statement();
4230 statement = parse_if();
4234 statement = parse_switch();
4238 statement = parse_while();
4242 statement = parse_do();
4246 statement = parse_for();
4250 statement = parse_goto();
4254 statement = parse_continue();
4258 statement = parse_break();
4262 statement = parse_return();
4271 if(look_ahead(1)->type == ':') {
4272 statement = parse_label_statement();
4276 if(is_typedef_symbol(token.v.symbol)) {
4277 statement = parse_declaration_statement();
4281 statement = parse_expression_statement();
4284 case T___extension__:
4285 /* this can be a prefix to a declaration or an expression statement */
4286 /* we simply eat it now and parse the rest with tail recursion */
4289 } while(token.type == T___extension__);
4290 statement = parse_statement();
4294 statement = parse_declaration_statement();
4298 statement = parse_expression_statement();
4302 assert(statement == NULL || statement->source_position.input_name != NULL);
4307 static statement_t *parse_compound_statement(void)
4309 compound_statement_t *compound_statement
4310 = allocate_ast_zero(sizeof(compound_statement[0]));
4311 compound_statement->statement.type = STATEMENT_COMPOUND;
4312 compound_statement->statement.source_position = token.source_position;
4316 int top = environment_top();
4317 context_t *last_context = context;
4318 set_context(&compound_statement->context);
4320 statement_t *last_statement = NULL;
4322 while(token.type != '}' && token.type != T_EOF) {
4323 statement_t *statement = parse_statement();
4324 if(statement == NULL)
4327 if(last_statement != NULL) {
4328 last_statement->next = statement;
4330 compound_statement->statements = statement;
4333 while(statement->next != NULL)
4334 statement = statement->next;
4336 last_statement = statement;
4339 if(token.type != '}') {
4340 parser_print_error_prefix_pos(
4341 compound_statement->statement.source_position);
4342 fprintf(stderr, "end of file while looking for closing '}'\n");
4346 assert(context == &compound_statement->context);
4347 set_context(last_context);
4348 environment_pop_to(top);
4350 return (statement_t*) compound_statement;
4353 static translation_unit_t *parse_translation_unit(void)
4355 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4357 assert(global_context == NULL);
4358 global_context = &unit->context;
4360 assert(context == NULL);
4361 set_context(&unit->context);
4363 while(token.type != T_EOF) {
4364 parse_declaration();
4367 assert(context == &unit->context);
4369 last_declaration = NULL;
4371 assert(global_context == &unit->context);
4372 global_context = NULL;
4377 translation_unit_t *parse(void)
4379 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4380 label_stack = NEW_ARR_F(stack_entry_t, 0);
4381 found_error = false;
4383 type_set_output(stderr);
4384 ast_set_output(stderr);
4386 lookahead_bufpos = 0;
4387 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4390 translation_unit_t *unit = parse_translation_unit();
4392 DEL_ARR_F(environment_stack);
4393 DEL_ARR_F(label_stack);
4401 void init_parser(void)
4403 init_expression_parsers();
4404 obstack_init(&temp_obst);
4406 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4407 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4408 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4409 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4410 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4411 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4412 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4413 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4414 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4415 type_void_ptr = make_pointer_type(type_void, 0);
4416 type_string = make_pointer_type(type_const_char, 0);
4419 void exit_parser(void)
4421 obstack_free(&temp_obst, NULL);