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)
2525 eat(T___FUNCTION__);
2528 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2529 expression->expression.type = EXPR_FUNCTION;
2530 expression->expression.datatype = type_string;
2531 expression->value = "TODO: FUNCTION";
2533 return (expression_t*) expression;
2536 static expression_t *parse_pretty_function_keyword(void)
2538 eat(T___PRETTY_FUNCTION__);
2541 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2542 expression->expression.type = EXPR_PRETTY_FUNCTION;
2543 expression->expression.datatype = type_string;
2544 expression->value = "TODO: PRETTY FUNCTION";
2546 return (expression_t*) expression;
2549 static designator_t *parse_designator(void)
2551 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2553 if(token.type != T_IDENTIFIER) {
2554 parse_error_expected("while parsing member designator",
2559 result->symbol = token.v.symbol;
2562 designator_t *last_designator = result;
2564 if(token.type == '.') {
2566 if(token.type != T_IDENTIFIER) {
2567 parse_error_expected("while parsing member designator",
2572 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2573 designator->symbol = token.v.symbol;
2576 last_designator->next = designator;
2577 last_designator = designator;
2580 if(token.type == '[') {
2582 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2583 designator->array_access = parse_expression();
2584 if(designator->array_access == NULL) {
2590 last_designator->next = designator;
2591 last_designator = designator;
2600 static expression_t *parse_offsetof(void)
2602 eat(T___builtin_offsetof);
2604 offsetof_expression_t *expression
2605 = allocate_ast_zero(sizeof(expression[0]));
2606 expression->expression.type = EXPR_OFFSETOF;
2607 expression->expression.datatype = type_size_t;
2610 expression->type = parse_typename();
2612 expression->designator = parse_designator();
2615 return (expression_t*) expression;
2618 static expression_t *parse_va_arg(void)
2620 eat(T___builtin_va_arg);
2622 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2623 expression->expression.type = EXPR_VA_ARG;
2626 expression->arg = parse_assignment_expression();
2628 expression->expression.datatype = parse_typename();
2631 return (expression_t*) expression;
2634 static expression_t *parse_builtin_symbol(void)
2636 builtin_symbol_expression_t *expression
2637 = allocate_ast_zero(sizeof(expression[0]));
2638 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2640 /* TODO: set datatype */
2642 expression->symbol = token.v.symbol;
2646 return (expression_t*) expression;
2649 static expression_t *parse_primary_expression(void)
2651 switch(token.type) {
2653 return parse_int_const();
2654 case T_FLOATINGPOINT:
2655 return parse_float_const();
2656 case T_STRING_LITERAL:
2657 return parse_string_const();
2659 return parse_reference();
2660 case T___FUNCTION__:
2661 return parse_function_keyword();
2662 case T___PRETTY_FUNCTION__:
2663 return parse_pretty_function_keyword();
2664 case T___builtin_offsetof:
2665 return parse_offsetof();
2666 case T___builtin_va_arg:
2667 return parse_va_arg();
2668 case T___builtin_expect:
2669 case T___builtin_va_start:
2670 case T___builtin_va_end:
2671 return parse_builtin_symbol();
2674 return parse_brace_expression();
2677 parser_print_error_prefix();
2678 fprintf(stderr, "unexpected token ");
2679 print_token(stderr, &token);
2680 fprintf(stderr, "\n");
2683 return make_invalid_expression();
2686 static expression_t *parse_array_expression(unsigned precedence,
2687 expression_t *array_ref)
2693 array_access_expression_t *array_access
2694 = allocate_ast_zero(sizeof(array_access[0]));
2696 array_access->expression.type = EXPR_ARRAY_ACCESS;
2697 array_access->array_ref = array_ref;
2698 array_access->index = parse_expression();
2700 type_t *type = array_ref->datatype;
2702 if(type->type == TYPE_POINTER) {
2703 pointer_type_t *pointer = (pointer_type_t*) type;
2704 array_access->expression.datatype = pointer->points_to;
2705 } else if(type->type == TYPE_ARRAY) {
2706 array_type_t *array_type = (array_type_t*) type;
2707 array_access->expression.datatype = array_type->element_type;
2709 parser_print_error_prefix();
2710 fprintf(stderr, "array access on object with non-pointer type ");
2711 print_type_quoted(type);
2712 fprintf(stderr, "\n");
2716 if(token.type != ']') {
2717 parse_error_expected("Problem while parsing array access", ']', 0);
2718 return (expression_t*) array_access;
2722 return (expression_t*) array_access;
2725 static bool is_declaration_specifier(const token_t *token,
2726 bool only_type_specifiers)
2728 switch(token->type) {
2732 return is_typedef_symbol(token->v.symbol);
2735 if(only_type_specifiers)
2744 static expression_t *parse_sizeof(unsigned precedence)
2748 sizeof_expression_t *sizeof_expression
2749 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2750 sizeof_expression->expression.type = EXPR_SIZEOF;
2751 sizeof_expression->expression.datatype = type_size_t;
2753 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2755 sizeof_expression->type = parse_typename();
2758 expression_t *expression = parse_sub_expression(precedence);
2759 sizeof_expression->type = expression->datatype;
2760 sizeof_expression->size_expression = expression;
2763 return (expression_t*) sizeof_expression;
2766 static expression_t *parse_select_expression(unsigned precedence,
2767 expression_t *compound)
2770 assert(token.type == '.' || token.type == T_MINUSGREATER);
2772 bool is_pointer = (token.type == T_MINUSGREATER);
2775 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2777 select->expression.type = EXPR_SELECT;
2778 select->compound = compound;
2780 if(token.type != T_IDENTIFIER) {
2781 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2782 return (expression_t*) select;
2784 symbol_t *symbol = token.v.symbol;
2785 select->symbol = symbol;
2788 type_t *type = compound->datatype;
2790 return make_invalid_expression();
2792 type_t *type_left = type;
2794 if(type->type != TYPE_POINTER) {
2795 parser_print_error_prefix();
2796 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2797 print_type_quoted(type);
2798 fputc('\n', stderr);
2799 return make_invalid_expression();
2801 pointer_type_t *pointer_type = (pointer_type_t*) type;
2802 type_left = pointer_type->points_to;
2804 type_left = skip_typeref(type_left);
2806 if(type_left->type != TYPE_COMPOUND_STRUCT
2807 && type_left->type != TYPE_COMPOUND_UNION) {
2808 parser_print_error_prefix();
2809 fprintf(stderr, "request for member '%s' in something not a struct or "
2810 "union, but ", symbol->string);
2811 print_type_quoted(type_left);
2812 fputc('\n', stderr);
2813 return make_invalid_expression();
2816 compound_type_t *compound_type = (compound_type_t*) type_left;
2817 declaration_t *declaration = compound_type->declaration;
2819 if(!declaration->init.is_defined) {
2820 parser_print_error_prefix();
2821 fprintf(stderr, "request for member '%s' of incomplete type ",
2823 print_type_quoted(type_left);
2824 fputc('\n', stderr);
2825 return make_invalid_expression();
2828 declaration_t *iter = declaration->context.declarations;
2829 for( ; iter != NULL; iter = iter->next) {
2830 if(iter->symbol == symbol) {
2835 parser_print_error_prefix();
2836 print_type_quoted(type_left);
2837 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2838 return make_invalid_expression();
2841 select->compound_entry = iter;
2842 select->expression.datatype = iter->type;
2843 return (expression_t*) select;
2846 static expression_t *parse_call_expression(unsigned precedence,
2847 expression_t *expression)
2850 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2851 call->expression.type = EXPR_CALL;
2852 call->function = expression;
2854 function_type_t *function_type;
2855 type_t *type = expression->datatype;
2856 if (type->type == TYPE_FUNCTION) {
2857 function_type = (function_type_t*) type;
2858 call->expression.datatype = function_type->result_type;
2859 } else if (type->type == TYPE_POINTER &&
2860 ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2861 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2862 function_type = (function_type_t*)ptr_type->points_to;
2863 call->expression.datatype = function_type->result_type;
2865 parser_print_error_prefix();
2866 fputs("called object '", stderr);
2867 print_expression(expression);
2868 fputs("' (type ", stderr);
2869 print_type_quoted(type);
2870 fputs(") is not a function\n", stderr);
2872 function_type = NULL;
2873 call->expression.datatype = NULL;
2876 /* parse arguments */
2879 if(token.type != ')') {
2880 call_argument_t *last_argument = NULL;
2883 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2885 argument->expression = parse_assignment_expression();
2886 if(last_argument == NULL) {
2887 call->arguments = argument;
2889 last_argument->next = argument;
2891 last_argument = argument;
2893 if(token.type != ',')
2900 if(function_type != NULL) {
2901 function_parameter_t *parameter = function_type->parameters;
2902 call_argument_t *argument = call->arguments;
2903 for( ; parameter != NULL && argument != NULL;
2904 parameter = parameter->next, argument = argument->next) {
2905 type_t *expected_type = parameter->type;
2906 /* TODO report context in error messages */
2907 argument->expression = create_implicit_cast(argument->expression,
2910 /* too few parameters */
2911 if(parameter != NULL) {
2912 parser_print_error_prefix();
2913 fprintf(stderr, "too few arguments to function '");
2914 print_expression(expression);
2915 fprintf(stderr, "'\n");
2916 } else if(argument != NULL) {
2917 /* too many parameters */
2918 if(!function_type->variadic
2919 && !function_type->unspecified_parameters) {
2920 parser_print_error_prefix();
2921 fprintf(stderr, "too many arguments to function '");
2922 print_expression(expression);
2923 fprintf(stderr, "'\n");
2925 /* do default promotion */
2926 for( ; argument != NULL; argument = argument->next) {
2927 type_t *type = argument->expression->datatype;
2932 if(is_type_integer(type)) {
2933 type = promote_integer(type);
2934 } else if(type == type_float) {
2937 argument->expression
2938 = create_implicit_cast(argument->expression, type);
2944 return (expression_t*) call;
2947 static type_t *get_type_after_conversion(const type_t *type1,
2948 const type_t *type2)
2952 return (type_t*) type1;
2955 static expression_t *parse_conditional_expression(unsigned precedence,
2956 expression_t *expression)
2960 conditional_expression_t *conditional
2961 = allocate_ast_zero(sizeof(conditional[0]));
2962 conditional->expression.type = EXPR_CONDITIONAL;
2963 conditional->condition = expression;
2966 type_t *condition_type_orig = conditional->condition->datatype;
2967 type_t *condition_type = skip_typeref(condition_type_orig);
2968 if(condition_type != NULL && !is_type_scalar(condition_type)) {
2969 type_error("expected a scalar type", expression->source_position,
2970 condition_type_orig);
2973 conditional->true_expression = parse_expression();
2975 conditional->false_expression = parse_sub_expression(precedence);
2977 type_t *true_type = conditional->true_expression->datatype;
2978 if(true_type == NULL)
2979 return (expression_t*) conditional;
2980 type_t *false_type = conditional->false_expression->datatype;
2981 if(false_type == NULL)
2982 return (expression_t*) conditional;
2984 type_t *const skipped_true_type = skip_typeref(true_type);
2985 type_t *const skipped_false_type = skip_typeref(false_type);
2988 if (skipped_true_type == skipped_false_type) {
2989 conditional->expression.datatype = skipped_true_type;
2990 } else if (is_type_arithmetic(skipped_true_type) &&
2991 is_type_arithmetic(skipped_false_type)) {
2992 type_t *const result = get_type_after_conversion(skipped_true_type,
2993 skipped_false_type);
2994 /* TODO: create implicit convs if necessary */
2995 conditional->expression.datatype = result;
2996 } else if (skipped_true_type->type == TYPE_POINTER &&
2997 skipped_false_type->type == TYPE_POINTER &&
2998 true /* TODO compatible points_to types */) {
3000 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3001 skipped_false_type->type == TYPE_POINTER)
3002 || (is_null_ptr_const(skipped_false_type) &&
3003 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3005 } else if(/* 1 is pointer to object type, other is void* */ false) {
3008 type_error_incompatible("while parsing conditional",
3009 expression->source_position, true_type,
3010 skipped_false_type);
3013 return (expression_t*) conditional;
3016 static expression_t *parse_extension(unsigned precedence)
3018 eat(T___extension__);
3020 /* TODO enable extensions */
3022 return parse_sub_expression(precedence);
3025 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3027 eat(T___builtin_classify_type);
3029 classify_type_expression_t *const classify_type_expr =
3030 allocate_ast_zero(sizeof(classify_type_expr[0]));
3031 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3032 classify_type_expr->expression.datatype = type_int;
3035 expression_t *const expression = parse_sub_expression(precedence);
3037 classify_type_expr->type_expression = expression;
3039 return (expression_t*)classify_type_expr;
3042 static void semantic_incdec(unary_expression_t *expression)
3044 type_t *orig_type = expression->value->datatype;
3045 if(orig_type == NULL)
3048 type_t *type = skip_typeref(orig_type);
3049 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3050 /* TODO: improve error message */
3051 parser_print_error_prefix();
3052 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3056 expression->expression.datatype = orig_type;
3059 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3061 type_t *orig_type = expression->value->datatype;
3062 if(orig_type == NULL)
3065 type_t *type = skip_typeref(orig_type);
3066 if(!is_type_arithmetic(type)) {
3067 /* TODO: improve error message */
3068 parser_print_error_prefix();
3069 fprintf(stderr, "operation needs an arithmetic type\n");
3073 expression->expression.datatype = orig_type;
3076 static void semantic_unexpr_scalar(unary_expression_t *expression)
3078 type_t *orig_type = expression->value->datatype;
3079 if(orig_type == NULL)
3082 type_t *type = skip_typeref(orig_type);
3083 if (!is_type_scalar(type)) {
3084 parse_error("operand of ! must be of scalar type\n");
3088 expression->expression.datatype = orig_type;
3091 static void semantic_unexpr_integer(unary_expression_t *expression)
3093 type_t *orig_type = expression->value->datatype;
3094 if(orig_type == NULL)
3097 type_t *type = skip_typeref(orig_type);
3098 if (!is_type_integer(type)) {
3099 parse_error("operand of ~ must be of integer type\n");
3103 expression->expression.datatype = orig_type;
3106 static void semantic_dereference(unary_expression_t *expression)
3108 type_t *orig_type = expression->value->datatype;
3109 if(orig_type == NULL)
3112 type_t *type = skip_typeref(orig_type);
3113 switch (type->type) {
3115 array_type_t *const array_type = (array_type_t*)type;
3116 expression->expression.datatype = array_type->element_type;
3120 case TYPE_POINTER: {
3121 pointer_type_t *pointer_type = (pointer_type_t*)type;
3122 expression->expression.datatype = pointer_type->points_to;
3127 parser_print_error_prefix();
3128 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3129 print_type_quoted(orig_type);
3130 fputs(" given.\n", stderr);
3135 static void semantic_take_addr(unary_expression_t *expression)
3137 type_t *orig_type = expression->value->datatype;
3138 if(orig_type == NULL)
3141 expression_t *value = expression->value;
3142 if(value->type == EXPR_REFERENCE) {
3143 reference_expression_t *reference = (reference_expression_t*) value;
3144 declaration_t *declaration = reference->declaration;
3145 if(declaration != NULL) {
3146 declaration->address_taken = 1;
3150 expression->expression.datatype = make_pointer_type(orig_type, 0);
3153 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3154 static expression_t *parse_##unexpression_type(unsigned precedence) \
3158 unary_expression_t *unary_expression \
3159 = allocate_ast_zero(sizeof(unary_expression[0])); \
3160 unary_expression->expression.type = EXPR_UNARY; \
3161 unary_expression->type = unexpression_type; \
3162 unary_expression->value = parse_sub_expression(precedence); \
3164 sfunc(unary_expression); \
3166 return (expression_t*) unary_expression; \
3169 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3170 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3171 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3172 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3173 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3174 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3175 semantic_unexpr_integer)
3176 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3178 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3181 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3183 static expression_t *parse_##unexpression_type(unsigned precedence, \
3184 expression_t *left) \
3186 (void) precedence; \
3189 unary_expression_t *unary_expression \
3190 = allocate_ast_zero(sizeof(unary_expression[0])); \
3191 unary_expression->expression.type = EXPR_UNARY; \
3192 unary_expression->type = unexpression_type; \
3193 unary_expression->value = left; \
3195 sfunc(unary_expression); \
3197 return (expression_t*) unary_expression; \
3200 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3202 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3205 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3207 /* TODO: handle complex + imaginary types */
3209 /* § 6.3.1.8 Usual arithmetic conversions */
3210 if(type_left == type_long_double || type_right == type_long_double) {
3211 return type_long_double;
3212 } else if(type_left == type_double || type_right == type_double) {
3214 } else if(type_left == type_float || type_right == type_float) {
3218 type_right = promote_integer(type_right);
3219 type_left = promote_integer(type_left);
3221 if(type_left == type_right)
3224 bool signed_left = is_type_signed(type_left);
3225 bool signed_right = is_type_signed(type_right);
3226 if(get_rank(type_left) < get_rank(type_right)) {
3227 if(signed_left == signed_right || !signed_right) {
3233 if(signed_left == signed_right || !signed_left) {
3241 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3243 expression_t *left = expression->left;
3244 expression_t *right = expression->right;
3245 type_t *orig_type_left = left->datatype;
3246 type_t *orig_type_right = right->datatype;
3248 if(orig_type_left == NULL || orig_type_right == NULL)
3251 type_t *type_left = skip_typeref(orig_type_left);
3252 type_t *type_right = skip_typeref(orig_type_right);
3254 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3255 /* TODO: improve error message */
3256 parser_print_error_prefix();
3257 fprintf(stderr, "operation needs arithmetic types\n");
3261 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3262 expression->left = create_implicit_cast(left, arithmetic_type);
3263 expression->right = create_implicit_cast(right, arithmetic_type);
3264 expression->expression.datatype = arithmetic_type;
3267 static void semantic_shift_op(binary_expression_t *expression)
3269 expression_t *left = expression->left;
3270 expression_t *right = expression->right;
3271 type_t *orig_type_left = left->datatype;
3272 type_t *orig_type_right = right->datatype;
3274 if(orig_type_left == NULL || orig_type_right == NULL)
3277 type_t *type_left = skip_typeref(orig_type_left);
3278 type_t *type_right = skip_typeref(orig_type_right);
3280 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3281 /* TODO: improve error message */
3282 parser_print_error_prefix();
3283 fprintf(stderr, "operation needs integer types\n");
3287 type_left = promote_integer(type_left);
3288 type_right = promote_integer(type_right);
3290 expression->left = create_implicit_cast(left, type_left);
3291 expression->right = create_implicit_cast(right, type_right);
3292 expression->expression.datatype = type_left;
3295 static void semantic_add(binary_expression_t *expression)
3297 expression_t *left = expression->left;
3298 expression_t *right = expression->right;
3299 type_t *orig_type_left = left->datatype;
3300 type_t *orig_type_right = right->datatype;
3302 if(orig_type_left == NULL || orig_type_right == NULL)
3305 type_t *type_left = skip_typeref(orig_type_left);
3306 type_t *type_right = skip_typeref(orig_type_right);
3309 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3310 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3311 expression->left = create_implicit_cast(left, arithmetic_type);
3312 expression->right = create_implicit_cast(right, arithmetic_type);
3313 expression->expression.datatype = arithmetic_type;
3315 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3316 expression->expression.datatype = type_left;
3317 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3318 expression->expression.datatype = type_right;
3319 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3320 const array_type_t *const arr_type = (const array_type_t*)type_left;
3321 expression->expression.datatype =
3322 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3323 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3324 const array_type_t *const arr_type = (const array_type_t*)type_right;
3325 expression->expression.datatype =
3326 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3328 parser_print_error_prefix();
3329 fprintf(stderr, "invalid operands to binary + (");
3330 print_type_quoted(orig_type_left);
3331 fprintf(stderr, ", ");
3332 print_type_quoted(orig_type_right);
3333 fprintf(stderr, ")\n");
3337 static void semantic_sub(binary_expression_t *expression)
3339 expression_t *left = expression->left;
3340 expression_t *right = expression->right;
3341 type_t *orig_type_left = left->datatype;
3342 type_t *orig_type_right = right->datatype;
3344 if(orig_type_left == NULL || orig_type_right == NULL)
3347 type_t *type_left = skip_typeref(orig_type_left);
3348 type_t *type_right = skip_typeref(orig_type_right);
3351 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3352 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3353 expression->left = create_implicit_cast(left, arithmetic_type);
3354 expression->right = create_implicit_cast(right, arithmetic_type);
3355 expression->expression.datatype = arithmetic_type;
3357 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3358 expression->expression.datatype = type_left;
3359 } else if(type_left->type == TYPE_POINTER &&
3360 type_right->type == TYPE_POINTER) {
3361 if(!pointers_compatible(type_left, type_right)) {
3362 parser_print_error_prefix();
3363 fprintf(stderr, "pointers to incompatible objects to binary - (");
3364 print_type_quoted(orig_type_left);
3365 fprintf(stderr, ", ");
3366 print_type_quoted(orig_type_right);
3367 fprintf(stderr, ")\n");
3369 expression->expression.datatype = type_ptrdiff_t;
3372 parser_print_error_prefix();
3373 fprintf(stderr, "invalid operands to binary - (");
3374 print_type_quoted(orig_type_left);
3375 fprintf(stderr, ", ");
3376 print_type_quoted(orig_type_right);
3377 fprintf(stderr, ")\n");
3381 static void semantic_comparison(binary_expression_t *expression)
3383 expression_t *left = expression->left;
3384 expression_t *right = expression->right;
3385 type_t *orig_type_left = left->datatype;
3386 type_t *orig_type_right = right->datatype;
3388 if(orig_type_left == NULL || orig_type_right == NULL)
3391 type_t *type_left = skip_typeref(orig_type_left);
3392 type_t *type_right = skip_typeref(orig_type_right);
3394 /* TODO non-arithmetic types */
3395 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3396 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3397 expression->left = create_implicit_cast(left, arithmetic_type);
3398 expression->right = create_implicit_cast(right, arithmetic_type);
3399 expression->expression.datatype = arithmetic_type;
3400 } else if (type_left->type == TYPE_POINTER &&
3401 type_right->type == TYPE_POINTER) {
3402 /* TODO check compatibility */
3403 } else if (type_left->type == TYPE_POINTER) {
3404 expression->right = create_implicit_cast(right, type_left);
3405 } else if (type_right->type == TYPE_POINTER) {
3406 expression->left = create_implicit_cast(left, type_right);
3408 type_error_incompatible("invalid operands in comparison",
3409 expression->expression.source_position,
3410 type_left, type_right);
3412 expression->expression.datatype = type_int;
3415 static void semantic_arithmetic_assign(binary_expression_t *expression)
3417 expression_t *left = expression->left;
3418 expression_t *right = expression->right;
3419 type_t *orig_type_left = left->datatype;
3420 type_t *orig_type_right = right->datatype;
3422 if(orig_type_left == NULL || orig_type_right == NULL)
3425 type_t *type_left = skip_typeref(orig_type_left);
3426 type_t *type_right = skip_typeref(orig_type_right);
3428 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3429 /* TODO: improve error message */
3430 parser_print_error_prefix();
3431 fprintf(stderr, "operation needs arithmetic types\n");
3435 /* combined instructions are tricky. We can't create an implicit cast on
3436 * the left side, because we need the uncasted form for the store.
3437 * The ast2firm pass has to know that left_type must be right_type
3438 * for the arithmeitc operation and create a cast by itself */
3439 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3440 expression->right = create_implicit_cast(right, arithmetic_type);
3441 expression->expression.datatype = type_left;
3444 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3446 expression_t *left = expression->left;
3447 expression_t *right = expression->right;
3448 type_t *orig_type_left = left->datatype;
3449 type_t *orig_type_right = right->datatype;
3451 if(orig_type_left == NULL || orig_type_right == NULL)
3454 type_t *type_left = skip_typeref(orig_type_left);
3455 type_t *type_right = skip_typeref(orig_type_right);
3457 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3458 /* combined instructions are tricky. We can't create an implicit cast on
3459 * the left side, because we need the uncasted form for the store.
3460 * The ast2firm pass has to know that left_type must be right_type
3461 * for the arithmeitc operation and create a cast by itself */
3462 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3463 expression->right = create_implicit_cast(right, arithmetic_type);
3464 expression->expression.datatype = type_left;
3465 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3466 expression->expression.datatype = type_left;
3468 parser_print_error_prefix();
3469 fputs("Incompatible types ", stderr);
3470 print_type_quoted(orig_type_left);
3471 fputs(" and ", stderr);
3472 print_type_quoted(orig_type_right);
3473 fputs(" in assignment\n", stderr);
3478 static void semantic_logical_op(binary_expression_t *expression)
3480 expression_t *left = expression->left;
3481 expression_t *right = expression->right;
3482 type_t *orig_type_left = left->datatype;
3483 type_t *orig_type_right = right->datatype;
3485 if(orig_type_left == NULL || orig_type_right == NULL)
3488 type_t *type_left = skip_typeref(orig_type_left);
3489 type_t *type_right = skip_typeref(orig_type_right);
3491 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3492 /* TODO: improve error message */
3493 parser_print_error_prefix();
3494 fprintf(stderr, "operation needs scalar types\n");
3498 expression->expression.datatype = type_int;
3501 static void semantic_binexpr_assign(binary_expression_t *expression)
3503 expression_t *left = expression->left;
3504 type_t *type_left = left->datatype;
3506 if (type_left->type == TYPE_ARRAY) {
3507 parse_error("Cannot assign to arrays.");
3508 } else if (type_left != NULL) {
3509 semantic_assign(type_left, &expression->right, "assignment");
3512 expression->expression.datatype = type_left;
3515 static void semantic_comma(binary_expression_t *expression)
3517 expression->expression.datatype = expression->right->datatype;
3520 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3521 static expression_t *parse_##binexpression_type(unsigned precedence, \
3522 expression_t *left) \
3526 expression_t *right = parse_sub_expression(precedence + lr); \
3528 binary_expression_t *binexpr \
3529 = allocate_ast_zero(sizeof(binexpr[0])); \
3530 binexpr->expression.type = EXPR_BINARY; \
3531 binexpr->type = binexpression_type; \
3532 binexpr->left = left; \
3533 binexpr->right = right; \
3536 return (expression_t*) binexpr; \
3539 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3540 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3541 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3542 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3543 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3544 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3545 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3546 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3547 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3548 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3549 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3550 semantic_comparison, 1)
3551 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3552 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3553 semantic_comparison, 1)
3554 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3555 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3556 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3557 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3558 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3559 /* TODO shift has a bit special semantic */
3560 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3561 semantic_shift_op, 1)
3562 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3563 semantic_shift_op, 1)
3564 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3565 semantic_arithmetic_addsubb_assign, 0)
3566 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3567 semantic_arithmetic_addsubb_assign, 0)
3568 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3569 semantic_arithmetic_assign, 0)
3570 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3571 semantic_arithmetic_assign, 0)
3572 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3573 semantic_arithmetic_assign, 0)
3574 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3575 semantic_arithmetic_assign, 0)
3576 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3577 semantic_arithmetic_assign, 0)
3578 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3579 semantic_arithmetic_assign, 0)
3580 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3581 semantic_arithmetic_assign, 0)
3582 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3583 semantic_arithmetic_assign, 0)
3585 static expression_t *parse_sub_expression(unsigned precedence)
3587 if(token.type < 0) {
3588 return expected_expression_error();
3591 expression_parser_function_t *parser
3592 = &expression_parsers[token.type];
3593 source_position_t source_position = token.source_position;
3596 if(parser->parser != NULL) {
3597 left = parser->parser(parser->precedence);
3599 left = parse_primary_expression();
3601 assert(left != NULL);
3602 left->source_position = source_position;
3605 if(token.type < 0) {
3606 return expected_expression_error();
3609 parser = &expression_parsers[token.type];
3610 if(parser->infix_parser == NULL)
3612 if(parser->infix_precedence < precedence)
3615 left = parser->infix_parser(parser->infix_precedence, left);
3617 assert(left != NULL);
3618 assert(left->type != EXPR_UNKNOWN);
3619 left->source_position = source_position;
3625 static expression_t *parse_expression(void)
3627 return parse_sub_expression(1);
3632 static void register_expression_parser(parse_expression_function parser,
3633 int token_type, unsigned precedence)
3635 expression_parser_function_t *entry = &expression_parsers[token_type];
3637 if(entry->parser != NULL) {
3638 fprintf(stderr, "for token ");
3639 print_token_type(stderr, token_type);
3640 fprintf(stderr, "\n");
3641 panic("trying to register multiple expression parsers for a token");
3643 entry->parser = parser;
3644 entry->precedence = precedence;
3647 static void register_expression_infix_parser(
3648 parse_expression_infix_function parser, int token_type,
3649 unsigned precedence)
3651 expression_parser_function_t *entry = &expression_parsers[token_type];
3653 if(entry->infix_parser != NULL) {
3654 fprintf(stderr, "for token ");
3655 print_token_type(stderr, token_type);
3656 fprintf(stderr, "\n");
3657 panic("trying to register multiple infix expression parsers for a "
3660 entry->infix_parser = parser;
3661 entry->infix_precedence = precedence;
3664 static void init_expression_parsers(void)
3666 memset(&expression_parsers, 0, sizeof(expression_parsers));
3668 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3669 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3670 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3671 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3672 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3673 T_GREATERGREATER, 16);
3674 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3675 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3676 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3677 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3678 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3679 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3680 T_GREATEREQUAL, 14);
3681 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3682 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3683 T_EXCLAMATIONMARKEQUAL, 13);
3684 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3685 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3686 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3687 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3688 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3689 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3690 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3691 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3692 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3693 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3694 T_ASTERISKEQUAL, 2);
3695 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3696 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3698 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3699 T_LESSLESSEQUAL, 2);
3700 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3701 T_GREATERGREATEREQUAL, 2);
3702 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3704 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3706 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3709 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3711 register_expression_infix_parser(parse_array_expression, '[', 30);
3712 register_expression_infix_parser(parse_call_expression, '(', 30);
3713 register_expression_infix_parser(parse_select_expression, '.', 30);
3714 register_expression_infix_parser(parse_select_expression,
3715 T_MINUSGREATER, 30);
3716 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3718 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3721 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3722 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3723 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3724 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3725 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3726 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3727 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3728 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3729 register_expression_parser(parse_sizeof, T_sizeof, 25);
3730 register_expression_parser(parse_extension, T___extension__, 25);
3731 register_expression_parser(parse_builtin_classify_type,
3732 T___builtin_classify_type, 25);
3736 static statement_t *parse_case_statement(void)
3739 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3740 label->statement.type = STATEMENT_CASE_LABEL;
3741 label->statement.source_position = token.source_position;
3743 label->expression = parse_expression();
3746 label->statement.next = parse_statement();
3748 return (statement_t*) label;
3751 static statement_t *parse_default_statement(void)
3755 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3756 label->statement.type = STATEMENT_CASE_LABEL;
3757 label->statement.source_position = token.source_position;
3760 label->statement.next = parse_statement();
3762 return (statement_t*) label;
3765 static declaration_t *get_label(symbol_t *symbol)
3767 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3768 assert(current_function != NULL);
3769 /* if we found a label in the same function, then we already created the
3771 if(candidate != NULL
3772 && candidate->parent_context == ¤t_function->context) {
3776 /* otherwise we need to create a new one */
3777 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3778 declaration->namespc = NAMESPACE_LABEL;
3779 declaration->symbol = symbol;
3781 label_push(declaration);
3786 static statement_t *parse_label_statement(void)
3788 assert(token.type == T_IDENTIFIER);
3789 symbol_t *symbol = token.v.symbol;
3792 declaration_t *label = get_label(symbol);
3794 /* if source position is already set then the label is defined twice,
3795 * otherwise it was just mentioned in a goto so far */
3796 if(label->source_position.input_name != NULL) {
3797 parser_print_error_prefix();
3798 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3799 parser_print_error_prefix_pos(label->source_position);
3800 fprintf(stderr, "previous definition of '%s' was here\n",
3803 label->source_position = token.source_position;
3806 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3808 label_statement->statement.type = STATEMENT_LABEL;
3809 label_statement->statement.source_position = token.source_position;
3810 label_statement->label = label;
3814 if(token.type == '}') {
3815 parse_error("label at end of compound statement");
3816 return (statement_t*) label_statement;
3818 label_statement->label_statement = parse_statement();
3821 return (statement_t*) label_statement;
3824 static statement_t *parse_if(void)
3828 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3829 statement->statement.type = STATEMENT_IF;
3830 statement->statement.source_position = token.source_position;
3833 statement->condition = parse_expression();
3836 statement->true_statement = parse_statement();
3837 if(token.type == T_else) {
3839 statement->false_statement = parse_statement();
3842 return (statement_t*) statement;
3845 static statement_t *parse_switch(void)
3849 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3850 statement->statement.type = STATEMENT_SWITCH;
3851 statement->statement.source_position = token.source_position;
3854 statement->expression = parse_expression();
3856 statement->body = parse_statement();
3858 return (statement_t*) statement;
3861 static statement_t *parse_while(void)
3865 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3866 statement->statement.type = STATEMENT_WHILE;
3867 statement->statement.source_position = token.source_position;
3870 statement->condition = parse_expression();
3872 statement->body = parse_statement();
3874 return (statement_t*) statement;
3877 static statement_t *parse_do(void)
3881 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3882 statement->statement.type = STATEMENT_DO_WHILE;
3883 statement->statement.source_position = token.source_position;
3885 statement->body = parse_statement();
3888 statement->condition = parse_expression();
3892 return (statement_t*) statement;
3895 static statement_t *parse_for(void)
3899 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3900 statement->statement.type = STATEMENT_FOR;
3901 statement->statement.source_position = token.source_position;
3905 int top = environment_top();
3906 context_t *last_context = context;
3907 set_context(&statement->context);
3909 if(token.type != ';') {
3910 if(is_declaration_specifier(&token, false)) {
3911 parse_declaration();
3913 statement->initialisation = parse_expression();
3920 if(token.type != ';') {
3921 statement->condition = parse_expression();
3924 if(token.type != ')') {
3925 statement->step = parse_expression();
3928 statement->body = parse_statement();
3930 assert(context == &statement->context);
3931 set_context(last_context);
3932 environment_pop_to(top);
3934 return (statement_t*) statement;
3937 static statement_t *parse_goto(void)
3941 if(token.type != T_IDENTIFIER) {
3942 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3946 symbol_t *symbol = token.v.symbol;
3949 declaration_t *label = get_label(symbol);
3951 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3953 statement->statement.type = STATEMENT_GOTO;
3954 statement->statement.source_position = token.source_position;
3956 statement->label = label;
3960 return (statement_t*) statement;
3963 static statement_t *parse_continue(void)
3968 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3969 statement->type = STATEMENT_CONTINUE;
3970 statement->source_position = token.source_position;
3975 static statement_t *parse_break(void)
3980 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3981 statement->type = STATEMENT_BREAK;
3982 statement->source_position = token.source_position;
3987 static statement_t *parse_return(void)
3991 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3993 statement->statement.type = STATEMENT_RETURN;
3994 statement->statement.source_position = token.source_position;
3996 assert(current_function->type->type == TYPE_FUNCTION);
3997 function_type_t *function_type = (function_type_t*) current_function->type;
3998 type_t *return_type = function_type->result_type;
4000 expression_t *return_value;
4001 if(token.type != ';') {
4002 return_value = parse_expression();
4004 if(return_type == type_void && return_value->datatype != type_void) {
4005 parse_warning("'return' with a value, in function returning void");
4006 return_value = NULL;
4008 if(return_type != NULL) {
4009 semantic_assign(return_type, &return_value, "'return'");
4013 return_value = NULL;
4014 if(return_type != type_void) {
4015 parse_warning("'return' without value, in function returning "
4019 statement->return_value = return_value;
4023 return (statement_t*) statement;
4026 static statement_t *parse_declaration_statement(void)
4028 declaration_t *before = last_declaration;
4030 declaration_statement_t *statement
4031 = allocate_ast_zero(sizeof(statement[0]));
4032 statement->statement.type = STATEMENT_DECLARATION;
4033 statement->statement.source_position = token.source_position;
4035 declaration_specifiers_t specifiers;
4036 memset(&specifiers, 0, sizeof(specifiers));
4037 parse_declaration_specifiers(&specifiers);
4039 if(token.type == ';') {
4042 parse_init_declarators(&specifiers);
4045 if(before == NULL) {
4046 statement->declarations_begin = context->declarations;
4048 statement->declarations_begin = before->next;
4050 statement->declarations_end = last_declaration;
4052 return (statement_t*) statement;
4055 static statement_t *parse_expression_statement(void)
4057 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4058 statement->statement.type = STATEMENT_EXPRESSION;
4059 statement->statement.source_position = token.source_position;
4061 statement->expression = parse_expression();
4065 return (statement_t*) statement;
4068 static statement_t *parse_statement(void)
4070 statement_t *statement = NULL;
4072 /* declaration or statement */
4073 switch(token.type) {
4075 statement = parse_case_statement();
4079 statement = parse_default_statement();
4083 statement = parse_compound_statement();
4087 statement = parse_if();
4091 statement = parse_switch();
4095 statement = parse_while();
4099 statement = parse_do();
4103 statement = parse_for();
4107 statement = parse_goto();
4111 statement = parse_continue();
4115 statement = parse_break();
4119 statement = parse_return();
4128 if(look_ahead(1)->type == ':') {
4129 statement = parse_label_statement();
4133 if(is_typedef_symbol(token.v.symbol)) {
4134 statement = parse_declaration_statement();
4138 statement = parse_expression_statement();
4141 case T___extension__:
4142 /* this can be a prefix to a declaration or an expression statement */
4143 /* we simply eat it now and parse the rest with tail recursion */
4146 } while(token.type == T___extension__);
4147 statement = parse_statement();
4151 statement = parse_declaration_statement();
4155 statement = parse_expression_statement();
4159 assert(statement == NULL || statement->source_position.input_name != NULL);
4164 static statement_t *parse_compound_statement(void)
4166 compound_statement_t *compound_statement
4167 = allocate_ast_zero(sizeof(compound_statement[0]));
4168 compound_statement->statement.type = STATEMENT_COMPOUND;
4169 compound_statement->statement.source_position = token.source_position;
4173 int top = environment_top();
4174 context_t *last_context = context;
4175 set_context(&compound_statement->context);
4177 statement_t *last_statement = NULL;
4179 while(token.type != '}' && token.type != T_EOF) {
4180 statement_t *statement = parse_statement();
4181 if(statement == NULL)
4184 if(last_statement != NULL) {
4185 last_statement->next = statement;
4187 compound_statement->statements = statement;
4190 while(statement->next != NULL)
4191 statement = statement->next;
4193 last_statement = statement;
4196 if(token.type != '}') {
4197 parser_print_error_prefix_pos(
4198 compound_statement->statement.source_position);
4199 fprintf(stderr, "end of file while looking for closing '}'\n");
4203 assert(context == &compound_statement->context);
4204 set_context(last_context);
4205 environment_pop_to(top);
4207 return (statement_t*) compound_statement;
4210 static translation_unit_t *parse_translation_unit(void)
4212 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4214 assert(global_context == NULL);
4215 global_context = &unit->context;
4217 assert(context == NULL);
4218 set_context(&unit->context);
4220 while(token.type != T_EOF) {
4221 parse_declaration();
4224 assert(context == &unit->context);
4226 last_declaration = NULL;
4228 assert(global_context == &unit->context);
4229 global_context = NULL;
4234 translation_unit_t *parse(void)
4236 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4237 label_stack = NEW_ARR_F(stack_entry_t, 0);
4238 found_error = false;
4240 type_set_output(stderr);
4241 ast_set_output(stderr);
4243 lookahead_bufpos = 0;
4244 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4247 translation_unit_t *unit = parse_translation_unit();
4249 DEL_ARR_F(environment_stack);
4250 DEL_ARR_F(label_stack);
4258 void init_parser(void)
4260 init_expression_parsers();
4261 obstack_init(&temp_obst);
4263 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4264 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4265 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4266 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4267 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4268 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4269 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4270 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4271 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4272 type_string = make_pointer_type(type_const_char, 0);
4275 void exit_parser(void)
4277 obstack_free(&temp_obst, NULL);