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;
28 typedef struct declaration_specifiers_t declaration_specifiers_t;
29 struct declaration_specifiers_t {
30 source_position_t source_position;
31 unsigned char storage_class;
36 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
39 static token_t lookahead_buffer[MAX_LOOKAHEAD];
40 static int lookahead_bufpos;
41 static stack_entry_t *environment_stack = NULL;
42 static stack_entry_t *label_stack = NULL;
43 static context_t *global_context = NULL;
44 static context_t *context = NULL;
45 static declaration_t *last_declaration = NULL;
46 static declaration_t *current_function = NULL;
47 static struct obstack temp_obst;
48 static bool found_error;
50 static type_t *type_int = NULL;
51 static type_t *type_long_double = NULL;
52 static type_t *type_double = NULL;
53 static type_t *type_float = NULL;
54 static type_t *type_char = NULL;
55 static type_t *type_string = NULL;
56 static type_t *type_void = NULL;
57 static type_t *type_void_ptr = NULL;
59 type_t *type_size_t = NULL;
60 type_t *type_ptrdiff_t = NULL;
61 type_t *type_wchar_t = NULL;
62 type_t *type_wchar_t_ptr = NULL;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(void);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 #define STORAGE_CLASSES \
83 #define TYPE_QUALIFIERS \
89 #ifdef PROVIDE_COMPLEX
90 #define COMPLEX_SPECIFIERS \
92 #define IMAGINARY_SPECIFIERS \
95 #define COMPLEX_SPECIFIERS
96 #define IMAGINARY_SPECIFIERS
99 #define TYPE_SPECIFIERS \
117 #define DECLARATION_START \
122 #define TYPENAME_START \
126 static void *allocate_ast_zero(size_t size)
128 void *res = allocate_ast(size);
129 memset(res, 0, size);
133 static size_t get_statement_struct_size(statement_type_t type)
135 static const size_t sizes[] = {
136 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
137 [STATEMENT_RETURN] = sizeof(return_statement_t),
138 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
139 [STATEMENT_IF] = sizeof(if_statement_t),
140 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
141 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
142 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
143 [STATEMENT_BREAK] = sizeof(statement_base_t),
144 [STATEMENT_GOTO] = sizeof(goto_statement_t),
145 [STATEMENT_LABEL] = sizeof(label_statement_t),
146 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
147 [STATEMENT_WHILE] = sizeof(while_statement_t),
148 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
149 [STATEMENT_FOR] = sizeof(for_statement_t),
150 [STATEMENT_ASM] = sizeof(asm_statement_t)
152 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
153 assert(type <= STATEMENT_ASM);
154 assert(sizes[type] != 0);
158 static statement_t *allocate_statement_zero(statement_type_t type)
160 size_t size = get_statement_struct_size(type);
161 statement_t *res = allocate_ast_zero(size);
163 res->base.type = type;
168 static size_t get_expression_struct_size(expression_type_t type)
170 static const size_t sizes[] = {
171 [EXPR_INVALID] = sizeof(expression_base_t),
172 [EXPR_REFERENCE] = sizeof(reference_expression_t),
173 [EXPR_CONST] = sizeof(const_expression_t),
174 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
175 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
176 [EXPR_CALL] = sizeof(call_expression_t),
177 [EXPR_UNARY] = sizeof(unary_expression_t),
178 [EXPR_BINARY] = sizeof(binary_expression_t),
179 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
180 [EXPR_SELECT] = sizeof(select_expression_t),
181 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
182 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
183 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
184 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
185 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
186 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
187 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
188 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
189 [EXPR_STATEMENT] = sizeof(statement_expression_t)
191 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
192 assert(type <= EXPR_STATEMENT);
193 assert(sizes[type] != 0);
197 static expression_t *allocate_expression_zero(expression_type_t type)
199 size_t size = get_expression_struct_size(type);
200 expression_t *res = allocate_ast_zero(size);
202 res->base.type = type;
206 static size_t get_type_struct_size(type_type_t type)
208 static const size_t sizes[] = {
209 [TYPE_ATOMIC] = sizeof(atomic_type_t),
210 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
211 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
212 [TYPE_ENUM] = sizeof(enum_type_t),
213 [TYPE_FUNCTION] = sizeof(function_type_t),
214 [TYPE_POINTER] = sizeof(pointer_type_t),
215 [TYPE_ARRAY] = sizeof(array_type_t),
216 [TYPE_BUILTIN] = sizeof(builtin_type_t),
217 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
218 [TYPE_TYPEOF] = sizeof(typeof_type_t),
220 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
221 assert(type <= TYPE_TYPEOF);
222 assert(sizes[type] != 0);
226 static type_t *allocate_type_zero(type_type_t type)
228 size_t size = get_type_struct_size(type);
229 type_t *res = obstack_alloc(type_obst, size);
230 memset(res, 0, size);
232 res->base.type = type;
236 static size_t get_initializer_size(initializer_type_t type)
238 static const size_t sizes[] = {
239 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
240 [INITIALIZER_STRING] = sizeof(initializer_string_t),
241 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
242 [INITIALIZER_LIST] = sizeof(initializer_list_t)
244 assert(type < sizeof(sizes) / sizeof(*sizes));
245 assert(sizes[type] != 0);
249 static initializer_t *allocate_initializer(initializer_type_t type)
251 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
257 static void free_type(void *type)
259 obstack_free(type_obst, type);
263 * returns the top element of the environment stack
265 static size_t environment_top(void)
267 return ARR_LEN(environment_stack);
270 static size_t label_top(void)
272 return ARR_LEN(label_stack);
277 static inline void next_token(void)
279 token = lookahead_buffer[lookahead_bufpos];
280 lookahead_buffer[lookahead_bufpos] = lexer_token;
283 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
286 print_token(stderr, &token);
287 fprintf(stderr, "\n");
291 static inline const token_t *look_ahead(int num)
293 assert(num > 0 && num <= MAX_LOOKAHEAD);
294 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
295 return &lookahead_buffer[pos];
298 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
300 static void error(void)
303 #ifdef ABORT_ON_ERROR
308 static void parser_print_prefix_pos(const source_position_t source_position)
310 fputs(source_position.input_name, stderr);
312 fprintf(stderr, "%u", source_position.linenr);
316 static void parser_print_error_prefix_pos(
317 const source_position_t source_position)
319 parser_print_prefix_pos(source_position);
320 fputs("error: ", stderr);
324 static void parser_print_error_prefix(void)
326 parser_print_error_prefix_pos(token.source_position);
329 static void parse_error(const char *message)
331 parser_print_error_prefix();
332 fprintf(stderr, "parse error: %s\n", message);
335 static void parser_print_warning_prefix_pos(
336 const source_position_t source_position)
338 parser_print_prefix_pos(source_position);
339 fputs("warning: ", stderr);
342 static void parser_print_warning_prefix(void)
344 parser_print_warning_prefix_pos(token.source_position);
347 static void parse_warning_pos(const source_position_t source_position,
348 const char *const message)
350 parser_print_prefix_pos(source_position);
351 fprintf(stderr, "warning: %s\n", message);
354 static void parse_warning(const char *message)
356 parse_warning_pos(token.source_position, message);
359 static void parse_error_expected(const char *message, ...)
364 if(message != NULL) {
365 parser_print_error_prefix();
366 fprintf(stderr, "%s\n", message);
368 parser_print_error_prefix();
369 fputs("Parse error: got ", stderr);
370 print_token(stderr, &token);
371 fputs(", expected ", stderr);
373 va_start(args, message);
374 token_type_t token_type = va_arg(args, token_type_t);
375 while(token_type != 0) {
379 fprintf(stderr, ", ");
381 print_token_type(stderr, token_type);
382 token_type = va_arg(args, token_type_t);
385 fprintf(stderr, "\n");
388 static void print_type_quoted(type_t *type)
395 static void type_error(const char *msg, const source_position_t source_position,
398 parser_print_error_prefix_pos(source_position);
399 fprintf(stderr, "%s, but found type ", msg);
400 print_type_quoted(type);
404 static void type_error_incompatible(const char *msg,
405 const source_position_t source_position, type_t *type1, type_t *type2)
407 parser_print_error_prefix_pos(source_position);
408 fprintf(stderr, "%s, incompatible types: ", msg);
409 print_type_quoted(type1);
410 fprintf(stderr, " - ");
411 print_type_quoted(type2);
412 fprintf(stderr, ")\n");
415 static void eat_block(void)
417 if(token.type == '{')
420 while(token.type != '}') {
421 if(token.type == T_EOF)
423 if(token.type == '{') {
432 static void eat_statement(void)
434 while(token.type != ';') {
435 if(token.type == T_EOF)
437 if(token.type == '}')
439 if(token.type == '{') {
448 static void eat_brace(void)
450 if(token.type == '(')
453 while(token.type != ')') {
454 if(token.type == T_EOF)
456 if(token.type == ')' || token.type == ';' || token.type == '}') {
459 if(token.type == '(') {
463 if(token.type == '{') {
472 #define expect(expected) \
473 if(UNLIKELY(token.type != (expected))) { \
474 parse_error_expected(NULL, (expected), 0); \
480 #define expect_block(expected) \
481 if(UNLIKELY(token.type != (expected))) { \
482 parse_error_expected(NULL, (expected), 0); \
488 #define expect_void(expected) \
489 if(UNLIKELY(token.type != (expected))) { \
490 parse_error_expected(NULL, (expected), 0); \
496 static void set_context(context_t *new_context)
498 context = new_context;
500 last_declaration = new_context->declarations;
501 if(last_declaration != NULL) {
502 while(last_declaration->next != NULL) {
503 last_declaration = last_declaration->next;
509 * called when we find a 2nd declarator for an identifier we already have a
512 static bool is_compatible_declaration(declaration_t *declaration,
513 declaration_t *previous)
515 /* happens for K&R style function parameters */
516 if(previous->type == NULL) {
517 previous->type = declaration->type;
521 type_t *type1 = skip_typeref(declaration->type);
522 type_t *type2 = skip_typeref(previous->type);
524 return types_compatible(type1, type2);
527 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
529 declaration_t *declaration = symbol->declaration;
530 for( ; declaration != NULL; declaration = declaration->symbol_next) {
531 if(declaration->namespc == namespc)
538 static const char *get_namespace_prefix(namespace_t namespc)
541 case NAMESPACE_NORMAL:
543 case NAMESPACE_UNION:
545 case NAMESPACE_STRUCT:
549 case NAMESPACE_LABEL:
552 panic("invalid namespace found");
556 * pushs an environment_entry on the environment stack and links the
557 * corresponding symbol to the new entry
559 static declaration_t *stack_push(stack_entry_t **stack_ptr,
560 declaration_t *declaration,
561 context_t *parent_context)
563 symbol_t *symbol = declaration->symbol;
564 namespace_t namespc = (namespace_t)declaration->namespc;
566 /* a declaration should be only pushed once */
567 declaration->parent_context = parent_context;
569 declaration_t *previous_declaration = get_declaration(symbol, namespc);
570 assert(declaration != previous_declaration);
571 if(previous_declaration != NULL
572 && previous_declaration->parent_context == context) {
573 if(!is_compatible_declaration(declaration, previous_declaration)) {
574 parser_print_error_prefix_pos(declaration->source_position);
575 fprintf(stderr, "definition of symbol '%s%s' with type ",
576 get_namespace_prefix(namespc), symbol->string);
577 print_type_quoted(declaration->type);
579 parser_print_error_prefix_pos(
580 previous_declaration->source_position);
581 fprintf(stderr, "is incompatible with previous declaration "
583 print_type_quoted(previous_declaration->type);
586 unsigned old_storage_class = previous_declaration->storage_class;
587 unsigned new_storage_class = declaration->storage_class;
588 if (current_function == NULL) {
589 if (old_storage_class != STORAGE_CLASS_STATIC &&
590 new_storage_class == STORAGE_CLASS_STATIC) {
591 parser_print_error_prefix_pos(declaration->source_position);
593 "static declaration of '%s' follows non-static declaration\n",
595 parser_print_error_prefix_pos(previous_declaration->source_position);
596 fprintf(stderr, "previous declaration of '%s' was here\n",
599 if (old_storage_class == STORAGE_CLASS_EXTERN) {
600 if (new_storage_class == STORAGE_CLASS_NONE) {
601 previous_declaration->storage_class = STORAGE_CLASS_NONE;
604 parser_print_warning_prefix_pos(declaration->source_position);
605 fprintf(stderr, "redundant declaration for '%s'\n",
607 parser_print_warning_prefix_pos(previous_declaration->source_position);
608 fprintf(stderr, "previous declaration of '%s' was here\n",
613 if (old_storage_class == STORAGE_CLASS_EXTERN &&
614 new_storage_class == STORAGE_CLASS_EXTERN) {
615 parser_print_warning_prefix_pos(declaration->source_position);
616 fprintf(stderr, "redundant extern declaration for '%s'\n",
618 parser_print_warning_prefix_pos(previous_declaration->source_position);
619 fprintf(stderr, "previous declaration of '%s' was here\n",
622 parser_print_error_prefix_pos(declaration->source_position);
623 if (old_storage_class == new_storage_class) {
624 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
626 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
628 parser_print_error_prefix_pos(previous_declaration->source_position);
629 fprintf(stderr, "previous declaration of '%s' was here\n",
634 return previous_declaration;
637 /* remember old declaration */
639 entry.symbol = symbol;
640 entry.old_declaration = symbol->declaration;
641 entry.namespc = (unsigned short) namespc;
642 ARR_APP1(stack_entry_t, *stack_ptr, entry);
644 /* replace/add declaration into declaration list of the symbol */
645 if(symbol->declaration == NULL) {
646 symbol->declaration = declaration;
648 declaration_t *iter_last = NULL;
649 declaration_t *iter = symbol->declaration;
650 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
651 /* replace an entry? */
652 if(iter->namespc == namespc) {
653 if(iter_last == NULL) {
654 symbol->declaration = declaration;
656 iter_last->symbol_next = declaration;
658 declaration->symbol_next = iter->symbol_next;
663 assert(iter_last->symbol_next == NULL);
664 iter_last->symbol_next = declaration;
671 static declaration_t *environment_push(declaration_t *declaration)
673 assert(declaration->source_position.input_name != NULL);
674 return stack_push(&environment_stack, declaration, context);
677 static declaration_t *label_push(declaration_t *declaration)
679 return stack_push(&label_stack, declaration, ¤t_function->context);
683 * pops symbols from the environment stack until @p new_top is the top element
685 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
687 stack_entry_t *stack = *stack_ptr;
688 size_t top = ARR_LEN(stack);
691 assert(new_top <= top);
695 for(i = top; i > new_top; --i) {
696 stack_entry_t *entry = &stack[i - 1];
698 declaration_t *old_declaration = entry->old_declaration;
699 symbol_t *symbol = entry->symbol;
700 namespace_t namespc = (namespace_t)entry->namespc;
702 /* replace/remove declaration */
703 declaration_t *declaration = symbol->declaration;
704 assert(declaration != NULL);
705 if(declaration->namespc == namespc) {
706 if(old_declaration == NULL) {
707 symbol->declaration = declaration->symbol_next;
709 symbol->declaration = old_declaration;
712 declaration_t *iter_last = declaration;
713 declaration_t *iter = declaration->symbol_next;
714 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
715 /* replace an entry? */
716 if(iter->namespc == namespc) {
717 assert(iter_last != NULL);
718 iter_last->symbol_next = old_declaration;
719 old_declaration->symbol_next = iter->symbol_next;
723 assert(iter != NULL);
727 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
730 static void environment_pop_to(size_t new_top)
732 stack_pop_to(&environment_stack, new_top);
735 static void label_pop_to(size_t new_top)
737 stack_pop_to(&label_stack, new_top);
741 static int get_rank(const type_t *type)
743 assert(!is_typeref(type));
744 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
745 * and esp. footnote 108). However we can't fold constants (yet), so we
746 * can't decide wether unsigned int is possible, while int always works.
747 * (unsigned int would be preferable when possible... for stuff like
748 * struct { enum { ... } bla : 4; } ) */
749 if(type->type == TYPE_ENUM)
750 return ATOMIC_TYPE_INT;
752 assert(type->type == TYPE_ATOMIC);
753 const atomic_type_t *atomic_type = &type->atomic;
754 atomic_type_type_t atype = atomic_type->atype;
758 static type_t *promote_integer(type_t *type)
760 if(get_rank(type) < ATOMIC_TYPE_INT)
766 static expression_t *create_cast_expression(expression_t *expression,
769 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
771 cast->unary.type = UNEXPR_CAST_IMPLICIT;
772 cast->unary.value = expression;
773 cast->base.datatype = dest_type;
778 static bool is_null_pointer_constant(const expression_t *expression)
780 /* skip void* cast */
781 if(expression->type == EXPR_UNARY) {
782 const unary_expression_t *unary = &expression->unary;
783 if(unary->type == UNEXPR_CAST
784 && expression->base.datatype == type_void_ptr) {
785 expression = unary->value;
789 /* TODO: not correct yet, should be any constant integer expression
790 * which evaluates to 0 */
791 if (expression->type != EXPR_CONST)
794 type_t *const type = skip_typeref(expression->base.datatype);
795 if (!is_type_integer(type))
798 return expression->conste.v.int_value == 0;
801 static expression_t *create_implicit_cast(expression_t *expression,
804 type_t *source_type = expression->base.datatype;
806 if(source_type == NULL)
809 source_type = skip_typeref(source_type);
810 dest_type = skip_typeref(dest_type);
812 if(source_type == dest_type)
815 switch (dest_type->type) {
817 /* TODO warning for implicitly converting to enum */
819 if (source_type->type != TYPE_ATOMIC &&
820 source_type->type != TYPE_ENUM) {
821 panic("casting of non-atomic types not implemented yet");
824 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
825 type_error_incompatible("can't cast types",
826 expression->base.source_position, source_type,
831 return create_cast_expression(expression, dest_type);
834 switch (source_type->type) {
836 if (is_null_pointer_constant(expression)) {
837 return create_cast_expression(expression, dest_type);
842 if (pointers_compatible(source_type, dest_type)) {
843 return create_cast_expression(expression, dest_type);
848 array_type_t *array_type = &source_type->array;
849 pointer_type_t *pointer_type = &dest_type->pointer;
850 if (types_compatible(array_type->element_type,
851 pointer_type->points_to)) {
852 return create_cast_expression(expression, dest_type);
858 panic("casting of non-atomic types not implemented yet");
861 type_error_incompatible("can't implicitly cast types",
862 expression->base.source_position, source_type, dest_type);
866 panic("casting of non-atomic types not implemented yet");
870 /** Implements the rules from § 6.5.16.1 */
871 static void semantic_assign(type_t *orig_type_left, expression_t **right,
874 type_t *orig_type_right = (*right)->base.datatype;
876 if(orig_type_right == NULL)
879 type_t *const type_left = skip_typeref(orig_type_left);
880 type_t *const type_right = skip_typeref(orig_type_right);
882 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
883 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
884 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
885 && is_type_pointer(type_right))) {
886 *right = create_implicit_cast(*right, type_left);
890 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
891 pointer_type_t *pointer_type_left = &type_left->pointer;
892 pointer_type_t *pointer_type_right = &type_right->pointer;
893 type_t *points_to_left = pointer_type_left->points_to;
894 type_t *points_to_right = pointer_type_right->points_to;
896 points_to_left = skip_typeref(points_to_left);
897 points_to_right = skip_typeref(points_to_right);
899 /* the left type has all qualifiers from the right type */
900 unsigned missing_qualifiers
901 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
902 if(missing_qualifiers != 0) {
903 parser_print_error_prefix();
904 fprintf(stderr, "destination type ");
905 print_type_quoted(type_left);
906 fprintf(stderr, " in %s from type ", context);
907 print_type_quoted(type_right);
908 fprintf(stderr, " lacks qualifiers '");
909 print_type_qualifiers(missing_qualifiers);
910 fprintf(stderr, "' in pointed-to type\n");
914 points_to_left = get_unqualified_type(points_to_left);
915 points_to_right = get_unqualified_type(points_to_right);
917 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
918 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
919 && !types_compatible(points_to_left, points_to_right)) {
920 goto incompatible_assign_types;
923 *right = create_implicit_cast(*right, type_left);
927 if (is_type_compound(type_left)
928 && types_compatible(type_left, type_right)) {
929 *right = create_implicit_cast(*right, type_left);
933 incompatible_assign_types:
934 /* TODO: improve error message */
935 parser_print_error_prefix();
936 fprintf(stderr, "incompatible types in %s\n", context);
937 parser_print_error_prefix();
938 print_type_quoted(orig_type_left);
939 fputs(" <- ", stderr);
940 print_type_quoted(orig_type_right);
944 static expression_t *parse_constant_expression(void)
946 /* start parsing at precedence 7 (conditional expression) */
947 return parse_sub_expression(7);
950 static expression_t *parse_assignment_expression(void)
952 /* start parsing at precedence 2 (assignment expression) */
953 return parse_sub_expression(2);
956 static type_t *make_global_typedef(const char *name, type_t *type)
958 symbol_t *symbol = symbol_table_insert(name);
960 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
961 declaration->namespc = NAMESPACE_NORMAL;
962 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
963 declaration->type = type;
964 declaration->symbol = symbol;
965 declaration->source_position = builtin_source_position;
967 record_declaration(declaration);
969 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
970 typedef_type->typedeft.declaration = declaration;
975 static const char *parse_string_literals(void)
977 assert(token.type == T_STRING_LITERAL);
978 const char *result = token.v.string;
982 while(token.type == T_STRING_LITERAL) {
983 result = concat_strings(result, token.v.string);
990 static void parse_attributes(void)
994 case T___attribute__: {
1000 switch(token.type) {
1002 parse_error("EOF while parsing attribute");
1021 if(token.type != T_STRING_LITERAL) {
1022 parse_error_expected("while parsing assembler attribute",
1027 parse_string_literals();
1032 goto attributes_finished;
1036 attributes_finished:
1041 static designator_t *parse_designation(void)
1043 if(token.type != '[' && token.type != '.')
1046 designator_t *result = NULL;
1047 designator_t *last = NULL;
1050 designator_t *designator;
1051 switch(token.type) {
1053 designator = allocate_ast_zero(sizeof(designator[0]));
1055 designator->array_access = parse_constant_expression();
1059 designator = allocate_ast_zero(sizeof(designator[0]));
1061 if(token.type != T_IDENTIFIER) {
1062 parse_error_expected("while parsing designator",
1066 designator->symbol = token.v.symbol;
1074 assert(designator != NULL);
1076 last->next = designator;
1078 result = designator;
1085 static initializer_t *initializer_from_string(array_type_t *type,
1088 /* TODO: check len vs. size of array type */
1091 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1092 initializer->string.string = string;
1097 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1098 wide_string_t *const string)
1100 /* TODO: check len vs. size of array type */
1103 initializer_t *const initializer =
1104 allocate_initializer(INITIALIZER_WIDE_STRING);
1105 initializer->wide_string.string = *string;
1110 static initializer_t *initializer_from_expression(type_t *type,
1111 expression_t *expression)
1113 /* TODO check that expression is a constant expression */
1115 /* § 6.7.8.14/15 char array may be initialized by string literals */
1116 type_t *const expr_type = expression->base.datatype;
1117 if (is_type_array(type) && expr_type->type == TYPE_POINTER) {
1118 array_type_t *const array_type = &type->array;
1119 type_t *const element_type = skip_typeref(array_type->element_type);
1121 if (element_type->type == TYPE_ATOMIC) {
1122 switch (expression->type) {
1123 case EXPR_STRING_LITERAL:
1124 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1125 return initializer_from_string(array_type,
1126 expression->string.value);
1129 case EXPR_WIDE_STRING_LITERAL:
1130 if (get_unqualified_type(element_type) == skip_typeref(type_wchar_t)) {
1131 return initializer_from_wide_string(array_type,
1132 &expression->wide_string.value);
1140 type_t *expression_type = skip_typeref(expression->base.datatype);
1141 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1142 semantic_assign(type, &expression, "initializer");
1144 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1145 result->value.value = expression;
1153 static initializer_t *parse_sub_initializer(type_t *type,
1154 expression_t *expression,
1155 type_t *expression_type);
1157 static initializer_t *parse_sub_initializer_elem(type_t *type)
1159 if(token.type == '{') {
1160 return parse_sub_initializer(type, NULL, NULL);
1163 expression_t *expression = parse_assignment_expression();
1164 type_t *expression_type = skip_typeref(expression->base.datatype);
1166 return parse_sub_initializer(type, expression, expression_type);
1169 static bool had_initializer_brace_warning;
1171 static initializer_t *parse_sub_initializer(type_t *type,
1172 expression_t *expression,
1173 type_t *expression_type)
1175 if(is_type_scalar(type)) {
1176 /* there might be extra {} hierarchies */
1177 if(token.type == '{') {
1179 if(!had_initializer_brace_warning) {
1180 parse_warning("braces around scalar initializer");
1181 had_initializer_brace_warning = true;
1183 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1184 if(token.type == ',') {
1186 /* TODO: warn about excessive elements */
1192 if(expression == NULL) {
1193 expression = parse_assignment_expression();
1195 return initializer_from_expression(type, expression);
1198 /* does the expression match the currently looked at object to initalize */
1199 if(expression != NULL) {
1200 initializer_t *result = initializer_from_expression(type, expression);
1205 bool read_paren = false;
1206 if(token.type == '{') {
1211 /* descend into subtype */
1212 initializer_t *result = NULL;
1213 initializer_t **elems;
1214 if(is_type_array(type)) {
1215 array_type_t *array_type = &type->array;
1216 type_t *element_type = array_type->element_type;
1217 element_type = skip_typeref(element_type);
1220 had_initializer_brace_warning = false;
1221 if(expression == NULL) {
1222 sub = parse_sub_initializer_elem(element_type);
1224 sub = parse_sub_initializer(element_type, expression,
1228 /* didn't match the subtypes -> try the parent type */
1230 assert(!read_paren);
1234 elems = NEW_ARR_F(initializer_t*, 0);
1235 ARR_APP1(initializer_t*, elems, sub);
1238 if(token.type == '}')
1241 if(token.type == '}')
1244 sub = parse_sub_initializer_elem(element_type);
1246 /* TODO error, do nicer cleanup */
1247 parse_error("member initializer didn't match");
1251 ARR_APP1(initializer_t*, elems, sub);
1254 assert(is_type_compound(type));
1255 compound_type_t *compound_type = &type->compound;
1256 context_t *context = &compound_type->declaration->context;
1258 declaration_t *first = context->declarations;
1261 type_t *first_type = first->type;
1262 first_type = skip_typeref(first_type);
1265 had_initializer_brace_warning = false;
1266 if(expression == NULL) {
1267 sub = parse_sub_initializer_elem(first_type);
1269 sub = parse_sub_initializer(first_type, expression,expression_type);
1272 /* didn't match the subtypes -> try our parent type */
1274 assert(!read_paren);
1278 elems = NEW_ARR_F(initializer_t*, 0);
1279 ARR_APP1(initializer_t*, elems, sub);
1281 declaration_t *iter = first->next;
1282 for( ; iter != NULL; iter = iter->next) {
1283 if(iter->symbol == NULL)
1285 if(iter->namespc != NAMESPACE_NORMAL)
1288 if(token.type == '}')
1291 if(token.type == '}')
1294 type_t *iter_type = iter->type;
1295 iter_type = skip_typeref(iter_type);
1297 sub = parse_sub_initializer_elem(iter_type);
1299 /* TODO error, do nicer cleanup*/
1300 parse_error("member initializer didn't match");
1304 ARR_APP1(initializer_t*, elems, sub);
1308 int len = ARR_LEN(elems);
1309 size_t elems_size = sizeof(initializer_t*) * len;
1311 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1313 init->initializer.type = INITIALIZER_LIST;
1315 memcpy(init->initializers, elems, elems_size);
1318 result = (initializer_t*) init;
1321 if(token.type == ',')
1328 static initializer_t *parse_initializer(type_t *type)
1330 initializer_t *result;
1332 type = skip_typeref(type);
1334 if(token.type != '{') {
1335 expression_t *expression = parse_assignment_expression();
1336 initializer_t *initializer = initializer_from_expression(type, expression);
1337 if(initializer == NULL) {
1338 parser_print_error_prefix();
1339 fprintf(stderr, "initializer expression '");
1340 print_expression(expression);
1341 fprintf(stderr, "', type ");
1342 print_type_quoted(expression->base.datatype);
1343 fprintf(stderr, " is incompatible with type ");
1344 print_type_quoted(type);
1345 fprintf(stderr, "\n");
1350 if(is_type_scalar(type)) {
1354 expression_t *expression = parse_assignment_expression();
1355 result = initializer_from_expression(type, expression);
1357 if(token.type == ',')
1363 result = parse_sub_initializer(type, NULL, NULL);
1371 static declaration_t *parse_compound_type_specifier(bool is_struct)
1379 symbol_t *symbol = NULL;
1380 declaration_t *declaration = NULL;
1382 if (token.type == T___attribute__) {
1387 if(token.type == T_IDENTIFIER) {
1388 symbol = token.v.symbol;
1392 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1394 declaration = get_declaration(symbol, NAMESPACE_UNION);
1396 } else if(token.type != '{') {
1398 parse_error_expected("while parsing struct type specifier",
1399 T_IDENTIFIER, '{', 0);
1401 parse_error_expected("while parsing union type specifier",
1402 T_IDENTIFIER, '{', 0);
1408 if(declaration == NULL) {
1409 declaration = allocate_ast_zero(sizeof(declaration[0]));
1412 declaration->namespc = NAMESPACE_STRUCT;
1414 declaration->namespc = NAMESPACE_UNION;
1416 declaration->source_position = token.source_position;
1417 declaration->symbol = symbol;
1418 record_declaration(declaration);
1421 if(token.type == '{') {
1422 if(declaration->init.is_defined) {
1423 assert(symbol != NULL);
1424 parser_print_error_prefix();
1425 fprintf(stderr, "multiple definition of %s %s\n",
1426 is_struct ? "struct" : "union", symbol->string);
1427 declaration->context.declarations = NULL;
1429 declaration->init.is_defined = true;
1431 int top = environment_top();
1432 context_t *last_context = context;
1433 set_context(&declaration->context);
1435 parse_compound_type_entries();
1438 assert(context == &declaration->context);
1439 set_context(last_context);
1440 environment_pop_to(top);
1446 static void parse_enum_entries(enum_type_t *const enum_type)
1450 if(token.type == '}') {
1452 parse_error("empty enum not allowed");
1457 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1459 if(token.type != T_IDENTIFIER) {
1460 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1464 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1465 entry->type = (type_t*) enum_type;
1466 entry->symbol = token.v.symbol;
1467 entry->source_position = token.source_position;
1470 if(token.type == '=') {
1472 entry->init.enum_value = parse_constant_expression();
1477 record_declaration(entry);
1479 if(token.type != ',')
1482 } while(token.type != '}');
1487 static type_t *parse_enum_specifier(void)
1491 declaration_t *declaration;
1494 if(token.type == T_IDENTIFIER) {
1495 symbol = token.v.symbol;
1498 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1499 } else if(token.type != '{') {
1500 parse_error_expected("while parsing enum type specifier",
1501 T_IDENTIFIER, '{', 0);
1508 if(declaration == NULL) {
1509 declaration = allocate_ast_zero(sizeof(declaration[0]));
1511 declaration->namespc = NAMESPACE_ENUM;
1512 declaration->source_position = token.source_position;
1513 declaration->symbol = symbol;
1516 type_t *const type = allocate_type_zero(TYPE_ENUM);
1517 type->enumt.declaration = declaration;
1519 if(token.type == '{') {
1520 if(declaration->init.is_defined) {
1521 parser_print_error_prefix();
1522 fprintf(stderr, "multiple definitions of enum %s\n",
1525 record_declaration(declaration);
1526 declaration->init.is_defined = 1;
1528 parse_enum_entries(&type->enumt);
1536 * if a symbol is a typedef to another type, return true
1538 static bool is_typedef_symbol(symbol_t *symbol)
1540 const declaration_t *const declaration =
1541 get_declaration(symbol, NAMESPACE_NORMAL);
1543 declaration != NULL &&
1544 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1547 static type_t *parse_typeof(void)
1555 expression_t *expression = NULL;
1558 switch(token.type) {
1559 case T___extension__:
1560 /* this can be a prefix to a typename or an expression */
1561 /* we simply eat it now. */
1564 } while(token.type == T___extension__);
1568 if(is_typedef_symbol(token.v.symbol)) {
1569 type = parse_typename();
1571 expression = parse_expression();
1572 type = expression->base.datatype;
1577 type = parse_typename();
1581 expression = parse_expression();
1582 type = expression->base.datatype;
1588 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1589 typeof_type->typeoft.expression = expression;
1590 typeof_type->typeoft.typeof_type = type;
1596 SPECIFIER_SIGNED = 1 << 0,
1597 SPECIFIER_UNSIGNED = 1 << 1,
1598 SPECIFIER_LONG = 1 << 2,
1599 SPECIFIER_INT = 1 << 3,
1600 SPECIFIER_DOUBLE = 1 << 4,
1601 SPECIFIER_CHAR = 1 << 5,
1602 SPECIFIER_SHORT = 1 << 6,
1603 SPECIFIER_LONG_LONG = 1 << 7,
1604 SPECIFIER_FLOAT = 1 << 8,
1605 SPECIFIER_BOOL = 1 << 9,
1606 SPECIFIER_VOID = 1 << 10,
1607 #ifdef PROVIDE_COMPLEX
1608 SPECIFIER_COMPLEX = 1 << 11,
1609 SPECIFIER_IMAGINARY = 1 << 12,
1613 static type_t *create_builtin_type(symbol_t *symbol)
1615 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1616 type->builtin.symbol = symbol;
1618 type->builtin.real_type = type_int;
1623 static type_t *get_typedef_type(symbol_t *symbol)
1625 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1626 if(declaration == NULL
1627 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1630 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1631 type->typedeft.declaration = declaration;
1636 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1638 type_t *type = NULL;
1639 unsigned type_qualifiers = 0;
1640 unsigned type_specifiers = 0;
1643 specifiers->source_position = token.source_position;
1646 switch(token.type) {
1649 #define MATCH_STORAGE_CLASS(token, class) \
1651 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1652 parse_error("multiple storage classes in declaration " \
1655 specifiers->storage_class = class; \
1659 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1660 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1661 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1662 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1663 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1666 switch (specifiers->storage_class) {
1667 case STORAGE_CLASS_NONE:
1668 specifiers->storage_class = STORAGE_CLASS_THREAD;
1671 case STORAGE_CLASS_EXTERN:
1672 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1675 case STORAGE_CLASS_STATIC:
1676 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1680 parse_error("multiple storage classes in declaration specifiers");
1686 /* type qualifiers */
1687 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1689 type_qualifiers |= qualifier; \
1693 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1694 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1695 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1697 case T___extension__:
1702 /* type specifiers */
1703 #define MATCH_SPECIFIER(token, specifier, name) \
1706 if(type_specifiers & specifier) { \
1707 parse_error("multiple " name " type specifiers given"); \
1709 type_specifiers |= specifier; \
1713 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1714 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1715 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1716 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1717 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1718 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1719 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1720 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1721 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1722 #ifdef PROVIDE_COMPLEX
1723 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1724 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1728 specifiers->is_inline = true;
1733 if(type_specifiers & SPECIFIER_LONG_LONG) {
1734 parse_error("multiple type specifiers given");
1735 } else if(type_specifiers & SPECIFIER_LONG) {
1736 type_specifiers |= SPECIFIER_LONG_LONG;
1738 type_specifiers |= SPECIFIER_LONG;
1742 /* TODO: if type != NULL for the following rules should issue
1745 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1747 type->compound.declaration = parse_compound_type_specifier(true);
1751 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1753 type->compound.declaration = parse_compound_type_specifier(false);
1757 type = parse_enum_specifier();
1760 type = parse_typeof();
1762 case T___builtin_va_list:
1763 type = create_builtin_type(token.v.symbol);
1767 case T___attribute__:
1772 case T_IDENTIFIER: {
1773 type_t *typedef_type = get_typedef_type(token.v.symbol);
1775 if(typedef_type == NULL)
1776 goto finish_specifiers;
1779 type = typedef_type;
1783 /* function specifier */
1785 goto finish_specifiers;
1792 atomic_type_type_t atomic_type;
1794 /* match valid basic types */
1795 switch(type_specifiers) {
1796 case SPECIFIER_VOID:
1797 atomic_type = ATOMIC_TYPE_VOID;
1799 case SPECIFIER_CHAR:
1800 atomic_type = ATOMIC_TYPE_CHAR;
1802 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1803 atomic_type = ATOMIC_TYPE_SCHAR;
1805 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1806 atomic_type = ATOMIC_TYPE_UCHAR;
1808 case SPECIFIER_SHORT:
1809 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1810 case SPECIFIER_SHORT | SPECIFIER_INT:
1811 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1812 atomic_type = ATOMIC_TYPE_SHORT;
1814 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1815 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1816 atomic_type = ATOMIC_TYPE_USHORT;
1819 case SPECIFIER_SIGNED:
1820 case SPECIFIER_SIGNED | SPECIFIER_INT:
1821 atomic_type = ATOMIC_TYPE_INT;
1823 case SPECIFIER_UNSIGNED:
1824 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1825 atomic_type = ATOMIC_TYPE_UINT;
1827 case SPECIFIER_LONG:
1828 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1829 case SPECIFIER_LONG | SPECIFIER_INT:
1830 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1831 atomic_type = ATOMIC_TYPE_LONG;
1833 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1834 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1835 atomic_type = ATOMIC_TYPE_ULONG;
1837 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1838 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1839 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1840 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1842 atomic_type = ATOMIC_TYPE_LONGLONG;
1844 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1845 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1847 atomic_type = ATOMIC_TYPE_ULONGLONG;
1849 case SPECIFIER_FLOAT:
1850 atomic_type = ATOMIC_TYPE_FLOAT;
1852 case SPECIFIER_DOUBLE:
1853 atomic_type = ATOMIC_TYPE_DOUBLE;
1855 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1856 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1858 case SPECIFIER_BOOL:
1859 atomic_type = ATOMIC_TYPE_BOOL;
1861 #ifdef PROVIDE_COMPLEX
1862 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1863 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1865 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1866 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1868 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1869 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1871 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1872 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1874 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1875 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1877 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1878 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1882 /* invalid specifier combination, give an error message */
1883 if(type_specifiers == 0) {
1885 parse_warning("no type specifiers in declaration, using int");
1886 atomic_type = ATOMIC_TYPE_INT;
1889 parse_error("no type specifiers given in declaration");
1891 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1892 (type_specifiers & SPECIFIER_UNSIGNED)) {
1893 parse_error("signed and unsigned specifiers gives");
1894 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1895 parse_error("only integer types can be signed or unsigned");
1897 parse_error("multiple datatypes in declaration");
1899 atomic_type = ATOMIC_TYPE_INVALID;
1902 type = allocate_type_zero(TYPE_ATOMIC);
1903 type->atomic.atype = atomic_type;
1906 if(type_specifiers != 0) {
1907 parse_error("multiple datatypes in declaration");
1911 type->base.qualifiers = type_qualifiers;
1913 type_t *result = typehash_insert(type);
1914 if(newtype && result != type) {
1918 specifiers->type = result;
1921 static type_qualifiers_t parse_type_qualifiers(void)
1923 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1926 switch(token.type) {
1927 /* type qualifiers */
1928 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1929 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1930 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1933 return type_qualifiers;
1938 static declaration_t *parse_identifier_list(void)
1940 declaration_t *declarations = NULL;
1941 declaration_t *last_declaration = NULL;
1943 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1945 declaration->source_position = token.source_position;
1946 declaration->symbol = token.v.symbol;
1949 if(last_declaration != NULL) {
1950 last_declaration->next = declaration;
1952 declarations = declaration;
1954 last_declaration = declaration;
1956 if(token.type != ',')
1959 } while(token.type == T_IDENTIFIER);
1961 return declarations;
1964 static void semantic_parameter(declaration_t *declaration)
1966 /* TODO: improve error messages */
1968 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1969 parse_error("typedef not allowed in parameter list");
1970 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1971 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1972 parse_error("parameter may only have none or register storage class");
1975 type_t *orig_type = declaration->type;
1976 if(orig_type == NULL)
1978 type_t *type = skip_typeref(orig_type);
1980 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1981 * into a pointer. § 6.7.5.3 (7) */
1982 if (is_type_array(type)) {
1983 const array_type_t *arr_type = &type->array;
1984 type_t *element_type = arr_type->element_type;
1986 type = make_pointer_type(element_type, type->base.qualifiers);
1988 declaration->type = type;
1991 if(is_type_incomplete(type)) {
1992 parser_print_error_prefix();
1993 fprintf(stderr, "incomplete type (");
1994 print_type_quoted(orig_type);
1995 fprintf(stderr, ") not allowed for parameter '%s'\n",
1996 declaration->symbol->string);
2000 static declaration_t *parse_parameter(void)
2002 declaration_specifiers_t specifiers;
2003 memset(&specifiers, 0, sizeof(specifiers));
2005 parse_declaration_specifiers(&specifiers);
2007 declaration_t *declaration = parse_declarator(&specifiers, true);
2009 semantic_parameter(declaration);
2014 static declaration_t *parse_parameters(function_type_t *type)
2016 if(token.type == T_IDENTIFIER) {
2017 symbol_t *symbol = token.v.symbol;
2018 if(!is_typedef_symbol(symbol)) {
2019 type->kr_style_parameters = true;
2020 return parse_identifier_list();
2024 if(token.type == ')') {
2025 type->unspecified_parameters = 1;
2028 if(token.type == T_void && look_ahead(1)->type == ')') {
2033 declaration_t *declarations = NULL;
2034 declaration_t *declaration;
2035 declaration_t *last_declaration = NULL;
2036 function_parameter_t *parameter;
2037 function_parameter_t *last_parameter = NULL;
2040 switch(token.type) {
2044 return declarations;
2047 case T___extension__:
2049 declaration = parse_parameter();
2051 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2052 memset(parameter, 0, sizeof(parameter[0]));
2053 parameter->type = declaration->type;
2055 if(last_parameter != NULL) {
2056 last_declaration->next = declaration;
2057 last_parameter->next = parameter;
2059 type->parameters = parameter;
2060 declarations = declaration;
2062 last_parameter = parameter;
2063 last_declaration = declaration;
2067 return declarations;
2069 if(token.type != ',')
2070 return declarations;
2080 } construct_type_type_t;
2082 typedef struct construct_type_t construct_type_t;
2083 struct construct_type_t {
2084 construct_type_type_t type;
2085 construct_type_t *next;
2088 typedef struct parsed_pointer_t parsed_pointer_t;
2089 struct parsed_pointer_t {
2090 construct_type_t construct_type;
2091 type_qualifiers_t type_qualifiers;
2094 typedef struct construct_function_type_t construct_function_type_t;
2095 struct construct_function_type_t {
2096 construct_type_t construct_type;
2097 type_t *function_type;
2100 typedef struct parsed_array_t parsed_array_t;
2101 struct parsed_array_t {
2102 construct_type_t construct_type;
2103 type_qualifiers_t type_qualifiers;
2109 typedef struct construct_base_type_t construct_base_type_t;
2110 struct construct_base_type_t {
2111 construct_type_t construct_type;
2115 static construct_type_t *parse_pointer_declarator(void)
2119 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2120 memset(pointer, 0, sizeof(pointer[0]));
2121 pointer->construct_type.type = CONSTRUCT_POINTER;
2122 pointer->type_qualifiers = parse_type_qualifiers();
2124 return (construct_type_t*) pointer;
2127 static construct_type_t *parse_array_declarator(void)
2131 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2132 memset(array, 0, sizeof(array[0]));
2133 array->construct_type.type = CONSTRUCT_ARRAY;
2135 if(token.type == T_static) {
2136 array->is_static = true;
2140 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2141 if(type_qualifiers != 0) {
2142 if(token.type == T_static) {
2143 array->is_static = true;
2147 array->type_qualifiers = type_qualifiers;
2149 if(token.type == '*' && look_ahead(1)->type == ']') {
2150 array->is_variable = true;
2152 } else if(token.type != ']') {
2153 array->size = parse_assignment_expression();
2158 return (construct_type_t*) array;
2161 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2165 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2167 declaration_t *parameters = parse_parameters(&type->function);
2168 if(declaration != NULL) {
2169 declaration->context.declarations = parameters;
2172 construct_function_type_t *construct_function_type =
2173 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2174 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2175 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2176 construct_function_type->function_type = type;
2180 return (construct_type_t*) construct_function_type;
2183 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2184 bool may_be_abstract)
2186 /* construct a single linked list of construct_type_t's which describe
2187 * how to construct the final declarator type */
2188 construct_type_t *first = NULL;
2189 construct_type_t *last = NULL;
2192 while(token.type == '*') {
2193 construct_type_t *type = parse_pointer_declarator();
2204 /* TODO: find out if this is correct */
2207 construct_type_t *inner_types = NULL;
2209 switch(token.type) {
2211 if(declaration == NULL) {
2212 parse_error("no identifier expected in typename");
2214 declaration->symbol = token.v.symbol;
2215 declaration->source_position = token.source_position;
2221 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2227 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2228 /* avoid a loop in the outermost scope, because eat_statement doesn't
2230 if(token.type == '}' && current_function == NULL) {
2238 construct_type_t *p = last;
2241 construct_type_t *type;
2242 switch(token.type) {
2244 type = parse_function_declarator(declaration);
2247 type = parse_array_declarator();
2250 goto declarator_finished;
2253 /* insert in the middle of the list (behind p) */
2255 type->next = p->next;
2266 declarator_finished:
2269 /* append inner_types at the end of the list, we don't to set last anymore
2270 * as it's not needed anymore */
2272 assert(first == NULL);
2273 first = inner_types;
2275 last->next = inner_types;
2281 static type_t *construct_declarator_type(construct_type_t *construct_list,
2284 construct_type_t *iter = construct_list;
2285 for( ; iter != NULL; iter = iter->next) {
2286 switch(iter->type) {
2287 case CONSTRUCT_INVALID:
2288 panic("invalid type construction found");
2289 case CONSTRUCT_FUNCTION: {
2290 construct_function_type_t *construct_function_type
2291 = (construct_function_type_t*) iter;
2293 type_t *function_type = construct_function_type->function_type;
2295 function_type->function.return_type = type;
2297 type = function_type;
2301 case CONSTRUCT_POINTER: {
2302 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2303 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2304 pointer_type->pointer.points_to = type;
2305 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2307 type = pointer_type;
2311 case CONSTRUCT_ARRAY: {
2312 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2313 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2315 array_type->base.qualifiers = parsed_array->type_qualifiers;
2316 array_type->array.element_type = type;
2317 array_type->array.is_static = parsed_array->is_static;
2318 array_type->array.is_variable = parsed_array->is_variable;
2319 array_type->array.size = parsed_array->size;
2326 type_t *hashed_type = typehash_insert(type);
2327 if(hashed_type != type) {
2328 /* the function type was constructed earlier freeing it here will
2329 * destroy other types... */
2330 if(iter->type != CONSTRUCT_FUNCTION) {
2340 static declaration_t *parse_declarator(
2341 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2343 type_t *type = specifiers->type;
2344 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2345 declaration->storage_class = specifiers->storage_class;
2346 declaration->is_inline = specifiers->is_inline;
2348 construct_type_t *construct_type
2349 = parse_inner_declarator(declaration, may_be_abstract);
2350 declaration->type = construct_declarator_type(construct_type, type);
2352 if(construct_type != NULL) {
2353 obstack_free(&temp_obst, construct_type);
2359 static type_t *parse_abstract_declarator(type_t *base_type)
2361 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2363 type_t *result = construct_declarator_type(construct_type, base_type);
2364 if(construct_type != NULL) {
2365 obstack_free(&temp_obst, construct_type);
2371 static declaration_t *record_declaration(declaration_t *declaration)
2373 assert(declaration->parent_context == NULL);
2374 assert(context != NULL);
2376 symbol_t *symbol = declaration->symbol;
2377 if(symbol != NULL) {
2378 declaration_t *alias = environment_push(declaration);
2379 if(alias != declaration)
2382 declaration->parent_context = context;
2385 if(last_declaration != NULL) {
2386 last_declaration->next = declaration;
2388 context->declarations = declaration;
2390 last_declaration = declaration;
2395 static void parser_error_multiple_definition(declaration_t *declaration,
2396 const source_position_t source_position)
2398 parser_print_error_prefix_pos(source_position);
2399 fprintf(stderr, "multiple definition of symbol '%s'\n",
2400 declaration->symbol->string);
2401 parser_print_error_prefix_pos(declaration->source_position);
2402 fprintf(stderr, "this is the location of the previous definition.\n");
2405 static bool is_declaration_specifier(const token_t *token,
2406 bool only_type_specifiers)
2408 switch(token->type) {
2412 return is_typedef_symbol(token->v.symbol);
2414 case T___extension__:
2417 return !only_type_specifiers;
2424 static void parse_init_declarator_rest(declaration_t *declaration)
2428 type_t *orig_type = declaration->type;
2429 type_t *type = NULL;
2430 if(orig_type != NULL)
2431 type = skip_typeref(orig_type);
2433 if(declaration->init.initializer != NULL) {
2434 parser_error_multiple_definition(declaration, token.source_position);
2437 initializer_t *initializer = parse_initializer(type);
2439 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2440 * the array type size */
2441 if(type != NULL && is_type_array(type) && initializer != NULL) {
2442 array_type_t *array_type = &type->array;
2444 if(array_type->size == NULL) {
2445 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2447 cnst->base.datatype = type_size_t;
2449 switch (initializer->type) {
2450 case INITIALIZER_LIST: {
2451 initializer_list_t *const list = &initializer->list;
2452 cnst->conste.v.int_value = list->len;
2456 case INITIALIZER_STRING: {
2457 initializer_string_t *const string = &initializer->string;
2458 cnst->conste.v.int_value = strlen(string->string) + 1;
2462 case INITIALIZER_WIDE_STRING: {
2463 initializer_wide_string_t *const string = &initializer->wide_string;
2464 cnst->conste.v.int_value = string->string.size;
2469 panic("invalid initializer type");
2472 array_type->size = cnst;
2476 if(type != NULL && is_type_function(type)) {
2477 parser_print_error_prefix_pos(declaration->source_position);
2478 fprintf(stderr, "initializers not allowed for function types at "
2479 "declator '%s' (type ", declaration->symbol->string);
2480 print_type_quoted(orig_type);
2481 fprintf(stderr, ")\n");
2483 declaration->init.initializer = initializer;
2487 /* parse rest of a declaration without any declarator */
2488 static void parse_anonymous_declaration_rest(
2489 const declaration_specifiers_t *specifiers,
2490 parsed_declaration_func finished_declaration)
2494 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2496 declaration->type = specifiers->type;
2497 declaration->storage_class = specifiers->storage_class;
2498 declaration->source_position = specifiers->source_position;
2500 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2501 parse_warning_pos(declaration->source_position,
2502 "useless storage class in empty declaration");
2505 type_t *type = declaration->type;
2506 switch (type->type) {
2507 case TYPE_COMPOUND_STRUCT:
2508 case TYPE_COMPOUND_UNION: {
2509 const compound_type_t *compound_type = &type->compound;
2510 if (compound_type->declaration->symbol == NULL) {
2511 parse_warning_pos(declaration->source_position,
2512 "unnamed struct/union that defines no instances");
2521 parse_warning_pos(declaration->source_position,
2522 "empty declaration");
2526 finished_declaration(declaration);
2529 static void parse_declaration_rest(declaration_t *ndeclaration,
2530 const declaration_specifiers_t *specifiers,
2531 parsed_declaration_func finished_declaration)
2534 declaration_t *declaration = finished_declaration(ndeclaration);
2536 type_t *orig_type = declaration->type;
2537 type_t *type = skip_typeref(orig_type);
2539 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2540 parser_print_warning_prefix_pos(declaration->source_position);
2541 fprintf(stderr, "variable '%s' declared 'inline'\n",
2542 declaration->symbol->string);
2545 if(token.type == '=') {
2546 parse_init_declarator_rest(declaration);
2549 if(token.type != ',')
2553 ndeclaration = parse_declarator(specifiers, false);
2558 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2560 /* TODO: check that it was actually a parameter that gets a type */
2562 /* we should have a declaration for the parameter in the current
2564 return record_declaration(declaration);
2567 static void parse_declaration(parsed_declaration_func finished_declaration)
2569 declaration_specifiers_t specifiers;
2570 memset(&specifiers, 0, sizeof(specifiers));
2571 parse_declaration_specifiers(&specifiers);
2573 if(token.type == ';') {
2574 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2576 declaration_t *declaration = parse_declarator(&specifiers, false);
2577 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2581 static void parse_kr_declaration_list(declaration_t *declaration)
2583 type_t *type = skip_typeref(declaration->type);
2584 assert(is_type_function(type));
2586 if(!type->function.kr_style_parameters)
2589 /* push function parameters */
2590 int top = environment_top();
2591 context_t *last_context = context;
2592 set_context(&declaration->context);
2594 declaration_t *parameter = declaration->context.declarations;
2595 for( ; parameter != NULL; parameter = parameter->next) {
2596 environment_push(parameter);
2599 /* parse declaration list */
2600 while(is_declaration_specifier(&token, false)) {
2601 parse_declaration(finished_kr_declaration);
2604 /* pop function parameters */
2605 assert(context == &declaration->context);
2606 set_context(last_context);
2607 environment_pop_to(top);
2609 /* update function type */
2610 type_t *new_type = duplicate_type(type);
2611 new_type->function.kr_style_parameters = false;
2613 function_parameter_t *parameters = NULL;
2614 function_parameter_t *last_parameter = NULL;
2616 declaration_t *parameter_declaration = declaration->context.declarations;
2617 for( ; parameter_declaration != NULL;
2618 parameter_declaration = parameter_declaration->next) {
2619 type_t *parameter_type = parameter_declaration->type;
2620 if(parameter_type == NULL) {
2622 parser_print_error_prefix();
2623 fprintf(stderr, "no type specified for function parameter '%s'\n",
2624 parameter_declaration->symbol->string);
2626 parser_print_warning_prefix();
2627 fprintf(stderr, "no type specified for function parameter '%s', "
2628 "using int\n", parameter_declaration->symbol->string);
2629 parameter_type = type_int;
2630 parameter_declaration->type = parameter_type;
2634 semantic_parameter(parameter_declaration);
2635 parameter_type = parameter_declaration->type;
2637 function_parameter_t *function_parameter
2638 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2639 memset(function_parameter, 0, sizeof(function_parameter[0]));
2641 function_parameter->type = parameter_type;
2642 if(last_parameter != NULL) {
2643 last_parameter->next = function_parameter;
2645 parameters = function_parameter;
2647 last_parameter = function_parameter;
2649 new_type->function.parameters = parameters;
2651 type = typehash_insert(new_type);
2652 if(type != new_type) {
2653 obstack_free(type_obst, new_type);
2656 declaration->type = type;
2659 static void parse_external_declaration(void)
2661 /* function-definitions and declarations both start with declaration
2663 declaration_specifiers_t specifiers;
2664 memset(&specifiers, 0, sizeof(specifiers));
2665 parse_declaration_specifiers(&specifiers);
2667 /* must be a declaration */
2668 if(token.type == ';') {
2669 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2673 /* declarator is common to both function-definitions and declarations */
2674 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2676 /* must be a declaration */
2677 if(token.type == ',' || token.type == '=' || token.type == ';') {
2678 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2682 /* must be a function definition */
2683 parse_kr_declaration_list(ndeclaration);
2685 if(token.type != '{') {
2686 parse_error_expected("while parsing function definition", '{', 0);
2691 type_t *type = ndeclaration->type;
2697 /* note that we don't skip typerefs: the standard doesn't allow them here
2698 * (so we can't use is_type_function here) */
2699 if(type->type != TYPE_FUNCTION) {
2700 parser_print_error_prefix();
2701 fprintf(stderr, "declarator '");
2702 print_type_ext(type, ndeclaration->symbol, NULL);
2703 fprintf(stderr, "' has a body but is not a function type.\n");
2708 /* § 6.7.5.3 (14) a function definition with () means no
2709 * parameters (and not unspecified parameters) */
2710 if(type->function.unspecified_parameters) {
2711 type_t *duplicate = duplicate_type(type);
2712 duplicate->function.unspecified_parameters = false;
2714 type = typehash_insert(duplicate);
2715 if(type != duplicate) {
2716 obstack_free(type_obst, duplicate);
2718 ndeclaration->type = type;
2721 declaration_t *declaration = record_declaration(ndeclaration);
2722 if(ndeclaration != declaration) {
2723 memcpy(&declaration->context, &ndeclaration->context,
2724 sizeof(declaration->context));
2726 type = skip_typeref(declaration->type);
2728 /* push function parameters and switch context */
2729 int top = environment_top();
2730 context_t *last_context = context;
2731 set_context(&declaration->context);
2733 declaration_t *parameter = declaration->context.declarations;
2734 for( ; parameter != NULL; parameter = parameter->next) {
2735 environment_push(parameter);
2738 if(declaration->init.statement != NULL) {
2739 parser_error_multiple_definition(declaration, token.source_position);
2741 goto end_of_parse_external_declaration;
2743 /* parse function body */
2744 int label_stack_top = label_top();
2745 declaration_t *old_current_function = current_function;
2746 current_function = declaration;
2748 declaration->init.statement = parse_compound_statement();
2750 assert(current_function == declaration);
2751 current_function = old_current_function;
2752 label_pop_to(label_stack_top);
2755 end_of_parse_external_declaration:
2756 assert(context == &declaration->context);
2757 set_context(last_context);
2758 environment_pop_to(top);
2761 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2764 if(token.type == ':') {
2766 parse_constant_expression();
2767 /* TODO (bitfields) */
2769 declaration_t *declaration = parse_declarator(specifiers, true);
2771 /* TODO: check constraints for struct declarations */
2772 /* TODO: check for doubled fields */
2773 record_declaration(declaration);
2775 if(token.type == ':') {
2777 parse_constant_expression();
2778 /* TODO (bitfields) */
2782 if(token.type != ',')
2789 static void parse_compound_type_entries(void)
2793 while(token.type != '}' && token.type != T_EOF) {
2794 declaration_specifiers_t specifiers;
2795 memset(&specifiers, 0, sizeof(specifiers));
2796 parse_declaration_specifiers(&specifiers);
2798 parse_struct_declarators(&specifiers);
2800 if(token.type == T_EOF) {
2801 parse_error("EOF while parsing struct");
2806 static type_t *parse_typename(void)
2808 declaration_specifiers_t specifiers;
2809 memset(&specifiers, 0, sizeof(specifiers));
2810 parse_declaration_specifiers(&specifiers);
2811 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2812 /* TODO: improve error message, user does probably not know what a
2813 * storage class is...
2815 parse_error("typename may not have a storage class");
2818 type_t *result = parse_abstract_declarator(specifiers.type);
2826 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2827 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2828 expression_t *left);
2830 typedef struct expression_parser_function_t expression_parser_function_t;
2831 struct expression_parser_function_t {
2832 unsigned precedence;
2833 parse_expression_function parser;
2834 unsigned infix_precedence;
2835 parse_expression_infix_function infix_parser;
2838 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2840 static expression_t *make_invalid_expression(void)
2842 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2843 expression->base.source_position = token.source_position;
2847 static expression_t *expected_expression_error(void)
2849 parser_print_error_prefix();
2850 fprintf(stderr, "expected expression, got token ");
2851 print_token(stderr, &token);
2852 fprintf(stderr, "\n");
2856 return make_invalid_expression();
2859 static expression_t *parse_string_const(void)
2861 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2862 cnst->base.datatype = type_string;
2863 cnst->string.value = parse_string_literals();
2868 static expression_t *parse_wide_string_const(void)
2870 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2871 cnst->base.datatype = type_wchar_t_ptr;
2872 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2877 static expression_t *parse_int_const(void)
2879 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2880 cnst->base.datatype = token.datatype;
2881 cnst->conste.v.int_value = token.v.intvalue;
2888 static expression_t *parse_float_const(void)
2890 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2891 cnst->base.datatype = token.datatype;
2892 cnst->conste.v.float_value = token.v.floatvalue;
2899 static declaration_t *create_implicit_function(symbol_t *symbol,
2900 const source_position_t source_position)
2902 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2903 ntype->function.return_type = type_int;
2904 ntype->function.unspecified_parameters = true;
2906 type_t *type = typehash_insert(ntype);
2911 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2913 declaration->storage_class = STORAGE_CLASS_EXTERN;
2914 declaration->type = type;
2915 declaration->symbol = symbol;
2916 declaration->source_position = source_position;
2918 /* prepend the implicit definition to the global context
2919 * this is safe since the symbol wasn't declared as anything else yet
2921 assert(symbol->declaration == NULL);
2923 context_t *last_context = context;
2924 context = global_context;
2926 environment_push(declaration);
2927 declaration->next = context->declarations;
2928 context->declarations = declaration;
2930 context = last_context;
2935 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2937 function_parameter_t *parameter
2938 = obstack_alloc(type_obst, sizeof(parameter[0]));
2939 memset(parameter, 0, sizeof(parameter[0]));
2940 parameter->type = argument_type;
2942 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2943 type->function.return_type = return_type;
2944 type->function.parameters = parameter;
2946 type_t *result = typehash_insert(type);
2947 if(result != type) {
2954 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2956 switch(symbol->ID) {
2957 case T___builtin_alloca:
2958 return make_function_1_type(type_void_ptr, type_size_t);
2959 case T___builtin_nan:
2960 return make_function_1_type(type_double, type_string);
2961 case T___builtin_nanf:
2962 return make_function_1_type(type_float, type_string);
2963 case T___builtin_nand:
2964 return make_function_1_type(type_long_double, type_string);
2966 panic("not implemented builtin symbol found");
2971 * performs automatic type cast as described in § 6.3.2.1
2973 static type_t *automatic_type_conversion(type_t *orig_type)
2975 if(orig_type == NULL)
2978 type_t *type = skip_typeref(orig_type);
2979 if(is_type_array(type)) {
2980 array_type_t *array_type = &type->array;
2981 type_t *element_type = array_type->element_type;
2982 unsigned qualifiers = array_type->type.qualifiers;
2984 return make_pointer_type(element_type, qualifiers);
2987 if(is_type_function(type)) {
2988 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
2995 * reverts the automatic casts of array to pointer types and function
2996 * to function-pointer types as defined § 6.3.2.1
2998 type_t *revert_automatic_type_conversion(const expression_t *expression)
3000 if(expression->base.datatype == NULL)
3003 switch(expression->type) {
3004 case EXPR_REFERENCE: {
3005 const reference_expression_t *ref = &expression->reference;
3006 return ref->declaration->type;
3009 const select_expression_t *select = &expression->select;
3010 return select->compound_entry->type;
3013 const unary_expression_t *unary = &expression->unary;
3014 if(unary->type == UNEXPR_DEREFERENCE) {
3015 expression_t *value = unary->value;
3016 type_t *type = skip_typeref(value->base.datatype);
3017 pointer_type_t *pointer_type = &type->pointer;
3019 return pointer_type->points_to;
3023 case EXPR_BUILTIN_SYMBOL: {
3024 const builtin_symbol_expression_t *builtin
3025 = &expression->builtin_symbol;
3026 return get_builtin_symbol_type(builtin->symbol);
3028 case EXPR_ARRAY_ACCESS: {
3029 const array_access_expression_t *array_access
3030 = &expression->array_access;
3031 const expression_t *array_ref = array_access->array_ref;
3032 type_t *type_left = skip_typeref(array_ref->base.datatype);
3033 assert(is_type_pointer(type_left));
3034 pointer_type_t *pointer_type = &type_left->pointer;
3035 return pointer_type->points_to;
3042 return expression->base.datatype;
3045 static expression_t *parse_reference(void)
3047 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3049 reference_expression_t *ref = &expression->reference;
3050 ref->symbol = token.v.symbol;
3052 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3054 source_position_t source_position = token.source_position;
3057 if(declaration == NULL) {
3059 /* an implicitly defined function */
3060 if(token.type == '(') {
3061 parser_print_prefix_pos(token.source_position);
3062 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3063 ref->symbol->string);
3065 declaration = create_implicit_function(ref->symbol,
3070 parser_print_error_prefix();
3071 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3076 type_t *type = declaration->type;
3077 /* we always do the auto-type conversions; the & and sizeof parser contains
3078 * code to revert this! */
3079 type = automatic_type_conversion(type);
3081 ref->declaration = declaration;
3082 ref->expression.datatype = type;
3087 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3091 /* TODO check if explicit cast is allowed and issue warnings/errors */
3094 static expression_t *parse_cast(void)
3096 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
3098 cast->unary.type = UNEXPR_CAST;
3099 cast->base.source_position = token.source_position;
3101 type_t *type = parse_typename();
3104 expression_t *value = parse_sub_expression(20);
3106 check_cast_allowed(value, type);
3108 cast->base.datatype = type;
3109 cast->unary.value = value;
3114 static expression_t *parse_statement_expression(void)
3116 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3118 statement_t *statement = parse_compound_statement();
3119 expression->statement.statement = statement;
3120 if(statement == NULL) {
3125 assert(statement->type == STATEMENT_COMPOUND);
3126 compound_statement_t *compound_statement = &statement->compound;
3128 /* find last statement and use it's type */
3129 const statement_t *last_statement = NULL;
3130 const statement_t *iter = compound_statement->statements;
3131 for( ; iter != NULL; iter = iter->base.next) {
3132 last_statement = iter;
3135 if(last_statement->type == STATEMENT_EXPRESSION) {
3136 const expression_statement_t *expression_statement
3137 = &last_statement->expression;
3138 expression->base.datatype
3139 = expression_statement->expression->base.datatype;
3141 expression->base.datatype = type_void;
3149 static expression_t *parse_brace_expression(void)
3153 switch(token.type) {
3155 /* gcc extension: a stement expression */
3156 return parse_statement_expression();
3160 return parse_cast();
3162 if(is_typedef_symbol(token.v.symbol)) {
3163 return parse_cast();
3167 expression_t *result = parse_expression();
3173 static expression_t *parse_function_keyword(void)
3178 if (current_function == NULL) {
3179 parse_error("'__func__' used outside of a function");
3182 string_literal_expression_t *expression
3183 = allocate_ast_zero(sizeof(expression[0]));
3185 expression->expression.type = EXPR_FUNCTION;
3186 expression->expression.datatype = type_string;
3187 expression->value = "TODO: FUNCTION";
3189 return (expression_t*) expression;
3192 static expression_t *parse_pretty_function_keyword(void)
3194 eat(T___PRETTY_FUNCTION__);
3197 string_literal_expression_t *expression
3198 = allocate_ast_zero(sizeof(expression[0]));
3200 expression->expression.type = EXPR_PRETTY_FUNCTION;
3201 expression->expression.datatype = type_string;
3202 expression->value = "TODO: PRETTY FUNCTION";
3204 return (expression_t*) expression;
3207 static designator_t *parse_designator(void)
3209 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3211 if(token.type != T_IDENTIFIER) {
3212 parse_error_expected("while parsing member designator",
3217 result->symbol = token.v.symbol;
3220 designator_t *last_designator = result;
3222 if(token.type == '.') {
3224 if(token.type != T_IDENTIFIER) {
3225 parse_error_expected("while parsing member designator",
3230 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3231 designator->symbol = token.v.symbol;
3234 last_designator->next = designator;
3235 last_designator = designator;
3238 if(token.type == '[') {
3240 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3241 designator->array_access = parse_expression();
3242 if(designator->array_access == NULL) {
3248 last_designator->next = designator;
3249 last_designator = designator;
3258 static expression_t *parse_offsetof(void)
3260 eat(T___builtin_offsetof);
3262 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3263 expression->base.datatype = type_size_t;
3266 expression->offsetofe.type = parse_typename();
3268 expression->offsetofe.designator = parse_designator();
3274 static expression_t *parse_va_arg(void)
3276 eat(T___builtin_va_arg);
3278 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3281 expression->va_arge.arg = parse_assignment_expression();
3283 expression->base.datatype = parse_typename();
3289 static expression_t *parse_builtin_symbol(void)
3291 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3293 symbol_t *symbol = token.v.symbol;
3295 expression->builtin_symbol.symbol = symbol;
3298 type_t *type = get_builtin_symbol_type(symbol);
3299 type = automatic_type_conversion(type);
3301 expression->base.datatype = type;
3305 static expression_t *parse_primary_expression(void)
3307 switch(token.type) {
3309 return parse_int_const();
3310 case T_FLOATINGPOINT:
3311 return parse_float_const();
3312 case T_STRING_LITERAL: /* TODO merge */
3313 return parse_string_const();
3314 case T_WIDE_STRING_LITERAL:
3315 return parse_wide_string_const();
3317 return parse_reference();
3318 case T___FUNCTION__:
3320 return parse_function_keyword();
3321 case T___PRETTY_FUNCTION__:
3322 return parse_pretty_function_keyword();
3323 case T___builtin_offsetof:
3324 return parse_offsetof();
3325 case T___builtin_va_arg:
3326 return parse_va_arg();
3327 case T___builtin_nanf:
3328 case T___builtin_alloca:
3329 case T___builtin_expect:
3330 case T___builtin_va_start:
3331 case T___builtin_va_end:
3332 return parse_builtin_symbol();
3335 return parse_brace_expression();
3338 parser_print_error_prefix();
3339 fprintf(stderr, "unexpected token ");
3340 print_token(stderr, &token);
3341 fprintf(stderr, "\n");
3344 return make_invalid_expression();
3347 static expression_t *parse_array_expression(unsigned precedence,
3354 expression_t *inside = parse_expression();
3356 array_access_expression_t *array_access
3357 = allocate_ast_zero(sizeof(array_access[0]));
3359 array_access->expression.type = EXPR_ARRAY_ACCESS;
3361 type_t *type_left = left->base.datatype;
3362 type_t *type_inside = inside->base.datatype;
3363 type_t *return_type = NULL;
3365 if(type_left != NULL && type_inside != NULL) {
3366 type_left = skip_typeref(type_left);
3367 type_inside = skip_typeref(type_inside);
3369 if(is_type_pointer(type_left)) {
3370 pointer_type_t *pointer = &type_left->pointer;
3371 return_type = pointer->points_to;
3372 array_access->array_ref = left;
3373 array_access->index = inside;
3374 } else if(is_type_pointer(type_inside)) {
3375 pointer_type_t *pointer = &type_inside->pointer;
3376 return_type = pointer->points_to;
3377 array_access->array_ref = inside;
3378 array_access->index = left;
3379 array_access->flipped = true;
3381 parser_print_error_prefix();
3382 fprintf(stderr, "array access on object with non-pointer types ");
3383 print_type_quoted(type_left);
3384 fprintf(stderr, ", ");
3385 print_type_quoted(type_inside);
3386 fprintf(stderr, "\n");
3389 array_access->array_ref = left;
3390 array_access->index = inside;
3393 if(token.type != ']') {
3394 parse_error_expected("Problem while parsing array access", ']', 0);
3395 return (expression_t*) array_access;
3399 return_type = automatic_type_conversion(return_type);
3400 array_access->expression.datatype = return_type;
3402 return (expression_t*) array_access;
3405 static expression_t *parse_sizeof(unsigned precedence)
3409 sizeof_expression_t *sizeof_expression
3410 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3411 sizeof_expression->expression.type = EXPR_SIZEOF;
3412 sizeof_expression->expression.datatype = type_size_t;
3414 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3416 sizeof_expression->type = parse_typename();
3419 expression_t *expression = parse_sub_expression(precedence);
3420 expression->base.datatype = revert_automatic_type_conversion(expression);
3422 sizeof_expression->type = expression->base.datatype;
3423 sizeof_expression->size_expression = expression;
3426 return (expression_t*) sizeof_expression;
3429 static expression_t *parse_select_expression(unsigned precedence,
3430 expression_t *compound)
3433 assert(token.type == '.' || token.type == T_MINUSGREATER);
3435 bool is_pointer = (token.type == T_MINUSGREATER);
3438 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3439 select->select.compound = compound;
3441 if(token.type != T_IDENTIFIER) {
3442 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3445 symbol_t *symbol = token.v.symbol;
3446 select->select.symbol = symbol;
3449 type_t *orig_type = compound->base.datatype;
3450 if(orig_type == NULL)
3451 return make_invalid_expression();
3453 type_t *type = skip_typeref(orig_type);
3455 type_t *type_left = type;
3457 if(type->type != TYPE_POINTER) {
3458 parser_print_error_prefix();
3459 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3460 print_type_quoted(orig_type);
3461 fputc('\n', stderr);
3462 return make_invalid_expression();
3464 pointer_type_t *pointer_type = &type->pointer;
3465 type_left = pointer_type->points_to;
3467 type_left = skip_typeref(type_left);
3469 if(type_left->type != TYPE_COMPOUND_STRUCT
3470 && type_left->type != TYPE_COMPOUND_UNION) {
3471 parser_print_error_prefix();
3472 fprintf(stderr, "request for member '%s' in something not a struct or "
3473 "union, but ", symbol->string);
3474 print_type_quoted(type_left);
3475 fputc('\n', stderr);
3476 return make_invalid_expression();
3479 compound_type_t *compound_type = &type_left->compound;
3480 declaration_t *declaration = compound_type->declaration;
3482 if(!declaration->init.is_defined) {
3483 parser_print_error_prefix();
3484 fprintf(stderr, "request for member '%s' of incomplete type ",
3486 print_type_quoted(type_left);
3487 fputc('\n', stderr);
3488 return make_invalid_expression();
3491 declaration_t *iter = declaration->context.declarations;
3492 for( ; iter != NULL; iter = iter->next) {
3493 if(iter->symbol == symbol) {
3498 parser_print_error_prefix();
3499 print_type_quoted(type_left);
3500 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3501 return make_invalid_expression();
3504 /* we always do the auto-type conversions; the & and sizeof parser contains
3505 * code to revert this! */
3506 type_t *expression_type = automatic_type_conversion(iter->type);
3508 select->select.compound_entry = iter;
3509 select->base.datatype = expression_type;
3513 static expression_t *parse_call_expression(unsigned precedence,
3514 expression_t *expression)
3517 expression_t *result = allocate_expression_zero(EXPR_CALL);
3519 call_expression_t *call = &result->call;
3520 call->function = expression;
3522 function_type_t *function_type = NULL;
3523 type_t *orig_type = expression->base.datatype;
3524 if(orig_type != NULL) {
3525 type_t *type = skip_typeref(orig_type);
3527 if(is_type_pointer(type)) {
3528 pointer_type_t *pointer_type = &type->pointer;
3530 type = skip_typeref(pointer_type->points_to);
3532 if (is_type_function(type)) {
3533 function_type = &type->function;
3534 call->expression.datatype = function_type->return_type;
3537 if(function_type == NULL) {
3538 parser_print_error_prefix();
3539 fputs("called object '", stderr);
3540 print_expression(expression);
3541 fputs("' (type ", stderr);
3542 print_type_quoted(orig_type);
3543 fputs(") is not a pointer to a function\n", stderr);
3545 function_type = NULL;
3546 call->expression.datatype = NULL;
3550 /* parse arguments */
3553 if(token.type != ')') {
3554 call_argument_t *last_argument = NULL;
3557 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3559 argument->expression = parse_assignment_expression();
3560 if(last_argument == NULL) {
3561 call->arguments = argument;
3563 last_argument->next = argument;
3565 last_argument = argument;
3567 if(token.type != ',')
3574 if(function_type != NULL) {
3575 function_parameter_t *parameter = function_type->parameters;
3576 call_argument_t *argument = call->arguments;
3577 for( ; parameter != NULL && argument != NULL;
3578 parameter = parameter->next, argument = argument->next) {
3579 type_t *expected_type = parameter->type;
3580 /* TODO report context in error messages */
3581 argument->expression = create_implicit_cast(argument->expression,
3584 /* too few parameters */
3585 if(parameter != NULL) {
3586 parser_print_error_prefix();
3587 fprintf(stderr, "too few arguments to function '");
3588 print_expression(expression);
3589 fprintf(stderr, "'\n");
3590 } else if(argument != NULL) {
3591 /* too many parameters */
3592 if(!function_type->variadic
3593 && !function_type->unspecified_parameters) {
3594 parser_print_error_prefix();
3595 fprintf(stderr, "too many arguments to function '");
3596 print_expression(expression);
3597 fprintf(stderr, "'\n");
3599 /* do default promotion */
3600 for( ; argument != NULL; argument = argument->next) {
3601 type_t *type = argument->expression->base.datatype;
3606 type = skip_typeref(type);
3607 if(is_type_integer(type)) {
3608 type = promote_integer(type);
3609 } else if(type == type_float) {
3613 argument->expression
3614 = create_implicit_cast(argument->expression, type);
3623 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3625 static bool same_compound_type(const type_t *type1, const type_t *type2)
3627 if(!is_type_compound(type1))
3629 if(type1->type != type2->type)
3632 const compound_type_t *compound1 = &type1->compound;
3633 const compound_type_t *compound2 = &type2->compound;
3635 return compound1->declaration == compound2->declaration;
3638 static expression_t *parse_conditional_expression(unsigned precedence,
3639 expression_t *expression)
3643 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3645 conditional_expression_t *conditional = &result->conditional;
3646 conditional->condition = expression;
3649 type_t *condition_type_orig = expression->base.datatype;
3650 if(condition_type_orig != NULL) {
3651 type_t *condition_type = skip_typeref(condition_type_orig);
3652 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3653 type_error("expected a scalar type in conditional condition",
3654 expression->base.source_position, condition_type_orig);
3658 expression_t *true_expression = parse_expression();
3660 expression_t *false_expression = parse_sub_expression(precedence);
3662 conditional->true_expression = true_expression;
3663 conditional->false_expression = false_expression;
3665 type_t *orig_true_type = true_expression->base.datatype;
3666 type_t *orig_false_type = false_expression->base.datatype;
3667 if(orig_true_type == NULL || orig_false_type == NULL)
3670 type_t *true_type = skip_typeref(orig_true_type);
3671 type_t *false_type = skip_typeref(orig_false_type);
3674 type_t *result_type = NULL;
3675 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3676 result_type = semantic_arithmetic(true_type, false_type);
3678 true_expression = create_implicit_cast(true_expression, result_type);
3679 false_expression = create_implicit_cast(false_expression, result_type);
3681 conditional->true_expression = true_expression;
3682 conditional->false_expression = false_expression;
3683 conditional->expression.datatype = result_type;
3684 } else if (same_compound_type(true_type, false_type)
3685 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3686 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3687 /* just take 1 of the 2 types */
3688 result_type = true_type;
3689 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3690 && pointers_compatible(true_type, false_type)) {
3692 result_type = true_type;
3695 type_error_incompatible("while parsing conditional",
3696 expression->base.source_position, true_type,
3700 conditional->expression.datatype = result_type;
3704 static expression_t *parse_extension(unsigned precedence)
3706 eat(T___extension__);
3708 /* TODO enable extensions */
3710 return parse_sub_expression(precedence);
3713 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3715 eat(T___builtin_classify_type);
3717 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3718 result->base.datatype = type_int;
3721 expression_t *expression = parse_sub_expression(precedence);
3723 result->classify_type.type_expression = expression;
3728 static void semantic_incdec(unary_expression_t *expression)
3730 type_t *orig_type = expression->value->base.datatype;
3731 if(orig_type == NULL)
3734 type_t *type = skip_typeref(orig_type);
3735 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3736 /* TODO: improve error message */
3737 parser_print_error_prefix();
3738 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3742 expression->expression.datatype = orig_type;
3745 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3747 type_t *orig_type = expression->value->base.datatype;
3748 if(orig_type == NULL)
3751 type_t *type = skip_typeref(orig_type);
3752 if(!is_type_arithmetic(type)) {
3753 /* TODO: improve error message */
3754 parser_print_error_prefix();
3755 fprintf(stderr, "operation needs an arithmetic type\n");
3759 expression->expression.datatype = orig_type;
3762 static void semantic_unexpr_scalar(unary_expression_t *expression)
3764 type_t *orig_type = expression->value->base.datatype;
3765 if(orig_type == NULL)
3768 type_t *type = skip_typeref(orig_type);
3769 if (!is_type_scalar(type)) {
3770 parse_error("operand of ! must be of scalar type\n");
3774 expression->expression.datatype = orig_type;
3777 static void semantic_unexpr_integer(unary_expression_t *expression)
3779 type_t *orig_type = expression->value->base.datatype;
3780 if(orig_type == NULL)
3783 type_t *type = skip_typeref(orig_type);
3784 if (!is_type_integer(type)) {
3785 parse_error("operand of ~ must be of integer type\n");
3789 expression->expression.datatype = orig_type;
3792 static void semantic_dereference(unary_expression_t *expression)
3794 type_t *orig_type = expression->value->base.datatype;
3795 if(orig_type == NULL)
3798 type_t *type = skip_typeref(orig_type);
3799 if(!is_type_pointer(type)) {
3800 parser_print_error_prefix();
3801 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3802 print_type_quoted(orig_type);
3803 fputs(" given.\n", stderr);
3807 pointer_type_t *pointer_type = &type->pointer;
3808 type_t *result_type = pointer_type->points_to;
3810 result_type = automatic_type_conversion(result_type);
3811 expression->expression.datatype = result_type;
3814 static void semantic_take_addr(unary_expression_t *expression)
3816 expression_t *value = expression->value;
3817 value->base.datatype = revert_automatic_type_conversion(value);
3819 type_t *orig_type = value->base.datatype;
3820 if(orig_type == NULL)
3823 if(value->type == EXPR_REFERENCE) {
3824 reference_expression_t *reference = (reference_expression_t*) value;
3825 declaration_t *declaration = reference->declaration;
3826 if(declaration != NULL) {
3827 declaration->address_taken = 1;
3831 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3834 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3835 static expression_t *parse_##unexpression_type(unsigned precedence) \
3839 unary_expression_t *unary_expression \
3840 = allocate_ast_zero(sizeof(unary_expression[0])); \
3841 unary_expression->expression.type = EXPR_UNARY; \
3842 unary_expression->type = unexpression_type; \
3843 unary_expression->value = parse_sub_expression(precedence); \
3845 sfunc(unary_expression); \
3847 return (expression_t*) unary_expression; \
3850 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3851 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3852 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3853 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3854 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3855 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3856 semantic_unexpr_integer)
3857 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3859 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3862 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3864 static expression_t *parse_##unexpression_type(unsigned precedence, \
3865 expression_t *left) \
3867 (void) precedence; \
3870 unary_expression_t *unary_expression \
3871 = allocate_ast_zero(sizeof(unary_expression[0])); \
3872 unary_expression->expression.type = EXPR_UNARY; \
3873 unary_expression->type = unexpression_type; \
3874 unary_expression->value = left; \
3876 sfunc(unary_expression); \
3878 return (expression_t*) unary_expression; \
3881 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3883 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3886 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3888 /* TODO: handle complex + imaginary types */
3890 /* § 6.3.1.8 Usual arithmetic conversions */
3891 if(type_left == type_long_double || type_right == type_long_double) {
3892 return type_long_double;
3893 } else if(type_left == type_double || type_right == type_double) {
3895 } else if(type_left == type_float || type_right == type_float) {
3899 type_right = promote_integer(type_right);
3900 type_left = promote_integer(type_left);
3902 if(type_left == type_right)
3905 bool signed_left = is_type_signed(type_left);
3906 bool signed_right = is_type_signed(type_right);
3907 int rank_left = get_rank(type_left);
3908 int rank_right = get_rank(type_right);
3909 if(rank_left < rank_right) {
3910 if(signed_left == signed_right || !signed_right) {
3916 if(signed_left == signed_right || !signed_left) {
3924 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3926 expression_t *left = expression->left;
3927 expression_t *right = expression->right;
3928 type_t *orig_type_left = left->base.datatype;
3929 type_t *orig_type_right = right->base.datatype;
3931 if(orig_type_left == NULL || orig_type_right == NULL)
3934 type_t *type_left = skip_typeref(orig_type_left);
3935 type_t *type_right = skip_typeref(orig_type_right);
3937 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3938 /* TODO: improve error message */
3939 parser_print_error_prefix();
3940 fprintf(stderr, "operation needs arithmetic types\n");
3944 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3945 expression->left = create_implicit_cast(left, arithmetic_type);
3946 expression->right = create_implicit_cast(right, arithmetic_type);
3947 expression->expression.datatype = arithmetic_type;
3950 static void semantic_shift_op(binary_expression_t *expression)
3952 expression_t *left = expression->left;
3953 expression_t *right = expression->right;
3954 type_t *orig_type_left = left->base.datatype;
3955 type_t *orig_type_right = right->base.datatype;
3957 if(orig_type_left == NULL || orig_type_right == NULL)
3960 type_t *type_left = skip_typeref(orig_type_left);
3961 type_t *type_right = skip_typeref(orig_type_right);
3963 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3964 /* TODO: improve error message */
3965 parser_print_error_prefix();
3966 fprintf(stderr, "operation needs integer types\n");
3970 type_left = promote_integer(type_left);
3971 type_right = promote_integer(type_right);
3973 expression->left = create_implicit_cast(left, type_left);
3974 expression->right = create_implicit_cast(right, type_right);
3975 expression->expression.datatype = type_left;
3978 static void semantic_add(binary_expression_t *expression)
3980 expression_t *left = expression->left;
3981 expression_t *right = expression->right;
3982 type_t *orig_type_left = left->base.datatype;
3983 type_t *orig_type_right = right->base.datatype;
3985 if(orig_type_left == NULL || orig_type_right == NULL)
3988 type_t *type_left = skip_typeref(orig_type_left);
3989 type_t *type_right = skip_typeref(orig_type_right);
3992 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3993 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3994 expression->left = create_implicit_cast(left, arithmetic_type);
3995 expression->right = create_implicit_cast(right, arithmetic_type);
3996 expression->expression.datatype = arithmetic_type;
3998 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3999 expression->expression.datatype = type_left;
4000 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4001 expression->expression.datatype = type_right;
4003 parser_print_error_prefix();
4004 fprintf(stderr, "invalid operands to binary + (");
4005 print_type_quoted(orig_type_left);
4006 fprintf(stderr, ", ");
4007 print_type_quoted(orig_type_right);
4008 fprintf(stderr, ")\n");
4012 static void semantic_sub(binary_expression_t *expression)
4014 expression_t *left = expression->left;
4015 expression_t *right = expression->right;
4016 type_t *orig_type_left = left->base.datatype;
4017 type_t *orig_type_right = right->base.datatype;
4019 if(orig_type_left == NULL || orig_type_right == NULL)
4022 type_t *type_left = skip_typeref(orig_type_left);
4023 type_t *type_right = skip_typeref(orig_type_right);
4026 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4027 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4028 expression->left = create_implicit_cast(left, arithmetic_type);
4029 expression->right = create_implicit_cast(right, arithmetic_type);
4030 expression->expression.datatype = arithmetic_type;
4032 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4033 expression->expression.datatype = type_left;
4034 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4035 if(!pointers_compatible(type_left, type_right)) {
4036 parser_print_error_prefix();
4037 fprintf(stderr, "pointers to incompatible objects to binary - (");
4038 print_type_quoted(orig_type_left);
4039 fprintf(stderr, ", ");
4040 print_type_quoted(orig_type_right);
4041 fprintf(stderr, ")\n");
4043 expression->expression.datatype = type_ptrdiff_t;
4046 parser_print_error_prefix();
4047 fprintf(stderr, "invalid operands to binary - (");
4048 print_type_quoted(orig_type_left);
4049 fprintf(stderr, ", ");
4050 print_type_quoted(orig_type_right);
4051 fprintf(stderr, ")\n");
4055 static void semantic_comparison(binary_expression_t *expression)
4057 expression_t *left = expression->left;
4058 expression_t *right = expression->right;
4059 type_t *orig_type_left = left->base.datatype;
4060 type_t *orig_type_right = right->base.datatype;
4062 if(orig_type_left == NULL || orig_type_right == NULL)
4065 type_t *type_left = skip_typeref(orig_type_left);
4066 type_t *type_right = skip_typeref(orig_type_right);
4068 /* TODO non-arithmetic types */
4069 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4070 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4071 expression->left = create_implicit_cast(left, arithmetic_type);
4072 expression->right = create_implicit_cast(right, arithmetic_type);
4073 expression->expression.datatype = arithmetic_type;
4074 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4075 /* TODO check compatibility */
4076 } else if (is_type_pointer(type_left)) {
4077 expression->right = create_implicit_cast(right, type_left);
4078 } else if (is_type_pointer(type_right)) {
4079 expression->left = create_implicit_cast(left, type_right);
4081 type_error_incompatible("invalid operands in comparison",
4082 token.source_position, type_left, type_right);
4084 expression->expression.datatype = type_int;
4087 static void semantic_arithmetic_assign(binary_expression_t *expression)
4089 expression_t *left = expression->left;
4090 expression_t *right = expression->right;
4091 type_t *orig_type_left = left->base.datatype;
4092 type_t *orig_type_right = right->base.datatype;
4094 if(orig_type_left == NULL || orig_type_right == NULL)
4097 type_t *type_left = skip_typeref(orig_type_left);
4098 type_t *type_right = skip_typeref(orig_type_right);
4100 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4101 /* TODO: improve error message */
4102 parser_print_error_prefix();
4103 fprintf(stderr, "operation needs arithmetic types\n");
4107 /* combined instructions are tricky. We can't create an implicit cast on
4108 * the left side, because we need the uncasted form for the store.
4109 * The ast2firm pass has to know that left_type must be right_type
4110 * for the arithmeitc operation and create a cast by itself */
4111 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4112 expression->right = create_implicit_cast(right, arithmetic_type);
4113 expression->expression.datatype = type_left;
4116 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4118 expression_t *left = expression->left;
4119 expression_t *right = expression->right;
4120 type_t *orig_type_left = left->base.datatype;
4121 type_t *orig_type_right = right->base.datatype;
4123 if(orig_type_left == NULL || orig_type_right == NULL)
4126 type_t *type_left = skip_typeref(orig_type_left);
4127 type_t *type_right = skip_typeref(orig_type_right);
4129 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4130 /* combined instructions are tricky. We can't create an implicit cast on
4131 * the left side, because we need the uncasted form for the store.
4132 * The ast2firm pass has to know that left_type must be right_type
4133 * for the arithmeitc operation and create a cast by itself */
4134 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4135 expression->right = create_implicit_cast(right, arithmetic_type);
4136 expression->expression.datatype = type_left;
4137 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4138 expression->expression.datatype = type_left;
4140 parser_print_error_prefix();
4141 fputs("Incompatible types ", stderr);
4142 print_type_quoted(orig_type_left);
4143 fputs(" and ", stderr);
4144 print_type_quoted(orig_type_right);
4145 fputs(" in assignment\n", stderr);
4150 static void semantic_logical_op(binary_expression_t *expression)
4152 expression_t *left = expression->left;
4153 expression_t *right = expression->right;
4154 type_t *orig_type_left = left->base.datatype;
4155 type_t *orig_type_right = right->base.datatype;
4157 if(orig_type_left == NULL || orig_type_right == NULL)
4160 type_t *type_left = skip_typeref(orig_type_left);
4161 type_t *type_right = skip_typeref(orig_type_right);
4163 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4164 /* TODO: improve error message */
4165 parser_print_error_prefix();
4166 fprintf(stderr, "operation needs scalar types\n");
4170 expression->expression.datatype = type_int;
4173 static bool has_const_fields(type_t *type)
4180 static void semantic_binexpr_assign(binary_expression_t *expression)
4182 expression_t *left = expression->left;
4183 type_t *orig_type_left = left->base.datatype;
4185 if(orig_type_left == NULL)
4188 type_t *type_left = revert_automatic_type_conversion(left);
4189 type_left = skip_typeref(orig_type_left);
4191 /* must be a modifiable lvalue */
4192 if (is_type_array(type_left)) {
4193 parser_print_error_prefix();
4194 fprintf(stderr, "Cannot assign to arrays ('");
4195 print_expression(left);
4196 fprintf(stderr, "')\n");
4199 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4200 parser_print_error_prefix();
4201 fprintf(stderr, "assignment to readonly location '");
4202 print_expression(left);
4203 fprintf(stderr, "' (type ");
4204 print_type_quoted(orig_type_left);
4205 fprintf(stderr, ")\n");
4208 if(is_type_incomplete(type_left)) {
4209 parser_print_error_prefix();
4210 fprintf(stderr, "left-hand side of assignment '");
4211 print_expression(left);
4212 fprintf(stderr, "' has incomplete type ");
4213 print_type_quoted(orig_type_left);
4214 fprintf(stderr, "\n");
4217 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4218 parser_print_error_prefix();
4219 fprintf(stderr, "can't assign to '");
4220 print_expression(left);
4221 fprintf(stderr, "' because compound type ");
4222 print_type_quoted(orig_type_left);
4223 fprintf(stderr, " has readonly fields\n");
4227 semantic_assign(orig_type_left, &expression->right, "assignment");
4229 expression->expression.datatype = orig_type_left;
4232 static void semantic_comma(binary_expression_t *expression)
4234 expression->expression.datatype = expression->right->base.datatype;
4237 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4238 static expression_t *parse_##binexpression_type(unsigned precedence, \
4239 expression_t *left) \
4243 expression_t *right = parse_sub_expression(precedence + lr); \
4245 binary_expression_t *binexpr \
4246 = allocate_ast_zero(sizeof(binexpr[0])); \
4247 binexpr->expression.type = EXPR_BINARY; \
4248 binexpr->type = binexpression_type; \
4249 binexpr->left = left; \
4250 binexpr->right = right; \
4253 return (expression_t*) binexpr; \
4256 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
4257 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
4258 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
4259 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
4260 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
4261 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
4262 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
4263 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
4264 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
4265 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
4266 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
4267 semantic_comparison, 1)
4268 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
4269 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
4270 semantic_comparison, 1)
4271 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
4272 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
4273 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
4274 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
4275 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4276 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4277 semantic_shift_op, 1)
4278 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4279 semantic_shift_op, 1)
4280 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4281 semantic_arithmetic_addsubb_assign, 0)
4282 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4283 semantic_arithmetic_addsubb_assign, 0)
4284 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4285 semantic_arithmetic_assign, 0)
4286 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4287 semantic_arithmetic_assign, 0)
4288 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4289 semantic_arithmetic_assign, 0)
4290 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4291 semantic_arithmetic_assign, 0)
4292 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4293 semantic_arithmetic_assign, 0)
4294 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4295 semantic_arithmetic_assign, 0)
4296 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4297 semantic_arithmetic_assign, 0)
4298 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4299 semantic_arithmetic_assign, 0)
4301 static expression_t *parse_sub_expression(unsigned precedence)
4303 if(token.type < 0) {
4304 return expected_expression_error();
4307 expression_parser_function_t *parser
4308 = &expression_parsers[token.type];
4309 source_position_t source_position = token.source_position;
4312 if(parser->parser != NULL) {
4313 left = parser->parser(parser->precedence);
4315 left = parse_primary_expression();
4317 assert(left != NULL);
4318 left->base.source_position = source_position;
4321 if(token.type < 0) {
4322 return expected_expression_error();
4325 parser = &expression_parsers[token.type];
4326 if(parser->infix_parser == NULL)
4328 if(parser->infix_precedence < precedence)
4331 left = parser->infix_parser(parser->infix_precedence, left);
4333 assert(left != NULL);
4334 assert(left->type != EXPR_UNKNOWN);
4335 left->base.source_position = source_position;
4341 static expression_t *parse_expression(void)
4343 return parse_sub_expression(1);
4348 static void register_expression_parser(parse_expression_function parser,
4349 int token_type, unsigned precedence)
4351 expression_parser_function_t *entry = &expression_parsers[token_type];
4353 if(entry->parser != NULL) {
4354 fprintf(stderr, "for token ");
4355 print_token_type(stderr, (token_type_t) token_type);
4356 fprintf(stderr, "\n");
4357 panic("trying to register multiple expression parsers for a token");
4359 entry->parser = parser;
4360 entry->precedence = precedence;
4363 static void register_expression_infix_parser(
4364 parse_expression_infix_function parser, int token_type,
4365 unsigned precedence)
4367 expression_parser_function_t *entry = &expression_parsers[token_type];
4369 if(entry->infix_parser != NULL) {
4370 fprintf(stderr, "for token ");
4371 print_token_type(stderr, (token_type_t) token_type);
4372 fprintf(stderr, "\n");
4373 panic("trying to register multiple infix expression parsers for a "
4376 entry->infix_parser = parser;
4377 entry->infix_precedence = precedence;
4380 static void init_expression_parsers(void)
4382 memset(&expression_parsers, 0, sizeof(expression_parsers));
4384 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4385 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4386 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4387 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4388 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4389 T_GREATERGREATER, 16);
4390 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4391 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4392 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4393 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4394 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4395 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4396 T_GREATEREQUAL, 14);
4397 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4398 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4399 T_EXCLAMATIONMARKEQUAL, 13);
4400 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4401 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4402 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4403 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4404 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4405 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4406 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4407 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4408 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4409 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4410 T_ASTERISKEQUAL, 2);
4411 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4412 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4414 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4415 T_LESSLESSEQUAL, 2);
4416 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4417 T_GREATERGREATEREQUAL, 2);
4418 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4420 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4422 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4425 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4427 register_expression_infix_parser(parse_array_expression, '[', 30);
4428 register_expression_infix_parser(parse_call_expression, '(', 30);
4429 register_expression_infix_parser(parse_select_expression, '.', 30);
4430 register_expression_infix_parser(parse_select_expression,
4431 T_MINUSGREATER, 30);
4432 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4434 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4437 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4438 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4439 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4440 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4441 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4442 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4443 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4444 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4445 register_expression_parser(parse_sizeof, T_sizeof, 25);
4446 register_expression_parser(parse_extension, T___extension__, 25);
4447 register_expression_parser(parse_builtin_classify_type,
4448 T___builtin_classify_type, 25);
4451 static asm_constraint_t *parse_asm_constraints(void)
4453 asm_constraint_t *result = NULL;
4454 asm_constraint_t *last = NULL;
4456 while(token.type == T_STRING_LITERAL || token.type == '[') {
4457 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4458 memset(constraint, 0, sizeof(constraint[0]));
4460 if(token.type == '[') {
4462 if(token.type != T_IDENTIFIER) {
4463 parse_error_expected("while parsing asm constraint",
4467 constraint->symbol = token.v.symbol;
4472 constraint->constraints = parse_string_literals();
4474 constraint->expression = parse_expression();
4478 last->next = constraint;
4480 result = constraint;
4484 if(token.type != ',')
4492 static asm_clobber_t *parse_asm_clobbers(void)
4494 asm_clobber_t *result = NULL;
4495 asm_clobber_t *last = NULL;
4497 while(token.type == T_STRING_LITERAL) {
4498 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4499 clobber->clobber = parse_string_literals();
4502 last->next = clobber;
4508 if(token.type != ',')
4516 static statement_t *parse_asm_statement(void)
4520 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4521 statement->base.source_position = token.source_position;
4523 asm_statement_t *asm_statement = &statement->asms;
4525 if(token.type == T_volatile) {
4527 asm_statement->is_volatile = true;
4531 asm_statement->asm_text = parse_string_literals();
4533 if(token.type != ':')
4537 asm_statement->inputs = parse_asm_constraints();
4538 if(token.type != ':')
4542 asm_statement->outputs = parse_asm_constraints();
4543 if(token.type != ':')
4547 asm_statement->clobbers = parse_asm_clobbers();
4555 static statement_t *parse_case_statement(void)
4559 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4561 statement->base.source_position = token.source_position;
4562 statement->case_label.expression = parse_expression();
4565 statement->case_label.label_statement = parse_statement();
4570 static statement_t *parse_default_statement(void)
4574 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4576 statement->base.source_position = token.source_position;
4579 statement->label.label_statement = parse_statement();
4584 static declaration_t *get_label(symbol_t *symbol)
4586 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4587 assert(current_function != NULL);
4588 /* if we found a label in the same function, then we already created the
4590 if(candidate != NULL
4591 && candidate->parent_context == ¤t_function->context) {
4595 /* otherwise we need to create a new one */
4596 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4597 declaration->namespc = NAMESPACE_LABEL;
4598 declaration->symbol = symbol;
4600 label_push(declaration);
4605 static statement_t *parse_label_statement(void)
4607 assert(token.type == T_IDENTIFIER);
4608 symbol_t *symbol = token.v.symbol;
4611 declaration_t *label = get_label(symbol);
4613 /* if source position is already set then the label is defined twice,
4614 * otherwise it was just mentioned in a goto so far */
4615 if(label->source_position.input_name != NULL) {
4616 parser_print_error_prefix();
4617 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4618 parser_print_error_prefix_pos(label->source_position);
4619 fprintf(stderr, "previous definition of '%s' was here\n",
4622 label->source_position = token.source_position;
4625 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4627 label_statement->statement.type = STATEMENT_LABEL;
4628 label_statement->statement.source_position = token.source_position;
4629 label_statement->label = label;
4633 if(token.type == '}') {
4634 parse_error("label at end of compound statement");
4635 return (statement_t*) label_statement;
4637 label_statement->label_statement = parse_statement();
4640 return (statement_t*) label_statement;
4643 static statement_t *parse_if(void)
4647 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4648 statement->statement.type = STATEMENT_IF;
4649 statement->statement.source_position = token.source_position;
4652 statement->condition = parse_expression();
4655 statement->true_statement = parse_statement();
4656 if(token.type == T_else) {
4658 statement->false_statement = parse_statement();
4661 return (statement_t*) statement;
4664 static statement_t *parse_switch(void)
4668 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4669 statement->statement.type = STATEMENT_SWITCH;
4670 statement->statement.source_position = token.source_position;
4673 statement->expression = parse_expression();
4675 statement->body = parse_statement();
4677 return (statement_t*) statement;
4680 static statement_t *parse_while(void)
4684 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4685 statement->statement.type = STATEMENT_WHILE;
4686 statement->statement.source_position = token.source_position;
4689 statement->condition = parse_expression();
4691 statement->body = parse_statement();
4693 return (statement_t*) statement;
4696 static statement_t *parse_do(void)
4700 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4701 statement->statement.type = STATEMENT_DO_WHILE;
4702 statement->statement.source_position = token.source_position;
4704 statement->body = parse_statement();
4707 statement->condition = parse_expression();
4711 return (statement_t*) statement;
4714 static statement_t *parse_for(void)
4718 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4719 statement->statement.type = STATEMENT_FOR;
4720 statement->statement.source_position = token.source_position;
4724 int top = environment_top();
4725 context_t *last_context = context;
4726 set_context(&statement->context);
4728 if(token.type != ';') {
4729 if(is_declaration_specifier(&token, false)) {
4730 parse_declaration(record_declaration);
4732 statement->initialisation = parse_expression();
4739 if(token.type != ';') {
4740 statement->condition = parse_expression();
4743 if(token.type != ')') {
4744 statement->step = parse_expression();
4747 statement->body = parse_statement();
4749 assert(context == &statement->context);
4750 set_context(last_context);
4751 environment_pop_to(top);
4753 return (statement_t*) statement;
4756 static statement_t *parse_goto(void)
4760 if(token.type != T_IDENTIFIER) {
4761 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4765 symbol_t *symbol = token.v.symbol;
4768 declaration_t *label = get_label(symbol);
4770 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4772 statement->statement.type = STATEMENT_GOTO;
4773 statement->statement.source_position = token.source_position;
4775 statement->label = label;
4779 return (statement_t*) statement;
4782 static statement_t *parse_continue(void)
4787 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4788 statement->type = STATEMENT_CONTINUE;
4789 statement->base.source_position = token.source_position;
4794 static statement_t *parse_break(void)
4799 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4800 statement->type = STATEMENT_BREAK;
4801 statement->base.source_position = token.source_position;
4806 static statement_t *parse_return(void)
4810 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4812 statement->statement.type = STATEMENT_RETURN;
4813 statement->statement.source_position = token.source_position;
4815 assert(is_type_function(current_function->type));
4816 function_type_t *function_type = ¤t_function->type->function;
4817 type_t *return_type = function_type->return_type;
4819 expression_t *return_value = NULL;
4820 if(token.type != ';') {
4821 return_value = parse_expression();
4825 if(return_type == NULL)
4826 return (statement_t*) statement;
4828 return_type = skip_typeref(return_type);
4830 if(return_value != NULL) {
4831 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4833 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4834 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4835 parse_warning("'return' with a value, in function returning void");
4836 return_value = NULL;
4838 if(return_type != NULL) {
4839 semantic_assign(return_type, &return_value, "'return'");
4843 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4844 parse_warning("'return' without value, in function returning "
4848 statement->return_value = return_value;
4850 return (statement_t*) statement;
4853 static statement_t *parse_declaration_statement(void)
4855 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4857 statement->base.source_position = token.source_position;
4859 declaration_t *before = last_declaration;
4860 parse_declaration(record_declaration);
4862 if(before == NULL) {
4863 statement->declaration.declarations_begin = context->declarations;
4865 statement->declaration.declarations_begin = before->next;
4867 statement->declaration.declarations_end = last_declaration;
4872 static statement_t *parse_expression_statement(void)
4874 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4876 statement->base.source_position = token.source_position;
4877 statement->expression.expression = parse_expression();
4884 static statement_t *parse_statement(void)
4886 statement_t *statement = NULL;
4888 /* declaration or statement */
4889 switch(token.type) {
4891 statement = parse_asm_statement();
4895 statement = parse_case_statement();
4899 statement = parse_default_statement();
4903 statement = parse_compound_statement();
4907 statement = parse_if();
4911 statement = parse_switch();
4915 statement = parse_while();
4919 statement = parse_do();
4923 statement = parse_for();
4927 statement = parse_goto();
4931 statement = parse_continue();
4935 statement = parse_break();
4939 statement = parse_return();
4948 if(look_ahead(1)->type == ':') {
4949 statement = parse_label_statement();
4953 if(is_typedef_symbol(token.v.symbol)) {
4954 statement = parse_declaration_statement();
4958 statement = parse_expression_statement();
4961 case T___extension__:
4962 /* this can be a prefix to a declaration or an expression statement */
4963 /* we simply eat it now and parse the rest with tail recursion */
4966 } while(token.type == T___extension__);
4967 statement = parse_statement();
4971 statement = parse_declaration_statement();
4975 statement = parse_expression_statement();
4979 assert(statement == NULL
4980 || statement->base.source_position.input_name != NULL);
4985 static statement_t *parse_compound_statement(void)
4987 compound_statement_t *compound_statement
4988 = allocate_ast_zero(sizeof(compound_statement[0]));
4989 compound_statement->statement.type = STATEMENT_COMPOUND;
4990 compound_statement->statement.source_position = token.source_position;
4994 int top = environment_top();
4995 context_t *last_context = context;
4996 set_context(&compound_statement->context);
4998 statement_t *last_statement = NULL;
5000 while(token.type != '}' && token.type != T_EOF) {
5001 statement_t *statement = parse_statement();
5002 if(statement == NULL)
5005 if(last_statement != NULL) {
5006 last_statement->base.next = statement;
5008 compound_statement->statements = statement;
5011 while(statement->base.next != NULL)
5012 statement = statement->base.next;
5014 last_statement = statement;
5017 if(token.type != '}') {
5018 parser_print_error_prefix_pos(
5019 compound_statement->statement.source_position);
5020 fprintf(stderr, "end of file while looking for closing '}'\n");
5024 assert(context == &compound_statement->context);
5025 set_context(last_context);
5026 environment_pop_to(top);
5028 return (statement_t*) compound_statement;
5031 static void initialize_builtins(void)
5033 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5034 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5035 type_size_t = make_global_typedef("__SIZE_TYPE__",
5036 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
5037 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
5038 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
5041 static translation_unit_t *parse_translation_unit(void)
5043 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5045 assert(global_context == NULL);
5046 global_context = &unit->context;
5048 assert(context == NULL);
5049 set_context(&unit->context);
5051 initialize_builtins();
5053 while(token.type != T_EOF) {
5054 parse_external_declaration();
5057 assert(context == &unit->context);
5059 last_declaration = NULL;
5061 assert(global_context == &unit->context);
5062 global_context = NULL;
5067 translation_unit_t *parse(void)
5069 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5070 label_stack = NEW_ARR_F(stack_entry_t, 0);
5071 found_error = false;
5073 type_set_output(stderr);
5074 ast_set_output(stderr);
5076 lookahead_bufpos = 0;
5077 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5080 translation_unit_t *unit = parse_translation_unit();
5082 DEL_ARR_F(environment_stack);
5083 DEL_ARR_F(label_stack);
5091 void init_parser(void)
5093 init_expression_parsers();
5094 obstack_init(&temp_obst);
5096 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5097 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5098 TYPE_QUALIFIER_NONE);
5099 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5100 TYPE_QUALIFIER_NONE);
5101 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5102 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5103 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5104 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5105 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5108 void exit_parser(void)
5110 obstack_free(&temp_obst, NULL);