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(type_t *enum_type)
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->type = enum_type;
1239 entry->symbol = token.v.symbol;
1240 entry->source_position = token.source_position;
1243 if(token.type == '=') {
1245 entry->init.enum_value = parse_constant_expression();
1250 record_declaration(entry);
1252 if(token.type != ',')
1255 } while(token.type != '}');
1260 static declaration_t *parse_enum_specifier(void)
1264 declaration_t *declaration;
1267 if(token.type == T_IDENTIFIER) {
1268 symbol = token.v.symbol;
1271 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1272 } else if(token.type != '{') {
1273 parse_error_expected("while parsing enum type specifier",
1274 T_IDENTIFIER, '{', 0);
1281 if(declaration == NULL) {
1282 declaration = allocate_type_zero(sizeof(declaration[0]));
1284 declaration->namespc = NAMESPACE_ENUM;
1285 declaration->source_position = token.source_position;
1286 declaration->symbol = symbol;
1289 if(token.type == '{') {
1290 if(declaration->init.is_defined) {
1291 parser_print_error_prefix();
1292 fprintf(stderr, "multiple definitions of enum %s\n",
1295 record_declaration(declaration);
1296 declaration->init.is_defined = 1;
1298 parse_enum_entries(NULL);
1306 * if a symbol is a typedef to another type, return true
1308 static bool is_typedef_symbol(symbol_t *symbol)
1310 const declaration_t *const declaration =
1311 get_declaration(symbol, NAMESPACE_NORMAL);
1313 declaration != NULL &&
1314 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1317 static type_t *parse_typeof(void)
1325 expression_t *expression = NULL;
1328 switch(token.type) {
1329 case T___extension__:
1330 /* this can be a prefix to a typename or an expression */
1331 /* we simply eat it now. */
1334 } while(token.type == T___extension__);
1338 if(is_typedef_symbol(token.v.symbol)) {
1339 type = parse_typename();
1341 expression = parse_expression();
1342 type = expression->datatype;
1347 type = parse_typename();
1351 expression = parse_expression();
1352 type = expression->datatype;
1358 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1359 typeof->type.type = TYPE_TYPEOF;
1360 typeof->expression = expression;
1361 typeof->typeof_type = type;
1363 return (type_t*) typeof;
1367 SPECIFIER_SIGNED = 1 << 0,
1368 SPECIFIER_UNSIGNED = 1 << 1,
1369 SPECIFIER_LONG = 1 << 2,
1370 SPECIFIER_INT = 1 << 3,
1371 SPECIFIER_DOUBLE = 1 << 4,
1372 SPECIFIER_CHAR = 1 << 5,
1373 SPECIFIER_SHORT = 1 << 6,
1374 SPECIFIER_LONG_LONG = 1 << 7,
1375 SPECIFIER_FLOAT = 1 << 8,
1376 SPECIFIER_BOOL = 1 << 9,
1377 SPECIFIER_VOID = 1 << 10,
1378 #ifdef PROVIDE_COMPLEX
1379 SPECIFIER_COMPLEX = 1 << 11,
1380 SPECIFIER_IMAGINARY = 1 << 12,
1384 static type_t *create_builtin_type(symbol_t *symbol)
1386 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1387 type->type.type = TYPE_BUILTIN;
1388 type->symbol = symbol;
1390 type->real_type = type_int;
1392 return (type_t*) type;
1395 static type_t *get_typedef_type(symbol_t *symbol)
1397 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1398 if(declaration == NULL
1399 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1402 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1403 typedef_type->type.type = TYPE_TYPEDEF;
1404 typedef_type->declaration = declaration;
1406 return (type_t*) typedef_type;
1409 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1411 type_t *type = NULL;
1412 unsigned type_qualifiers = 0;
1413 unsigned type_specifiers = 0;
1417 switch(token.type) {
1420 #define MATCH_STORAGE_CLASS(token, class) \
1422 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1423 parse_error("multiple storage classes in declaration " \
1426 specifiers->storage_class = class; \
1430 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1431 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1432 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1433 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1434 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1436 /* type qualifiers */
1437 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1439 type_qualifiers |= qualifier; \
1443 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1444 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1445 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1447 case T___extension__:
1452 /* type specifiers */
1453 #define MATCH_SPECIFIER(token, specifier, name) \
1456 if(type_specifiers & specifier) { \
1457 parse_error("multiple " name " type specifiers given"); \
1459 type_specifiers |= specifier; \
1463 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1464 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1465 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1466 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1467 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1468 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1469 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1470 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1471 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1472 #ifdef PROVIDE_COMPLEX
1473 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1474 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1478 specifiers->is_inline = true;
1483 if(type_specifiers & SPECIFIER_LONG_LONG) {
1484 parse_error("multiple type specifiers given");
1485 } else if(type_specifiers & SPECIFIER_LONG) {
1486 type_specifiers |= SPECIFIER_LONG_LONG;
1488 type_specifiers |= SPECIFIER_LONG;
1492 /* TODO: if type != NULL for the following rules should issue
1495 compound_type_t *compound_type
1496 = allocate_type_zero(sizeof(compound_type[0]));
1497 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1498 compound_type->declaration = parse_compound_type_specifier(true);
1500 type = (type_t*) compound_type;
1504 compound_type_t *compound_type
1505 = allocate_type_zero(sizeof(compound_type[0]));
1506 compound_type->type.type = TYPE_COMPOUND_UNION;
1507 compound_type->declaration = parse_compound_type_specifier(false);
1509 type = (type_t*) compound_type;
1513 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1514 enum_type->type.type = TYPE_ENUM;
1515 enum_type->declaration = parse_enum_specifier();
1517 type = (type_t*) enum_type;
1521 type = parse_typeof();
1523 case T___builtin_va_list:
1524 type = create_builtin_type(token.v.symbol);
1528 case T___attribute__:
1533 case T_IDENTIFIER: {
1534 type_t *typedef_type = get_typedef_type(token.v.symbol);
1536 if(typedef_type == NULL)
1537 goto finish_specifiers;
1540 type = typedef_type;
1544 /* function specifier */
1546 goto finish_specifiers;
1553 atomic_type_type_t atomic_type;
1555 /* match valid basic types */
1556 switch(type_specifiers) {
1557 case SPECIFIER_VOID:
1558 atomic_type = ATOMIC_TYPE_VOID;
1560 case SPECIFIER_CHAR:
1561 atomic_type = ATOMIC_TYPE_CHAR;
1563 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1564 atomic_type = ATOMIC_TYPE_SCHAR;
1566 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1567 atomic_type = ATOMIC_TYPE_UCHAR;
1569 case SPECIFIER_SHORT:
1570 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1571 case SPECIFIER_SHORT | SPECIFIER_INT:
1572 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1573 atomic_type = ATOMIC_TYPE_SHORT;
1575 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1576 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1577 atomic_type = ATOMIC_TYPE_USHORT;
1580 case SPECIFIER_SIGNED:
1581 case SPECIFIER_SIGNED | SPECIFIER_INT:
1582 atomic_type = ATOMIC_TYPE_INT;
1584 case SPECIFIER_UNSIGNED:
1585 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1586 atomic_type = ATOMIC_TYPE_UINT;
1588 case SPECIFIER_LONG:
1589 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1590 case SPECIFIER_LONG | SPECIFIER_INT:
1591 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1592 atomic_type = ATOMIC_TYPE_LONG;
1594 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1595 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1596 atomic_type = ATOMIC_TYPE_ULONG;
1598 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1599 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1600 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1601 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1603 atomic_type = ATOMIC_TYPE_LONGLONG;
1605 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1606 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1608 atomic_type = ATOMIC_TYPE_ULONGLONG;
1610 case SPECIFIER_FLOAT:
1611 atomic_type = ATOMIC_TYPE_FLOAT;
1613 case SPECIFIER_DOUBLE:
1614 atomic_type = ATOMIC_TYPE_DOUBLE;
1616 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1617 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1619 case SPECIFIER_BOOL:
1620 atomic_type = ATOMIC_TYPE_BOOL;
1622 #ifdef PROVIDE_COMPLEX
1623 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1624 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1626 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1627 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1629 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1630 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1632 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1633 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1635 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1636 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1638 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1639 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1643 /* invalid specifier combination, give an error message */
1644 if(type_specifiers == 0) {
1646 parse_warning("no type specifiers in declaration (using int)");
1647 atomic_type = ATOMIC_TYPE_INT;
1650 parse_error("no type specifiers given in declaration");
1652 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1653 (type_specifiers & SPECIFIER_UNSIGNED)) {
1654 parse_error("signed and unsigned specifiers gives");
1655 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1656 parse_error("only integer types can be signed or unsigned");
1658 parse_error("multiple datatypes in declaration");
1660 atomic_type = ATOMIC_TYPE_INVALID;
1663 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1664 atype->type.type = TYPE_ATOMIC;
1665 atype->atype = atomic_type;
1668 type = (type_t*) atype;
1670 if(type_specifiers != 0) {
1671 parse_error("multiple datatypes in declaration");
1675 type->qualifiers = (type_qualifier_t)type_qualifiers;
1677 type_t *result = typehash_insert(type);
1678 if(newtype && result != (type_t*) type) {
1682 specifiers->type = result;
1685 static unsigned parse_type_qualifiers(void)
1687 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1690 switch(token.type) {
1691 /* type qualifiers */
1692 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1693 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1694 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1697 return type_qualifiers;
1702 static void parse_identifier_list(void)
1705 if(token.type != T_IDENTIFIER) {
1706 parse_error_expected("while parsing parameter identifier list",
1711 if(token.type != ',')
1717 static declaration_t *parse_parameter(void)
1719 declaration_specifiers_t specifiers;
1720 memset(&specifiers, 0, sizeof(specifiers));
1722 parse_declaration_specifiers(&specifiers);
1724 declaration_t *declaration
1725 = parse_declarator(&specifiers, specifiers.type, true);
1727 /* TODO check declaration constraints for parameters */
1728 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1729 parse_error("typedef not allowed in parameter list");
1732 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1734 if (declaration->type->type == TYPE_ARRAY) {
1735 const array_type_t *const arr_type =
1736 (const array_type_t*)declaration->type;
1738 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1744 static declaration_t *parse_parameters(function_type_t *type)
1746 if(token.type == T_IDENTIFIER) {
1747 symbol_t *symbol = token.v.symbol;
1748 if(!is_typedef_symbol(symbol)) {
1749 /* TODO: K&R style C parameters */
1750 parse_identifier_list();
1755 if(token.type == ')') {
1756 type->unspecified_parameters = 1;
1759 if(token.type == T_void && look_ahead(1)->type == ')') {
1764 declaration_t *declarations = NULL;
1765 declaration_t *declaration;
1766 declaration_t *last_declaration = NULL;
1767 function_parameter_t *parameter;
1768 function_parameter_t *last_parameter = NULL;
1771 switch(token.type) {
1775 return declarations;
1778 case T___extension__:
1780 declaration = parse_parameter();
1782 parameter = allocate_type_zero(sizeof(parameter[0]));
1783 parameter->type = declaration->type;
1785 if(last_parameter != NULL) {
1786 last_declaration->next = declaration;
1787 last_parameter->next = parameter;
1789 type->parameters = parameter;
1790 declarations = declaration;
1792 last_parameter = parameter;
1793 last_declaration = declaration;
1797 return declarations;
1799 if(token.type != ',')
1800 return declarations;
1810 } construct_type_type_t;
1812 typedef struct construct_type_t construct_type_t;
1813 struct construct_type_t {
1814 construct_type_type_t type;
1815 construct_type_t *next;
1818 typedef struct parsed_pointer_t parsed_pointer_t;
1819 struct parsed_pointer_t {
1820 construct_type_t construct_type;
1821 type_qualifier_t type_qualifiers;
1824 typedef struct construct_function_type_t construct_function_type_t;
1825 struct construct_function_type_t {
1826 construct_type_t construct_type;
1827 function_type_t *function_type;
1830 typedef struct parsed_array_t parsed_array_t;
1831 struct parsed_array_t {
1832 construct_type_t construct_type;
1833 type_qualifier_t type_qualifiers;
1839 typedef struct construct_base_type_t construct_base_type_t;
1840 struct construct_base_type_t {
1841 construct_type_t construct_type;
1845 static construct_type_t *parse_pointer_declarator(void)
1849 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1850 memset(pointer, 0, sizeof(pointer[0]));
1851 pointer->construct_type.type = CONSTRUCT_POINTER;
1852 pointer->type_qualifiers = parse_type_qualifiers();
1854 return (construct_type_t*) pointer;
1857 static construct_type_t *parse_array_declarator(void)
1861 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1862 memset(array, 0, sizeof(array[0]));
1863 array->construct_type.type = CONSTRUCT_ARRAY;
1865 if(token.type == T_static) {
1866 array->is_static = true;
1870 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1871 if(type_qualifiers != 0) {
1872 if(token.type == T_static) {
1873 array->is_static = true;
1877 array->type_qualifiers = type_qualifiers;
1879 if(token.type == '*' && look_ahead(1)->type == ']') {
1880 array->is_variable = true;
1882 } else if(token.type != ']') {
1883 array->size = parse_assignment_expression();
1888 return (construct_type_t*) array;
1891 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1895 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1896 type->type.type = TYPE_FUNCTION;
1898 declaration_t *parameters = parse_parameters(type);
1899 if(declaration != NULL) {
1900 declaration->context.declarations = parameters;
1903 construct_function_type_t *construct_function_type =
1904 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1905 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1906 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1907 construct_function_type->function_type = type;
1911 return (construct_type_t*) construct_function_type;
1914 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1915 bool may_be_abstract)
1917 /* construct a single linked list of construct_type_t's which describe
1918 * how to construct the final declarator type */
1919 construct_type_t *first = NULL;
1920 construct_type_t *last = NULL;
1923 while(token.type == '*') {
1924 construct_type_t *type = parse_pointer_declarator();
1935 /* TODO: find out if this is correct */
1938 construct_type_t *inner_types = NULL;
1940 switch(token.type) {
1942 if(declaration == NULL) {
1943 parse_error("no identifier expected in typename");
1945 declaration->symbol = token.v.symbol;
1946 declaration->source_position = token.source_position;
1952 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1958 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1959 /* avoid a loop in the outermost scope, because eat_statement doesn't
1961 if(token.type == '}' && current_function == NULL) {
1969 construct_type_t *p = last;
1972 construct_type_t *type;
1973 switch(token.type) {
1975 type = parse_function_declarator(declaration);
1978 type = parse_array_declarator();
1981 goto declarator_finished;
1984 /* insert in the middle of the list (behind p) */
1986 type->next = p->next;
1997 declarator_finished:
2000 /* append inner_types at the end of the list, we don't to set last anymore
2001 * as it's not needed anymore */
2003 assert(first == NULL);
2004 first = inner_types;
2006 last->next = inner_types;
2012 static type_t *construct_declarator_type(construct_type_t *construct_list,
2015 construct_type_t *iter = construct_list;
2016 for( ; iter != NULL; iter = iter->next) {
2017 parsed_pointer_t *parsed_pointer;
2018 parsed_array_t *parsed_array;
2019 construct_function_type_t *construct_function_type;
2020 function_type_t *function_type;
2021 pointer_type_t *pointer_type;
2022 array_type_t *array_type;
2024 switch(iter->type) {
2025 case CONSTRUCT_INVALID:
2026 panic("invalid type construction found");
2027 case CONSTRUCT_FUNCTION:
2028 construct_function_type = (construct_function_type_t*) iter;
2029 function_type = construct_function_type->function_type;
2031 function_type->result_type = type;
2032 type = (type_t*) function_type;
2035 case CONSTRUCT_POINTER:
2036 parsed_pointer = (parsed_pointer_t*) iter;
2037 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2039 pointer_type->type.type = TYPE_POINTER;
2040 pointer_type->points_to = type;
2041 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2042 type = (type_t*) pointer_type;
2045 case CONSTRUCT_ARRAY:
2046 parsed_array = (parsed_array_t*) iter;
2047 array_type = allocate_type_zero(sizeof(array_type[0]));
2049 array_type->type.type = TYPE_ARRAY;
2050 array_type->element_type = type;
2051 array_type->type.qualifiers = parsed_array->type_qualifiers;
2052 array_type->is_static = parsed_array->is_static;
2053 array_type->is_variable = parsed_array->is_variable;
2054 array_type->size = parsed_array->size;
2055 type = (type_t*) array_type;
2059 type_t *hashed_type = typehash_insert((type_t*) type);
2060 if(hashed_type != type) {
2061 /* the function type was constructed earlier freeing it here will
2062 * destroy other types... */
2063 if(iter->type != CONSTRUCT_FUNCTION) {
2073 static declaration_t *parse_declarator(
2074 const declaration_specifiers_t *specifiers,
2075 type_t *type, bool may_be_abstract)
2077 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2078 declaration->storage_class = specifiers->storage_class;
2079 declaration->is_inline = specifiers->is_inline;
2081 construct_type_t *construct_type
2082 = parse_inner_declarator(declaration, may_be_abstract);
2083 declaration->type = construct_declarator_type(construct_type, type);
2085 if(construct_type != NULL) {
2086 obstack_free(&temp_obst, construct_type);
2092 static type_t *parse_abstract_declarator(type_t *base_type)
2094 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2096 type_t *result = construct_declarator_type(construct_type, base_type);
2097 if(construct_type != NULL) {
2098 obstack_free(&temp_obst, construct_type);
2104 static declaration_t *record_declaration(declaration_t *declaration)
2106 assert(context != NULL);
2108 symbol_t *symbol = declaration->symbol;
2109 if(symbol != NULL) {
2110 declaration_t *alias = environment_push(declaration);
2111 if(alias != declaration)
2114 declaration->parent_context = context;
2117 if(last_declaration != NULL) {
2118 last_declaration->next = declaration;
2120 context->declarations = declaration;
2122 last_declaration = declaration;
2127 static void parser_error_multiple_definition(declaration_t *previous,
2128 declaration_t *declaration)
2130 parser_print_error_prefix_pos(declaration->source_position);
2131 fprintf(stderr, "multiple definition of symbol '%s'\n",
2132 declaration->symbol->string);
2133 parser_print_error_prefix_pos(previous->source_position);
2134 fprintf(stderr, "this is the location of the previous definition.\n");
2137 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2140 declaration_t *ndeclaration
2141 = parse_declarator(specifiers, specifiers->type, false);
2143 declaration_t *declaration = record_declaration(ndeclaration);
2145 type_t *orig_type = declaration->type;
2146 type_t *type = skip_typeref(orig_type);
2147 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2148 parser_print_warning_prefix_pos(declaration->source_position);
2149 fprintf(stderr, "variable '%s' declared 'inline'\n",
2150 declaration->symbol->string);
2153 if(token.type == '=') {
2156 /* TODO: check that this is an allowed type (no function type) */
2158 if(declaration->init.initializer != NULL) {
2159 parser_error_multiple_definition(declaration, ndeclaration);
2162 initializer_t *initializer = parse_initializer(type);
2164 if(type->type == TYPE_ARRAY && initializer != NULL) {
2165 assert(initializer->type == INITIALIZER_LIST);
2167 initializer_list_t *list = (initializer_list_t*) initializer;
2168 array_type_t *array_type = (array_type_t*) type;
2170 if(array_type->size == NULL) {
2171 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2173 cnst->expression.type = EXPR_CONST;
2174 cnst->expression.datatype = type_size_t;
2175 cnst->v.int_value = list->len;
2177 array_type->size = (expression_t*) cnst;
2182 ndeclaration->init.initializer = initializer;
2183 } else if(token.type == '{') {
2184 if(type->type != TYPE_FUNCTION) {
2185 parser_print_error_prefix();
2186 fprintf(stderr, "declarator '");
2187 print_type_ext(orig_type, declaration->symbol, NULL);
2188 fprintf(stderr, "' has a body but is not a function type.\n");
2193 if(declaration->init.statement != NULL) {
2194 parser_error_multiple_definition(declaration, ndeclaration);
2196 if(ndeclaration != declaration) {
2197 memcpy(&declaration->context, &ndeclaration->context,
2198 sizeof(declaration->context));
2201 int top = environment_top();
2202 context_t *last_context = context;
2203 set_context(&declaration->context);
2205 /* push function parameters */
2206 declaration_t *parameter = declaration->context.declarations;
2207 for( ; parameter != NULL; parameter = parameter->next) {
2208 environment_push(parameter);
2211 int label_stack_top = label_top();
2212 declaration_t *old_current_function = current_function;
2213 current_function = declaration;
2215 statement_t *statement = parse_compound_statement();
2217 assert(current_function == declaration);
2218 current_function = old_current_function;
2219 label_pop_to(label_stack_top);
2221 assert(context == &declaration->context);
2222 set_context(last_context);
2223 environment_pop_to(top);
2225 declaration->init.statement = statement;
2229 if(token.type != ',')
2236 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2239 if(token.type == ':') {
2241 parse_constant_expression();
2242 /* TODO (bitfields) */
2244 declaration_t *declaration
2245 = parse_declarator(specifiers, specifiers->type, true);
2247 /* TODO: check constraints for struct declarations */
2248 /* TODO: check for doubled fields */
2249 record_declaration(declaration);
2251 if(token.type == ':') {
2253 parse_constant_expression();
2254 /* TODO (bitfields) */
2258 if(token.type != ',')
2265 static void parse_compound_type_entries(void)
2269 while(token.type != '}' && token.type != T_EOF) {
2270 declaration_specifiers_t specifiers;
2271 memset(&specifiers, 0, sizeof(specifiers));
2272 parse_declaration_specifiers(&specifiers);
2274 parse_struct_declarators(&specifiers);
2276 if(token.type == T_EOF) {
2277 parse_error("unexpected error while parsing struct");
2282 static void parse_declaration(void)
2284 source_position_t source_position = token.source_position;
2286 declaration_specifiers_t specifiers;
2287 memset(&specifiers, 0, sizeof(specifiers));
2288 parse_declaration_specifiers(&specifiers);
2290 if(token.type == ';') {
2291 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2292 parse_warning_pos(source_position,
2293 "useless keyword in empty declaration");
2295 switch (specifiers.type->type) {
2296 case TYPE_COMPOUND_STRUCT:
2297 case TYPE_COMPOUND_UNION: {
2298 const compound_type_t *const comp_type =
2299 (const compound_type_t*)specifiers.type;
2300 if (comp_type->declaration->symbol == NULL) {
2301 parse_warning_pos(source_position,
2302 "unnamed struct/union that defines no instances");
2307 case TYPE_ENUM: break;
2310 parse_warning_pos(source_position, "empty declaration");
2316 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2318 declaration->type = specifiers.type;
2319 declaration->storage_class = specifiers.storage_class;
2320 declaration->source_position = source_position;
2321 record_declaration(declaration);
2324 parse_init_declarators(&specifiers);
2327 static type_t *parse_typename(void)
2329 declaration_specifiers_t specifiers;
2330 memset(&specifiers, 0, sizeof(specifiers));
2331 parse_declaration_specifiers(&specifiers);
2332 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2333 /* TODO: improve error message, user does probably not know what a
2334 * storage class is...
2336 parse_error("typename may not have a storage class");
2339 type_t *result = parse_abstract_declarator(specifiers.type);
2347 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2348 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2349 expression_t *left);
2351 typedef struct expression_parser_function_t expression_parser_function_t;
2352 struct expression_parser_function_t {
2353 unsigned precedence;
2354 parse_expression_function parser;
2355 unsigned infix_precedence;
2356 parse_expression_infix_function infix_parser;
2359 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2361 static expression_t *make_invalid_expression(void)
2363 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2364 expression->type = EXPR_INVALID;
2365 expression->source_position = token.source_position;
2369 static expression_t *expected_expression_error(void)
2371 parser_print_error_prefix();
2372 fprintf(stderr, "expected expression, got token ");
2373 print_token(stderr, & token);
2374 fprintf(stderr, "\n");
2378 return make_invalid_expression();
2381 static expression_t *parse_string_const(void)
2383 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2385 cnst->expression.type = EXPR_STRING_LITERAL;
2386 cnst->expression.datatype = type_string;
2387 cnst->value = parse_string_literals();
2389 return (expression_t*) cnst;
2392 static expression_t *parse_int_const(void)
2394 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2396 cnst->expression.type = EXPR_CONST;
2397 cnst->expression.datatype = token.datatype;
2398 cnst->v.int_value = token.v.intvalue;
2402 return (expression_t*) cnst;
2405 static expression_t *parse_float_const(void)
2407 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2409 cnst->expression.type = EXPR_CONST;
2410 cnst->expression.datatype = token.datatype;
2411 cnst->v.float_value = token.v.floatvalue;
2415 return (expression_t*) cnst;
2418 static declaration_t *create_implicit_function(symbol_t *symbol,
2419 const source_position_t source_position)
2421 function_type_t *function_type
2422 = allocate_type_zero(sizeof(function_type[0]));
2424 function_type->type.type = TYPE_FUNCTION;
2425 function_type->result_type = type_int;
2426 function_type->unspecified_parameters = true;
2428 type_t *type = typehash_insert((type_t*) function_type);
2429 if(type != (type_t*) function_type) {
2430 free_type(function_type);
2433 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2435 declaration->storage_class = STORAGE_CLASS_EXTERN;
2436 declaration->type = type;
2437 declaration->symbol = symbol;
2438 declaration->source_position = source_position;
2440 /* prepend the implicit definition to the global context
2441 * this is safe since the symbol wasn't declared as anything else yet
2443 assert(symbol->declaration == NULL);
2445 context_t *last_context = context;
2446 context = global_context;
2448 environment_push(declaration);
2449 declaration->next = context->declarations;
2450 context->declarations = declaration;
2452 context = last_context;
2457 static expression_t *parse_reference(void)
2459 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2461 ref->expression.type = EXPR_REFERENCE;
2462 ref->symbol = token.v.symbol;
2464 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2466 source_position_t source_position = token.source_position;
2469 if(declaration == NULL) {
2471 /* an implicitly defined function */
2472 if(token.type == '(') {
2473 parser_print_prefix_pos(token.source_position);
2474 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2475 ref->symbol->string);
2477 declaration = create_implicit_function(ref->symbol,
2482 parser_print_error_prefix();
2483 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2484 return (expression_t*) ref;
2488 ref->declaration = declaration;
2489 ref->expression.datatype = declaration->type;
2491 return (expression_t*) ref;
2494 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2498 /* TODO check if explicit cast is allowed and issue warnings/errors */
2501 static expression_t *parse_cast(void)
2503 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2505 cast->expression.type = EXPR_UNARY;
2506 cast->type = UNEXPR_CAST;
2507 cast->expression.source_position = token.source_position;
2509 type_t *type = parse_typename();
2512 expression_t *value = parse_sub_expression(20);
2514 check_cast_allowed(value, type);
2516 cast->expression.datatype = type;
2517 cast->value = value;
2519 return (expression_t*) cast;
2522 static expression_t *parse_statement_expression(void)
2524 statement_expression_t *expression
2525 = allocate_ast_zero(sizeof(expression[0]));
2526 expression->expression.type = EXPR_STATEMENT;
2528 statement_t *statement = parse_compound_statement();
2529 expression->statement = statement;
2530 if(statement == NULL) {
2535 assert(statement->type == STATEMENT_COMPOUND);
2536 compound_statement_t *compound_statement
2537 = (compound_statement_t*) statement;
2539 /* find last statement and use it's type */
2540 const statement_t *last_statement = NULL;
2541 const statement_t *iter = compound_statement->statements;
2542 for( ; iter != NULL; iter = iter->next) {
2543 last_statement = iter;
2546 if(last_statement->type == STATEMENT_EXPRESSION) {
2547 const expression_statement_t *expression_statement =
2548 (const expression_statement_t*) last_statement;
2549 expression->expression.datatype
2550 = expression_statement->expression->datatype;
2552 expression->expression.datatype = type_void;
2557 return (expression_t*) expression;
2560 static expression_t *parse_brace_expression(void)
2564 switch(token.type) {
2566 /* gcc extension: a stement expression */
2567 return parse_statement_expression();
2571 return parse_cast();
2573 if(is_typedef_symbol(token.v.symbol)) {
2574 return parse_cast();
2578 expression_t *result = parse_expression();
2584 static expression_t *parse_function_keyword(void)
2589 if (current_function == NULL) {
2590 parse_error("'__func__' used outside of a function");
2593 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2594 expression->expression.type = EXPR_FUNCTION;
2595 expression->expression.datatype = type_string;
2596 expression->value = "TODO: FUNCTION";
2598 return (expression_t*) expression;
2601 static expression_t *parse_pretty_function_keyword(void)
2603 eat(T___PRETTY_FUNCTION__);
2606 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2607 expression->expression.type = EXPR_PRETTY_FUNCTION;
2608 expression->expression.datatype = type_string;
2609 expression->value = "TODO: PRETTY FUNCTION";
2611 return (expression_t*) expression;
2614 static designator_t *parse_designator(void)
2616 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2618 if(token.type != T_IDENTIFIER) {
2619 parse_error_expected("while parsing member designator",
2624 result->symbol = token.v.symbol;
2627 designator_t *last_designator = result;
2629 if(token.type == '.') {
2631 if(token.type != T_IDENTIFIER) {
2632 parse_error_expected("while parsing member designator",
2637 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2638 designator->symbol = token.v.symbol;
2641 last_designator->next = designator;
2642 last_designator = designator;
2645 if(token.type == '[') {
2647 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2648 designator->array_access = parse_expression();
2649 if(designator->array_access == NULL) {
2655 last_designator->next = designator;
2656 last_designator = designator;
2665 static expression_t *parse_offsetof(void)
2667 eat(T___builtin_offsetof);
2669 offsetof_expression_t *expression
2670 = allocate_ast_zero(sizeof(expression[0]));
2671 expression->expression.type = EXPR_OFFSETOF;
2672 expression->expression.datatype = type_size_t;
2675 expression->type = parse_typename();
2677 expression->designator = parse_designator();
2680 return (expression_t*) expression;
2683 static expression_t *parse_va_arg(void)
2685 eat(T___builtin_va_arg);
2687 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2688 expression->expression.type = EXPR_VA_ARG;
2691 expression->arg = parse_assignment_expression();
2693 expression->expression.datatype = parse_typename();
2696 return (expression_t*) expression;
2699 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2701 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2702 parameter->type = argument_type;
2704 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2705 type->type.type = TYPE_FUNCTION;
2706 type->result_type = result_type;
2707 type->parameters = parameter;
2709 type_t *result = typehash_insert((type_t*) type);
2710 if(result != (type_t*) type) {
2717 static expression_t *parse_builtin_symbol(void)
2719 builtin_symbol_expression_t *expression
2720 = allocate_ast_zero(sizeof(expression[0]));
2721 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2723 expression->symbol = token.v.symbol;
2726 switch(token.type) {
2727 case T___builtin_alloca:
2728 type = make_function_1_type(type_void_ptr, type_size_t);
2734 expression->expression.datatype = type;
2735 return (expression_t*) expression;
2738 static expression_t *parse_primary_expression(void)
2740 switch(token.type) {
2742 return parse_int_const();
2743 case T_FLOATINGPOINT:
2744 return parse_float_const();
2745 case T_STRING_LITERAL:
2746 return parse_string_const();
2748 return parse_reference();
2749 case T___FUNCTION__:
2751 return parse_function_keyword();
2752 case T___PRETTY_FUNCTION__:
2753 return parse_pretty_function_keyword();
2754 case T___builtin_offsetof:
2755 return parse_offsetof();
2756 case T___builtin_va_arg:
2757 return parse_va_arg();
2758 case T___builtin_alloca:
2759 case T___builtin_expect:
2760 case T___builtin_va_start:
2761 case T___builtin_va_end:
2762 return parse_builtin_symbol();
2765 return parse_brace_expression();
2768 parser_print_error_prefix();
2769 fprintf(stderr, "unexpected token ");
2770 print_token(stderr, &token);
2771 fprintf(stderr, "\n");
2774 return make_invalid_expression();
2777 static expression_t *parse_array_expression(unsigned precedence,
2778 expression_t *array_ref)
2784 expression_t *index = parse_expression();
2786 array_access_expression_t *array_access
2787 = allocate_ast_zero(sizeof(array_access[0]));
2789 array_access->expression.type = EXPR_ARRAY_ACCESS;
2790 array_access->array_ref = array_ref;
2791 array_access->index = index;
2793 type_t *type_left = skip_typeref(array_ref->datatype);
2794 type_t *type_right = skip_typeref(index->datatype);
2796 if(type_left != NULL && type_right != NULL) {
2797 if(type_left->type == TYPE_POINTER) {
2798 pointer_type_t *pointer = (pointer_type_t*) type_left;
2799 array_access->expression.datatype = pointer->points_to;
2800 } else if(type_left->type == TYPE_ARRAY) {
2801 array_type_t *array_type = (array_type_t*) type_left;
2802 array_access->expression.datatype = array_type->element_type;
2803 } else if(type_right->type == TYPE_POINTER) {
2804 pointer_type_t *pointer = (pointer_type_t*) type_right;
2805 array_access->expression.datatype = pointer->points_to;
2806 } else if(type_right->type == TYPE_ARRAY) {
2807 array_type_t *array_type = (array_type_t*) type_right;
2808 array_access->expression.datatype = array_type->element_type;
2810 parser_print_error_prefix();
2811 fprintf(stderr, "array access on object with non-pointer types ");
2812 print_type_quoted(type_left);
2813 fprintf(stderr, ", ");
2814 print_type_quoted(type_right);
2815 fprintf(stderr, "\n");
2819 if(token.type != ']') {
2820 parse_error_expected("Problem while parsing array access", ']', 0);
2821 return (expression_t*) array_access;
2825 return (expression_t*) array_access;
2828 static bool is_declaration_specifier(const token_t *token,
2829 bool only_type_specifiers)
2831 switch(token->type) {
2835 return is_typedef_symbol(token->v.symbol);
2838 if(only_type_specifiers)
2847 static expression_t *parse_sizeof(unsigned precedence)
2851 sizeof_expression_t *sizeof_expression
2852 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2853 sizeof_expression->expression.type = EXPR_SIZEOF;
2854 sizeof_expression->expression.datatype = type_size_t;
2856 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2858 sizeof_expression->type = parse_typename();
2861 expression_t *expression = parse_sub_expression(precedence);
2862 sizeof_expression->type = expression->datatype;
2863 sizeof_expression->size_expression = expression;
2866 return (expression_t*) sizeof_expression;
2869 static expression_t *parse_select_expression(unsigned precedence,
2870 expression_t *compound)
2873 assert(token.type == '.' || token.type == T_MINUSGREATER);
2875 bool is_pointer = (token.type == T_MINUSGREATER);
2878 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2880 select->expression.type = EXPR_SELECT;
2881 select->compound = compound;
2883 if(token.type != T_IDENTIFIER) {
2884 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2885 return (expression_t*) select;
2887 symbol_t *symbol = token.v.symbol;
2888 select->symbol = symbol;
2891 type_t *orig_type = compound->datatype;
2892 if(orig_type == NULL)
2893 return make_invalid_expression();
2895 type_t *type = skip_typeref(orig_type);
2897 type_t *type_left = type;
2899 if(type->type != TYPE_POINTER) {
2900 parser_print_error_prefix();
2901 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2902 print_type_quoted(orig_type);
2903 fputc('\n', stderr);
2904 return make_invalid_expression();
2906 pointer_type_t *pointer_type = (pointer_type_t*) type;
2907 type_left = pointer_type->points_to;
2909 type_left = skip_typeref(type_left);
2911 if(type_left->type != TYPE_COMPOUND_STRUCT
2912 && type_left->type != TYPE_COMPOUND_UNION) {
2913 parser_print_error_prefix();
2914 fprintf(stderr, "request for member '%s' in something not a struct or "
2915 "union, but ", symbol->string);
2916 print_type_quoted(type_left);
2917 fputc('\n', stderr);
2918 return make_invalid_expression();
2921 compound_type_t *compound_type = (compound_type_t*) type_left;
2922 declaration_t *declaration = compound_type->declaration;
2924 if(!declaration->init.is_defined) {
2925 parser_print_error_prefix();
2926 fprintf(stderr, "request for member '%s' of incomplete type ",
2928 print_type_quoted(type_left);
2929 fputc('\n', stderr);
2930 return make_invalid_expression();
2933 declaration_t *iter = declaration->context.declarations;
2934 for( ; iter != NULL; iter = iter->next) {
2935 if(iter->symbol == symbol) {
2940 parser_print_error_prefix();
2941 print_type_quoted(type_left);
2942 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2943 return make_invalid_expression();
2946 select->compound_entry = iter;
2947 select->expression.datatype = iter->type;
2948 return (expression_t*) select;
2951 static expression_t *parse_call_expression(unsigned precedence,
2952 expression_t *expression)
2955 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2956 call->expression.type = EXPR_CALL;
2957 call->function = expression;
2959 function_type_t *function_type;
2960 type_t *orig_type = expression->datatype;
2961 type_t *type = skip_typeref(orig_type);
2963 if(type->type == TYPE_POINTER) {
2964 pointer_type_t *pointer_type = (pointer_type_t*) type;
2966 type = skip_typeref(pointer_type->points_to);
2968 if (type->type == TYPE_FUNCTION) {
2969 function_type = (function_type_t*) type;
2970 call->expression.datatype = function_type->result_type;
2972 parser_print_error_prefix();
2973 fputs("called object '", stderr);
2974 print_expression(expression);
2975 fputs("' (type ", stderr);
2976 print_type_quoted(orig_type);
2977 fputs(") is not a function\n", stderr);
2979 function_type = NULL;
2980 call->expression.datatype = NULL;
2983 /* parse arguments */
2986 if(token.type != ')') {
2987 call_argument_t *last_argument = NULL;
2990 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2992 argument->expression = parse_assignment_expression();
2993 if(last_argument == NULL) {
2994 call->arguments = argument;
2996 last_argument->next = argument;
2998 last_argument = argument;
3000 if(token.type != ',')
3007 if(function_type != NULL) {
3008 function_parameter_t *parameter = function_type->parameters;
3009 call_argument_t *argument = call->arguments;
3010 for( ; parameter != NULL && argument != NULL;
3011 parameter = parameter->next, argument = argument->next) {
3012 type_t *expected_type = parameter->type;
3013 /* TODO report context in error messages */
3014 argument->expression = create_implicit_cast(argument->expression,
3017 /* too few parameters */
3018 if(parameter != NULL) {
3019 parser_print_error_prefix();
3020 fprintf(stderr, "too few arguments to function '");
3021 print_expression(expression);
3022 fprintf(stderr, "'\n");
3023 } else if(argument != NULL) {
3024 /* too many parameters */
3025 if(!function_type->variadic
3026 && !function_type->unspecified_parameters) {
3027 parser_print_error_prefix();
3028 fprintf(stderr, "too many arguments to function '");
3029 print_expression(expression);
3030 fprintf(stderr, "'\n");
3032 /* do default promotion */
3033 for( ; argument != NULL; argument = argument->next) {
3034 type_t *type = argument->expression->datatype;
3039 if(is_type_integer(type)) {
3040 type = promote_integer(type);
3041 } else if(type == type_float) {
3044 argument->expression
3045 = create_implicit_cast(argument->expression, type);
3051 return (expression_t*) call;
3054 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3056 static expression_t *parse_conditional_expression(unsigned precedence,
3057 expression_t *expression)
3061 conditional_expression_t *conditional
3062 = allocate_ast_zero(sizeof(conditional[0]));
3063 conditional->expression.type = EXPR_CONDITIONAL;
3064 conditional->condition = expression;
3067 type_t *condition_type_orig = conditional->condition->datatype;
3068 if(condition_type_orig != NULL) {
3069 type_t *condition_type = skip_typeref(condition_type_orig);
3070 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3071 type_error("expected a scalar type", expression->source_position,
3072 condition_type_orig);
3076 expression_t *const t_expr = parse_expression();
3077 conditional->true_expression = t_expr;
3079 expression_t *const f_expr = parse_sub_expression(precedence);
3080 conditional->false_expression = f_expr;
3082 type_t *const true_type = t_expr->datatype;
3083 if(true_type == NULL)
3084 return (expression_t*) conditional;
3085 type_t *const false_type = f_expr->datatype;
3086 if(false_type == NULL)
3087 return (expression_t*) conditional;
3089 type_t *const skipped_true_type = skip_typeref(true_type);
3090 type_t *const skipped_false_type = skip_typeref(false_type);
3093 if (skipped_true_type == skipped_false_type) {
3094 conditional->expression.datatype = skipped_true_type;
3095 } else if (is_type_arithmetic(skipped_true_type) &&
3096 is_type_arithmetic(skipped_false_type)) {
3097 type_t *const result = semantic_arithmetic(skipped_true_type,
3098 skipped_false_type);
3099 conditional->true_expression = create_implicit_cast(t_expr, result);
3100 conditional->false_expression = create_implicit_cast(f_expr, result);
3101 conditional->expression.datatype = result;
3102 } else if (skipped_true_type->type == TYPE_POINTER &&
3103 skipped_false_type->type == TYPE_POINTER &&
3104 true /* TODO compatible points_to types */) {
3106 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3107 skipped_false_type->type == TYPE_POINTER)
3108 || (is_null_ptr_const(skipped_false_type) &&
3109 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3111 } else if(/* 1 is pointer to object type, other is void* */ false) {
3114 type_error_incompatible("while parsing conditional",
3115 expression->source_position, true_type,
3116 skipped_false_type);
3119 return (expression_t*) conditional;
3122 static expression_t *parse_extension(unsigned precedence)
3124 eat(T___extension__);
3126 /* TODO enable extensions */
3128 return parse_sub_expression(precedence);
3131 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3133 eat(T___builtin_classify_type);
3135 classify_type_expression_t *const classify_type_expr =
3136 allocate_ast_zero(sizeof(classify_type_expr[0]));
3137 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3138 classify_type_expr->expression.datatype = type_int;
3141 expression_t *const expression = parse_sub_expression(precedence);
3143 classify_type_expr->type_expression = expression;
3145 return (expression_t*)classify_type_expr;
3148 static void semantic_incdec(unary_expression_t *expression)
3150 type_t *orig_type = expression->value->datatype;
3151 if(orig_type == NULL)
3154 type_t *type = skip_typeref(orig_type);
3155 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3156 /* TODO: improve error message */
3157 parser_print_error_prefix();
3158 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3162 expression->expression.datatype = orig_type;
3165 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3167 type_t *orig_type = expression->value->datatype;
3168 if(orig_type == NULL)
3171 type_t *type = skip_typeref(orig_type);
3172 if(!is_type_arithmetic(type)) {
3173 /* TODO: improve error message */
3174 parser_print_error_prefix();
3175 fprintf(stderr, "operation needs an arithmetic type\n");
3179 expression->expression.datatype = orig_type;
3182 static void semantic_unexpr_scalar(unary_expression_t *expression)
3184 type_t *orig_type = expression->value->datatype;
3185 if(orig_type == NULL)
3188 type_t *type = skip_typeref(orig_type);
3189 if (!is_type_scalar(type)) {
3190 parse_error("operand of ! must be of scalar type\n");
3194 expression->expression.datatype = orig_type;
3197 static void semantic_unexpr_integer(unary_expression_t *expression)
3199 type_t *orig_type = expression->value->datatype;
3200 if(orig_type == NULL)
3203 type_t *type = skip_typeref(orig_type);
3204 if (!is_type_integer(type)) {
3205 parse_error("operand of ~ must be of integer type\n");
3209 expression->expression.datatype = orig_type;
3212 static void semantic_dereference(unary_expression_t *expression)
3214 type_t *orig_type = expression->value->datatype;
3215 if(orig_type == NULL)
3218 type_t *type = skip_typeref(orig_type);
3219 switch (type->type) {
3221 array_type_t *const array_type = (array_type_t*)type;
3222 expression->expression.datatype = array_type->element_type;
3226 case TYPE_POINTER: {
3227 pointer_type_t *pointer_type = (pointer_type_t*)type;
3228 expression->expression.datatype = pointer_type->points_to;
3233 parser_print_error_prefix();
3234 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3235 print_type_quoted(orig_type);
3236 fputs(" given.\n", stderr);
3241 static void semantic_take_addr(unary_expression_t *expression)
3243 type_t *orig_type = expression->value->datatype;
3244 if(orig_type == NULL)
3247 expression_t *value = expression->value;
3248 if(value->type == EXPR_REFERENCE) {
3249 reference_expression_t *reference = (reference_expression_t*) value;
3250 declaration_t *declaration = reference->declaration;
3251 if(declaration != NULL) {
3252 declaration->address_taken = 1;
3256 expression->expression.datatype = make_pointer_type(orig_type, 0);
3259 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3260 static expression_t *parse_##unexpression_type(unsigned precedence) \
3264 unary_expression_t *unary_expression \
3265 = allocate_ast_zero(sizeof(unary_expression[0])); \
3266 unary_expression->expression.type = EXPR_UNARY; \
3267 unary_expression->type = unexpression_type; \
3268 unary_expression->value = parse_sub_expression(precedence); \
3270 sfunc(unary_expression); \
3272 return (expression_t*) unary_expression; \
3275 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3276 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3277 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3278 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3279 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3280 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3281 semantic_unexpr_integer)
3282 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3284 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3287 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3289 static expression_t *parse_##unexpression_type(unsigned precedence, \
3290 expression_t *left) \
3292 (void) precedence; \
3295 unary_expression_t *unary_expression \
3296 = allocate_ast_zero(sizeof(unary_expression[0])); \
3297 unary_expression->expression.type = EXPR_UNARY; \
3298 unary_expression->type = unexpression_type; \
3299 unary_expression->value = left; \
3301 sfunc(unary_expression); \
3303 return (expression_t*) unary_expression; \
3306 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3308 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3311 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3313 /* TODO: handle complex + imaginary types */
3315 /* § 6.3.1.8 Usual arithmetic conversions */
3316 if(type_left == type_long_double || type_right == type_long_double) {
3317 return type_long_double;
3318 } else if(type_left == type_double || type_right == type_double) {
3320 } else if(type_left == type_float || type_right == type_float) {
3324 type_right = promote_integer(type_right);
3325 type_left = promote_integer(type_left);
3327 if(type_left == type_right)
3330 bool signed_left = is_type_signed(type_left);
3331 bool signed_right = is_type_signed(type_right);
3332 if(get_rank(type_left) < get_rank(type_right)) {
3333 if(signed_left == signed_right || !signed_right) {
3339 if(signed_left == signed_right || !signed_left) {
3347 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3349 expression_t *left = expression->left;
3350 expression_t *right = expression->right;
3351 type_t *orig_type_left = left->datatype;
3352 type_t *orig_type_right = right->datatype;
3354 if(orig_type_left == NULL || orig_type_right == NULL)
3357 type_t *type_left = skip_typeref(orig_type_left);
3358 type_t *type_right = skip_typeref(orig_type_right);
3360 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3361 /* TODO: improve error message */
3362 parser_print_error_prefix();
3363 fprintf(stderr, "operation needs arithmetic types\n");
3367 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3368 expression->left = create_implicit_cast(left, arithmetic_type);
3369 expression->right = create_implicit_cast(right, arithmetic_type);
3370 expression->expression.datatype = arithmetic_type;
3373 static void semantic_shift_op(binary_expression_t *expression)
3375 expression_t *left = expression->left;
3376 expression_t *right = expression->right;
3377 type_t *orig_type_left = left->datatype;
3378 type_t *orig_type_right = right->datatype;
3380 if(orig_type_left == NULL || orig_type_right == NULL)
3383 type_t *type_left = skip_typeref(orig_type_left);
3384 type_t *type_right = skip_typeref(orig_type_right);
3386 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3387 /* TODO: improve error message */
3388 parser_print_error_prefix();
3389 fprintf(stderr, "operation needs integer types\n");
3393 type_left = promote_integer(type_left);
3394 type_right = promote_integer(type_right);
3396 expression->left = create_implicit_cast(left, type_left);
3397 expression->right = create_implicit_cast(right, type_right);
3398 expression->expression.datatype = type_left;
3401 static void semantic_add(binary_expression_t *expression)
3403 expression_t *left = expression->left;
3404 expression_t *right = expression->right;
3405 type_t *orig_type_left = left->datatype;
3406 type_t *orig_type_right = right->datatype;
3408 if(orig_type_left == NULL || orig_type_right == NULL)
3411 type_t *type_left = skip_typeref(orig_type_left);
3412 type_t *type_right = skip_typeref(orig_type_right);
3415 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3416 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3417 expression->left = create_implicit_cast(left, arithmetic_type);
3418 expression->right = create_implicit_cast(right, arithmetic_type);
3419 expression->expression.datatype = arithmetic_type;
3421 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3422 expression->expression.datatype = type_left;
3423 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3424 expression->expression.datatype = type_right;
3425 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3426 const array_type_t *const arr_type = (const array_type_t*)type_left;
3427 expression->expression.datatype =
3428 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3429 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3430 const array_type_t *const arr_type = (const array_type_t*)type_right;
3431 expression->expression.datatype =
3432 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3434 parser_print_error_prefix();
3435 fprintf(stderr, "invalid operands to binary + (");
3436 print_type_quoted(orig_type_left);
3437 fprintf(stderr, ", ");
3438 print_type_quoted(orig_type_right);
3439 fprintf(stderr, ")\n");
3443 static void semantic_sub(binary_expression_t *expression)
3445 expression_t *left = expression->left;
3446 expression_t *right = expression->right;
3447 type_t *orig_type_left = left->datatype;
3448 type_t *orig_type_right = right->datatype;
3450 if(orig_type_left == NULL || orig_type_right == NULL)
3453 type_t *type_left = skip_typeref(orig_type_left);
3454 type_t *type_right = skip_typeref(orig_type_right);
3457 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3458 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3459 expression->left = create_implicit_cast(left, arithmetic_type);
3460 expression->right = create_implicit_cast(right, arithmetic_type);
3461 expression->expression.datatype = arithmetic_type;
3463 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3464 expression->expression.datatype = type_left;
3465 } else if(type_left->type == TYPE_POINTER &&
3466 type_right->type == TYPE_POINTER) {
3467 if(!pointers_compatible(type_left, type_right)) {
3468 parser_print_error_prefix();
3469 fprintf(stderr, "pointers to incompatible objects to binary - (");
3470 print_type_quoted(orig_type_left);
3471 fprintf(stderr, ", ");
3472 print_type_quoted(orig_type_right);
3473 fprintf(stderr, ")\n");
3475 expression->expression.datatype = type_ptrdiff_t;
3478 parser_print_error_prefix();
3479 fprintf(stderr, "invalid operands to binary - (");
3480 print_type_quoted(orig_type_left);
3481 fprintf(stderr, ", ");
3482 print_type_quoted(orig_type_right);
3483 fprintf(stderr, ")\n");
3487 static void semantic_comparison(binary_expression_t *expression)
3489 expression_t *left = expression->left;
3490 expression_t *right = expression->right;
3491 type_t *orig_type_left = left->datatype;
3492 type_t *orig_type_right = right->datatype;
3494 if(orig_type_left == NULL || orig_type_right == NULL)
3497 type_t *type_left = skip_typeref(orig_type_left);
3498 type_t *type_right = skip_typeref(orig_type_right);
3500 /* TODO non-arithmetic types */
3501 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3502 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3503 expression->left = create_implicit_cast(left, arithmetic_type);
3504 expression->right = create_implicit_cast(right, arithmetic_type);
3505 expression->expression.datatype = arithmetic_type;
3506 } else if (type_left->type == TYPE_POINTER &&
3507 type_right->type == TYPE_POINTER) {
3508 /* TODO check compatibility */
3509 } else if (type_left->type == TYPE_POINTER) {
3510 expression->right = create_implicit_cast(right, type_left);
3511 } else if (type_right->type == TYPE_POINTER) {
3512 expression->left = create_implicit_cast(left, type_right);
3514 type_error_incompatible("invalid operands in comparison",
3515 token.source_position, type_left, type_right);
3517 expression->expression.datatype = type_int;
3520 static void semantic_arithmetic_assign(binary_expression_t *expression)
3522 expression_t *left = expression->left;
3523 expression_t *right = expression->right;
3524 type_t *orig_type_left = left->datatype;
3525 type_t *orig_type_right = right->datatype;
3527 if(orig_type_left == NULL || orig_type_right == NULL)
3530 type_t *type_left = skip_typeref(orig_type_left);
3531 type_t *type_right = skip_typeref(orig_type_right);
3533 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3534 /* TODO: improve error message */
3535 parser_print_error_prefix();
3536 fprintf(stderr, "operation needs arithmetic types\n");
3540 /* combined instructions are tricky. We can't create an implicit cast on
3541 * the left side, because we need the uncasted form for the store.
3542 * The ast2firm pass has to know that left_type must be right_type
3543 * for the arithmeitc operation and create a cast by itself */
3544 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3545 expression->right = create_implicit_cast(right, arithmetic_type);
3546 expression->expression.datatype = type_left;
3549 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3551 expression_t *left = expression->left;
3552 expression_t *right = expression->right;
3553 type_t *orig_type_left = left->datatype;
3554 type_t *orig_type_right = right->datatype;
3556 if(orig_type_left == NULL || orig_type_right == NULL)
3559 type_t *type_left = skip_typeref(orig_type_left);
3560 type_t *type_right = skip_typeref(orig_type_right);
3562 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3563 /* combined instructions are tricky. We can't create an implicit cast on
3564 * the left side, because we need the uncasted form for the store.
3565 * The ast2firm pass has to know that left_type must be right_type
3566 * for the arithmeitc operation and create a cast by itself */
3567 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3568 expression->right = create_implicit_cast(right, arithmetic_type);
3569 expression->expression.datatype = type_left;
3570 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3571 expression->expression.datatype = type_left;
3573 parser_print_error_prefix();
3574 fputs("Incompatible types ", stderr);
3575 print_type_quoted(orig_type_left);
3576 fputs(" and ", stderr);
3577 print_type_quoted(orig_type_right);
3578 fputs(" in assignment\n", stderr);
3583 static void semantic_logical_op(binary_expression_t *expression)
3585 expression_t *left = expression->left;
3586 expression_t *right = expression->right;
3587 type_t *orig_type_left = left->datatype;
3588 type_t *orig_type_right = right->datatype;
3590 if(orig_type_left == NULL || orig_type_right == NULL)
3593 type_t *type_left = skip_typeref(orig_type_left);
3594 type_t *type_right = skip_typeref(orig_type_right);
3596 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3597 /* TODO: improve error message */
3598 parser_print_error_prefix();
3599 fprintf(stderr, "operation needs scalar types\n");
3603 expression->expression.datatype = type_int;
3606 static void semantic_binexpr_assign(binary_expression_t *expression)
3608 expression_t *left = expression->left;
3609 type_t *type_left = left->datatype;
3611 if(type_left == NULL)
3614 if (type_left->type == TYPE_ARRAY) {
3615 parse_error("Cannot assign to arrays.");
3616 } else if (type_left != NULL) {
3617 semantic_assign(type_left, &expression->right, "assignment");
3620 expression->expression.datatype = type_left;
3623 static void semantic_comma(binary_expression_t *expression)
3625 expression->expression.datatype = expression->right->datatype;
3628 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3629 static expression_t *parse_##binexpression_type(unsigned precedence, \
3630 expression_t *left) \
3634 expression_t *right = parse_sub_expression(precedence + lr); \
3636 binary_expression_t *binexpr \
3637 = allocate_ast_zero(sizeof(binexpr[0])); \
3638 binexpr->expression.type = EXPR_BINARY; \
3639 binexpr->type = binexpression_type; \
3640 binexpr->left = left; \
3641 binexpr->right = right; \
3644 return (expression_t*) binexpr; \
3647 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3648 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3649 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3650 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3651 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3652 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3653 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3654 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3655 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3656 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3657 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3658 semantic_comparison, 1)
3659 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3660 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3661 semantic_comparison, 1)
3662 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3663 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3664 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3665 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3666 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3667 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3668 semantic_shift_op, 1)
3669 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3670 semantic_shift_op, 1)
3671 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3672 semantic_arithmetic_addsubb_assign, 0)
3673 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3674 semantic_arithmetic_addsubb_assign, 0)
3675 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3676 semantic_arithmetic_assign, 0)
3677 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3678 semantic_arithmetic_assign, 0)
3679 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3680 semantic_arithmetic_assign, 0)
3681 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3682 semantic_arithmetic_assign, 0)
3683 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3684 semantic_arithmetic_assign, 0)
3685 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3686 semantic_arithmetic_assign, 0)
3687 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3688 semantic_arithmetic_assign, 0)
3689 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3690 semantic_arithmetic_assign, 0)
3692 static expression_t *parse_sub_expression(unsigned precedence)
3694 if(token.type < 0) {
3695 return expected_expression_error();
3698 expression_parser_function_t *parser
3699 = &expression_parsers[token.type];
3700 source_position_t source_position = token.source_position;
3703 if(parser->parser != NULL) {
3704 left = parser->parser(parser->precedence);
3706 left = parse_primary_expression();
3708 assert(left != NULL);
3709 left->source_position = source_position;
3712 if(token.type < 0) {
3713 return expected_expression_error();
3716 parser = &expression_parsers[token.type];
3717 if(parser->infix_parser == NULL)
3719 if(parser->infix_precedence < precedence)
3722 left = parser->infix_parser(parser->infix_precedence, left);
3724 assert(left != NULL);
3725 assert(left->type != EXPR_UNKNOWN);
3726 left->source_position = source_position;
3732 static expression_t *parse_expression(void)
3734 return parse_sub_expression(1);
3739 static void register_expression_parser(parse_expression_function parser,
3740 int token_type, unsigned precedence)
3742 expression_parser_function_t *entry = &expression_parsers[token_type];
3744 if(entry->parser != NULL) {
3745 fprintf(stderr, "for token ");
3746 print_token_type(stderr, token_type);
3747 fprintf(stderr, "\n");
3748 panic("trying to register multiple expression parsers for a token");
3750 entry->parser = parser;
3751 entry->precedence = precedence;
3754 static void register_expression_infix_parser(
3755 parse_expression_infix_function parser, int token_type,
3756 unsigned precedence)
3758 expression_parser_function_t *entry = &expression_parsers[token_type];
3760 if(entry->infix_parser != NULL) {
3761 fprintf(stderr, "for token ");
3762 print_token_type(stderr, token_type);
3763 fprintf(stderr, "\n");
3764 panic("trying to register multiple infix expression parsers for a "
3767 entry->infix_parser = parser;
3768 entry->infix_precedence = precedence;
3771 static void init_expression_parsers(void)
3773 memset(&expression_parsers, 0, sizeof(expression_parsers));
3775 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3776 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3777 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3778 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3779 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3780 T_GREATERGREATER, 16);
3781 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3782 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3783 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3784 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3785 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3786 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3787 T_GREATEREQUAL, 14);
3788 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3789 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3790 T_EXCLAMATIONMARKEQUAL, 13);
3791 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3792 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3793 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3794 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3795 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3796 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3797 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3798 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3799 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3800 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3801 T_ASTERISKEQUAL, 2);
3802 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3803 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3805 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3806 T_LESSLESSEQUAL, 2);
3807 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3808 T_GREATERGREATEREQUAL, 2);
3809 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3811 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3813 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3816 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3818 register_expression_infix_parser(parse_array_expression, '[', 30);
3819 register_expression_infix_parser(parse_call_expression, '(', 30);
3820 register_expression_infix_parser(parse_select_expression, '.', 30);
3821 register_expression_infix_parser(parse_select_expression,
3822 T_MINUSGREATER, 30);
3823 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3825 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3828 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3829 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3830 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3831 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3832 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3833 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3834 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3835 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3836 register_expression_parser(parse_sizeof, T_sizeof, 25);
3837 register_expression_parser(parse_extension, T___extension__, 25);
3838 register_expression_parser(parse_builtin_classify_type,
3839 T___builtin_classify_type, 25);
3843 static statement_t *parse_case_statement(void)
3846 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3847 label->statement.type = STATEMENT_CASE_LABEL;
3848 label->statement.source_position = token.source_position;
3850 label->expression = parse_expression();
3853 label->label_statement = parse_statement();
3855 return (statement_t*) label;
3858 static statement_t *parse_default_statement(void)
3862 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3863 label->statement.type = STATEMENT_CASE_LABEL;
3864 label->statement.source_position = token.source_position;
3867 label->label_statement = parse_statement();
3869 return (statement_t*) label;
3872 static declaration_t *get_label(symbol_t *symbol)
3874 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3875 assert(current_function != NULL);
3876 /* if we found a label in the same function, then we already created the
3878 if(candidate != NULL
3879 && candidate->parent_context == ¤t_function->context) {
3883 /* otherwise we need to create a new one */
3884 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3885 declaration->namespc = NAMESPACE_LABEL;
3886 declaration->symbol = symbol;
3888 label_push(declaration);
3893 static statement_t *parse_label_statement(void)
3895 assert(token.type == T_IDENTIFIER);
3896 symbol_t *symbol = token.v.symbol;
3899 declaration_t *label = get_label(symbol);
3901 /* if source position is already set then the label is defined twice,
3902 * otherwise it was just mentioned in a goto so far */
3903 if(label->source_position.input_name != NULL) {
3904 parser_print_error_prefix();
3905 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3906 parser_print_error_prefix_pos(label->source_position);
3907 fprintf(stderr, "previous definition of '%s' was here\n",
3910 label->source_position = token.source_position;
3913 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3915 label_statement->statement.type = STATEMENT_LABEL;
3916 label_statement->statement.source_position = token.source_position;
3917 label_statement->label = label;
3921 if(token.type == '}') {
3922 parse_error("label at end of compound statement");
3923 return (statement_t*) label_statement;
3925 label_statement->label_statement = parse_statement();
3928 return (statement_t*) label_statement;
3931 static statement_t *parse_if(void)
3935 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3936 statement->statement.type = STATEMENT_IF;
3937 statement->statement.source_position = token.source_position;
3940 statement->condition = parse_expression();
3943 statement->true_statement = parse_statement();
3944 if(token.type == T_else) {
3946 statement->false_statement = parse_statement();
3949 return (statement_t*) statement;
3952 static statement_t *parse_switch(void)
3956 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3957 statement->statement.type = STATEMENT_SWITCH;
3958 statement->statement.source_position = token.source_position;
3961 statement->expression = parse_expression();
3963 statement->body = parse_statement();
3965 return (statement_t*) statement;
3968 static statement_t *parse_while(void)
3972 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3973 statement->statement.type = STATEMENT_WHILE;
3974 statement->statement.source_position = token.source_position;
3977 statement->condition = parse_expression();
3979 statement->body = parse_statement();
3981 return (statement_t*) statement;
3984 static statement_t *parse_do(void)
3988 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3989 statement->statement.type = STATEMENT_DO_WHILE;
3990 statement->statement.source_position = token.source_position;
3992 statement->body = parse_statement();
3995 statement->condition = parse_expression();
3999 return (statement_t*) statement;
4002 static statement_t *parse_for(void)
4006 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4007 statement->statement.type = STATEMENT_FOR;
4008 statement->statement.source_position = token.source_position;
4012 int top = environment_top();
4013 context_t *last_context = context;
4014 set_context(&statement->context);
4016 if(token.type != ';') {
4017 if(is_declaration_specifier(&token, false)) {
4018 parse_declaration();
4020 statement->initialisation = parse_expression();
4027 if(token.type != ';') {
4028 statement->condition = parse_expression();
4031 if(token.type != ')') {
4032 statement->step = parse_expression();
4035 statement->body = parse_statement();
4037 assert(context == &statement->context);
4038 set_context(last_context);
4039 environment_pop_to(top);
4041 return (statement_t*) statement;
4044 static statement_t *parse_goto(void)
4048 if(token.type != T_IDENTIFIER) {
4049 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4053 symbol_t *symbol = token.v.symbol;
4056 declaration_t *label = get_label(symbol);
4058 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4060 statement->statement.type = STATEMENT_GOTO;
4061 statement->statement.source_position = token.source_position;
4063 statement->label = label;
4067 return (statement_t*) statement;
4070 static statement_t *parse_continue(void)
4075 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4076 statement->type = STATEMENT_CONTINUE;
4077 statement->source_position = token.source_position;
4082 static statement_t *parse_break(void)
4087 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4088 statement->type = STATEMENT_BREAK;
4089 statement->source_position = token.source_position;
4094 static statement_t *parse_return(void)
4098 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4100 statement->statement.type = STATEMENT_RETURN;
4101 statement->statement.source_position = token.source_position;
4103 assert(current_function->type->type == TYPE_FUNCTION);
4104 function_type_t *function_type = (function_type_t*) current_function->type;
4105 type_t *return_type = function_type->result_type;
4107 expression_t *return_value;
4108 if(token.type != ';') {
4109 return_value = parse_expression();
4111 if(return_type == type_void && return_value->datatype != type_void) {
4112 parse_warning("'return' with a value, in function returning void");
4113 return_value = NULL;
4115 if(return_type != NULL) {
4116 semantic_assign(return_type, &return_value, "'return'");
4120 return_value = NULL;
4121 if(return_type != type_void) {
4122 parse_warning("'return' without value, in function returning "
4126 statement->return_value = return_value;
4130 return (statement_t*) statement;
4133 static statement_t *parse_declaration_statement(void)
4135 declaration_t *before = last_declaration;
4137 declaration_statement_t *statement
4138 = allocate_ast_zero(sizeof(statement[0]));
4139 statement->statement.type = STATEMENT_DECLARATION;
4140 statement->statement.source_position = token.source_position;
4142 declaration_specifiers_t specifiers;
4143 memset(&specifiers, 0, sizeof(specifiers));
4144 parse_declaration_specifiers(&specifiers);
4146 if(token.type == ';') {
4149 parse_init_declarators(&specifiers);
4152 if(before == NULL) {
4153 statement->declarations_begin = context->declarations;
4155 statement->declarations_begin = before->next;
4157 statement->declarations_end = last_declaration;
4159 return (statement_t*) statement;
4162 static statement_t *parse_expression_statement(void)
4164 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4165 statement->statement.type = STATEMENT_EXPRESSION;
4166 statement->statement.source_position = token.source_position;
4168 statement->expression = parse_expression();
4172 return (statement_t*) statement;
4175 static statement_t *parse_statement(void)
4177 statement_t *statement = NULL;
4179 /* declaration or statement */
4180 switch(token.type) {
4182 statement = parse_case_statement();
4186 statement = parse_default_statement();
4190 statement = parse_compound_statement();
4194 statement = parse_if();
4198 statement = parse_switch();
4202 statement = parse_while();
4206 statement = parse_do();
4210 statement = parse_for();
4214 statement = parse_goto();
4218 statement = parse_continue();
4222 statement = parse_break();
4226 statement = parse_return();
4235 if(look_ahead(1)->type == ':') {
4236 statement = parse_label_statement();
4240 if(is_typedef_symbol(token.v.symbol)) {
4241 statement = parse_declaration_statement();
4245 statement = parse_expression_statement();
4248 case T___extension__:
4249 /* this can be a prefix to a declaration or an expression statement */
4250 /* we simply eat it now and parse the rest with tail recursion */
4253 } while(token.type == T___extension__);
4254 statement = parse_statement();
4258 statement = parse_declaration_statement();
4262 statement = parse_expression_statement();
4266 assert(statement == NULL || statement->source_position.input_name != NULL);
4271 static statement_t *parse_compound_statement(void)
4273 compound_statement_t *compound_statement
4274 = allocate_ast_zero(sizeof(compound_statement[0]));
4275 compound_statement->statement.type = STATEMENT_COMPOUND;
4276 compound_statement->statement.source_position = token.source_position;
4280 int top = environment_top();
4281 context_t *last_context = context;
4282 set_context(&compound_statement->context);
4284 statement_t *last_statement = NULL;
4286 while(token.type != '}' && token.type != T_EOF) {
4287 statement_t *statement = parse_statement();
4288 if(statement == NULL)
4291 if(last_statement != NULL) {
4292 last_statement->next = statement;
4294 compound_statement->statements = statement;
4297 while(statement->next != NULL)
4298 statement = statement->next;
4300 last_statement = statement;
4303 if(token.type != '}') {
4304 parser_print_error_prefix_pos(
4305 compound_statement->statement.source_position);
4306 fprintf(stderr, "end of file while looking for closing '}'\n");
4310 assert(context == &compound_statement->context);
4311 set_context(last_context);
4312 environment_pop_to(top);
4314 return (statement_t*) compound_statement;
4317 static translation_unit_t *parse_translation_unit(void)
4319 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4321 assert(global_context == NULL);
4322 global_context = &unit->context;
4324 assert(context == NULL);
4325 set_context(&unit->context);
4327 while(token.type != T_EOF) {
4328 parse_declaration();
4331 assert(context == &unit->context);
4333 last_declaration = NULL;
4335 assert(global_context == &unit->context);
4336 global_context = NULL;
4341 translation_unit_t *parse(void)
4343 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4344 label_stack = NEW_ARR_F(stack_entry_t, 0);
4345 found_error = false;
4347 type_set_output(stderr);
4348 ast_set_output(stderr);
4350 lookahead_bufpos = 0;
4351 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4354 translation_unit_t *unit = parse_translation_unit();
4356 DEL_ARR_F(environment_stack);
4357 DEL_ARR_F(label_stack);
4365 void init_parser(void)
4367 init_expression_parsers();
4368 obstack_init(&temp_obst);
4370 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4371 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4372 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4373 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4374 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4375 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4376 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4377 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4378 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4379 type_void_ptr = make_pointer_type(type_void, 0);
4380 type_string = make_pointer_type(type_const_char, 0);
4383 void exit_parser(void)
4385 obstack_free(&temp_obst, NULL);