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_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
75 #define COMPLEX_SPECIFIERS
78 #ifdef PROVIDE_IMAGINARY
79 #define IMAGINARY_SPECIFIERS \
82 #define IMAGINARY_SPECIFIERS
85 #define TYPE_SPECIFIERS \
103 #define DECLARATION_START \
108 #define TYPENAME_START \
112 static inline void *allocate_ast_zero(size_t size)
114 void *res = allocate_ast(size);
115 memset(res, 0, size);
119 static inline void *allocate_type_zero(size_t size)
121 void *res = obstack_alloc(type_obst, size);
122 memset(res, 0, size);
126 static inline void free_type(void *type)
128 obstack_free(type_obst, type);
132 * returns the top element of the environment stack
134 static inline size_t environment_top(void)
136 return ARR_LEN(environment_stack);
139 static inline size_t label_top(void)
141 return ARR_LEN(label_stack);
146 static inline void next_token(void)
148 token = lookahead_buffer[lookahead_bufpos];
149 lookahead_buffer[lookahead_bufpos] = lexer_token;
152 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
155 print_token(stderr, &token);
156 fprintf(stderr, "\n");
160 static inline const token_t *look_ahead(int num)
162 assert(num > 0 && num <= MAX_LOOKAHEAD);
163 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
164 return & lookahead_buffer[pos];
167 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
169 static void error(void)
172 #ifdef ABORT_ON_ERROR
177 static void parser_print_prefix_pos(const source_position_t source_position)
179 fputs(source_position.input_name, stderr);
181 fprintf(stderr, "%d", source_position.linenr);
185 static void parser_print_error_prefix_pos(
186 const source_position_t source_position)
188 parser_print_prefix_pos(source_position);
189 fputs("error: ", stderr);
193 static void parser_print_error_prefix(void)
195 parser_print_error_prefix_pos(token.source_position);
198 static void parse_error(const char *message)
200 parser_print_error_prefix();
201 fprintf(stderr, "parse error: %s\n", message);
204 static void parser_print_warning_prefix_pos(
205 const source_position_t source_position)
207 parser_print_prefix_pos(source_position);
208 fputs("warning: ", stderr);
211 static void parse_warning_pos(const source_position_t source_position,
212 const char *const message)
214 parser_print_prefix_pos(source_position);
215 fprintf(stderr, "warning: %s\n", message);
218 static void parse_warning(const char *message)
220 parse_warning_pos(token.source_position, message);
223 static void parse_error_expected(const char *message, ...)
228 if(message != NULL) {
229 parser_print_error_prefix();
230 fprintf(stderr, "%s\n", message);
232 parser_print_error_prefix();
233 fputs("Parse error: got ", stderr);
234 print_token(stderr, &token);
235 fputs(", expected ", stderr);
237 va_start(args, message);
238 token_type_t token_type = va_arg(args, token_type_t);
239 while(token_type != 0) {
243 fprintf(stderr, ", ");
245 print_token_type(stderr, token_type);
246 token_type = va_arg(args, token_type_t);
249 fprintf(stderr, "\n");
252 static void print_type_quoted(type_t *type)
259 static void type_error(const char *msg, const source_position_t source_position,
262 parser_print_error_prefix_pos(source_position);
263 fprintf(stderr, "%s, but found type ", msg);
264 print_type_quoted(type);
268 static void type_error_incompatible(const char *msg,
269 const source_position_t source_position, type_t *type1, type_t *type2)
271 parser_print_error_prefix_pos(source_position);
272 fprintf(stderr, "%s, incompatible types: ", msg);
273 print_type_quoted(type1);
274 fprintf(stderr, " - ");
275 print_type_quoted(type2);
276 fprintf(stderr, ")\n");
279 static void eat_block(void)
281 if(token.type == '{')
284 while(token.type != '}') {
285 if(token.type == T_EOF)
287 if(token.type == '{') {
296 static void eat_statement(void)
298 while(token.type != ';') {
299 if(token.type == T_EOF)
301 if(token.type == '}')
303 if(token.type == '{') {
312 static void eat_brace(void)
314 if(token.type == '(')
317 while(token.type != ')') {
318 if(token.type == T_EOF)
320 if(token.type == ')' || token.type == ';' || token.type == '}') {
323 if(token.type == '(') {
327 if(token.type == '{') {
336 #define expect(expected) \
337 if(UNLIKELY(token.type != (expected))) { \
338 parse_error_expected(NULL, (expected), 0); \
344 #define expect_block(expected) \
345 if(UNLIKELY(token.type != (expected))) { \
346 parse_error_expected(NULL, (expected), 0); \
352 #define expect_void(expected) \
353 if(UNLIKELY(token.type != (expected))) { \
354 parse_error_expected(NULL, (expected), 0); \
360 static void set_context(context_t *new_context)
362 context = new_context;
364 last_declaration = new_context->declarations;
365 if(last_declaration != NULL) {
366 while(last_declaration->next != NULL) {
367 last_declaration = last_declaration->next;
373 * called when we find a 2nd declarator for an identifier we already have a
376 static bool is_compatible_declaration (declaration_t *declaration,
377 declaration_t *previous)
379 /* TODO: not correct yet */
380 return declaration->type == previous->type;
383 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
385 declaration_t *declaration = symbol->declaration;
386 for( ; declaration != NULL; declaration = declaration->symbol_next) {
387 if(declaration->namespc == namespc)
394 static const char *get_namespace_prefix(namespace_t namespc)
397 case NAMESPACE_NORMAL:
399 case NAMESPACE_UNION:
401 case NAMESPACE_STRUCT:
405 case NAMESPACE_LABEL:
408 panic("invalid namespace found");
412 * pushs an environment_entry on the environment stack and links the
413 * corresponding symbol to the new entry
415 static declaration_t *stack_push(stack_entry_t **stack_ptr,
416 declaration_t *declaration,
417 context_t *parent_context)
419 symbol_t *symbol = declaration->symbol;
420 namespace_t namespc = (namespace_t)declaration->namespc;
422 /* a declaration should be only pushed once */
423 assert(declaration->parent_context == NULL);
424 declaration->parent_context = parent_context;
426 declaration_t *previous_declaration = get_declaration(symbol, namespc);
427 assert(declaration != previous_declaration);
428 if(previous_declaration != NULL
429 && previous_declaration->parent_context == context) {
430 if(!is_compatible_declaration(declaration, previous_declaration)) {
431 parser_print_error_prefix_pos(declaration->source_position);
432 fprintf(stderr, "definition of symbol %s%s with type ",
433 get_namespace_prefix(namespc), symbol->string);
434 print_type_quoted(declaration->type);
436 parser_print_error_prefix_pos(
437 previous_declaration->source_position);
438 fprintf(stderr, "is incompatible with previous declaration "
440 print_type_quoted(previous_declaration->type);
443 const storage_class_t old_storage = previous_declaration->storage_class;
444 const storage_class_t new_storage = declaration->storage_class;
445 if (current_function == NULL) {
446 if (old_storage != STORAGE_CLASS_STATIC &&
447 new_storage == STORAGE_CLASS_STATIC) {
448 parser_print_error_prefix_pos(declaration->source_position);
450 "static declaration of '%s' follows non-static declaration\n",
452 parser_print_error_prefix_pos(previous_declaration->source_position);
453 fprintf(stderr, "previous declaration of '%s' was here\n",
456 if (old_storage == STORAGE_CLASS_EXTERN) {
457 if (new_storage == STORAGE_CLASS_NONE) {
458 previous_declaration->storage_class = STORAGE_CLASS_NONE;
461 parser_print_warning_prefix_pos(declaration->source_position);
462 fprintf(stderr, "redundant declaration for '%s'\n",
464 parser_print_warning_prefix_pos(previous_declaration->source_position);
465 fprintf(stderr, "previous declaration of '%s' was here\n",
470 if (old_storage == STORAGE_CLASS_EXTERN &&
471 new_storage == STORAGE_CLASS_EXTERN) {
472 parser_print_warning_prefix_pos(declaration->source_position);
473 fprintf(stderr, "redundant extern declaration for '%s'\n",
475 parser_print_warning_prefix_pos(previous_declaration->source_position);
476 fprintf(stderr, "previous declaration of '%s' was here\n",
479 parser_print_error_prefix_pos(declaration->source_position);
480 if (old_storage == new_storage) {
481 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
483 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
485 parser_print_error_prefix_pos(previous_declaration->source_position);
486 fprintf(stderr, "previous declaration of '%s' was here\n",
491 return previous_declaration;
494 /* remember old declaration */
496 entry.symbol = symbol;
497 entry.old_declaration = symbol->declaration;
498 entry.namespc = namespc;
499 ARR_APP1(stack_entry_t, *stack_ptr, entry);
501 /* replace/add declaration into declaration list of the symbol */
502 if(symbol->declaration == NULL) {
503 symbol->declaration = declaration;
505 declaration_t *iter_last = NULL;
506 declaration_t *iter = symbol->declaration;
507 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
508 /* replace an entry? */
509 if(iter->namespc == namespc) {
510 if(iter_last == NULL) {
511 symbol->declaration = declaration;
513 iter_last->symbol_next = declaration;
515 declaration->symbol_next = iter->symbol_next;
520 assert(iter_last->symbol_next == NULL);
521 iter_last->symbol_next = declaration;
528 static declaration_t *environment_push(declaration_t *declaration)
530 assert(declaration->source_position.input_name != NULL);
531 return stack_push(&environment_stack, declaration, context);
534 static declaration_t *label_push(declaration_t *declaration)
536 return stack_push(&label_stack, declaration, ¤t_function->context);
540 * pops symbols from the environment stack until @p new_top is the top element
542 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
544 stack_entry_t *stack = *stack_ptr;
545 size_t top = ARR_LEN(stack);
548 assert(new_top <= top);
552 for(i = top; i > new_top; --i) {
553 stack_entry_t *entry = & stack[i - 1];
555 declaration_t *old_declaration = entry->old_declaration;
556 symbol_t *symbol = entry->symbol;
557 namespace_t namespc = (namespace_t)entry->namespc;
559 /* replace/remove declaration */
560 declaration_t *declaration = symbol->declaration;
561 assert(declaration != NULL);
562 if(declaration->namespc == namespc) {
563 if(old_declaration == NULL) {
564 symbol->declaration = declaration->symbol_next;
566 symbol->declaration = old_declaration;
569 declaration_t *iter_last = declaration;
570 declaration_t *iter = declaration->symbol_next;
571 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
572 /* replace an entry? */
573 if(iter->namespc == namespc) {
574 assert(iter_last != NULL);
575 iter_last->symbol_next = old_declaration;
576 old_declaration->symbol_next = iter->symbol_next;
580 assert(iter != NULL);
584 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
587 static void environment_pop_to(size_t new_top)
589 stack_pop_to(&environment_stack, new_top);
592 static void label_pop_to(size_t new_top)
594 stack_pop_to(&label_stack, new_top);
598 static int get_rank(const type_t *type)
600 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
601 * and esp. footnote 108). However we can't fold constants (yet), so we
602 * can't decide wether unsigned int is possible, while int always works.
603 * (unsigned int would be preferable when possible... for stuff like
604 * struct { enum { ... } bla : 4; } ) */
605 if(type->type == TYPE_ENUM)
606 return ATOMIC_TYPE_INT;
608 assert(type->type == TYPE_ATOMIC);
609 atomic_type_t *atomic_type = (atomic_type_t*) type;
610 atomic_type_type_t atype = atomic_type->atype;
614 static type_t *promote_integer(type_t *type)
616 if(get_rank(type) < ATOMIC_TYPE_INT)
622 static expression_t *create_cast_expression(expression_t *expression,
625 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
627 cast->expression.type = EXPR_UNARY;
628 cast->type = UNEXPR_CAST;
629 cast->value = expression;
630 cast->expression.datatype = dest_type;
632 return (expression_t*) cast;
635 static bool is_null_expression(const expression_t *const expr)
637 if (expr->type != EXPR_CONST) return false;
639 type_t *const type = skip_typeref(expr->datatype);
640 if (!is_type_integer(type)) return false;
642 const const_t *const const_expr = (const const_t*)expr;
643 return const_expr->v.int_value == 0;
646 static expression_t *create_implicit_cast(expression_t *expression,
649 type_t *source_type = expression->datatype;
651 if(source_type == NULL)
654 source_type = skip_typeref(source_type);
655 dest_type = skip_typeref(dest_type);
657 if(source_type == dest_type)
660 if(dest_type->type == TYPE_ATOMIC) {
661 if(source_type->type != TYPE_ATOMIC)
662 panic("casting of non-atomic types not implemented yet");
664 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
665 type_error_incompatible("can't cast types",
666 expression->source_position,
667 source_type, dest_type);
671 return create_cast_expression(expression, dest_type);
673 if(dest_type->type == TYPE_POINTER) {
674 pointer_type_t *pointer_type
675 = (pointer_type_t*) dest_type;
676 switch (source_type->type) {
678 if (is_null_expression(expression)) {
679 return create_cast_expression(expression, dest_type);
684 if (pointers_compatible(source_type, dest_type)) {
685 return create_cast_expression(expression, dest_type);
690 array_type_t *const array_type = (array_type_t*) source_type;
691 if (types_compatible(array_type->element_type,
692 pointer_type->points_to)) {
693 return create_cast_expression(expression, dest_type);
699 panic("casting of non-atomic types not implemented yet");
702 type_error_incompatible("can't implicitly cast types",
703 expression->source_position,
704 source_type, dest_type);
708 panic("casting of non-atomic types not implemented yet");
711 static void semantic_assign(type_t *orig_type_left, expression_t **right,
714 type_t *orig_type_right = (*right)->datatype;
716 if(orig_type_right == NULL)
719 type_t *const type_left = skip_typeref(orig_type_left);
720 type_t *const type_right = skip_typeref(orig_type_right);
722 if (type_left == type_right) {
726 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
727 (type_left->type == TYPE_POINTER && is_null_expression(*right)) ||
728 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
729 *right = create_implicit_cast(*right, type_left);
733 if (type_left->type == TYPE_POINTER) {
734 switch (type_right->type) {
735 case TYPE_FUNCTION: {
736 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
737 if (ptr_type->points_to == type_right) {
744 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
745 array_type_t *const arr_type = (array_type_t*)type_right;
746 if (ptr_type->points_to == arr_type->element_type) {
756 /* TODO: improve error message */
757 parser_print_error_prefix();
758 fprintf(stderr, "incompatible types in %s\n", context);
759 parser_print_error_prefix();
760 print_type_quoted(type_left);
761 fputs(" <- ", stderr);
762 print_type_quoted(type_right);
766 static expression_t *parse_constant_expression(void)
768 /* start parsing at precedence 7 (conditional expression) */
769 return parse_sub_expression(7);
772 static expression_t *parse_assignment_expression(void)
774 /* start parsing at precedence 2 (assignment expression) */
775 return parse_sub_expression(2);
778 typedef struct declaration_specifiers_t declaration_specifiers_t;
779 struct declaration_specifiers_t {
780 storage_class_t storage_class;
785 static void parse_compound_type_entries(void);
786 static declaration_t *parse_declarator(
787 const declaration_specifiers_t *specifiers, type_t *type,
788 bool may_be_abstract);
789 static declaration_t *record_declaration(declaration_t *declaration);
791 static const char *parse_string_literals(void)
793 assert(token.type == T_STRING_LITERAL);
794 const char *result = token.v.string;
798 while(token.type == T_STRING_LITERAL) {
799 result = concat_strings(result, token.v.string);
806 static void parse_attributes(void)
810 case T___attribute__:
818 parse_error("EOF while parsing attribute");
836 if(token.type != T_STRING_LITERAL) {
837 parse_error_expected("while parsing assembler attribute",
842 parse_string_literals();
847 goto attributes_finished;
856 static designator_t *parse_designation(void)
858 if(token.type != '[' && token.type != '.')
861 designator_t *result = NULL;
862 designator_t *last = NULL;
865 designator_t *designator;
868 designator = allocate_ast_zero(sizeof(designator[0]));
870 designator->array_access = parse_constant_expression();
874 designator = allocate_ast_zero(sizeof(designator[0]));
876 if(token.type != T_IDENTIFIER) {
877 parse_error_expected("while parsing designator",
881 designator->symbol = token.v.symbol;
889 assert(designator != NULL);
891 last->next = designator;
900 static initializer_t *initializer_from_expression(type_t *type,
901 expression_t *expression)
903 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
905 /* TODO check that expression is a constant expression */
907 /* § 6.7.8.14/15 char array may be initialized by string literals */
908 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
909 array_type_t *array_type = (array_type_t*) type;
910 type_t *element_type = array_type->element_type;
912 if(element_type->type == TYPE_ATOMIC) {
913 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
914 atomic_type_type_t atype = atomic_type->atype;
916 /* TODO handle wide strings */
917 if(atype == ATOMIC_TYPE_CHAR
918 || atype == ATOMIC_TYPE_SCHAR
919 || atype == ATOMIC_TYPE_UCHAR) {
920 /* it's fine TODO: check for length of string array... */
921 goto initializer_from_expression_finished;
926 semantic_assign(type, &expression, "initializer");
928 initializer_from_expression_finished:
929 result->initializer.type = INITIALIZER_VALUE;
930 result->value = expression;
932 return (initializer_t*) result;
935 static initializer_t *parse_sub_initializer(type_t *type,
936 expression_t *expression,
937 type_t *expression_type);
939 static initializer_t *parse_sub_initializer_elem(type_t *type)
941 if(token.type == '{') {
942 return parse_sub_initializer(type, NULL, NULL);
945 expression_t *expression = parse_assignment_expression();
946 type_t *expression_type = skip_typeref(expression->datatype);
948 return parse_sub_initializer(type, expression, expression_type);
951 static bool had_initializer_brace_warning;
953 static initializer_t *parse_sub_initializer(type_t *type,
954 expression_t *expression,
955 type_t *expression_type)
957 if(is_type_scalar(type)) {
958 /* there might be extra {} hierarchies */
959 if(token.type == '{') {
961 if(!had_initializer_brace_warning) {
962 parse_warning("braces around scalar initializer");
963 had_initializer_brace_warning = true;
965 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
966 if(token.type == ',') {
968 /* TODO: warn about excessive elements */
974 if(expression == NULL) {
975 expression = parse_assignment_expression();
977 return initializer_from_expression(type, expression);
980 /* TODO: ignore qualifiers, comparing pointers is probably
982 if(expression != NULL && expression_type == type) {
983 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
984 result->type = INITIALIZER_VALUE;
987 semantic_assign(type, &expression, "initializer");
989 //result->v.value = expression;
994 bool read_paren = false;
995 if(token.type == '{') {
1000 /* descend into subtype */
1001 initializer_t *result = NULL;
1002 if(type->type == TYPE_ARRAY) {
1003 array_type_t *array_type = (array_type_t*) type;
1004 type_t *element_type = array_type->element_type;
1005 element_type = skip_typeref(element_type);
1008 = parse_sub_initializer(element_type, expression, expression_type);
1010 assert(type->type == TYPE_COMPOUND_STRUCT
1011 || type->type == TYPE_COMPOUND_UNION);
1012 compound_type_t *compound_type = (compound_type_t*) type;
1013 context_t *context = & compound_type->declaration->context;
1015 declaration_t *first = context->declarations;
1018 type_t *first_type = first->type;
1019 first_type = skip_typeref(first_type);
1022 had_initializer_brace_warning = false;
1023 if(expression == NULL) {
1024 sub = parse_sub_initializer_elem(first_type);
1026 sub = parse_sub_initializer(first_type, expression,expression_type);
1029 /* didn't match the subtypes -> try our parent type */
1031 assert(!read_paren);
1035 initializer_t **elems = NEW_ARR_F(initializer_t*, 0);
1036 ARR_APP1(initializer_t*, elems, sub);
1038 declaration_t *iter = first->next;
1039 for( ; iter != NULL; iter = iter->next) {
1040 if(iter->symbol == NULL)
1042 if(iter->namespc != NAMESPACE_NORMAL)
1045 if(token.type == '}')
1049 type_t *iter_type = iter->type;
1050 iter_type = skip_typeref(iter_type);
1052 /* read next token */
1053 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1055 /* TODO error, do nicer cleanup*/
1056 parse_error("member initializer didn't match");
1060 ARR_APP1(initializer_t*, elems, sub);
1063 int len = ARR_LEN(elems);
1064 size_t elems_size = sizeof(initializer_t*) * len;
1066 initializer_list_t *init
1067 = allocate_ast_zero(sizeof(init[0]) + elems_size);
1069 init->initializer.type = INITIALIZER_LIST;
1071 memcpy(init->initializers, elems, elems_size);
1074 result = (initializer_t*) init;
1078 if(token.type == ',')
1085 static initializer_t *parse_initializer(type_t *type)
1087 initializer_t *result;
1089 type = skip_typeref(type);
1091 if(token.type != '{') {
1092 expression_t *expression = parse_assignment_expression();
1093 return initializer_from_expression(type, expression);
1096 if(is_type_scalar(type)) {
1100 expression_t *expression = parse_assignment_expression();
1101 result = initializer_from_expression(type, expression);
1103 if(token.type == ',')
1109 result = parse_sub_initializer(type, NULL, NULL);
1117 static declaration_t *parse_compound_type_specifier(bool is_struct)
1125 symbol_t *symbol = NULL;
1126 declaration_t *declaration = NULL;
1128 if (token.type == T___attribute__) {
1133 if(token.type == T_IDENTIFIER) {
1134 symbol = token.v.symbol;
1138 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1140 declaration = get_declaration(symbol, NAMESPACE_UNION);
1142 } else if(token.type != '{') {
1144 parse_error_expected("while parsing struct type specifier",
1145 T_IDENTIFIER, '{', 0);
1147 parse_error_expected("while parsing union type specifier",
1148 T_IDENTIFIER, '{', 0);
1154 if(declaration == NULL) {
1155 declaration = allocate_type_zero(sizeof(declaration[0]));
1158 declaration->namespc = NAMESPACE_STRUCT;
1160 declaration->namespc = NAMESPACE_UNION;
1162 declaration->source_position = token.source_position;
1163 declaration->symbol = symbol;
1164 record_declaration(declaration);
1167 if(token.type == '{') {
1168 if(declaration->init.is_defined) {
1169 assert(symbol != NULL);
1170 parser_print_error_prefix();
1171 fprintf(stderr, "multiple definition of %s %s\n",
1172 is_struct ? "struct" : "union", symbol->string);
1173 declaration->context.declarations = NULL;
1175 declaration->init.is_defined = true;
1177 int top = environment_top();
1178 context_t *last_context = context;
1179 set_context(& declaration->context);
1181 parse_compound_type_entries();
1184 assert(context == & declaration->context);
1185 set_context(last_context);
1186 environment_pop_to(top);
1192 static void parse_enum_entries(void)
1196 if(token.type == '}') {
1198 parse_error("empty enum not allowed");
1203 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1205 if(token.type != T_IDENTIFIER) {
1206 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1210 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1211 entry->symbol = token.v.symbol;
1212 entry->source_position = token.source_position;
1215 if(token.type == '=') {
1217 entry->init.initializer = parse_initializer(type_int);
1220 record_declaration(entry);
1222 if(token.type != ',')
1225 } while(token.type != '}');
1230 static declaration_t *parse_enum_specifier(void)
1234 declaration_t *declaration;
1237 if(token.type == T_IDENTIFIER) {
1238 symbol = token.v.symbol;
1241 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1242 } else if(token.type != '{') {
1243 parse_error_expected("while parsing enum type specifier",
1244 T_IDENTIFIER, '{', 0);
1251 if(declaration == NULL) {
1252 declaration = allocate_type_zero(sizeof(declaration[0]));
1254 declaration->namespc = NAMESPACE_ENUM;
1255 declaration->source_position = token.source_position;
1256 declaration->symbol = symbol;
1259 if(token.type == '{') {
1260 if(declaration->init.is_defined) {
1261 parser_print_error_prefix();
1262 fprintf(stderr, "multiple definitions of enum %s\n",
1265 record_declaration(declaration);
1266 declaration->init.is_defined = 1;
1268 parse_enum_entries();
1276 * if a symbol is a typedef to another type, return true
1278 static bool is_typedef_symbol(symbol_t *symbol)
1280 const declaration_t *const declaration =
1281 get_declaration(symbol, NAMESPACE_NORMAL);
1283 declaration != NULL &&
1284 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1287 static type_t *parse_typeof(void)
1295 expression_t *expression = NULL;
1298 switch(token.type) {
1299 case T___extension__:
1300 /* this can be a prefix to a typename or an expression */
1301 /* we simply eat it now. */
1304 } while(token.type == T___extension__);
1308 if(is_typedef_symbol(token.v.symbol)) {
1309 type = parse_typename();
1311 expression = parse_expression();
1312 type = expression->datatype;
1317 type = parse_typename();
1321 expression = parse_expression();
1322 type = expression->datatype;
1328 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1329 typeof->type.type = TYPE_TYPEOF;
1330 typeof->expression = expression;
1331 typeof->typeof_type = type;
1333 return (type_t*) typeof;
1337 SPECIFIER_SIGNED = 1 << 0,
1338 SPECIFIER_UNSIGNED = 1 << 1,
1339 SPECIFIER_LONG = 1 << 2,
1340 SPECIFIER_INT = 1 << 3,
1341 SPECIFIER_DOUBLE = 1 << 4,
1342 SPECIFIER_CHAR = 1 << 5,
1343 SPECIFIER_SHORT = 1 << 6,
1344 SPECIFIER_LONG_LONG = 1 << 7,
1345 SPECIFIER_FLOAT = 1 << 8,
1346 SPECIFIER_BOOL = 1 << 9,
1347 SPECIFIER_VOID = 1 << 10,
1348 #ifdef PROVIDE_COMPLEX
1349 SPECIFIER_COMPLEX = 1 << 11,
1351 #ifdef PROVIDE_IMAGINARY
1352 SPECIFIER_IMAGINARY = 1 << 12,
1356 static type_t *create_builtin_type(symbol_t *symbol)
1358 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1359 type->type.type = TYPE_BUILTIN;
1360 type->symbol = symbol;
1362 type->real_type = type_int;
1364 return (type_t*) type;
1367 static type_t *get_typedef_type(symbol_t *symbol)
1369 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1370 if(declaration == NULL
1371 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1374 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1375 typedef_type->type.type = TYPE_TYPEDEF;
1376 typedef_type->declaration = declaration;
1378 return (type_t*) typedef_type;
1381 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1383 type_t *type = NULL;
1384 unsigned type_qualifiers = 0;
1385 unsigned type_specifiers = 0;
1389 switch(token.type) {
1392 #define MATCH_STORAGE_CLASS(token, class) \
1394 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1395 parse_error("multiple storage classes in declaration " \
1398 specifiers->storage_class = class; \
1402 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1403 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1404 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1405 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1406 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1408 /* type qualifiers */
1409 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1411 type_qualifiers |= qualifier; \
1415 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1416 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1417 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1419 case T___extension__:
1424 /* type specifiers */
1425 #define MATCH_SPECIFIER(token, specifier, name) \
1428 if(type_specifiers & specifier) { \
1429 parse_error("multiple " name " type specifiers given"); \
1431 type_specifiers |= specifier; \
1435 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1436 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1437 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1438 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1439 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1440 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1441 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1442 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1443 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1444 #ifdef PROVIDE_COMPLEX
1445 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1447 #ifdef PROVIDE_IMAGINARY
1448 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1452 specifiers->is_inline = true;
1457 if(type_specifiers & SPECIFIER_LONG_LONG) {
1458 parse_error("multiple type specifiers given");
1459 } else if(type_specifiers & SPECIFIER_LONG) {
1460 type_specifiers |= SPECIFIER_LONG_LONG;
1462 type_specifiers |= SPECIFIER_LONG;
1466 /* TODO: if type != NULL for the following rules should issue
1469 compound_type_t *compound_type
1470 = allocate_type_zero(sizeof(compound_type[0]));
1471 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1472 compound_type->declaration = parse_compound_type_specifier(true);
1474 type = (type_t*) compound_type;
1478 compound_type_t *compound_type
1479 = allocate_type_zero(sizeof(compound_type[0]));
1480 compound_type->type.type = TYPE_COMPOUND_UNION;
1481 compound_type->declaration = parse_compound_type_specifier(false);
1483 type = (type_t*) compound_type;
1487 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1488 enum_type->type.type = TYPE_ENUM;
1489 enum_type->declaration = parse_enum_specifier();
1491 type = (type_t*) enum_type;
1495 type = parse_typeof();
1497 case T___builtin_va_list:
1498 type = create_builtin_type(token.v.symbol);
1502 case T___attribute__:
1507 case T_IDENTIFIER: {
1508 type_t *typedef_type = get_typedef_type(token.v.symbol);
1510 if(typedef_type == NULL)
1511 goto finish_specifiers;
1514 type = typedef_type;
1518 /* function specifier */
1520 goto finish_specifiers;
1527 atomic_type_type_t atomic_type;
1529 /* match valid basic types */
1530 switch(type_specifiers) {
1531 case SPECIFIER_VOID:
1532 atomic_type = ATOMIC_TYPE_VOID;
1534 case SPECIFIER_CHAR:
1535 atomic_type = ATOMIC_TYPE_CHAR;
1537 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1538 atomic_type = ATOMIC_TYPE_SCHAR;
1540 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1541 atomic_type = ATOMIC_TYPE_UCHAR;
1543 case SPECIFIER_SHORT:
1544 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1545 case SPECIFIER_SHORT | SPECIFIER_INT:
1546 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1547 atomic_type = ATOMIC_TYPE_SHORT;
1549 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1550 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1551 atomic_type = ATOMIC_TYPE_USHORT;
1554 case SPECIFIER_SIGNED:
1555 case SPECIFIER_SIGNED | SPECIFIER_INT:
1556 atomic_type = ATOMIC_TYPE_INT;
1558 case SPECIFIER_UNSIGNED:
1559 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1560 atomic_type = ATOMIC_TYPE_UINT;
1562 case SPECIFIER_LONG:
1563 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1564 case SPECIFIER_LONG | SPECIFIER_INT:
1565 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1566 atomic_type = ATOMIC_TYPE_LONG;
1568 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1569 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1570 atomic_type = ATOMIC_TYPE_ULONG;
1572 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1573 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1574 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1575 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1577 atomic_type = ATOMIC_TYPE_LONGLONG;
1579 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1580 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1582 atomic_type = ATOMIC_TYPE_ULONGLONG;
1584 case SPECIFIER_FLOAT:
1585 atomic_type = ATOMIC_TYPE_FLOAT;
1587 case SPECIFIER_DOUBLE:
1588 atomic_type = ATOMIC_TYPE_DOUBLE;
1590 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1591 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1593 case SPECIFIER_BOOL:
1594 atomic_type = ATOMIC_TYPE_BOOL;
1596 #ifdef PROVIDE_COMPLEX
1597 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1598 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1600 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1601 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1603 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1604 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1607 #ifdef PROVIDE_IMAGINARY
1608 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1609 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1611 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1612 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1614 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1615 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1619 /* invalid specifier combination, give an error message */
1620 if(type_specifiers == 0) {
1622 parse_warning("no type specifiers in declaration (using int)");
1623 atomic_type = ATOMIC_TYPE_INT;
1626 parse_error("no type specifiers given in declaration");
1628 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1629 (type_specifiers & SPECIFIER_UNSIGNED)) {
1630 parse_error("signed and unsigned specifiers gives");
1631 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1632 parse_error("only integer types can be signed or unsigned");
1634 parse_error("multiple datatypes in declaration");
1636 atomic_type = ATOMIC_TYPE_INVALID;
1639 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1640 atype->type.type = TYPE_ATOMIC;
1641 atype->atype = atomic_type;
1644 type = (type_t*) atype;
1646 if(type_specifiers != 0) {
1647 parse_error("multiple datatypes in declaration");
1651 type->qualifiers = (type_qualifier_t)type_qualifiers;
1653 type_t *result = typehash_insert(type);
1654 if(newtype && result != (type_t*) type) {
1658 specifiers->type = result;
1661 static unsigned parse_type_qualifiers(void)
1663 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1666 switch(token.type) {
1667 /* type qualifiers */
1668 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1669 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1670 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1673 return type_qualifiers;
1678 static void parse_identifier_list(void)
1681 if(token.type != T_IDENTIFIER) {
1682 parse_error_expected("while parsing parameter identifier list",
1687 if(token.type != ',')
1693 static declaration_t *parse_parameter(void)
1695 declaration_specifiers_t specifiers;
1696 memset(&specifiers, 0, sizeof(specifiers));
1698 parse_declaration_specifiers(&specifiers);
1700 declaration_t *declaration
1701 = parse_declarator(&specifiers, specifiers.type, true);
1703 /* TODO check declaration constraints for parameters */
1704 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1705 parse_error("typedef not allowed in parameter list");
1708 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1710 if (declaration->type->type == TYPE_ARRAY) {
1711 const array_type_t *const arr_type =
1712 (const array_type_t*)declaration->type;
1714 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1720 static declaration_t *parse_parameters(function_type_t *type)
1722 if(token.type == T_IDENTIFIER) {
1723 symbol_t *symbol = token.v.symbol;
1724 if(!is_typedef_symbol(symbol)) {
1726 parse_identifier_list();
1731 if(token.type == ')') {
1732 type->unspecified_parameters = 1;
1735 if(token.type == T_void && look_ahead(1)->type == ')') {
1740 declaration_t *declarations = NULL;
1741 declaration_t *declaration;
1742 declaration_t *last_declaration = NULL;
1743 function_parameter_t *parameter;
1744 function_parameter_t *last_parameter = NULL;
1747 switch(token.type) {
1751 return declarations;
1754 case T___extension__:
1756 declaration = parse_parameter();
1758 parameter = allocate_type_zero(sizeof(parameter[0]));
1759 parameter->type = declaration->type;
1761 if(last_parameter != NULL) {
1762 last_declaration->next = declaration;
1763 last_parameter->next = parameter;
1765 type->parameters = parameter;
1766 declarations = declaration;
1768 last_parameter = parameter;
1769 last_declaration = declaration;
1773 return declarations;
1775 if(token.type != ',')
1776 return declarations;
1786 } construct_type_type_t;
1788 typedef struct construct_type_t construct_type_t;
1789 struct construct_type_t {
1790 construct_type_type_t type;
1791 construct_type_t *next;
1794 typedef struct parsed_pointer_t parsed_pointer_t;
1795 struct parsed_pointer_t {
1796 construct_type_t construct_type;
1797 type_qualifier_t type_qualifiers;
1800 typedef struct construct_function_type_t construct_function_type_t;
1801 struct construct_function_type_t {
1802 construct_type_t construct_type;
1803 function_type_t *function_type;
1806 typedef struct parsed_array_t parsed_array_t;
1807 struct parsed_array_t {
1808 construct_type_t construct_type;
1809 type_qualifier_t type_qualifiers;
1815 typedef struct construct_base_type_t construct_base_type_t;
1816 struct construct_base_type_t {
1817 construct_type_t construct_type;
1821 static construct_type_t *parse_pointer_declarator(void)
1825 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1826 memset(pointer, 0, sizeof(pointer[0]));
1827 pointer->construct_type.type = CONSTRUCT_POINTER;
1828 pointer->type_qualifiers = parse_type_qualifiers();
1830 return (construct_type_t*) pointer;
1833 static construct_type_t *parse_array_declarator(void)
1837 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1838 memset(array, 0, sizeof(array[0]));
1839 array->construct_type.type = CONSTRUCT_ARRAY;
1841 if(token.type == T_static) {
1842 array->is_static = true;
1846 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1847 if(type_qualifiers != 0) {
1848 if(token.type == T_static) {
1849 array->is_static = true;
1853 array->type_qualifiers = type_qualifiers;
1855 if(token.type == '*' && look_ahead(1)->type == ']') {
1856 array->is_variable = true;
1858 } else if(token.type != ']') {
1859 array->size = parse_assignment_expression();
1864 return (construct_type_t*) array;
1867 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1871 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1872 type->type.type = TYPE_FUNCTION;
1874 declaration_t *parameters = parse_parameters(type);
1875 if(declaration != NULL) {
1876 declaration->context.declarations = parameters;
1879 construct_function_type_t *construct_function_type =
1880 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1881 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1882 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1883 construct_function_type->function_type = type;
1887 return (construct_type_t*) construct_function_type;
1890 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1891 int may_be_abstract)
1893 construct_type_t *result = NULL;
1894 construct_type_t *last = NULL;
1896 while(token.type == '*') {
1897 construct_type_t *type = parse_pointer_declarator();
1906 /* TODO: find out if this is correct */
1909 construct_type_t *inner_types = NULL;
1911 switch(token.type) {
1913 if(declaration == NULL) {
1914 parse_error("no identifier expected in typename");
1916 declaration->symbol = token.v.symbol;
1917 declaration->source_position = token.source_position;
1923 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1929 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1930 /* avoid a loop in the outermost scope, because eat_statement doesn't
1932 if(token.type == '}' && current_function == NULL) {
1941 construct_type_t *type;
1942 switch(token.type) {
1944 type = parse_function_declarator(declaration);
1947 type = parse_array_declarator();
1950 goto declarator_finished;
1961 declarator_finished:
1964 if(inner_types != NULL) {
1966 last->next = inner_types;
1968 result = inner_types;
1976 static type_t *construct_declarator_type(construct_type_t *construct_list,
1979 construct_type_t *iter = construct_list;
1980 for( ; iter != NULL; iter = iter->next) {
1981 parsed_pointer_t *parsed_pointer;
1982 parsed_array_t *parsed_array;
1983 construct_function_type_t *construct_function_type;
1984 function_type_t *function_type;
1985 pointer_type_t *pointer_type;
1986 array_type_t *array_type;
1988 switch(iter->type) {
1989 case CONSTRUCT_INVALID:
1990 panic("invalid type construction found");
1991 case CONSTRUCT_FUNCTION:
1992 construct_function_type = (construct_function_type_t*) iter;
1993 function_type = construct_function_type->function_type;
1995 function_type->result_type = type;
1996 type = (type_t*) function_type;
1999 case CONSTRUCT_POINTER:
2000 parsed_pointer = (parsed_pointer_t*) iter;
2001 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2003 pointer_type->type.type = TYPE_POINTER;
2004 pointer_type->points_to = type;
2005 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2006 type = (type_t*) pointer_type;
2009 case CONSTRUCT_ARRAY:
2010 parsed_array = (parsed_array_t*) iter;
2011 array_type = allocate_type_zero(sizeof(array_type[0]));
2013 array_type->type.type = TYPE_ARRAY;
2014 array_type->element_type = type;
2015 array_type->type.qualifiers = parsed_array->type_qualifiers;
2016 array_type->is_static = parsed_array->is_static;
2017 array_type->is_variable = parsed_array->is_variable;
2018 array_type->size = parsed_array->size;
2019 type = (type_t*) array_type;
2023 type_t *hashed_type = typehash_insert((type_t*) type);
2024 if(hashed_type != type) {
2033 static declaration_t *parse_declarator(
2034 const declaration_specifiers_t *specifiers,
2035 type_t *type, bool may_be_abstract)
2037 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2038 declaration->storage_class = specifiers->storage_class;
2039 declaration->is_inline = specifiers->is_inline;
2041 construct_type_t *construct_type
2042 = parse_inner_declarator(declaration, may_be_abstract);
2043 declaration->type = construct_declarator_type(construct_type, type);
2045 if(construct_type != NULL) {
2046 obstack_free(&temp_obst, construct_type);
2052 static type_t *parse_abstract_declarator(type_t *base_type)
2054 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2056 type_t *result = construct_declarator_type(construct_type, base_type);
2057 if(construct_type != NULL) {
2058 obstack_free(&temp_obst, construct_type);
2064 static declaration_t *record_declaration(declaration_t *declaration)
2066 assert(context != NULL);
2068 symbol_t *symbol = declaration->symbol;
2069 if(symbol != NULL) {
2070 declaration_t *alias = environment_push(declaration);
2071 if(alias != declaration)
2074 declaration->parent_context = context;
2077 if(last_declaration != NULL) {
2078 last_declaration->next = declaration;
2080 context->declarations = declaration;
2082 last_declaration = declaration;
2087 static void parser_error_multiple_definition(declaration_t *previous,
2088 declaration_t *declaration)
2090 parser_print_error_prefix_pos(declaration->source_position);
2091 fprintf(stderr, "multiple definition of symbol '%s'\n",
2092 declaration->symbol->string);
2093 parser_print_error_prefix_pos(previous->source_position);
2094 fprintf(stderr, "this is the location of the previous definition.\n");
2097 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2100 declaration_t *ndeclaration
2101 = parse_declarator(specifiers, specifiers->type, false);
2103 declaration_t *declaration = record_declaration(ndeclaration);
2105 type_t *type = declaration->type;
2106 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2107 parser_print_warning_prefix_pos(declaration->source_position);
2108 fprintf(stderr, "variable '%s' declared 'inline'\n",
2109 declaration->symbol->string);
2112 if(token.type == '=') {
2115 /* TODO: check that this is an allowed type (no function type) */
2117 if(declaration->init.initializer != NULL) {
2118 parser_error_multiple_definition(declaration, ndeclaration);
2121 ndeclaration->init.initializer = parse_initializer(declaration->type);
2122 } else if(token.type == '{') {
2123 if(declaration->type->type != TYPE_FUNCTION) {
2124 parser_print_error_prefix();
2125 fprintf(stderr, "Declarator ");
2126 print_type_ext(declaration->type, declaration->symbol, NULL);
2127 fprintf(stderr, " has a body but is not a function type.\n");
2132 if(declaration->init.statement != NULL) {
2133 parser_error_multiple_definition(declaration, ndeclaration);
2135 if(ndeclaration != declaration) {
2136 memcpy(&declaration->context, &ndeclaration->context,
2137 sizeof(declaration->context));
2140 int top = environment_top();
2141 context_t *last_context = context;
2142 set_context(&declaration->context);
2144 /* push function parameters */
2145 declaration_t *parameter = declaration->context.declarations;
2146 for( ; parameter != NULL; parameter = parameter->next) {
2147 environment_push(parameter);
2150 int label_stack_top = label_top();
2151 declaration_t *old_current_function = current_function;
2152 current_function = declaration;
2154 statement_t *statement = parse_compound_statement();
2156 assert(current_function == declaration);
2157 current_function = old_current_function;
2158 label_pop_to(label_stack_top);
2160 assert(context == &declaration->context);
2161 set_context(last_context);
2162 environment_pop_to(top);
2164 declaration->init.statement = statement;
2168 if(token.type != ',')
2175 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2178 if(token.type == ':') {
2180 parse_constant_expression();
2181 /* TODO (bitfields) */
2183 declaration_t *declaration
2184 = parse_declarator(specifiers, specifiers->type, true);
2186 /* TODO: check constraints for struct declarations */
2187 /* TODO: check for doubled fields */
2188 record_declaration(declaration);
2190 if(token.type == ':') {
2192 parse_constant_expression();
2193 /* TODO (bitfields) */
2197 if(token.type != ',')
2204 static void parse_compound_type_entries(void)
2208 while(token.type != '}' && token.type != T_EOF) {
2209 declaration_specifiers_t specifiers;
2210 memset(&specifiers, 0, sizeof(specifiers));
2211 parse_declaration_specifiers(&specifiers);
2213 parse_struct_declarators(&specifiers);
2215 if(token.type == T_EOF) {
2216 parse_error("unexpected error while parsing struct");
2221 static void parse_declaration(void)
2223 source_position_t source_position = token.source_position;
2225 declaration_specifiers_t specifiers;
2226 memset(&specifiers, 0, sizeof(specifiers));
2227 parse_declaration_specifiers(&specifiers);
2229 if(token.type == ';') {
2230 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2231 parse_warning_pos(source_position,
2232 "useless keyword in empty declaration");
2234 switch (specifiers.type->type) {
2235 case TYPE_COMPOUND_STRUCT:
2236 case TYPE_COMPOUND_UNION: {
2237 const compound_type_t *const comp_type =
2238 (const compound_type_t*)specifiers.type;
2239 if (comp_type->declaration->symbol == NULL) {
2240 parse_warning_pos(source_position,
2241 "unnamed struct/union that defines no instances");
2246 case TYPE_ENUM: break;
2249 parse_warning_pos(source_position, "empty declaration");
2255 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2257 declaration->type = specifiers.type;
2258 declaration->storage_class = specifiers.storage_class;
2259 declaration->source_position = source_position;
2260 record_declaration(declaration);
2263 parse_init_declarators(&specifiers);
2266 static type_t *parse_typename(void)
2268 declaration_specifiers_t specifiers;
2269 memset(&specifiers, 0, sizeof(specifiers));
2270 parse_declaration_specifiers(&specifiers);
2271 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2272 /* TODO: improve error message, user does probably not know what a
2273 * storage class is...
2275 parse_error("typename may not have a storage class");
2278 type_t *result = parse_abstract_declarator(specifiers.type);
2286 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2287 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2288 expression_t *left);
2290 typedef struct expression_parser_function_t expression_parser_function_t;
2291 struct expression_parser_function_t {
2292 unsigned precedence;
2293 parse_expression_function parser;
2294 unsigned infix_precedence;
2295 parse_expression_infix_function infix_parser;
2298 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2300 static expression_t *make_invalid_expression(void)
2302 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2303 expression->type = EXPR_INVALID;
2304 expression->source_position = token.source_position;
2308 static expression_t *expected_expression_error(void)
2310 parser_print_error_prefix();
2311 fprintf(stderr, "expected expression, got token ");
2312 print_token(stderr, & token);
2313 fprintf(stderr, "\n");
2317 return make_invalid_expression();
2320 static expression_t *parse_string_const(void)
2322 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2324 cnst->expression.type = EXPR_STRING_LITERAL;
2325 cnst->expression.datatype = type_string;
2326 cnst->value = parse_string_literals();
2328 return (expression_t*) cnst;
2331 static expression_t *parse_int_const(void)
2333 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2335 cnst->expression.type = EXPR_CONST;
2336 cnst->expression.datatype = type_int;
2337 cnst->v.int_value = token.v.intvalue;
2341 return (expression_t*) cnst;
2344 static expression_t *parse_float_const(void)
2346 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2348 cnst->expression.type = EXPR_CONST;
2349 cnst->expression.datatype = type_double;
2350 cnst->v.float_value = token.v.floatvalue;
2354 return (expression_t*) cnst;
2357 static declaration_t *create_implicit_function(symbol_t *symbol,
2358 const source_position_t source_position)
2360 function_type_t *function_type
2361 = allocate_type_zero(sizeof(function_type[0]));
2363 function_type->type.type = TYPE_FUNCTION;
2364 function_type->result_type = type_int;
2365 function_type->unspecified_parameters = true;
2367 type_t *type = typehash_insert((type_t*) function_type);
2368 if(type != (type_t*) function_type) {
2369 free_type(function_type);
2372 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2374 declaration->storage_class = STORAGE_CLASS_EXTERN;
2375 declaration->type = type;
2376 declaration->symbol = symbol;
2377 declaration->source_position = source_position;
2379 /* prepend the implicit definition to the global context
2380 * this is safe since the symbol wasn't declared as anything else yet
2382 assert(symbol->declaration == NULL);
2384 context_t *last_context = context;
2385 context = global_context;
2387 environment_push(declaration);
2388 declaration->next = context->declarations;
2389 context->declarations = declaration;
2391 context = last_context;
2396 static expression_t *parse_reference(void)
2398 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2400 ref->expression.type = EXPR_REFERENCE;
2401 ref->symbol = token.v.symbol;
2403 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2405 source_position_t source_position = token.source_position;
2408 if(declaration == NULL) {
2410 /* an implicitly defined function */
2411 if(token.type == '(') {
2412 parser_print_prefix_pos(token.source_position);
2413 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2414 ref->symbol->string);
2416 declaration = create_implicit_function(ref->symbol,
2421 parser_print_error_prefix();
2422 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2423 return (expression_t*) ref;
2427 ref->declaration = declaration;
2428 ref->expression.datatype = declaration->type;
2430 return (expression_t*) ref;
2433 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2437 /* TODO check if explicit cast is allowed and issue warnings/errors */
2440 static expression_t *parse_cast(void)
2442 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2444 cast->expression.type = EXPR_UNARY;
2445 cast->type = UNEXPR_CAST;
2446 cast->expression.source_position = token.source_position;
2448 type_t *type = parse_typename();
2451 expression_t *value = parse_sub_expression(20);
2453 check_cast_allowed(value, type);
2455 cast->expression.datatype = type;
2456 cast->value = value;
2458 return (expression_t*) cast;
2461 static expression_t *parse_statement_expression(void)
2463 statement_expression_t *expression
2464 = allocate_ast_zero(sizeof(expression[0]));
2465 expression->expression.type = EXPR_STATEMENT;
2467 statement_t *statement = parse_compound_statement();
2468 expression->statement = statement;
2469 if(statement == NULL) {
2474 assert(statement->type == STATEMENT_COMPOUND);
2475 compound_statement_t *compound_statement
2476 = (compound_statement_t*) statement;
2478 /* find last statement and use it's type */
2479 const statement_t *last_statement = NULL;
2480 const statement_t *iter = compound_statement->statements;
2481 for( ; iter != NULL; iter = iter->next) {
2482 last_statement = iter;
2485 if(last_statement->type == STATEMENT_EXPRESSION) {
2486 const expression_statement_t *expression_statement =
2487 (const expression_statement_t*) last_statement;
2488 expression->expression.datatype
2489 = expression_statement->expression->datatype;
2491 expression->expression.datatype = type_void;
2496 return (expression_t*) expression;
2499 static expression_t *parse_brace_expression(void)
2503 switch(token.type) {
2505 /* gcc extension: a stement expression */
2506 return parse_statement_expression();
2510 return parse_cast();
2512 if(is_typedef_symbol(token.v.symbol)) {
2513 return parse_cast();
2517 expression_t *result = parse_expression();
2523 static expression_t *parse_function_keyword(void)
2528 if (current_function == NULL) {
2529 parse_error("'__func__' used outside of a function");
2532 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2533 expression->expression.type = EXPR_FUNCTION;
2534 expression->expression.datatype = type_string;
2535 expression->value = "TODO: FUNCTION";
2537 return (expression_t*) expression;
2540 static expression_t *parse_pretty_function_keyword(void)
2542 eat(T___PRETTY_FUNCTION__);
2545 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2546 expression->expression.type = EXPR_PRETTY_FUNCTION;
2547 expression->expression.datatype = type_string;
2548 expression->value = "TODO: PRETTY FUNCTION";
2550 return (expression_t*) expression;
2553 static designator_t *parse_designator(void)
2555 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2557 if(token.type != T_IDENTIFIER) {
2558 parse_error_expected("while parsing member designator",
2563 result->symbol = token.v.symbol;
2566 designator_t *last_designator = result;
2568 if(token.type == '.') {
2570 if(token.type != T_IDENTIFIER) {
2571 parse_error_expected("while parsing member designator",
2576 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2577 designator->symbol = token.v.symbol;
2580 last_designator->next = designator;
2581 last_designator = designator;
2584 if(token.type == '[') {
2586 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2587 designator->array_access = parse_expression();
2588 if(designator->array_access == NULL) {
2594 last_designator->next = designator;
2595 last_designator = designator;
2604 static expression_t *parse_offsetof(void)
2606 eat(T___builtin_offsetof);
2608 offsetof_expression_t *expression
2609 = allocate_ast_zero(sizeof(expression[0]));
2610 expression->expression.type = EXPR_OFFSETOF;
2611 expression->expression.datatype = type_size_t;
2614 expression->type = parse_typename();
2616 expression->designator = parse_designator();
2619 return (expression_t*) expression;
2622 static expression_t *parse_va_arg(void)
2624 eat(T___builtin_va_arg);
2626 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2627 expression->expression.type = EXPR_VA_ARG;
2630 expression->arg = parse_assignment_expression();
2632 expression->expression.datatype = parse_typename();
2635 return (expression_t*) expression;
2638 static expression_t *parse_builtin_symbol(void)
2640 builtin_symbol_expression_t *expression
2641 = allocate_ast_zero(sizeof(expression[0]));
2642 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2644 /* TODO: set datatype */
2646 expression->symbol = token.v.symbol;
2650 return (expression_t*) expression;
2653 static expression_t *parse_primary_expression(void)
2655 switch(token.type) {
2657 return parse_int_const();
2658 case T_FLOATINGPOINT:
2659 return parse_float_const();
2660 case T_STRING_LITERAL:
2661 return parse_string_const();
2663 return parse_reference();
2664 case T___FUNCTION__:
2666 return parse_function_keyword();
2667 case T___PRETTY_FUNCTION__:
2668 return parse_pretty_function_keyword();
2669 case T___builtin_offsetof:
2670 return parse_offsetof();
2671 case T___builtin_va_arg:
2672 return parse_va_arg();
2673 case T___builtin_expect:
2674 case T___builtin_va_start:
2675 case T___builtin_va_end:
2676 return parse_builtin_symbol();
2679 return parse_brace_expression();
2682 parser_print_error_prefix();
2683 fprintf(stderr, "unexpected token ");
2684 print_token(stderr, &token);
2685 fprintf(stderr, "\n");
2688 return make_invalid_expression();
2691 static expression_t *parse_array_expression(unsigned precedence,
2692 expression_t *array_ref)
2698 array_access_expression_t *array_access
2699 = allocate_ast_zero(sizeof(array_access[0]));
2701 array_access->expression.type = EXPR_ARRAY_ACCESS;
2702 array_access->array_ref = array_ref;
2703 array_access->index = parse_expression();
2705 type_t *type = array_ref->datatype;
2707 if(type->type == TYPE_POINTER) {
2708 pointer_type_t *pointer = (pointer_type_t*) type;
2709 array_access->expression.datatype = pointer->points_to;
2710 } else if(type->type == TYPE_ARRAY) {
2711 array_type_t *array_type = (array_type_t*) type;
2712 array_access->expression.datatype = array_type->element_type;
2714 parser_print_error_prefix();
2715 fprintf(stderr, "array access on object with non-pointer type ");
2716 print_type_quoted(type);
2717 fprintf(stderr, "\n");
2721 if(token.type != ']') {
2722 parse_error_expected("Problem while parsing array access", ']', 0);
2723 return (expression_t*) array_access;
2727 return (expression_t*) array_access;
2730 static bool is_declaration_specifier(const token_t *token,
2731 bool only_type_specifiers)
2733 switch(token->type) {
2737 return is_typedef_symbol(token->v.symbol);
2740 if(only_type_specifiers)
2749 static expression_t *parse_sizeof(unsigned precedence)
2753 sizeof_expression_t *sizeof_expression
2754 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2755 sizeof_expression->expression.type = EXPR_SIZEOF;
2756 sizeof_expression->expression.datatype = type_size_t;
2758 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2760 sizeof_expression->type = parse_typename();
2763 expression_t *expression = parse_sub_expression(precedence);
2764 sizeof_expression->type = expression->datatype;
2765 sizeof_expression->size_expression = expression;
2768 return (expression_t*) sizeof_expression;
2771 static expression_t *parse_select_expression(unsigned precedence,
2772 expression_t *compound)
2775 assert(token.type == '.' || token.type == T_MINUSGREATER);
2777 bool is_pointer = (token.type == T_MINUSGREATER);
2780 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2782 select->expression.type = EXPR_SELECT;
2783 select->compound = compound;
2785 if(token.type != T_IDENTIFIER) {
2786 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2787 return (expression_t*) select;
2789 symbol_t *symbol = token.v.symbol;
2790 select->symbol = symbol;
2793 type_t *type = compound->datatype;
2795 return make_invalid_expression();
2797 type_t *type_left = type;
2799 if(type->type != TYPE_POINTER) {
2800 parser_print_error_prefix();
2801 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2802 print_type_quoted(type);
2803 fputc('\n', stderr);
2804 return make_invalid_expression();
2806 pointer_type_t *pointer_type = (pointer_type_t*) type;
2807 type_left = pointer_type->points_to;
2809 type_left = skip_typeref(type_left);
2811 if(type_left->type != TYPE_COMPOUND_STRUCT
2812 && type_left->type != TYPE_COMPOUND_UNION) {
2813 parser_print_error_prefix();
2814 fprintf(stderr, "request for member '%s' in something not a struct or "
2815 "union, but ", symbol->string);
2816 print_type_quoted(type_left);
2817 fputc('\n', stderr);
2818 return make_invalid_expression();
2821 compound_type_t *compound_type = (compound_type_t*) type_left;
2822 declaration_t *declaration = compound_type->declaration;
2824 if(!declaration->init.is_defined) {
2825 parser_print_error_prefix();
2826 fprintf(stderr, "request for member '%s' of incomplete type ",
2828 print_type_quoted(type_left);
2829 fputc('\n', stderr);
2830 return make_invalid_expression();
2833 declaration_t *iter = declaration->context.declarations;
2834 for( ; iter != NULL; iter = iter->next) {
2835 if(iter->symbol == symbol) {
2840 parser_print_error_prefix();
2841 print_type_quoted(type_left);
2842 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2843 return make_invalid_expression();
2846 select->compound_entry = iter;
2847 select->expression.datatype = iter->type;
2848 return (expression_t*) select;
2851 static expression_t *parse_call_expression(unsigned precedence,
2852 expression_t *expression)
2855 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2856 call->expression.type = EXPR_CALL;
2857 call->function = expression;
2859 function_type_t *function_type;
2860 type_t *type = expression->datatype;
2861 if (type->type == TYPE_FUNCTION) {
2862 function_type = (function_type_t*) type;
2863 call->expression.datatype = function_type->result_type;
2864 } else if (type->type == TYPE_POINTER &&
2865 ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2866 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2867 function_type = (function_type_t*)ptr_type->points_to;
2868 call->expression.datatype = function_type->result_type;
2870 parser_print_error_prefix();
2871 fputs("called object '", stderr);
2872 print_expression(expression);
2873 fputs("' (type ", stderr);
2874 print_type_quoted(type);
2875 fputs(") is not a function\n", stderr);
2877 function_type = NULL;
2878 call->expression.datatype = NULL;
2881 /* parse arguments */
2884 if(token.type != ')') {
2885 call_argument_t *last_argument = NULL;
2888 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2890 argument->expression = parse_assignment_expression();
2891 if(last_argument == NULL) {
2892 call->arguments = argument;
2894 last_argument->next = argument;
2896 last_argument = argument;
2898 if(token.type != ',')
2905 if(function_type != NULL) {
2906 function_parameter_t *parameter = function_type->parameters;
2907 call_argument_t *argument = call->arguments;
2908 for( ; parameter != NULL && argument != NULL;
2909 parameter = parameter->next, argument = argument->next) {
2910 type_t *expected_type = parameter->type;
2911 /* TODO report context in error messages */
2912 argument->expression = create_implicit_cast(argument->expression,
2915 /* too few parameters */
2916 if(parameter != NULL) {
2917 parser_print_error_prefix();
2918 fprintf(stderr, "too few arguments to function '");
2919 print_expression(expression);
2920 fprintf(stderr, "'\n");
2921 } else if(argument != NULL) {
2922 /* too many parameters */
2923 if(!function_type->variadic
2924 && !function_type->unspecified_parameters) {
2925 parser_print_error_prefix();
2926 fprintf(stderr, "too many arguments to function '");
2927 print_expression(expression);
2928 fprintf(stderr, "'\n");
2930 /* do default promotion */
2931 for( ; argument != NULL; argument = argument->next) {
2932 type_t *type = argument->expression->datatype;
2937 if(is_type_integer(type)) {
2938 type = promote_integer(type);
2939 } else if(type == type_float) {
2942 argument->expression
2943 = create_implicit_cast(argument->expression, type);
2949 return (expression_t*) call;
2952 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
2954 static expression_t *parse_conditional_expression(unsigned precedence,
2955 expression_t *expression)
2959 conditional_expression_t *conditional
2960 = allocate_ast_zero(sizeof(conditional[0]));
2961 conditional->expression.type = EXPR_CONDITIONAL;
2962 conditional->condition = expression;
2965 type_t *condition_type_orig = conditional->condition->datatype;
2966 type_t *condition_type = skip_typeref(condition_type_orig);
2967 if(condition_type != NULL && !is_type_scalar(condition_type)) {
2968 type_error("expected a scalar type", expression->source_position,
2969 condition_type_orig);
2972 expression_t *const t_expr = parse_expression();
2973 conditional->true_expression = t_expr;
2975 expression_t *const f_expr = parse_sub_expression(precedence);
2976 conditional->false_expression = f_expr;
2978 type_t *const true_type = t_expr->datatype;
2979 if(true_type == NULL)
2980 return (expression_t*) conditional;
2981 type_t *const false_type = f_expr->datatype;
2982 if(false_type == NULL)
2983 return (expression_t*) conditional;
2985 type_t *const skipped_true_type = skip_typeref(true_type);
2986 type_t *const skipped_false_type = skip_typeref(false_type);
2989 if (skipped_true_type == skipped_false_type) {
2990 conditional->expression.datatype = skipped_true_type;
2991 } else if (is_type_arithmetic(skipped_true_type) &&
2992 is_type_arithmetic(skipped_false_type)) {
2993 type_t *const result = semantic_arithmetic(skipped_true_type,
2994 skipped_false_type);
2995 conditional->true_expression = create_implicit_cast(t_expr, result);
2996 conditional->false_expression = create_implicit_cast(f_expr, result);
2997 conditional->expression.datatype = result;
2998 } else if (skipped_true_type->type == TYPE_POINTER &&
2999 skipped_false_type->type == TYPE_POINTER &&
3000 true /* TODO compatible points_to types */) {
3002 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3003 skipped_false_type->type == TYPE_POINTER)
3004 || (is_null_ptr_const(skipped_false_type) &&
3005 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3007 } else if(/* 1 is pointer to object type, other is void* */ false) {
3010 type_error_incompatible("while parsing conditional",
3011 expression->source_position, true_type,
3012 skipped_false_type);
3015 return (expression_t*) conditional;
3018 static expression_t *parse_extension(unsigned precedence)
3020 eat(T___extension__);
3022 /* TODO enable extensions */
3024 return parse_sub_expression(precedence);
3027 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3029 eat(T___builtin_classify_type);
3031 classify_type_expression_t *const classify_type_expr =
3032 allocate_ast_zero(sizeof(classify_type_expr[0]));
3033 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3034 classify_type_expr->expression.datatype = type_int;
3037 expression_t *const expression = parse_sub_expression(precedence);
3039 classify_type_expr->type_expression = expression;
3041 return (expression_t*)classify_type_expr;
3044 static void semantic_incdec(unary_expression_t *expression)
3046 type_t *orig_type = expression->value->datatype;
3047 if(orig_type == NULL)
3050 type_t *type = skip_typeref(orig_type);
3051 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3052 /* TODO: improve error message */
3053 parser_print_error_prefix();
3054 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3058 expression->expression.datatype = orig_type;
3061 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3063 type_t *orig_type = expression->value->datatype;
3064 if(orig_type == NULL)
3067 type_t *type = skip_typeref(orig_type);
3068 if(!is_type_arithmetic(type)) {
3069 /* TODO: improve error message */
3070 parser_print_error_prefix();
3071 fprintf(stderr, "operation needs an arithmetic type\n");
3075 expression->expression.datatype = orig_type;
3078 static void semantic_unexpr_scalar(unary_expression_t *expression)
3080 type_t *orig_type = expression->value->datatype;
3081 if(orig_type == NULL)
3084 type_t *type = skip_typeref(orig_type);
3085 if (!is_type_scalar(type)) {
3086 parse_error("operand of ! must be of scalar type\n");
3090 expression->expression.datatype = orig_type;
3093 static void semantic_unexpr_integer(unary_expression_t *expression)
3095 type_t *orig_type = expression->value->datatype;
3096 if(orig_type == NULL)
3099 type_t *type = skip_typeref(orig_type);
3100 if (!is_type_integer(type)) {
3101 parse_error("operand of ~ must be of integer type\n");
3105 expression->expression.datatype = orig_type;
3108 static void semantic_dereference(unary_expression_t *expression)
3110 type_t *orig_type = expression->value->datatype;
3111 if(orig_type == NULL)
3114 type_t *type = skip_typeref(orig_type);
3115 switch (type->type) {
3117 array_type_t *const array_type = (array_type_t*)type;
3118 expression->expression.datatype = array_type->element_type;
3122 case TYPE_POINTER: {
3123 pointer_type_t *pointer_type = (pointer_type_t*)type;
3124 expression->expression.datatype = pointer_type->points_to;
3129 parser_print_error_prefix();
3130 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3131 print_type_quoted(orig_type);
3132 fputs(" given.\n", stderr);
3137 static void semantic_take_addr(unary_expression_t *expression)
3139 type_t *orig_type = expression->value->datatype;
3140 if(orig_type == NULL)
3143 expression_t *value = expression->value;
3144 if(value->type == EXPR_REFERENCE) {
3145 reference_expression_t *reference = (reference_expression_t*) value;
3146 declaration_t *declaration = reference->declaration;
3147 if(declaration != NULL) {
3148 declaration->address_taken = 1;
3152 expression->expression.datatype = make_pointer_type(orig_type, 0);
3155 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3156 static expression_t *parse_##unexpression_type(unsigned precedence) \
3160 unary_expression_t *unary_expression \
3161 = allocate_ast_zero(sizeof(unary_expression[0])); \
3162 unary_expression->expression.type = EXPR_UNARY; \
3163 unary_expression->type = unexpression_type; \
3164 unary_expression->value = parse_sub_expression(precedence); \
3166 sfunc(unary_expression); \
3168 return (expression_t*) unary_expression; \
3171 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3172 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3173 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3174 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3175 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3176 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3177 semantic_unexpr_integer)
3178 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3180 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3183 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3185 static expression_t *parse_##unexpression_type(unsigned precedence, \
3186 expression_t *left) \
3188 (void) precedence; \
3191 unary_expression_t *unary_expression \
3192 = allocate_ast_zero(sizeof(unary_expression[0])); \
3193 unary_expression->expression.type = EXPR_UNARY; \
3194 unary_expression->type = unexpression_type; \
3195 unary_expression->value = left; \
3197 sfunc(unary_expression); \
3199 return (expression_t*) unary_expression; \
3202 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3204 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3207 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3209 /* TODO: handle complex + imaginary types */
3211 /* § 6.3.1.8 Usual arithmetic conversions */
3212 if(type_left == type_long_double || type_right == type_long_double) {
3213 return type_long_double;
3214 } else if(type_left == type_double || type_right == type_double) {
3216 } else if(type_left == type_float || type_right == type_float) {
3220 type_right = promote_integer(type_right);
3221 type_left = promote_integer(type_left);
3223 if(type_left == type_right)
3226 bool signed_left = is_type_signed(type_left);
3227 bool signed_right = is_type_signed(type_right);
3228 if(get_rank(type_left) < get_rank(type_right)) {
3229 if(signed_left == signed_right || !signed_right) {
3235 if(signed_left == signed_right || !signed_left) {
3243 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3245 expression_t *left = expression->left;
3246 expression_t *right = expression->right;
3247 type_t *orig_type_left = left->datatype;
3248 type_t *orig_type_right = right->datatype;
3250 if(orig_type_left == NULL || orig_type_right == NULL)
3253 type_t *type_left = skip_typeref(orig_type_left);
3254 type_t *type_right = skip_typeref(orig_type_right);
3256 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3257 /* TODO: improve error message */
3258 parser_print_error_prefix();
3259 fprintf(stderr, "operation needs arithmetic types\n");
3263 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3264 expression->left = create_implicit_cast(left, arithmetic_type);
3265 expression->right = create_implicit_cast(right, arithmetic_type);
3266 expression->expression.datatype = arithmetic_type;
3269 static void semantic_shift_op(binary_expression_t *expression)
3271 expression_t *left = expression->left;
3272 expression_t *right = expression->right;
3273 type_t *orig_type_left = left->datatype;
3274 type_t *orig_type_right = right->datatype;
3276 if(orig_type_left == NULL || orig_type_right == NULL)
3279 type_t *type_left = skip_typeref(orig_type_left);
3280 type_t *type_right = skip_typeref(orig_type_right);
3282 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3283 /* TODO: improve error message */
3284 parser_print_error_prefix();
3285 fprintf(stderr, "operation needs integer types\n");
3289 type_left = promote_integer(type_left);
3290 type_right = promote_integer(type_right);
3292 expression->left = create_implicit_cast(left, type_left);
3293 expression->right = create_implicit_cast(right, type_right);
3294 expression->expression.datatype = type_left;
3297 static void semantic_add(binary_expression_t *expression)
3299 expression_t *left = expression->left;
3300 expression_t *right = expression->right;
3301 type_t *orig_type_left = left->datatype;
3302 type_t *orig_type_right = right->datatype;
3304 if(orig_type_left == NULL || orig_type_right == NULL)
3307 type_t *type_left = skip_typeref(orig_type_left);
3308 type_t *type_right = skip_typeref(orig_type_right);
3311 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3312 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3313 expression->left = create_implicit_cast(left, arithmetic_type);
3314 expression->right = create_implicit_cast(right, arithmetic_type);
3315 expression->expression.datatype = arithmetic_type;
3317 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3318 expression->expression.datatype = type_left;
3319 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3320 expression->expression.datatype = type_right;
3321 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3322 const array_type_t *const arr_type = (const array_type_t*)type_left;
3323 expression->expression.datatype =
3324 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3325 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3326 const array_type_t *const arr_type = (const array_type_t*)type_right;
3327 expression->expression.datatype =
3328 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3330 parser_print_error_prefix();
3331 fprintf(stderr, "invalid operands to binary + (");
3332 print_type_quoted(orig_type_left);
3333 fprintf(stderr, ", ");
3334 print_type_quoted(orig_type_right);
3335 fprintf(stderr, ")\n");
3339 static void semantic_sub(binary_expression_t *expression)
3341 expression_t *left = expression->left;
3342 expression_t *right = expression->right;
3343 type_t *orig_type_left = left->datatype;
3344 type_t *orig_type_right = right->datatype;
3346 if(orig_type_left == NULL || orig_type_right == NULL)
3349 type_t *type_left = skip_typeref(orig_type_left);
3350 type_t *type_right = skip_typeref(orig_type_right);
3353 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3354 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3355 expression->left = create_implicit_cast(left, arithmetic_type);
3356 expression->right = create_implicit_cast(right, arithmetic_type);
3357 expression->expression.datatype = arithmetic_type;
3359 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3360 expression->expression.datatype = type_left;
3361 } else if(type_left->type == TYPE_POINTER &&
3362 type_right->type == TYPE_POINTER) {
3363 if(!pointers_compatible(type_left, type_right)) {
3364 parser_print_error_prefix();
3365 fprintf(stderr, "pointers to incompatible objects to binary - (");
3366 print_type_quoted(orig_type_left);
3367 fprintf(stderr, ", ");
3368 print_type_quoted(orig_type_right);
3369 fprintf(stderr, ")\n");
3371 expression->expression.datatype = type_ptrdiff_t;
3374 parser_print_error_prefix();
3375 fprintf(stderr, "invalid operands to binary - (");
3376 print_type_quoted(orig_type_left);
3377 fprintf(stderr, ", ");
3378 print_type_quoted(orig_type_right);
3379 fprintf(stderr, ")\n");
3383 static void semantic_comparison(binary_expression_t *expression)
3385 expression_t *left = expression->left;
3386 expression_t *right = expression->right;
3387 type_t *orig_type_left = left->datatype;
3388 type_t *orig_type_right = right->datatype;
3390 if(orig_type_left == NULL || orig_type_right == NULL)
3393 type_t *type_left = skip_typeref(orig_type_left);
3394 type_t *type_right = skip_typeref(orig_type_right);
3396 /* TODO non-arithmetic types */
3397 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3398 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3399 expression->left = create_implicit_cast(left, arithmetic_type);
3400 expression->right = create_implicit_cast(right, arithmetic_type);
3401 expression->expression.datatype = arithmetic_type;
3402 } else if (type_left->type == TYPE_POINTER &&
3403 type_right->type == TYPE_POINTER) {
3404 /* TODO check compatibility */
3405 } else if (type_left->type == TYPE_POINTER) {
3406 expression->right = create_implicit_cast(right, type_left);
3407 } else if (type_right->type == TYPE_POINTER) {
3408 expression->left = create_implicit_cast(left, type_right);
3410 type_error_incompatible("invalid operands in comparison",
3411 expression->expression.source_position,
3412 type_left, type_right);
3414 expression->expression.datatype = type_int;
3417 static void semantic_arithmetic_assign(binary_expression_t *expression)
3419 expression_t *left = expression->left;
3420 expression_t *right = expression->right;
3421 type_t *orig_type_left = left->datatype;
3422 type_t *orig_type_right = right->datatype;
3424 if(orig_type_left == NULL || orig_type_right == NULL)
3427 type_t *type_left = skip_typeref(orig_type_left);
3428 type_t *type_right = skip_typeref(orig_type_right);
3430 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3431 /* TODO: improve error message */
3432 parser_print_error_prefix();
3433 fprintf(stderr, "operation needs arithmetic types\n");
3437 /* combined instructions are tricky. We can't create an implicit cast on
3438 * the left side, because we need the uncasted form for the store.
3439 * The ast2firm pass has to know that left_type must be right_type
3440 * for the arithmeitc operation and create a cast by itself */
3441 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3442 expression->right = create_implicit_cast(right, arithmetic_type);
3443 expression->expression.datatype = type_left;
3446 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3448 expression_t *left = expression->left;
3449 expression_t *right = expression->right;
3450 type_t *orig_type_left = left->datatype;
3451 type_t *orig_type_right = right->datatype;
3453 if(orig_type_left == NULL || orig_type_right == NULL)
3456 type_t *type_left = skip_typeref(orig_type_left);
3457 type_t *type_right = skip_typeref(orig_type_right);
3459 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3460 /* combined instructions are tricky. We can't create an implicit cast on
3461 * the left side, because we need the uncasted form for the store.
3462 * The ast2firm pass has to know that left_type must be right_type
3463 * for the arithmeitc operation and create a cast by itself */
3464 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3465 expression->right = create_implicit_cast(right, arithmetic_type);
3466 expression->expression.datatype = type_left;
3467 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3468 expression->expression.datatype = type_left;
3470 parser_print_error_prefix();
3471 fputs("Incompatible types ", stderr);
3472 print_type_quoted(orig_type_left);
3473 fputs(" and ", stderr);
3474 print_type_quoted(orig_type_right);
3475 fputs(" in assignment\n", stderr);
3480 static void semantic_logical_op(binary_expression_t *expression)
3482 expression_t *left = expression->left;
3483 expression_t *right = expression->right;
3484 type_t *orig_type_left = left->datatype;
3485 type_t *orig_type_right = right->datatype;
3487 if(orig_type_left == NULL || orig_type_right == NULL)
3490 type_t *type_left = skip_typeref(orig_type_left);
3491 type_t *type_right = skip_typeref(orig_type_right);
3493 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3494 /* TODO: improve error message */
3495 parser_print_error_prefix();
3496 fprintf(stderr, "operation needs scalar types\n");
3500 expression->expression.datatype = type_int;
3503 static void semantic_binexpr_assign(binary_expression_t *expression)
3505 expression_t *left = expression->left;
3506 type_t *type_left = left->datatype;
3508 if (type_left->type == TYPE_ARRAY) {
3509 parse_error("Cannot assign to arrays.");
3510 } else if (type_left != NULL) {
3511 semantic_assign(type_left, &expression->right, "assignment");
3514 expression->expression.datatype = type_left;
3517 static void semantic_comma(binary_expression_t *expression)
3519 expression->expression.datatype = expression->right->datatype;
3522 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3523 static expression_t *parse_##binexpression_type(unsigned precedence, \
3524 expression_t *left) \
3528 expression_t *right = parse_sub_expression(precedence + lr); \
3530 binary_expression_t *binexpr \
3531 = allocate_ast_zero(sizeof(binexpr[0])); \
3532 binexpr->expression.type = EXPR_BINARY; \
3533 binexpr->type = binexpression_type; \
3534 binexpr->left = left; \
3535 binexpr->right = right; \
3538 return (expression_t*) binexpr; \
3541 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3542 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3543 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3544 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3545 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3546 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3547 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3548 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3549 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3550 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3551 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3552 semantic_comparison, 1)
3553 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3554 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3555 semantic_comparison, 1)
3556 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3557 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3558 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3559 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3560 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3561 /* TODO shift has a bit special semantic */
3562 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3563 semantic_shift_op, 1)
3564 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3565 semantic_shift_op, 1)
3566 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3567 semantic_arithmetic_addsubb_assign, 0)
3568 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3569 semantic_arithmetic_addsubb_assign, 0)
3570 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3571 semantic_arithmetic_assign, 0)
3572 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3573 semantic_arithmetic_assign, 0)
3574 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3575 semantic_arithmetic_assign, 0)
3576 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3577 semantic_arithmetic_assign, 0)
3578 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3579 semantic_arithmetic_assign, 0)
3580 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3581 semantic_arithmetic_assign, 0)
3582 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3583 semantic_arithmetic_assign, 0)
3584 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3585 semantic_arithmetic_assign, 0)
3587 static expression_t *parse_sub_expression(unsigned precedence)
3589 if(token.type < 0) {
3590 return expected_expression_error();
3593 expression_parser_function_t *parser
3594 = &expression_parsers[token.type];
3595 source_position_t source_position = token.source_position;
3598 if(parser->parser != NULL) {
3599 left = parser->parser(parser->precedence);
3601 left = parse_primary_expression();
3603 assert(left != NULL);
3604 left->source_position = source_position;
3607 if(token.type < 0) {
3608 return expected_expression_error();
3611 parser = &expression_parsers[token.type];
3612 if(parser->infix_parser == NULL)
3614 if(parser->infix_precedence < precedence)
3617 left = parser->infix_parser(parser->infix_precedence, left);
3619 assert(left != NULL);
3620 assert(left->type != EXPR_UNKNOWN);
3621 left->source_position = source_position;
3627 static expression_t *parse_expression(void)
3629 return parse_sub_expression(1);
3634 static void register_expression_parser(parse_expression_function parser,
3635 int token_type, unsigned precedence)
3637 expression_parser_function_t *entry = &expression_parsers[token_type];
3639 if(entry->parser != NULL) {
3640 fprintf(stderr, "for token ");
3641 print_token_type(stderr, token_type);
3642 fprintf(stderr, "\n");
3643 panic("trying to register multiple expression parsers for a token");
3645 entry->parser = parser;
3646 entry->precedence = precedence;
3649 static void register_expression_infix_parser(
3650 parse_expression_infix_function parser, int token_type,
3651 unsigned precedence)
3653 expression_parser_function_t *entry = &expression_parsers[token_type];
3655 if(entry->infix_parser != NULL) {
3656 fprintf(stderr, "for token ");
3657 print_token_type(stderr, token_type);
3658 fprintf(stderr, "\n");
3659 panic("trying to register multiple infix expression parsers for a "
3662 entry->infix_parser = parser;
3663 entry->infix_precedence = precedence;
3666 static void init_expression_parsers(void)
3668 memset(&expression_parsers, 0, sizeof(expression_parsers));
3670 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3671 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3672 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3673 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3674 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3675 T_GREATERGREATER, 16);
3676 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3677 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3678 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3679 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3680 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3681 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3682 T_GREATEREQUAL, 14);
3683 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3684 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3685 T_EXCLAMATIONMARKEQUAL, 13);
3686 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3687 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3688 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3689 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3690 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3691 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3692 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3693 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3694 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3695 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3696 T_ASTERISKEQUAL, 2);
3697 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3698 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3700 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3701 T_LESSLESSEQUAL, 2);
3702 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3703 T_GREATERGREATEREQUAL, 2);
3704 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3706 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3708 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3711 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3713 register_expression_infix_parser(parse_array_expression, '[', 30);
3714 register_expression_infix_parser(parse_call_expression, '(', 30);
3715 register_expression_infix_parser(parse_select_expression, '.', 30);
3716 register_expression_infix_parser(parse_select_expression,
3717 T_MINUSGREATER, 30);
3718 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3720 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3723 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3724 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3725 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3726 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3727 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3728 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3729 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3730 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3731 register_expression_parser(parse_sizeof, T_sizeof, 25);
3732 register_expression_parser(parse_extension, T___extension__, 25);
3733 register_expression_parser(parse_builtin_classify_type,
3734 T___builtin_classify_type, 25);
3738 static statement_t *parse_case_statement(void)
3741 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3742 label->statement.type = STATEMENT_CASE_LABEL;
3743 label->statement.source_position = token.source_position;
3745 label->expression = parse_expression();
3748 label->statement.next = parse_statement();
3750 return (statement_t*) label;
3753 static statement_t *parse_default_statement(void)
3757 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3758 label->statement.type = STATEMENT_CASE_LABEL;
3759 label->statement.source_position = token.source_position;
3762 label->statement.next = parse_statement();
3764 return (statement_t*) label;
3767 static declaration_t *get_label(symbol_t *symbol)
3769 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3770 assert(current_function != NULL);
3771 /* if we found a label in the same function, then we already created the
3773 if(candidate != NULL
3774 && candidate->parent_context == ¤t_function->context) {
3778 /* otherwise we need to create a new one */
3779 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3780 declaration->namespc = NAMESPACE_LABEL;
3781 declaration->symbol = symbol;
3783 label_push(declaration);
3788 static statement_t *parse_label_statement(void)
3790 assert(token.type == T_IDENTIFIER);
3791 symbol_t *symbol = token.v.symbol;
3794 declaration_t *label = get_label(symbol);
3796 /* if source position is already set then the label is defined twice,
3797 * otherwise it was just mentioned in a goto so far */
3798 if(label->source_position.input_name != NULL) {
3799 parser_print_error_prefix();
3800 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3801 parser_print_error_prefix_pos(label->source_position);
3802 fprintf(stderr, "previous definition of '%s' was here\n",
3805 label->source_position = token.source_position;
3808 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3810 label_statement->statement.type = STATEMENT_LABEL;
3811 label_statement->statement.source_position = token.source_position;
3812 label_statement->label = label;
3816 if(token.type == '}') {
3817 parse_error("label at end of compound statement");
3818 return (statement_t*) label_statement;
3820 label_statement->label_statement = parse_statement();
3823 return (statement_t*) label_statement;
3826 static statement_t *parse_if(void)
3830 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3831 statement->statement.type = STATEMENT_IF;
3832 statement->statement.source_position = token.source_position;
3835 statement->condition = parse_expression();
3838 statement->true_statement = parse_statement();
3839 if(token.type == T_else) {
3841 statement->false_statement = parse_statement();
3844 return (statement_t*) statement;
3847 static statement_t *parse_switch(void)
3851 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3852 statement->statement.type = STATEMENT_SWITCH;
3853 statement->statement.source_position = token.source_position;
3856 statement->expression = parse_expression();
3858 statement->body = parse_statement();
3860 return (statement_t*) statement;
3863 static statement_t *parse_while(void)
3867 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3868 statement->statement.type = STATEMENT_WHILE;
3869 statement->statement.source_position = token.source_position;
3872 statement->condition = parse_expression();
3874 statement->body = parse_statement();
3876 return (statement_t*) statement;
3879 static statement_t *parse_do(void)
3883 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3884 statement->statement.type = STATEMENT_DO_WHILE;
3885 statement->statement.source_position = token.source_position;
3887 statement->body = parse_statement();
3890 statement->condition = parse_expression();
3894 return (statement_t*) statement;
3897 static statement_t *parse_for(void)
3901 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3902 statement->statement.type = STATEMENT_FOR;
3903 statement->statement.source_position = token.source_position;
3907 int top = environment_top();
3908 context_t *last_context = context;
3909 set_context(&statement->context);
3911 if(token.type != ';') {
3912 if(is_declaration_specifier(&token, false)) {
3913 parse_declaration();
3915 statement->initialisation = parse_expression();
3922 if(token.type != ';') {
3923 statement->condition = parse_expression();
3926 if(token.type != ')') {
3927 statement->step = parse_expression();
3930 statement->body = parse_statement();
3932 assert(context == &statement->context);
3933 set_context(last_context);
3934 environment_pop_to(top);
3936 return (statement_t*) statement;
3939 static statement_t *parse_goto(void)
3943 if(token.type != T_IDENTIFIER) {
3944 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3948 symbol_t *symbol = token.v.symbol;
3951 declaration_t *label = get_label(symbol);
3953 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3955 statement->statement.type = STATEMENT_GOTO;
3956 statement->statement.source_position = token.source_position;
3958 statement->label = label;
3962 return (statement_t*) statement;
3965 static statement_t *parse_continue(void)
3970 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3971 statement->type = STATEMENT_CONTINUE;
3972 statement->source_position = token.source_position;
3977 static statement_t *parse_break(void)
3982 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3983 statement->type = STATEMENT_BREAK;
3984 statement->source_position = token.source_position;
3989 static statement_t *parse_return(void)
3993 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3995 statement->statement.type = STATEMENT_RETURN;
3996 statement->statement.source_position = token.source_position;
3998 assert(current_function->type->type == TYPE_FUNCTION);
3999 function_type_t *function_type = (function_type_t*) current_function->type;
4000 type_t *return_type = function_type->result_type;
4002 expression_t *return_value;
4003 if(token.type != ';') {
4004 return_value = parse_expression();
4006 if(return_type == type_void && return_value->datatype != type_void) {
4007 parse_warning("'return' with a value, in function returning void");
4008 return_value = NULL;
4010 if(return_type != NULL) {
4011 semantic_assign(return_type, &return_value, "'return'");
4015 return_value = NULL;
4016 if(return_type != type_void) {
4017 parse_warning("'return' without value, in function returning "
4021 statement->return_value = return_value;
4025 return (statement_t*) statement;
4028 static statement_t *parse_declaration_statement(void)
4030 declaration_t *before = last_declaration;
4032 declaration_statement_t *statement
4033 = allocate_ast_zero(sizeof(statement[0]));
4034 statement->statement.type = STATEMENT_DECLARATION;
4035 statement->statement.source_position = token.source_position;
4037 declaration_specifiers_t specifiers;
4038 memset(&specifiers, 0, sizeof(specifiers));
4039 parse_declaration_specifiers(&specifiers);
4041 if(token.type == ';') {
4044 parse_init_declarators(&specifiers);
4047 if(before == NULL) {
4048 statement->declarations_begin = context->declarations;
4050 statement->declarations_begin = before->next;
4052 statement->declarations_end = last_declaration;
4054 return (statement_t*) statement;
4057 static statement_t *parse_expression_statement(void)
4059 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4060 statement->statement.type = STATEMENT_EXPRESSION;
4061 statement->statement.source_position = token.source_position;
4063 statement->expression = parse_expression();
4067 return (statement_t*) statement;
4070 static statement_t *parse_statement(void)
4072 statement_t *statement = NULL;
4074 /* declaration or statement */
4075 switch(token.type) {
4077 statement = parse_case_statement();
4081 statement = parse_default_statement();
4085 statement = parse_compound_statement();
4089 statement = parse_if();
4093 statement = parse_switch();
4097 statement = parse_while();
4101 statement = parse_do();
4105 statement = parse_for();
4109 statement = parse_goto();
4113 statement = parse_continue();
4117 statement = parse_break();
4121 statement = parse_return();
4130 if(look_ahead(1)->type == ':') {
4131 statement = parse_label_statement();
4135 if(is_typedef_symbol(token.v.symbol)) {
4136 statement = parse_declaration_statement();
4140 statement = parse_expression_statement();
4143 case T___extension__:
4144 /* this can be a prefix to a declaration or an expression statement */
4145 /* we simply eat it now and parse the rest with tail recursion */
4148 } while(token.type == T___extension__);
4149 statement = parse_statement();
4153 statement = parse_declaration_statement();
4157 statement = parse_expression_statement();
4161 assert(statement == NULL || statement->source_position.input_name != NULL);
4166 static statement_t *parse_compound_statement(void)
4168 compound_statement_t *compound_statement
4169 = allocate_ast_zero(sizeof(compound_statement[0]));
4170 compound_statement->statement.type = STATEMENT_COMPOUND;
4171 compound_statement->statement.source_position = token.source_position;
4175 int top = environment_top();
4176 context_t *last_context = context;
4177 set_context(&compound_statement->context);
4179 statement_t *last_statement = NULL;
4181 while(token.type != '}' && token.type != T_EOF) {
4182 statement_t *statement = parse_statement();
4183 if(statement == NULL)
4186 if(last_statement != NULL) {
4187 last_statement->next = statement;
4189 compound_statement->statements = statement;
4192 while(statement->next != NULL)
4193 statement = statement->next;
4195 last_statement = statement;
4198 if(token.type != '}') {
4199 parser_print_error_prefix_pos(
4200 compound_statement->statement.source_position);
4201 fprintf(stderr, "end of file while looking for closing '}'\n");
4205 assert(context == &compound_statement->context);
4206 set_context(last_context);
4207 environment_pop_to(top);
4209 return (statement_t*) compound_statement;
4212 static translation_unit_t *parse_translation_unit(void)
4214 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4216 assert(global_context == NULL);
4217 global_context = &unit->context;
4219 assert(context == NULL);
4220 set_context(&unit->context);
4222 while(token.type != T_EOF) {
4223 parse_declaration();
4226 assert(context == &unit->context);
4228 last_declaration = NULL;
4230 assert(global_context == &unit->context);
4231 global_context = NULL;
4236 translation_unit_t *parse(void)
4238 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4239 label_stack = NEW_ARR_F(stack_entry_t, 0);
4240 found_error = false;
4242 type_set_output(stderr);
4243 ast_set_output(stderr);
4245 lookahead_bufpos = 0;
4246 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4249 translation_unit_t *unit = parse_translation_unit();
4251 DEL_ARR_F(environment_stack);
4252 DEL_ARR_F(label_stack);
4260 void init_parser(void)
4262 init_expression_parsers();
4263 obstack_init(&temp_obst);
4265 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4266 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4267 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4268 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4269 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4270 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4271 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4272 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4273 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4274 type_string = make_pointer_type(type_const_char, 0);
4277 void exit_parser(void)
4279 obstack_free(&temp_obst, NULL);