11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_long_double = NULL;
42 static type_t *type_double = NULL;
43 static type_t *type_float = NULL;
44 static type_t *type_char = NULL;
45 static type_t *type_string = NULL;
46 static type_t *type_void = NULL;
47 static type_t *type_void_ptr = NULL;
49 type_t *type_size_t = NULL;
50 type_t *type_ptrdiff_t = NULL;
51 type_t *type_wchar_t = NULL;
52 type_t *type_wchar_ptr_t = NULL;
54 static statement_t *parse_compound_statement(void);
55 static statement_t *parse_statement(void);
57 static expression_t *parse_sub_expression(unsigned precedence);
58 static expression_t *parse_expression(void);
59 static type_t *parse_typename(void);
61 #define STORAGE_CLASSES \
68 #define TYPE_QUALIFIERS \
74 #ifdef PROVIDE_COMPLEX
75 #define COMPLEX_SPECIFIERS \
77 #define IMAGINARY_SPECIFIERS \
80 #define COMPLEX_SPECIFIERS
81 #define IMAGINARY_SPECIFIERS
84 #define TYPE_SPECIFIERS \
102 #define DECLARATION_START \
107 #define TYPENAME_START \
111 static void *allocate_ast_zero(size_t size)
113 void *res = allocate_ast(size);
114 memset(res, 0, size);
118 static size_t get_statement_struct_size(statement_type_t type)
120 static const size_t sizes[] = {
121 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
122 [STATEMENT_RETURN] = sizeof(return_statement_t),
123 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
124 [STATEMENT_IF] = sizeof(if_statement_t),
125 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
126 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
127 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
128 [STATEMENT_BREAK] = sizeof(statement_base_t),
129 [STATEMENT_GOTO] = sizeof(goto_statement_t),
130 [STATEMENT_LABEL] = sizeof(label_statement_t),
131 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
132 [STATEMENT_WHILE] = sizeof(while_statement_t),
133 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
134 [STATEMENT_FOR] = sizeof(for_statement_t),
135 [STATEMENT_ASM] = sizeof(asm_statement_t)
137 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
138 assert(type <= STATEMENT_ASM);
139 assert(sizes[type] != 0);
143 static statement_t *allocate_statement_zero(statement_type_t type)
145 size_t size = get_statement_struct_size(type);
146 statement_t *res = allocate_ast_zero(size);
148 res->base.type = type;
153 static size_t get_expression_struct_size(expression_type_t type)
155 static const size_t sizes[] = {
156 [EXPR_INVALID] = sizeof(expression_base_t),
157 [EXPR_REFERENCE] = sizeof(reference_expression_t),
158 [EXPR_CONST] = sizeof(const_expression_t),
159 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
160 [EXPR_CALL] = sizeof(call_expression_t),
161 [EXPR_UNARY] = sizeof(unary_expression_t),
162 [EXPR_BINARY] = sizeof(binary_expression_t),
163 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
164 [EXPR_SELECT] = sizeof(select_expression_t),
165 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
166 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
167 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
168 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
169 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
170 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
171 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
172 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
173 [EXPR_STATEMENT] = sizeof(statement_expression_t)
175 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
176 assert(type <= EXPR_STATEMENT);
177 assert(sizes[type] != 0);
181 static expression_t *allocate_expression_zero(expression_type_t type)
183 size_t size = get_expression_struct_size(type);
184 expression_t *res = allocate_ast_zero(size);
186 res->base.type = type;
190 static size_t get_type_struct_size(type_type_t type)
192 static const size_t sizes[] = {
193 [TYPE_ATOMIC] = sizeof(atomic_type_t),
194 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
195 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
196 [TYPE_ENUM] = sizeof(enum_type_t),
197 [TYPE_FUNCTION] = sizeof(function_type_t),
198 [TYPE_POINTER] = sizeof(pointer_type_t),
199 [TYPE_ARRAY] = sizeof(array_type_t),
200 [TYPE_BUILTIN] = sizeof(builtin_type_t),
201 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
202 [TYPE_TYPEOF] = sizeof(typeof_type_t),
204 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
205 assert(type <= TYPE_TYPEOF);
206 assert(sizes[type] != 0);
210 static type_t *allocate_type_zero(type_type_t type)
212 size_t size = get_type_struct_size(type);
213 type_t *res = obstack_alloc(type_obst, size);
214 memset(res, 0, size);
216 res->base.type = type;
220 static size_t get_initializer_size(initializer_type_t type)
222 static const size_t sizes[] = {
223 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
224 [INITIALIZER_STRING] = sizeof(initializer_string_t),
225 [INITIALIZER_LIST] = sizeof(initializer_list_t)
227 assert(type < INITIALIZER_COUNT);
228 assert(sizes[type] != 0);
232 static initializer_t *allocate_initializer(initializer_type_t type)
234 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
240 static void free_type(void *type)
242 obstack_free(type_obst, type);
246 * returns the top element of the environment stack
248 static size_t environment_top(void)
250 return ARR_LEN(environment_stack);
253 static size_t label_top(void)
255 return ARR_LEN(label_stack);
260 static inline void next_token(void)
262 token = lookahead_buffer[lookahead_bufpos];
263 lookahead_buffer[lookahead_bufpos] = lexer_token;
266 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
269 print_token(stderr, &token);
270 fprintf(stderr, "\n");
274 static inline const token_t *look_ahead(int num)
276 assert(num > 0 && num <= MAX_LOOKAHEAD);
277 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
278 return & lookahead_buffer[pos];
281 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
283 static void error(void)
286 #ifdef ABORT_ON_ERROR
291 static void parser_print_prefix_pos(const source_position_t source_position)
293 fputs(source_position.input_name, stderr);
295 fprintf(stderr, "%u", source_position.linenr);
299 static void parser_print_error_prefix_pos(
300 const source_position_t source_position)
302 parser_print_prefix_pos(source_position);
303 fputs("error: ", stderr);
307 static void parser_print_error_prefix(void)
309 parser_print_error_prefix_pos(token.source_position);
312 static void parse_error(const char *message)
314 parser_print_error_prefix();
315 fprintf(stderr, "parse error: %s\n", message);
318 static void parser_print_warning_prefix_pos(
319 const source_position_t source_position)
321 parser_print_prefix_pos(source_position);
322 fputs("warning: ", stderr);
325 static void parse_warning_pos(const source_position_t source_position,
326 const char *const message)
328 parser_print_prefix_pos(source_position);
329 fprintf(stderr, "warning: %s\n", message);
332 static void parse_warning(const char *message)
334 parse_warning_pos(token.source_position, message);
337 static void parse_error_expected(const char *message, ...)
342 if(message != NULL) {
343 parser_print_error_prefix();
344 fprintf(stderr, "%s\n", message);
346 parser_print_error_prefix();
347 fputs("Parse error: got ", stderr);
348 print_token(stderr, &token);
349 fputs(", expected ", stderr);
351 va_start(args, message);
352 token_type_t token_type = va_arg(args, token_type_t);
353 while(token_type != 0) {
357 fprintf(stderr, ", ");
359 print_token_type(stderr, token_type);
360 token_type = va_arg(args, token_type_t);
363 fprintf(stderr, "\n");
366 static void print_type_quoted(type_t *type)
373 static void type_error(const char *msg, const source_position_t source_position,
376 parser_print_error_prefix_pos(source_position);
377 fprintf(stderr, "%s, but found type ", msg);
378 print_type_quoted(type);
382 static void type_error_incompatible(const char *msg,
383 const source_position_t source_position, type_t *type1, type_t *type2)
385 parser_print_error_prefix_pos(source_position);
386 fprintf(stderr, "%s, incompatible types: ", msg);
387 print_type_quoted(type1);
388 fprintf(stderr, " - ");
389 print_type_quoted(type2);
390 fprintf(stderr, ")\n");
393 static void eat_block(void)
395 if(token.type == '{')
398 while(token.type != '}') {
399 if(token.type == T_EOF)
401 if(token.type == '{') {
410 static void eat_statement(void)
412 while(token.type != ';') {
413 if(token.type == T_EOF)
415 if(token.type == '}')
417 if(token.type == '{') {
426 static void eat_brace(void)
428 if(token.type == '(')
431 while(token.type != ')') {
432 if(token.type == T_EOF)
434 if(token.type == ')' || token.type == ';' || token.type == '}') {
437 if(token.type == '(') {
441 if(token.type == '{') {
450 #define expect(expected) \
451 if(UNLIKELY(token.type != (expected))) { \
452 parse_error_expected(NULL, (expected), 0); \
458 #define expect_block(expected) \
459 if(UNLIKELY(token.type != (expected))) { \
460 parse_error_expected(NULL, (expected), 0); \
466 #define expect_void(expected) \
467 if(UNLIKELY(token.type != (expected))) { \
468 parse_error_expected(NULL, (expected), 0); \
474 static void set_context(context_t *new_context)
476 context = new_context;
478 last_declaration = new_context->declarations;
479 if(last_declaration != NULL) {
480 while(last_declaration->next != NULL) {
481 last_declaration = last_declaration->next;
487 * called when we find a 2nd declarator for an identifier we already have a
490 static bool is_compatible_declaration (declaration_t *declaration,
491 declaration_t *previous)
493 if (declaration->type->type == TYPE_FUNCTION &&
494 previous->type->type == TYPE_FUNCTION &&
495 previous->type->function.unspecified_parameters) {
496 function_type_t* const prev_func = &previous->type->function;
497 function_type_t* const decl_func = &declaration->type->function;
498 if (prev_func->unspecified_parameters &&
499 prev_func->result_type == decl_func->result_type) {
500 declaration->type = previous->type;
504 /* TODO: not correct yet */
505 return declaration->type == previous->type;
508 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
510 declaration_t *declaration = symbol->declaration;
511 for( ; declaration != NULL; declaration = declaration->symbol_next) {
512 if(declaration->namespc == namespc)
519 static const char *get_namespace_prefix(namespace_t namespc)
522 case NAMESPACE_NORMAL:
524 case NAMESPACE_UNION:
526 case NAMESPACE_STRUCT:
530 case NAMESPACE_LABEL:
533 panic("invalid namespace found");
537 * pushs an environment_entry on the environment stack and links the
538 * corresponding symbol to the new entry
540 static declaration_t *stack_push(stack_entry_t **stack_ptr,
541 declaration_t *declaration,
542 context_t *parent_context)
544 symbol_t *symbol = declaration->symbol;
545 namespace_t namespc = (namespace_t)declaration->namespc;
547 /* a declaration should be only pushed once */
548 assert(declaration->parent_context == NULL);
549 declaration->parent_context = parent_context;
551 declaration_t *previous_declaration = get_declaration(symbol, namespc);
552 assert(declaration != previous_declaration);
553 if(previous_declaration != NULL
554 && previous_declaration->parent_context == context) {
555 if(!is_compatible_declaration(declaration, previous_declaration)) {
556 parser_print_error_prefix_pos(declaration->source_position);
557 fprintf(stderr, "definition of symbol %s%s with type ",
558 get_namespace_prefix(namespc), symbol->string);
559 print_type_quoted(declaration->type);
561 parser_print_error_prefix_pos(
562 previous_declaration->source_position);
563 fprintf(stderr, "is incompatible with previous declaration "
565 print_type_quoted(previous_declaration->type);
568 unsigned old_storage_class = previous_declaration->storage_class;
569 unsigned new_storage_class = declaration->storage_class;
570 if (current_function == NULL) {
571 if (old_storage_class != STORAGE_CLASS_STATIC &&
572 new_storage_class == STORAGE_CLASS_STATIC) {
573 parser_print_error_prefix_pos(declaration->source_position);
575 "static declaration of '%s' follows non-static declaration\n",
577 parser_print_error_prefix_pos(previous_declaration->source_position);
578 fprintf(stderr, "previous declaration of '%s' was here\n",
581 if (old_storage_class == STORAGE_CLASS_EXTERN) {
582 if (new_storage_class == STORAGE_CLASS_NONE) {
583 previous_declaration->storage_class = STORAGE_CLASS_NONE;
586 parser_print_warning_prefix_pos(declaration->source_position);
587 fprintf(stderr, "redundant declaration for '%s'\n",
589 parser_print_warning_prefix_pos(previous_declaration->source_position);
590 fprintf(stderr, "previous declaration of '%s' was here\n",
595 if (old_storage_class == STORAGE_CLASS_EXTERN &&
596 new_storage_class == STORAGE_CLASS_EXTERN) {
597 parser_print_warning_prefix_pos(declaration->source_position);
598 fprintf(stderr, "redundant extern declaration for '%s'\n",
600 parser_print_warning_prefix_pos(previous_declaration->source_position);
601 fprintf(stderr, "previous declaration of '%s' was here\n",
604 parser_print_error_prefix_pos(declaration->source_position);
605 if (old_storage_class == new_storage_class) {
606 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
608 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
610 parser_print_error_prefix_pos(previous_declaration->source_position);
611 fprintf(stderr, "previous declaration of '%s' was here\n",
616 return previous_declaration;
619 /* remember old declaration */
621 entry.symbol = symbol;
622 entry.old_declaration = symbol->declaration;
623 entry.namespc = (unsigned short) namespc;
624 ARR_APP1(stack_entry_t, *stack_ptr, entry);
626 /* replace/add declaration into declaration list of the symbol */
627 if(symbol->declaration == NULL) {
628 symbol->declaration = declaration;
630 declaration_t *iter_last = NULL;
631 declaration_t *iter = symbol->declaration;
632 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
633 /* replace an entry? */
634 if(iter->namespc == namespc) {
635 if(iter_last == NULL) {
636 symbol->declaration = declaration;
638 iter_last->symbol_next = declaration;
640 declaration->symbol_next = iter->symbol_next;
645 assert(iter_last->symbol_next == NULL);
646 iter_last->symbol_next = declaration;
653 static declaration_t *environment_push(declaration_t *declaration)
655 assert(declaration->source_position.input_name != NULL);
656 return stack_push(&environment_stack, declaration, context);
659 static declaration_t *label_push(declaration_t *declaration)
661 return stack_push(&label_stack, declaration, ¤t_function->context);
665 * pops symbols from the environment stack until @p new_top is the top element
667 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
669 stack_entry_t *stack = *stack_ptr;
670 size_t top = ARR_LEN(stack);
673 assert(new_top <= top);
677 for(i = top; i > new_top; --i) {
678 stack_entry_t *entry = & stack[i - 1];
680 declaration_t *old_declaration = entry->old_declaration;
681 symbol_t *symbol = entry->symbol;
682 namespace_t namespc = (namespace_t)entry->namespc;
684 /* replace/remove declaration */
685 declaration_t *declaration = symbol->declaration;
686 assert(declaration != NULL);
687 if(declaration->namespc == namespc) {
688 if(old_declaration == NULL) {
689 symbol->declaration = declaration->symbol_next;
691 symbol->declaration = old_declaration;
694 declaration_t *iter_last = declaration;
695 declaration_t *iter = declaration->symbol_next;
696 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
697 /* replace an entry? */
698 if(iter->namespc == namespc) {
699 assert(iter_last != NULL);
700 iter_last->symbol_next = old_declaration;
701 old_declaration->symbol_next = iter->symbol_next;
705 assert(iter != NULL);
709 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
712 static void environment_pop_to(size_t new_top)
714 stack_pop_to(&environment_stack, new_top);
717 static void label_pop_to(size_t new_top)
719 stack_pop_to(&label_stack, new_top);
723 static int get_rank(const type_t *type)
725 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
726 * and esp. footnote 108). However we can't fold constants (yet), so we
727 * can't decide wether unsigned int is possible, while int always works.
728 * (unsigned int would be preferable when possible... for stuff like
729 * struct { enum { ... } bla : 4; } ) */
730 if(type->type == TYPE_ENUM)
731 return ATOMIC_TYPE_INT;
733 assert(type->type == TYPE_ATOMIC);
734 const atomic_type_t *atomic_type = &type->atomic;
735 atomic_type_type_t atype = atomic_type->atype;
739 static type_t *promote_integer(type_t *type)
741 if(get_rank(type) < ATOMIC_TYPE_INT)
747 static expression_t *create_cast_expression(expression_t *expression,
750 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
752 cast->unary.type = UNEXPR_CAST_IMPLICIT;
753 cast->unary.value = expression;
754 cast->base.datatype = dest_type;
759 static bool is_null_expression(const expression_t *const expression)
761 if (expression->type != EXPR_CONST)
764 type_t *const type = skip_typeref(expression->base.datatype);
765 if (!is_type_integer(type))
768 return expression->conste.v.int_value == 0;
771 static expression_t *create_implicit_cast(expression_t *expression,
774 type_t *source_type = expression->base.datatype;
776 if(source_type == NULL)
779 source_type = skip_typeref(source_type);
780 dest_type = skip_typeref(dest_type);
782 if(source_type == dest_type)
785 switch (dest_type->type) {
787 /* TODO warning for implicitly converting to enum */
789 if (source_type->type != TYPE_ATOMIC &&
790 source_type->type != TYPE_ENUM) {
791 panic("casting of non-atomic types not implemented yet");
794 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
795 type_error_incompatible("can't cast types",
796 expression->base.source_position, source_type,
801 return create_cast_expression(expression, dest_type);
804 switch (source_type->type) {
806 if (is_null_expression(expression)) {
807 return create_cast_expression(expression, dest_type);
812 if (pointers_compatible(source_type, dest_type)) {
813 return create_cast_expression(expression, dest_type);
818 array_type_t *array_type = &source_type->array;
819 pointer_type_t *pointer_type = &dest_type->pointer;
820 if (types_compatible(array_type->element_type,
821 pointer_type->points_to)) {
822 return create_cast_expression(expression, dest_type);
828 panic("casting of non-atomic types not implemented yet");
831 type_error_incompatible("can't implicitly cast types",
832 expression->base.source_position, source_type, dest_type);
836 panic("casting of non-atomic types not implemented yet");
840 /** Implements the rules from § 6.5.16.1 */
841 static void semantic_assign(type_t *orig_type_left, expression_t **right,
844 type_t *orig_type_right = (*right)->base.datatype;
846 if(orig_type_right == NULL)
849 type_t *const type_left = skip_typeref(orig_type_left);
850 type_t *const type_right = skip_typeref(orig_type_right);
852 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
853 (is_type_pointer(type_left) && is_null_expression(*right)) ||
854 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
855 && is_type_pointer(type_right))) {
856 *right = create_implicit_cast(*right, type_left);
860 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
861 pointer_type_t *pointer_type_left = &type_left->pointer;
862 pointer_type_t *pointer_type_right = &type_right->pointer;
863 type_t *points_to_left = pointer_type_left->points_to;
864 type_t *points_to_right = pointer_type_right->points_to;
866 points_to_left = skip_typeref(points_to_left);
867 points_to_right = skip_typeref(points_to_right);
869 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
870 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
871 && !types_compatible(points_to_left, points_to_right)) {
872 goto incompatible_assign_types;
875 /* the left type has all qualifiers from the right type */
876 unsigned missing_qualifiers
877 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
878 if(missing_qualifiers != 0) {
879 parser_print_error_prefix();
880 fprintf(stderr, "destination type ");
881 print_type_quoted(type_left);
882 fprintf(stderr, " in %s from type ", context);
883 print_type_quoted(type_right);
884 fprintf(stderr, " lacks qualifiers '");
885 print_type_qualifiers(missing_qualifiers);
886 fprintf(stderr, "' in pointed-to type\n");
890 *right = create_implicit_cast(*right, type_left);
894 if (is_type_compound(type_left)
895 && types_compatible(type_left, type_right)) {
896 *right = create_implicit_cast(*right, type_left);
900 incompatible_assign_types:
901 /* TODO: improve error message */
902 parser_print_error_prefix();
903 fprintf(stderr, "incompatible types in %s\n", context);
904 parser_print_error_prefix();
905 print_type_quoted(orig_type_left);
906 fputs(" <- ", stderr);
907 print_type_quoted(orig_type_right);
911 static expression_t *parse_constant_expression(void)
913 /* start parsing at precedence 7 (conditional expression) */
914 return parse_sub_expression(7);
917 static expression_t *parse_assignment_expression(void)
919 /* start parsing at precedence 2 (assignment expression) */
920 return parse_sub_expression(2);
923 typedef struct declaration_specifiers_t declaration_specifiers_t;
924 struct declaration_specifiers_t {
925 unsigned char storage_class;
930 static void parse_compound_type_entries(void);
931 static declaration_t *parse_declarator(
932 const declaration_specifiers_t *specifiers, type_t *type,
933 bool may_be_abstract);
934 static declaration_t *record_declaration(declaration_t *declaration);
936 static type_t *make_global_typedef(const char *name, type_t *type)
938 symbol_t *symbol = symbol_table_insert(name);
940 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
941 declaration->namespc = NAMESPACE_NORMAL;
942 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
943 declaration->type = type;
944 declaration->symbol = symbol;
945 declaration->source_position = builtin_source_position;
947 record_declaration(declaration);
949 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
950 typedef_type->typedeft.declaration = declaration;
955 static const char *parse_string_literals(void)
957 assert(token.type == T_STRING_LITERAL);
958 const char *result = token.v.string;
962 while(token.type == T_STRING_LITERAL) {
963 result = concat_strings(result, token.v.string);
970 static void parse_attributes(void)
974 case T___attribute__: {
982 parse_error("EOF while parsing attribute");
1001 if(token.type != T_STRING_LITERAL) {
1002 parse_error_expected("while parsing assembler attribute",
1007 parse_string_literals();
1012 goto attributes_finished;
1016 attributes_finished:
1021 static designator_t *parse_designation(void)
1023 if(token.type != '[' && token.type != '.')
1026 designator_t *result = NULL;
1027 designator_t *last = NULL;
1030 designator_t *designator;
1031 switch(token.type) {
1033 designator = allocate_ast_zero(sizeof(designator[0]));
1035 designator->array_access = parse_constant_expression();
1039 designator = allocate_ast_zero(sizeof(designator[0]));
1041 if(token.type != T_IDENTIFIER) {
1042 parse_error_expected("while parsing designator",
1046 designator->symbol = token.v.symbol;
1054 assert(designator != NULL);
1056 last->next = designator;
1058 result = designator;
1065 static initializer_t *initializer_from_string(array_type_t *type,
1068 /* TODO: check len vs. size of array type */
1071 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1072 initializer->string.string = string;
1077 static initializer_t *initializer_from_expression(type_t *type,
1078 expression_t *expression)
1080 /* TODO check that expression is a constant expression */
1082 /* § 6.7.8.14/15 char array may be initialized by string literals */
1083 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1084 array_type_t *array_type = &type->array;
1085 type_t *element_type = array_type->element_type;
1087 if(element_type->type == TYPE_ATOMIC) {
1088 atomic_type_t *atomic_type = &element_type->atomic;
1089 atomic_type_type_t atype = atomic_type->atype;
1091 /* TODO handle wide strings */
1092 if(atype == ATOMIC_TYPE_CHAR
1093 || atype == ATOMIC_TYPE_SCHAR
1094 || atype == ATOMIC_TYPE_UCHAR) {
1096 string_literal_expression_t *literal = &expression->string;
1097 return initializer_from_string(array_type, literal->value);
1102 semantic_assign(type, &expression, "initializer");
1104 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1105 result->value.value = expression;
1110 static initializer_t *parse_sub_initializer(type_t *type,
1111 expression_t *expression,
1112 type_t *expression_type);
1114 static initializer_t *parse_sub_initializer_elem(type_t *type)
1116 if(token.type == '{') {
1117 return parse_sub_initializer(type, NULL, NULL);
1120 expression_t *expression = parse_assignment_expression();
1121 type_t *expression_type = skip_typeref(expression->base.datatype);
1123 return parse_sub_initializer(type, expression, expression_type);
1126 static bool had_initializer_brace_warning;
1128 static initializer_t *parse_sub_initializer(type_t *type,
1129 expression_t *expression,
1130 type_t *expression_type)
1132 if(is_type_scalar(type)) {
1133 /* there might be extra {} hierarchies */
1134 if(token.type == '{') {
1136 if(!had_initializer_brace_warning) {
1137 parse_warning("braces around scalar initializer");
1138 had_initializer_brace_warning = true;
1140 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1141 if(token.type == ',') {
1143 /* TODO: warn about excessive elements */
1149 if(expression == NULL) {
1150 expression = parse_assignment_expression();
1152 return initializer_from_expression(type, expression);
1155 /* TODO: ignore qualifiers, comparing pointers is probably
1157 if(expression != NULL && expression_type == type) {
1158 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1161 semantic_assign(type, &expression, "initializer");
1163 result->value.value = expression;
1168 bool read_paren = false;
1169 if(token.type == '{') {
1174 /* descend into subtype */
1175 initializer_t *result = NULL;
1176 initializer_t **elems;
1177 if(type->type == TYPE_ARRAY) {
1178 array_type_t *array_type = &type->array;
1179 type_t *element_type = array_type->element_type;
1180 element_type = skip_typeref(element_type);
1183 had_initializer_brace_warning = false;
1184 if(expression == NULL) {
1185 sub = parse_sub_initializer_elem(element_type);
1187 sub = parse_sub_initializer(element_type, expression,
1191 /* didn't match the subtypes -> try the parent type */
1193 assert(!read_paren);
1197 elems = NEW_ARR_F(initializer_t*, 0);
1198 ARR_APP1(initializer_t*, elems, sub);
1201 if(token.type == '}')
1204 if(token.type == '}')
1207 sub = parse_sub_initializer(element_type, NULL, NULL);
1209 /* TODO error, do nicer cleanup */
1210 parse_error("member initializer didn't match");
1214 ARR_APP1(initializer_t*, elems, sub);
1217 assert(type->type == TYPE_COMPOUND_STRUCT
1218 || type->type == TYPE_COMPOUND_UNION);
1219 compound_type_t *compound_type = &type->compound;
1220 context_t *context = & compound_type->declaration->context;
1222 declaration_t *first = context->declarations;
1225 type_t *first_type = first->type;
1226 first_type = skip_typeref(first_type);
1229 had_initializer_brace_warning = false;
1230 if(expression == NULL) {
1231 sub = parse_sub_initializer_elem(first_type);
1233 sub = parse_sub_initializer(first_type, expression,expression_type);
1236 /* didn't match the subtypes -> try our parent type */
1238 assert(!read_paren);
1242 elems = NEW_ARR_F(initializer_t*, 0);
1243 ARR_APP1(initializer_t*, elems, sub);
1245 declaration_t *iter = first->next;
1246 for( ; iter != NULL; iter = iter->next) {
1247 if(iter->symbol == NULL)
1249 if(iter->namespc != NAMESPACE_NORMAL)
1252 if(token.type == '}')
1255 if(token.type == '}')
1258 type_t *iter_type = iter->type;
1259 iter_type = skip_typeref(iter_type);
1261 sub = parse_sub_initializer(iter_type, NULL, NULL);
1263 /* TODO error, do nicer cleanup*/
1264 parse_error("member initializer didn't match");
1268 ARR_APP1(initializer_t*, elems, sub);
1272 int len = ARR_LEN(elems);
1273 size_t elems_size = sizeof(initializer_t*) * len;
1275 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1277 init->initializer.type = INITIALIZER_LIST;
1279 memcpy(init->initializers, elems, elems_size);
1282 result = (initializer_t*) init;
1285 if(token.type == ',')
1292 static initializer_t *parse_initializer(type_t *type)
1294 initializer_t *result;
1296 type = skip_typeref(type);
1298 if(token.type != '{') {
1299 expression_t *expression = parse_assignment_expression();
1300 return initializer_from_expression(type, expression);
1303 if(is_type_scalar(type)) {
1307 expression_t *expression = parse_assignment_expression();
1308 result = initializer_from_expression(type, expression);
1310 if(token.type == ',')
1316 result = parse_sub_initializer(type, NULL, NULL);
1324 static declaration_t *parse_compound_type_specifier(bool is_struct)
1332 symbol_t *symbol = NULL;
1333 declaration_t *declaration = NULL;
1335 if (token.type == T___attribute__) {
1340 if(token.type == T_IDENTIFIER) {
1341 symbol = token.v.symbol;
1345 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1347 declaration = get_declaration(symbol, NAMESPACE_UNION);
1349 } else if(token.type != '{') {
1351 parse_error_expected("while parsing struct type specifier",
1352 T_IDENTIFIER, '{', 0);
1354 parse_error_expected("while parsing union type specifier",
1355 T_IDENTIFIER, '{', 0);
1361 if(declaration == NULL) {
1362 declaration = allocate_ast_zero(sizeof(declaration[0]));
1365 declaration->namespc = NAMESPACE_STRUCT;
1367 declaration->namespc = NAMESPACE_UNION;
1369 declaration->source_position = token.source_position;
1370 declaration->symbol = symbol;
1371 record_declaration(declaration);
1374 if(token.type == '{') {
1375 if(declaration->init.is_defined) {
1376 assert(symbol != NULL);
1377 parser_print_error_prefix();
1378 fprintf(stderr, "multiple definition of %s %s\n",
1379 is_struct ? "struct" : "union", symbol->string);
1380 declaration->context.declarations = NULL;
1382 declaration->init.is_defined = true;
1384 int top = environment_top();
1385 context_t *last_context = context;
1386 set_context(& declaration->context);
1388 parse_compound_type_entries();
1391 assert(context == & declaration->context);
1392 set_context(last_context);
1393 environment_pop_to(top);
1399 static void parse_enum_entries(enum_type_t *const enum_type)
1403 if(token.type == '}') {
1405 parse_error("empty enum not allowed");
1410 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1412 if(token.type != T_IDENTIFIER) {
1413 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1417 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1418 entry->type = (type_t*) enum_type;
1419 entry->symbol = token.v.symbol;
1420 entry->source_position = token.source_position;
1423 if(token.type == '=') {
1425 entry->init.enum_value = parse_constant_expression();
1430 record_declaration(entry);
1432 if(token.type != ',')
1435 } while(token.type != '}');
1440 static type_t *parse_enum_specifier(void)
1444 declaration_t *declaration;
1447 if(token.type == T_IDENTIFIER) {
1448 symbol = token.v.symbol;
1451 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1452 } else if(token.type != '{') {
1453 parse_error_expected("while parsing enum type specifier",
1454 T_IDENTIFIER, '{', 0);
1461 if(declaration == NULL) {
1462 declaration = allocate_ast_zero(sizeof(declaration[0]));
1464 declaration->namespc = NAMESPACE_ENUM;
1465 declaration->source_position = token.source_position;
1466 declaration->symbol = symbol;
1469 type_t *const type = allocate_type_zero(TYPE_ENUM);
1470 type->enumt.declaration = declaration;
1472 if(token.type == '{') {
1473 if(declaration->init.is_defined) {
1474 parser_print_error_prefix();
1475 fprintf(stderr, "multiple definitions of enum %s\n",
1478 record_declaration(declaration);
1479 declaration->init.is_defined = 1;
1481 parse_enum_entries(&type->enumt);
1489 * if a symbol is a typedef to another type, return true
1491 static bool is_typedef_symbol(symbol_t *symbol)
1493 const declaration_t *const declaration =
1494 get_declaration(symbol, NAMESPACE_NORMAL);
1496 declaration != NULL &&
1497 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1500 static type_t *parse_typeof(void)
1508 expression_t *expression = NULL;
1511 switch(token.type) {
1512 case T___extension__:
1513 /* this can be a prefix to a typename or an expression */
1514 /* we simply eat it now. */
1517 } while(token.type == T___extension__);
1521 if(is_typedef_symbol(token.v.symbol)) {
1522 type = parse_typename();
1524 expression = parse_expression();
1525 type = expression->base.datatype;
1530 type = parse_typename();
1534 expression = parse_expression();
1535 type = expression->base.datatype;
1541 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1542 typeof_type->typeoft.expression = expression;
1543 typeof_type->typeoft.typeof_type = type;
1549 SPECIFIER_SIGNED = 1 << 0,
1550 SPECIFIER_UNSIGNED = 1 << 1,
1551 SPECIFIER_LONG = 1 << 2,
1552 SPECIFIER_INT = 1 << 3,
1553 SPECIFIER_DOUBLE = 1 << 4,
1554 SPECIFIER_CHAR = 1 << 5,
1555 SPECIFIER_SHORT = 1 << 6,
1556 SPECIFIER_LONG_LONG = 1 << 7,
1557 SPECIFIER_FLOAT = 1 << 8,
1558 SPECIFIER_BOOL = 1 << 9,
1559 SPECIFIER_VOID = 1 << 10,
1560 #ifdef PROVIDE_COMPLEX
1561 SPECIFIER_COMPLEX = 1 << 11,
1562 SPECIFIER_IMAGINARY = 1 << 12,
1566 static type_t *create_builtin_type(symbol_t *symbol)
1568 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1569 type->builtin.symbol = symbol;
1571 type->builtin.real_type = type_int;
1576 static type_t *get_typedef_type(symbol_t *symbol)
1578 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1579 if(declaration == NULL
1580 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1583 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1584 type->typedeft.declaration = declaration;
1589 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1591 type_t *type = NULL;
1592 unsigned type_qualifiers = 0;
1593 unsigned type_specifiers = 0;
1597 switch(token.type) {
1600 #define MATCH_STORAGE_CLASS(token, class) \
1602 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1603 parse_error("multiple storage classes in declaration " \
1606 specifiers->storage_class = class; \
1610 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1611 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1612 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1613 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1614 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1617 switch (specifiers->storage_class) {
1618 case STORAGE_CLASS_NONE:
1619 specifiers->storage_class = STORAGE_CLASS_THREAD;
1622 case STORAGE_CLASS_EXTERN:
1623 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1626 case STORAGE_CLASS_STATIC:
1627 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1631 parse_error("multiple storage classes in declaration specifiers");
1637 /* type qualifiers */
1638 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1640 type_qualifiers |= qualifier; \
1644 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1645 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1646 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1648 case T___extension__:
1653 /* type specifiers */
1654 #define MATCH_SPECIFIER(token, specifier, name) \
1657 if(type_specifiers & specifier) { \
1658 parse_error("multiple " name " type specifiers given"); \
1660 type_specifiers |= specifier; \
1664 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1665 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1666 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1667 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1668 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1669 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1670 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1671 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1672 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1673 #ifdef PROVIDE_COMPLEX
1674 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1675 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1679 specifiers->is_inline = true;
1684 if(type_specifiers & SPECIFIER_LONG_LONG) {
1685 parse_error("multiple type specifiers given");
1686 } else if(type_specifiers & SPECIFIER_LONG) {
1687 type_specifiers |= SPECIFIER_LONG_LONG;
1689 type_specifiers |= SPECIFIER_LONG;
1693 /* TODO: if type != NULL for the following rules should issue
1696 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1698 type->compound.declaration = parse_compound_type_specifier(true);
1702 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1704 type->compound.declaration = parse_compound_type_specifier(false);
1708 type = parse_enum_specifier();
1711 type = parse_typeof();
1713 case T___builtin_va_list:
1714 type = create_builtin_type(token.v.symbol);
1718 case T___attribute__:
1723 case T_IDENTIFIER: {
1724 type_t *typedef_type = get_typedef_type(token.v.symbol);
1726 if(typedef_type == NULL)
1727 goto finish_specifiers;
1730 type = typedef_type;
1734 /* function specifier */
1736 goto finish_specifiers;
1743 atomic_type_type_t atomic_type;
1745 /* match valid basic types */
1746 switch(type_specifiers) {
1747 case SPECIFIER_VOID:
1748 atomic_type = ATOMIC_TYPE_VOID;
1750 case SPECIFIER_CHAR:
1751 atomic_type = ATOMIC_TYPE_CHAR;
1753 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1754 atomic_type = ATOMIC_TYPE_SCHAR;
1756 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1757 atomic_type = ATOMIC_TYPE_UCHAR;
1759 case SPECIFIER_SHORT:
1760 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1761 case SPECIFIER_SHORT | SPECIFIER_INT:
1762 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1763 atomic_type = ATOMIC_TYPE_SHORT;
1765 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1766 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1767 atomic_type = ATOMIC_TYPE_USHORT;
1770 case SPECIFIER_SIGNED:
1771 case SPECIFIER_SIGNED | SPECIFIER_INT:
1772 atomic_type = ATOMIC_TYPE_INT;
1774 case SPECIFIER_UNSIGNED:
1775 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1776 atomic_type = ATOMIC_TYPE_UINT;
1778 case SPECIFIER_LONG:
1779 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1780 case SPECIFIER_LONG | SPECIFIER_INT:
1781 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1782 atomic_type = ATOMIC_TYPE_LONG;
1784 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1785 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1786 atomic_type = ATOMIC_TYPE_ULONG;
1788 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1789 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1790 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1791 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1793 atomic_type = ATOMIC_TYPE_LONGLONG;
1795 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1796 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1798 atomic_type = ATOMIC_TYPE_ULONGLONG;
1800 case SPECIFIER_FLOAT:
1801 atomic_type = ATOMIC_TYPE_FLOAT;
1803 case SPECIFIER_DOUBLE:
1804 atomic_type = ATOMIC_TYPE_DOUBLE;
1806 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1807 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1809 case SPECIFIER_BOOL:
1810 atomic_type = ATOMIC_TYPE_BOOL;
1812 #ifdef PROVIDE_COMPLEX
1813 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1814 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1816 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1817 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1819 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1820 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1822 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1823 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1825 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1826 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1828 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1829 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1833 /* invalid specifier combination, give an error message */
1834 if(type_specifiers == 0) {
1836 parse_warning("no type specifiers in declaration (using int)");
1837 atomic_type = ATOMIC_TYPE_INT;
1840 parse_error("no type specifiers given in declaration");
1842 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1843 (type_specifiers & SPECIFIER_UNSIGNED)) {
1844 parse_error("signed and unsigned specifiers gives");
1845 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1846 parse_error("only integer types can be signed or unsigned");
1848 parse_error("multiple datatypes in declaration");
1850 atomic_type = ATOMIC_TYPE_INVALID;
1853 type = allocate_type_zero(TYPE_ATOMIC);
1854 type->atomic.atype = atomic_type;
1857 if(type_specifiers != 0) {
1858 parse_error("multiple datatypes in declaration");
1862 type->base.qualifiers = type_qualifiers;
1864 type_t *result = typehash_insert(type);
1865 if(newtype && result != type) {
1869 specifiers->type = result;
1872 static type_qualifiers_t parse_type_qualifiers(void)
1874 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1877 switch(token.type) {
1878 /* type qualifiers */
1879 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1880 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1881 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1884 return type_qualifiers;
1889 static void parse_identifier_list(void)
1892 if(token.type != T_IDENTIFIER) {
1893 parse_error_expected("while parsing parameter identifier list",
1897 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1898 declaration->symbol = token.v.symbol;
1902 if(token.type != ',')
1908 static declaration_t *parse_parameter(void)
1910 declaration_specifiers_t specifiers;
1911 memset(&specifiers, 0, sizeof(specifiers));
1913 parse_declaration_specifiers(&specifiers);
1915 declaration_t *declaration
1916 = parse_declarator(&specifiers, specifiers.type, true);
1918 /* TODO check declaration constraints for parameters */
1919 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1920 parse_error("typedef not allowed in parameter list");
1923 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1925 if (declaration->type->type == TYPE_ARRAY) {
1926 const array_type_t *const arr_type = &declaration->type->array;
1927 type_t *element_type = arr_type->element_type;
1928 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1934 static declaration_t *parse_parameters(function_type_t *type)
1936 if(token.type == T_IDENTIFIER) {
1937 symbol_t *symbol = token.v.symbol;
1938 if(!is_typedef_symbol(symbol)) {
1939 /* TODO: K&R style C parameters */
1940 parse_identifier_list();
1945 if(token.type == ')') {
1946 type->unspecified_parameters = 1;
1949 if(token.type == T_void && look_ahead(1)->type == ')') {
1954 declaration_t *declarations = NULL;
1955 declaration_t *declaration;
1956 declaration_t *last_declaration = NULL;
1957 function_parameter_t *parameter;
1958 function_parameter_t *last_parameter = NULL;
1961 switch(token.type) {
1965 return declarations;
1968 case T___extension__:
1970 declaration = parse_parameter();
1972 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1973 memset(parameter, 0, sizeof(parameter[0]));
1974 parameter->type = declaration->type;
1976 if(last_parameter != NULL) {
1977 last_declaration->next = declaration;
1978 last_parameter->next = parameter;
1980 type->parameters = parameter;
1981 declarations = declaration;
1983 last_parameter = parameter;
1984 last_declaration = declaration;
1988 return declarations;
1990 if(token.type != ',')
1991 return declarations;
2001 } construct_type_type_t;
2003 typedef struct construct_type_t construct_type_t;
2004 struct construct_type_t {
2005 construct_type_type_t type;
2006 construct_type_t *next;
2009 typedef struct parsed_pointer_t parsed_pointer_t;
2010 struct parsed_pointer_t {
2011 construct_type_t construct_type;
2012 type_qualifiers_t type_qualifiers;
2015 typedef struct construct_function_type_t construct_function_type_t;
2016 struct construct_function_type_t {
2017 construct_type_t construct_type;
2018 type_t *function_type;
2021 typedef struct parsed_array_t parsed_array_t;
2022 struct parsed_array_t {
2023 construct_type_t construct_type;
2024 type_qualifiers_t type_qualifiers;
2030 typedef struct construct_base_type_t construct_base_type_t;
2031 struct construct_base_type_t {
2032 construct_type_t construct_type;
2036 static construct_type_t *parse_pointer_declarator(void)
2040 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2041 memset(pointer, 0, sizeof(pointer[0]));
2042 pointer->construct_type.type = CONSTRUCT_POINTER;
2043 pointer->type_qualifiers = parse_type_qualifiers();
2045 return (construct_type_t*) pointer;
2048 static construct_type_t *parse_array_declarator(void)
2052 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2053 memset(array, 0, sizeof(array[0]));
2054 array->construct_type.type = CONSTRUCT_ARRAY;
2056 if(token.type == T_static) {
2057 array->is_static = true;
2061 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2062 if(type_qualifiers != 0) {
2063 if(token.type == T_static) {
2064 array->is_static = true;
2068 array->type_qualifiers = type_qualifiers;
2070 if(token.type == '*' && look_ahead(1)->type == ']') {
2071 array->is_variable = true;
2073 } else if(token.type != ']') {
2074 array->size = parse_assignment_expression();
2079 return (construct_type_t*) array;
2082 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2086 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2088 declaration_t *parameters = parse_parameters(&type->function);
2089 if(declaration != NULL) {
2090 declaration->context.declarations = parameters;
2093 construct_function_type_t *construct_function_type =
2094 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2095 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2096 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2097 construct_function_type->function_type = type;
2101 return (construct_type_t*) construct_function_type;
2104 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2105 bool may_be_abstract)
2107 /* construct a single linked list of construct_type_t's which describe
2108 * how to construct the final declarator type */
2109 construct_type_t *first = NULL;
2110 construct_type_t *last = NULL;
2113 while(token.type == '*') {
2114 construct_type_t *type = parse_pointer_declarator();
2125 /* TODO: find out if this is correct */
2128 construct_type_t *inner_types = NULL;
2130 switch(token.type) {
2132 if(declaration == NULL) {
2133 parse_error("no identifier expected in typename");
2135 declaration->symbol = token.v.symbol;
2136 declaration->source_position = token.source_position;
2142 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2148 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2149 /* avoid a loop in the outermost scope, because eat_statement doesn't
2151 if(token.type == '}' && current_function == NULL) {
2159 construct_type_t *p = last;
2162 construct_type_t *type;
2163 switch(token.type) {
2165 type = parse_function_declarator(declaration);
2168 type = parse_array_declarator();
2171 goto declarator_finished;
2174 /* insert in the middle of the list (behind p) */
2176 type->next = p->next;
2187 declarator_finished:
2190 /* append inner_types at the end of the list, we don't to set last anymore
2191 * as it's not needed anymore */
2193 assert(first == NULL);
2194 first = inner_types;
2196 last->next = inner_types;
2202 static type_t *construct_declarator_type(construct_type_t *construct_list,
2205 construct_type_t *iter = construct_list;
2206 for( ; iter != NULL; iter = iter->next) {
2207 switch(iter->type) {
2208 case CONSTRUCT_INVALID:
2209 panic("invalid type construction found");
2210 case CONSTRUCT_FUNCTION: {
2211 construct_function_type_t *construct_function_type
2212 = (construct_function_type_t*) iter;
2214 type_t *function_type = construct_function_type->function_type;
2216 function_type->function.result_type = type;
2218 type = function_type;
2222 case CONSTRUCT_POINTER: {
2223 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2224 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2225 pointer_type->pointer.points_to = type;
2226 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2228 type = pointer_type;
2232 case CONSTRUCT_ARRAY: {
2233 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2234 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2236 array_type->base.qualifiers = parsed_array->type_qualifiers;
2237 array_type->array.element_type = type;
2238 array_type->array.is_static = parsed_array->is_static;
2239 array_type->array.is_variable = parsed_array->is_variable;
2240 array_type->array.size = parsed_array->size;
2247 type_t *hashed_type = typehash_insert(type);
2248 if(hashed_type != type) {
2249 /* the function type was constructed earlier freeing it here will
2250 * destroy other types... */
2251 if(iter->type != CONSTRUCT_FUNCTION) {
2261 static declaration_t *parse_declarator(
2262 const declaration_specifiers_t *specifiers,
2263 type_t *type, bool may_be_abstract)
2265 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2266 declaration->storage_class = specifiers->storage_class;
2267 declaration->is_inline = specifiers->is_inline;
2269 construct_type_t *construct_type
2270 = parse_inner_declarator(declaration, may_be_abstract);
2271 declaration->type = construct_declarator_type(construct_type, type);
2273 if(construct_type != NULL) {
2274 obstack_free(&temp_obst, construct_type);
2280 static type_t *parse_abstract_declarator(type_t *base_type)
2282 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2284 type_t *result = construct_declarator_type(construct_type, base_type);
2285 if(construct_type != NULL) {
2286 obstack_free(&temp_obst, construct_type);
2292 static declaration_t *record_declaration(declaration_t *declaration)
2294 assert(context != NULL);
2296 symbol_t *symbol = declaration->symbol;
2297 if(symbol != NULL) {
2298 declaration_t *alias = environment_push(declaration);
2299 if(alias != declaration)
2302 declaration->parent_context = context;
2305 if(last_declaration != NULL) {
2306 last_declaration->next = declaration;
2308 context->declarations = declaration;
2310 last_declaration = declaration;
2315 static void parser_error_multiple_definition(declaration_t *previous,
2316 declaration_t *declaration)
2318 parser_print_error_prefix_pos(declaration->source_position);
2319 fprintf(stderr, "multiple definition of symbol '%s'\n",
2320 declaration->symbol->string);
2321 parser_print_error_prefix_pos(previous->source_position);
2322 fprintf(stderr, "this is the location of the previous definition.\n");
2325 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2328 declaration_t *ndeclaration
2329 = parse_declarator(specifiers, specifiers->type, false);
2331 declaration_t *declaration = record_declaration(ndeclaration);
2333 type_t *orig_type = declaration->type;
2334 type_t *type = skip_typeref(orig_type);
2335 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2336 parser_print_warning_prefix_pos(declaration->source_position);
2337 fprintf(stderr, "variable '%s' declared 'inline'\n",
2338 declaration->symbol->string);
2341 if(token.type == '=') {
2344 /* TODO: check that this is an allowed type (no function type) */
2346 if(declaration->init.initializer != NULL) {
2347 parser_error_multiple_definition(declaration, ndeclaration);
2350 initializer_t *initializer = parse_initializer(type);
2352 if(type->type == TYPE_ARRAY && initializer != NULL) {
2353 array_type_t *array_type = &type->array;
2355 if(array_type->size == NULL) {
2356 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2358 cnst->base.datatype = type_size_t;
2360 if(initializer->type == INITIALIZER_LIST) {
2361 initializer_list_t *list = &initializer->list;
2362 cnst->conste.v.int_value = list->len;
2364 assert(initializer->type == INITIALIZER_STRING);
2365 initializer_string_t *string = &initializer->string;
2366 cnst->conste.v.int_value = strlen(string->string) + 1;
2369 array_type->size = cnst;
2374 ndeclaration->init.initializer = initializer;
2375 } else if(token.type == '{') {
2376 if(type->type != TYPE_FUNCTION) {
2377 parser_print_error_prefix();
2378 fprintf(stderr, "declarator '");
2379 print_type_ext(orig_type, declaration->symbol, NULL);
2380 fprintf(stderr, "' has a body but is not a function type.\n");
2384 function_type_t *function_type = &type->function;
2385 /* § 6.7.5.3 (14) a function definition with () means no
2387 if(function_type->unspecified_parameters) {
2388 type_t *duplicate = duplicate_type(type);
2389 duplicate->function.unspecified_parameters = false;
2391 type = typehash_insert(duplicate);
2392 if(type != duplicate) {
2393 //obstack_free(type_obst, duplicate);
2395 function_type = &type->function;
2398 if(declaration->init.statement != NULL) {
2399 parser_error_multiple_definition(declaration, ndeclaration);
2401 if(ndeclaration != declaration) {
2402 memcpy(&declaration->context, &ndeclaration->context,
2403 sizeof(declaration->context));
2406 int top = environment_top();
2407 context_t *last_context = context;
2408 set_context(&declaration->context);
2410 /* push function parameters */
2411 declaration_t *parameter = declaration->context.declarations;
2412 for( ; parameter != NULL; parameter = parameter->next) {
2413 environment_push(parameter);
2416 int label_stack_top = label_top();
2417 declaration_t *old_current_function = current_function;
2418 current_function = declaration;
2420 statement_t *statement = parse_compound_statement();
2422 assert(current_function == declaration);
2423 current_function = old_current_function;
2424 label_pop_to(label_stack_top);
2426 assert(context == &declaration->context);
2427 set_context(last_context);
2428 environment_pop_to(top);
2430 declaration->init.statement = statement;
2434 if(token.type != ',')
2441 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2444 if(token.type == ':') {
2446 parse_constant_expression();
2447 /* TODO (bitfields) */
2449 declaration_t *declaration
2450 = parse_declarator(specifiers, specifiers->type, true);
2452 /* TODO: check constraints for struct declarations */
2453 /* TODO: check for doubled fields */
2454 record_declaration(declaration);
2456 if(token.type == ':') {
2458 parse_constant_expression();
2459 /* TODO (bitfields) */
2463 if(token.type != ',')
2470 static void parse_compound_type_entries(void)
2474 while(token.type != '}' && token.type != T_EOF) {
2475 declaration_specifiers_t specifiers;
2476 memset(&specifiers, 0, sizeof(specifiers));
2477 parse_declaration_specifiers(&specifiers);
2479 parse_struct_declarators(&specifiers);
2481 if(token.type == T_EOF) {
2482 parse_error("unexpected error while parsing struct");
2487 static void parse_declaration(void)
2489 source_position_t source_position = token.source_position;
2491 declaration_specifiers_t specifiers;
2492 memset(&specifiers, 0, sizeof(specifiers));
2493 parse_declaration_specifiers(&specifiers);
2495 if(token.type == ';') {
2496 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2497 parse_warning_pos(source_position,
2498 "useless keyword in empty declaration");
2500 switch (specifiers.type->type) {
2501 case TYPE_COMPOUND_STRUCT:
2502 case TYPE_COMPOUND_UNION: {
2503 const compound_type_t *const comp_type
2504 = &specifiers.type->compound;
2505 if (comp_type->declaration->symbol == NULL) {
2506 parse_warning_pos(source_position,
2507 "unnamed struct/union that defines no instances");
2512 case TYPE_ENUM: break;
2515 parse_warning_pos(source_position, "empty declaration");
2521 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2523 declaration->type = specifiers.type;
2524 declaration->storage_class = specifiers.storage_class;
2525 declaration->source_position = source_position;
2526 record_declaration(declaration);
2529 parse_init_declarators(&specifiers);
2532 static type_t *parse_typename(void)
2534 declaration_specifiers_t specifiers;
2535 memset(&specifiers, 0, sizeof(specifiers));
2536 parse_declaration_specifiers(&specifiers);
2537 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2538 /* TODO: improve error message, user does probably not know what a
2539 * storage class is...
2541 parse_error("typename may not have a storage class");
2544 type_t *result = parse_abstract_declarator(specifiers.type);
2552 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2553 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2554 expression_t *left);
2556 typedef struct expression_parser_function_t expression_parser_function_t;
2557 struct expression_parser_function_t {
2558 unsigned precedence;
2559 parse_expression_function parser;
2560 unsigned infix_precedence;
2561 parse_expression_infix_function infix_parser;
2564 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2566 static expression_t *make_invalid_expression(void)
2568 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2569 expression->base.source_position = token.source_position;
2573 static expression_t *expected_expression_error(void)
2575 parser_print_error_prefix();
2576 fprintf(stderr, "expected expression, got token ");
2577 print_token(stderr, & token);
2578 fprintf(stderr, "\n");
2582 return make_invalid_expression();
2585 static expression_t *parse_string_const(void)
2587 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2588 cnst->base.datatype = type_string;
2589 cnst->string.value = parse_string_literals();
2594 static expression_t *parse_int_const(void)
2596 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2597 cnst->base.datatype = token.datatype;
2598 cnst->conste.v.int_value = token.v.intvalue;
2605 static expression_t *parse_float_const(void)
2607 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2608 cnst->base.datatype = token.datatype;
2609 cnst->conste.v.float_value = token.v.floatvalue;
2616 static declaration_t *create_implicit_function(symbol_t *symbol,
2617 const source_position_t source_position)
2619 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2620 ntype->function.result_type = type_int;
2621 ntype->function.unspecified_parameters = true;
2623 type_t *type = typehash_insert(ntype);
2628 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2630 declaration->storage_class = STORAGE_CLASS_EXTERN;
2631 declaration->type = type;
2632 declaration->symbol = symbol;
2633 declaration->source_position = source_position;
2635 /* prepend the implicit definition to the global context
2636 * this is safe since the symbol wasn't declared as anything else yet
2638 assert(symbol->declaration == NULL);
2640 context_t *last_context = context;
2641 context = global_context;
2643 environment_push(declaration);
2644 declaration->next = context->declarations;
2645 context->declarations = declaration;
2647 context = last_context;
2652 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2654 function_parameter_t *parameter
2655 = obstack_alloc(type_obst, sizeof(parameter[0]));
2656 memset(parameter, 0, sizeof(parameter[0]));
2657 parameter->type = argument_type;
2659 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2660 type->function.result_type = result_type;
2661 type->function.parameters = parameter;
2663 type_t *result = typehash_insert(type);
2664 if(result != type) {
2671 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2673 switch(symbol->ID) {
2674 case T___builtin_alloca:
2675 return make_function_1_type(type_void_ptr, type_size_t);
2676 case T___builtin_nan:
2677 return make_function_1_type(type_double, type_string);
2678 case T___builtin_nanf:
2679 return make_function_1_type(type_float, type_string);
2680 case T___builtin_nand:
2681 return make_function_1_type(type_long_double, type_string);
2683 panic("not implemented builtin symbol found");
2688 * performs automatic type cast as described in § 6.3.2.1
2690 static type_t *automatic_type_conversion(type_t *type)
2695 if(type->type == TYPE_ARRAY) {
2696 array_type_t *array_type = &type->array;
2697 type_t *element_type = array_type->element_type;
2698 unsigned qualifiers = array_type->type.qualifiers;
2700 return make_pointer_type(element_type, qualifiers);
2703 if(type->type == TYPE_FUNCTION) {
2704 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2711 * reverts the automatic casts of array to pointer types and function
2712 * to function-pointer types as defined § 6.3.2.1
2714 type_t *revert_automatic_type_conversion(const expression_t *expression)
2716 if(expression->base.datatype == NULL)
2719 switch(expression->type) {
2720 case EXPR_REFERENCE: {
2721 const reference_expression_t *ref = &expression->reference;
2722 return ref->declaration->type;
2725 const select_expression_t *select = &expression->select;
2726 return select->compound_entry->type;
2729 const unary_expression_t *unary = &expression->unary;
2730 if(unary->type == UNEXPR_DEREFERENCE) {
2731 expression_t *value = unary->value;
2732 type_t *type = skip_typeref(value->base.datatype);
2733 pointer_type_t *pointer_type = &type->pointer;
2735 return pointer_type->points_to;
2739 case EXPR_BUILTIN_SYMBOL: {
2740 const builtin_symbol_expression_t *builtin
2741 = &expression->builtin_symbol;
2742 return get_builtin_symbol_type(builtin->symbol);
2744 case EXPR_ARRAY_ACCESS: {
2745 const array_access_expression_t *array_access
2746 = &expression->array_access;
2747 const expression_t *array_ref = array_access->array_ref;
2748 type_t *type_left = skip_typeref(array_ref->base.datatype);
2749 assert(is_type_pointer(type_left));
2750 pointer_type_t *pointer_type = &type_left->pointer;
2751 return pointer_type->points_to;
2758 return expression->base.datatype;
2761 static expression_t *parse_reference(void)
2763 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2765 reference_expression_t *ref = &expression->reference;
2766 ref->symbol = token.v.symbol;
2768 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2770 source_position_t source_position = token.source_position;
2773 if(declaration == NULL) {
2775 /* an implicitly defined function */
2776 if(token.type == '(') {
2777 parser_print_prefix_pos(token.source_position);
2778 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2779 ref->symbol->string);
2781 declaration = create_implicit_function(ref->symbol,
2786 parser_print_error_prefix();
2787 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2792 type_t *type = declaration->type;
2793 /* we always do the auto-type conversions; the & and sizeof parser contains
2794 * code to revert this! */
2795 type = automatic_type_conversion(type);
2797 ref->declaration = declaration;
2798 ref->expression.datatype = type;
2803 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2807 /* TODO check if explicit cast is allowed and issue warnings/errors */
2810 static expression_t *parse_cast(void)
2812 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
2814 cast->unary.type = UNEXPR_CAST;
2815 cast->base.source_position = token.source_position;
2817 type_t *type = parse_typename();
2820 expression_t *value = parse_sub_expression(20);
2822 check_cast_allowed(value, type);
2824 cast->base.datatype = type;
2825 cast->unary.value = value;
2830 static expression_t *parse_statement_expression(void)
2832 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2834 statement_t *statement = parse_compound_statement();
2835 expression->statement.statement = statement;
2836 if(statement == NULL) {
2841 assert(statement->type == STATEMENT_COMPOUND);
2842 compound_statement_t *compound_statement = &statement->compound;
2844 /* find last statement and use it's type */
2845 const statement_t *last_statement = NULL;
2846 const statement_t *iter = compound_statement->statements;
2847 for( ; iter != NULL; iter = iter->base.next) {
2848 last_statement = iter;
2851 if(last_statement->type == STATEMENT_EXPRESSION) {
2852 const expression_statement_t *expression_statement
2853 = &last_statement->expression;
2854 expression->base.datatype
2855 = expression_statement->expression->base.datatype;
2857 expression->base.datatype = type_void;
2865 static expression_t *parse_brace_expression(void)
2869 switch(token.type) {
2871 /* gcc extension: a stement expression */
2872 return parse_statement_expression();
2876 return parse_cast();
2878 if(is_typedef_symbol(token.v.symbol)) {
2879 return parse_cast();
2883 expression_t *result = parse_expression();
2889 static expression_t *parse_function_keyword(void)
2894 if (current_function == NULL) {
2895 parse_error("'__func__' used outside of a function");
2898 string_literal_expression_t *expression
2899 = allocate_ast_zero(sizeof(expression[0]));
2901 expression->expression.type = EXPR_FUNCTION;
2902 expression->expression.datatype = type_string;
2903 expression->value = "TODO: FUNCTION";
2905 return (expression_t*) expression;
2908 static expression_t *parse_pretty_function_keyword(void)
2910 eat(T___PRETTY_FUNCTION__);
2913 string_literal_expression_t *expression
2914 = allocate_ast_zero(sizeof(expression[0]));
2916 expression->expression.type = EXPR_PRETTY_FUNCTION;
2917 expression->expression.datatype = type_string;
2918 expression->value = "TODO: PRETTY FUNCTION";
2920 return (expression_t*) expression;
2923 static designator_t *parse_designator(void)
2925 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2927 if(token.type != T_IDENTIFIER) {
2928 parse_error_expected("while parsing member designator",
2933 result->symbol = token.v.symbol;
2936 designator_t *last_designator = result;
2938 if(token.type == '.') {
2940 if(token.type != T_IDENTIFIER) {
2941 parse_error_expected("while parsing member designator",
2946 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2947 designator->symbol = token.v.symbol;
2950 last_designator->next = designator;
2951 last_designator = designator;
2954 if(token.type == '[') {
2956 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2957 designator->array_access = parse_expression();
2958 if(designator->array_access == NULL) {
2964 last_designator->next = designator;
2965 last_designator = designator;
2974 static expression_t *parse_offsetof(void)
2976 eat(T___builtin_offsetof);
2978 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
2979 expression->base.datatype = type_size_t;
2982 expression->offsetofe.type = parse_typename();
2984 expression->offsetofe.designator = parse_designator();
2990 static expression_t *parse_va_arg(void)
2992 eat(T___builtin_va_arg);
2994 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
2997 expression->va_arge.arg = parse_assignment_expression();
2999 expression->base.datatype = parse_typename();
3005 static expression_t *parse_builtin_symbol(void)
3007 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3009 symbol_t *symbol = token.v.symbol;
3011 expression->builtin_symbol.symbol = symbol;
3014 type_t *type = get_builtin_symbol_type(symbol);
3015 type = automatic_type_conversion(type);
3017 expression->base.datatype = type;
3021 static expression_t *parse_primary_expression(void)
3023 switch(token.type) {
3025 return parse_int_const();
3026 case T_FLOATINGPOINT:
3027 return parse_float_const();
3028 case T_STRING_LITERAL:
3029 return parse_string_const();
3031 return parse_reference();
3032 case T___FUNCTION__:
3034 return parse_function_keyword();
3035 case T___PRETTY_FUNCTION__:
3036 return parse_pretty_function_keyword();
3037 case T___builtin_offsetof:
3038 return parse_offsetof();
3039 case T___builtin_va_arg:
3040 return parse_va_arg();
3041 case T___builtin_nanf:
3042 case T___builtin_alloca:
3043 case T___builtin_expect:
3044 case T___builtin_va_start:
3045 case T___builtin_va_end:
3046 return parse_builtin_symbol();
3049 return parse_brace_expression();
3052 parser_print_error_prefix();
3053 fprintf(stderr, "unexpected token ");
3054 print_token(stderr, &token);
3055 fprintf(stderr, "\n");
3058 return make_invalid_expression();
3061 static expression_t *parse_array_expression(unsigned precedence,
3068 expression_t *inside = parse_expression();
3070 array_access_expression_t *array_access
3071 = allocate_ast_zero(sizeof(array_access[0]));
3073 array_access->expression.type = EXPR_ARRAY_ACCESS;
3075 type_t *type_left = left->base.datatype;
3076 type_t *type_inside = inside->base.datatype;
3077 type_t *result_type = NULL;
3079 if(type_left != NULL && type_inside != NULL) {
3080 type_left = skip_typeref(type_left);
3081 type_inside = skip_typeref(type_inside);
3083 if(is_type_pointer(type_left)) {
3084 pointer_type_t *pointer = &type_left->pointer;
3085 result_type = pointer->points_to;
3086 array_access->array_ref = left;
3087 array_access->index = inside;
3088 } else if(is_type_pointer(type_inside)) {
3089 pointer_type_t *pointer = &type_inside->pointer;
3090 result_type = pointer->points_to;
3091 array_access->array_ref = inside;
3092 array_access->index = left;
3093 array_access->flipped = true;
3095 parser_print_error_prefix();
3096 fprintf(stderr, "array access on object with non-pointer types ");
3097 print_type_quoted(type_left);
3098 fprintf(stderr, ", ");
3099 print_type_quoted(type_inside);
3100 fprintf(stderr, "\n");
3103 array_access->array_ref = left;
3104 array_access->index = inside;
3107 if(token.type != ']') {
3108 parse_error_expected("Problem while parsing array access", ']', 0);
3109 return (expression_t*) array_access;
3113 result_type = automatic_type_conversion(result_type);
3114 array_access->expression.datatype = result_type;
3116 return (expression_t*) array_access;
3119 static bool is_declaration_specifier(const token_t *token,
3120 bool only_type_specifiers)
3122 switch(token->type) {
3126 return is_typedef_symbol(token->v.symbol);
3129 if(only_type_specifiers)
3138 static expression_t *parse_sizeof(unsigned precedence)
3142 sizeof_expression_t *sizeof_expression
3143 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3144 sizeof_expression->expression.type = EXPR_SIZEOF;
3145 sizeof_expression->expression.datatype = type_size_t;
3147 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3149 sizeof_expression->type = parse_typename();
3152 expression_t *expression = parse_sub_expression(precedence);
3153 expression->base.datatype = revert_automatic_type_conversion(expression);
3155 sizeof_expression->type = expression->base.datatype;
3156 sizeof_expression->size_expression = expression;
3159 return (expression_t*) sizeof_expression;
3162 static expression_t *parse_select_expression(unsigned precedence,
3163 expression_t *compound)
3166 assert(token.type == '.' || token.type == T_MINUSGREATER);
3168 bool is_pointer = (token.type == T_MINUSGREATER);
3171 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3172 select->select.compound = compound;
3174 if(token.type != T_IDENTIFIER) {
3175 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3178 symbol_t *symbol = token.v.symbol;
3179 select->select.symbol = symbol;
3182 type_t *orig_type = compound->base.datatype;
3183 if(orig_type == NULL)
3184 return make_invalid_expression();
3186 type_t *type = skip_typeref(orig_type);
3188 type_t *type_left = type;
3190 if(type->type != TYPE_POINTER) {
3191 parser_print_error_prefix();
3192 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3193 print_type_quoted(orig_type);
3194 fputc('\n', stderr);
3195 return make_invalid_expression();
3197 pointer_type_t *pointer_type = &type->pointer;
3198 type_left = pointer_type->points_to;
3200 type_left = skip_typeref(type_left);
3202 if(type_left->type != TYPE_COMPOUND_STRUCT
3203 && type_left->type != TYPE_COMPOUND_UNION) {
3204 parser_print_error_prefix();
3205 fprintf(stderr, "request for member '%s' in something not a struct or "
3206 "union, but ", symbol->string);
3207 print_type_quoted(type_left);
3208 fputc('\n', stderr);
3209 return make_invalid_expression();
3212 compound_type_t *compound_type = &type_left->compound;
3213 declaration_t *declaration = compound_type->declaration;
3215 if(!declaration->init.is_defined) {
3216 parser_print_error_prefix();
3217 fprintf(stderr, "request for member '%s' of incomplete type ",
3219 print_type_quoted(type_left);
3220 fputc('\n', stderr);
3221 return make_invalid_expression();
3224 declaration_t *iter = declaration->context.declarations;
3225 for( ; iter != NULL; iter = iter->next) {
3226 if(iter->symbol == symbol) {
3231 parser_print_error_prefix();
3232 print_type_quoted(type_left);
3233 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3234 return make_invalid_expression();
3237 /* we always do the auto-type conversions; the & and sizeof parser contains
3238 * code to revert this! */
3239 type_t *expression_type = automatic_type_conversion(iter->type);
3241 select->select.compound_entry = iter;
3242 select->base.datatype = expression_type;
3246 static expression_t *parse_call_expression(unsigned precedence,
3247 expression_t *expression)
3250 expression_t *result = allocate_expression_zero(EXPR_CALL);
3252 call_expression_t *call = &result->call;
3253 call->function = expression;
3255 function_type_t *function_type = NULL;
3256 type_t *orig_type = expression->base.datatype;
3257 if(orig_type != NULL) {
3258 type_t *type = skip_typeref(orig_type);
3260 if(is_type_pointer(type)) {
3261 pointer_type_t *pointer_type = &type->pointer;
3263 type = skip_typeref(pointer_type->points_to);
3265 if (type->type == TYPE_FUNCTION) {
3266 function_type = &type->function;
3267 call->expression.datatype = function_type->result_type;
3270 if(function_type == NULL) {
3271 parser_print_error_prefix();
3272 fputs("called object '", stderr);
3273 print_expression(expression);
3274 fputs("' (type ", stderr);
3275 print_type_quoted(orig_type);
3276 fputs(") is not a pointer to a function\n", stderr);
3278 function_type = NULL;
3279 call->expression.datatype = NULL;
3283 /* parse arguments */
3286 if(token.type != ')') {
3287 call_argument_t *last_argument = NULL;
3290 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3292 argument->expression = parse_assignment_expression();
3293 if(last_argument == NULL) {
3294 call->arguments = argument;
3296 last_argument->next = argument;
3298 last_argument = argument;
3300 if(token.type != ',')
3307 if(function_type != NULL) {
3308 function_parameter_t *parameter = function_type->parameters;
3309 call_argument_t *argument = call->arguments;
3310 for( ; parameter != NULL && argument != NULL;
3311 parameter = parameter->next, argument = argument->next) {
3312 type_t *expected_type = parameter->type;
3313 /* TODO report context in error messages */
3314 argument->expression = create_implicit_cast(argument->expression,
3317 /* too few parameters */
3318 if(parameter != NULL) {
3319 parser_print_error_prefix();
3320 fprintf(stderr, "too few arguments to function '");
3321 print_expression(expression);
3322 fprintf(stderr, "'\n");
3323 } else if(argument != NULL) {
3324 /* too many parameters */
3325 if(!function_type->variadic
3326 && !function_type->unspecified_parameters) {
3327 parser_print_error_prefix();
3328 fprintf(stderr, "too many arguments to function '");
3329 print_expression(expression);
3330 fprintf(stderr, "'\n");
3332 /* do default promotion */
3333 for( ; argument != NULL; argument = argument->next) {
3334 type_t *type = argument->expression->base.datatype;
3335 type = skip_typeref(type);
3340 if(is_type_integer(type)) {
3341 type = promote_integer(type);
3342 } else if(type == type_float) {
3346 argument->expression
3347 = create_implicit_cast(argument->expression, type);
3356 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3358 static expression_t *parse_conditional_expression(unsigned precedence,
3359 expression_t *expression)
3363 conditional_expression_t *conditional
3364 = allocate_ast_zero(sizeof(conditional[0]));
3365 conditional->expression.type = EXPR_CONDITIONAL;
3366 conditional->condition = expression;
3369 type_t *condition_type_orig = conditional->condition->base.datatype;
3370 if(condition_type_orig != NULL) {
3371 type_t *condition_type = skip_typeref(condition_type_orig);
3372 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3373 type_error("expected a scalar type",
3374 expression->base.source_position, condition_type_orig);
3378 expression_t *const t_expr = parse_expression();
3379 conditional->true_expression = t_expr;
3381 expression_t *const f_expr = parse_sub_expression(precedence);
3382 conditional->false_expression = f_expr;
3384 type_t *const true_type = t_expr->base.datatype;
3385 if(true_type == NULL)
3386 return (expression_t*) conditional;
3387 type_t *const false_type = f_expr->base.datatype;
3388 if(false_type == NULL)
3389 return (expression_t*) conditional;
3391 type_t *const skipped_true_type = skip_typeref(true_type);
3392 type_t *const skipped_false_type = skip_typeref(false_type);
3395 if (skipped_true_type == skipped_false_type) {
3396 conditional->expression.datatype = skipped_true_type;
3397 } else if (is_type_arithmetic(skipped_true_type) &&
3398 is_type_arithmetic(skipped_false_type)) {
3399 type_t *const result = semantic_arithmetic(skipped_true_type,
3400 skipped_false_type);
3401 conditional->true_expression = create_implicit_cast(t_expr, result);
3402 conditional->false_expression = create_implicit_cast(f_expr, result);
3403 conditional->expression.datatype = result;
3404 } else if (skipped_true_type->type == TYPE_POINTER &&
3405 skipped_false_type->type == TYPE_POINTER &&
3406 true /* TODO compatible points_to types */) {
3408 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3409 skipped_false_type->type == TYPE_POINTER)
3410 || (is_null_ptr_const(skipped_false_type) &&
3411 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3413 } else if(/* 1 is pointer to object type, other is void* */ false) {
3416 type_error_incompatible("while parsing conditional",
3417 expression->base.source_position, true_type,
3418 skipped_false_type);
3421 return (expression_t*) conditional;
3424 static expression_t *parse_extension(unsigned precedence)
3426 eat(T___extension__);
3428 /* TODO enable extensions */
3430 return parse_sub_expression(precedence);
3433 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3435 eat(T___builtin_classify_type);
3437 classify_type_expression_t *const classify_type_expr =
3438 allocate_ast_zero(sizeof(classify_type_expr[0]));
3439 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3440 classify_type_expr->expression.datatype = type_int;
3443 expression_t *const expression = parse_sub_expression(precedence);
3445 classify_type_expr->type_expression = expression;
3447 return (expression_t*)classify_type_expr;
3450 static void semantic_incdec(unary_expression_t *expression)
3452 type_t *orig_type = expression->value->base.datatype;
3453 if(orig_type == NULL)
3456 type_t *type = skip_typeref(orig_type);
3457 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3458 /* TODO: improve error message */
3459 parser_print_error_prefix();
3460 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3464 expression->expression.datatype = orig_type;
3467 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3469 type_t *orig_type = expression->value->base.datatype;
3470 if(orig_type == NULL)
3473 type_t *type = skip_typeref(orig_type);
3474 if(!is_type_arithmetic(type)) {
3475 /* TODO: improve error message */
3476 parser_print_error_prefix();
3477 fprintf(stderr, "operation needs an arithmetic type\n");
3481 expression->expression.datatype = orig_type;
3484 static void semantic_unexpr_scalar(unary_expression_t *expression)
3486 type_t *orig_type = expression->value->base.datatype;
3487 if(orig_type == NULL)
3490 type_t *type = skip_typeref(orig_type);
3491 if (!is_type_scalar(type)) {
3492 parse_error("operand of ! must be of scalar type\n");
3496 expression->expression.datatype = orig_type;
3499 static void semantic_unexpr_integer(unary_expression_t *expression)
3501 type_t *orig_type = expression->value->base.datatype;
3502 if(orig_type == NULL)
3505 type_t *type = skip_typeref(orig_type);
3506 if (!is_type_integer(type)) {
3507 parse_error("operand of ~ must be of integer type\n");
3511 expression->expression.datatype = orig_type;
3514 static void semantic_dereference(unary_expression_t *expression)
3516 type_t *orig_type = expression->value->base.datatype;
3517 if(orig_type == NULL)
3520 type_t *type = skip_typeref(orig_type);
3521 if(!is_type_pointer(type)) {
3522 parser_print_error_prefix();
3523 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3524 print_type_quoted(orig_type);
3525 fputs(" given.\n", stderr);
3529 pointer_type_t *pointer_type = &type->pointer;
3530 type_t *result_type = pointer_type->points_to;
3532 result_type = automatic_type_conversion(result_type);
3533 expression->expression.datatype = result_type;
3536 static void semantic_take_addr(unary_expression_t *expression)
3538 expression_t *value = expression->value;
3539 value->base.datatype = revert_automatic_type_conversion(value);
3541 type_t *orig_type = value->base.datatype;
3542 if(orig_type == NULL)
3545 if(value->type == EXPR_REFERENCE) {
3546 reference_expression_t *reference = (reference_expression_t*) value;
3547 declaration_t *declaration = reference->declaration;
3548 if(declaration != NULL) {
3549 declaration->address_taken = 1;
3553 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3556 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3557 static expression_t *parse_##unexpression_type(unsigned precedence) \
3561 unary_expression_t *unary_expression \
3562 = allocate_ast_zero(sizeof(unary_expression[0])); \
3563 unary_expression->expression.type = EXPR_UNARY; \
3564 unary_expression->type = unexpression_type; \
3565 unary_expression->value = parse_sub_expression(precedence); \
3567 sfunc(unary_expression); \
3569 return (expression_t*) unary_expression; \
3572 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3573 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3574 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3575 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3576 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3577 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3578 semantic_unexpr_integer)
3579 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3581 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3584 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3586 static expression_t *parse_##unexpression_type(unsigned precedence, \
3587 expression_t *left) \
3589 (void) precedence; \
3592 unary_expression_t *unary_expression \
3593 = allocate_ast_zero(sizeof(unary_expression[0])); \
3594 unary_expression->expression.type = EXPR_UNARY; \
3595 unary_expression->type = unexpression_type; \
3596 unary_expression->value = left; \
3598 sfunc(unary_expression); \
3600 return (expression_t*) unary_expression; \
3603 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3605 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3608 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3610 /* TODO: handle complex + imaginary types */
3612 /* § 6.3.1.8 Usual arithmetic conversions */
3613 if(type_left == type_long_double || type_right == type_long_double) {
3614 return type_long_double;
3615 } else if(type_left == type_double || type_right == type_double) {
3617 } else if(type_left == type_float || type_right == type_float) {
3621 type_right = promote_integer(type_right);
3622 type_left = promote_integer(type_left);
3624 if(type_left == type_right)
3627 bool signed_left = is_type_signed(type_left);
3628 bool signed_right = is_type_signed(type_right);
3629 int rank_left = get_rank(type_left);
3630 int rank_right = get_rank(type_right);
3631 if(rank_left < rank_right) {
3632 if(signed_left == signed_right || !signed_right) {
3638 if(signed_left == signed_right || !signed_left) {
3646 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3648 expression_t *left = expression->left;
3649 expression_t *right = expression->right;
3650 type_t *orig_type_left = left->base.datatype;
3651 type_t *orig_type_right = right->base.datatype;
3653 if(orig_type_left == NULL || orig_type_right == NULL)
3656 type_t *type_left = skip_typeref(orig_type_left);
3657 type_t *type_right = skip_typeref(orig_type_right);
3659 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3660 /* TODO: improve error message */
3661 parser_print_error_prefix();
3662 fprintf(stderr, "operation needs arithmetic types\n");
3666 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3667 expression->left = create_implicit_cast(left, arithmetic_type);
3668 expression->right = create_implicit_cast(right, arithmetic_type);
3669 expression->expression.datatype = arithmetic_type;
3672 static void semantic_shift_op(binary_expression_t *expression)
3674 expression_t *left = expression->left;
3675 expression_t *right = expression->right;
3676 type_t *orig_type_left = left->base.datatype;
3677 type_t *orig_type_right = right->base.datatype;
3679 if(orig_type_left == NULL || orig_type_right == NULL)
3682 type_t *type_left = skip_typeref(orig_type_left);
3683 type_t *type_right = skip_typeref(orig_type_right);
3685 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3686 /* TODO: improve error message */
3687 parser_print_error_prefix();
3688 fprintf(stderr, "operation needs integer types\n");
3692 type_left = promote_integer(type_left);
3693 type_right = promote_integer(type_right);
3695 expression->left = create_implicit_cast(left, type_left);
3696 expression->right = create_implicit_cast(right, type_right);
3697 expression->expression.datatype = type_left;
3700 static void semantic_add(binary_expression_t *expression)
3702 expression_t *left = expression->left;
3703 expression_t *right = expression->right;
3704 type_t *orig_type_left = left->base.datatype;
3705 type_t *orig_type_right = right->base.datatype;
3707 if(orig_type_left == NULL || orig_type_right == NULL)
3710 type_t *type_left = skip_typeref(orig_type_left);
3711 type_t *type_right = skip_typeref(orig_type_right);
3714 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3715 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3716 expression->left = create_implicit_cast(left, arithmetic_type);
3717 expression->right = create_implicit_cast(right, arithmetic_type);
3718 expression->expression.datatype = arithmetic_type;
3720 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3721 expression->expression.datatype = type_left;
3722 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3723 expression->expression.datatype = type_right;
3725 parser_print_error_prefix();
3726 fprintf(stderr, "invalid operands to binary + (");
3727 print_type_quoted(orig_type_left);
3728 fprintf(stderr, ", ");
3729 print_type_quoted(orig_type_right);
3730 fprintf(stderr, ")\n");
3734 static void semantic_sub(binary_expression_t *expression)
3736 expression_t *left = expression->left;
3737 expression_t *right = expression->right;
3738 type_t *orig_type_left = left->base.datatype;
3739 type_t *orig_type_right = right->base.datatype;
3741 if(orig_type_left == NULL || orig_type_right == NULL)
3744 type_t *type_left = skip_typeref(orig_type_left);
3745 type_t *type_right = skip_typeref(orig_type_right);
3748 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3749 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3750 expression->left = create_implicit_cast(left, arithmetic_type);
3751 expression->right = create_implicit_cast(right, arithmetic_type);
3752 expression->expression.datatype = arithmetic_type;
3754 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3755 expression->expression.datatype = type_left;
3756 } else if(type_left->type == TYPE_POINTER &&
3757 type_right->type == TYPE_POINTER) {
3758 if(!pointers_compatible(type_left, type_right)) {
3759 parser_print_error_prefix();
3760 fprintf(stderr, "pointers to incompatible objects to binary - (");
3761 print_type_quoted(orig_type_left);
3762 fprintf(stderr, ", ");
3763 print_type_quoted(orig_type_right);
3764 fprintf(stderr, ")\n");
3766 expression->expression.datatype = type_ptrdiff_t;
3769 parser_print_error_prefix();
3770 fprintf(stderr, "invalid operands to binary - (");
3771 print_type_quoted(orig_type_left);
3772 fprintf(stderr, ", ");
3773 print_type_quoted(orig_type_right);
3774 fprintf(stderr, ")\n");
3778 static void semantic_comparison(binary_expression_t *expression)
3780 expression_t *left = expression->left;
3781 expression_t *right = expression->right;
3782 type_t *orig_type_left = left->base.datatype;
3783 type_t *orig_type_right = right->base.datatype;
3785 if(orig_type_left == NULL || orig_type_right == NULL)
3788 type_t *type_left = skip_typeref(orig_type_left);
3789 type_t *type_right = skip_typeref(orig_type_right);
3791 /* TODO non-arithmetic types */
3792 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3793 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3794 expression->left = create_implicit_cast(left, arithmetic_type);
3795 expression->right = create_implicit_cast(right, arithmetic_type);
3796 expression->expression.datatype = arithmetic_type;
3797 } else if (type_left->type == TYPE_POINTER &&
3798 type_right->type == TYPE_POINTER) {
3799 /* TODO check compatibility */
3800 } else if (type_left->type == TYPE_POINTER) {
3801 expression->right = create_implicit_cast(right, type_left);
3802 } else if (type_right->type == TYPE_POINTER) {
3803 expression->left = create_implicit_cast(left, type_right);
3805 type_error_incompatible("invalid operands in comparison",
3806 token.source_position, type_left, type_right);
3808 expression->expression.datatype = type_int;
3811 static void semantic_arithmetic_assign(binary_expression_t *expression)
3813 expression_t *left = expression->left;
3814 expression_t *right = expression->right;
3815 type_t *orig_type_left = left->base.datatype;
3816 type_t *orig_type_right = right->base.datatype;
3818 if(orig_type_left == NULL || orig_type_right == NULL)
3821 type_t *type_left = skip_typeref(orig_type_left);
3822 type_t *type_right = skip_typeref(orig_type_right);
3824 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3825 /* TODO: improve error message */
3826 parser_print_error_prefix();
3827 fprintf(stderr, "operation needs arithmetic types\n");
3831 /* combined instructions are tricky. We can't create an implicit cast on
3832 * the left side, because we need the uncasted form for the store.
3833 * The ast2firm pass has to know that left_type must be right_type
3834 * for the arithmeitc operation and create a cast by itself */
3835 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3836 expression->right = create_implicit_cast(right, arithmetic_type);
3837 expression->expression.datatype = type_left;
3840 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3842 expression_t *left = expression->left;
3843 expression_t *right = expression->right;
3844 type_t *orig_type_left = left->base.datatype;
3845 type_t *orig_type_right = right->base.datatype;
3847 if(orig_type_left == NULL || orig_type_right == NULL)
3850 type_t *type_left = skip_typeref(orig_type_left);
3851 type_t *type_right = skip_typeref(orig_type_right);
3853 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3854 /* combined instructions are tricky. We can't create an implicit cast on
3855 * the left side, because we need the uncasted form for the store.
3856 * The ast2firm pass has to know that left_type must be right_type
3857 * for the arithmeitc operation and create a cast by itself */
3858 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3859 expression->right = create_implicit_cast(right, arithmetic_type);
3860 expression->expression.datatype = type_left;
3861 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3862 expression->expression.datatype = type_left;
3864 parser_print_error_prefix();
3865 fputs("Incompatible types ", stderr);
3866 print_type_quoted(orig_type_left);
3867 fputs(" and ", stderr);
3868 print_type_quoted(orig_type_right);
3869 fputs(" in assignment\n", stderr);
3874 static void semantic_logical_op(binary_expression_t *expression)
3876 expression_t *left = expression->left;
3877 expression_t *right = expression->right;
3878 type_t *orig_type_left = left->base.datatype;
3879 type_t *orig_type_right = right->base.datatype;
3881 if(orig_type_left == NULL || orig_type_right == NULL)
3884 type_t *type_left = skip_typeref(orig_type_left);
3885 type_t *type_right = skip_typeref(orig_type_right);
3887 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3888 /* TODO: improve error message */
3889 parser_print_error_prefix();
3890 fprintf(stderr, "operation needs scalar types\n");
3894 expression->expression.datatype = type_int;
3897 static bool has_const_fields(type_t *type)
3904 static void semantic_binexpr_assign(binary_expression_t *expression)
3906 expression_t *left = expression->left;
3907 type_t *orig_type_left = left->base.datatype;
3909 if(orig_type_left == NULL)
3912 type_t *type_left = revert_automatic_type_conversion(left);
3913 type_left = skip_typeref(orig_type_left);
3915 /* must be a modifiable lvalue */
3916 if (type_left->type == TYPE_ARRAY) {
3917 parser_print_error_prefix();
3918 fprintf(stderr, "Cannot assign to arrays ('");
3919 print_expression(left);
3920 fprintf(stderr, "')\n");
3923 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3924 parser_print_error_prefix();
3925 fprintf(stderr, "assignment to readonly location '");
3926 print_expression(left);
3927 fprintf(stderr, "' (type ");
3928 print_type_quoted(orig_type_left);
3929 fprintf(stderr, ")\n");
3932 if(is_type_incomplete(type_left)) {
3933 parser_print_error_prefix();
3934 fprintf(stderr, "left-hand side of assignment '");
3935 print_expression(left);
3936 fprintf(stderr, "' has incomplete type ");
3937 print_type_quoted(orig_type_left);
3938 fprintf(stderr, "\n");
3941 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3942 parser_print_error_prefix();
3943 fprintf(stderr, "can't assign to '");
3944 print_expression(left);
3945 fprintf(stderr, "' because compound type ");
3946 print_type_quoted(orig_type_left);
3947 fprintf(stderr, " has readonly fields\n");
3951 semantic_assign(orig_type_left, &expression->right, "assignment");
3953 expression->expression.datatype = orig_type_left;
3956 static void semantic_comma(binary_expression_t *expression)
3958 expression->expression.datatype = expression->right->base.datatype;
3961 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3962 static expression_t *parse_##binexpression_type(unsigned precedence, \
3963 expression_t *left) \
3967 expression_t *right = parse_sub_expression(precedence + lr); \
3969 binary_expression_t *binexpr \
3970 = allocate_ast_zero(sizeof(binexpr[0])); \
3971 binexpr->expression.type = EXPR_BINARY; \
3972 binexpr->type = binexpression_type; \
3973 binexpr->left = left; \
3974 binexpr->right = right; \
3977 return (expression_t*) binexpr; \
3980 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3981 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3982 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3983 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3984 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3985 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3986 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3987 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3988 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3989 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3990 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3991 semantic_comparison, 1)
3992 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3993 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3994 semantic_comparison, 1)
3995 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3996 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3997 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3998 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3999 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4000 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4001 semantic_shift_op, 1)
4002 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4003 semantic_shift_op, 1)
4004 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4005 semantic_arithmetic_addsubb_assign, 0)
4006 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4007 semantic_arithmetic_addsubb_assign, 0)
4008 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4009 semantic_arithmetic_assign, 0)
4010 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4011 semantic_arithmetic_assign, 0)
4012 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4013 semantic_arithmetic_assign, 0)
4014 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4015 semantic_arithmetic_assign, 0)
4016 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4017 semantic_arithmetic_assign, 0)
4018 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4019 semantic_arithmetic_assign, 0)
4020 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4021 semantic_arithmetic_assign, 0)
4022 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4023 semantic_arithmetic_assign, 0)
4025 static expression_t *parse_sub_expression(unsigned precedence)
4027 if(token.type < 0) {
4028 return expected_expression_error();
4031 expression_parser_function_t *parser
4032 = &expression_parsers[token.type];
4033 source_position_t source_position = token.source_position;
4036 if(parser->parser != NULL) {
4037 left = parser->parser(parser->precedence);
4039 left = parse_primary_expression();
4041 assert(left != NULL);
4042 left->base.source_position = source_position;
4045 if(token.type < 0) {
4046 return expected_expression_error();
4049 parser = &expression_parsers[token.type];
4050 if(parser->infix_parser == NULL)
4052 if(parser->infix_precedence < precedence)
4055 left = parser->infix_parser(parser->infix_precedence, left);
4057 assert(left != NULL);
4058 assert(left->type != EXPR_UNKNOWN);
4059 left->base.source_position = source_position;
4065 static expression_t *parse_expression(void)
4067 return parse_sub_expression(1);
4072 static void register_expression_parser(parse_expression_function parser,
4073 int token_type, unsigned precedence)
4075 expression_parser_function_t *entry = &expression_parsers[token_type];
4077 if(entry->parser != NULL) {
4078 fprintf(stderr, "for token ");
4079 print_token_type(stderr, (token_type_t) token_type);
4080 fprintf(stderr, "\n");
4081 panic("trying to register multiple expression parsers for a token");
4083 entry->parser = parser;
4084 entry->precedence = precedence;
4087 static void register_expression_infix_parser(
4088 parse_expression_infix_function parser, int token_type,
4089 unsigned precedence)
4091 expression_parser_function_t *entry = &expression_parsers[token_type];
4093 if(entry->infix_parser != NULL) {
4094 fprintf(stderr, "for token ");
4095 print_token_type(stderr, (token_type_t) token_type);
4096 fprintf(stderr, "\n");
4097 panic("trying to register multiple infix expression parsers for a "
4100 entry->infix_parser = parser;
4101 entry->infix_precedence = precedence;
4104 static void init_expression_parsers(void)
4106 memset(&expression_parsers, 0, sizeof(expression_parsers));
4108 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4109 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4110 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4111 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4112 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4113 T_GREATERGREATER, 16);
4114 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4115 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4116 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4117 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4118 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4119 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4120 T_GREATEREQUAL, 14);
4121 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4122 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4123 T_EXCLAMATIONMARKEQUAL, 13);
4124 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4125 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4126 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4127 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4128 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4129 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4130 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4131 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4132 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4133 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4134 T_ASTERISKEQUAL, 2);
4135 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4136 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4138 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4139 T_LESSLESSEQUAL, 2);
4140 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4141 T_GREATERGREATEREQUAL, 2);
4142 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4144 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4146 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4149 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4151 register_expression_infix_parser(parse_array_expression, '[', 30);
4152 register_expression_infix_parser(parse_call_expression, '(', 30);
4153 register_expression_infix_parser(parse_select_expression, '.', 30);
4154 register_expression_infix_parser(parse_select_expression,
4155 T_MINUSGREATER, 30);
4156 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4158 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4161 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4162 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4163 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4164 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4165 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4166 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4167 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4168 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4169 register_expression_parser(parse_sizeof, T_sizeof, 25);
4170 register_expression_parser(parse_extension, T___extension__, 25);
4171 register_expression_parser(parse_builtin_classify_type,
4172 T___builtin_classify_type, 25);
4175 static asm_constraint_t *parse_asm_constraints(void)
4177 asm_constraint_t *result = NULL;
4178 asm_constraint_t *last = NULL;
4180 while(token.type == T_STRING_LITERAL || token.type == '[') {
4181 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4182 memset(constraint, 0, sizeof(constraint[0]));
4184 if(token.type == '[') {
4186 if(token.type != T_IDENTIFIER) {
4187 parse_error_expected("while parsing asm constraint",
4191 constraint->symbol = token.v.symbol;
4196 constraint->constraints = parse_string_literals();
4198 constraint->expression = parse_expression();
4202 last->next = constraint;
4204 result = constraint;
4208 if(token.type != ',')
4216 static asm_clobber_t *parse_asm_clobbers(void)
4218 asm_clobber_t *result = NULL;
4219 asm_clobber_t *last = NULL;
4221 while(token.type == T_STRING_LITERAL) {
4222 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4223 clobber->clobber = parse_string_literals();
4226 last->next = clobber;
4232 if(token.type != ',')
4240 static statement_t *parse_asm_statement(void)
4244 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4245 statement->base.source_position = token.source_position;
4247 asm_statement_t *asm_statement = &statement->asms;
4249 if(token.type == T_volatile) {
4251 asm_statement->is_volatile = true;
4255 asm_statement->asm_text = parse_string_literals();
4257 if(token.type != ':')
4261 asm_statement->inputs = parse_asm_constraints();
4262 if(token.type != ':')
4266 asm_statement->outputs = parse_asm_constraints();
4267 if(token.type != ':')
4271 asm_statement->clobbers = parse_asm_clobbers();
4279 static statement_t *parse_case_statement(void)
4282 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4283 label->statement.type = STATEMENT_CASE_LABEL;
4284 label->statement.source_position = token.source_position;
4286 label->expression = parse_expression();
4289 label->label_statement = parse_statement();
4291 return (statement_t*) label;
4294 static statement_t *parse_default_statement(void)
4298 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4299 label->statement.type = STATEMENT_CASE_LABEL;
4300 label->statement.source_position = token.source_position;
4303 label->label_statement = parse_statement();
4305 return (statement_t*) label;
4308 static declaration_t *get_label(symbol_t *symbol)
4310 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4311 assert(current_function != NULL);
4312 /* if we found a label in the same function, then we already created the
4314 if(candidate != NULL
4315 && candidate->parent_context == ¤t_function->context) {
4319 /* otherwise we need to create a new one */
4320 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4321 declaration->namespc = NAMESPACE_LABEL;
4322 declaration->symbol = symbol;
4324 label_push(declaration);
4329 static statement_t *parse_label_statement(void)
4331 assert(token.type == T_IDENTIFIER);
4332 symbol_t *symbol = token.v.symbol;
4335 declaration_t *label = get_label(symbol);
4337 /* if source position is already set then the label is defined twice,
4338 * otherwise it was just mentioned in a goto so far */
4339 if(label->source_position.input_name != NULL) {
4340 parser_print_error_prefix();
4341 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4342 parser_print_error_prefix_pos(label->source_position);
4343 fprintf(stderr, "previous definition of '%s' was here\n",
4346 label->source_position = token.source_position;
4349 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4351 label_statement->statement.type = STATEMENT_LABEL;
4352 label_statement->statement.source_position = token.source_position;
4353 label_statement->label = label;
4357 if(token.type == '}') {
4358 parse_error("label at end of compound statement");
4359 return (statement_t*) label_statement;
4361 label_statement->label_statement = parse_statement();
4364 return (statement_t*) label_statement;
4367 static statement_t *parse_if(void)
4371 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4372 statement->statement.type = STATEMENT_IF;
4373 statement->statement.source_position = token.source_position;
4376 statement->condition = parse_expression();
4379 statement->true_statement = parse_statement();
4380 if(token.type == T_else) {
4382 statement->false_statement = parse_statement();
4385 return (statement_t*) statement;
4388 static statement_t *parse_switch(void)
4392 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4393 statement->statement.type = STATEMENT_SWITCH;
4394 statement->statement.source_position = token.source_position;
4397 statement->expression = parse_expression();
4399 statement->body = parse_statement();
4401 return (statement_t*) statement;
4404 static statement_t *parse_while(void)
4408 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4409 statement->statement.type = STATEMENT_WHILE;
4410 statement->statement.source_position = token.source_position;
4413 statement->condition = parse_expression();
4415 statement->body = parse_statement();
4417 return (statement_t*) statement;
4420 static statement_t *parse_do(void)
4424 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4425 statement->statement.type = STATEMENT_DO_WHILE;
4426 statement->statement.source_position = token.source_position;
4428 statement->body = parse_statement();
4431 statement->condition = parse_expression();
4435 return (statement_t*) statement;
4438 static statement_t *parse_for(void)
4442 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4443 statement->statement.type = STATEMENT_FOR;
4444 statement->statement.source_position = token.source_position;
4448 int top = environment_top();
4449 context_t *last_context = context;
4450 set_context(&statement->context);
4452 if(token.type != ';') {
4453 if(is_declaration_specifier(&token, false)) {
4454 parse_declaration();
4456 statement->initialisation = parse_expression();
4463 if(token.type != ';') {
4464 statement->condition = parse_expression();
4467 if(token.type != ')') {
4468 statement->step = parse_expression();
4471 statement->body = parse_statement();
4473 assert(context == &statement->context);
4474 set_context(last_context);
4475 environment_pop_to(top);
4477 return (statement_t*) statement;
4480 static statement_t *parse_goto(void)
4484 if(token.type != T_IDENTIFIER) {
4485 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4489 symbol_t *symbol = token.v.symbol;
4492 declaration_t *label = get_label(symbol);
4494 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4496 statement->statement.type = STATEMENT_GOTO;
4497 statement->statement.source_position = token.source_position;
4499 statement->label = label;
4503 return (statement_t*) statement;
4506 static statement_t *parse_continue(void)
4511 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4512 statement->type = STATEMENT_CONTINUE;
4513 statement->base.source_position = token.source_position;
4518 static statement_t *parse_break(void)
4523 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4524 statement->type = STATEMENT_BREAK;
4525 statement->base.source_position = token.source_position;
4530 static statement_t *parse_return(void)
4534 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4536 statement->statement.type = STATEMENT_RETURN;
4537 statement->statement.source_position = token.source_position;
4539 assert(current_function->type->type == TYPE_FUNCTION);
4540 function_type_t *function_type = ¤t_function->type->function;
4541 type_t *return_type = function_type->result_type;
4543 expression_t *return_value = NULL;
4544 if(token.type != ';') {
4545 return_value = parse_expression();
4549 if(return_type == NULL)
4550 return (statement_t*) statement;
4552 return_type = skip_typeref(return_type);
4554 if(return_value != NULL) {
4555 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4557 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4558 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4559 parse_warning("'return' with a value, in function returning void");
4560 return_value = NULL;
4562 if(return_type != NULL) {
4563 semantic_assign(return_type, &return_value, "'return'");
4567 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4568 parse_warning("'return' without value, in function returning "
4572 statement->return_value = return_value;
4574 return (statement_t*) statement;
4577 static statement_t *parse_declaration_statement(void)
4579 declaration_t *before = last_declaration;
4581 declaration_statement_t *statement
4582 = allocate_ast_zero(sizeof(statement[0]));
4583 statement->statement.type = STATEMENT_DECLARATION;
4584 statement->statement.source_position = token.source_position;
4586 declaration_specifiers_t specifiers;
4587 memset(&specifiers, 0, sizeof(specifiers));
4588 parse_declaration_specifiers(&specifiers);
4590 if(token.type == ';') {
4593 parse_init_declarators(&specifiers);
4596 if(before == NULL) {
4597 statement->declarations_begin = context->declarations;
4599 statement->declarations_begin = before->next;
4601 statement->declarations_end = last_declaration;
4603 return (statement_t*) statement;
4606 static statement_t *parse_expression_statement(void)
4608 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4609 statement->statement.type = STATEMENT_EXPRESSION;
4610 statement->statement.source_position = token.source_position;
4612 statement->expression = parse_expression();
4616 return (statement_t*) statement;
4619 static statement_t *parse_statement(void)
4621 statement_t *statement = NULL;
4623 /* declaration or statement */
4624 switch(token.type) {
4626 statement = parse_asm_statement();
4630 statement = parse_case_statement();
4634 statement = parse_default_statement();
4638 statement = parse_compound_statement();
4642 statement = parse_if();
4646 statement = parse_switch();
4650 statement = parse_while();
4654 statement = parse_do();
4658 statement = parse_for();
4662 statement = parse_goto();
4666 statement = parse_continue();
4670 statement = parse_break();
4674 statement = parse_return();
4683 if(look_ahead(1)->type == ':') {
4684 statement = parse_label_statement();
4688 if(is_typedef_symbol(token.v.symbol)) {
4689 statement = parse_declaration_statement();
4693 statement = parse_expression_statement();
4696 case T___extension__:
4697 /* this can be a prefix to a declaration or an expression statement */
4698 /* we simply eat it now and parse the rest with tail recursion */
4701 } while(token.type == T___extension__);
4702 statement = parse_statement();
4706 statement = parse_declaration_statement();
4710 statement = parse_expression_statement();
4714 assert(statement == NULL
4715 || statement->base.source_position.input_name != NULL);
4720 static statement_t *parse_compound_statement(void)
4722 compound_statement_t *compound_statement
4723 = allocate_ast_zero(sizeof(compound_statement[0]));
4724 compound_statement->statement.type = STATEMENT_COMPOUND;
4725 compound_statement->statement.source_position = token.source_position;
4729 int top = environment_top();
4730 context_t *last_context = context;
4731 set_context(&compound_statement->context);
4733 statement_t *last_statement = NULL;
4735 while(token.type != '}' && token.type != T_EOF) {
4736 statement_t *statement = parse_statement();
4737 if(statement == NULL)
4740 if(last_statement != NULL) {
4741 last_statement->base.next = statement;
4743 compound_statement->statements = statement;
4746 while(statement->base.next != NULL)
4747 statement = statement->base.next;
4749 last_statement = statement;
4752 if(token.type != '}') {
4753 parser_print_error_prefix_pos(
4754 compound_statement->statement.source_position);
4755 fprintf(stderr, "end of file while looking for closing '}'\n");
4759 assert(context == &compound_statement->context);
4760 set_context(last_context);
4761 environment_pop_to(top);
4763 return (statement_t*) compound_statement;
4766 static void initialize_builtins(void)
4768 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4769 type_wchar_ptr_t = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4770 type_size_t = make_global_typedef("__SIZE_TYPE__",
4771 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
4772 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
4773 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
4776 static translation_unit_t *parse_translation_unit(void)
4778 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4780 assert(global_context == NULL);
4781 global_context = &unit->context;
4783 assert(context == NULL);
4784 set_context(&unit->context);
4786 initialize_builtins();
4788 while(token.type != T_EOF) {
4789 parse_declaration();
4792 assert(context == &unit->context);
4794 last_declaration = NULL;
4796 assert(global_context == &unit->context);
4797 global_context = NULL;
4802 translation_unit_t *parse(void)
4804 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4805 label_stack = NEW_ARR_F(stack_entry_t, 0);
4806 found_error = false;
4808 type_set_output(stderr);
4809 ast_set_output(stderr);
4811 lookahead_bufpos = 0;
4812 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4815 translation_unit_t *unit = parse_translation_unit();
4817 DEL_ARR_F(environment_stack);
4818 DEL_ARR_F(label_stack);
4826 void init_parser(void)
4828 init_expression_parsers();
4829 obstack_init(&temp_obst);
4831 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4832 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4833 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4834 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4835 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4836 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4837 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4838 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4841 void exit_parser(void)
4843 obstack_free(&temp_obst, NULL);