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;
48 static type_t *type_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
74 #define IMAGINARY_SPECIFIERS \
77 #define COMPLEX_SPECIFIERS
78 #define IMAGINARY_SPECIFIERS
81 #define TYPE_SPECIFIERS \
99 #define DECLARATION_START \
104 #define TYPENAME_START \
108 static void *allocate_ast_zero(size_t size)
110 void *res = allocate_ast(size);
111 memset(res, 0, size);
115 static size_t get_expression_struct_size(expression_type_t type)
117 static const size_t sizes[] = {
118 [EXPR_INVALID] = sizeof(expression_base_t),
119 [EXPR_REFERENCE] = sizeof(reference_expression_t),
120 [EXPR_CONST] = sizeof(const_expression_t),
121 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
122 [EXPR_CALL] = sizeof(call_expression_t),
123 [EXPR_UNARY] = sizeof(unary_expression_t),
124 [EXPR_BINARY] = sizeof(binary_expression_t),
125 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
126 [EXPR_SELECT] = sizeof(select_expression_t),
127 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
128 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
129 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
130 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
131 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
132 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
133 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
134 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
135 [EXPR_STATEMENT] = sizeof(statement_expression_t)
137 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
138 assert(type <= EXPR_STATEMENT);
139 assert(sizes[type] != 0);
143 static expression_t *allocate_expression_zero(expression_type_t type)
145 size_t size = get_expression_struct_size(type);
146 expression_t *res = allocate_ast_zero(size);
148 res->base.type = type;
152 static size_t get_type_struct_size(type_type_t type)
154 static const size_t sizes[] = {
155 [TYPE_ATOMIC] = sizeof(atomic_type_t),
156 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
157 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
158 [TYPE_ENUM] = sizeof(enum_type_t),
159 [TYPE_FUNCTION] = sizeof(function_type_t),
160 [TYPE_POINTER] = sizeof(pointer_type_t),
161 [TYPE_ARRAY] = sizeof(array_type_t),
162 [TYPE_BUILTIN] = sizeof(builtin_type_t),
163 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
164 [TYPE_TYPEOF] = sizeof(typeof_type_t),
166 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
167 assert(type <= TYPE_TYPEOF);
168 assert(sizes[type] != 0);
172 static type_t *allocate_type_zero(type_type_t type)
174 size_t size = get_type_struct_size(type);
175 type_t *res = obstack_alloc(type_obst, size);
176 memset(res, 0, size);
178 res->base.type = type;
182 static size_t get_initializer_size(initializer_type_t type)
184 static const size_t sizes[] = {
185 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
186 [INITIALIZER_STRING] = sizeof(initializer_string_t),
187 [INITIALIZER_LIST] = sizeof(initializer_list_t)
189 assert(type < INITIALIZER_COUNT);
190 assert(sizes[type] != 0);
194 static initializer_t *allocate_initializer(initializer_type_t type)
196 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
202 static void free_type(void *type)
204 obstack_free(type_obst, type);
208 * returns the top element of the environment stack
210 static size_t environment_top(void)
212 return ARR_LEN(environment_stack);
215 static size_t label_top(void)
217 return ARR_LEN(label_stack);
222 static inline void next_token(void)
224 token = lookahead_buffer[lookahead_bufpos];
225 lookahead_buffer[lookahead_bufpos] = lexer_token;
228 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
231 print_token(stderr, &token);
232 fprintf(stderr, "\n");
236 static inline const token_t *look_ahead(int num)
238 assert(num > 0 && num <= MAX_LOOKAHEAD);
239 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
240 return & lookahead_buffer[pos];
243 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
245 static void error(void)
248 #ifdef ABORT_ON_ERROR
253 static void parser_print_prefix_pos(const source_position_t source_position)
255 fputs(source_position.input_name, stderr);
257 fprintf(stderr, "%u", source_position.linenr);
261 static void parser_print_error_prefix_pos(
262 const source_position_t source_position)
264 parser_print_prefix_pos(source_position);
265 fputs("error: ", stderr);
269 static void parser_print_error_prefix(void)
271 parser_print_error_prefix_pos(token.source_position);
274 static void parse_error(const char *message)
276 parser_print_error_prefix();
277 fprintf(stderr, "parse error: %s\n", message);
280 static void parser_print_warning_prefix_pos(
281 const source_position_t source_position)
283 parser_print_prefix_pos(source_position);
284 fputs("warning: ", stderr);
287 static void parse_warning_pos(const source_position_t source_position,
288 const char *const message)
290 parser_print_prefix_pos(source_position);
291 fprintf(stderr, "warning: %s\n", message);
294 static void parse_warning(const char *message)
296 parse_warning_pos(token.source_position, message);
299 static void parse_error_expected(const char *message, ...)
304 if(message != NULL) {
305 parser_print_error_prefix();
306 fprintf(stderr, "%s\n", message);
308 parser_print_error_prefix();
309 fputs("Parse error: got ", stderr);
310 print_token(stderr, &token);
311 fputs(", expected ", stderr);
313 va_start(args, message);
314 token_type_t token_type = va_arg(args, token_type_t);
315 while(token_type != 0) {
319 fprintf(stderr, ", ");
321 print_token_type(stderr, token_type);
322 token_type = va_arg(args, token_type_t);
325 fprintf(stderr, "\n");
328 static void print_type_quoted(type_t *type)
335 static void type_error(const char *msg, const source_position_t source_position,
338 parser_print_error_prefix_pos(source_position);
339 fprintf(stderr, "%s, but found type ", msg);
340 print_type_quoted(type);
344 static void type_error_incompatible(const char *msg,
345 const source_position_t source_position, type_t *type1, type_t *type2)
347 parser_print_error_prefix_pos(source_position);
348 fprintf(stderr, "%s, incompatible types: ", msg);
349 print_type_quoted(type1);
350 fprintf(stderr, " - ");
351 print_type_quoted(type2);
352 fprintf(stderr, ")\n");
355 static void eat_block(void)
357 if(token.type == '{')
360 while(token.type != '}') {
361 if(token.type == T_EOF)
363 if(token.type == '{') {
372 static void eat_statement(void)
374 while(token.type != ';') {
375 if(token.type == T_EOF)
377 if(token.type == '}')
379 if(token.type == '{') {
388 static void eat_brace(void)
390 if(token.type == '(')
393 while(token.type != ')') {
394 if(token.type == T_EOF)
396 if(token.type == ')' || token.type == ';' || token.type == '}') {
399 if(token.type == '(') {
403 if(token.type == '{') {
412 #define expect(expected) \
413 if(UNLIKELY(token.type != (expected))) { \
414 parse_error_expected(NULL, (expected), 0); \
420 #define expect_block(expected) \
421 if(UNLIKELY(token.type != (expected))) { \
422 parse_error_expected(NULL, (expected), 0); \
428 #define expect_void(expected) \
429 if(UNLIKELY(token.type != (expected))) { \
430 parse_error_expected(NULL, (expected), 0); \
436 static void set_context(context_t *new_context)
438 context = new_context;
440 last_declaration = new_context->declarations;
441 if(last_declaration != NULL) {
442 while(last_declaration->next != NULL) {
443 last_declaration = last_declaration->next;
449 * called when we find a 2nd declarator for an identifier we already have a
452 static bool is_compatible_declaration (declaration_t *declaration,
453 declaration_t *previous)
455 if (declaration->type->type == TYPE_FUNCTION &&
456 previous->type->type == TYPE_FUNCTION &&
457 previous->type->function.unspecified_parameters) {
458 function_type_t* const prev_func = &previous->type->function;
459 function_type_t* const decl_func = &declaration->type->function;
460 if (prev_func->unspecified_parameters &&
461 prev_func->result_type == decl_func->result_type) {
462 declaration->type = previous->type;
466 /* TODO: not correct yet */
467 return declaration->type == previous->type;
470 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
472 declaration_t *declaration = symbol->declaration;
473 for( ; declaration != NULL; declaration = declaration->symbol_next) {
474 if(declaration->namespc == namespc)
481 static const char *get_namespace_prefix(namespace_t namespc)
484 case NAMESPACE_NORMAL:
486 case NAMESPACE_UNION:
488 case NAMESPACE_STRUCT:
492 case NAMESPACE_LABEL:
495 panic("invalid namespace found");
499 * pushs an environment_entry on the environment stack and links the
500 * corresponding symbol to the new entry
502 static declaration_t *stack_push(stack_entry_t **stack_ptr,
503 declaration_t *declaration,
504 context_t *parent_context)
506 symbol_t *symbol = declaration->symbol;
507 namespace_t namespc = (namespace_t)declaration->namespc;
509 /* a declaration should be only pushed once */
510 assert(declaration->parent_context == NULL);
511 declaration->parent_context = parent_context;
513 declaration_t *previous_declaration = get_declaration(symbol, namespc);
514 assert(declaration != previous_declaration);
515 if(previous_declaration != NULL
516 && previous_declaration->parent_context == context) {
517 if(!is_compatible_declaration(declaration, previous_declaration)) {
518 parser_print_error_prefix_pos(declaration->source_position);
519 fprintf(stderr, "definition of symbol %s%s with type ",
520 get_namespace_prefix(namespc), symbol->string);
521 print_type_quoted(declaration->type);
523 parser_print_error_prefix_pos(
524 previous_declaration->source_position);
525 fprintf(stderr, "is incompatible with previous declaration "
527 print_type_quoted(previous_declaration->type);
530 unsigned old_storage_class = previous_declaration->storage_class;
531 unsigned new_storage_class = declaration->storage_class;
532 if (current_function == NULL) {
533 if (old_storage_class != STORAGE_CLASS_STATIC &&
534 new_storage_class == STORAGE_CLASS_STATIC) {
535 parser_print_error_prefix_pos(declaration->source_position);
537 "static declaration of '%s' follows non-static declaration\n",
539 parser_print_error_prefix_pos(previous_declaration->source_position);
540 fprintf(stderr, "previous declaration of '%s' was here\n",
543 if (old_storage_class == STORAGE_CLASS_EXTERN) {
544 if (new_storage_class == STORAGE_CLASS_NONE) {
545 previous_declaration->storage_class = STORAGE_CLASS_NONE;
548 parser_print_warning_prefix_pos(declaration->source_position);
549 fprintf(stderr, "redundant declaration for '%s'\n",
551 parser_print_warning_prefix_pos(previous_declaration->source_position);
552 fprintf(stderr, "previous declaration of '%s' was here\n",
557 if (old_storage_class == STORAGE_CLASS_EXTERN &&
558 new_storage_class == STORAGE_CLASS_EXTERN) {
559 parser_print_warning_prefix_pos(declaration->source_position);
560 fprintf(stderr, "redundant extern declaration for '%s'\n",
562 parser_print_warning_prefix_pos(previous_declaration->source_position);
563 fprintf(stderr, "previous declaration of '%s' was here\n",
566 parser_print_error_prefix_pos(declaration->source_position);
567 if (old_storage_class == new_storage_class) {
568 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
570 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
572 parser_print_error_prefix_pos(previous_declaration->source_position);
573 fprintf(stderr, "previous declaration of '%s' was here\n",
578 return previous_declaration;
581 /* remember old declaration */
583 entry.symbol = symbol;
584 entry.old_declaration = symbol->declaration;
585 entry.namespc = (unsigned short) namespc;
586 ARR_APP1(stack_entry_t, *stack_ptr, entry);
588 /* replace/add declaration into declaration list of the symbol */
589 if(symbol->declaration == NULL) {
590 symbol->declaration = declaration;
592 declaration_t *iter_last = NULL;
593 declaration_t *iter = symbol->declaration;
594 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
595 /* replace an entry? */
596 if(iter->namespc == namespc) {
597 if(iter_last == NULL) {
598 symbol->declaration = declaration;
600 iter_last->symbol_next = declaration;
602 declaration->symbol_next = iter->symbol_next;
607 assert(iter_last->symbol_next == NULL);
608 iter_last->symbol_next = declaration;
615 static declaration_t *environment_push(declaration_t *declaration)
617 assert(declaration->source_position.input_name != NULL);
618 return stack_push(&environment_stack, declaration, context);
621 static declaration_t *label_push(declaration_t *declaration)
623 return stack_push(&label_stack, declaration, ¤t_function->context);
627 * pops symbols from the environment stack until @p new_top is the top element
629 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
631 stack_entry_t *stack = *stack_ptr;
632 size_t top = ARR_LEN(stack);
635 assert(new_top <= top);
639 for(i = top; i > new_top; --i) {
640 stack_entry_t *entry = & stack[i - 1];
642 declaration_t *old_declaration = entry->old_declaration;
643 symbol_t *symbol = entry->symbol;
644 namespace_t namespc = (namespace_t)entry->namespc;
646 /* replace/remove declaration */
647 declaration_t *declaration = symbol->declaration;
648 assert(declaration != NULL);
649 if(declaration->namespc == namespc) {
650 if(old_declaration == NULL) {
651 symbol->declaration = declaration->symbol_next;
653 symbol->declaration = old_declaration;
656 declaration_t *iter_last = declaration;
657 declaration_t *iter = declaration->symbol_next;
658 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
659 /* replace an entry? */
660 if(iter->namespc == namespc) {
661 assert(iter_last != NULL);
662 iter_last->symbol_next = old_declaration;
663 old_declaration->symbol_next = iter->symbol_next;
667 assert(iter != NULL);
671 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
674 static void environment_pop_to(size_t new_top)
676 stack_pop_to(&environment_stack, new_top);
679 static void label_pop_to(size_t new_top)
681 stack_pop_to(&label_stack, new_top);
685 static int get_rank(const type_t *type)
687 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
688 * and esp. footnote 108). However we can't fold constants (yet), so we
689 * can't decide wether unsigned int is possible, while int always works.
690 * (unsigned int would be preferable when possible... for stuff like
691 * struct { enum { ... } bla : 4; } ) */
692 if(type->type == TYPE_ENUM)
693 return ATOMIC_TYPE_INT;
695 assert(type->type == TYPE_ATOMIC);
696 const atomic_type_t *atomic_type = &type->atomic;
697 atomic_type_type_t atype = atomic_type->atype;
701 static type_t *promote_integer(type_t *type)
703 if(get_rank(type) < ATOMIC_TYPE_INT)
709 static expression_t *create_cast_expression(expression_t *expression,
712 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
714 cast->unary.type = UNEXPR_CAST;
715 cast->unary.value = expression;
716 cast->base.datatype = dest_type;
721 static bool is_null_expression(const expression_t *const expression)
723 if (expression->type != EXPR_CONST)
726 type_t *const type = skip_typeref(expression->base.datatype);
727 if (!is_type_integer(type))
730 return expression->conste.v.int_value == 0;
733 static expression_t *create_implicit_cast(expression_t *expression,
736 type_t *source_type = expression->base.datatype;
738 if(source_type == NULL)
741 source_type = skip_typeref(source_type);
742 dest_type = skip_typeref(dest_type);
744 if(source_type == dest_type)
747 switch (dest_type->type) {
749 /* TODO warning for implicitly converting to enum */
751 if (source_type->type != TYPE_ATOMIC &&
752 source_type->type != TYPE_ENUM) {
753 panic("casting of non-atomic types not implemented yet");
756 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
757 type_error_incompatible("can't cast types",
758 expression->base.source_position, source_type,
763 return create_cast_expression(expression, dest_type);
766 switch (source_type->type) {
768 if (is_null_expression(expression)) {
769 return create_cast_expression(expression, dest_type);
774 if (pointers_compatible(source_type, dest_type)) {
775 return create_cast_expression(expression, dest_type);
780 array_type_t *array_type = &source_type->array;
781 pointer_type_t *pointer_type = &dest_type->pointer;
782 if (types_compatible(array_type->element_type,
783 pointer_type->points_to)) {
784 return create_cast_expression(expression, dest_type);
790 panic("casting of non-atomic types not implemented yet");
793 type_error_incompatible("can't implicitly cast types",
794 expression->base.source_position, source_type, dest_type);
798 panic("casting of non-atomic types not implemented yet");
802 /** Implements the rules from § 6.5.16.1 */
803 static void semantic_assign(type_t *orig_type_left, expression_t **right,
806 type_t *orig_type_right = (*right)->base.datatype;
808 if(orig_type_right == NULL)
811 type_t *const type_left = skip_typeref(orig_type_left);
812 type_t *const type_right = skip_typeref(orig_type_right);
814 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
815 (is_type_pointer(type_left) && is_null_expression(*right)) ||
816 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
817 && is_type_pointer(type_right))) {
818 *right = create_implicit_cast(*right, type_left);
822 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
823 pointer_type_t *pointer_type_left = &type_left->pointer;
824 pointer_type_t *pointer_type_right = &type_right->pointer;
825 type_t *points_to_left = pointer_type_left->points_to;
826 type_t *points_to_right = pointer_type_right->points_to;
828 points_to_left = skip_typeref(points_to_left);
829 points_to_right = skip_typeref(points_to_right);
831 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
832 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
833 && !types_compatible(points_to_left, points_to_right)) {
834 goto incompatible_assign_types;
837 /* the left type has all qualifiers from the right type */
838 unsigned missing_qualifiers
839 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
840 if(missing_qualifiers != 0) {
841 parser_print_error_prefix();
842 fprintf(stderr, "destination type ");
843 print_type_quoted(type_left);
844 fprintf(stderr, " in %s from type ", context);
845 print_type_quoted(type_right);
846 fprintf(stderr, " lacks qualifiers '");
847 print_type_qualifiers(missing_qualifiers);
848 fprintf(stderr, "' in pointed-to type\n");
852 *right = create_implicit_cast(*right, type_left);
856 if (is_type_compound(type_left)
857 && types_compatible(type_left, type_right)) {
858 *right = create_implicit_cast(*right, type_left);
862 incompatible_assign_types:
863 /* TODO: improve error message */
864 parser_print_error_prefix();
865 fprintf(stderr, "incompatible types in %s\n", context);
866 parser_print_error_prefix();
867 print_type_quoted(type_left);
868 fputs(" <- ", stderr);
869 print_type_quoted(type_right);
873 static expression_t *parse_constant_expression(void)
875 /* start parsing at precedence 7 (conditional expression) */
876 return parse_sub_expression(7);
879 static expression_t *parse_assignment_expression(void)
881 /* start parsing at precedence 2 (assignment expression) */
882 return parse_sub_expression(2);
885 typedef struct declaration_specifiers_t declaration_specifiers_t;
886 struct declaration_specifiers_t {
887 unsigned char storage_class;
892 static void parse_compound_type_entries(void);
893 static declaration_t *parse_declarator(
894 const declaration_specifiers_t *specifiers, type_t *type,
895 bool may_be_abstract);
896 static declaration_t *record_declaration(declaration_t *declaration);
898 static const char *parse_string_literals(void)
900 assert(token.type == T_STRING_LITERAL);
901 const char *result = token.v.string;
905 while(token.type == T_STRING_LITERAL) {
906 result = concat_strings(result, token.v.string);
913 static void parse_attributes(void)
917 case T___attribute__: {
925 parse_error("EOF while parsing attribute");
944 if(token.type != T_STRING_LITERAL) {
945 parse_error_expected("while parsing assembler attribute",
950 parse_string_literals();
955 goto attributes_finished;
964 static designator_t *parse_designation(void)
966 if(token.type != '[' && token.type != '.')
969 designator_t *result = NULL;
970 designator_t *last = NULL;
973 designator_t *designator;
976 designator = allocate_ast_zero(sizeof(designator[0]));
978 designator->array_access = parse_constant_expression();
982 designator = allocate_ast_zero(sizeof(designator[0]));
984 if(token.type != T_IDENTIFIER) {
985 parse_error_expected("while parsing designator",
989 designator->symbol = token.v.symbol;
997 assert(designator != NULL);
999 last->next = designator;
1001 result = designator;
1008 static initializer_t *initializer_from_string(array_type_t *type,
1011 /* TODO: check len vs. size of array type */
1014 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1015 initializer->string.string = string;
1020 static initializer_t *initializer_from_expression(type_t *type,
1021 expression_t *expression)
1023 /* TODO check that expression is a constant expression */
1025 /* § 6.7.8.14/15 char array may be initialized by string literals */
1026 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1027 array_type_t *array_type = &type->array;
1028 type_t *element_type = array_type->element_type;
1030 if(element_type->type == TYPE_ATOMIC) {
1031 atomic_type_t *atomic_type = &element_type->atomic;
1032 atomic_type_type_t atype = atomic_type->atype;
1034 /* TODO handle wide strings */
1035 if(atype == ATOMIC_TYPE_CHAR
1036 || atype == ATOMIC_TYPE_SCHAR
1037 || atype == ATOMIC_TYPE_UCHAR) {
1039 string_literal_expression_t *literal = &expression->string;
1040 return initializer_from_string(array_type, literal->value);
1045 semantic_assign(type, &expression, "initializer");
1047 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1048 result->value.value = expression;
1053 static initializer_t *parse_sub_initializer(type_t *type,
1054 expression_t *expression,
1055 type_t *expression_type);
1057 static initializer_t *parse_sub_initializer_elem(type_t *type)
1059 if(token.type == '{') {
1060 return parse_sub_initializer(type, NULL, NULL);
1063 expression_t *expression = parse_assignment_expression();
1064 type_t *expression_type = skip_typeref(expression->base.datatype);
1066 return parse_sub_initializer(type, expression, expression_type);
1069 static bool had_initializer_brace_warning;
1071 static initializer_t *parse_sub_initializer(type_t *type,
1072 expression_t *expression,
1073 type_t *expression_type)
1075 if(is_type_scalar(type)) {
1076 /* there might be extra {} hierarchies */
1077 if(token.type == '{') {
1079 if(!had_initializer_brace_warning) {
1080 parse_warning("braces around scalar initializer");
1081 had_initializer_brace_warning = true;
1083 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1084 if(token.type == ',') {
1086 /* TODO: warn about excessive elements */
1092 if(expression == NULL) {
1093 expression = parse_assignment_expression();
1095 return initializer_from_expression(type, expression);
1098 /* TODO: ignore qualifiers, comparing pointers is probably
1100 if(expression != NULL && expression_type == type) {
1101 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1104 semantic_assign(type, &expression, "initializer");
1106 result->value.value = expression;
1111 bool read_paren = false;
1112 if(token.type == '{') {
1117 /* descend into subtype */
1118 initializer_t *result = NULL;
1119 initializer_t **elems;
1120 if(type->type == TYPE_ARRAY) {
1121 array_type_t *array_type = &type->array;
1122 type_t *element_type = array_type->element_type;
1123 element_type = skip_typeref(element_type);
1126 had_initializer_brace_warning = false;
1127 if(expression == NULL) {
1128 sub = parse_sub_initializer_elem(element_type);
1130 sub = parse_sub_initializer(element_type, expression,
1134 /* didn't match the subtypes -> try the parent type */
1136 assert(!read_paren);
1140 elems = NEW_ARR_F(initializer_t*, 0);
1141 ARR_APP1(initializer_t*, elems, sub);
1144 if(token.type == '}')
1147 if(token.type == '}')
1150 sub = parse_sub_initializer(element_type, NULL, NULL);
1152 /* TODO error, do nicer cleanup */
1153 parse_error("member initializer didn't match");
1157 ARR_APP1(initializer_t*, elems, sub);
1160 assert(type->type == TYPE_COMPOUND_STRUCT
1161 || type->type == TYPE_COMPOUND_UNION);
1162 compound_type_t *compound_type = &type->compound;
1163 context_t *context = & compound_type->declaration->context;
1165 declaration_t *first = context->declarations;
1168 type_t *first_type = first->type;
1169 first_type = skip_typeref(first_type);
1172 had_initializer_brace_warning = false;
1173 if(expression == NULL) {
1174 sub = parse_sub_initializer_elem(first_type);
1176 sub = parse_sub_initializer(first_type, expression,expression_type);
1179 /* didn't match the subtypes -> try our parent type */
1181 assert(!read_paren);
1185 elems = NEW_ARR_F(initializer_t*, 0);
1186 ARR_APP1(initializer_t*, elems, sub);
1188 declaration_t *iter = first->next;
1189 for( ; iter != NULL; iter = iter->next) {
1190 if(iter->symbol == NULL)
1192 if(iter->namespc != NAMESPACE_NORMAL)
1195 if(token.type == '}')
1198 if(token.type == '}')
1201 type_t *iter_type = iter->type;
1202 iter_type = skip_typeref(iter_type);
1204 sub = parse_sub_initializer(iter_type, NULL, NULL);
1206 /* TODO error, do nicer cleanup*/
1207 parse_error("member initializer didn't match");
1211 ARR_APP1(initializer_t*, elems, sub);
1215 int len = ARR_LEN(elems);
1216 size_t elems_size = sizeof(initializer_t*) * len;
1218 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1220 init->initializer.type = INITIALIZER_LIST;
1222 memcpy(init->initializers, elems, elems_size);
1225 result = (initializer_t*) init;
1228 if(token.type == ',')
1235 static initializer_t *parse_initializer(type_t *type)
1237 initializer_t *result;
1239 type = skip_typeref(type);
1241 if(token.type != '{') {
1242 expression_t *expression = parse_assignment_expression();
1243 return initializer_from_expression(type, expression);
1246 if(is_type_scalar(type)) {
1250 expression_t *expression = parse_assignment_expression();
1251 result = initializer_from_expression(type, expression);
1253 if(token.type == ',')
1259 result = parse_sub_initializer(type, NULL, NULL);
1267 static declaration_t *parse_compound_type_specifier(bool is_struct)
1275 symbol_t *symbol = NULL;
1276 declaration_t *declaration = NULL;
1278 if (token.type == T___attribute__) {
1283 if(token.type == T_IDENTIFIER) {
1284 symbol = token.v.symbol;
1288 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1290 declaration = get_declaration(symbol, NAMESPACE_UNION);
1292 } else if(token.type != '{') {
1294 parse_error_expected("while parsing struct type specifier",
1295 T_IDENTIFIER, '{', 0);
1297 parse_error_expected("while parsing union type specifier",
1298 T_IDENTIFIER, '{', 0);
1304 if(declaration == NULL) {
1305 declaration = allocate_ast_zero(sizeof(declaration[0]));
1308 declaration->namespc = NAMESPACE_STRUCT;
1310 declaration->namespc = NAMESPACE_UNION;
1312 declaration->source_position = token.source_position;
1313 declaration->symbol = symbol;
1314 record_declaration(declaration);
1317 if(token.type == '{') {
1318 if(declaration->init.is_defined) {
1319 assert(symbol != NULL);
1320 parser_print_error_prefix();
1321 fprintf(stderr, "multiple definition of %s %s\n",
1322 is_struct ? "struct" : "union", symbol->string);
1323 declaration->context.declarations = NULL;
1325 declaration->init.is_defined = true;
1327 int top = environment_top();
1328 context_t *last_context = context;
1329 set_context(& declaration->context);
1331 parse_compound_type_entries();
1334 assert(context == & declaration->context);
1335 set_context(last_context);
1336 environment_pop_to(top);
1342 static void parse_enum_entries(enum_type_t *const enum_type)
1346 if(token.type == '}') {
1348 parse_error("empty enum not allowed");
1353 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1355 if(token.type != T_IDENTIFIER) {
1356 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1360 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1361 entry->type = (type_t*) enum_type;
1362 entry->symbol = token.v.symbol;
1363 entry->source_position = token.source_position;
1366 if(token.type == '=') {
1368 entry->init.enum_value = parse_constant_expression();
1373 record_declaration(entry);
1375 if(token.type != ',')
1378 } while(token.type != '}');
1383 static type_t *parse_enum_specifier(void)
1387 declaration_t *declaration;
1390 if(token.type == T_IDENTIFIER) {
1391 symbol = token.v.symbol;
1394 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1395 } else if(token.type != '{') {
1396 parse_error_expected("while parsing enum type specifier",
1397 T_IDENTIFIER, '{', 0);
1404 if(declaration == NULL) {
1405 declaration = allocate_ast_zero(sizeof(declaration[0]));
1407 declaration->namespc = NAMESPACE_ENUM;
1408 declaration->source_position = token.source_position;
1409 declaration->symbol = symbol;
1412 type_t *const type = allocate_type_zero(TYPE_ENUM);
1413 type->enumt.declaration = declaration;
1415 if(token.type == '{') {
1416 if(declaration->init.is_defined) {
1417 parser_print_error_prefix();
1418 fprintf(stderr, "multiple definitions of enum %s\n",
1421 record_declaration(declaration);
1422 declaration->init.is_defined = 1;
1424 parse_enum_entries(&type->enumt);
1432 * if a symbol is a typedef to another type, return true
1434 static bool is_typedef_symbol(symbol_t *symbol)
1436 const declaration_t *const declaration =
1437 get_declaration(symbol, NAMESPACE_NORMAL);
1439 declaration != NULL &&
1440 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1443 static type_t *parse_typeof(void)
1451 expression_t *expression = NULL;
1454 switch(token.type) {
1455 case T___extension__:
1456 /* this can be a prefix to a typename or an expression */
1457 /* we simply eat it now. */
1460 } while(token.type == T___extension__);
1464 if(is_typedef_symbol(token.v.symbol)) {
1465 type = parse_typename();
1467 expression = parse_expression();
1468 type = expression->base.datatype;
1473 type = parse_typename();
1477 expression = parse_expression();
1478 type = expression->base.datatype;
1484 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1485 typeof_type->typeoft.expression = expression;
1486 typeof_type->typeoft.typeof_type = type;
1492 SPECIFIER_SIGNED = 1 << 0,
1493 SPECIFIER_UNSIGNED = 1 << 1,
1494 SPECIFIER_LONG = 1 << 2,
1495 SPECIFIER_INT = 1 << 3,
1496 SPECIFIER_DOUBLE = 1 << 4,
1497 SPECIFIER_CHAR = 1 << 5,
1498 SPECIFIER_SHORT = 1 << 6,
1499 SPECIFIER_LONG_LONG = 1 << 7,
1500 SPECIFIER_FLOAT = 1 << 8,
1501 SPECIFIER_BOOL = 1 << 9,
1502 SPECIFIER_VOID = 1 << 10,
1503 #ifdef PROVIDE_COMPLEX
1504 SPECIFIER_COMPLEX = 1 << 11,
1505 SPECIFIER_IMAGINARY = 1 << 12,
1509 static type_t *create_builtin_type(symbol_t *symbol)
1511 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1512 type->builtin.symbol = symbol;
1514 type->builtin.real_type = type_int;
1519 static type_t *get_typedef_type(symbol_t *symbol)
1521 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1522 if(declaration == NULL
1523 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1526 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1527 type->typedeft.declaration = declaration;
1532 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1534 type_t *type = NULL;
1535 unsigned type_qualifiers = 0;
1536 unsigned type_specifiers = 0;
1540 switch(token.type) {
1543 #define MATCH_STORAGE_CLASS(token, class) \
1545 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1546 parse_error("multiple storage classes in declaration " \
1549 specifiers->storage_class = class; \
1553 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1554 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1555 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1556 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1557 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1560 switch (specifiers->storage_class) {
1561 case STORAGE_CLASS_NONE:
1562 specifiers->storage_class = STORAGE_CLASS_THREAD;
1565 case STORAGE_CLASS_EXTERN:
1566 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1569 case STORAGE_CLASS_STATIC:
1570 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1574 parse_error("multiple storage classes in declaration specifiers");
1580 /* type qualifiers */
1581 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1583 type_qualifiers |= qualifier; \
1587 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1588 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1589 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1591 case T___extension__:
1596 /* type specifiers */
1597 #define MATCH_SPECIFIER(token, specifier, name) \
1600 if(type_specifiers & specifier) { \
1601 parse_error("multiple " name " type specifiers given"); \
1603 type_specifiers |= specifier; \
1607 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1608 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1609 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1610 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1611 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1612 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1613 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1614 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1615 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1616 #ifdef PROVIDE_COMPLEX
1617 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1618 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1622 specifiers->is_inline = true;
1627 if(type_specifiers & SPECIFIER_LONG_LONG) {
1628 parse_error("multiple type specifiers given");
1629 } else if(type_specifiers & SPECIFIER_LONG) {
1630 type_specifiers |= SPECIFIER_LONG_LONG;
1632 type_specifiers |= SPECIFIER_LONG;
1636 /* TODO: if type != NULL for the following rules should issue
1639 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1641 type->compound.declaration = parse_compound_type_specifier(true);
1645 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1647 type->compound.declaration = parse_compound_type_specifier(false);
1651 type = parse_enum_specifier();
1654 type = parse_typeof();
1656 case T___builtin_va_list:
1657 type = create_builtin_type(token.v.symbol);
1661 case T___attribute__:
1666 case T_IDENTIFIER: {
1667 type_t *typedef_type = get_typedef_type(token.v.symbol);
1669 if(typedef_type == NULL)
1670 goto finish_specifiers;
1673 type = typedef_type;
1677 /* function specifier */
1679 goto finish_specifiers;
1686 atomic_type_type_t atomic_type;
1688 /* match valid basic types */
1689 switch(type_specifiers) {
1690 case SPECIFIER_VOID:
1691 atomic_type = ATOMIC_TYPE_VOID;
1693 case SPECIFIER_CHAR:
1694 atomic_type = ATOMIC_TYPE_CHAR;
1696 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1697 atomic_type = ATOMIC_TYPE_SCHAR;
1699 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1700 atomic_type = ATOMIC_TYPE_UCHAR;
1702 case SPECIFIER_SHORT:
1703 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1704 case SPECIFIER_SHORT | SPECIFIER_INT:
1705 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1706 atomic_type = ATOMIC_TYPE_SHORT;
1708 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1709 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1710 atomic_type = ATOMIC_TYPE_USHORT;
1713 case SPECIFIER_SIGNED:
1714 case SPECIFIER_SIGNED | SPECIFIER_INT:
1715 atomic_type = ATOMIC_TYPE_INT;
1717 case SPECIFIER_UNSIGNED:
1718 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1719 atomic_type = ATOMIC_TYPE_UINT;
1721 case SPECIFIER_LONG:
1722 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1723 case SPECIFIER_LONG | SPECIFIER_INT:
1724 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1725 atomic_type = ATOMIC_TYPE_LONG;
1727 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1728 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1729 atomic_type = ATOMIC_TYPE_ULONG;
1731 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1732 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1733 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1734 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1736 atomic_type = ATOMIC_TYPE_LONGLONG;
1738 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1739 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1741 atomic_type = ATOMIC_TYPE_ULONGLONG;
1743 case SPECIFIER_FLOAT:
1744 atomic_type = ATOMIC_TYPE_FLOAT;
1746 case SPECIFIER_DOUBLE:
1747 atomic_type = ATOMIC_TYPE_DOUBLE;
1749 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1750 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1752 case SPECIFIER_BOOL:
1753 atomic_type = ATOMIC_TYPE_BOOL;
1755 #ifdef PROVIDE_COMPLEX
1756 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1757 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1759 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1760 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1762 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1763 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1765 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1766 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1768 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1769 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1771 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1772 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1776 /* invalid specifier combination, give an error message */
1777 if(type_specifiers == 0) {
1779 parse_warning("no type specifiers in declaration (using int)");
1780 atomic_type = ATOMIC_TYPE_INT;
1783 parse_error("no type specifiers given in declaration");
1785 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1786 (type_specifiers & SPECIFIER_UNSIGNED)) {
1787 parse_error("signed and unsigned specifiers gives");
1788 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1789 parse_error("only integer types can be signed or unsigned");
1791 parse_error("multiple datatypes in declaration");
1793 atomic_type = ATOMIC_TYPE_INVALID;
1796 type = allocate_type_zero(TYPE_ATOMIC);
1797 type->atomic.atype = atomic_type;
1800 if(type_specifiers != 0) {
1801 parse_error("multiple datatypes in declaration");
1805 type->base.qualifiers = type_qualifiers;
1807 type_t *result = typehash_insert(type);
1808 if(newtype && result != type) {
1812 specifiers->type = result;
1815 static type_qualifiers_t parse_type_qualifiers(void)
1817 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1820 switch(token.type) {
1821 /* type qualifiers */
1822 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1823 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1824 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1827 return type_qualifiers;
1832 static void parse_identifier_list(void)
1835 if(token.type != T_IDENTIFIER) {
1836 parse_error_expected("while parsing parameter identifier list",
1840 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1841 declaration->symbol = token.v.symbol;
1845 if(token.type != ',')
1851 static declaration_t *parse_parameter(void)
1853 declaration_specifiers_t specifiers;
1854 memset(&specifiers, 0, sizeof(specifiers));
1856 parse_declaration_specifiers(&specifiers);
1858 declaration_t *declaration
1859 = parse_declarator(&specifiers, specifiers.type, true);
1861 /* TODO check declaration constraints for parameters */
1862 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1863 parse_error("typedef not allowed in parameter list");
1866 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1868 if (declaration->type->type == TYPE_ARRAY) {
1869 const array_type_t *const arr_type = &declaration->type->array;
1870 type_t *element_type = arr_type->element_type;
1871 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1877 static declaration_t *parse_parameters(function_type_t *type)
1879 if(token.type == T_IDENTIFIER) {
1880 symbol_t *symbol = token.v.symbol;
1881 if(!is_typedef_symbol(symbol)) {
1882 /* TODO: K&R style C parameters */
1883 parse_identifier_list();
1888 if(token.type == ')') {
1889 type->unspecified_parameters = 1;
1892 if(token.type == T_void && look_ahead(1)->type == ')') {
1897 declaration_t *declarations = NULL;
1898 declaration_t *declaration;
1899 declaration_t *last_declaration = NULL;
1900 function_parameter_t *parameter;
1901 function_parameter_t *last_parameter = NULL;
1904 switch(token.type) {
1908 return declarations;
1911 case T___extension__:
1913 declaration = parse_parameter();
1915 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1916 memset(parameter, 0, sizeof(parameter[0]));
1917 parameter->type = declaration->type;
1919 if(last_parameter != NULL) {
1920 last_declaration->next = declaration;
1921 last_parameter->next = parameter;
1923 type->parameters = parameter;
1924 declarations = declaration;
1926 last_parameter = parameter;
1927 last_declaration = declaration;
1931 return declarations;
1933 if(token.type != ',')
1934 return declarations;
1944 } construct_type_type_t;
1946 typedef struct construct_type_t construct_type_t;
1947 struct construct_type_t {
1948 construct_type_type_t type;
1949 construct_type_t *next;
1952 typedef struct parsed_pointer_t parsed_pointer_t;
1953 struct parsed_pointer_t {
1954 construct_type_t construct_type;
1955 type_qualifiers_t type_qualifiers;
1958 typedef struct construct_function_type_t construct_function_type_t;
1959 struct construct_function_type_t {
1960 construct_type_t construct_type;
1961 type_t *function_type;
1964 typedef struct parsed_array_t parsed_array_t;
1965 struct parsed_array_t {
1966 construct_type_t construct_type;
1967 type_qualifiers_t type_qualifiers;
1973 typedef struct construct_base_type_t construct_base_type_t;
1974 struct construct_base_type_t {
1975 construct_type_t construct_type;
1979 static construct_type_t *parse_pointer_declarator(void)
1983 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1984 memset(pointer, 0, sizeof(pointer[0]));
1985 pointer->construct_type.type = CONSTRUCT_POINTER;
1986 pointer->type_qualifiers = parse_type_qualifiers();
1988 return (construct_type_t*) pointer;
1991 static construct_type_t *parse_array_declarator(void)
1995 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1996 memset(array, 0, sizeof(array[0]));
1997 array->construct_type.type = CONSTRUCT_ARRAY;
1999 if(token.type == T_static) {
2000 array->is_static = true;
2004 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2005 if(type_qualifiers != 0) {
2006 if(token.type == T_static) {
2007 array->is_static = true;
2011 array->type_qualifiers = type_qualifiers;
2013 if(token.type == '*' && look_ahead(1)->type == ']') {
2014 array->is_variable = true;
2016 } else if(token.type != ']') {
2017 array->size = parse_assignment_expression();
2022 return (construct_type_t*) array;
2025 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2029 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2031 declaration_t *parameters = parse_parameters(&type->function);
2032 if(declaration != NULL) {
2033 declaration->context.declarations = parameters;
2036 construct_function_type_t *construct_function_type =
2037 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2038 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2039 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2040 construct_function_type->function_type = type;
2044 return (construct_type_t*) construct_function_type;
2047 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2048 bool may_be_abstract)
2050 /* construct a single linked list of construct_type_t's which describe
2051 * how to construct the final declarator type */
2052 construct_type_t *first = NULL;
2053 construct_type_t *last = NULL;
2056 while(token.type == '*') {
2057 construct_type_t *type = parse_pointer_declarator();
2068 /* TODO: find out if this is correct */
2071 construct_type_t *inner_types = NULL;
2073 switch(token.type) {
2075 if(declaration == NULL) {
2076 parse_error("no identifier expected in typename");
2078 declaration->symbol = token.v.symbol;
2079 declaration->source_position = token.source_position;
2085 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2091 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2092 /* avoid a loop in the outermost scope, because eat_statement doesn't
2094 if(token.type == '}' && current_function == NULL) {
2102 construct_type_t *p = last;
2105 construct_type_t *type;
2106 switch(token.type) {
2108 type = parse_function_declarator(declaration);
2111 type = parse_array_declarator();
2114 goto declarator_finished;
2117 /* insert in the middle of the list (behind p) */
2119 type->next = p->next;
2130 declarator_finished:
2133 /* append inner_types at the end of the list, we don't to set last anymore
2134 * as it's not needed anymore */
2136 assert(first == NULL);
2137 first = inner_types;
2139 last->next = inner_types;
2145 static type_t *construct_declarator_type(construct_type_t *construct_list,
2148 construct_type_t *iter = construct_list;
2149 for( ; iter != NULL; iter = iter->next) {
2150 switch(iter->type) {
2151 case CONSTRUCT_INVALID:
2152 panic("invalid type construction found");
2153 case CONSTRUCT_FUNCTION: {
2154 construct_function_type_t *construct_function_type
2155 = (construct_function_type_t*) iter;
2157 type_t *function_type = construct_function_type->function_type;
2159 function_type->function.result_type = type;
2161 type = function_type;
2165 case CONSTRUCT_POINTER: {
2166 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2167 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2168 pointer_type->pointer.points_to = type;
2169 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2171 type = pointer_type;
2175 case CONSTRUCT_ARRAY: {
2176 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2177 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2179 array_type->base.qualifiers = parsed_array->type_qualifiers;
2180 array_type->array.element_type = type;
2181 array_type->array.is_static = parsed_array->is_static;
2182 array_type->array.is_variable = parsed_array->is_variable;
2183 array_type->array.size = parsed_array->size;
2190 type_t *hashed_type = typehash_insert(type);
2191 if(hashed_type != type) {
2192 /* the function type was constructed earlier freeing it here will
2193 * destroy other types... */
2194 if(iter->type != CONSTRUCT_FUNCTION) {
2204 static declaration_t *parse_declarator(
2205 const declaration_specifiers_t *specifiers,
2206 type_t *type, bool may_be_abstract)
2208 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2209 declaration->storage_class = specifiers->storage_class;
2210 declaration->is_inline = specifiers->is_inline;
2212 construct_type_t *construct_type
2213 = parse_inner_declarator(declaration, may_be_abstract);
2214 declaration->type = construct_declarator_type(construct_type, type);
2216 if(construct_type != NULL) {
2217 obstack_free(&temp_obst, construct_type);
2223 static type_t *parse_abstract_declarator(type_t *base_type)
2225 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2227 type_t *result = construct_declarator_type(construct_type, base_type);
2228 if(construct_type != NULL) {
2229 obstack_free(&temp_obst, construct_type);
2235 static declaration_t *record_declaration(declaration_t *declaration)
2237 assert(context != NULL);
2239 symbol_t *symbol = declaration->symbol;
2240 if(symbol != NULL) {
2241 declaration_t *alias = environment_push(declaration);
2242 if(alias != declaration)
2245 declaration->parent_context = context;
2248 if(last_declaration != NULL) {
2249 last_declaration->next = declaration;
2251 context->declarations = declaration;
2253 last_declaration = declaration;
2258 static void parser_error_multiple_definition(declaration_t *previous,
2259 declaration_t *declaration)
2261 parser_print_error_prefix_pos(declaration->source_position);
2262 fprintf(stderr, "multiple definition of symbol '%s'\n",
2263 declaration->symbol->string);
2264 parser_print_error_prefix_pos(previous->source_position);
2265 fprintf(stderr, "this is the location of the previous definition.\n");
2268 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2271 declaration_t *ndeclaration
2272 = parse_declarator(specifiers, specifiers->type, false);
2274 declaration_t *declaration = record_declaration(ndeclaration);
2276 type_t *orig_type = declaration->type;
2277 type_t *type = skip_typeref(orig_type);
2278 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2279 parser_print_warning_prefix_pos(declaration->source_position);
2280 fprintf(stderr, "variable '%s' declared 'inline'\n",
2281 declaration->symbol->string);
2284 if(token.type == '=') {
2287 /* TODO: check that this is an allowed type (no function type) */
2289 if(declaration->init.initializer != NULL) {
2290 parser_error_multiple_definition(declaration, ndeclaration);
2293 initializer_t *initializer = parse_initializer(type);
2295 if(type->type == TYPE_ARRAY && initializer != NULL) {
2296 array_type_t *array_type = &type->array;
2298 if(array_type->size == NULL) {
2299 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2301 cnst->base.datatype = type_size_t;
2303 if(initializer->type == INITIALIZER_LIST) {
2304 initializer_list_t *list = &initializer->list;
2305 cnst->conste.v.int_value = list->len;
2307 assert(initializer->type == INITIALIZER_STRING);
2308 initializer_string_t *string = &initializer->string;
2309 cnst->conste.v.int_value = strlen(string->string) + 1;
2312 array_type->size = cnst;
2317 ndeclaration->init.initializer = initializer;
2318 } else if(token.type == '{') {
2319 if(type->type != TYPE_FUNCTION) {
2320 parser_print_error_prefix();
2321 fprintf(stderr, "declarator '");
2322 print_type_ext(orig_type, declaration->symbol, NULL);
2323 fprintf(stderr, "' has a body but is not a function type.\n");
2327 function_type_t *function_type = &type->function;
2328 /* § 6.7.5.3 (14) a function definition with () means no
2330 if(function_type->unspecified_parameters) {
2331 type_t *duplicate = duplicate_type(type);
2332 duplicate->function.unspecified_parameters = false;
2334 type = typehash_insert(duplicate);
2335 if(type != duplicate) {
2336 //obstack_free(type_obst, duplicate);
2338 function_type = &type->function;
2341 if(declaration->init.statement != NULL) {
2342 parser_error_multiple_definition(declaration, ndeclaration);
2344 if(ndeclaration != declaration) {
2345 memcpy(&declaration->context, &ndeclaration->context,
2346 sizeof(declaration->context));
2349 int top = environment_top();
2350 context_t *last_context = context;
2351 set_context(&declaration->context);
2353 /* push function parameters */
2354 declaration_t *parameter = declaration->context.declarations;
2355 for( ; parameter != NULL; parameter = parameter->next) {
2356 environment_push(parameter);
2359 int label_stack_top = label_top();
2360 declaration_t *old_current_function = current_function;
2361 current_function = declaration;
2363 statement_t *statement = parse_compound_statement();
2365 assert(current_function == declaration);
2366 current_function = old_current_function;
2367 label_pop_to(label_stack_top);
2369 assert(context == &declaration->context);
2370 set_context(last_context);
2371 environment_pop_to(top);
2373 declaration->init.statement = statement;
2377 if(token.type != ',')
2384 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2387 if(token.type == ':') {
2389 parse_constant_expression();
2390 /* TODO (bitfields) */
2392 declaration_t *declaration
2393 = parse_declarator(specifiers, specifiers->type, true);
2395 /* TODO: check constraints for struct declarations */
2396 /* TODO: check for doubled fields */
2397 record_declaration(declaration);
2399 if(token.type == ':') {
2401 parse_constant_expression();
2402 /* TODO (bitfields) */
2406 if(token.type != ',')
2413 static void parse_compound_type_entries(void)
2417 while(token.type != '}' && token.type != T_EOF) {
2418 declaration_specifiers_t specifiers;
2419 memset(&specifiers, 0, sizeof(specifiers));
2420 parse_declaration_specifiers(&specifiers);
2422 parse_struct_declarators(&specifiers);
2424 if(token.type == T_EOF) {
2425 parse_error("unexpected error while parsing struct");
2430 static void parse_declaration(void)
2432 source_position_t source_position = token.source_position;
2434 declaration_specifiers_t specifiers;
2435 memset(&specifiers, 0, sizeof(specifiers));
2436 parse_declaration_specifiers(&specifiers);
2438 if(token.type == ';') {
2439 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2440 parse_warning_pos(source_position,
2441 "useless keyword in empty declaration");
2443 switch (specifiers.type->type) {
2444 case TYPE_COMPOUND_STRUCT:
2445 case TYPE_COMPOUND_UNION: {
2446 const compound_type_t *const comp_type
2447 = &specifiers.type->compound;
2448 if (comp_type->declaration->symbol == NULL) {
2449 parse_warning_pos(source_position,
2450 "unnamed struct/union that defines no instances");
2455 case TYPE_ENUM: break;
2458 parse_warning_pos(source_position, "empty declaration");
2464 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2466 declaration->type = specifiers.type;
2467 declaration->storage_class = specifiers.storage_class;
2468 declaration->source_position = source_position;
2469 record_declaration(declaration);
2472 parse_init_declarators(&specifiers);
2475 static type_t *parse_typename(void)
2477 declaration_specifiers_t specifiers;
2478 memset(&specifiers, 0, sizeof(specifiers));
2479 parse_declaration_specifiers(&specifiers);
2480 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2481 /* TODO: improve error message, user does probably not know what a
2482 * storage class is...
2484 parse_error("typename may not have a storage class");
2487 type_t *result = parse_abstract_declarator(specifiers.type);
2495 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2496 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2497 expression_t *left);
2499 typedef struct expression_parser_function_t expression_parser_function_t;
2500 struct expression_parser_function_t {
2501 unsigned precedence;
2502 parse_expression_function parser;
2503 unsigned infix_precedence;
2504 parse_expression_infix_function infix_parser;
2507 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2509 static expression_t *make_invalid_expression(void)
2511 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2512 expression->base.source_position = token.source_position;
2516 static expression_t *expected_expression_error(void)
2518 parser_print_error_prefix();
2519 fprintf(stderr, "expected expression, got token ");
2520 print_token(stderr, & token);
2521 fprintf(stderr, "\n");
2525 return make_invalid_expression();
2528 static expression_t *parse_string_const(void)
2530 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2531 cnst->base.datatype = type_string;
2532 cnst->string.value = parse_string_literals();
2537 static expression_t *parse_int_const(void)
2539 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2540 cnst->base.datatype = token.datatype;
2541 cnst->conste.v.int_value = token.v.intvalue;
2548 static expression_t *parse_float_const(void)
2550 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2551 cnst->base.datatype = token.datatype;
2552 cnst->conste.v.float_value = token.v.floatvalue;
2559 static declaration_t *create_implicit_function(symbol_t *symbol,
2560 const source_position_t source_position)
2562 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2563 ntype->function.result_type = type_int;
2564 ntype->function.unspecified_parameters = true;
2566 type_t *type = typehash_insert(ntype);
2571 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2573 declaration->storage_class = STORAGE_CLASS_EXTERN;
2574 declaration->type = type;
2575 declaration->symbol = symbol;
2576 declaration->source_position = source_position;
2578 /* prepend the implicit definition to the global context
2579 * this is safe since the symbol wasn't declared as anything else yet
2581 assert(symbol->declaration == NULL);
2583 context_t *last_context = context;
2584 context = global_context;
2586 environment_push(declaration);
2587 declaration->next = context->declarations;
2588 context->declarations = declaration;
2590 context = last_context;
2595 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2597 function_parameter_t *parameter
2598 = obstack_alloc(type_obst, sizeof(parameter[0]));
2599 memset(parameter, 0, sizeof(parameter[0]));
2600 parameter->type = argument_type;
2602 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2603 type->function.result_type = result_type;
2604 type->function.parameters = parameter;
2606 type_t *result = typehash_insert(type);
2607 if(result != type) {
2614 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2616 switch(symbol->ID) {
2617 case T___builtin_alloca:
2618 return make_function_1_type(type_void_ptr, type_size_t);
2620 panic("not implemented builtin symbol found");
2625 * performs automatic type cast as described in § 6.3.2.1
2627 static type_t *automatic_type_conversion(type_t *type)
2632 if(type->type == TYPE_ARRAY) {
2633 array_type_t *array_type = &type->array;
2634 type_t *element_type = array_type->element_type;
2635 unsigned qualifiers = array_type->type.qualifiers;
2637 return make_pointer_type(element_type, qualifiers);
2640 if(type->type == TYPE_FUNCTION) {
2641 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2648 * reverts the automatic casts of array to pointer types and function
2649 * to function-pointer types as defined § 6.3.2.1
2651 type_t *revert_automatic_type_conversion(const expression_t *expression)
2653 if(expression->base.datatype == NULL)
2656 switch(expression->type) {
2657 case EXPR_REFERENCE: {
2658 const reference_expression_t *ref = &expression->reference;
2659 return ref->declaration->type;
2662 const select_expression_t *select = &expression->select;
2663 return select->compound_entry->type;
2666 const unary_expression_t *unary = &expression->unary;
2667 if(unary->type == UNEXPR_DEREFERENCE) {
2668 expression_t *value = unary->value;
2669 type_t *type = skip_typeref(value->base.datatype);
2670 pointer_type_t *pointer_type = &type->pointer;
2672 return pointer_type->points_to;
2676 case EXPR_BUILTIN_SYMBOL: {
2677 const builtin_symbol_expression_t *builtin
2678 = &expression->builtin_symbol;
2679 return get_builtin_symbol_type(builtin->symbol);
2681 case EXPR_ARRAY_ACCESS: {
2682 const array_access_expression_t *array_access
2683 = &expression->array_access;
2684 const expression_t *array_ref = array_access->array_ref;
2685 type_t *type_left = skip_typeref(array_ref->base.datatype);
2686 assert(is_type_pointer(type_left));
2687 pointer_type_t *pointer_type = &type_left->pointer;
2688 return pointer_type->points_to;
2695 return expression->base.datatype;
2698 static expression_t *parse_reference(void)
2700 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2702 reference_expression_t *ref = &expression->reference;
2703 ref->symbol = token.v.symbol;
2705 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2707 source_position_t source_position = token.source_position;
2710 if(declaration == NULL) {
2712 /* an implicitly defined function */
2713 if(token.type == '(') {
2714 parser_print_prefix_pos(token.source_position);
2715 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2716 ref->symbol->string);
2718 declaration = create_implicit_function(ref->symbol,
2723 parser_print_error_prefix();
2724 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2729 type_t *type = declaration->type;
2730 /* we always do the auto-type conversions; the & and sizeof parser contains
2731 * code to revert this! */
2732 type = automatic_type_conversion(type);
2734 ref->declaration = declaration;
2735 ref->expression.datatype = type;
2740 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2744 /* TODO check if explicit cast is allowed and issue warnings/errors */
2747 static expression_t *parse_cast(void)
2749 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
2751 cast->unary.type = UNEXPR_CAST;
2752 cast->base.source_position = token.source_position;
2754 type_t *type = parse_typename();
2757 expression_t *value = parse_sub_expression(20);
2759 check_cast_allowed(value, type);
2761 cast->base.datatype = type;
2762 cast->unary.value = value;
2767 static expression_t *parse_statement_expression(void)
2769 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2771 statement_t *statement = parse_compound_statement();
2772 expression->statement.statement = statement;
2773 if(statement == NULL) {
2778 assert(statement->type == STATEMENT_COMPOUND);
2779 compound_statement_t *compound_statement = &statement->compound;
2781 /* find last statement and use it's type */
2782 const statement_t *last_statement = NULL;
2783 const statement_t *iter = compound_statement->statements;
2784 for( ; iter != NULL; iter = iter->base.next) {
2785 last_statement = iter;
2788 if(last_statement->type == STATEMENT_EXPRESSION) {
2789 const expression_statement_t *expression_statement
2790 = &last_statement->expression;
2791 expression->base.datatype
2792 = expression_statement->expression->base.datatype;
2794 expression->base.datatype = type_void;
2802 static expression_t *parse_brace_expression(void)
2806 switch(token.type) {
2808 /* gcc extension: a stement expression */
2809 return parse_statement_expression();
2813 return parse_cast();
2815 if(is_typedef_symbol(token.v.symbol)) {
2816 return parse_cast();
2820 expression_t *result = parse_expression();
2826 static expression_t *parse_function_keyword(void)
2831 if (current_function == NULL) {
2832 parse_error("'__func__' used outside of a function");
2835 string_literal_expression_t *expression
2836 = allocate_ast_zero(sizeof(expression[0]));
2838 expression->expression.type = EXPR_FUNCTION;
2839 expression->expression.datatype = type_string;
2840 expression->value = "TODO: FUNCTION";
2842 return (expression_t*) expression;
2845 static expression_t *parse_pretty_function_keyword(void)
2847 eat(T___PRETTY_FUNCTION__);
2850 string_literal_expression_t *expression
2851 = allocate_ast_zero(sizeof(expression[0]));
2853 expression->expression.type = EXPR_PRETTY_FUNCTION;
2854 expression->expression.datatype = type_string;
2855 expression->value = "TODO: PRETTY FUNCTION";
2857 return (expression_t*) expression;
2860 static designator_t *parse_designator(void)
2862 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2864 if(token.type != T_IDENTIFIER) {
2865 parse_error_expected("while parsing member designator",
2870 result->symbol = token.v.symbol;
2873 designator_t *last_designator = result;
2875 if(token.type == '.') {
2877 if(token.type != T_IDENTIFIER) {
2878 parse_error_expected("while parsing member designator",
2883 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2884 designator->symbol = token.v.symbol;
2887 last_designator->next = designator;
2888 last_designator = designator;
2891 if(token.type == '[') {
2893 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2894 designator->array_access = parse_expression();
2895 if(designator->array_access == NULL) {
2901 last_designator->next = designator;
2902 last_designator = designator;
2911 static expression_t *parse_offsetof(void)
2913 eat(T___builtin_offsetof);
2915 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
2916 expression->base.datatype = type_size_t;
2919 expression->offsetofe.type = parse_typename();
2921 expression->offsetofe.designator = parse_designator();
2927 static expression_t *parse_va_arg(void)
2929 eat(T___builtin_va_arg);
2931 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
2934 expression->va_arge.arg = parse_assignment_expression();
2936 expression->base.datatype = parse_typename();
2942 static expression_t *parse_builtin_symbol(void)
2944 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
2946 symbol_t *symbol = token.v.symbol;
2948 expression->builtin_symbol.symbol = symbol;
2951 type_t *type = get_builtin_symbol_type(symbol);
2952 type = automatic_type_conversion(type);
2954 expression->base.datatype = type;
2958 static expression_t *parse_primary_expression(void)
2960 switch(token.type) {
2962 return parse_int_const();
2963 case T_FLOATINGPOINT:
2964 return parse_float_const();
2965 case T_STRING_LITERAL:
2966 return parse_string_const();
2968 return parse_reference();
2969 case T___FUNCTION__:
2971 return parse_function_keyword();
2972 case T___PRETTY_FUNCTION__:
2973 return parse_pretty_function_keyword();
2974 case T___builtin_offsetof:
2975 return parse_offsetof();
2976 case T___builtin_va_arg:
2977 return parse_va_arg();
2978 case T___builtin_alloca:
2979 case T___builtin_expect:
2980 case T___builtin_va_start:
2981 case T___builtin_va_end:
2982 return parse_builtin_symbol();
2985 return parse_brace_expression();
2988 parser_print_error_prefix();
2989 fprintf(stderr, "unexpected token ");
2990 print_token(stderr, &token);
2991 fprintf(stderr, "\n");
2994 return make_invalid_expression();
2997 static expression_t *parse_array_expression(unsigned precedence,
3004 expression_t *inside = parse_expression();
3006 array_access_expression_t *array_access
3007 = allocate_ast_zero(sizeof(array_access[0]));
3009 array_access->expression.type = EXPR_ARRAY_ACCESS;
3011 type_t *type_left = left->base.datatype;
3012 type_t *type_inside = inside->base.datatype;
3013 type_t *result_type = NULL;
3015 if(type_left != NULL && type_inside != NULL) {
3016 type_left = skip_typeref(type_left);
3017 type_inside = skip_typeref(type_inside);
3019 if(is_type_pointer(type_left)) {
3020 pointer_type_t *pointer = &type_left->pointer;
3021 result_type = pointer->points_to;
3022 array_access->array_ref = left;
3023 array_access->index = inside;
3024 } else if(is_type_pointer(type_inside)) {
3025 pointer_type_t *pointer = &type_inside->pointer;
3026 result_type = pointer->points_to;
3027 array_access->array_ref = inside;
3028 array_access->index = left;
3029 array_access->flipped = true;
3031 parser_print_error_prefix();
3032 fprintf(stderr, "array access on object with non-pointer types ");
3033 print_type_quoted(type_left);
3034 fprintf(stderr, ", ");
3035 print_type_quoted(type_inside);
3036 fprintf(stderr, "\n");
3039 array_access->array_ref = left;
3040 array_access->index = inside;
3043 if(token.type != ']') {
3044 parse_error_expected("Problem while parsing array access", ']', 0);
3045 return (expression_t*) array_access;
3049 result_type = automatic_type_conversion(result_type);
3050 array_access->expression.datatype = result_type;
3052 return (expression_t*) array_access;
3055 static bool is_declaration_specifier(const token_t *token,
3056 bool only_type_specifiers)
3058 switch(token->type) {
3062 return is_typedef_symbol(token->v.symbol);
3065 if(only_type_specifiers)
3074 static expression_t *parse_sizeof(unsigned precedence)
3078 sizeof_expression_t *sizeof_expression
3079 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3080 sizeof_expression->expression.type = EXPR_SIZEOF;
3081 sizeof_expression->expression.datatype = type_size_t;
3083 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3085 sizeof_expression->type = parse_typename();
3088 expression_t *expression = parse_sub_expression(precedence);
3089 expression->base.datatype = revert_automatic_type_conversion(expression);
3091 sizeof_expression->type = expression->base.datatype;
3092 sizeof_expression->size_expression = expression;
3095 return (expression_t*) sizeof_expression;
3098 static expression_t *parse_select_expression(unsigned precedence,
3099 expression_t *compound)
3102 assert(token.type == '.' || token.type == T_MINUSGREATER);
3104 bool is_pointer = (token.type == T_MINUSGREATER);
3107 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3108 select->select.compound = compound;
3110 if(token.type != T_IDENTIFIER) {
3111 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3114 symbol_t *symbol = token.v.symbol;
3115 select->select.symbol = symbol;
3118 type_t *orig_type = compound->base.datatype;
3119 if(orig_type == NULL)
3120 return make_invalid_expression();
3122 type_t *type = skip_typeref(orig_type);
3124 type_t *type_left = type;
3126 if(type->type != TYPE_POINTER) {
3127 parser_print_error_prefix();
3128 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3129 print_type_quoted(orig_type);
3130 fputc('\n', stderr);
3131 return make_invalid_expression();
3133 pointer_type_t *pointer_type = &type->pointer;
3134 type_left = pointer_type->points_to;
3136 type_left = skip_typeref(type_left);
3138 if(type_left->type != TYPE_COMPOUND_STRUCT
3139 && type_left->type != TYPE_COMPOUND_UNION) {
3140 parser_print_error_prefix();
3141 fprintf(stderr, "request for member '%s' in something not a struct or "
3142 "union, but ", symbol->string);
3143 print_type_quoted(type_left);
3144 fputc('\n', stderr);
3145 return make_invalid_expression();
3148 compound_type_t *compound_type = &type_left->compound;
3149 declaration_t *declaration = compound_type->declaration;
3151 if(!declaration->init.is_defined) {
3152 parser_print_error_prefix();
3153 fprintf(stderr, "request for member '%s' of incomplete type ",
3155 print_type_quoted(type_left);
3156 fputc('\n', stderr);
3157 return make_invalid_expression();
3160 declaration_t *iter = declaration->context.declarations;
3161 for( ; iter != NULL; iter = iter->next) {
3162 if(iter->symbol == symbol) {
3167 parser_print_error_prefix();
3168 print_type_quoted(type_left);
3169 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3170 return make_invalid_expression();
3173 /* we always do the auto-type conversions; the & and sizeof parser contains
3174 * code to revert this! */
3175 type_t *expression_type = automatic_type_conversion(iter->type);
3177 select->select.compound_entry = iter;
3178 select->base.datatype = expression_type;
3182 static expression_t *parse_call_expression(unsigned precedence,
3183 expression_t *expression)
3186 expression_t *result = allocate_expression_zero(EXPR_CALL);
3188 call_expression_t *call = &result->call;
3189 call->function = expression;
3191 function_type_t *function_type = NULL;
3192 type_t *orig_type = expression->base.datatype;
3193 if(orig_type != NULL) {
3194 type_t *type = skip_typeref(orig_type);
3196 if(is_type_pointer(type)) {
3197 pointer_type_t *pointer_type = &type->pointer;
3199 type = skip_typeref(pointer_type->points_to);
3201 if (type->type == TYPE_FUNCTION) {
3202 function_type = &type->function;
3203 call->expression.datatype = function_type->result_type;
3206 if(function_type == NULL) {
3207 parser_print_error_prefix();
3208 fputs("called object '", stderr);
3209 print_expression(expression);
3210 fputs("' (type ", stderr);
3211 print_type_quoted(orig_type);
3212 fputs(") is not a pointer to a function\n", stderr);
3214 function_type = NULL;
3215 call->expression.datatype = NULL;
3219 /* parse arguments */
3222 if(token.type != ')') {
3223 call_argument_t *last_argument = NULL;
3226 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3228 argument->expression = parse_assignment_expression();
3229 if(last_argument == NULL) {
3230 call->arguments = argument;
3232 last_argument->next = argument;
3234 last_argument = argument;
3236 if(token.type != ',')
3243 if(function_type != NULL) {
3244 function_parameter_t *parameter = function_type->parameters;
3245 call_argument_t *argument = call->arguments;
3246 for( ; parameter != NULL && argument != NULL;
3247 parameter = parameter->next, argument = argument->next) {
3248 type_t *expected_type = parameter->type;
3249 /* TODO report context in error messages */
3250 argument->expression = create_implicit_cast(argument->expression,
3253 /* too few parameters */
3254 if(parameter != NULL) {
3255 parser_print_error_prefix();
3256 fprintf(stderr, "too few arguments to function '");
3257 print_expression(expression);
3258 fprintf(stderr, "'\n");
3259 } else if(argument != NULL) {
3260 /* too many parameters */
3261 if(!function_type->variadic
3262 && !function_type->unspecified_parameters) {
3263 parser_print_error_prefix();
3264 fprintf(stderr, "too many arguments to function '");
3265 print_expression(expression);
3266 fprintf(stderr, "'\n");
3268 /* do default promotion */
3269 for( ; argument != NULL; argument = argument->next) {
3270 type_t *type = argument->expression->base.datatype;
3271 type = skip_typeref(type);
3276 if(is_type_integer(type)) {
3277 type = promote_integer(type);
3278 } else if(type == type_float) {
3282 argument->expression
3283 = create_implicit_cast(argument->expression, type);
3292 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3294 static expression_t *parse_conditional_expression(unsigned precedence,
3295 expression_t *expression)
3299 conditional_expression_t *conditional
3300 = allocate_ast_zero(sizeof(conditional[0]));
3301 conditional->expression.type = EXPR_CONDITIONAL;
3302 conditional->condition = expression;
3305 type_t *condition_type_orig = conditional->condition->base.datatype;
3306 if(condition_type_orig != NULL) {
3307 type_t *condition_type = skip_typeref(condition_type_orig);
3308 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3309 type_error("expected a scalar type",
3310 expression->base.source_position, condition_type_orig);
3314 expression_t *const t_expr = parse_expression();
3315 conditional->true_expression = t_expr;
3317 expression_t *const f_expr = parse_sub_expression(precedence);
3318 conditional->false_expression = f_expr;
3320 type_t *const true_type = t_expr->base.datatype;
3321 if(true_type == NULL)
3322 return (expression_t*) conditional;
3323 type_t *const false_type = f_expr->base.datatype;
3324 if(false_type == NULL)
3325 return (expression_t*) conditional;
3327 type_t *const skipped_true_type = skip_typeref(true_type);
3328 type_t *const skipped_false_type = skip_typeref(false_type);
3331 if (skipped_true_type == skipped_false_type) {
3332 conditional->expression.datatype = skipped_true_type;
3333 } else if (is_type_arithmetic(skipped_true_type) &&
3334 is_type_arithmetic(skipped_false_type)) {
3335 type_t *const result = semantic_arithmetic(skipped_true_type,
3336 skipped_false_type);
3337 conditional->true_expression = create_implicit_cast(t_expr, result);
3338 conditional->false_expression = create_implicit_cast(f_expr, result);
3339 conditional->expression.datatype = result;
3340 } else if (skipped_true_type->type == TYPE_POINTER &&
3341 skipped_false_type->type == TYPE_POINTER &&
3342 true /* TODO compatible points_to types */) {
3344 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3345 skipped_false_type->type == TYPE_POINTER)
3346 || (is_null_ptr_const(skipped_false_type) &&
3347 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3349 } else if(/* 1 is pointer to object type, other is void* */ false) {
3352 type_error_incompatible("while parsing conditional",
3353 expression->base.source_position, true_type,
3354 skipped_false_type);
3357 return (expression_t*) conditional;
3360 static expression_t *parse_extension(unsigned precedence)
3362 eat(T___extension__);
3364 /* TODO enable extensions */
3366 return parse_sub_expression(precedence);
3369 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3371 eat(T___builtin_classify_type);
3373 classify_type_expression_t *const classify_type_expr =
3374 allocate_ast_zero(sizeof(classify_type_expr[0]));
3375 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3376 classify_type_expr->expression.datatype = type_int;
3379 expression_t *const expression = parse_sub_expression(precedence);
3381 classify_type_expr->type_expression = expression;
3383 return (expression_t*)classify_type_expr;
3386 static void semantic_incdec(unary_expression_t *expression)
3388 type_t *orig_type = expression->value->base.datatype;
3389 if(orig_type == NULL)
3392 type_t *type = skip_typeref(orig_type);
3393 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3394 /* TODO: improve error message */
3395 parser_print_error_prefix();
3396 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3400 expression->expression.datatype = orig_type;
3403 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3405 type_t *orig_type = expression->value->base.datatype;
3406 if(orig_type == NULL)
3409 type_t *type = skip_typeref(orig_type);
3410 if(!is_type_arithmetic(type)) {
3411 /* TODO: improve error message */
3412 parser_print_error_prefix();
3413 fprintf(stderr, "operation needs an arithmetic type\n");
3417 expression->expression.datatype = orig_type;
3420 static void semantic_unexpr_scalar(unary_expression_t *expression)
3422 type_t *orig_type = expression->value->base.datatype;
3423 if(orig_type == NULL)
3426 type_t *type = skip_typeref(orig_type);
3427 if (!is_type_scalar(type)) {
3428 parse_error("operand of ! must be of scalar type\n");
3432 expression->expression.datatype = orig_type;
3435 static void semantic_unexpr_integer(unary_expression_t *expression)
3437 type_t *orig_type = expression->value->base.datatype;
3438 if(orig_type == NULL)
3441 type_t *type = skip_typeref(orig_type);
3442 if (!is_type_integer(type)) {
3443 parse_error("operand of ~ must be of integer type\n");
3447 expression->expression.datatype = orig_type;
3450 static void semantic_dereference(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_pointer(type)) {
3458 parser_print_error_prefix();
3459 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3460 print_type_quoted(orig_type);
3461 fputs(" given.\n", stderr);
3465 pointer_type_t *pointer_type = &type->pointer;
3466 type_t *result_type = pointer_type->points_to;
3468 result_type = automatic_type_conversion(result_type);
3469 expression->expression.datatype = result_type;
3472 static void semantic_take_addr(unary_expression_t *expression)
3474 expression_t *value = expression->value;
3475 value->base.datatype = revert_automatic_type_conversion(value);
3477 type_t *orig_type = value->base.datatype;
3478 if(orig_type == NULL)
3481 if(value->type == EXPR_REFERENCE) {
3482 reference_expression_t *reference = (reference_expression_t*) value;
3483 declaration_t *declaration = reference->declaration;
3484 if(declaration != NULL) {
3485 declaration->address_taken = 1;
3489 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3492 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3493 static expression_t *parse_##unexpression_type(unsigned precedence) \
3497 unary_expression_t *unary_expression \
3498 = allocate_ast_zero(sizeof(unary_expression[0])); \
3499 unary_expression->expression.type = EXPR_UNARY; \
3500 unary_expression->type = unexpression_type; \
3501 unary_expression->value = parse_sub_expression(precedence); \
3503 sfunc(unary_expression); \
3505 return (expression_t*) unary_expression; \
3508 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3509 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3510 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3511 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3512 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3513 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3514 semantic_unexpr_integer)
3515 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3517 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3520 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3522 static expression_t *parse_##unexpression_type(unsigned precedence, \
3523 expression_t *left) \
3525 (void) precedence; \
3528 unary_expression_t *unary_expression \
3529 = allocate_ast_zero(sizeof(unary_expression[0])); \
3530 unary_expression->expression.type = EXPR_UNARY; \
3531 unary_expression->type = unexpression_type; \
3532 unary_expression->value = left; \
3534 sfunc(unary_expression); \
3536 return (expression_t*) unary_expression; \
3539 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3541 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3544 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3546 /* TODO: handle complex + imaginary types */
3548 /* § 6.3.1.8 Usual arithmetic conversions */
3549 if(type_left == type_long_double || type_right == type_long_double) {
3550 return type_long_double;
3551 } else if(type_left == type_double || type_right == type_double) {
3553 } else if(type_left == type_float || type_right == type_float) {
3557 type_right = promote_integer(type_right);
3558 type_left = promote_integer(type_left);
3560 if(type_left == type_right)
3563 bool signed_left = is_type_signed(type_left);
3564 bool signed_right = is_type_signed(type_right);
3565 int rank_left = get_rank(type_left);
3566 int rank_right = get_rank(type_right);
3567 if(rank_left < rank_right) {
3568 if(signed_left == signed_right || !signed_right) {
3574 if(signed_left == signed_right || !signed_left) {
3582 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3584 expression_t *left = expression->left;
3585 expression_t *right = expression->right;
3586 type_t *orig_type_left = left->base.datatype;
3587 type_t *orig_type_right = right->base.datatype;
3589 if(orig_type_left == NULL || orig_type_right == NULL)
3592 type_t *type_left = skip_typeref(orig_type_left);
3593 type_t *type_right = skip_typeref(orig_type_right);
3595 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3596 /* TODO: improve error message */
3597 parser_print_error_prefix();
3598 fprintf(stderr, "operation needs arithmetic types\n");
3602 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3603 expression->left = create_implicit_cast(left, arithmetic_type);
3604 expression->right = create_implicit_cast(right, arithmetic_type);
3605 expression->expression.datatype = arithmetic_type;
3608 static void semantic_shift_op(binary_expression_t *expression)
3610 expression_t *left = expression->left;
3611 expression_t *right = expression->right;
3612 type_t *orig_type_left = left->base.datatype;
3613 type_t *orig_type_right = right->base.datatype;
3615 if(orig_type_left == NULL || orig_type_right == NULL)
3618 type_t *type_left = skip_typeref(orig_type_left);
3619 type_t *type_right = skip_typeref(orig_type_right);
3621 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3622 /* TODO: improve error message */
3623 parser_print_error_prefix();
3624 fprintf(stderr, "operation needs integer types\n");
3628 type_left = promote_integer(type_left);
3629 type_right = promote_integer(type_right);
3631 expression->left = create_implicit_cast(left, type_left);
3632 expression->right = create_implicit_cast(right, type_right);
3633 expression->expression.datatype = type_left;
3636 static void semantic_add(binary_expression_t *expression)
3638 expression_t *left = expression->left;
3639 expression_t *right = expression->right;
3640 type_t *orig_type_left = left->base.datatype;
3641 type_t *orig_type_right = right->base.datatype;
3643 if(orig_type_left == NULL || orig_type_right == NULL)
3646 type_t *type_left = skip_typeref(orig_type_left);
3647 type_t *type_right = skip_typeref(orig_type_right);
3650 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3651 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3652 expression->left = create_implicit_cast(left, arithmetic_type);
3653 expression->right = create_implicit_cast(right, arithmetic_type);
3654 expression->expression.datatype = arithmetic_type;
3656 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3657 expression->expression.datatype = type_left;
3658 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3659 expression->expression.datatype = type_right;
3661 parser_print_error_prefix();
3662 fprintf(stderr, "invalid operands to binary + (");
3663 print_type_quoted(orig_type_left);
3664 fprintf(stderr, ", ");
3665 print_type_quoted(orig_type_right);
3666 fprintf(stderr, ")\n");
3670 static void semantic_sub(binary_expression_t *expression)
3672 expression_t *left = expression->left;
3673 expression_t *right = expression->right;
3674 type_t *orig_type_left = left->base.datatype;
3675 type_t *orig_type_right = right->base.datatype;
3677 if(orig_type_left == NULL || orig_type_right == NULL)
3680 type_t *type_left = skip_typeref(orig_type_left);
3681 type_t *type_right = skip_typeref(orig_type_right);
3684 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3685 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3686 expression->left = create_implicit_cast(left, arithmetic_type);
3687 expression->right = create_implicit_cast(right, arithmetic_type);
3688 expression->expression.datatype = arithmetic_type;
3690 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3691 expression->expression.datatype = type_left;
3692 } else if(type_left->type == TYPE_POINTER &&
3693 type_right->type == TYPE_POINTER) {
3694 if(!pointers_compatible(type_left, type_right)) {
3695 parser_print_error_prefix();
3696 fprintf(stderr, "pointers to incompatible objects to binary - (");
3697 print_type_quoted(orig_type_left);
3698 fprintf(stderr, ", ");
3699 print_type_quoted(orig_type_right);
3700 fprintf(stderr, ")\n");
3702 expression->expression.datatype = type_ptrdiff_t;
3705 parser_print_error_prefix();
3706 fprintf(stderr, "invalid operands to binary - (");
3707 print_type_quoted(orig_type_left);
3708 fprintf(stderr, ", ");
3709 print_type_quoted(orig_type_right);
3710 fprintf(stderr, ")\n");
3714 static void semantic_comparison(binary_expression_t *expression)
3716 expression_t *left = expression->left;
3717 expression_t *right = expression->right;
3718 type_t *orig_type_left = left->base.datatype;
3719 type_t *orig_type_right = right->base.datatype;
3721 if(orig_type_left == NULL || orig_type_right == NULL)
3724 type_t *type_left = skip_typeref(orig_type_left);
3725 type_t *type_right = skip_typeref(orig_type_right);
3727 /* TODO non-arithmetic types */
3728 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3729 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3730 expression->left = create_implicit_cast(left, arithmetic_type);
3731 expression->right = create_implicit_cast(right, arithmetic_type);
3732 expression->expression.datatype = arithmetic_type;
3733 } else if (type_left->type == TYPE_POINTER &&
3734 type_right->type == TYPE_POINTER) {
3735 /* TODO check compatibility */
3736 } else if (type_left->type == TYPE_POINTER) {
3737 expression->right = create_implicit_cast(right, type_left);
3738 } else if (type_right->type == TYPE_POINTER) {
3739 expression->left = create_implicit_cast(left, type_right);
3741 type_error_incompatible("invalid operands in comparison",
3742 token.source_position, type_left, type_right);
3744 expression->expression.datatype = type_int;
3747 static void semantic_arithmetic_assign(binary_expression_t *expression)
3749 expression_t *left = expression->left;
3750 expression_t *right = expression->right;
3751 type_t *orig_type_left = left->base.datatype;
3752 type_t *orig_type_right = right->base.datatype;
3754 if(orig_type_left == NULL || orig_type_right == NULL)
3757 type_t *type_left = skip_typeref(orig_type_left);
3758 type_t *type_right = skip_typeref(orig_type_right);
3760 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3761 /* TODO: improve error message */
3762 parser_print_error_prefix();
3763 fprintf(stderr, "operation needs arithmetic types\n");
3767 /* combined instructions are tricky. We can't create an implicit cast on
3768 * the left side, because we need the uncasted form for the store.
3769 * The ast2firm pass has to know that left_type must be right_type
3770 * for the arithmeitc operation and create a cast by itself */
3771 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3772 expression->right = create_implicit_cast(right, arithmetic_type);
3773 expression->expression.datatype = type_left;
3776 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3778 expression_t *left = expression->left;
3779 expression_t *right = expression->right;
3780 type_t *orig_type_left = left->base.datatype;
3781 type_t *orig_type_right = right->base.datatype;
3783 if(orig_type_left == NULL || orig_type_right == NULL)
3786 type_t *type_left = skip_typeref(orig_type_left);
3787 type_t *type_right = skip_typeref(orig_type_right);
3789 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3790 /* combined instructions are tricky. We can't create an implicit cast on
3791 * the left side, because we need the uncasted form for the store.
3792 * The ast2firm pass has to know that left_type must be right_type
3793 * for the arithmeitc operation and create a cast by itself */
3794 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3795 expression->right = create_implicit_cast(right, arithmetic_type);
3796 expression->expression.datatype = type_left;
3797 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3798 expression->expression.datatype = type_left;
3800 parser_print_error_prefix();
3801 fputs("Incompatible types ", stderr);
3802 print_type_quoted(orig_type_left);
3803 fputs(" and ", stderr);
3804 print_type_quoted(orig_type_right);
3805 fputs(" in assignment\n", stderr);
3810 static void semantic_logical_op(binary_expression_t *expression)
3812 expression_t *left = expression->left;
3813 expression_t *right = expression->right;
3814 type_t *orig_type_left = left->base.datatype;
3815 type_t *orig_type_right = right->base.datatype;
3817 if(orig_type_left == NULL || orig_type_right == NULL)
3820 type_t *type_left = skip_typeref(orig_type_left);
3821 type_t *type_right = skip_typeref(orig_type_right);
3823 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3824 /* TODO: improve error message */
3825 parser_print_error_prefix();
3826 fprintf(stderr, "operation needs scalar types\n");
3830 expression->expression.datatype = type_int;
3833 static bool has_const_fields(type_t *type)
3840 static void semantic_binexpr_assign(binary_expression_t *expression)
3842 expression_t *left = expression->left;
3843 type_t *orig_type_left = left->base.datatype;
3845 if(orig_type_left == NULL)
3848 type_t *type_left = revert_automatic_type_conversion(left);
3849 type_left = skip_typeref(orig_type_left);
3851 /* must be a modifiable lvalue */
3852 if (type_left->type == TYPE_ARRAY) {
3853 parser_print_error_prefix();
3854 fprintf(stderr, "Cannot assign to arrays ('");
3855 print_expression(left);
3856 fprintf(stderr, "')\n");
3859 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3860 parser_print_error_prefix();
3861 fprintf(stderr, "assignment to readonly location '");
3862 print_expression(left);
3863 fprintf(stderr, "' (type ");
3864 print_type_quoted(orig_type_left);
3865 fprintf(stderr, ")\n");
3868 if(is_type_incomplete(type_left)) {
3869 parser_print_error_prefix();
3870 fprintf(stderr, "left-hand side of assignment '");
3871 print_expression(left);
3872 fprintf(stderr, "' has incomplete type ");
3873 print_type_quoted(orig_type_left);
3874 fprintf(stderr, "\n");
3877 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3878 parser_print_error_prefix();
3879 fprintf(stderr, "can't assign to '");
3880 print_expression(left);
3881 fprintf(stderr, "' because compound type ");
3882 print_type_quoted(orig_type_left);
3883 fprintf(stderr, " has readonly fields\n");
3887 semantic_assign(orig_type_left, &expression->right, "assignment");
3889 expression->expression.datatype = orig_type_left;
3892 static void semantic_comma(binary_expression_t *expression)
3894 expression->expression.datatype = expression->right->base.datatype;
3897 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3898 static expression_t *parse_##binexpression_type(unsigned precedence, \
3899 expression_t *left) \
3903 expression_t *right = parse_sub_expression(precedence + lr); \
3905 binary_expression_t *binexpr \
3906 = allocate_ast_zero(sizeof(binexpr[0])); \
3907 binexpr->expression.type = EXPR_BINARY; \
3908 binexpr->type = binexpression_type; \
3909 binexpr->left = left; \
3910 binexpr->right = right; \
3913 return (expression_t*) binexpr; \
3916 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3917 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3918 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3919 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3920 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3921 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3922 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3923 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3924 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3925 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3926 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3927 semantic_comparison, 1)
3928 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3929 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3930 semantic_comparison, 1)
3931 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3932 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3933 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3934 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3935 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3936 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3937 semantic_shift_op, 1)
3938 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3939 semantic_shift_op, 1)
3940 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3941 semantic_arithmetic_addsubb_assign, 0)
3942 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3943 semantic_arithmetic_addsubb_assign, 0)
3944 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3945 semantic_arithmetic_assign, 0)
3946 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3947 semantic_arithmetic_assign, 0)
3948 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3949 semantic_arithmetic_assign, 0)
3950 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3951 semantic_arithmetic_assign, 0)
3952 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3953 semantic_arithmetic_assign, 0)
3954 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3955 semantic_arithmetic_assign, 0)
3956 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3957 semantic_arithmetic_assign, 0)
3958 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3959 semantic_arithmetic_assign, 0)
3961 static expression_t *parse_sub_expression(unsigned precedence)
3963 if(token.type < 0) {
3964 return expected_expression_error();
3967 expression_parser_function_t *parser
3968 = &expression_parsers[token.type];
3969 source_position_t source_position = token.source_position;
3972 if(parser->parser != NULL) {
3973 left = parser->parser(parser->precedence);
3975 left = parse_primary_expression();
3977 assert(left != NULL);
3978 left->base.source_position = source_position;
3981 if(token.type < 0) {
3982 return expected_expression_error();
3985 parser = &expression_parsers[token.type];
3986 if(parser->infix_parser == NULL)
3988 if(parser->infix_precedence < precedence)
3991 left = parser->infix_parser(parser->infix_precedence, left);
3993 assert(left != NULL);
3994 assert(left->type != EXPR_UNKNOWN);
3995 left->base.source_position = source_position;
4001 static expression_t *parse_expression(void)
4003 return parse_sub_expression(1);
4008 static void register_expression_parser(parse_expression_function parser,
4009 int token_type, unsigned precedence)
4011 expression_parser_function_t *entry = &expression_parsers[token_type];
4013 if(entry->parser != NULL) {
4014 fprintf(stderr, "for token ");
4015 print_token_type(stderr, (token_type_t) token_type);
4016 fprintf(stderr, "\n");
4017 panic("trying to register multiple expression parsers for a token");
4019 entry->parser = parser;
4020 entry->precedence = precedence;
4023 static void register_expression_infix_parser(
4024 parse_expression_infix_function parser, int token_type,
4025 unsigned precedence)
4027 expression_parser_function_t *entry = &expression_parsers[token_type];
4029 if(entry->infix_parser != NULL) {
4030 fprintf(stderr, "for token ");
4031 print_token_type(stderr, (token_type_t) token_type);
4032 fprintf(stderr, "\n");
4033 panic("trying to register multiple infix expression parsers for a "
4036 entry->infix_parser = parser;
4037 entry->infix_precedence = precedence;
4040 static void init_expression_parsers(void)
4042 memset(&expression_parsers, 0, sizeof(expression_parsers));
4044 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4045 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4046 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4047 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4048 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4049 T_GREATERGREATER, 16);
4050 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4051 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4052 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4053 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4054 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4055 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4056 T_GREATEREQUAL, 14);
4057 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4058 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4059 T_EXCLAMATIONMARKEQUAL, 13);
4060 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4061 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4062 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4063 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4064 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4065 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4066 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4067 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4068 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4069 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4070 T_ASTERISKEQUAL, 2);
4071 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4072 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4074 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4075 T_LESSLESSEQUAL, 2);
4076 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4077 T_GREATERGREATEREQUAL, 2);
4078 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4080 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4082 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4085 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4087 register_expression_infix_parser(parse_array_expression, '[', 30);
4088 register_expression_infix_parser(parse_call_expression, '(', 30);
4089 register_expression_infix_parser(parse_select_expression, '.', 30);
4090 register_expression_infix_parser(parse_select_expression,
4091 T_MINUSGREATER, 30);
4092 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4094 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4097 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4098 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4099 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4100 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4101 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4102 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4103 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4104 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4105 register_expression_parser(parse_sizeof, T_sizeof, 25);
4106 register_expression_parser(parse_extension, T___extension__, 25);
4107 register_expression_parser(parse_builtin_classify_type,
4108 T___builtin_classify_type, 25);
4112 static statement_t *parse_case_statement(void)
4115 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4116 label->statement.type = STATEMENT_CASE_LABEL;
4117 label->statement.source_position = token.source_position;
4119 label->expression = parse_expression();
4122 label->label_statement = parse_statement();
4124 return (statement_t*) label;
4127 static statement_t *parse_default_statement(void)
4131 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4132 label->statement.type = STATEMENT_CASE_LABEL;
4133 label->statement.source_position = token.source_position;
4136 label->label_statement = parse_statement();
4138 return (statement_t*) label;
4141 static declaration_t *get_label(symbol_t *symbol)
4143 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4144 assert(current_function != NULL);
4145 /* if we found a label in the same function, then we already created the
4147 if(candidate != NULL
4148 && candidate->parent_context == ¤t_function->context) {
4152 /* otherwise we need to create a new one */
4153 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4154 declaration->namespc = NAMESPACE_LABEL;
4155 declaration->symbol = symbol;
4157 label_push(declaration);
4162 static statement_t *parse_label_statement(void)
4164 assert(token.type == T_IDENTIFIER);
4165 symbol_t *symbol = token.v.symbol;
4168 declaration_t *label = get_label(symbol);
4170 /* if source position is already set then the label is defined twice,
4171 * otherwise it was just mentioned in a goto so far */
4172 if(label->source_position.input_name != NULL) {
4173 parser_print_error_prefix();
4174 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4175 parser_print_error_prefix_pos(label->source_position);
4176 fprintf(stderr, "previous definition of '%s' was here\n",
4179 label->source_position = token.source_position;
4182 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4184 label_statement->statement.type = STATEMENT_LABEL;
4185 label_statement->statement.source_position = token.source_position;
4186 label_statement->label = label;
4190 if(token.type == '}') {
4191 parse_error("label at end of compound statement");
4192 return (statement_t*) label_statement;
4194 label_statement->label_statement = parse_statement();
4197 return (statement_t*) label_statement;
4200 static statement_t *parse_if(void)
4204 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4205 statement->statement.type = STATEMENT_IF;
4206 statement->statement.source_position = token.source_position;
4209 statement->condition = parse_expression();
4212 statement->true_statement = parse_statement();
4213 if(token.type == T_else) {
4215 statement->false_statement = parse_statement();
4218 return (statement_t*) statement;
4221 static statement_t *parse_switch(void)
4225 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4226 statement->statement.type = STATEMENT_SWITCH;
4227 statement->statement.source_position = token.source_position;
4230 statement->expression = parse_expression();
4232 statement->body = parse_statement();
4234 return (statement_t*) statement;
4237 static statement_t *parse_while(void)
4241 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4242 statement->statement.type = STATEMENT_WHILE;
4243 statement->statement.source_position = token.source_position;
4246 statement->condition = parse_expression();
4248 statement->body = parse_statement();
4250 return (statement_t*) statement;
4253 static statement_t *parse_do(void)
4257 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4258 statement->statement.type = STATEMENT_DO_WHILE;
4259 statement->statement.source_position = token.source_position;
4261 statement->body = parse_statement();
4264 statement->condition = parse_expression();
4268 return (statement_t*) statement;
4271 static statement_t *parse_for(void)
4275 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4276 statement->statement.type = STATEMENT_FOR;
4277 statement->statement.source_position = token.source_position;
4281 int top = environment_top();
4282 context_t *last_context = context;
4283 set_context(&statement->context);
4285 if(token.type != ';') {
4286 if(is_declaration_specifier(&token, false)) {
4287 parse_declaration();
4289 statement->initialisation = parse_expression();
4296 if(token.type != ';') {
4297 statement->condition = parse_expression();
4300 if(token.type != ')') {
4301 statement->step = parse_expression();
4304 statement->body = parse_statement();
4306 assert(context == &statement->context);
4307 set_context(last_context);
4308 environment_pop_to(top);
4310 return (statement_t*) statement;
4313 static statement_t *parse_goto(void)
4317 if(token.type != T_IDENTIFIER) {
4318 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4322 symbol_t *symbol = token.v.symbol;
4325 declaration_t *label = get_label(symbol);
4327 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4329 statement->statement.type = STATEMENT_GOTO;
4330 statement->statement.source_position = token.source_position;
4332 statement->label = label;
4336 return (statement_t*) statement;
4339 static statement_t *parse_continue(void)
4344 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4345 statement->type = STATEMENT_CONTINUE;
4346 statement->base.source_position = token.source_position;
4351 static statement_t *parse_break(void)
4356 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4357 statement->type = STATEMENT_BREAK;
4358 statement->base.source_position = token.source_position;
4363 static statement_t *parse_return(void)
4367 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4369 statement->statement.type = STATEMENT_RETURN;
4370 statement->statement.source_position = token.source_position;
4372 assert(current_function->type->type == TYPE_FUNCTION);
4373 function_type_t *function_type = ¤t_function->type->function;
4374 type_t *return_type = function_type->result_type;
4376 expression_t *return_value = NULL;
4377 if(token.type != ';') {
4378 return_value = parse_expression();
4382 if(return_type == NULL)
4383 return (statement_t*) statement;
4385 return_type = skip_typeref(return_type);
4387 if(return_value != NULL) {
4388 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4390 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4391 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4392 parse_warning("'return' with a value, in function returning void");
4393 return_value = NULL;
4395 if(return_type != NULL) {
4396 semantic_assign(return_type, &return_value, "'return'");
4400 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4401 parse_warning("'return' without value, in function returning "
4405 statement->return_value = return_value;
4407 return (statement_t*) statement;
4410 static statement_t *parse_declaration_statement(void)
4412 declaration_t *before = last_declaration;
4414 declaration_statement_t *statement
4415 = allocate_ast_zero(sizeof(statement[0]));
4416 statement->statement.type = STATEMENT_DECLARATION;
4417 statement->statement.source_position = token.source_position;
4419 declaration_specifiers_t specifiers;
4420 memset(&specifiers, 0, sizeof(specifiers));
4421 parse_declaration_specifiers(&specifiers);
4423 if(token.type == ';') {
4426 parse_init_declarators(&specifiers);
4429 if(before == NULL) {
4430 statement->declarations_begin = context->declarations;
4432 statement->declarations_begin = before->next;
4434 statement->declarations_end = last_declaration;
4436 return (statement_t*) statement;
4439 static statement_t *parse_expression_statement(void)
4441 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4442 statement->statement.type = STATEMENT_EXPRESSION;
4443 statement->statement.source_position = token.source_position;
4445 statement->expression = parse_expression();
4449 return (statement_t*) statement;
4452 static statement_t *parse_statement(void)
4454 statement_t *statement = NULL;
4456 /* declaration or statement */
4457 switch(token.type) {
4459 statement = parse_case_statement();
4463 statement = parse_default_statement();
4467 statement = parse_compound_statement();
4471 statement = parse_if();
4475 statement = parse_switch();
4479 statement = parse_while();
4483 statement = parse_do();
4487 statement = parse_for();
4491 statement = parse_goto();
4495 statement = parse_continue();
4499 statement = parse_break();
4503 statement = parse_return();
4512 if(look_ahead(1)->type == ':') {
4513 statement = parse_label_statement();
4517 if(is_typedef_symbol(token.v.symbol)) {
4518 statement = parse_declaration_statement();
4522 statement = parse_expression_statement();
4525 case T___extension__:
4526 /* this can be a prefix to a declaration or an expression statement */
4527 /* we simply eat it now and parse the rest with tail recursion */
4530 } while(token.type == T___extension__);
4531 statement = parse_statement();
4535 statement = parse_declaration_statement();
4539 statement = parse_expression_statement();
4543 assert(statement == NULL
4544 || statement->base.source_position.input_name != NULL);
4549 static statement_t *parse_compound_statement(void)
4551 compound_statement_t *compound_statement
4552 = allocate_ast_zero(sizeof(compound_statement[0]));
4553 compound_statement->statement.type = STATEMENT_COMPOUND;
4554 compound_statement->statement.source_position = token.source_position;
4558 int top = environment_top();
4559 context_t *last_context = context;
4560 set_context(&compound_statement->context);
4562 statement_t *last_statement = NULL;
4564 while(token.type != '}' && token.type != T_EOF) {
4565 statement_t *statement = parse_statement();
4566 if(statement == NULL)
4569 if(last_statement != NULL) {
4570 last_statement->base.next = statement;
4572 compound_statement->statements = statement;
4575 while(statement->base.next != NULL)
4576 statement = statement->base.next;
4578 last_statement = statement;
4581 if(token.type != '}') {
4582 parser_print_error_prefix_pos(
4583 compound_statement->statement.source_position);
4584 fprintf(stderr, "end of file while looking for closing '}'\n");
4588 assert(context == &compound_statement->context);
4589 set_context(last_context);
4590 environment_pop_to(top);
4592 return (statement_t*) compound_statement;
4595 static translation_unit_t *parse_translation_unit(void)
4597 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4599 assert(global_context == NULL);
4600 global_context = &unit->context;
4602 assert(context == NULL);
4603 set_context(&unit->context);
4605 while(token.type != T_EOF) {
4606 parse_declaration();
4609 assert(context == &unit->context);
4611 last_declaration = NULL;
4613 assert(global_context == &unit->context);
4614 global_context = NULL;
4619 translation_unit_t *parse(void)
4621 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4622 label_stack = NEW_ARR_F(stack_entry_t, 0);
4623 found_error = false;
4625 type_set_output(stderr);
4626 ast_set_output(stderr);
4628 lookahead_bufpos = 0;
4629 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4632 translation_unit_t *unit = parse_translation_unit();
4634 DEL_ARR_F(environment_stack);
4635 DEL_ARR_F(label_stack);
4643 void init_parser(void)
4645 init_expression_parsers();
4646 obstack_init(&temp_obst);
4648 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4649 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4650 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4651 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4652 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4653 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4654 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4655 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4656 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4657 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4660 void exit_parser(void)
4662 obstack_free(&temp_obst, NULL);