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 (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_expression(type_t *type,
898 expression_t *expression)
900 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
902 /* TODO check that expression is a constant expression */
904 /* § 6.7.8.14/15 char array may be initialized by string literals */
905 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
906 array_type_t *array_type = (array_type_t*) type;
907 type_t *element_type = array_type->element_type;
909 if(element_type->type == TYPE_ATOMIC) {
910 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
911 atomic_type_type_t atype = atomic_type->atype;
913 /* TODO handle wide strings */
914 if(atype == ATOMIC_TYPE_CHAR
915 || atype == ATOMIC_TYPE_SCHAR
916 || atype == ATOMIC_TYPE_UCHAR) {
917 /* it's fine TODO: check for length of string array... */
918 goto initializer_from_expression_finished;
923 semantic_assign(type, &expression, "initializer");
925 initializer_from_expression_finished:
926 result->initializer.type = INITIALIZER_VALUE;
927 result->value = expression;
929 return (initializer_t*) result;
932 static initializer_t *parse_sub_initializer(type_t *type,
933 expression_t *expression,
934 type_t *expression_type);
936 static initializer_t *parse_sub_initializer_elem(type_t *type)
938 if(token.type == '{') {
939 return parse_sub_initializer(type, NULL, NULL);
942 expression_t *expression = parse_assignment_expression();
943 type_t *expression_type = skip_typeref(expression->datatype);
945 return parse_sub_initializer(type, expression, expression_type);
948 static bool had_initializer_brace_warning;
950 static initializer_t *parse_sub_initializer(type_t *type,
951 expression_t *expression,
952 type_t *expression_type)
954 if(is_type_scalar(type)) {
955 /* there might be extra {} hierarchies */
956 if(token.type == '{') {
958 if(!had_initializer_brace_warning) {
959 parse_warning("braces around scalar initializer");
960 had_initializer_brace_warning = true;
962 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
963 if(token.type == ',') {
965 /* TODO: warn about excessive elements */
971 if(expression == NULL) {
972 expression = parse_assignment_expression();
974 return initializer_from_expression(type, expression);
977 /* TODO: ignore qualifiers, comparing pointers is probably
979 if(expression != NULL && expression_type == type) {
980 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
981 result->initializer.type = INITIALIZER_VALUE;
984 semantic_assign(type, &expression, "initializer");
986 result->value = expression;
988 return (initializer_t*) result;
991 bool read_paren = false;
992 if(token.type == '{') {
997 /* descend into subtype */
998 initializer_t *result = NULL;
999 initializer_t **elems;
1000 if(type->type == TYPE_ARRAY) {
1001 array_type_t *array_type = (array_type_t*) type;
1002 type_t *element_type = array_type->element_type;
1003 element_type = skip_typeref(element_type);
1006 had_initializer_brace_warning = false;
1007 if(expression == NULL) {
1008 sub = parse_sub_initializer_elem(element_type);
1010 sub = parse_sub_initializer(element_type, expression,
1014 /* didn't match the subtypes -> try the parent type */
1016 assert(!read_paren);
1020 elems = NEW_ARR_F(initializer_t*, 0);
1021 ARR_APP1(initializer_t*, elems, sub);
1024 if(token.type == '}')
1029 = parse_sub_initializer(element_type, NULL, NULL);
1031 /* TODO error, do nicer cleanup */
1032 parse_error("member initializer didn't match");
1036 ARR_APP1(initializer_t*, elems, sub);
1039 assert(type->type == TYPE_COMPOUND_STRUCT
1040 || type->type == TYPE_COMPOUND_UNION);
1041 compound_type_t *compound_type = (compound_type_t*) type;
1042 context_t *context = & compound_type->declaration->context;
1044 declaration_t *first = context->declarations;
1047 type_t *first_type = first->type;
1048 first_type = skip_typeref(first_type);
1051 had_initializer_brace_warning = false;
1052 if(expression == NULL) {
1053 sub = parse_sub_initializer_elem(first_type);
1055 sub = parse_sub_initializer(first_type, expression,expression_type);
1058 /* didn't match the subtypes -> try our parent type */
1060 assert(!read_paren);
1064 elems = NEW_ARR_F(initializer_t*, 0);
1065 ARR_APP1(initializer_t*, elems, sub);
1067 declaration_t *iter = first->next;
1068 for( ; iter != NULL; iter = iter->next) {
1069 if(iter->symbol == NULL)
1071 if(iter->namespc != NAMESPACE_NORMAL)
1074 if(token.type == '}')
1078 type_t *iter_type = iter->type;
1079 iter_type = skip_typeref(iter_type);
1081 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1083 /* TODO error, do nicer cleanup*/
1084 parse_error("member initializer didn't match");
1088 ARR_APP1(initializer_t*, elems, sub);
1092 int len = ARR_LEN(elems);
1093 size_t elems_size = sizeof(initializer_t*) * len;
1095 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1097 init->initializer.type = INITIALIZER_LIST;
1099 memcpy(init->initializers, elems, elems_size);
1102 result = (initializer_t*) init;
1105 if(token.type == ',')
1112 static initializer_t *parse_initializer(type_t *type)
1114 initializer_t *result;
1116 type = skip_typeref(type);
1118 if(token.type != '{') {
1119 expression_t *expression = parse_assignment_expression();
1120 return initializer_from_expression(type, expression);
1123 if(is_type_scalar(type)) {
1127 expression_t *expression = parse_assignment_expression();
1128 result = initializer_from_expression(type, expression);
1130 if(token.type == ',')
1136 result = parse_sub_initializer(type, NULL, NULL);
1144 static declaration_t *parse_compound_type_specifier(bool is_struct)
1152 symbol_t *symbol = NULL;
1153 declaration_t *declaration = NULL;
1155 if (token.type == T___attribute__) {
1160 if(token.type == T_IDENTIFIER) {
1161 symbol = token.v.symbol;
1165 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1167 declaration = get_declaration(symbol, NAMESPACE_UNION);
1169 } else if(token.type != '{') {
1171 parse_error_expected("while parsing struct type specifier",
1172 T_IDENTIFIER, '{', 0);
1174 parse_error_expected("while parsing union type specifier",
1175 T_IDENTIFIER, '{', 0);
1181 if(declaration == NULL) {
1182 declaration = allocate_type_zero(sizeof(declaration[0]));
1185 declaration->namespc = NAMESPACE_STRUCT;
1187 declaration->namespc = NAMESPACE_UNION;
1189 declaration->source_position = token.source_position;
1190 declaration->symbol = symbol;
1191 record_declaration(declaration);
1194 if(token.type == '{') {
1195 if(declaration->init.is_defined) {
1196 assert(symbol != NULL);
1197 parser_print_error_prefix();
1198 fprintf(stderr, "multiple definition of %s %s\n",
1199 is_struct ? "struct" : "union", symbol->string);
1200 declaration->context.declarations = NULL;
1202 declaration->init.is_defined = true;
1204 int top = environment_top();
1205 context_t *last_context = context;
1206 set_context(& declaration->context);
1208 parse_compound_type_entries();
1211 assert(context == & declaration->context);
1212 set_context(last_context);
1213 environment_pop_to(top);
1219 static void parse_enum_entries(void)
1223 if(token.type == '}') {
1225 parse_error("empty enum not allowed");
1230 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1232 if(token.type != T_IDENTIFIER) {
1233 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1237 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1238 entry->symbol = token.v.symbol;
1239 entry->source_position = token.source_position;
1242 if(token.type == '=') {
1244 entry->init.enum_value = parse_constant_expression();
1247 record_declaration(entry);
1249 if(token.type != ',')
1252 } while(token.type != '}');
1257 static declaration_t *parse_enum_specifier(void)
1261 declaration_t *declaration;
1264 if(token.type == T_IDENTIFIER) {
1265 symbol = token.v.symbol;
1268 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1269 } else if(token.type != '{') {
1270 parse_error_expected("while parsing enum type specifier",
1271 T_IDENTIFIER, '{', 0);
1278 if(declaration == NULL) {
1279 declaration = allocate_type_zero(sizeof(declaration[0]));
1281 declaration->namespc = NAMESPACE_ENUM;
1282 declaration->source_position = token.source_position;
1283 declaration->symbol = symbol;
1286 if(token.type == '{') {
1287 if(declaration->init.is_defined) {
1288 parser_print_error_prefix();
1289 fprintf(stderr, "multiple definitions of enum %s\n",
1292 record_declaration(declaration);
1293 declaration->init.is_defined = 1;
1295 parse_enum_entries();
1303 * if a symbol is a typedef to another type, return true
1305 static bool is_typedef_symbol(symbol_t *symbol)
1307 const declaration_t *const declaration =
1308 get_declaration(symbol, NAMESPACE_NORMAL);
1310 declaration != NULL &&
1311 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1314 static type_t *parse_typeof(void)
1322 expression_t *expression = NULL;
1325 switch(token.type) {
1326 case T___extension__:
1327 /* this can be a prefix to a typename or an expression */
1328 /* we simply eat it now. */
1331 } while(token.type == T___extension__);
1335 if(is_typedef_symbol(token.v.symbol)) {
1336 type = parse_typename();
1338 expression = parse_expression();
1339 type = expression->datatype;
1344 type = parse_typename();
1348 expression = parse_expression();
1349 type = expression->datatype;
1355 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1356 typeof->type.type = TYPE_TYPEOF;
1357 typeof->expression = expression;
1358 typeof->typeof_type = type;
1360 return (type_t*) typeof;
1364 SPECIFIER_SIGNED = 1 << 0,
1365 SPECIFIER_UNSIGNED = 1 << 1,
1366 SPECIFIER_LONG = 1 << 2,
1367 SPECIFIER_INT = 1 << 3,
1368 SPECIFIER_DOUBLE = 1 << 4,
1369 SPECIFIER_CHAR = 1 << 5,
1370 SPECIFIER_SHORT = 1 << 6,
1371 SPECIFIER_LONG_LONG = 1 << 7,
1372 SPECIFIER_FLOAT = 1 << 8,
1373 SPECIFIER_BOOL = 1 << 9,
1374 SPECIFIER_VOID = 1 << 10,
1375 #ifdef PROVIDE_COMPLEX
1376 SPECIFIER_COMPLEX = 1 << 11,
1377 SPECIFIER_IMAGINARY = 1 << 12,
1381 static type_t *create_builtin_type(symbol_t *symbol)
1383 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1384 type->type.type = TYPE_BUILTIN;
1385 type->symbol = symbol;
1387 type->real_type = type_int;
1389 return (type_t*) type;
1392 static type_t *get_typedef_type(symbol_t *symbol)
1394 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1395 if(declaration == NULL
1396 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1399 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1400 typedef_type->type.type = TYPE_TYPEDEF;
1401 typedef_type->declaration = declaration;
1403 return (type_t*) typedef_type;
1406 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1408 type_t *type = NULL;
1409 unsigned type_qualifiers = 0;
1410 unsigned type_specifiers = 0;
1414 switch(token.type) {
1417 #define MATCH_STORAGE_CLASS(token, class) \
1419 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1420 parse_error("multiple storage classes in declaration " \
1423 specifiers->storage_class = class; \
1427 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1428 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1429 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1430 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1431 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1433 /* type qualifiers */
1434 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1436 type_qualifiers |= qualifier; \
1440 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1441 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1442 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1444 case T___extension__:
1449 /* type specifiers */
1450 #define MATCH_SPECIFIER(token, specifier, name) \
1453 if(type_specifiers & specifier) { \
1454 parse_error("multiple " name " type specifiers given"); \
1456 type_specifiers |= specifier; \
1460 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1461 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1462 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1463 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1464 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1465 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1466 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1467 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1468 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1469 #ifdef PROVIDE_COMPLEX
1470 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1471 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1475 specifiers->is_inline = true;
1480 if(type_specifiers & SPECIFIER_LONG_LONG) {
1481 parse_error("multiple type specifiers given");
1482 } else if(type_specifiers & SPECIFIER_LONG) {
1483 type_specifiers |= SPECIFIER_LONG_LONG;
1485 type_specifiers |= SPECIFIER_LONG;
1489 /* TODO: if type != NULL for the following rules should issue
1492 compound_type_t *compound_type
1493 = allocate_type_zero(sizeof(compound_type[0]));
1494 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1495 compound_type->declaration = parse_compound_type_specifier(true);
1497 type = (type_t*) compound_type;
1501 compound_type_t *compound_type
1502 = allocate_type_zero(sizeof(compound_type[0]));
1503 compound_type->type.type = TYPE_COMPOUND_UNION;
1504 compound_type->declaration = parse_compound_type_specifier(false);
1506 type = (type_t*) compound_type;
1510 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1511 enum_type->type.type = TYPE_ENUM;
1512 enum_type->declaration = parse_enum_specifier();
1514 type = (type_t*) enum_type;
1518 type = parse_typeof();
1520 case T___builtin_va_list:
1521 type = create_builtin_type(token.v.symbol);
1525 case T___attribute__:
1530 case T_IDENTIFIER: {
1531 type_t *typedef_type = get_typedef_type(token.v.symbol);
1533 if(typedef_type == NULL)
1534 goto finish_specifiers;
1537 type = typedef_type;
1541 /* function specifier */
1543 goto finish_specifiers;
1550 atomic_type_type_t atomic_type;
1552 /* match valid basic types */
1553 switch(type_specifiers) {
1554 case SPECIFIER_VOID:
1555 atomic_type = ATOMIC_TYPE_VOID;
1557 case SPECIFIER_CHAR:
1558 atomic_type = ATOMIC_TYPE_CHAR;
1560 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1561 atomic_type = ATOMIC_TYPE_SCHAR;
1563 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1564 atomic_type = ATOMIC_TYPE_UCHAR;
1566 case SPECIFIER_SHORT:
1567 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1568 case SPECIFIER_SHORT | SPECIFIER_INT:
1569 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1570 atomic_type = ATOMIC_TYPE_SHORT;
1572 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1573 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1574 atomic_type = ATOMIC_TYPE_USHORT;
1577 case SPECIFIER_SIGNED:
1578 case SPECIFIER_SIGNED | SPECIFIER_INT:
1579 atomic_type = ATOMIC_TYPE_INT;
1581 case SPECIFIER_UNSIGNED:
1582 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1583 atomic_type = ATOMIC_TYPE_UINT;
1585 case SPECIFIER_LONG:
1586 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1587 case SPECIFIER_LONG | SPECIFIER_INT:
1588 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1589 atomic_type = ATOMIC_TYPE_LONG;
1591 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1592 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1593 atomic_type = ATOMIC_TYPE_ULONG;
1595 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1596 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1597 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1598 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1600 atomic_type = ATOMIC_TYPE_LONGLONG;
1602 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1603 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1605 atomic_type = ATOMIC_TYPE_ULONGLONG;
1607 case SPECIFIER_FLOAT:
1608 atomic_type = ATOMIC_TYPE_FLOAT;
1610 case SPECIFIER_DOUBLE:
1611 atomic_type = ATOMIC_TYPE_DOUBLE;
1613 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1614 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1616 case SPECIFIER_BOOL:
1617 atomic_type = ATOMIC_TYPE_BOOL;
1619 #ifdef PROVIDE_COMPLEX
1620 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1621 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1623 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1624 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1626 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1627 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1629 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1630 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1632 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1633 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1635 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1636 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1640 /* invalid specifier combination, give an error message */
1641 if(type_specifiers == 0) {
1643 parse_warning("no type specifiers in declaration (using int)");
1644 atomic_type = ATOMIC_TYPE_INT;
1647 parse_error("no type specifiers given in declaration");
1649 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1650 (type_specifiers & SPECIFIER_UNSIGNED)) {
1651 parse_error("signed and unsigned specifiers gives");
1652 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1653 parse_error("only integer types can be signed or unsigned");
1655 parse_error("multiple datatypes in declaration");
1657 atomic_type = ATOMIC_TYPE_INVALID;
1660 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1661 atype->type.type = TYPE_ATOMIC;
1662 atype->atype = atomic_type;
1665 type = (type_t*) atype;
1667 if(type_specifiers != 0) {
1668 parse_error("multiple datatypes in declaration");
1672 type->qualifiers = (type_qualifier_t)type_qualifiers;
1674 type_t *result = typehash_insert(type);
1675 if(newtype && result != (type_t*) type) {
1679 specifiers->type = result;
1682 static unsigned parse_type_qualifiers(void)
1684 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1687 switch(token.type) {
1688 /* type qualifiers */
1689 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1690 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1691 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1694 return type_qualifiers;
1699 static void parse_identifier_list(void)
1702 if(token.type != T_IDENTIFIER) {
1703 parse_error_expected("while parsing parameter identifier list",
1708 if(token.type != ',')
1714 static declaration_t *parse_parameter(void)
1716 declaration_specifiers_t specifiers;
1717 memset(&specifiers, 0, sizeof(specifiers));
1719 parse_declaration_specifiers(&specifiers);
1721 declaration_t *declaration
1722 = parse_declarator(&specifiers, specifiers.type, true);
1724 /* TODO check declaration constraints for parameters */
1725 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1726 parse_error("typedef not allowed in parameter list");
1729 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1731 if (declaration->type->type == TYPE_ARRAY) {
1732 const array_type_t *const arr_type =
1733 (const array_type_t*)declaration->type;
1735 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1741 static declaration_t *parse_parameters(function_type_t *type)
1743 if(token.type == T_IDENTIFIER) {
1744 symbol_t *symbol = token.v.symbol;
1745 if(!is_typedef_symbol(symbol)) {
1746 /* TODO: K&R style C parameters */
1747 parse_identifier_list();
1752 if(token.type == ')') {
1753 type->unspecified_parameters = 1;
1756 if(token.type == T_void && look_ahead(1)->type == ')') {
1761 declaration_t *declarations = NULL;
1762 declaration_t *declaration;
1763 declaration_t *last_declaration = NULL;
1764 function_parameter_t *parameter;
1765 function_parameter_t *last_parameter = NULL;
1768 switch(token.type) {
1772 return declarations;
1775 case T___extension__:
1777 declaration = parse_parameter();
1779 parameter = allocate_type_zero(sizeof(parameter[0]));
1780 parameter->type = declaration->type;
1782 if(last_parameter != NULL) {
1783 last_declaration->next = declaration;
1784 last_parameter->next = parameter;
1786 type->parameters = parameter;
1787 declarations = declaration;
1789 last_parameter = parameter;
1790 last_declaration = declaration;
1794 return declarations;
1796 if(token.type != ',')
1797 return declarations;
1807 } construct_type_type_t;
1809 typedef struct construct_type_t construct_type_t;
1810 struct construct_type_t {
1811 construct_type_type_t type;
1812 construct_type_t *next;
1815 typedef struct parsed_pointer_t parsed_pointer_t;
1816 struct parsed_pointer_t {
1817 construct_type_t construct_type;
1818 type_qualifier_t type_qualifiers;
1821 typedef struct construct_function_type_t construct_function_type_t;
1822 struct construct_function_type_t {
1823 construct_type_t construct_type;
1824 function_type_t *function_type;
1827 typedef struct parsed_array_t parsed_array_t;
1828 struct parsed_array_t {
1829 construct_type_t construct_type;
1830 type_qualifier_t type_qualifiers;
1836 typedef struct construct_base_type_t construct_base_type_t;
1837 struct construct_base_type_t {
1838 construct_type_t construct_type;
1842 static construct_type_t *parse_pointer_declarator(void)
1846 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1847 memset(pointer, 0, sizeof(pointer[0]));
1848 pointer->construct_type.type = CONSTRUCT_POINTER;
1849 pointer->type_qualifiers = parse_type_qualifiers();
1851 return (construct_type_t*) pointer;
1854 static construct_type_t *parse_array_declarator(void)
1858 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1859 memset(array, 0, sizeof(array[0]));
1860 array->construct_type.type = CONSTRUCT_ARRAY;
1862 if(token.type == T_static) {
1863 array->is_static = true;
1867 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1868 if(type_qualifiers != 0) {
1869 if(token.type == T_static) {
1870 array->is_static = true;
1874 array->type_qualifiers = type_qualifiers;
1876 if(token.type == '*' && look_ahead(1)->type == ']') {
1877 array->is_variable = true;
1879 } else if(token.type != ']') {
1880 array->size = parse_assignment_expression();
1885 return (construct_type_t*) array;
1888 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1892 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1893 type->type.type = TYPE_FUNCTION;
1895 declaration_t *parameters = parse_parameters(type);
1896 if(declaration != NULL) {
1897 declaration->context.declarations = parameters;
1900 construct_function_type_t *construct_function_type =
1901 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1902 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1903 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1904 construct_function_type->function_type = type;
1908 return (construct_type_t*) construct_function_type;
1911 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1912 int may_be_abstract)
1914 construct_type_t *result = NULL;
1915 construct_type_t *last = NULL;
1917 while(token.type == '*') {
1918 construct_type_t *type = parse_pointer_declarator();
1927 /* TODO: find out if this is correct */
1930 construct_type_t *inner_types = NULL;
1932 switch(token.type) {
1934 if(declaration == NULL) {
1935 parse_error("no identifier expected in typename");
1937 declaration->symbol = token.v.symbol;
1938 declaration->source_position = token.source_position;
1944 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1950 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1951 /* avoid a loop in the outermost scope, because eat_statement doesn't
1953 if(token.type == '}' && current_function == NULL) {
1962 construct_type_t *type;
1963 switch(token.type) {
1965 type = parse_function_declarator(declaration);
1968 type = parse_array_declarator();
1971 goto declarator_finished;
1982 declarator_finished:
1985 if(inner_types != NULL) {
1987 last->next = inner_types;
1989 result = inner_types;
1997 static type_t *construct_declarator_type(construct_type_t *construct_list,
2000 construct_type_t *iter = construct_list;
2001 for( ; iter != NULL; iter = iter->next) {
2002 parsed_pointer_t *parsed_pointer;
2003 parsed_array_t *parsed_array;
2004 construct_function_type_t *construct_function_type;
2005 function_type_t *function_type;
2006 pointer_type_t *pointer_type;
2007 array_type_t *array_type;
2009 switch(iter->type) {
2010 case CONSTRUCT_INVALID:
2011 panic("invalid type construction found");
2012 case CONSTRUCT_FUNCTION:
2013 construct_function_type = (construct_function_type_t*) iter;
2014 function_type = construct_function_type->function_type;
2016 function_type->result_type = type;
2017 type = (type_t*) function_type;
2020 case CONSTRUCT_POINTER:
2021 parsed_pointer = (parsed_pointer_t*) iter;
2022 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2024 pointer_type->type.type = TYPE_POINTER;
2025 pointer_type->points_to = type;
2026 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2027 type = (type_t*) pointer_type;
2030 case CONSTRUCT_ARRAY:
2031 parsed_array = (parsed_array_t*) iter;
2032 array_type = allocate_type_zero(sizeof(array_type[0]));
2034 array_type->type.type = TYPE_ARRAY;
2035 array_type->element_type = type;
2036 array_type->type.qualifiers = parsed_array->type_qualifiers;
2037 array_type->is_static = parsed_array->is_static;
2038 array_type->is_variable = parsed_array->is_variable;
2039 array_type->size = parsed_array->size;
2040 type = (type_t*) array_type;
2044 type_t *hashed_type = typehash_insert((type_t*) type);
2045 if(hashed_type != type) {
2054 static declaration_t *parse_declarator(
2055 const declaration_specifiers_t *specifiers,
2056 type_t *type, bool may_be_abstract)
2058 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2059 declaration->storage_class = specifiers->storage_class;
2060 declaration->is_inline = specifiers->is_inline;
2062 construct_type_t *construct_type
2063 = parse_inner_declarator(declaration, may_be_abstract);
2064 declaration->type = construct_declarator_type(construct_type, type);
2066 if(construct_type != NULL) {
2067 obstack_free(&temp_obst, construct_type);
2073 static type_t *parse_abstract_declarator(type_t *base_type)
2075 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2077 type_t *result = construct_declarator_type(construct_type, base_type);
2078 if(construct_type != NULL) {
2079 obstack_free(&temp_obst, construct_type);
2085 static declaration_t *record_declaration(declaration_t *declaration)
2087 assert(context != NULL);
2089 symbol_t *symbol = declaration->symbol;
2090 if(symbol != NULL) {
2091 declaration_t *alias = environment_push(declaration);
2092 if(alias != declaration)
2095 declaration->parent_context = context;
2098 if(last_declaration != NULL) {
2099 last_declaration->next = declaration;
2101 context->declarations = declaration;
2103 last_declaration = declaration;
2108 static void parser_error_multiple_definition(declaration_t *previous,
2109 declaration_t *declaration)
2111 parser_print_error_prefix_pos(declaration->source_position);
2112 fprintf(stderr, "multiple definition of symbol '%s'\n",
2113 declaration->symbol->string);
2114 parser_print_error_prefix_pos(previous->source_position);
2115 fprintf(stderr, "this is the location of the previous definition.\n");
2118 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2121 declaration_t *ndeclaration
2122 = parse_declarator(specifiers, specifiers->type, false);
2124 declaration_t *declaration = record_declaration(ndeclaration);
2126 type_t *orig_type = declaration->type;
2127 type_t *type = skip_typeref(orig_type);
2128 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2129 parser_print_warning_prefix_pos(declaration->source_position);
2130 fprintf(stderr, "variable '%s' declared 'inline'\n",
2131 declaration->symbol->string);
2134 if(token.type == '=') {
2137 /* TODO: check that this is an allowed type (no function type) */
2139 if(declaration->init.initializer != NULL) {
2140 parser_error_multiple_definition(declaration, ndeclaration);
2143 initializer_t *initializer = parse_initializer(type);
2145 if(type->type == TYPE_ARRAY && initializer != NULL) {
2146 assert(initializer->type == INITIALIZER_LIST);
2148 initializer_list_t *list = (initializer_list_t*) initializer;
2149 array_type_t *array_type = (array_type_t*) type;
2151 if(array_type->size == NULL) {
2152 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2154 cnst->expression.type = EXPR_CONST;
2155 cnst->expression.datatype = type_size_t;
2156 cnst->v.int_value = list->len;
2158 array_type->size = (expression_t*) cnst;
2163 ndeclaration->init.initializer = initializer;
2164 } else if(token.type == '{') {
2165 if(type->type != TYPE_FUNCTION) {
2166 parser_print_error_prefix();
2167 fprintf(stderr, "declarator '");
2168 print_type_ext(orig_type, declaration->symbol, NULL);
2169 fprintf(stderr, "' has a body but is not a function type.\n");
2174 if(declaration->init.statement != NULL) {
2175 parser_error_multiple_definition(declaration, ndeclaration);
2177 if(ndeclaration != declaration) {
2178 memcpy(&declaration->context, &ndeclaration->context,
2179 sizeof(declaration->context));
2182 int top = environment_top();
2183 context_t *last_context = context;
2184 set_context(&declaration->context);
2186 /* push function parameters */
2187 declaration_t *parameter = declaration->context.declarations;
2188 for( ; parameter != NULL; parameter = parameter->next) {
2189 environment_push(parameter);
2192 int label_stack_top = label_top();
2193 declaration_t *old_current_function = current_function;
2194 current_function = declaration;
2196 statement_t *statement = parse_compound_statement();
2198 assert(current_function == declaration);
2199 current_function = old_current_function;
2200 label_pop_to(label_stack_top);
2202 assert(context == &declaration->context);
2203 set_context(last_context);
2204 environment_pop_to(top);
2206 declaration->init.statement = statement;
2210 if(token.type != ',')
2217 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2220 if(token.type == ':') {
2222 parse_constant_expression();
2223 /* TODO (bitfields) */
2225 declaration_t *declaration
2226 = parse_declarator(specifiers, specifiers->type, true);
2228 /* TODO: check constraints for struct declarations */
2229 /* TODO: check for doubled fields */
2230 record_declaration(declaration);
2232 if(token.type == ':') {
2234 parse_constant_expression();
2235 /* TODO (bitfields) */
2239 if(token.type != ',')
2246 static void parse_compound_type_entries(void)
2250 while(token.type != '}' && token.type != T_EOF) {
2251 declaration_specifiers_t specifiers;
2252 memset(&specifiers, 0, sizeof(specifiers));
2253 parse_declaration_specifiers(&specifiers);
2255 parse_struct_declarators(&specifiers);
2257 if(token.type == T_EOF) {
2258 parse_error("unexpected error while parsing struct");
2263 static void parse_declaration(void)
2265 source_position_t source_position = token.source_position;
2267 declaration_specifiers_t specifiers;
2268 memset(&specifiers, 0, sizeof(specifiers));
2269 parse_declaration_specifiers(&specifiers);
2271 if(token.type == ';') {
2272 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2273 parse_warning_pos(source_position,
2274 "useless keyword in empty declaration");
2276 switch (specifiers.type->type) {
2277 case TYPE_COMPOUND_STRUCT:
2278 case TYPE_COMPOUND_UNION: {
2279 const compound_type_t *const comp_type =
2280 (const compound_type_t*)specifiers.type;
2281 if (comp_type->declaration->symbol == NULL) {
2282 parse_warning_pos(source_position,
2283 "unnamed struct/union that defines no instances");
2288 case TYPE_ENUM: break;
2291 parse_warning_pos(source_position, "empty declaration");
2297 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2299 declaration->type = specifiers.type;
2300 declaration->storage_class = specifiers.storage_class;
2301 declaration->source_position = source_position;
2302 record_declaration(declaration);
2305 parse_init_declarators(&specifiers);
2308 static type_t *parse_typename(void)
2310 declaration_specifiers_t specifiers;
2311 memset(&specifiers, 0, sizeof(specifiers));
2312 parse_declaration_specifiers(&specifiers);
2313 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2314 /* TODO: improve error message, user does probably not know what a
2315 * storage class is...
2317 parse_error("typename may not have a storage class");
2320 type_t *result = parse_abstract_declarator(specifiers.type);
2328 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2329 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2330 expression_t *left);
2332 typedef struct expression_parser_function_t expression_parser_function_t;
2333 struct expression_parser_function_t {
2334 unsigned precedence;
2335 parse_expression_function parser;
2336 unsigned infix_precedence;
2337 parse_expression_infix_function infix_parser;
2340 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2342 static expression_t *make_invalid_expression(void)
2344 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2345 expression->type = EXPR_INVALID;
2346 expression->source_position = token.source_position;
2350 static expression_t *expected_expression_error(void)
2352 parser_print_error_prefix();
2353 fprintf(stderr, "expected expression, got token ");
2354 print_token(stderr, & token);
2355 fprintf(stderr, "\n");
2359 return make_invalid_expression();
2362 static expression_t *parse_string_const(void)
2364 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2366 cnst->expression.type = EXPR_STRING_LITERAL;
2367 cnst->expression.datatype = type_string;
2368 cnst->value = parse_string_literals();
2370 return (expression_t*) cnst;
2373 static expression_t *parse_int_const(void)
2375 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2377 cnst->expression.type = EXPR_CONST;
2378 cnst->expression.datatype = token.datatype;
2379 cnst->v.int_value = token.v.intvalue;
2383 return (expression_t*) cnst;
2386 static expression_t *parse_float_const(void)
2388 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2390 cnst->expression.type = EXPR_CONST;
2391 cnst->expression.datatype = token.datatype;
2392 cnst->v.float_value = token.v.floatvalue;
2396 return (expression_t*) cnst;
2399 static declaration_t *create_implicit_function(symbol_t *symbol,
2400 const source_position_t source_position)
2402 function_type_t *function_type
2403 = allocate_type_zero(sizeof(function_type[0]));
2405 function_type->type.type = TYPE_FUNCTION;
2406 function_type->result_type = type_int;
2407 function_type->unspecified_parameters = true;
2409 type_t *type = typehash_insert((type_t*) function_type);
2410 if(type != (type_t*) function_type) {
2411 free_type(function_type);
2414 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2416 declaration->storage_class = STORAGE_CLASS_EXTERN;
2417 declaration->type = type;
2418 declaration->symbol = symbol;
2419 declaration->source_position = source_position;
2421 /* prepend the implicit definition to the global context
2422 * this is safe since the symbol wasn't declared as anything else yet
2424 assert(symbol->declaration == NULL);
2426 context_t *last_context = context;
2427 context = global_context;
2429 environment_push(declaration);
2430 declaration->next = context->declarations;
2431 context->declarations = declaration;
2433 context = last_context;
2438 static expression_t *parse_reference(void)
2440 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2442 ref->expression.type = EXPR_REFERENCE;
2443 ref->symbol = token.v.symbol;
2445 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2447 source_position_t source_position = token.source_position;
2450 if(declaration == NULL) {
2452 /* an implicitly defined function */
2453 if(token.type == '(') {
2454 parser_print_prefix_pos(token.source_position);
2455 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2456 ref->symbol->string);
2458 declaration = create_implicit_function(ref->symbol,
2463 parser_print_error_prefix();
2464 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2465 return (expression_t*) ref;
2469 ref->declaration = declaration;
2470 ref->expression.datatype = declaration->type;
2472 return (expression_t*) ref;
2475 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2479 /* TODO check if explicit cast is allowed and issue warnings/errors */
2482 static expression_t *parse_cast(void)
2484 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2486 cast->expression.type = EXPR_UNARY;
2487 cast->type = UNEXPR_CAST;
2488 cast->expression.source_position = token.source_position;
2490 type_t *type = parse_typename();
2493 expression_t *value = parse_sub_expression(20);
2495 check_cast_allowed(value, type);
2497 cast->expression.datatype = type;
2498 cast->value = value;
2500 return (expression_t*) cast;
2503 static expression_t *parse_statement_expression(void)
2505 statement_expression_t *expression
2506 = allocate_ast_zero(sizeof(expression[0]));
2507 expression->expression.type = EXPR_STATEMENT;
2509 statement_t *statement = parse_compound_statement();
2510 expression->statement = statement;
2511 if(statement == NULL) {
2516 assert(statement->type == STATEMENT_COMPOUND);
2517 compound_statement_t *compound_statement
2518 = (compound_statement_t*) statement;
2520 /* find last statement and use it's type */
2521 const statement_t *last_statement = NULL;
2522 const statement_t *iter = compound_statement->statements;
2523 for( ; iter != NULL; iter = iter->next) {
2524 last_statement = iter;
2527 if(last_statement->type == STATEMENT_EXPRESSION) {
2528 const expression_statement_t *expression_statement =
2529 (const expression_statement_t*) last_statement;
2530 expression->expression.datatype
2531 = expression_statement->expression->datatype;
2533 expression->expression.datatype = type_void;
2538 return (expression_t*) expression;
2541 static expression_t *parse_brace_expression(void)
2545 switch(token.type) {
2547 /* gcc extension: a stement expression */
2548 return parse_statement_expression();
2552 return parse_cast();
2554 if(is_typedef_symbol(token.v.symbol)) {
2555 return parse_cast();
2559 expression_t *result = parse_expression();
2565 static expression_t *parse_function_keyword(void)
2570 if (current_function == NULL) {
2571 parse_error("'__func__' used outside of a function");
2574 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2575 expression->expression.type = EXPR_FUNCTION;
2576 expression->expression.datatype = type_string;
2577 expression->value = "TODO: FUNCTION";
2579 return (expression_t*) expression;
2582 static expression_t *parse_pretty_function_keyword(void)
2584 eat(T___PRETTY_FUNCTION__);
2587 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2588 expression->expression.type = EXPR_PRETTY_FUNCTION;
2589 expression->expression.datatype = type_string;
2590 expression->value = "TODO: PRETTY FUNCTION";
2592 return (expression_t*) expression;
2595 static designator_t *parse_designator(void)
2597 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2599 if(token.type != T_IDENTIFIER) {
2600 parse_error_expected("while parsing member designator",
2605 result->symbol = token.v.symbol;
2608 designator_t *last_designator = result;
2610 if(token.type == '.') {
2612 if(token.type != T_IDENTIFIER) {
2613 parse_error_expected("while parsing member designator",
2618 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2619 designator->symbol = token.v.symbol;
2622 last_designator->next = designator;
2623 last_designator = designator;
2626 if(token.type == '[') {
2628 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2629 designator->array_access = parse_expression();
2630 if(designator->array_access == NULL) {
2636 last_designator->next = designator;
2637 last_designator = designator;
2646 static expression_t *parse_offsetof(void)
2648 eat(T___builtin_offsetof);
2650 offsetof_expression_t *expression
2651 = allocate_ast_zero(sizeof(expression[0]));
2652 expression->expression.type = EXPR_OFFSETOF;
2653 expression->expression.datatype = type_size_t;
2656 expression->type = parse_typename();
2658 expression->designator = parse_designator();
2661 return (expression_t*) expression;
2664 static expression_t *parse_va_arg(void)
2666 eat(T___builtin_va_arg);
2668 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2669 expression->expression.type = EXPR_VA_ARG;
2672 expression->arg = parse_assignment_expression();
2674 expression->expression.datatype = parse_typename();
2677 return (expression_t*) expression;
2680 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2682 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2683 parameter->type = argument_type;
2685 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2686 type->type.type = TYPE_FUNCTION;
2687 type->result_type = result_type;
2688 type->parameters = parameter;
2690 type_t *result = typehash_insert((type_t*) type);
2691 if(result != (type_t*) type) {
2698 static expression_t *parse_builtin_symbol(void)
2700 builtin_symbol_expression_t *expression
2701 = allocate_ast_zero(sizeof(expression[0]));
2702 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2704 expression->symbol = token.v.symbol;
2707 switch(token.type) {
2708 case T___builtin_alloca:
2709 type = make_function_1_type(type_void_ptr, type_size_t);
2715 expression->expression.datatype = type;
2716 return (expression_t*) expression;
2719 static expression_t *parse_primary_expression(void)
2721 switch(token.type) {
2723 return parse_int_const();
2724 case T_FLOATINGPOINT:
2725 return parse_float_const();
2726 case T_STRING_LITERAL:
2727 return parse_string_const();
2729 return parse_reference();
2730 case T___FUNCTION__:
2732 return parse_function_keyword();
2733 case T___PRETTY_FUNCTION__:
2734 return parse_pretty_function_keyword();
2735 case T___builtin_offsetof:
2736 return parse_offsetof();
2737 case T___builtin_va_arg:
2738 return parse_va_arg();
2739 case T___builtin_alloca:
2740 case T___builtin_expect:
2741 case T___builtin_va_start:
2742 case T___builtin_va_end:
2743 return parse_builtin_symbol();
2746 return parse_brace_expression();
2749 parser_print_error_prefix();
2750 fprintf(stderr, "unexpected token ");
2751 print_token(stderr, &token);
2752 fprintf(stderr, "\n");
2755 return make_invalid_expression();
2758 static expression_t *parse_array_expression(unsigned precedence,
2759 expression_t *array_ref)
2765 expression_t *index = parse_expression();
2767 array_access_expression_t *array_access
2768 = allocate_ast_zero(sizeof(array_access[0]));
2770 array_access->expression.type = EXPR_ARRAY_ACCESS;
2771 array_access->array_ref = array_ref;
2772 array_access->index = index;
2774 type_t *type_left = skip_typeref(array_ref->datatype);
2775 type_t *type_right = skip_typeref(index->datatype);
2777 if(type_left != NULL && type_right != NULL) {
2778 if(type_left->type == TYPE_POINTER) {
2779 pointer_type_t *pointer = (pointer_type_t*) type_left;
2780 array_access->expression.datatype = pointer->points_to;
2781 } else if(type_left->type == TYPE_ARRAY) {
2782 array_type_t *array_type = (array_type_t*) type_left;
2783 array_access->expression.datatype = array_type->element_type;
2784 } else if(type_right->type == TYPE_POINTER) {
2785 pointer_type_t *pointer = (pointer_type_t*) type_right;
2786 array_access->expression.datatype = pointer->points_to;
2787 } else if(type_right->type == TYPE_ARRAY) {
2788 array_type_t *array_type = (array_type_t*) type_right;
2789 array_access->expression.datatype = array_type->element_type;
2791 parser_print_error_prefix();
2792 fprintf(stderr, "array access on object with non-pointer types ");
2793 print_type_quoted(type_left);
2794 fprintf(stderr, ", ");
2795 print_type_quoted(type_right);
2796 fprintf(stderr, "\n");
2800 if(token.type != ']') {
2801 parse_error_expected("Problem while parsing array access", ']', 0);
2802 return (expression_t*) array_access;
2806 return (expression_t*) array_access;
2809 static bool is_declaration_specifier(const token_t *token,
2810 bool only_type_specifiers)
2812 switch(token->type) {
2816 return is_typedef_symbol(token->v.symbol);
2819 if(only_type_specifiers)
2828 static expression_t *parse_sizeof(unsigned precedence)
2832 sizeof_expression_t *sizeof_expression
2833 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2834 sizeof_expression->expression.type = EXPR_SIZEOF;
2835 sizeof_expression->expression.datatype = type_size_t;
2837 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2839 sizeof_expression->type = parse_typename();
2842 expression_t *expression = parse_sub_expression(precedence);
2843 sizeof_expression->type = expression->datatype;
2844 sizeof_expression->size_expression = expression;
2847 return (expression_t*) sizeof_expression;
2850 static expression_t *parse_select_expression(unsigned precedence,
2851 expression_t *compound)
2854 assert(token.type == '.' || token.type == T_MINUSGREATER);
2856 bool is_pointer = (token.type == T_MINUSGREATER);
2859 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2861 select->expression.type = EXPR_SELECT;
2862 select->compound = compound;
2864 if(token.type != T_IDENTIFIER) {
2865 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2866 return (expression_t*) select;
2868 symbol_t *symbol = token.v.symbol;
2869 select->symbol = symbol;
2872 type_t *orig_type = compound->datatype;
2873 if(orig_type == NULL)
2874 return make_invalid_expression();
2876 type_t *type = skip_typeref(orig_type);
2878 type_t *type_left = type;
2880 if(type->type != TYPE_POINTER) {
2881 parser_print_error_prefix();
2882 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2883 print_type_quoted(orig_type);
2884 fputc('\n', stderr);
2885 return make_invalid_expression();
2887 pointer_type_t *pointer_type = (pointer_type_t*) type;
2888 type_left = pointer_type->points_to;
2890 type_left = skip_typeref(type_left);
2892 if(type_left->type != TYPE_COMPOUND_STRUCT
2893 && type_left->type != TYPE_COMPOUND_UNION) {
2894 parser_print_error_prefix();
2895 fprintf(stderr, "request for member '%s' in something not a struct or "
2896 "union, but ", symbol->string);
2897 print_type_quoted(type_left);
2898 fputc('\n', stderr);
2899 return make_invalid_expression();
2902 compound_type_t *compound_type = (compound_type_t*) type_left;
2903 declaration_t *declaration = compound_type->declaration;
2905 if(!declaration->init.is_defined) {
2906 parser_print_error_prefix();
2907 fprintf(stderr, "request for member '%s' of incomplete type ",
2909 print_type_quoted(type_left);
2910 fputc('\n', stderr);
2911 return make_invalid_expression();
2914 declaration_t *iter = declaration->context.declarations;
2915 for( ; iter != NULL; iter = iter->next) {
2916 if(iter->symbol == symbol) {
2921 parser_print_error_prefix();
2922 print_type_quoted(type_left);
2923 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2924 return make_invalid_expression();
2927 select->compound_entry = iter;
2928 select->expression.datatype = iter->type;
2929 return (expression_t*) select;
2932 static expression_t *parse_call_expression(unsigned precedence,
2933 expression_t *expression)
2936 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2937 call->expression.type = EXPR_CALL;
2938 call->function = expression;
2940 function_type_t *function_type;
2941 type_t *type = expression->datatype;
2942 if (type->type == TYPE_FUNCTION) {
2943 function_type = (function_type_t*) type;
2944 call->expression.datatype = function_type->result_type;
2945 } else if (type->type == TYPE_POINTER &&
2946 ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2947 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2948 function_type = (function_type_t*)ptr_type->points_to;
2949 call->expression.datatype = function_type->result_type;
2951 parser_print_error_prefix();
2952 fputs("called object '", stderr);
2953 print_expression(expression);
2954 fputs("' (type ", stderr);
2955 print_type_quoted(type);
2956 fputs(") is not a function\n", stderr);
2958 function_type = NULL;
2959 call->expression.datatype = NULL;
2962 /* parse arguments */
2965 if(token.type != ')') {
2966 call_argument_t *last_argument = NULL;
2969 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2971 argument->expression = parse_assignment_expression();
2972 if(last_argument == NULL) {
2973 call->arguments = argument;
2975 last_argument->next = argument;
2977 last_argument = argument;
2979 if(token.type != ',')
2986 if(function_type != NULL) {
2987 function_parameter_t *parameter = function_type->parameters;
2988 call_argument_t *argument = call->arguments;
2989 for( ; parameter != NULL && argument != NULL;
2990 parameter = parameter->next, argument = argument->next) {
2991 type_t *expected_type = parameter->type;
2992 /* TODO report context in error messages */
2993 argument->expression = create_implicit_cast(argument->expression,
2996 /* too few parameters */
2997 if(parameter != NULL) {
2998 parser_print_error_prefix();
2999 fprintf(stderr, "too few arguments to function '");
3000 print_expression(expression);
3001 fprintf(stderr, "'\n");
3002 } else if(argument != NULL) {
3003 /* too many parameters */
3004 if(!function_type->variadic
3005 && !function_type->unspecified_parameters) {
3006 parser_print_error_prefix();
3007 fprintf(stderr, "too many arguments to function '");
3008 print_expression(expression);
3009 fprintf(stderr, "'\n");
3011 /* do default promotion */
3012 for( ; argument != NULL; argument = argument->next) {
3013 type_t *type = argument->expression->datatype;
3018 if(is_type_integer(type)) {
3019 type = promote_integer(type);
3020 } else if(type == type_float) {
3023 argument->expression
3024 = create_implicit_cast(argument->expression, type);
3030 return (expression_t*) call;
3033 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3035 static expression_t *parse_conditional_expression(unsigned precedence,
3036 expression_t *expression)
3040 conditional_expression_t *conditional
3041 = allocate_ast_zero(sizeof(conditional[0]));
3042 conditional->expression.type = EXPR_CONDITIONAL;
3043 conditional->condition = expression;
3046 type_t *condition_type_orig = conditional->condition->datatype;
3047 if(condition_type_orig != NULL) {
3048 type_t *condition_type = skip_typeref(condition_type_orig);
3049 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3050 type_error("expected a scalar type", expression->source_position,
3051 condition_type_orig);
3055 expression_t *const t_expr = parse_expression();
3056 conditional->true_expression = t_expr;
3058 expression_t *const f_expr = parse_sub_expression(precedence);
3059 conditional->false_expression = f_expr;
3061 type_t *const true_type = t_expr->datatype;
3062 if(true_type == NULL)
3063 return (expression_t*) conditional;
3064 type_t *const false_type = f_expr->datatype;
3065 if(false_type == NULL)
3066 return (expression_t*) conditional;
3068 type_t *const skipped_true_type = skip_typeref(true_type);
3069 type_t *const skipped_false_type = skip_typeref(false_type);
3072 if (skipped_true_type == skipped_false_type) {
3073 conditional->expression.datatype = skipped_true_type;
3074 } else if (is_type_arithmetic(skipped_true_type) &&
3075 is_type_arithmetic(skipped_false_type)) {
3076 type_t *const result = semantic_arithmetic(skipped_true_type,
3077 skipped_false_type);
3078 conditional->true_expression = create_implicit_cast(t_expr, result);
3079 conditional->false_expression = create_implicit_cast(f_expr, result);
3080 conditional->expression.datatype = result;
3081 } else if (skipped_true_type->type == TYPE_POINTER &&
3082 skipped_false_type->type == TYPE_POINTER &&
3083 true /* TODO compatible points_to types */) {
3085 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3086 skipped_false_type->type == TYPE_POINTER)
3087 || (is_null_ptr_const(skipped_false_type) &&
3088 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3090 } else if(/* 1 is pointer to object type, other is void* */ false) {
3093 type_error_incompatible("while parsing conditional",
3094 expression->source_position, true_type,
3095 skipped_false_type);
3098 return (expression_t*) conditional;
3101 static expression_t *parse_extension(unsigned precedence)
3103 eat(T___extension__);
3105 /* TODO enable extensions */
3107 return parse_sub_expression(precedence);
3110 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3112 eat(T___builtin_classify_type);
3114 classify_type_expression_t *const classify_type_expr =
3115 allocate_ast_zero(sizeof(classify_type_expr[0]));
3116 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3117 classify_type_expr->expression.datatype = type_int;
3120 expression_t *const expression = parse_sub_expression(precedence);
3122 classify_type_expr->type_expression = expression;
3124 return (expression_t*)classify_type_expr;
3127 static void semantic_incdec(unary_expression_t *expression)
3129 type_t *orig_type = expression->value->datatype;
3130 if(orig_type == NULL)
3133 type_t *type = skip_typeref(orig_type);
3134 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3135 /* TODO: improve error message */
3136 parser_print_error_prefix();
3137 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3141 expression->expression.datatype = orig_type;
3144 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3146 type_t *orig_type = expression->value->datatype;
3147 if(orig_type == NULL)
3150 type_t *type = skip_typeref(orig_type);
3151 if(!is_type_arithmetic(type)) {
3152 /* TODO: improve error message */
3153 parser_print_error_prefix();
3154 fprintf(stderr, "operation needs an arithmetic type\n");
3158 expression->expression.datatype = orig_type;
3161 static void semantic_unexpr_scalar(unary_expression_t *expression)
3163 type_t *orig_type = expression->value->datatype;
3164 if(orig_type == NULL)
3167 type_t *type = skip_typeref(orig_type);
3168 if (!is_type_scalar(type)) {
3169 parse_error("operand of ! must be of scalar type\n");
3173 expression->expression.datatype = orig_type;
3176 static void semantic_unexpr_integer(unary_expression_t *expression)
3178 type_t *orig_type = expression->value->datatype;
3179 if(orig_type == NULL)
3182 type_t *type = skip_typeref(orig_type);
3183 if (!is_type_integer(type)) {
3184 parse_error("operand of ~ must be of integer type\n");
3188 expression->expression.datatype = orig_type;
3191 static void semantic_dereference(unary_expression_t *expression)
3193 type_t *orig_type = expression->value->datatype;
3194 if(orig_type == NULL)
3197 type_t *type = skip_typeref(orig_type);
3198 switch (type->type) {
3200 array_type_t *const array_type = (array_type_t*)type;
3201 expression->expression.datatype = array_type->element_type;
3205 case TYPE_POINTER: {
3206 pointer_type_t *pointer_type = (pointer_type_t*)type;
3207 expression->expression.datatype = pointer_type->points_to;
3212 parser_print_error_prefix();
3213 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3214 print_type_quoted(orig_type);
3215 fputs(" given.\n", stderr);
3220 static void semantic_take_addr(unary_expression_t *expression)
3222 type_t *orig_type = expression->value->datatype;
3223 if(orig_type == NULL)
3226 expression_t *value = expression->value;
3227 if(value->type == EXPR_REFERENCE) {
3228 reference_expression_t *reference = (reference_expression_t*) value;
3229 declaration_t *declaration = reference->declaration;
3230 if(declaration != NULL) {
3231 declaration->address_taken = 1;
3235 expression->expression.datatype = make_pointer_type(orig_type, 0);
3238 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3239 static expression_t *parse_##unexpression_type(unsigned precedence) \
3243 unary_expression_t *unary_expression \
3244 = allocate_ast_zero(sizeof(unary_expression[0])); \
3245 unary_expression->expression.type = EXPR_UNARY; \
3246 unary_expression->type = unexpression_type; \
3247 unary_expression->value = parse_sub_expression(precedence); \
3249 sfunc(unary_expression); \
3251 return (expression_t*) unary_expression; \
3254 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3255 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3256 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3257 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3258 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3259 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3260 semantic_unexpr_integer)
3261 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3263 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3266 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3268 static expression_t *parse_##unexpression_type(unsigned precedence, \
3269 expression_t *left) \
3271 (void) precedence; \
3274 unary_expression_t *unary_expression \
3275 = allocate_ast_zero(sizeof(unary_expression[0])); \
3276 unary_expression->expression.type = EXPR_UNARY; \
3277 unary_expression->type = unexpression_type; \
3278 unary_expression->value = left; \
3280 sfunc(unary_expression); \
3282 return (expression_t*) unary_expression; \
3285 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3287 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3290 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3292 /* TODO: handle complex + imaginary types */
3294 /* § 6.3.1.8 Usual arithmetic conversions */
3295 if(type_left == type_long_double || type_right == type_long_double) {
3296 return type_long_double;
3297 } else if(type_left == type_double || type_right == type_double) {
3299 } else if(type_left == type_float || type_right == type_float) {
3303 type_right = promote_integer(type_right);
3304 type_left = promote_integer(type_left);
3306 if(type_left == type_right)
3309 bool signed_left = is_type_signed(type_left);
3310 bool signed_right = is_type_signed(type_right);
3311 if(get_rank(type_left) < get_rank(type_right)) {
3312 if(signed_left == signed_right || !signed_right) {
3318 if(signed_left == signed_right || !signed_left) {
3326 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3328 expression_t *left = expression->left;
3329 expression_t *right = expression->right;
3330 type_t *orig_type_left = left->datatype;
3331 type_t *orig_type_right = right->datatype;
3333 if(orig_type_left == NULL || orig_type_right == NULL)
3336 type_t *type_left = skip_typeref(orig_type_left);
3337 type_t *type_right = skip_typeref(orig_type_right);
3339 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3340 /* TODO: improve error message */
3341 parser_print_error_prefix();
3342 fprintf(stderr, "operation needs arithmetic types\n");
3346 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3347 expression->left = create_implicit_cast(left, arithmetic_type);
3348 expression->right = create_implicit_cast(right, arithmetic_type);
3349 expression->expression.datatype = arithmetic_type;
3352 static void semantic_shift_op(binary_expression_t *expression)
3354 expression_t *left = expression->left;
3355 expression_t *right = expression->right;
3356 type_t *orig_type_left = left->datatype;
3357 type_t *orig_type_right = right->datatype;
3359 if(orig_type_left == NULL || orig_type_right == NULL)
3362 type_t *type_left = skip_typeref(orig_type_left);
3363 type_t *type_right = skip_typeref(orig_type_right);
3365 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3366 /* TODO: improve error message */
3367 parser_print_error_prefix();
3368 fprintf(stderr, "operation needs integer types\n");
3372 type_left = promote_integer(type_left);
3373 type_right = promote_integer(type_right);
3375 expression->left = create_implicit_cast(left, type_left);
3376 expression->right = create_implicit_cast(right, type_right);
3377 expression->expression.datatype = type_left;
3380 static void semantic_add(binary_expression_t *expression)
3382 expression_t *left = expression->left;
3383 expression_t *right = expression->right;
3384 type_t *orig_type_left = left->datatype;
3385 type_t *orig_type_right = right->datatype;
3387 if(orig_type_left == NULL || orig_type_right == NULL)
3390 type_t *type_left = skip_typeref(orig_type_left);
3391 type_t *type_right = skip_typeref(orig_type_right);
3394 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3395 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3396 expression->left = create_implicit_cast(left, arithmetic_type);
3397 expression->right = create_implicit_cast(right, arithmetic_type);
3398 expression->expression.datatype = arithmetic_type;
3400 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3401 expression->expression.datatype = type_left;
3402 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3403 expression->expression.datatype = type_right;
3404 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3405 const array_type_t *const arr_type = (const array_type_t*)type_left;
3406 expression->expression.datatype =
3407 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3408 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3409 const array_type_t *const arr_type = (const array_type_t*)type_right;
3410 expression->expression.datatype =
3411 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3413 parser_print_error_prefix();
3414 fprintf(stderr, "invalid operands to binary + (");
3415 print_type_quoted(orig_type_left);
3416 fprintf(stderr, ", ");
3417 print_type_quoted(orig_type_right);
3418 fprintf(stderr, ")\n");
3422 static void semantic_sub(binary_expression_t *expression)
3424 expression_t *left = expression->left;
3425 expression_t *right = expression->right;
3426 type_t *orig_type_left = left->datatype;
3427 type_t *orig_type_right = right->datatype;
3429 if(orig_type_left == NULL || orig_type_right == NULL)
3432 type_t *type_left = skip_typeref(orig_type_left);
3433 type_t *type_right = skip_typeref(orig_type_right);
3436 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3437 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3438 expression->left = create_implicit_cast(left, arithmetic_type);
3439 expression->right = create_implicit_cast(right, arithmetic_type);
3440 expression->expression.datatype = arithmetic_type;
3442 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3443 expression->expression.datatype = type_left;
3444 } else if(type_left->type == TYPE_POINTER &&
3445 type_right->type == TYPE_POINTER) {
3446 if(!pointers_compatible(type_left, type_right)) {
3447 parser_print_error_prefix();
3448 fprintf(stderr, "pointers to incompatible objects to binary - (");
3449 print_type_quoted(orig_type_left);
3450 fprintf(stderr, ", ");
3451 print_type_quoted(orig_type_right);
3452 fprintf(stderr, ")\n");
3454 expression->expression.datatype = type_ptrdiff_t;
3457 parser_print_error_prefix();
3458 fprintf(stderr, "invalid operands to binary - (");
3459 print_type_quoted(orig_type_left);
3460 fprintf(stderr, ", ");
3461 print_type_quoted(orig_type_right);
3462 fprintf(stderr, ")\n");
3466 static void semantic_comparison(binary_expression_t *expression)
3468 expression_t *left = expression->left;
3469 expression_t *right = expression->right;
3470 type_t *orig_type_left = left->datatype;
3471 type_t *orig_type_right = right->datatype;
3473 if(orig_type_left == NULL || orig_type_right == NULL)
3476 type_t *type_left = skip_typeref(orig_type_left);
3477 type_t *type_right = skip_typeref(orig_type_right);
3479 /* TODO non-arithmetic types */
3480 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3481 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3482 expression->left = create_implicit_cast(left, arithmetic_type);
3483 expression->right = create_implicit_cast(right, arithmetic_type);
3484 expression->expression.datatype = arithmetic_type;
3485 } else if (type_left->type == TYPE_POINTER &&
3486 type_right->type == TYPE_POINTER) {
3487 /* TODO check compatibility */
3488 } else if (type_left->type == TYPE_POINTER) {
3489 expression->right = create_implicit_cast(right, type_left);
3490 } else if (type_right->type == TYPE_POINTER) {
3491 expression->left = create_implicit_cast(left, type_right);
3493 type_error_incompatible("invalid operands in comparison",
3494 token.source_position, type_left, type_right);
3496 expression->expression.datatype = type_int;
3499 static void semantic_arithmetic_assign(binary_expression_t *expression)
3501 expression_t *left = expression->left;
3502 expression_t *right = expression->right;
3503 type_t *orig_type_left = left->datatype;
3504 type_t *orig_type_right = right->datatype;
3506 if(orig_type_left == NULL || orig_type_right == NULL)
3509 type_t *type_left = skip_typeref(orig_type_left);
3510 type_t *type_right = skip_typeref(orig_type_right);
3512 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3513 /* TODO: improve error message */
3514 parser_print_error_prefix();
3515 fprintf(stderr, "operation needs arithmetic types\n");
3519 /* combined instructions are tricky. We can't create an implicit cast on
3520 * the left side, because we need the uncasted form for the store.
3521 * The ast2firm pass has to know that left_type must be right_type
3522 * for the arithmeitc operation and create a cast by itself */
3523 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3524 expression->right = create_implicit_cast(right, arithmetic_type);
3525 expression->expression.datatype = type_left;
3528 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3530 expression_t *left = expression->left;
3531 expression_t *right = expression->right;
3532 type_t *orig_type_left = left->datatype;
3533 type_t *orig_type_right = right->datatype;
3535 if(orig_type_left == NULL || orig_type_right == NULL)
3538 type_t *type_left = skip_typeref(orig_type_left);
3539 type_t *type_right = skip_typeref(orig_type_right);
3541 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3542 /* combined instructions are tricky. We can't create an implicit cast on
3543 * the left side, because we need the uncasted form for the store.
3544 * The ast2firm pass has to know that left_type must be right_type
3545 * for the arithmeitc operation and create a cast by itself */
3546 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3547 expression->right = create_implicit_cast(right, arithmetic_type);
3548 expression->expression.datatype = type_left;
3549 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3550 expression->expression.datatype = type_left;
3552 parser_print_error_prefix();
3553 fputs("Incompatible types ", stderr);
3554 print_type_quoted(orig_type_left);
3555 fputs(" and ", stderr);
3556 print_type_quoted(orig_type_right);
3557 fputs(" in assignment\n", stderr);
3562 static void semantic_logical_op(binary_expression_t *expression)
3564 expression_t *left = expression->left;
3565 expression_t *right = expression->right;
3566 type_t *orig_type_left = left->datatype;
3567 type_t *orig_type_right = right->datatype;
3569 if(orig_type_left == NULL || orig_type_right == NULL)
3572 type_t *type_left = skip_typeref(orig_type_left);
3573 type_t *type_right = skip_typeref(orig_type_right);
3575 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3576 /* TODO: improve error message */
3577 parser_print_error_prefix();
3578 fprintf(stderr, "operation needs scalar types\n");
3582 expression->expression.datatype = type_int;
3585 static void semantic_binexpr_assign(binary_expression_t *expression)
3587 expression_t *left = expression->left;
3588 type_t *type_left = left->datatype;
3590 if(type_left == NULL)
3593 if (type_left->type == TYPE_ARRAY) {
3594 parse_error("Cannot assign to arrays.");
3595 } else if (type_left != NULL) {
3596 semantic_assign(type_left, &expression->right, "assignment");
3599 expression->expression.datatype = type_left;
3602 static void semantic_comma(binary_expression_t *expression)
3604 expression->expression.datatype = expression->right->datatype;
3607 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3608 static expression_t *parse_##binexpression_type(unsigned precedence, \
3609 expression_t *left) \
3613 expression_t *right = parse_sub_expression(precedence + lr); \
3615 binary_expression_t *binexpr \
3616 = allocate_ast_zero(sizeof(binexpr[0])); \
3617 binexpr->expression.type = EXPR_BINARY; \
3618 binexpr->type = binexpression_type; \
3619 binexpr->left = left; \
3620 binexpr->right = right; \
3623 return (expression_t*) binexpr; \
3626 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3627 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3628 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3629 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3630 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3631 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3632 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3633 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3634 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3635 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3636 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3637 semantic_comparison, 1)
3638 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3639 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3640 semantic_comparison, 1)
3641 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3642 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3643 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3644 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3645 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3646 /* TODO shift has a bit special semantic */
3647 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3648 semantic_shift_op, 1)
3649 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3650 semantic_shift_op, 1)
3651 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3652 semantic_arithmetic_addsubb_assign, 0)
3653 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3654 semantic_arithmetic_addsubb_assign, 0)
3655 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3656 semantic_arithmetic_assign, 0)
3657 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3658 semantic_arithmetic_assign, 0)
3659 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3660 semantic_arithmetic_assign, 0)
3661 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3662 semantic_arithmetic_assign, 0)
3663 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3664 semantic_arithmetic_assign, 0)
3665 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3666 semantic_arithmetic_assign, 0)
3667 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3668 semantic_arithmetic_assign, 0)
3669 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3670 semantic_arithmetic_assign, 0)
3672 static expression_t *parse_sub_expression(unsigned precedence)
3674 if(token.type < 0) {
3675 return expected_expression_error();
3678 expression_parser_function_t *parser
3679 = &expression_parsers[token.type];
3680 source_position_t source_position = token.source_position;
3683 if(parser->parser != NULL) {
3684 left = parser->parser(parser->precedence);
3686 left = parse_primary_expression();
3688 assert(left != NULL);
3689 left->source_position = source_position;
3692 if(token.type < 0) {
3693 return expected_expression_error();
3696 parser = &expression_parsers[token.type];
3697 if(parser->infix_parser == NULL)
3699 if(parser->infix_precedence < precedence)
3702 left = parser->infix_parser(parser->infix_precedence, left);
3704 assert(left != NULL);
3705 assert(left->type != EXPR_UNKNOWN);
3706 left->source_position = source_position;
3712 static expression_t *parse_expression(void)
3714 return parse_sub_expression(1);
3719 static void register_expression_parser(parse_expression_function parser,
3720 int token_type, unsigned precedence)
3722 expression_parser_function_t *entry = &expression_parsers[token_type];
3724 if(entry->parser != NULL) {
3725 fprintf(stderr, "for token ");
3726 print_token_type(stderr, token_type);
3727 fprintf(stderr, "\n");
3728 panic("trying to register multiple expression parsers for a token");
3730 entry->parser = parser;
3731 entry->precedence = precedence;
3734 static void register_expression_infix_parser(
3735 parse_expression_infix_function parser, int token_type,
3736 unsigned precedence)
3738 expression_parser_function_t *entry = &expression_parsers[token_type];
3740 if(entry->infix_parser != NULL) {
3741 fprintf(stderr, "for token ");
3742 print_token_type(stderr, token_type);
3743 fprintf(stderr, "\n");
3744 panic("trying to register multiple infix expression parsers for a "
3747 entry->infix_parser = parser;
3748 entry->infix_precedence = precedence;
3751 static void init_expression_parsers(void)
3753 memset(&expression_parsers, 0, sizeof(expression_parsers));
3755 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3756 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3757 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3758 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3759 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3760 T_GREATERGREATER, 16);
3761 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3762 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3763 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3764 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3765 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3766 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3767 T_GREATEREQUAL, 14);
3768 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3769 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3770 T_EXCLAMATIONMARKEQUAL, 13);
3771 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3772 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3773 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3774 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3775 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3776 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3777 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3778 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3779 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3780 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3781 T_ASTERISKEQUAL, 2);
3782 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3783 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3785 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3786 T_LESSLESSEQUAL, 2);
3787 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3788 T_GREATERGREATEREQUAL, 2);
3789 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3791 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3793 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3796 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3798 register_expression_infix_parser(parse_array_expression, '[', 30);
3799 register_expression_infix_parser(parse_call_expression, '(', 30);
3800 register_expression_infix_parser(parse_select_expression, '.', 30);
3801 register_expression_infix_parser(parse_select_expression,
3802 T_MINUSGREATER, 30);
3803 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3805 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3808 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3809 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3810 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3811 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3812 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3813 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3814 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3815 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3816 register_expression_parser(parse_sizeof, T_sizeof, 25);
3817 register_expression_parser(parse_extension, T___extension__, 25);
3818 register_expression_parser(parse_builtin_classify_type,
3819 T___builtin_classify_type, 25);
3823 static statement_t *parse_case_statement(void)
3826 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3827 label->statement.type = STATEMENT_CASE_LABEL;
3828 label->statement.source_position = token.source_position;
3830 label->expression = parse_expression();
3833 label->label_statement = parse_statement();
3835 return (statement_t*) label;
3838 static statement_t *parse_default_statement(void)
3842 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3843 label->statement.type = STATEMENT_CASE_LABEL;
3844 label->statement.source_position = token.source_position;
3847 label->label_statement = parse_statement();
3849 return (statement_t*) label;
3852 static declaration_t *get_label(symbol_t *symbol)
3854 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3855 assert(current_function != NULL);
3856 /* if we found a label in the same function, then we already created the
3858 if(candidate != NULL
3859 && candidate->parent_context == ¤t_function->context) {
3863 /* otherwise we need to create a new one */
3864 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3865 declaration->namespc = NAMESPACE_LABEL;
3866 declaration->symbol = symbol;
3868 label_push(declaration);
3873 static statement_t *parse_label_statement(void)
3875 assert(token.type == T_IDENTIFIER);
3876 symbol_t *symbol = token.v.symbol;
3879 declaration_t *label = get_label(symbol);
3881 /* if source position is already set then the label is defined twice,
3882 * otherwise it was just mentioned in a goto so far */
3883 if(label->source_position.input_name != NULL) {
3884 parser_print_error_prefix();
3885 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3886 parser_print_error_prefix_pos(label->source_position);
3887 fprintf(stderr, "previous definition of '%s' was here\n",
3890 label->source_position = token.source_position;
3893 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3895 label_statement->statement.type = STATEMENT_LABEL;
3896 label_statement->statement.source_position = token.source_position;
3897 label_statement->label = label;
3901 if(token.type == '}') {
3902 parse_error("label at end of compound statement");
3903 return (statement_t*) label_statement;
3905 label_statement->label_statement = parse_statement();
3908 return (statement_t*) label_statement;
3911 static statement_t *parse_if(void)
3915 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3916 statement->statement.type = STATEMENT_IF;
3917 statement->statement.source_position = token.source_position;
3920 statement->condition = parse_expression();
3923 statement->true_statement = parse_statement();
3924 if(token.type == T_else) {
3926 statement->false_statement = parse_statement();
3929 return (statement_t*) statement;
3932 static statement_t *parse_switch(void)
3936 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3937 statement->statement.type = STATEMENT_SWITCH;
3938 statement->statement.source_position = token.source_position;
3941 statement->expression = parse_expression();
3943 statement->body = parse_statement();
3945 return (statement_t*) statement;
3948 static statement_t *parse_while(void)
3952 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3953 statement->statement.type = STATEMENT_WHILE;
3954 statement->statement.source_position = token.source_position;
3957 statement->condition = parse_expression();
3959 statement->body = parse_statement();
3961 return (statement_t*) statement;
3964 static statement_t *parse_do(void)
3968 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3969 statement->statement.type = STATEMENT_DO_WHILE;
3970 statement->statement.source_position = token.source_position;
3972 statement->body = parse_statement();
3975 statement->condition = parse_expression();
3979 return (statement_t*) statement;
3982 static statement_t *parse_for(void)
3986 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3987 statement->statement.type = STATEMENT_FOR;
3988 statement->statement.source_position = token.source_position;
3992 int top = environment_top();
3993 context_t *last_context = context;
3994 set_context(&statement->context);
3996 if(token.type != ';') {
3997 if(is_declaration_specifier(&token, false)) {
3998 parse_declaration();
4000 statement->initialisation = parse_expression();
4007 if(token.type != ';') {
4008 statement->condition = parse_expression();
4011 if(token.type != ')') {
4012 statement->step = parse_expression();
4015 statement->body = parse_statement();
4017 assert(context == &statement->context);
4018 set_context(last_context);
4019 environment_pop_to(top);
4021 return (statement_t*) statement;
4024 static statement_t *parse_goto(void)
4028 if(token.type != T_IDENTIFIER) {
4029 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4033 symbol_t *symbol = token.v.symbol;
4036 declaration_t *label = get_label(symbol);
4038 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4040 statement->statement.type = STATEMENT_GOTO;
4041 statement->statement.source_position = token.source_position;
4043 statement->label = label;
4047 return (statement_t*) statement;
4050 static statement_t *parse_continue(void)
4055 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4056 statement->type = STATEMENT_CONTINUE;
4057 statement->source_position = token.source_position;
4062 static statement_t *parse_break(void)
4067 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4068 statement->type = STATEMENT_BREAK;
4069 statement->source_position = token.source_position;
4074 static statement_t *parse_return(void)
4078 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4080 statement->statement.type = STATEMENT_RETURN;
4081 statement->statement.source_position = token.source_position;
4083 assert(current_function->type->type == TYPE_FUNCTION);
4084 function_type_t *function_type = (function_type_t*) current_function->type;
4085 type_t *return_type = function_type->result_type;
4087 expression_t *return_value;
4088 if(token.type != ';') {
4089 return_value = parse_expression();
4091 if(return_type == type_void && return_value->datatype != type_void) {
4092 parse_warning("'return' with a value, in function returning void");
4093 return_value = NULL;
4095 if(return_type != NULL) {
4096 semantic_assign(return_type, &return_value, "'return'");
4100 return_value = NULL;
4101 if(return_type != type_void) {
4102 parse_warning("'return' without value, in function returning "
4106 statement->return_value = return_value;
4110 return (statement_t*) statement;
4113 static statement_t *parse_declaration_statement(void)
4115 declaration_t *before = last_declaration;
4117 declaration_statement_t *statement
4118 = allocate_ast_zero(sizeof(statement[0]));
4119 statement->statement.type = STATEMENT_DECLARATION;
4120 statement->statement.source_position = token.source_position;
4122 declaration_specifiers_t specifiers;
4123 memset(&specifiers, 0, sizeof(specifiers));
4124 parse_declaration_specifiers(&specifiers);
4126 if(token.type == ';') {
4129 parse_init_declarators(&specifiers);
4132 if(before == NULL) {
4133 statement->declarations_begin = context->declarations;
4135 statement->declarations_begin = before->next;
4137 statement->declarations_end = last_declaration;
4139 return (statement_t*) statement;
4142 static statement_t *parse_expression_statement(void)
4144 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4145 statement->statement.type = STATEMENT_EXPRESSION;
4146 statement->statement.source_position = token.source_position;
4148 statement->expression = parse_expression();
4152 return (statement_t*) statement;
4155 static statement_t *parse_statement(void)
4157 statement_t *statement = NULL;
4159 /* declaration or statement */
4160 switch(token.type) {
4162 statement = parse_case_statement();
4166 statement = parse_default_statement();
4170 statement = parse_compound_statement();
4174 statement = parse_if();
4178 statement = parse_switch();
4182 statement = parse_while();
4186 statement = parse_do();
4190 statement = parse_for();
4194 statement = parse_goto();
4198 statement = parse_continue();
4202 statement = parse_break();
4206 statement = parse_return();
4215 if(look_ahead(1)->type == ':') {
4216 statement = parse_label_statement();
4220 if(is_typedef_symbol(token.v.symbol)) {
4221 statement = parse_declaration_statement();
4225 statement = parse_expression_statement();
4228 case T___extension__:
4229 /* this can be a prefix to a declaration or an expression statement */
4230 /* we simply eat it now and parse the rest with tail recursion */
4233 } while(token.type == T___extension__);
4234 statement = parse_statement();
4238 statement = parse_declaration_statement();
4242 statement = parse_expression_statement();
4246 assert(statement == NULL || statement->source_position.input_name != NULL);
4251 static statement_t *parse_compound_statement(void)
4253 compound_statement_t *compound_statement
4254 = allocate_ast_zero(sizeof(compound_statement[0]));
4255 compound_statement->statement.type = STATEMENT_COMPOUND;
4256 compound_statement->statement.source_position = token.source_position;
4260 int top = environment_top();
4261 context_t *last_context = context;
4262 set_context(&compound_statement->context);
4264 statement_t *last_statement = NULL;
4266 while(token.type != '}' && token.type != T_EOF) {
4267 statement_t *statement = parse_statement();
4268 if(statement == NULL)
4271 if(last_statement != NULL) {
4272 last_statement->next = statement;
4274 compound_statement->statements = statement;
4277 while(statement->next != NULL)
4278 statement = statement->next;
4280 last_statement = statement;
4283 if(token.type != '}') {
4284 parser_print_error_prefix_pos(
4285 compound_statement->statement.source_position);
4286 fprintf(stderr, "end of file while looking for closing '}'\n");
4290 assert(context == &compound_statement->context);
4291 set_context(last_context);
4292 environment_pop_to(top);
4294 return (statement_t*) compound_statement;
4297 static translation_unit_t *parse_translation_unit(void)
4299 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4301 assert(global_context == NULL);
4302 global_context = &unit->context;
4304 assert(context == NULL);
4305 set_context(&unit->context);
4307 while(token.type != T_EOF) {
4308 parse_declaration();
4311 assert(context == &unit->context);
4313 last_declaration = NULL;
4315 assert(global_context == &unit->context);
4316 global_context = NULL;
4321 translation_unit_t *parse(void)
4323 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4324 label_stack = NEW_ARR_F(stack_entry_t, 0);
4325 found_error = false;
4327 type_set_output(stderr);
4328 ast_set_output(stderr);
4330 lookahead_bufpos = 0;
4331 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4334 translation_unit_t *unit = parse_translation_unit();
4336 DEL_ARR_F(environment_stack);
4337 DEL_ARR_F(label_stack);
4345 void init_parser(void)
4347 init_expression_parsers();
4348 obstack_init(&temp_obst);
4350 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4351 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4352 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4353 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4354 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4355 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4356 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4357 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4358 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4359 type_void_ptr = make_pointer_type(type_void, 0);
4360 type_string = make_pointer_type(type_const_char, 0);
4363 void exit_parser(void)
4365 obstack_free(&temp_obst, NULL);